com.cloudbase.cbhelperdemo.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.cloudbase.cbhelperdemo.MainActivity.java

Source

/* Copyright (C) 2012 cloudbase.io
     
 This program is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License, version 2, as published by
 the Free Software Foundation.
     
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
     
 You should have received a copy of the GNU General Public License
 along with this program; see the file COPYING.  If not, write to the Free
 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 02111-1307, USA.
 */
package com.cloudbase.cbhelperdemo;

import com.cloudbase.*;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import com.google.android.gcm.GCMRegistrar;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    /**
     * The serialization (saved instance state) Bundle key representing the
     * current tab position.
     */
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
    public static CBHelper helper;
    public static String c2dmRegistrationId;
    public static String gcmRegistrationId;

    public void registerForNotifications(String cgmId) {
        // register for GCM notifications
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        Log.d("CloudBase", "tried registration with id " + regId);
        if (regId.equals("")) {
            GCMRegistrar.register(this, cgmId);
        } else {
            Log.v("CloudBase", "Already registered");
            MainActivity.gcmRegistrationId = regId;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar to show tabs.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // For each of the sections in the app, add a tab to the action bar.
        actionBar.addTab(actionBar.newTab().setText(R.string.title_section1).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.title_section2).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.title_section3).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.title_section4).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.title_section5).setTabListener(this));
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the previously serialized current tab position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Serialize the current tab position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

        if (tab.getPosition() == 0) { // settings
            SettingsScreen settings = new SettingsScreen();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, settings).commit();
        }
        if (tab.getPosition() == 1) { // log
            LogScreen log = new LogScreen();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, log).commit();
        }
        if (tab.getPosition() == 2) { // log
            DataScreen data = new DataScreen();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, data).commit();
        }
        if (tab.getPosition() == 3) { // notifications
            NotificationScreen notif = new NotificationScreen();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, notif).commit();
        }
        if (tab.getPosition() == 4) { // notifications
            FunctionScreen func = new FunctionScreen();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, func).commit();
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    /*
     * Push Notification methods
     * These handle the registration for push notifications
     */
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
            handleRegistration(context, intent);
        } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
            handleMessage(context, intent);
        }
    }

    private void handleRegistration(Context context, Intent intent) {
        String registration = intent.getStringExtra("registration_id");
        if (intent.getStringExtra("error") != null) {
            // Registration failed, should try again later.
        } else if (intent.getStringExtra("unregistered") != null) {
            // unregistration done, new messages from the authorized sender will be rejected
        } else if (registration != null) {
            // Send the registration ID to the 3rd party site that is sending the messages.
            // This should be done in a separate thread.
            // When done, remember that all registration is done. 
            this.c2dmRegistrationId = registration;
            //this.helper.notificationSubscribeDevice(registration, "test");
        }
    }

    private void handleMessage(Context context, Intent intent) {
        Log.d("DEMOAPP", "received message");
    }
}