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.debug; 18 19 import static com.android.devicelockcontroller.common.DeviceLockConstants.DeviceProvisionState.PROVISION_STATE_UNSPECIFIED; 20 import static com.android.devicelockcontroller.common.DeviceLockConstants.STATUS_UNSPECIFIED; 21 22 import android.os.SystemProperties; 23 import android.util.ArraySet; 24 25 import androidx.annotation.Keep; 26 import androidx.annotation.Nullable; 27 28 import com.android.devicelockcontroller.common.DeviceId; 29 import com.android.devicelockcontroller.common.DeviceLockConstants.DeviceCheckInStatus; 30 import com.android.devicelockcontroller.common.DeviceLockConstants.DeviceProvisionState; 31 import com.android.devicelockcontroller.common.DeviceLockConstants.ProvisioningType; 32 import com.android.devicelockcontroller.provision.grpc.DeviceCheckInClient; 33 import com.android.devicelockcontroller.provision.grpc.GetDeviceCheckInStatusGrpcResponse; 34 import com.android.devicelockcontroller.provision.grpc.IsDeviceInApprovedCountryGrpcResponse; 35 import com.android.devicelockcontroller.provision.grpc.PauseDeviceProvisioningGrpcResponse; 36 import com.android.devicelockcontroller.provision.grpc.ProvisioningConfiguration; 37 import com.android.devicelockcontroller.provision.grpc.ReportDeviceProvisionStateGrpcResponse; 38 39 import java.time.Duration; 40 import java.time.Instant; 41 42 /** 43 * An implementation of the {@link DeviceCheckInClient} which simulate server responses by 44 * reading it from {@link SystemProperties}. 45 */ 46 @Keep 47 public final class DeviceCheckInClientDebug extends DeviceCheckInClient { 48 49 public static final String TAG = "DeviceCheckInClientDebug"; 50 51 /** 52 * Check In with DeviceLock backend server and get the next step for the device. 53 */ 54 @Override getDeviceCheckInStatus(ArraySet<DeviceId> deviceIds, String carrierInfo, @Nullable String fcmRegistrationToken)55 public GetDeviceCheckInStatusGrpcResponse getDeviceCheckInStatus(ArraySet<DeviceId> deviceIds, 56 String carrierInfo, @Nullable String fcmRegistrationToken) { 57 return new GetDeviceCheckInStatusGrpcResponse() { 58 @Override 59 @DeviceCheckInStatus 60 public int getDeviceCheckInStatus() { 61 return DebugLogUtil.logAndReturn(TAG, 62 SystemProperties.getInt("debug.devicelock.checkin.status", 63 STATUS_UNSPECIFIED)); 64 } 65 66 @Nullable 67 @Override 68 public String getRegisteredDeviceIdentifier() { 69 return DebugLogUtil.logAndReturn(TAG, SystemProperties.get( 70 "debug.devicelock.checkin.registered-id")); 71 } 72 73 @Nullable 74 @Override 75 public Instant getNextCheckInTime() { 76 Duration delay = Duration.ofMinutes( 77 SystemProperties.getInt( 78 "debug.devicelock.checkin.retry-delay", /* def= */ 1)); 79 return DebugLogUtil.logAndReturn(TAG, Instant.now().plusSeconds(delay.toSeconds())); 80 } 81 82 @Nullable 83 @Override 84 public ProvisioningConfiguration getProvisioningConfig() { 85 // Can be override using SetupParametersOverrider. 86 return null; 87 } 88 89 @Override 90 public @ProvisioningType int getProvisioningType() { 91 return DebugLogUtil.logAndReturn(TAG, 92 SystemProperties.getInt("debug.devicelock.checkin.provision-type", 93 ProvisioningType.TYPE_UNDEFINED)); 94 } 95 96 @Override 97 public boolean isProvisioningMandatory() { 98 return DebugLogUtil.logAndReturn(TAG, SystemProperties.getBoolean( 99 "debug.devicelock.checkin.mandatory-provisioning", 100 false)); 101 } 102 103 @Override 104 public boolean isProvisionForced() { 105 return DebugLogUtil.logAndReturn(TAG, SystemProperties.getBoolean( 106 "debug.devicelock.checkin.force-provisioning", 107 false)); 108 } 109 110 @Override 111 public boolean isDeviceInApprovedCountry() { 112 return DebugLogUtil.logAndReturn(TAG, SystemProperties.getBoolean( 113 "debug.devicelock.checkin.approved-country", 114 true)); 115 } 116 }; 117 } 118 119 /** 120 * Check if the device is in an approved country for the device lock program. 121 */ 122 @Override 123 public IsDeviceInApprovedCountryGrpcResponse isDeviceInApprovedCountry( 124 @Nullable String carrierInfo) { 125 return new IsDeviceInApprovedCountryGrpcResponse() { 126 @Override 127 public boolean isDeviceInApprovedCountry() { 128 return DebugLogUtil.logAndReturn(TAG, SystemProperties.getBoolean( 129 "debug.devicelock.checkin.approved-country", 130 true)); 131 } 132 }; 133 } 134 135 /** 136 * Inform the server that device provisioning has been paused for a certain amount of time. 137 */ 138 @Override 139 public PauseDeviceProvisioningGrpcResponse pauseDeviceProvisioning(int reason) { 140 return new PauseDeviceProvisioningGrpcResponse() { 141 @Override 142 public boolean shouldForceProvisioning() { 143 return DebugLogUtil.logAndReturn(TAG, SystemProperties.getBoolean( 144 "debug.devicelock.checkin.force-provisioning", 145 true)); 146 } 147 }; 148 } 149 150 /** 151 * Reports the current provision state of the device. 152 */ 153 @Override 154 public ReportDeviceProvisionStateGrpcResponse reportDeviceProvisionState(int reasonOfFailure, 155 int lastReceivedProvisionState, boolean isSuccessful) { 156 return new ReportDeviceProvisionStateGrpcResponse() { 157 @Override 158 @DeviceProvisionState 159 public int getNextClientProvisionState() { 160 return DebugLogUtil.logAndReturn(TAG, SystemProperties.getInt( 161 "debug.devicelock.checkin.next-provision-state", 162 PROVISION_STATE_UNSPECIFIED)); 163 } 164 165 @Nullable 166 @Override 167 public String getEnrollmentToken() { 168 // Not useful in local testing setup. 169 return null; 170 } 171 172 @Override 173 public int getDaysLeftUntilReset() { 174 return DebugLogUtil.logAndReturn(TAG, 175 SystemProperties.getInt( 176 "debug.devicelock.checkin.days-left", /* def= */ 1)); 177 } 178 }; 179 } 180 } 181