com.wondereight.sensioair.gcm.GcmMain.java Source code

Java tutorial

Introduction

Here is the source code for com.wondereight.sensioair.gcm.GcmMain.java

Source

/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * 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.wondereight.sensioair.gcm;
    
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
    
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.wondereight.sensioair.R;
    
public class MainActivity extends AppCompatActivity {
    
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private static final String TAG = "MainActivity";
    
private BroadcastReceiver mRegistrationBroadcastReceiver;
private ProgressBar mRegistrationProgressBar;
private TextView mInformationTextView;
private boolean isReceiverRegistered;
    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
            SharedPreferences sharedPreferences =
                    PreferenceManager.getDefaultSharedPreferences(context);
            boolean sentToken = sharedPreferences
                    .getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
            if (sentToken) {
                mInformationTextView.setText(getString(R.string.gcm_send_message));
            } else {
                mInformationTextView.setText(getString(R.string.token_error_message));
            }
        }
    };
    mInformationTextView = (TextView) findViewById(R.id.informationTextView);
    
    // Registering BroadcastReceiver
    registerReceiver();
    
    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
}
    
@Override
protected void onResume() {
    super.onResume();
    registerReceiver();
}
    
@Override
protected void onPause() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
    isReceiverRegistered = false;
    super.onPause();
}
    
private void registerReceiver(){
    if(!isReceiverRegistered) {
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
                new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
        isReceiverRegistered = true;
    }
}
*/
/**
 * Check the device to make sure it has the Google Play Services APK. If
 * it doesn't, display a dialog that allows users to download the APK from
 * the Google Play Store or enable it in the device's system settings.
 *//*
        
    private boolean checkPlayServices() {
     GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
     int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
     if (resultCode != ConnectionResult.SUCCESS) {
         if (apiAvailability.isUserResolvableError(resultCode)) {
             apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
                     .show();
         } else {
             Log.i(TAG, "This device is not supported.");
             finish();
         }
         return false;
     }
     return true;
    }
        
    }
    */
package com.wondereight.sensioair.gcm;

import android.app.Activity;
import android.app.Application;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import com.wondereight.sensioair.R;

import java.io.IOException;

public class GcmMain {
    private static String LOG_TAG = "GcmMain";
    private static GcmMain mInstance = null;

    /* for GCM Register and tocken*/
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;

    private BroadcastReceiver mRegistrationBroadcastReceiver;
    private boolean isReceiverRegistered;

    public static GcmMain GetInstance() {
        if (mInstance == null) {
            mInstance = new GcmMain();
            mInstance.init();
        }
        return mInstance;
    }

    public void init() {

        isReceiverRegistered = false;
        mRegistrationBroadcastReceiver = null;

    }

    public void registerReceiver(Context context, BroadcastReceiver receiver) {
        mRegistrationBroadcastReceiver = receiver;
        if (!isReceiverRegistered && mRegistrationBroadcastReceiver != null) {
            LocalBroadcastManager.getInstance(context).registerReceiver(mRegistrationBroadcastReceiver,
                    new IntentFilter(SAPreferences.REGISTRATION_COMPLETE));
            isReceiverRegistered = true;
        }
    }

    public void registerReceiver(Context context) {
        registerReceiver(context, mRegistrationBroadcastReceiver);
    }

    public void unregisterReceiver(Context context) {
        LocalBroadcastManager.getInstance(context).unregisterReceiver(mRegistrationBroadcastReceiver);
        isReceiverRegistered = false;
    }

    //    public void deleteGCMToken(Context context){
    //        final InstanceID instanceID = InstanceID.getInstance(context);
    //        final String authorizedEntity = context.getString(R.string.gcm_defaultSenderId);
    //        Log.i(LOG_TAG, "To delete token :: instanceID: " + instanceID + ", authorizedEntity:" + authorizedEntity);
    //
    //        Thread thread = new Thread(new Runnable() {
    //            @Override
    //            public void run() {
    //                try {
    //                    instanceID.deleteToken(authorizedEntity, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
    //                    Log.i(LOG_TAG, "GCM Token deleted to success");
    //                } catch (Exception e) {
    //                    e.printStackTrace();
    //                    Log.i(LOG_TAG, "GCM Token deleted to failure");
    //                }
    //                // [END delete_token]
    //
    //            }
    //        });
    //
    //        thread.start();
    //
    //    }

    public boolean checkPlayServices(Activity activity) {
        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (apiAvailability.isUserResolvableError(resultCode)) {
                apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
                Log.i(LOG_TAG, "Google Play Services Version Error.");
            } else {

                Log.i(LOG_TAG, "This device is not supported with Google Play Services.");
            }
            return false;
        }
        Log.i(LOG_TAG, "This device is supported the Google Play Services.");
        return true;
    }

    public void removeLocalNotification(Context context) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        //notificationManager.cancel( 0 );
        notificationManager.cancelAll();
    }
}