2014-11-24 01:05:09 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2014 Open Whisper Systems
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2014-07-25 22:14:29 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2014-11-24 01:05:09 +00:00
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
2014-08-04 23:15:13 +00:00
|
|
|
import android.content.Context;
|
2014-11-24 01:05:09 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
2014-07-25 22:14:29 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.google.android.gms.common.ConnectionResult;
|
|
|
|
import com.google.android.gms.common.GooglePlayServicesUtil;
|
|
|
|
import com.google.android.gms.gcm.GoogleCloudMessaging;
|
|
|
|
|
2015-09-30 23:19:50 +00:00
|
|
|
import org.thoughtcrime.redphone.signaling.RedPhoneAccountManager;
|
|
|
|
import org.thoughtcrime.redphone.signaling.UnauthorizedException;
|
2014-11-24 01:05:09 +00:00
|
|
|
import org.thoughtcrime.securesms.PlayServicesProblemActivity;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-09-30 23:19:50 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
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;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException;
|
2014-07-25 22:14:29 +00:00
|
|
|
|
2015-09-30 23:19:50 +00:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
public class GcmRefreshJob extends ContextJob implements InjectableType {
|
2014-07-25 22:14:29 +00:00
|
|
|
|
|
|
|
private static final String TAG = GcmRefreshJob.class.getSimpleName();
|
|
|
|
|
|
|
|
public static final String REGISTRATION_ID = "312334754206";
|
|
|
|
|
2016-03-23 17:34:41 +00:00
|
|
|
@Inject transient SignalServiceAccountManager textSecureAccountManager;
|
|
|
|
@Inject transient RedPhoneAccountManager redPhoneAccountManager;
|
2015-09-30 23:19:50 +00:00
|
|
|
|
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 {
|
2015-09-30 23:19:50 +00:00
|
|
|
String registrationId = TextSecurePreferences.getGcmRegistrationId(context);
|
2014-07-25 22:14:29 +00:00
|
|
|
|
|
|
|
if (registrationId == null) {
|
|
|
|
Log.w(TAG, "GCM registrationId expired, reregistering...");
|
|
|
|
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
|
|
|
|
|
|
|
|
if (result != ConnectionResult.SUCCESS) {
|
2014-11-24 01:05:09 +00:00
|
|
|
notifyGcmFailure();
|
|
|
|
} else {
|
|
|
|
String gcmId = GoogleCloudMessaging.getInstance(context).register(REGISTRATION_ID);
|
2015-09-30 23:19:50 +00:00
|
|
|
textSecureAccountManager.setGcmId(Optional.of(gcmId));
|
|
|
|
|
|
|
|
try {
|
|
|
|
redPhoneAccountManager.setGcmId(Optional.of(gcmId));
|
|
|
|
} catch (UnauthorizedException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
|
2014-11-24 01:05:09 +00:00
|
|
|
TextSecurePreferences.setGcmRegistrationId(context, gcmId);
|
2015-03-07 17:02:59 +00:00
|
|
|
TextSecurePreferences.setWebsocketRegistered(context, true);
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-11-12 05:11:57 +00:00
|
|
|
public boolean onShouldRetry(Exception 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-11-24 01:05:09 +00:00
|
|
|
private void notifyGcmFailure() {
|
|
|
|
Intent intent = new Intent(context, PlayServicesProblemActivity.class);
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1122, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
|
|
|
|
|
|
|
|
builder.setSmallIcon(R.drawable.icon_notification);
|
|
|
|
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
|
|
|
|
R.drawable.ic_action_warning_red));
|
2015-09-23 20:35:18 +00:00
|
|
|
builder.setContentTitle(context.getString(R.string.GcmRefreshJob_Permanent_Signal_communication_failure));
|
|
|
|
builder.setContentText(context.getString(R.string.GcmRefreshJob_Signal_was_unable_to_register_with_Google_Play_Services));
|
|
|
|
builder.setTicker(context.getString(R.string.GcmRefreshJob_Permanent_Signal_communication_failure));
|
2014-11-24 18:48:15 +00:00
|
|
|
builder.setVibrate(new long[] {0, 1000});
|
2014-11-24 01:05:09 +00:00
|
|
|
builder.setContentIntent(pendingIntent);
|
|
|
|
|
|
|
|
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
|
|
|
|
.notify(12, builder.build());
|
|
|
|
}
|
|
|
|
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|