Android Open Source - taxitwin-app Gcm Connector






From Project

Back to project page taxitwin-app.

License

The source code is released under:

GNU General Public License

If you think the Android project taxitwin-app 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 kimle.michal.android.taxitwin.gcm;
//www .  jav  a  2 s. c o m
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import kimle.michal.android.taxitwin.R;

public class GcmConnector {

    private final Context context;
    private GoogleCloudMessaging gcm;
    private static final int TTL = 180;
    private static final String SENDER_ID = "275458664476";
    private static final String GCM_SERVER = "@gcm.googleapis.com";
    private static final String LOG = "GcmConnector";
    private final AtomicInteger messageId;

    public GcmConnector(Context context) {
        this.context = context;
        messageId = new AtomicInteger(new Random(System.currentTimeMillis()).nextInt(Integer.MAX_VALUE));
    }

    public void connect() {
        if (gcm == null) {
            gcm = GoogleCloudMessaging.getInstance(context);
        }
        if (!isRegistred()) {
            register();
        }
    }

    private boolean isRegistred() {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
        String gcmId = pref.getString(context.getResources().getString(R.string.pref_gcm_id), "");
        return !gcmId.isEmpty();
    }

    private void register() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    String regid = gcm.register(SENDER_ID);
                    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
                    SharedPreferences.Editor editor = pref.edit();
                    editor.putString(context.getResources().getString(R.string.pref_gcm_id), regid);
                    editor.commit();
                } catch (IOException ex) {
                    Log.e(LOG, ex.getMessage());
                }

                return null;
            }
        }.execute(null, null, null);
    }

    public void send(Bundle data) {
        connect();
        new AsyncTask<Bundle, Void, Void>() {
            @Override
            protected Void doInBackground(Bundle... data) {
                try {
                    String id = Integer.toString(messageId.incrementAndGet());
                    gcm.send(SENDER_ID + GCM_SERVER, id, TTL, data[0]);
                } catch (IOException ex) {
                    Log.e(LOG, ex.getMessage());
                }

                return null;
            }
        }.execute(data, null, null);
    }
}




Java Source Code List

kimle.michal.android.taxitwin.activity.LauncherActivity.java
kimle.michal.android.taxitwin.activity.MainActivity.java
kimle.michal.android.taxitwin.activity.MyTaxiTwinActivity.java
kimle.michal.android.taxitwin.activity.OfferDetailActivity.java
kimle.michal.android.taxitwin.activity.ResponseDetailActivity.java
kimle.michal.android.taxitwin.activity.ResponsesActivity.java
kimle.michal.android.taxitwin.adapter.TaxiTwinPlacesAutoCompleteAdapter.java
kimle.michal.android.taxitwin.application.TaxiTwinApplication.java
kimle.michal.android.taxitwin.contentprovider.TaxiTwinContentProvider.java
kimle.michal.android.taxitwin.db.DbContract.java
kimle.michal.android.taxitwin.db.DbHelper.java
kimle.michal.android.taxitwin.dialog.alert.AddressAlertDialogFragment.java
kimle.michal.android.taxitwin.dialog.alert.GPSAlertDialogFragment.java
kimle.michal.android.taxitwin.dialog.alert.GooglePlayServicesAlertDialogFragment.java
kimle.michal.android.taxitwin.dialog.alert.InternetAlertDialogFragment.java
kimle.michal.android.taxitwin.dialog.alert.LeaveTaxiTwinAlertDialogFragment.java
kimle.michal.android.taxitwin.dialog.alert.ServicesAlertDialogFragment.java
kimle.michal.android.taxitwin.dialog.alert.TaxiTwinAlertDialogFragment.java
kimle.michal.android.taxitwin.dialog.alert.TaxiTwinNoLongerAlertDialogFragment.java
kimle.michal.android.taxitwin.dialog.error.GooglePlayServicesErrorDialogFragment.java
kimle.michal.android.taxitwin.dialog.error.OfferErrorDialogFragment.java
kimle.michal.android.taxitwin.dialog.error.PlaceErrorDialogFragment.java
kimle.michal.android.taxitwin.dialog.error.ResponseErrorDialogFragment.java
kimle.michal.android.taxitwin.entity.Place.java
kimle.michal.android.taxitwin.enumerate.UserState.java
kimle.michal.android.taxitwin.fragment.TaxiTwinListFragment.java
kimle.michal.android.taxitwin.fragment.TaxiTwinMapFragment.java
kimle.michal.android.taxitwin.gcm.GcmBroadcastReceiver.java
kimle.michal.android.taxitwin.gcm.GcmConnector.java
kimle.michal.android.taxitwin.gcm.GcmHandler.java
kimle.michal.android.taxitwin.gcm.GcmIntentService.java
kimle.michal.android.taxitwin.popup.SettingsPopup.java
kimle.michal.android.taxitwin.services.ServicesManagement.java
kimle.michal.android.taxitwin.view.TaxiTwinAutoCompleteTextView.java