session-android/src/org/thoughtcrime/securesms/gcm/GcmIntentService.java

92 lines
3.3 KiB
Java
Raw Normal View History

2013-03-26 04:26:03 +00:00
package org.thoughtcrime.securesms.gcm;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
2013-03-26 04:26:03 +00:00
import com.google.android.gcm.GCMBaseIntentService;
import com.google.thoughtcrimegson.Gson;
2013-03-26 04:26:03 +00:00
import org.thoughtcrime.securesms.service.RegistrationService;
import org.thoughtcrime.securesms.service.SendReceiveService;
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.textsecure.push.IncomingPushMessage;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.push.RateLimitException;
2013-07-10 02:48:33 +00:00
import org.whispersystems.textsecure.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) {
if (!TextSecurePreferences.isPushRegistered(context)) {
2013-03-26 04:26:03 +00:00
Intent intent = new Intent(RegistrationService.GCM_REGISTRATION_EVENT);
intent.putExtra(RegistrationService.GCM_REGISTRATION_ID, registrationId);
sendBroadcast(intent);
} else {
try {
getGcmSocket(context).registerGcmId(registrationId);
} catch (IOException e) {
Log.w("GcmIntentService", e);
}
}
}
@Override
protected void onUnregistered(Context context, String registrationId) {
try {
getGcmSocket(context).unregisterGcmId();
} catch (IOException ioe) {
Log.w("GcmIntentService", ioe);
2013-03-26 04:26:03 +00:00
}
}
2013-03-26 04:26:03 +00:00
@Override
protected void onMessage(Context context, Intent intent) {
String data = intent.getStringExtra("message");
Log.w("GcmIntentService", "GCM message: " + data);
if (Util.isEmpty(data))
return;
IncomingPushMessage message = new Gson().fromJson(data, IncomingPushMessage.class);
if (!message.hasAttachments()) handleIncomingTextMessage(context, message);
else handleIncomingMediaMessage(context, message);
}
@Override
protected void onError(Context context, String s) {
Log.w("GcmIntentService", "GCM Error: " + s);
}
private void handleIncomingTextMessage(Context context, IncomingPushMessage message) {
ArrayList<IncomingTextMessage> messages = new ArrayList<IncomingTextMessage>();
messages.add(new IncomingTextMessage(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
}
private void handleIncomingMediaMessage(Context context, IncomingPushMessage message) {
Intent receivedIntent = new Intent(context, SendReceiveService.class);
receivedIntent.setAction(SendReceiveService.RECEIVE_PUSH_MMS_ACTION);
receivedIntent.putExtra("media_message", message);
context.startService(receivedIntent);
2013-03-26 04:26:03 +00:00
}
2013-07-08 23:29:28 +00:00
private PushServiceSocket getGcmSocket(Context context) {
String localNumber = TextSecurePreferences.getLocalNumber(context);
String password = TextSecurePreferences.getPushServerPassword(context);
2013-07-08 23:29:28 +00:00
return new PushServiceSocket(context, localNumber, password);
2013-03-26 04:26:03 +00:00
}
}