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 
17 package com.android.dynsystem;
18 
19 import static android.os.image.DynamicSystemClient.ACTION_NOTIFY_KEYGUARD_DISMISSED;
20 import static android.os.image.DynamicSystemClient.KEY_KEYGUARD_USE_DEFAULT_STRINGS;
21 
22 import android.app.Activity;
23 import android.app.KeyguardManager;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.os.UserHandle;
29 import android.os.image.DynamicSystemClient;
30 import android.util.Log;
31 
32 /**
33  * This Activity starts KeyguardManager and ask the user to confirm before any installation request.
34  * If the device is not protected by a password, it approves the request by default.
35  */
36 public class VerificationActivity extends Activity {
37 
38     private static final String TAG = "VerificationActivity";
39 
40     private static final int REQUEST_CODE = 1;
41 
42     // For install request verification
43     private static String sVerifiedUrl;
44 
45 
46     @Override
onCreate(Bundle savedInstanceState)47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49 
50         KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
51 
52         if (km != null) {
53             Intent intent = createConfirmDeviceCredentialIntent(km);
54             if (intent == null) {
55                 Log.d(TAG, "This device is not protected by a password/pin");
56                 startInstallationService();
57                 finish();
58             } else {
59                 startActivityForResult(intent, REQUEST_CODE);
60             }
61         } else {
62             finish();
63         }
64     }
65 
createConfirmDeviceCredentialIntent(KeyguardManager km)66     private Intent createConfirmDeviceCredentialIntent(KeyguardManager km) {
67         final boolean useDefaultStrings =
68                 getIntent().getBooleanExtra(KEY_KEYGUARD_USE_DEFAULT_STRINGS, false);
69         final String title;
70         final String description;
71         if (useDefaultStrings) {
72             // Use default strings provided by keyguard manager
73             title = null;
74             description = null;
75         } else {
76             // Use custom strings provided by DSU
77             title = getString(R.string.keyguard_title);
78             description = getString(R.string.keyguard_description);
79         }
80         return km.createConfirmDeviceCredentialIntent(title, description);
81     }
82 
83     @Override
onActivityResult(int requestCode, int resultCode, Intent data)84     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
85         if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
86             startInstallationService();
87         } else {
88             notifyKeyguardDismissed();
89         }
90 
91         finish();
92     }
93 
notifyKeyguardDismissed()94     private void notifyKeyguardDismissed() {
95         Intent intent = new Intent(this, DynamicSystemInstallationService.class);
96         intent.setAction(ACTION_NOTIFY_KEYGUARD_DISMISSED);
97         startServiceAsUser(intent, UserHandle.SYSTEM);
98     }
99 
startInstallationService()100     private void startInstallationService() {
101         // retrieve data from calling intent
102         Intent callingIntent = getIntent();
103         Uri url = callingIntent.getData();
104 
105         if (url != null) {
106             sVerifiedUrl = url.toString();
107         }
108 
109         // start service
110         Intent intent = new Intent(this, DynamicSystemInstallationService.class);
111         if (url != null) {
112             intent.setData(url);
113         }
114         intent.setAction(DynamicSystemClient.ACTION_START_INSTALL);
115         intent.putExtras(callingIntent);
116 
117         Log.d(TAG, "Starting Installation Service");
118         startServiceAsUser(intent, UserHandle.SYSTEM);
119     }
120 
isVerified(String url)121     static boolean isVerified(String url) {
122         if (url == null) return true;
123         return sVerifiedUrl != null && sVerifiedUrl.equals(url);
124     }
125 }
126