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 android.app.Activity;
20 import android.app.KeyguardManager;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.os.UserHandle;
26 import android.os.image.DynamicSystemClient;
27 import android.util.Log;
28 
29 /**
30  * This Activity starts KeyguardManager and ask the user to confirm before any installation request.
31  * If the device is not protected by a password, it approves the request by default.
32  */
33 public class VerificationActivity extends Activity {
34 
35     private static final String TAG = "VerificationActivity";
36 
37     private static final int REQUEST_CODE = 1;
38 
39     // For install request verification
40     private static String sVerifiedUrl;
41 
42 
43     @Override
onCreate(Bundle savedInstanceState)44     protected void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46 
47         KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
48 
49         if (km != null) {
50             String title = getString(R.string.keyguard_title);
51             String description = getString(R.string.keyguard_description);
52             Intent intent = km.createConfirmDeviceCredentialIntent(title, description);
53 
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 
66     @Override
onActivityResult(int requestCode, int resultCode, Intent data)67     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
68         if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
69             startInstallationService();
70         }
71 
72         finish();
73     }
74 
startInstallationService()75     private void startInstallationService() {
76         // retrieve data from calling intent
77         Intent callingIntent = getIntent();
78         Uri url = callingIntent.getData();
79 
80         if (url != null) {
81             sVerifiedUrl = url.toString();
82         }
83 
84         // start service
85         Intent intent = new Intent(this, DynamicSystemInstallationService.class);
86         if (url != null) {
87             intent.setData(url);
88         }
89         intent.setAction(DynamicSystemClient.ACTION_START_INSTALL);
90         intent.putExtras(callingIntent);
91 
92         Log.d(TAG, "Starting Installation Service");
93         startServiceAsUser(intent, UserHandle.SYSTEM);
94     }
95 
isVerified(String url)96     static boolean isVerified(String url) {
97         if (url == null) return true;
98         return sVerifiedUrl != null && sVerifiedUrl.equals(url);
99     }
100 }
101