Upgrade to new GCM API.

This commit is contained in:
Moxie Marlinspike
2014-07-23 15:40:45 -07:00
parent c632b32ff8
commit 9ef14a0f64
14 changed files with 234 additions and 239 deletions

View File

@@ -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;
}
}

View File

@@ -10,19 +10,16 @@ import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import com.google.android.gcm.GCMRegistrar;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.gcm.GcmIntentService;
import org.thoughtcrime.securesms.gcm.GcmRegistrationTimeoutException;
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
import org.thoughtcrime.securesms.util.DirectoryHelper;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.textsecure.crypto.IdentityKey;
import org.whispersystems.textsecure.crypto.MasterSecret;
import org.whispersystems.textsecure.crypto.PreKeyUtil;
import org.whispersystems.textsecure.crypto.ecc.Curve;
import org.whispersystems.textsecure.push.ExpectationFailedException;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.storage.PreKeyRecord;
@@ -62,10 +59,8 @@ public class RegistrationService extends Service {
public static final String NOTIFICATION_TEXT = "org.thoughtcrime.securesms.NOTIFICATION_TEXT";
public static final String CHALLENGE_EVENT = "org.thoughtcrime.securesms.CHALLENGE_EVENT";
public static final String REGISTRATION_EVENT = "org.thoughtcrime.securesms.REGISTRATION_EVENT";
public static final String GCM_REGISTRATION_EVENT = "org.thoughtcrime.securesms.GCM_REGISTRATION_EVENT";
public static final String CHALLENGE_EXTRA = "CAAChallenge";
public static final String GCM_REGISTRATION_ID = "GCMRegistrationId";
private static final long REGISTRATION_TIMEOUT_MILLIS = 120000;
@@ -76,9 +71,7 @@ public class RegistrationService extends Service {
private volatile Handler registrationStateHandler;
private volatile ChallengeReceiver challengeReceiver;
private volatile GcmRegistrationReceiver gcmRegistrationReceiver;
private String challenge;
private String gcmRegistrationId;
private long verificationStartTime;
private boolean generatingPreKeys;
@@ -130,19 +123,12 @@ public class RegistrationService extends Service {
}
private void initializeChallengeListener() {
this.challenge = null;
this.challenge = null;
challengeReceiver = new ChallengeReceiver();
IntentFilter filter = new IntentFilter(CHALLENGE_EVENT);
registerReceiver(challengeReceiver, filter);
}
private void initializeGcmRegistrationListener() {
this.gcmRegistrationId = null;
gcmRegistrationReceiver = new GcmRegistrationReceiver();
IntentFilter filter = new IntentFilter(GCM_REGISTRATION_EVENT);
registerReceiver(gcmRegistrationReceiver, filter);
}
private synchronized void shutdownChallengeListener() {
if (challengeReceiver != null) {
unregisterReceiver(challengeReceiver);
@@ -150,13 +136,6 @@ public class RegistrationService extends Service {
}
}
private synchronized void shutdownGcmRegistrationListener() {
if (gcmRegistrationReceiver != null) {
unregisterReceiver(gcmRegistrationReceiver);
gcmRegistrationReceiver = null;
}
}
private void handleVoiceRequestedIntent(Intent intent) {
setState(new RegistrationState(RegistrationState.STATE_VOICE_REQUESTED,
intent.getStringExtra("e164number"),
@@ -172,8 +151,6 @@ public class RegistrationService extends Service {
MasterSecret masterSecret = intent.getParcelableExtra("master_secret");
try {
initializeGcmRegistrationListener();
PushServiceSocket socket = PushServiceSocketFactory.create(this, number, password);
handleCommonRegistration(masterSecret, socket, number);
@@ -190,12 +167,6 @@ public class RegistrationService extends Service {
Log.w("RegistrationService", e);
setState(new RegistrationState(RegistrationState.STATE_NETWORK_ERROR, number));
broadcastComplete(false);
} catch (GcmRegistrationTimeoutException e) {
Log.w("RegistrationService", e);
setState(new RegistrationState(RegistrationState.STATE_GCM_TIMEOUT));
broadcastComplete(false);
} finally {
shutdownGcmRegistrationListener();
}
}
@@ -216,7 +187,6 @@ public class RegistrationService extends Service {
String signalingKey = Util.getSecret(52);
initializeChallengeListener();
initializeGcmRegistrationListener();
setState(new RegistrationState(RegistrationState.STATE_CONNECTING, number));
PushServiceSocket socket = PushServiceSocketFactory.create(this, number, password);
@@ -247,18 +217,13 @@ public class RegistrationService extends Service {
Log.w("RegistrationService", e);
setState(new RegistrationState(RegistrationState.STATE_NETWORK_ERROR, number));
broadcastComplete(false);
} catch (GcmRegistrationTimeoutException e) {
Log.w("RegistrationService", e);
setState(new RegistrationState(RegistrationState.STATE_GCM_TIMEOUT));
broadcastComplete(false);
} finally {
shutdownChallengeListener();
shutdownGcmRegistrationListener();
}
}
private void handleCommonRegistration(MasterSecret masterSecret, PushServiceSocket socket, String number)
throws GcmRegistrationTimeoutException, IOException
throws IOException
{
setState(new RegistrationState(RegistrationState.STATE_GENERATING_KEYS, number));
IdentityKey identityKey = IdentityKeyUtil.getIdentityKey(this);
@@ -267,8 +232,9 @@ public class RegistrationService extends Service {
socket.registerPreKeys(identityKey, lastResort, records);
setState(new RegistrationState(RegistrationState.STATE_GCM_REGISTERING, number));
GCMRegistrar.register(this, GcmIntentService.GCM_SENDER_ID);
String gcmRegistrationId = waitForGcmRegistrationId();
String gcmRegistrationId = GoogleCloudMessaging.getInstance(this).register("312334754206");
TextSecurePreferences.setGcmRegistrationId(this, gcmRegistrationId);
socket.registerGcmId(gcmRegistrationId);
DirectoryHelper.refreshDirectory(this, socket, number);
@@ -293,31 +259,11 @@ public class RegistrationService extends Service {
return this.challenge;
}
private synchronized String waitForGcmRegistrationId() throws GcmRegistrationTimeoutException {
if (this.gcmRegistrationId == null) {
try {
wait(10 * 60 * 1000);
} catch (InterruptedException e) {
throw new IllegalArgumentException(e);
}
}
if (this.gcmRegistrationId == null)
throw new GcmRegistrationTimeoutException();
return this.gcmRegistrationId;
}
private synchronized void challengeReceived(String challenge) {
this.challenge = challenge;
notifyAll();
}
private synchronized void gcmRegistrationReceived(String gcmRegistrationId) {
this.gcmRegistrationId = gcmRegistrationId;
notifyAll();
}
private void markAsVerifying(boolean verifying) {
TextSecurePreferences.setVerifying(this, verifying);
@@ -367,13 +313,6 @@ public class RegistrationService extends Service {
}
}
private class GcmRegistrationReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.w("RegistrationService", "Got gcm registration broadcast...");
gcmRegistrationReceived(intent.getStringExtra(GCM_REGISTRATION_ID));
}
}
private class ChallengeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {