1 /*
2  * Copyright 2012 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.test.wrappedgtest;
18 
19 import android.app.Activity;
20 import android.app.Instrumentation;
21 import android.app.KeyguardManager;
22 import android.content.Context;
23 import android.content.pm.PackageManager;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.util.Log;
27 
28 
29 public class WrappedGTestInstrumentation extends Instrumentation {
30 
31     private static final String TAG = "WrappedGTestInstrumentation";
32     private WrappedGTestActivity mActivity;
33     protected Class mActivityClass;
34 
WrappedGTestInstrumentation()35     public WrappedGTestInstrumentation() {
36     }
37 
38     @Override
onCreate(Bundle arguments)39     public void onCreate(Bundle arguments) {
40         // attempt to disable keyguard,  if current test has permission to do so
41         if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD)
42                 == PackageManager.PERMISSION_GRANTED) {
43             Log.i(TAG, "Disabling keyguard");
44             KeyguardManager keyguardManager =
45                 (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE);
46             keyguardManager.newKeyguardLock("cts").disableKeyguard();
47         } else {
48             Log.i(TAG, "Test lacks permission to disable keyguard. " +
49                     "UI based tests may fail if keyguard is up");
50         }
51         super.onCreate(arguments);
52         start();
53     }
54 
55     @Override
onStart()56     public void onStart() {
57         super.onStart();
58 
59         Intent intent = new Intent(getTargetContext(), mActivityClass);
60         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
61 
62         mActivity = (WrappedGTestActivity)startActivitySync(intent);
63         mActivity.setInstrumentation(this);
64         mActivity.runGTests();
65 
66         finish(Activity.RESULT_OK, new Bundle());
67     }
68 }
69