• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.grpc;
18 
19 import android.os.Build;
20 import android.os.SystemProperties;
21 import android.util.Pair;
22 
23 import androidx.annotation.NonNull;
24 
25 import com.android.devicelockcontroller.util.LogUtil;
26 
27 import io.grpc.Status;
28 
29 /**
30  * An abstract class that's intended for implementation of class that manages communication with
31  * Device finalize service.
32  */
33 public abstract class DeviceFinalizeClient {
34     public static final String TAG = "DeviceFinalizeClient";
35     public static final String DEVICE_FINALIZE_CLIENT_DEBUG_CLASS_NAME =
36             "com.android.devicelockcontroller.debug.DeviceFinalizeClientDebug";
37     private static volatile DeviceFinalizeClient sClient;
38     protected static String sEnrollmentToken = "";
39     protected static String sRegisteredId = "";
40     protected static String sHostName = "";
41     protected static int sPortNumber = 0;
42     protected static Pair<String, String> sApiKey = new Pair<>("", "");
43 
44     /**
45      * Get a instance of {@link DeviceFinalizeClient} object.
46      * Note that, the arguments will be ignored after first initialization.
47      */
getInstance( String className, String hostName, int portNumber, Pair<String, String> apiKey, String registeredId, String enrollmentToken)48     public static DeviceFinalizeClient getInstance(
49             String className,
50             String hostName,
51             int portNumber,
52             Pair<String, String> apiKey,
53             String registeredId,
54             String enrollmentToken) {
55         if (sClient == null) {
56             synchronized (DeviceFinalizeClient.class) {
57                 // In case the initialization is already done by other thread use existing
58                 // instance.
59                 if (sClient != null) {
60                     return sClient;
61                 }
62                 sHostName = hostName;
63                 sPortNumber = portNumber;
64                 sRegisteredId = registeredId;
65                 sEnrollmentToken = enrollmentToken;
66                 sApiKey = apiKey;
67                 try {
68                     if (Build.isDebuggable() && SystemProperties.getBoolean(
69                             "debug.devicelock.finalize", true)) {
70                         className = DEVICE_FINALIZE_CLIENT_DEBUG_CLASS_NAME;
71                     }
72                     LogUtil.d(TAG, "Creating instance for " + className);
73                     Class<?> clazz = Class.forName(className);
74                     sClient = (DeviceFinalizeClient) clazz.getDeclaredConstructor().newInstance();
75                 } catch (Exception e) {
76                     throw new RuntimeException("Failed to get DeviceFinalizeClient instance", e);
77                 }
78             }
79         }
80         return sClient;
81     }
82 
83     /**
84      * Reports that a device completed a Device Lock program.
85      */
reportDeviceProgramComplete()86     public abstract ReportDeviceProgramCompleteResponse reportDeviceProgramComplete();
87 
88     /**
89      * Class that used to indicate the successfulness / failure status of the response.
90      */
91     public static final class ReportDeviceProgramCompleteResponse extends
92             DeviceCheckInGrpcResponse {
ReportDeviceProgramCompleteResponse()93         public ReportDeviceProgramCompleteResponse() {
94             super();
95         }
96 
ReportDeviceProgramCompleteResponse(@onNull Status status)97         public ReportDeviceProgramCompleteResponse(@NonNull Status status) {
98             super(status);
99         }
100     }
101 }
102