1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.devicelockcontroller.provision.worker; 18 19 import android.content.Context; 20 21 import androidx.annotation.NonNull; 22 import androidx.annotation.VisibleForTesting; 23 import androidx.work.ListenableWorker; 24 import androidx.work.WorkerParameters; 25 26 import com.android.devicelockcontroller.FcmRegistrationTokenProvider; 27 28 import com.google.common.base.Strings; 29 import com.google.common.util.concurrent.Futures; 30 import com.google.common.util.concurrent.ListenableFuture; 31 import com.google.common.util.concurrent.MoreExecutors; 32 33 import java.time.Duration; 34 35 /** 36 * Worker class to retry getting the FCM registration token if it was not successfully retrieved 37 * with the initial check-in. 38 */ 39 public final class GetFcmTokenWorker extends ListenableWorker { 40 41 static final Duration FCM_TOKEN_WORKER_INITIAL_DELAY = Duration.ofMinutes(1); 42 static final Duration FCM_TOKEN_WORKER_BACKOFF_DELAY = Duration.ofMinutes(30); 43 static final String FCM_TOKEN_WORK_NAME = "fcm-token"; 44 45 private final FcmRegistrationTokenProvider mFcmRegistrationTokenProvider; 46 GetFcmTokenWorker(@onNull Context context, @NonNull WorkerParameters workerParameters)47 public GetFcmTokenWorker(@NonNull Context context, 48 @NonNull WorkerParameters workerParameters) { 49 this(context, workerParameters, 50 (FcmRegistrationTokenProvider) context.getApplicationContext()); 51 } 52 53 @VisibleForTesting GetFcmTokenWorker(@onNull Context context, @NonNull WorkerParameters workerParameters, FcmRegistrationTokenProvider tokenProvider)54 GetFcmTokenWorker(@NonNull Context context, 55 @NonNull WorkerParameters workerParameters, 56 FcmRegistrationTokenProvider tokenProvider) { 57 super(context, workerParameters); 58 mFcmRegistrationTokenProvider = tokenProvider; 59 } 60 61 @NonNull 62 @Override startWork()63 public ListenableFuture<Result> startWork() { 64 return Futures.transform(mFcmRegistrationTokenProvider.getFcmRegistrationToken(), 65 token -> { 66 if (Strings.isNullOrEmpty(token) || token.isBlank()) { 67 return Result.retry(); 68 } 69 return Result.success(); 70 }, MoreExecutors.directExecutor() 71 ); 72 } 73 } 74