1 /*
2  * Copyright (C) 2013 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 android.admin.app;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.Process;
23 import android.util.Log;
24 import android.view.WindowManager;
25 
26 import com.google.common.annotations.VisibleForTesting;
27 
28 /**
29  * Helper {@link Activity} for CTS tests of Device Admin activation. The {@code Activity}
30  * enables tests to capture the invocations of its {@link #onActivityResult(int, int, Intent)} by
31  * providing a {@link OnActivityResultListener}.
32  */
33 public class CtsDeviceAdminActivationTestActivity extends Activity {
34 
35     private static final String TAG = CtsDeviceAdminActivationTestActivity.class.getSimpleName();
36 
37     public interface OnActivityResultListener {
onActivityResult(int requestCode, int resultCode, Intent data)38         void onActivityResult(int requestCode, int resultCode, Intent data);
39     }
40 
41     private OnActivityResultListener mOnActivityResultListener;
42 
43     @Override
onCreate(Bundle savedInstanceState)44     protected void onCreate(Bundle savedInstanceState) {
45         Log.d(TAG, "onCreate(): user=" + Process.myUserHandle());
46         super.onCreate(savedInstanceState);
47 
48         // Dismiss keyguard and keep screen on while this Activity is displayed.
49         // This is needed because on older platforms, when the keyguard is on, onActivityResult is
50         // not invoked when a Device Admin activation is rejected without user interaction.
51         getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
52                 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
53                 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
54     }
55 
56     @VisibleForTesting
setOnActivityResultListener(OnActivityResultListener listener)57     public void setOnActivityResultListener(OnActivityResultListener listener) {
58         mOnActivityResultListener = listener;
59     }
60 
61     @Override
onActivityResult(int requestCode, int resultCode, Intent data)62     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
63         Log.d(TAG, "onActivityResult(): requestCode=" + requestCode + ", resultCode=" + resultCode
64                 + ", listener=" + mOnActivityResultListener + ", user=" + Process.myUserHandle());
65         if (mOnActivityResultListener != null) {
66             mOnActivityResultListener.onActivityResult(requestCode, resultCode, data);
67             return;
68         }
69 
70         super.onActivityResult(requestCode, resultCode, data);
71     }
72 }
73