com.kyakujin.android.autoeco.ui.BatterySettingActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.kyakujin.android.autoeco.ui.BatterySettingActivity.java

Source

/*
 * Copyright 2013 Yoshihiro Miyama
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.kyakujin.android.autoeco.ui;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.kyakujin.android.autoeco.Conf;
import com.kyakujin.android.autoeco.R;
import com.kyakujin.android.autoeco.db.AutoEcoContract.EcoTbl;
import com.kyakujin.android.autoeco.db.dao.EcoDAO;
import com.kyakujin.android.autoeco.db.dao.MappingDAO;
import com.kyakujin.android.autoeco.db.dao.MappingModel;

/**
 * ???<br>
 * ??()???
 * {@link BatteryFragment} <br>
 * {@link EcoFragment}
 */
public class BatterySettingActivity extends FragmentActivity {

    private Uri mEcoUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_battery);

        int batteryId = 0;
        batteryId = getIntent().getExtras().getInt(Conf.SHARED_BATTERYID);

        connectToMapping(batteryId);

        FragmentManager fm = this.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        // ?Fragment
        Bundle bundle = new Bundle();
        bundle.putInt(Conf.SHARED_BATTERYID, batteryId);
        BatteryFragment batteryFragment = BatteryFragment.newInstance();
        batteryFragment.setArguments(bundle);

        // Fragment
        bundle.putInt(Conf.SHARED_ECOID, Integer.valueOf(mEcoUri.getLastPathSegment()));
        EcoFragment ecoFragment = EcoFragment.newInstance();
        ecoFragment.setArguments(bundle);

        ft.add(R.id.batteryFragmentContainer, batteryFragment, Conf.FRAGTAG_BATTERY);
        ft.add(R.id.ecoFragmentContainer, ecoFragment, Conf.FRAGTAG_ECO);
        //ft.addToBackStack(null);
        ft.commit();

    }

    private int connectToMapping(int batteryId) {
        int mappingId = 0;

        if (batteryId == 0)
            return mappingId;

        // Mapping???????
        MappingDAO mappingDao = new MappingDAO(this);
        mappingId = mappingDao.searchMappingIdByBatteryId(batteryId);

        // Mapping????
        if (mappingId == 0) {
            // ??
            EcoDAO ecoDao = new EcoDAO(this);
            mEcoUri = ecoDao.insertDefaultEco();

            // Mapping??
            MappingModel mappingModel = new MappingModel();
            mappingModel.setEcoid(Integer.valueOf(mEcoUri.getLastPathSegment()));
            mappingModel.setSchedid(0);
            mappingModel.setManualid(0);
            mappingModel.setBatteryid(batteryId);
            mappingId = Integer.valueOf(mappingDao.insertMapping(mappingModel).getLastPathSegment());

        } else {
            // Mapping???????
            mEcoUri = Uri.withAppendedPath(EcoTbl.CONTENT_URI,
                    String.valueOf(mappingDao.searchEcoIdByBatteryId(batteryId)));
        }

        return mappingId;
    }
}