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