2014-07-25 22:14:29 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2014-08-04 23:15:13 +00:00
|
|
|
import android.content.Context;
|
2014-07-25 22:14:29 +00:00
|
|
|
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;
|
|
|
|
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
|
2014-07-25 22:14:29 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2014-08-04 23:15:13 +00:00
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
|
|
|
import org.whispersystems.textsecure.api.TextSecureAccountManager;
|
2014-07-25 22:14:29 +00:00
|
|
|
import org.whispersystems.textsecure.push.exceptions.NonSuccessfulResponseCodeException;
|
|
|
|
|
|
|
|
public class GcmRefreshJob extends ContextJob {
|
|
|
|
|
|
|
|
private static final String TAG = GcmRefreshJob.class.getSimpleName();
|
|
|
|
|
|
|
|
public static final String REGISTRATION_ID = "312334754206";
|
|
|
|
|
2014-08-04 23:15:13 +00:00
|
|
|
public GcmRefreshJob(Context context) {
|
|
|
|
super(context, JobParameters.newBuilder().withRequirement(new NetworkRequirement(context)).create());
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws Exception {
|
|
|
|
String registrationId = TextSecurePreferences.getGcmRegistrationId(context);
|
|
|
|
|
|
|
|
if (registrationId == null) {
|
|
|
|
Log.w(TAG, "GCM registrationId expired, reregistering...");
|
|
|
|
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
|
|
|
|
|
|
|
|
if (result != ConnectionResult.SUCCESS) {
|
|
|
|
Toast.makeText(context, "Unable to register with GCM!", Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
|
2014-11-10 04:35:08 +00:00
|
|
|
String gcmId = GoogleCloudMessaging.getInstance(context).register(REGISTRATION_ID);
|
|
|
|
TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context);
|
|
|
|
|
|
|
|
accountManager.setGcmId(Optional.of(gcmId));
|
2014-07-25 22:14:29 +00:00
|
|
|
|
|
|
|
TextSecurePreferences.setGcmRegistrationId(context, gcmId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-08-04 23:15:13 +00:00
|
|
|
public void onCanceled() {
|
2014-07-25 22:14:29 +00:00
|
|
|
Log.w(TAG, "GCM reregistration failed after retry attempt exhaustion!");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-08-04 23:15:13 +00:00
|
|
|
public boolean onShouldRetry(Throwable throwable) {
|
2014-07-25 22:14:29 +00:00
|
|
|
if (throwable instanceof NonSuccessfulResponseCodeException) return false;
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-04 23:15:13 +00:00
|
|
|
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|