Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 17 | package com.android.dynsystem; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 18 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.KeyguardManager; |
| 21 | import android.content.Context; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 22 | import android.content.Intent; |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 23 | import android.net.Uri; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 24 | import android.os.Bundle; |
Po-Chien Hsueh | 4167b42 | 2019-04-01 11:26:18 +0800 | [diff] [blame] | 25 | import android.os.SystemProperties; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 26 | import android.os.UserHandle; |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 27 | import android.os.image.DynamicSystemClient; |
Po-Chien Hsueh | 4167b42 | 2019-04-01 11:26:18 +0800 | [diff] [blame] | 28 | import android.util.FeatureFlagUtils; |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * This Activity starts KeyguardManager and ask the user to confirm |
| 34 | * before any installation request. If the device is not protected by |
| 35 | * a password, it approves the request by default. |
| 36 | */ |
| 37 | public class VerificationActivity extends Activity { |
| 38 | |
| 39 | private static final String TAG = "VerificationActivity"; |
| 40 | |
| 41 | private static final int REQUEST_CODE = 1; |
| 42 | |
| 43 | // For install request verification |
| 44 | private static String sVerifiedUrl; |
| 45 | |
| 46 | |
| 47 | @Override |
| 48 | protected void onCreate(Bundle savedInstanceState) { |
| 49 | super.onCreate(savedInstanceState); |
| 50 | |
Po-Chien Hsueh | 4167b42 | 2019-04-01 11:26:18 +0800 | [diff] [blame] | 51 | if (!featureFlagEnabled()) { |
| 52 | Log.w(TAG, FeatureFlagUtils.DYNAMIC_SYSTEM + " not enabled; activity aborted."); |
| 53 | finish(); |
| 54 | return; |
| 55 | } |
| 56 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 57 | KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); |
| 58 | |
| 59 | if (km != null) { |
| 60 | String title = getString(R.string.keyguard_title); |
| 61 | String description = getString(R.string.keyguard_description); |
| 62 | Intent intent = km.createConfirmDeviceCredentialIntent(title, description); |
| 63 | |
| 64 | if (intent == null) { |
| 65 | Log.d(TAG, "This device is not protected by a password/pin"); |
| 66 | startInstallationService(); |
| 67 | finish(); |
| 68 | } else { |
| 69 | startActivityForResult(intent, REQUEST_CODE); |
| 70 | } |
| 71 | } else { |
| 72 | finish(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 78 | if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { |
| 79 | startInstallationService(); |
| 80 | } |
| 81 | |
| 82 | finish(); |
| 83 | } |
| 84 | |
| 85 | private void startInstallationService() { |
| 86 | // retrieve data from calling intent |
| 87 | Intent callingIntent = getIntent(); |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 88 | Uri url = callingIntent.getData(); |
Yo Chiang | a69bb07 | 2019-12-18 15:58:02 +0800 | [diff] [blame] | 89 | Bundle extras = callingIntent.getExtras(); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 90 | |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 91 | sVerifiedUrl = url.toString(); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 92 | |
| 93 | // start service |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 94 | Intent intent = new Intent(this, DynamicSystemInstallationService.class); |
Po-Chien Hsueh | c51cf0f | 2019-03-21 17:16:06 +0800 | [diff] [blame] | 95 | intent.setData(url); |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 96 | intent.setAction(DynamicSystemClient.ACTION_START_INSTALL); |
Yo Chiang | a69bb07 | 2019-12-18 15:58:02 +0800 | [diff] [blame] | 97 | intent.putExtras(extras); |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 98 | |
| 99 | Log.d(TAG, "Starting Installation Service"); |
| 100 | startServiceAsUser(intent, UserHandle.SYSTEM); |
| 101 | } |
| 102 | |
Po-Chien Hsueh | 4167b42 | 2019-04-01 11:26:18 +0800 | [diff] [blame] | 103 | private boolean featureFlagEnabled() { |
| 104 | return SystemProperties.getBoolean( |
| 105 | FeatureFlagUtils.PERSIST_PREFIX + FeatureFlagUtils.DYNAMIC_SYSTEM, false); |
| 106 | } |
| 107 | |
Po-Chien Hsueh | 64aa782 | 2019-01-12 00:40:02 +0800 | [diff] [blame] | 108 | static boolean isVerified(String url) { |
| 109 | return sVerifiedUrl != null && sVerifiedUrl.equals(url); |
| 110 | } |
| 111 | } |