mirror of
https://github.com/oxen-io/session-android.git
synced 2025-02-19 19:18:27 +00:00
Upgrade to new GCM API.
This commit is contained in:
parent
c632b32ff8
commit
9ef14a0f64
@ -5,8 +5,6 @@
|
||||
android:versionCode="73"
|
||||
android:versionName="2.1.1">
|
||||
|
||||
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19"/>
|
||||
|
||||
<permission android:name="org.thoughtcrime.securesms.ACCESS_SECRETS"
|
||||
android:label="Access to TextSecure Secrets"
|
||||
android:protectionLevel="signature" />
|
||||
@ -33,7 +31,6 @@
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.READ_CALL_LOG" />
|
||||
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
|
||||
|
||||
<permission android:name="org.thoughtcrime.securesms.permission.C2D_MESSAGE"
|
||||
@ -45,6 +42,9 @@
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/TextSecure.LightTheme">
|
||||
|
||||
<meta-data android:name="com.google.android.gms.version"
|
||||
android:value="@integer/google_play_services_version" />
|
||||
|
||||
<activity android:name=".RoutingActivity"
|
||||
android:theme="@style/NoAnimation.Theme.BlackScreen"
|
||||
android:launchMode="singleTask"
|
||||
@ -209,7 +209,6 @@
|
||||
<service android:enabled="true" android:name=".service.RegistrationService"/>
|
||||
<service android:enabled="true" android:name=".service.DirectoryRefreshService"/>
|
||||
<service android:enabled="true" android:name=".service.PreKeyService"/>
|
||||
<service android:enabled="true" android:name=".gcm.GcmIntentService"/>
|
||||
|
||||
<service android:name=".service.QuickResponseService"
|
||||
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
|
||||
@ -236,7 +235,6 @@
|
||||
<receiver android:name=".gcm.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
|
||||
<intent-filter>
|
||||
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
|
||||
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
|
||||
<category android:name="org.thoughtcrime.securesms" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
@ -302,8 +300,4 @@
|
||||
|
||||
<uses-library android:name="android.test.runner" />
|
||||
</application>
|
||||
|
||||
<instrumentation android:name="android.test.InstrumentationTestRunner"
|
||||
android:targetPackage="org.thoughtcrime.securesms.tests" android:label="Tests for My App" />
|
||||
|
||||
</manifest>
|
||||
|
@ -8,7 +8,7 @@ buildscript {
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'android'
|
||||
apply plugin: 'com.android.application'
|
||||
apply plugin: 'witness'
|
||||
|
||||
repositories {
|
||||
@ -24,8 +24,8 @@ repositories {
|
||||
dependencies {
|
||||
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
|
||||
compile 'com.android.support:support-v4:19.1.0'
|
||||
compile 'com.google.android.gcm:gcm-client:1.0.2'
|
||||
compile 'se.emilsjolander:stickylistheaders:2.2.0'
|
||||
compile "com.google.android.gms:play-services:5.0.77"
|
||||
|
||||
androidTestCompile 'com.squareup:fest-android:1.0.8'
|
||||
|
||||
@ -36,7 +36,6 @@ dependencyVerification {
|
||||
verify = [
|
||||
'com.actionbarsherlock:actionbarsherlock:5ab04d74101f70024b222e3ff9c87bee151ec43331b4a2134b6cc08cf8565819',
|
||||
'com.android.support:support-v4:3f40fa7b3a4ead01ce15dce9453b061646e7fe2e7c51cb75ca01ee1e77037f3f',
|
||||
'com.google.android.gcm:gcm-client:5ff578202f93dcba1c210d015deb4241c7cdad9b7867bd1b32e0a5f4c16986ca',
|
||||
'se.emilsjolander:stickylistheaders:89146b46c96fea0e40200474a2625cda10fe94891e4128f53cdb42375091b9b6',
|
||||
'com.google.protobuf:protobuf-java:ad9769a22989e688a46af4d3accc348cc501ced22118033230542bc916e33f0b',
|
||||
'com.madgag:sc-light-jdk15on:931f39d351429fb96c2f749e7ecb1a256a8ebbf5edca7995c9cc085b94d1841d',
|
||||
|
@ -45,7 +45,7 @@ import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.actionbarsherlock.view.MenuItem;
|
||||
import com.google.android.gcm.GCMRegistrar;
|
||||
import com.google.android.gms.gcm.GoogleCloudMessaging;
|
||||
|
||||
import org.thoughtcrime.securesms.components.OutgoingSmsPreference;
|
||||
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
||||
@ -344,7 +344,8 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredSherlockPr
|
||||
PushServiceSocket socket = PushServiceSocketFactory.create(context);
|
||||
|
||||
socket.unregisterGcmId();
|
||||
GCMRegistrar.unregister(context);
|
||||
GoogleCloudMessaging.getInstance(context).unregister();
|
||||
|
||||
return SUCCESS;
|
||||
} catch (AuthorizationFailedException afe) {
|
||||
Log.w("ApplicationPreferencesActivity", afe);
|
||||
|
@ -30,6 +30,7 @@ import android.widget.ProgressBar;
|
||||
import org.thoughtcrime.securesms.crypto.DecryptingQueue;
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
||||
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.whispersystems.textsecure.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.util.VersionTracker;
|
||||
@ -78,25 +79,21 @@ public class DatabaseUpgradeActivity extends Activity {
|
||||
}
|
||||
|
||||
private boolean needsUpgradeTask() {
|
||||
try {
|
||||
int currentVersionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
|
||||
int lastSeenVersion = VersionTracker.getLastSeenVersion(this);
|
||||
int currentVersionCode = Util.getCurrentApkReleaseVersion(this);
|
||||
int lastSeenVersion = VersionTracker.getLastSeenVersion(this);
|
||||
|
||||
Log.w("DatabaseUpgradeActivity", "LastSeenVersion: " + lastSeenVersion);
|
||||
|
||||
if (lastSeenVersion >= currentVersionCode)
|
||||
return false;
|
||||
|
||||
for (int version : UPGRADE_VERSIONS) {
|
||||
Log.w("DatabaseUpgradeActivity", "Comparing: " + version);
|
||||
if (lastSeenVersion < version)
|
||||
return true;
|
||||
}
|
||||
Log.w("DatabaseUpgradeActivity", "LastSeenVersion: " + lastSeenVersion);
|
||||
|
||||
if (lastSeenVersion >= currentVersionCode)
|
||||
return false;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
throw new AssertionError(e);
|
||||
|
||||
for (int version : UPGRADE_VERSIONS) {
|
||||
Log.w("DatabaseUpgradeActivity", "Comparing: " + version);
|
||||
if (lastSeenVersion < version)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isUpdate(Context context) {
|
||||
|
@ -18,15 +18,16 @@ import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.actionbarsherlock.app.SherlockActivity;
|
||||
import com.google.android.gcm.GCMRegistrar;
|
||||
import com.google.android.gms.common.ConnectionResult;
|
||||
import com.google.android.gms.common.GooglePlayServicesUtil;
|
||||
import com.google.i18n.phonenumbers.AsYouTypeFormatter;
|
||||
import com.google.i18n.phonenumbers.NumberParseException;
|
||||
import com.google.i18n.phonenumbers.PhoneNumberUtil;
|
||||
import com.google.i18n.phonenumbers.Phonenumber;
|
||||
|
||||
import org.whispersystems.textsecure.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.thoughtcrime.securesms.util.Dialogs;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.whispersystems.textsecure.crypto.MasterSecret;
|
||||
import org.whispersystems.textsecure.util.PhoneNumberFormatter;
|
||||
import org.whispersystems.textsecure.util.Util;
|
||||
|
||||
@ -178,11 +179,15 @@ public class RegistrationActivity extends SherlockActivity {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
GCMRegistrar.checkDevice(self);
|
||||
} catch (UnsupportedOperationException uoe) {
|
||||
Dialogs.showAlertDialog(self, getString(R.string.RegistrationActivity_unsupported),
|
||||
getString(R.string.RegistrationActivity_sorry_this_device_is_not_supported_for_data_messaging));
|
||||
int gcmStatus = GooglePlayServicesUtil.isGooglePlayServicesAvailable(self);
|
||||
|
||||
if (gcmStatus != ConnectionResult.SUCCESS) {
|
||||
if (GooglePlayServicesUtil.isUserRecoverableError(gcmStatus)) {
|
||||
GooglePlayServicesUtil.getErrorDialog(gcmStatus, self, 9000).show();
|
||||
} else {
|
||||
Dialogs.showAlertDialog(self, getString(R.string.RegistrationActivity_unsupported),
|
||||
getString(R.string.RegistrationActivity_sorry_this_device_is_not_supported_for_data_messaging));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
||||
import org.thoughtcrime.securesms.recipients.Recipients;
|
||||
import org.thoughtcrime.securesms.service.ApplicationMigrationService;
|
||||
import org.thoughtcrime.securesms.service.GcmRegistrationService;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.whispersystems.textsecure.crypto.MasterSecret;
|
||||
|
||||
@ -128,8 +129,10 @@ public class RoutingActivity extends PassphraseRequiredSherlockActivity {
|
||||
|
||||
private void handleDisplayConversationOrList() {
|
||||
final ConversationParameters parameters = getConversationParameters();
|
||||
|
||||
final Intent intent;
|
||||
|
||||
scheduleRefreshActions();
|
||||
|
||||
if (isShareAction()) {
|
||||
intent = getShareIntent(parameters);
|
||||
} else if (parameters.recipients != null) {
|
||||
@ -137,6 +140,7 @@ public class RoutingActivity extends PassphraseRequiredSherlockActivity {
|
||||
} else {
|
||||
intent = getConversationListIntent();
|
||||
}
|
||||
|
||||
startActivity(intent);
|
||||
finish();
|
||||
}
|
||||
@ -182,6 +186,15 @@ public class RoutingActivity extends PassphraseRequiredSherlockActivity {
|
||||
return intent;
|
||||
}
|
||||
|
||||
private void scheduleRefreshActions() {
|
||||
if (TextSecurePreferences.isPushRegistered(this) &&
|
||||
TextSecurePreferences.getGcmRegistrationId(this) == null)
|
||||
{
|
||||
Intent intent = new Intent(this, GcmRegistrationService.class);
|
||||
startService(intent);
|
||||
}
|
||||
}
|
||||
|
||||
private int getApplicationState() {
|
||||
if (!MasterSecretUtil.isPassphraseInitialized(this))
|
||||
return STATE_CREATE_PASSPHRASE;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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) {
|
||||
|
@ -52,6 +52,24 @@ public class TextSecurePreferences {
|
||||
private static final String FALLBACK_SMS_ASK_REQUIRED_PREF = "pref_sms_fallback_ask";
|
||||
private static final String DIRECT_SMS_ALLOWED_PREF = "pref_sms_non_data_out";
|
||||
|
||||
private static final String GCM_REGISTRATION_ID_PREF = "pref_gcm_registration_id";
|
||||
private static final String GCM_REGISTRATION_ID_VERSION_PREF = "pref_gcm_registration_id_version";
|
||||
|
||||
public static void setGcmRegistrationId(Context context, String registrationId) {
|
||||
setStringPreference(context, GCM_REGISTRATION_ID_PREF, registrationId);
|
||||
setIntegerPrefrence(context, GCM_REGISTRATION_ID_VERSION_PREF, Util.getCurrentApkReleaseVersion(context));
|
||||
}
|
||||
|
||||
public static String getGcmRegistrationId(Context context) {
|
||||
int storedRegistrationIdVersion = getIntegerPreference(context, GCM_REGISTRATION_ID_VERSION_PREF, 0);
|
||||
|
||||
if (storedRegistrationIdVersion != Util.getCurrentApkReleaseVersion(context)) {
|
||||
return null;
|
||||
} else {
|
||||
return getStringPreference(context, GCM_REGISTRATION_ID_PREF, null);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isFallbackSmsAllowed(Context context) {
|
||||
return getBooleanPreference(context, FALLBACK_SMS_ALLOWED_PREF, true);
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Build;
|
||||
import android.provider.Telephony;
|
||||
@ -153,6 +154,14 @@ public class Util {
|
||||
(context.getPackageName().equals(Telephony.Sms.getDefaultSmsPackage(context)));
|
||||
}
|
||||
|
||||
public static int getCurrentApkReleaseVersion(Context context) {
|
||||
try {
|
||||
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
// public static Bitmap loadScaledBitmap(InputStream src, int targetWidth, int targetHeight) {
|
||||
// return BitmapFactory.decodeStream(src);
|
||||
//// BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
|
@ -14,12 +14,8 @@ public class VersionTracker {
|
||||
|
||||
public static void updateLastSeenVersion(Context context) {
|
||||
try {
|
||||
int currentVersionCode = context.getPackageManager()
|
||||
.getPackageInfo(context.getPackageName(), 0)
|
||||
.versionCode;
|
||||
int currentVersionCode = Util.getCurrentApkReleaseVersion(context);
|
||||
TextSecurePreferences.setLastVersionCode(context, currentVersionCode);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
throw new AssertionError(e);
|
||||
} catch (IOException ioe) {
|
||||
throw new AssertionError(ioe);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user