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;
|
2018-08-09 14:15:43 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
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;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-09-30 23:19:50 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2018-06-18 19:27:04 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
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;
|
|
|
|
|
2015-09-30 23:19:50 +00:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
import androidx.work.Data;
|
2018-11-27 20:34:42 +00:00
|
|
|
import androidx.work.WorkerParameters;
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2019-01-24 11:04:28 +00:00
|
|
|
public class FcmRefreshJob extends ContextJob implements InjectableType {
|
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
|
|
|
|
2016-03-23 17:34:41 +00:00
|
|
|
@Inject transient SignalServiceAccountManager textSecureAccountManager;
|
2015-09-30 23:19:50 +00:00
|
|
|
|
2019-01-24 11:04:28 +00:00
|
|
|
public FcmRefreshJob(@NonNull Context context, @NonNull WorkerParameters workerParameters) {
|
2018-11-27 20:34:42 +00:00
|
|
|
super(context, workerParameters);
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 11:04:28 +00:00
|
|
|
public FcmRefreshJob(Context context) {
|
2017-03-17 17:28:06 +00:00
|
|
|
super(context, JobParameters.newBuilder()
|
2019-01-24 11:04:28 +00:00
|
|
|
.withGroupId(FcmRefreshJob.class.getSimpleName())
|
2018-08-09 14:15:43 +00:00
|
|
|
.withDuplicatesIgnored(true)
|
|
|
|
.withNetworkRequirement()
|
2017-03-17 17:28:06 +00:00
|
|
|
.withRetryCount(1)
|
|
|
|
.create());
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 14:15:43 +00:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.build();
|
|
|
|
}
|
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()) {
|
|
|
|
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
|
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
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|