jp.co.conit.sss.sn.ex1.activity.SettingsActivity.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.conit.sss.sn.ex1.activity.SettingsActivity.java

Source

/*
 * Copyright (C) 2012 CONIT Co., Ltd.
 *
 * 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 jp.co.conit.sss.sn.ex1.activity;

import jp.co.conit.sss.sn.ex1.R;
import jp.co.conit.sss.sn.ex1.fragment.SettingsFragment;
import jp.co.conit.sss.sn.ex1.service.AbstRegistObserver;
import jp.co.conit.sss.sn.ex1.service.ResponseHandler;
import jp.co.conit.sss.sn.ex1.service.SendMessageIdIntentService;
import jp.co.conit.sss.sn.ex1.util.PrefrerencesUtil;
import jp.co.conit.sss.sn.ex1.util.StringUtil;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.google.android.gcm.GCMRegistrar;

/**
 * ?????Activity??
 * 
 * @author conit
 */
public class SettingsActivity extends FragmentActivity {

    /** ?? */
    private static final int DEFAULT_TEXT_SIZE = 20;

    private RegistObserver mRegistObserver;

    private class RegistObserver extends AbstRegistObserver {

        @Override
        public void onRegisted() {
            FragmentManager fm = getSupportFragmentManager();
            SettingsFragment f = (SettingsFragment) fm.findFragmentById(R.id.frame_settings);
            if (f != null) {
                f.setRegistBtnState();
            }
        }

    }

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

        setContentView(R.layout.activity_settings);
        mRegistObserver = new RegistObserver();

        init(getIntent());
    }

    /**
     * ????
     * 
     * @param intent
     */
    private void init(Intent intent) {
        int textSize = DEFAULT_TEXT_SIZE;

        Bundle extras = intent.getExtras();
        if (extras != null) {
            // message_id??
            String registrationId = PrefrerencesUtil.getString(getApplicationContext(), "regist_id", "");
            String mid = extras.getString("mid");
            if (!StringUtil.isEmpty(registrationId) && !StringUtil.isEmpty(mid)) {
                Intent serviceintent = new Intent(getApplicationContext(), SendMessageIdIntentService.class);
                serviceintent.putExtra("mid", mid);
                startService(serviceintent);
            }
            textSize = extras.getInt("text_size");
        }
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        SettingsFragment f = (SettingsFragment) fm.findFragmentById(R.id.frame_settings);
        if (f == null) {
            SettingsFragment fragment = SettingsFragment.newInstance(textSize);
            transaction.replace(R.id.frame_settings, fragment);
            transaction.commit();
        } else {
            f.setTextSize(textSize);
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        ResponseHandler.register(mRegistObserver);
    }

    @Override
    protected void onStop() {
        super.onStop();
        ResponseHandler.unregister(mRegistObserver);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        GCMRegistrar.onDestroy(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        init(intent);
    }

}