• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.cts.verifier.admin;
18 
19 import com.android.cts.verifier.managedprovisioning.DeviceAdminTestReceiver;
20 import com.android.cts.verifier.PassFailButtons;
21 import com.android.cts.verifier.R;
22 
23 import android.app.AlertDialog;
24 import android.app.KeyguardManager;
25 import android.app.admin.DevicePolicyManager;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentFilter;
30 import android.os.Bundle;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.widget.Button;
34 
35 public class ScreenLockTestActivity extends PassFailButtons.Activity {
36 
37     private static final int ADD_DEVICE_ADMIN_REQUEST_CODE = 1;
38 
39     private ScreenOffReceiver mReceiver;
40 
41     private Button mForceLockButton;
42 
43     private DevicePolicyManager mDevicePolicyManager;
44 
45     private KeyguardManager mKeyguardManager;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         setContentView(R.layout.da_screen_lock_main);
51         setPassFailButtonClickListeners();
52         setInfoResources(R.string.da_screen_lock_test, R.string.da_screen_lock_info, -1);
53 
54         mDevicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
55         mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
56 
57         getPassButton().setEnabled(false);
58 
59         mForceLockButton = (Button) findViewById(R.id.da_force_lock_button);
60         mForceLockButton.setOnClickListener(new OnClickListener() {
61             @Override
62             public void onClick(View v) {
63                 sendAddDeviceAdminIntent();
64             }
65         });
66 
67         mReceiver = new ScreenOffReceiver();
68         IntentFilter filter = new IntentFilter();
69         filter.addAction(Intent.ACTION_SCREEN_OFF);
70         registerReceiver(mReceiver, filter);
71     }
72 
sendAddDeviceAdminIntent()73     private void sendAddDeviceAdminIntent() {
74         Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
75         intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
76                 DeviceAdminTestReceiver.getReceiverComponentName());
77         startActivityForResult(intent, ADD_DEVICE_ADMIN_REQUEST_CODE);
78     }
79 
80     @Override
onActivityResult(int requestCode, int resultCode, Intent data)81     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
82         super.onActivityResult(requestCode, resultCode, data);
83         switch (requestCode) {
84             case ADD_DEVICE_ADMIN_REQUEST_CODE:
85                 handleAddDeviceAdminResult(resultCode, data);
86                 break;
87         }
88     }
89 
handleAddDeviceAdminResult(int resultCode, Intent data)90     private void handleAddDeviceAdminResult(int resultCode, Intent data) {
91         if (resultCode == RESULT_OK) {
92             mDevicePolicyManager.lockNow();
93         }
94     }
95 
96     @Override
onDestroy()97     protected void onDestroy() {
98         super.onDestroy();
99         unregisterReceiver(mReceiver);
100     }
101 
102     private class ScreenOffReceiver extends BroadcastReceiver {
103 
104         private static final int LOCK_CHECK_DELAY = 1000;
105 
106         @Override
onReceive(Context context, Intent intent)107         public void onReceive(Context context, Intent intent) {
108             mForceLockButton.postDelayed(new Runnable() {
109                 @Override
110                 public void run() {
111                     boolean lockSuccess = mKeyguardManager.inKeyguardRestrictedInputMode();
112                     getPassButton().setEnabled(lockSuccess);
113 
114                     int iconId = lockSuccess
115                             ? android.R.drawable.ic_dialog_info
116                             : android.R.drawable.ic_dialog_alert;
117                     int messageId = lockSuccess
118                             ? R.string.da_lock_success
119                             : R.string.da_lock_error;
120                     new AlertDialog.Builder(ScreenLockTestActivity.this)
121                         .setTitle(R.string.da_screen_lock_test)
122                         .setMessage(messageId)
123                         .setIcon(iconId)
124                         .setPositiveButton(android.R.string.ok, null)
125                         .show();
126                 }
127             }, LOCK_CHECK_DELAY);
128         }
129     }
130 }
131