1 /*
2  * Copyright (C) 2017 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 package com.android.compatibility.common.util.devicepolicy.provisioning;
17 
18 import android.app.Activity;
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.os.RemoteException;
22 import android.util.Log;
23 import android.view.WindowManager;
24 
25 /**
26  * Must register it in AndroidManifest.xml
27  * <activity android:name="com.android.compatibility.common.util.devicepolicy.provisioning.StartProvisioningActivity"></activity>
28  */
29 public class StartProvisioningActivity extends Activity {
30     private static final int REQUEST_CODE = 1;
31     private static final String TAG = "StartProvisionActivity";
32 
33     public static final String EXTRA_BOOLEAN_CALLBACK = "EXTRA_BOOLEAN_CALLBACK";
34 
35     IBooleanCallback mResultCallback;
36 
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40 
41         // Reduce flakiness of the test
42         // Show activity on top of keyguard
43         getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
44         // Turn on screen to prevent activity being paused by system
45         getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
46         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
47 
48         mResultCallback = IBooleanCallback.Stub.asInterface(
49                 getIntent().getExtras().getBinder(EXTRA_BOOLEAN_CALLBACK));
50         Log.i(TAG, "result callback class name " + mResultCallback);
51 
52         // Only provision it if the activity is not re-created
53         if (savedInstanceState == null) {
54             Intent provisioningIntent = getIntent().getParcelableExtra(Intent.EXTRA_INTENT);
55 
56             startActivityForResult(provisioningIntent, REQUEST_CODE);
57             Log.i(TAG, "Start provisioning intent");
58         }
59     }
60 
61     @Override
onActivityResult(int requestCode, int resultCode, Intent data)62     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
63         if (requestCode == REQUEST_CODE) {
64             try {
65                 boolean result = resultCode == RESULT_OK;
66                 mResultCallback.onResult(result);
67                 Log.i(TAG, "onActivityResult result: " + result);
68             } catch (RemoteException e) {
69                 Log.e(TAG, "onActivityResult", e);
70             }
71         } else {
72             super.onActivityResult(requestCode, resultCode, data);
73         }
74     }
75 }