Android Open Source - tehran_traffic_map Cloud Message






From Project

Back to project page tehran_traffic_map.

License

The source code is released under:

MIT License

If you think the Android project tehran_traffic_map listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.tehran.traffic.models;
/*  ww  w.  java 2s  .  c  o  m*/
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.tehran.traffic.ui.MainActivity;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Mohsen on 11/4/14.
 */
public class CloudMessage {

    public static final String EXTRA_MESSAGE = "message";
    public static final String PROPERTY_REG_ID = "registration_id";
    private static final String PROPERTY_APP_VERSION = "appVersion";
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    private static final String TAG = CloudMessage.class.getName();

    static String SENDER_ID = "247656184528";
    static String URL = "http://mirhoseini.com";
    static GoogleCloudMessaging gcm;
    static AtomicInteger msgId = new AtomicInteger();
    static SharedPreferences prefs;
    static Context context;
    static String regid;
    static String responseBody;


    public static void startGCM(Activity activity) {


        context = activity.getApplicationContext();

        if (checkPlayServices(activity)) {
            gcm = GoogleCloudMessaging.getInstance(activity);
            regid = getRegistrationId(context);
            if (regid.isEmpty()) {
                new registerInBackground().execute();
            }
        } else {
            Log.i("Px GCM", "No valid Google Play Services APK found.");
        }
    }

    public static boolean checkPlayServices(Activity activity) {
        int resultCode = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(activity);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, activity,
                        PLAY_SERVICES_RESOLUTION_REQUEST)
                        .show();
            } else {
                Log.i(TAG, "This device is not supported.");
                activity.finish();
            }
            return false;
        }
        return true;
    }


    private static String getRegistrationId(Context context) {
        final SharedPreferences prefs = getGCMPreferences(context);
        String registrationId = prefs.getString(PROPERTY_REG_ID, "");
        if (registrationId.isEmpty()) {
            Log.i("Px GCM", "Registration not found.");
            return "";
        }

        int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
        int currentVersion = getAppVersion(context);
        if (registeredVersion != currentVersion) {
            Log.i("Px GCM", "App version changed.");
            return "";
        }
        return registrationId;
    }

    private static SharedPreferences getGCMPreferences(Context context) {
        return context.getSharedPreferences(MainActivity.class.getSimpleName(),
                Context.MODE_PRIVATE);
    }

    private static int getAppVersion(Context context) {
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException("Could not get package name: " + e);
        }
    }

    private static void sendRegistrationIdToBackend() {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(URL + "/?regId=" + regid);
        try {
            HttpResponse response = httpclient.execute(httppost);
            responseBody = EntityUtils.toString(response.getEntity());

        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }
    }

    private static void storeRegistrationId(Context context, String regId) {
        final SharedPreferences prefs = getGCMPreferences(context);
        int appVersion = getAppVersion(context);
        Log.i("Px GCM", "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PROPERTY_REG_ID, regId);
        editor.putInt(PROPERTY_APP_VERSION, appVersion);
        editor.commit();
    }

    private static class registerInBackground extends AsyncTask<Void, Integer, String> {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }
                regid = gcm.register(SENDER_ID);
                msg = "Device registered \n registration ID=" + regid;
                sendRegistrationIdToBackend();

                storeRegistrationId(context, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            //mDisplay.append(msg + "\n");
        }
    }
}




Java Source Code List

com.tehran.traffic.models.CloudMessage.java
com.tehran.traffic.models.RoadData.java
com.tehran.traffic.network.DataLoader.java
com.tehran.traffic.service.GcmBroadcastReceiver.java
com.tehran.traffic.service.GcmIntentService.java
com.tehran.traffic.ui.MainActivity.java
com.tehran.traffic.ui.NavigationView.java
com.tehran.traffic.ui.TouchImageView.java
com.tehran.traffic.util.Base64DecoderException.java
com.tehran.traffic.util.Base64.java
com.tehran.traffic.util.IabException.java
com.tehran.traffic.util.IabHelper.java
com.tehran.traffic.util.IabResult.java
com.tehran.traffic.util.Inventory.java
com.tehran.traffic.util.Purchase.java
com.tehran.traffic.util.Security.java
com.tehran.traffic.util.SkuDetails.java