2013-03-26 04:26:03 +00:00
|
|
|
package org.thoughtcrime.securesms.gcm;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
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;
|
|
|
|
import org.thoughtcrime.securesms.service.RegistrationService;
|
2013-04-01 02:16:06 +00:00
|
|
|
import org.thoughtcrime.securesms.service.SendReceiveService;
|
2013-07-10 01:26:18 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2013-08-30 00:01:30 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.InvalidVersionException;
|
2013-10-12 10:57:17 +00:00
|
|
|
import org.whispersystems.textsecure.directory.Directory;
|
2013-08-30 00:01:30 +00:00
|
|
|
import org.whispersystems.textsecure.push.IncomingEncryptedPushMessage;
|
2013-07-19 00:42:45 +00:00
|
|
|
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
2013-07-10 23:22:58 +00:00
|
|
|
import org.whispersystems.textsecure.push.PushServiceSocket;
|
2013-07-10 02:48:33 +00:00
|
|
|
import org.whispersystems.textsecure.util.Util;
|
2013-04-01 02:16:06 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
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) {
|
2013-07-11 21:58:40 +00:00
|
|
|
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 {
|
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 {
|
2013-07-11 21:58:40 +00:00
|
|
|
getGcmSocket(context).unregisterGcmId();
|
2013-04-01 02:16:06 +00:00
|
|
|
} 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-08-30 00:01:30 +00:00
|
|
|
try {
|
|
|
|
String data = intent.getStringExtra("message");
|
|
|
|
Log.w("GcmIntentService", "GCM message: " + data);
|
2013-04-01 02:16:06 +00:00
|
|
|
|
2013-08-30 00:01:30 +00:00
|
|
|
if (Util.isEmpty(data))
|
|
|
|
return;
|
2013-04-01 02:16:06 +00:00
|
|
|
|
2013-08-30 00:01:30 +00:00
|
|
|
String sessionKey = TextSecurePreferences.getSignalingKey(context);
|
|
|
|
IncomingEncryptedPushMessage encryptedMessage = new IncomingEncryptedPushMessage(data, sessionKey);
|
|
|
|
IncomingPushMessage message = encryptedMessage.getIncomingPushMessage();
|
2013-07-19 00:42:45 +00:00
|
|
|
|
2013-09-09 01:19:05 +00:00
|
|
|
Intent service = new Intent(context, SendReceiveService.class);
|
|
|
|
service.setAction(SendReceiveService.RECEIVE_PUSH_ACTION);
|
|
|
|
service.putExtra("message", message);
|
|
|
|
|
2013-10-12 10:57:17 +00:00
|
|
|
Directory directory = Directory.getInstance(context);
|
|
|
|
directory.setToken(directory.getToken(message.getSource()), true);
|
|
|
|
|
2013-09-09 01:19:05 +00:00
|
|
|
context.startService(service);
|
2013-08-30 00:01:30 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("GcmIntentService", e);
|
|
|
|
} catch (InvalidVersionException e) {
|
|
|
|
Log.w("GcmIntentService", e);
|
|
|
|
}
|
2013-07-19 00:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onError(Context context, String s) {
|
|
|
|
Log.w("GcmIntentService", "GCM Error: " + s);
|
|
|
|
}
|
|
|
|
|
2013-07-08 23:29:28 +00:00
|
|
|
private PushServiceSocket getGcmSocket(Context context) {
|
2013-07-10 01:26:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|