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;
|
2018-08-09 14:15:43 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2014-11-24 01:05:09 +00:00
|
|
|
import android.support.v4.app.NotificationCompat;
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2019-01-24 11:04:28 +00:00
|
|
|
import com.google.android.gms.common.ConnectionResult;
|
|
|
|
import com.google.android.gms.common.GoogleApiAvailability;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.gcm.FcmUtil;
|
2019-03-28 15:56:35 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2014-07-25 22:14:29 +00:00
|
|
|
|
2014-11-24 01:05:09 +00:00
|
|
|
import org.thoughtcrime.securesms.PlayServicesProblemActivity;
|
2019-07-24 02:30:23 +00:00
|
|
|
import network.loki.messenger.R;
|
2015-09-30 23:19:50 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2018-08-06 16:20:24 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.NotificationChannels;
|
2019-01-24 11:04:28 +00:00
|
|
|
import org.thoughtcrime.securesms.transport.RetryLaterException;
|
2014-07-25 22:14:29 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
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
|
|
|
|
2019-01-24 11:04:28 +00:00
|
|
|
import java.io.IOException;
|
2019-03-28 15:56:35 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-01-24 11:04:28 +00:00
|
|
|
|
2015-09-30 23:19:50 +00:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public class FcmRefreshJob extends BaseJob implements InjectableType {
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public static final String KEY = "FcmRefreshJob";
|
2014-07-25 22:14:29 +00:00
|
|
|
|
2019-01-24 11:04:28 +00:00
|
|
|
private static final String TAG = FcmRefreshJob.class.getSimpleName();
|
2014-07-25 22:14:29 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
@Inject SignalServiceAccountManager textSecureAccountManager;
|
2015-09-30 23:19:50 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public FcmRefreshJob() {
|
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.setQueue("FcmRefreshJob")
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setMaxAttempts(1)
|
|
|
|
.setLifespan(TimeUnit.MINUTES.toMillis(5))
|
|
|
|
.setMaxInstances(1)
|
|
|
|
.build());
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
private FcmRefreshJob(@NonNull Job.Parameters parameters) {
|
|
|
|
super(parameters);
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return Data.EMPTY;
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
2014-07-25 22:14:29 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws Exception {
|
2019-01-24 11:04:28 +00:00
|
|
|
if (TextSecurePreferences.isFcmDisabled(context)) return;
|
2017-02-20 20:00:03 +00:00
|
|
|
|
2019-01-24 11:04:28 +00:00
|
|
|
Log.i(TAG, "Reregistering FCM...");
|
|
|
|
|
|
|
|
int result = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context);
|
2017-03-06 19:50:51 +00:00
|
|
|
|
|
|
|
if (result != ConnectionResult.SUCCESS) {
|
2019-01-24 11:04:28 +00:00
|
|
|
notifyFcmFailure();
|
2017-03-06 19:50:51 +00:00
|
|
|
} else {
|
2019-01-24 11:04:28 +00:00
|
|
|
Optional<String> token = FcmUtil.getToken();
|
|
|
|
|
|
|
|
if (token.isPresent()) {
|
2019-02-15 19:40:03 +00:00
|
|
|
String oldToken = TextSecurePreferences.getFcmToken(context);
|
|
|
|
|
|
|
|
if (!token.get().equals(oldToken)) {
|
|
|
|
int oldLength = oldToken != null ? oldToken.length() : -1;
|
|
|
|
Log.i(TAG, "Token changed. oldLength: " + oldLength + " newLength: " + token.get().length());
|
|
|
|
} else {
|
|
|
|
Log.i(TAG, "Token didn't change.");
|
2019-02-06 08:16:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 11:04:28 +00:00
|
|
|
textSecureAccountManager.setGcmId(token);
|
|
|
|
TextSecurePreferences.setFcmToken(context, token.get());
|
|
|
|
TextSecurePreferences.setFcmTokenLastSetTime(context, System.currentTimeMillis());
|
|
|
|
TextSecurePreferences.setWebsocketRegistered(context, true);
|
|
|
|
} else {
|
|
|
|
throw new RetryLaterException(new IOException("Failed to retrieve a token."));
|
|
|
|
}
|
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
|
2019-05-22 16:51:56 +00:00
|
|
|
public boolean onShouldRetry(@NonNull 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
|
|
|
|
2019-01-24 11:04:28 +00:00
|
|
|
private void notifyFcmFailure() {
|
2014-11-24 01:05:09 +00:00
|
|
|
Intent intent = new Intent(context, PlayServicesProblemActivity.class);
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1122, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
2018-08-06 16:20:24 +00:00
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannels.FAILURES);
|
2014-11-24 01:05:09 +00:00
|
|
|
|
2019-07-23 23:51:09 +00:00
|
|
|
builder.setSmallIcon(R.drawable.ic_notification);
|
2014-11-24 01:05:09 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public static final class Factory implements Job.Factory<FcmRefreshJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull FcmRefreshJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
|
|
return new FcmRefreshJob(parameters);
|
|
|
|
}
|
|
|
|
}
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|