1 /* 2 * Copyright (C) 2023 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 import android.content.res.Resources; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.Nullable; 24 import androidx.work.ListenableWorker; 25 import androidx.work.WorkerParameters; 26 27 import com.android.devicelockcontroller.ClientInterceptorProvider; 28 import com.android.devicelockcontroller.R; 29 import com.android.devicelockcontroller.provision.grpc.DeviceCheckInClient; 30 import com.android.devicelockcontroller.storage.GlobalParametersClient; 31 32 import com.google.common.util.concurrent.Futures; 33 import com.google.common.util.concurrent.ListenableFuture; 34 import com.google.common.util.concurrent.ListeningExecutorService; 35 import com.google.common.util.concurrent.MoreExecutors; 36 37 import io.grpc.ClientInterceptor; 38 39 import java.time.Duration; 40 41 /** 42 * A base class for workers that execute gRPC requests with DeviceLock backend server. 43 */ 44 public abstract class AbstractCheckInWorker extends ListenableWorker { 45 46 public static final Duration BACKOFF_DELAY = Duration.ofMinutes(1); 47 static final String TAG = "CheckInWorker"; 48 final ListenableFuture<DeviceCheckInClient> mClient; 49 final ListeningExecutorService mExecutorService; 50 final Context mContext; 51 AbstractCheckInWorker(@onNull Context context, @NonNull WorkerParameters workerParameters, @Nullable DeviceCheckInClient client, ListeningExecutorService executorService)52 AbstractCheckInWorker(@NonNull Context context, 53 @NonNull WorkerParameters workerParameters, @Nullable DeviceCheckInClient client, 54 ListeningExecutorService executorService) { 55 super(context, workerParameters); 56 if (client != null) { 57 mClient = Futures.immediateFuture(client); 58 } else { 59 Resources resources = context.getResources(); 60 String hostName = resources.getString(R.string.check_in_server_host_name); 61 int portNumber = resources.getInteger(R.integer.check_in_server_port_number); 62 ClientInterceptorProvider clientInterceptorProvider = 63 (ClientInterceptorProvider) context.getApplicationContext(); 64 ClientInterceptor clientInterceptor = clientInterceptorProvider.getClientInterceptor(); 65 mClient = Futures.transform( 66 GlobalParametersClient.getInstance().getRegisteredDeviceId(), 67 registeredId -> DeviceCheckInClient.getInstance( 68 context, hostName, portNumber, clientInterceptor, registeredId), 69 MoreExecutors.directExecutor()); 70 } 71 mContext = context; 72 mExecutorService = executorService; 73 } 74 } 75