Upgrade to new GCM API.
parent
c632b32ff8
commit
9ef14a0f64
@ -1,12 +1,80 @@
|
||||
package org.thoughtcrime.securesms.gcm;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
public class GcmBroadcastReceiver extends com.google.android.gcm.GCMBroadcastReceiver {
|
||||
import com.google.android.gms.gcm.GoogleCloudMessaging;
|
||||
|
||||
import org.thoughtcrime.securesms.service.SendReceiveService;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.whispersystems.textsecure.crypto.InvalidVersionException;
|
||||
import org.whispersystems.textsecure.directory.Directory;
|
||||
import org.whispersystems.textsecure.directory.NotInDirectoryException;
|
||||
import org.whispersystems.textsecure.push.ContactTokenDetails;
|
||||
import org.whispersystems.textsecure.push.IncomingEncryptedPushMessage;
|
||||
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
||||
import org.whispersystems.textsecure.util.Util;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class GcmBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
private static final String TAG = GcmBroadcastReceiver.class.getSimpleName();
|
||||
|
||||
@Override
|
||||
protected String getGCMIntentServiceClassName(Context context) {
|
||||
return "org.thoughtcrime.securesms.gcm.GcmIntentService";
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
|
||||
String messageType = gcm.getMessageType(intent);
|
||||
|
||||
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
|
||||
Log.w(TAG, "GCM message...");
|
||||
|
||||
try {
|
||||
String data = intent.getStringExtra("message");
|
||||
|
||||
if (Util.isEmpty(data))
|
||||
return;
|
||||
|
||||
if (!TextSecurePreferences.isPushRegistered(context)) {
|
||||
Log.w(TAG, "Not push registered!");
|
||||
return;
|
||||
}
|
||||
|
||||
String sessionKey = TextSecurePreferences.getSignalingKey(context);
|
||||
IncomingEncryptedPushMessage encryptedMessage = new IncomingEncryptedPushMessage(data, sessionKey);
|
||||
IncomingPushMessage message = encryptedMessage.getIncomingPushMessage();
|
||||
|
||||
if (!isActiveNumber(context, message.getSource())) {
|
||||
Directory directory = Directory.getInstance(context);
|
||||
ContactTokenDetails contactTokenDetails = new ContactTokenDetails();
|
||||
contactTokenDetails.setNumber(message.getSource());
|
||||
|
||||
directory.setNumber(contactTokenDetails, true);
|
||||
}
|
||||
|
||||
Intent service = new Intent(context, SendReceiveService.class);
|
||||
service.setAction(SendReceiveService.RECEIVE_PUSH_ACTION);
|
||||
service.putExtra("message", message);
|
||||
context.startService(service);
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, e);
|
||||
} catch (InvalidVersionException e) {
|
||||
Log.w(TAG, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isActiveNumber(Context context, String e164number) {
|
||||
boolean isActiveNumber;
|
||||
|
||||
try {
|
||||
isActiveNumber = Directory.getInstance(context).isActiveNumber(e164number);
|
||||
} catch (NotInDirectoryException e) {
|
||||
isActiveNumber = false;
|
||||
}
|
||||
|
||||
return isActiveNumber;
|
||||
}
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
package org.thoughtcrime.securesms.gcm;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.android.gcm.GCMBaseIntentService;
|
||||
|
||||
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
|
||||
import org.thoughtcrime.securesms.service.RegistrationService;
|
||||
import org.thoughtcrime.securesms.service.SendReceiveService;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.whispersystems.textsecure.crypto.InvalidVersionException;
|
||||
import org.whispersystems.textsecure.directory.Directory;
|
||||
import org.whispersystems.textsecure.directory.NotInDirectoryException;
|
||||
import org.whispersystems.textsecure.push.ContactTokenDetails;
|
||||
import org.whispersystems.textsecure.push.IncomingEncryptedPushMessage;
|
||||
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
||||
import org.whispersystems.textsecure.push.PushServiceSocket;
|
||||
import org.whispersystems.textsecure.util.Util;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
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)) {
|
||||
Intent intent = new Intent(RegistrationService.GCM_REGISTRATION_EVENT);
|
||||
intent.putExtra(RegistrationService.GCM_REGISTRATION_ID, registrationId);
|
||||
sendBroadcast(intent);
|
||||
} else {
|
||||
try {
|
||||
PushServiceSocket pushSocket = PushServiceSocketFactory.create(context);
|
||||
pushSocket.registerGcmId(registrationId);
|
||||
} catch (IOException e) {
|
||||
Log.w("GcmIntentService", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onUnregistered(Context context, String registrationId) {
|
||||
try {
|
||||
PushServiceSocket pushSocket = PushServiceSocketFactory.create(context);
|
||||
pushSocket.unregisterGcmId();
|
||||
} catch (IOException ioe) {
|
||||
Log.w("GcmIntentService", ioe);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onMessage(Context context, Intent intent) {
|
||||
try {
|
||||
String data = intent.getStringExtra("message");
|
||||
Log.w("GcmIntentService", "GCM message...");
|
||||
|
||||
if (Util.isEmpty(data))
|
||||
return;
|
||||
|
||||
if (!TextSecurePreferences.isPushRegistered(context)) {
|
||||
Log.w("GcmIntentService", "Not push registered!");
|
||||
return;
|
||||
}
|
||||
|
||||
String sessionKey = TextSecurePreferences.getSignalingKey(context);
|
||||
IncomingEncryptedPushMessage encryptedMessage = new IncomingEncryptedPushMessage(data, sessionKey);
|
||||
IncomingPushMessage message = encryptedMessage.getIncomingPushMessage();
|
||||
|
||||
if (!isActiveNumber(context, message.getSource())) {
|
||||
Directory directory = Directory.getInstance(context);
|
||||
ContactTokenDetails contactTokenDetails = new ContactTokenDetails();
|
||||
contactTokenDetails.setNumber(message.getSource());
|
||||
|
||||
directory.setNumber(contactTokenDetails, true);
|
||||
}
|
||||
|
||||
Intent service = new Intent(context, SendReceiveService.class);
|
||||
service.setAction(SendReceiveService.RECEIVE_PUSH_ACTION);
|
||||
service.putExtra("message", message);
|
||||
context.startService(service);
|
||||
} catch (IOException e) {
|
||||
Log.w("GcmIntentService", e);
|
||||
} catch (InvalidVersionException e) {
|
||||
Log.w("GcmIntentService", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onError(Context context, String s) {
|
||||
Log.w("GcmIntentService", "GCM Error: " + s);
|
||||
}
|
||||
|
||||
private boolean isActiveNumber(Context context, String e164number) {
|
||||
boolean isActiveNumber;
|
||||
|
||||
try {
|
||||
isActiveNumber = Directory.getInstance(context).isActiveNumber(e164number);
|
||||
} catch (NotInDirectoryException e) {
|
||||
isActiveNumber = false;
|
||||
}
|
||||
|
||||
return isActiveNumber;
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package org.thoughtcrime.securesms.gcm;
|
||||
|
||||
public class GcmRegistrationTimeoutException extends Exception {
|
||||
public GcmRegistrationTimeoutException() {
|
||||
}
|
||||
|
||||
public GcmRegistrationTimeoutException(String detailMessage) {
|
||||
super(detailMessage);
|
||||
}
|
||||
|
||||
public GcmRegistrationTimeoutException(String detailMessage, Throwable throwable) {
|
||||
super(detailMessage, throwable);
|
||||
}
|
||||
|
||||
public GcmRegistrationTimeoutException(Throwable throwable) {
|
||||
super(throwable);
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package org.thoughtcrime.securesms.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.android.gms.common.ConnectionResult;
|
||||
import com.google.android.gms.common.GooglePlayServicesUtil;
|
||||
import com.google.android.gms.gcm.GoogleCloudMessaging;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
|
||||
import org.thoughtcrime.securesms.util.Dialogs;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.whispersystems.textsecure.push.PushServiceSocket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class GcmRegistrationService extends Service implements Runnable {
|
||||
|
||||
private static final String TAG = GcmRegistrationService.class.getSimpleName();
|
||||
|
||||
public static final String REGISTRATION_ID = "312334754206";
|
||||
|
||||
private ExecutorService executor;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
this.executor = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flats, int startId) {
|
||||
executor.execute(this);
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Log.w(TAG, "Running GCM Registration Service...");
|
||||
try {
|
||||
String registrationId = TextSecurePreferences.getGcmRegistrationId(this);
|
||||
|
||||
if (registrationId == null) {
|
||||
Log.w(TAG, "GCM registrationId expired, reregistering...");
|
||||
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
|
||||
|
||||
if (result != ConnectionResult.SUCCESS) {
|
||||
Toast.makeText(this, "Unable to register with GCM!", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
String gcmId = GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID);
|
||||
PushServiceSocket socket = PushServiceSocketFactory.create(this);
|
||||
|
||||
socket.registerGcmId(gcmId);
|
||||
TextSecurePreferences.setGcmRegistrationId(this, gcmId);
|
||||
|
||||
stopSelf();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
executor.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue