| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.security; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.content.ContentResolver; |
| 21 |
import android.content.Context; |
| 22 |
import android.provider.Settings; |
| 23 |
import android.provider.Settings.SettingNotFoundException; |
| 24 |
import android.text.TextUtils; |
| 25 |
import android.util.Log; |
| 26 |
|
| 27 |
import java.util.Locale; |
| 28 |
import java.util.concurrent.Executor; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public class ConfirmationPrompt { |
| 87 |
private static final String TAG = "ConfirmationPrompt"; |
| 88 |
|
| 89 |
private CharSequence mPromptText; |
| 90 |
private byte[] mExtraData; |
| 91 |
private ConfirmationCallback mCallback; |
| 92 |
private Executor mExecutor; |
| 93 |
private Context mContext; |
| 94 |
|
| 95 |
private final KeyStore mKeyStore = KeyStore.getInstance(); |
| 96 |
private AndroidProtectedConfirmation mProtectedConfirmation; |
| 97 |
|
| 98 |
private AndroidProtectedConfirmation getService() { |
| 99 |
if (mProtectedConfirmation == null) { |
| 100 |
mProtectedConfirmation = new AndroidProtectedConfirmation(); |
| 101 |
} |
| 102 |
return mProtectedConfirmation; |
| 103 |
} |
| 104 |
|
| 105 |
private void doCallback(int responseCode, byte[] dataThatWasConfirmed, |
| 106 |
ConfirmationCallback callback) { |
| 107 |
switch (responseCode) { |
| 108 |
case AndroidProtectedConfirmation.ERROR_OK: |
| 109 |
callback.onConfirmed(dataThatWasConfirmed); |
| 110 |
break; |
| 111 |
|
| 112 |
case AndroidProtectedConfirmation.ERROR_CANCELED: |
| 113 |
callback.onDismissed(); |
| 114 |
break; |
| 115 |
|
| 116 |
case AndroidProtectedConfirmation.ERROR_ABORTED: |
| 117 |
callback.onCanceled(); |
| 118 |
break; |
| 119 |
|
| 120 |
case AndroidProtectedConfirmation.ERROR_SYSTEM_ERROR: |
| 121 |
callback.onError(new Exception("System error returned by ConfirmationUI.")); |
| 122 |
break; |
| 123 |
|
| 124 |
default: |
| 125 |
callback.onError(new Exception("Unexpected responseCode=" + responseCode |
| 126 |
+ " from onConfirmtionPromptCompleted() callback.")); |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
private final android.security.apc.IConfirmationCallback mConfirmationCallback = |
| 132 |
new android.security.apc.IConfirmationCallback.Stub() { |
| 133 |
@Override |
| 134 |
public void onCompleted(int result, byte[] dataThatWasConfirmed) |
| 135 |
throws android.os.RemoteException { |
| 136 |
if (mCallback != null) { |
| 137 |
ConfirmationCallback callback = mCallback; |
| 138 |
Executor executor = mExecutor; |
| 139 |
mCallback = null; |
| 140 |
mExecutor = null; |
| 141 |
if (executor == null) { |
| 142 |
doCallback(result, dataThatWasConfirmed, callback); |
| 143 |
} else { |
| 144 |
executor.execute(new Runnable() { |
| 145 |
@Override public void run() { |
| 146 |
doCallback(result, dataThatWasConfirmed, callback); |
| 147 |
} |
| 148 |
}); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public static final class Builder { |
| 158 |
|
| 159 |
private Context mContext; |
| 160 |
private CharSequence mPromptText; |
| 161 |
private byte[] mExtraData; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public Builder(Context context) { |
| 169 |
mContext = context; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public Builder setPromptText(CharSequence promptText) { |
| 179 |
mPromptText = promptText; |
| 180 |
return this; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public Builder setExtraData(byte[] extraData) { |
| 190 |
mExtraData = extraData; |
| 191 |
return this; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public ConfirmationPrompt build() { |
| 201 |
if (TextUtils.isEmpty(mPromptText)) { |
| 202 |
throw new IllegalArgumentException("prompt text must be set and non-empty"); |
| 203 |
} |
| 204 |
if (mExtraData == null) { |
| 205 |
throw new IllegalArgumentException("extraData must be set"); |
| 206 |
} |
| 207 |
return new ConfirmationPrompt(mContext, mPromptText, mExtraData); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
private ConfirmationPrompt(Context context, CharSequence promptText, byte[] extraData) { |
| 212 |
mContext = context; |
| 213 |
mPromptText = promptText; |
| 214 |
mExtraData = extraData; |
| 215 |
} |
| 216 |
|
| 217 |
private int getUiOptionsAsFlags() { |
| 218 |
int uiOptionsAsFlags = 0; |
| 219 |
ContentResolver contentResolver = mContext.getContentResolver(); |
| 220 |
int inversionEnabled = Settings.Secure.getInt(contentResolver, |
| 221 |
Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, 0); |
| 222 |
if (inversionEnabled == 1) { |
| 223 |
uiOptionsAsFlags |= AndroidProtectedConfirmation.FLAG_UI_OPTION_INVERTED; |
| 224 |
} |
| 225 |
float fontScale = Settings.System.getFloat(contentResolver, |
| 226 |
Settings.System.FONT_SCALE, (float) 1.0); |
| 227 |
if (fontScale > 1.0) { |
| 228 |
uiOptionsAsFlags |= AndroidProtectedConfirmation.FLAG_UI_OPTION_MAGNIFIED; |
| 229 |
} |
| 230 |
return uiOptionsAsFlags; |
| 231 |
} |
| 232 |
|
| 233 |
private static boolean isAccessibilityServiceRunning(Context context) { |
| 234 |
boolean serviceRunning = false; |
| 235 |
try { |
| 236 |
ContentResolver contentResolver = context.getContentResolver(); |
| 237 |
int a11yEnabled = Settings.Secure.getInt(contentResolver, |
| 238 |
Settings.Secure.ACCESSIBILITY_ENABLED); |
| 239 |
if (a11yEnabled == 1) { |
| 240 |
serviceRunning = true; |
| 241 |
} |
| 242 |
} catch (SettingNotFoundException e) { |
| 243 |
Log.w(TAG, "Unexpected SettingNotFoundException"); |
| 244 |
e.printStackTrace(); |
| 245 |
} |
| 246 |
return serviceRunning; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public void presentPrompt(@NonNull Executor executor, @NonNull ConfirmationCallback callback) |
| 266 |
throws ConfirmationAlreadyPresentingException, |
| 267 |
ConfirmationNotAvailableException { |
| 268 |
if (mCallback != null) { |
| 269 |
throw new ConfirmationAlreadyPresentingException(); |
| 270 |
} |
| 271 |
if (isAccessibilityServiceRunning(mContext)) { |
| 272 |
throw new ConfirmationNotAvailableException(); |
| 273 |
} |
| 274 |
mCallback = callback; |
| 275 |
mExecutor = executor; |
| 276 |
|
| 277 |
String locale = Locale.getDefault().toLanguageTag(); |
| 278 |
int uiOptionsAsFlags = getUiOptionsAsFlags(); |
| 279 |
int responseCode = getService().presentConfirmationPrompt( |
| 280 |
mConfirmationCallback, mPromptText.toString(), mExtraData, locale, |
| 281 |
uiOptionsAsFlags); |
| 282 |
switch (responseCode) { |
| 283 |
case AndroidProtectedConfirmation.ERROR_OK: |
| 284 |
return; |
| 285 |
|
| 286 |
case AndroidProtectedConfirmation.ERROR_OPERATION_PENDING: |
| 287 |
throw new ConfirmationAlreadyPresentingException(); |
| 288 |
|
| 289 |
case AndroidProtectedConfirmation.ERROR_UNIMPLEMENTED: |
| 290 |
throw new ConfirmationNotAvailableException(); |
| 291 |
|
| 292 |
default: |
| 293 |
|
| 294 |
Log.w(TAG, |
| 295 |
"Unexpected responseCode=" + responseCode |
| 296 |
+ " from presentConfirmationPrompt() call."); |
| 297 |
throw new IllegalArgumentException(); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public void cancelPrompt() { |
| 311 |
int responseCode = |
| 312 |
getService().cancelConfirmationPrompt(mConfirmationCallback); |
| 313 |
if (responseCode == AndroidProtectedConfirmation.ERROR_OK) { |
| 314 |
return; |
| 315 |
} else if (responseCode == AndroidProtectedConfirmation.ERROR_OPERATION_PENDING) { |
| 316 |
throw new IllegalStateException(); |
| 317 |
} else { |
| 318 |
|
| 319 |
Log.w(TAG, |
| 320 |
"Unexpected responseCode=" + responseCode |
| 321 |
+ " from cancelConfirmationPrompt() call."); |
| 322 |
throw new IllegalStateException(); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public static boolean isSupported(Context context) { |
| 333 |
if (isAccessibilityServiceRunning(context)) { |
| 334 |
return false; |
| 335 |
} |
| 336 |
return new AndroidProtectedConfirmation().isConfirmationPromptSupported(); |
| 337 |
} |
| 338 |
} |