2013-03-26 04:26:03 +00:00
|
|
|
package org.thoughtcrime.securesms.gcm;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
2013-04-01 02:16:06 +00:00
|
|
|
import android.util.Log;
|
2013-03-26 04:26:03 +00:00
|
|
|
|
|
|
|
import com.google.android.gcm.GCMBaseIntentService;
|
2013-04-01 02:16:06 +00:00
|
|
|
import com.google.thoughtcrimegson.Gson;
|
2013-03-26 04:26:03 +00:00
|
|
|
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
|
|
|
|
import org.thoughtcrime.securesms.service.RegistrationService;
|
2013-04-01 02:16:06 +00:00
|
|
|
import org.thoughtcrime.securesms.service.SendReceiveService;
|
|
|
|
import org.thoughtcrime.securesms.sms.TextMessage;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
2013-03-26 04:26:03 +00:00
|
|
|
|
|
|
|
public class GcmIntentService extends GCMBaseIntentService {
|
|
|
|
|
|
|
|
public static final String GCM_SENDER_ID = "312334754206";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onRegistered(Context context, String registrationId) {
|
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
if (!preferences.getBoolean(ApplicationPreferencesActivity.REGISTERED_GCM_PREF, false)) {
|
|
|
|
Intent intent = new Intent(RegistrationService.GCM_REGISTRATION_EVENT);
|
|
|
|
intent.putExtra(RegistrationService.GCM_REGISTRATION_ID, registrationId);
|
|
|
|
sendBroadcast(intent);
|
|
|
|
} else {
|
2013-04-01 02:16:06 +00:00
|
|
|
try {
|
|
|
|
getGcmSocket(context).registerGcmId(registrationId);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("GcmIntentService", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onUnregistered(Context context, String registrationId) {
|
|
|
|
try {
|
|
|
|
getGcmSocket(context).unregisterGcmId(registrationId);
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w("GcmIntentService", ioe);
|
2013-03-26 04:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 02:16:06 +00:00
|
|
|
|
2013-03-26 04:26:03 +00:00
|
|
|
@Override
|
|
|
|
protected void onMessage(Context context, Intent intent) {
|
2013-04-01 02:16:06 +00:00
|
|
|
Log.w("GcmIntentService", "Got GCM message!");
|
|
|
|
String data = intent.getStringExtra("message");
|
|
|
|
Log.w("GcmIntentService", "GCM message: " + data);
|
|
|
|
|
|
|
|
if (Util.isEmpty(data))
|
|
|
|
return;
|
|
|
|
|
|
|
|
IncomingGcmMessage message = new Gson().fromJson(data, IncomingGcmMessage.class);
|
|
|
|
ArrayList<TextMessage> messages = new ArrayList<TextMessage>();
|
|
|
|
messages.add(new TextMessage(message));
|
|
|
|
|
|
|
|
Intent receivedIntent = new Intent(context, SendReceiveService.class);
|
|
|
|
receivedIntent.setAction(SendReceiveService.RECEIVE_SMS_ACTION);
|
|
|
|
receivedIntent.putParcelableArrayListExtra("text_messages", messages);
|
|
|
|
context.startService(receivedIntent);
|
2013-03-26 04:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onError(Context context, String s) {
|
2013-04-01 02:16:06 +00:00
|
|
|
Log.w("GcmIntentService", "GCM Error: " + s);
|
2013-03-26 04:26:03 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 02:16:06 +00:00
|
|
|
private GcmSocket getGcmSocket(Context context) {
|
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
String localNumber = preferences.getString(ApplicationPreferencesActivity.LOCAL_NUMBER_PREF, null);
|
|
|
|
String password = preferences.getString(ApplicationPreferencesActivity.GCM_PASSWORD_PREF, null);
|
|
|
|
return new GcmSocket(context, localNumber, password);
|
2013-03-26 04:26:03 +00:00
|
|
|
}
|
|
|
|
}
|