Permalink
Browse files

Changed C2DM such that it can identify whether message came from same…

… device or not
  • Loading branch information...
1 parent c0b24ca commit 18b8a63632d2f519f6adf5cc5ffeedf91bd6cfbd @vettukal vettukal committed Aug 20, 2012
@@ -11,14 +11,14 @@
import org.openintents.cloudsync.shared.OICloudSyncRequest;
import org.openintents.cloudsync.shared.OICloudSyncRequestFactory;
import org.openintents.cloudsync.shared.TaskProxy;
-import org.openintents.cloudsync.util.Dumper;
import org.openintents.cloudsync.util.NotepadSync;
import org.openintents.cloudsync.util.RecievedData;
import org.openintents.cloudsync.util.SyncUtil;
import org.openintents.cloudsync.util.Ulg;
import android.content.ContentValues;
import android.content.Context;
+import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
@@ -127,7 +127,7 @@ public AsyncSync(Context cloudSyncActivity) {
if(!(rdArray.length == 0 & deleteList.size()==0)) {
// This means that something has come from Android for the sync.
// And hence something for update and insert
- sendC2DM("Update or Insert happened");
+ sendC2DM("Put a random Double and save in sharedResource");
}
return makeJsonArraysForClient();
@@ -137,6 +137,13 @@ public AsyncSync(Context cloudSyncActivity) {
private void sendC2DM(String message) {
+ message = Double.toString(Math.random());
+ SharedPreferences prefs = Util.getSharedPreferences(activity);
+ SharedPreferences.Editor editor = prefs.edit();
+ editor.putString(Util.C2DMID, message);
+ editor.commit();
+
+ message = prefs.getString(Util.DEVICE_REGISTRATION_ID, "no device id");
OICloudSyncRequestFactory factory = Util.getRequestFactory(activity, OICloudSyncRequestFactory.class);
factory.taskRequest().sendC2DM(message).fire(new Receiver<Boolean>() {
@@ -15,13 +15,20 @@
package org.openintents.cloudsync;
import com.google.android.c2dm.C2DMBaseReceiver;
+import com.google.web.bindery.requestfactory.shared.Receiver;
+import com.google.web.bindery.requestfactory.shared.ServerFailure;
import java.io.IOException;
import java.util.TimerTask;
+import org.openintents.cloudsync.client.MyRequestFactory;
+import org.openintents.cloudsync.client.MyRequestFactory.HelloWorldRequest;
+
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.os.AsyncTask;
+import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
@@ -37,8 +44,13 @@
private static final String TAG = "C2DMReceiver";
CDMTimer cdmTimeTask = new CDMTimer();
Handler timeHandle = new Handler();
+ C2DMReceiver receiver;
+
+ public void setReceiver(C2DMReceiver receiver) {
+ this.receiver = receiver;
+ }
- public C2DMReceiver() {
+ public C2DMReceiver() {
super(Setup.SENDER_ID);
}
@@ -86,8 +98,44 @@ public void onMessage(Context context, Intent intent) {
/*
* Replace this with your application-specific code
*/
- MessageDisplay.displayMessage(context, intent);
- if (debug) Log.d(TAG,"recieved c2dm message:-> ");
+ SharedPreferences prefs = Util.getSharedPreferences(this);
+ String storedMessageId = prefs.getString(Util.C2DMID, "nothing");
+ String storedDeviceId = prefs.getString(Util.DEVICE_REGISTRATION_ID, "no device id");
+ Bundle extras = intent.getExtras();
+ String gotMessage = "";
+ if (extras != null) {
+ gotMessage = (String) extras.get("message");
+ }
+
+ if (debug) Log.d(TAG,"storedMsg: "+storedMessageId+" GotMessage:-> "+gotMessage);
+ if(storedDeviceId.equals(gotMessage)){
+ MessageDisplay.displayMessage(context, intent);
+ if (debug) Log.d(TAG,"Same sender and reciever:-> "+gotMessage);
+ } else {
+ MessageDisplay.displayMessage(context, intent);
+ if (debug) Log.d(TAG,"C2DM message came from diff device:-> ");
+
+ // Going to start the sync service in the async task.
+ setReceiver(this);
+ new AsyncTask<Void, Void, String>() {
+ private String message;
+
+ @Override
+ protected String doInBackground(Void... arg0) {
+ Intent serviceIntent = new Intent();
+ serviceIntent.setAction("org.openintents.cloudsync.service.StartUpService");
+ receiver.startService(serviceIntent);
+ return null;
+ }
+
+ @Override
+ protected void onPostExecute(String result) {
+
+ }
+ }.execute();
+ }
+
+
// cdmTimeTask.setContext(this);
// cdmTimeTask.setHandle(timeHandle);
@@ -115,6 +115,8 @@
public static final long SYNC_DIFF_TIME = 120*1000;
+ public static final String C2DMID = "c2dmid";
+
/**
* Display a notification containing the given string.
*/

0 comments on commit 18b8a63

Please sign in to comment.