Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 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.app;
     18 
     19 import android.content.Context;
     20 import android.os.Binder;
     21 import android.os.RemoteException;
     22 import android.os.IBinder;
     23 import android.os.ServiceManager;
     24 import android.view.IWindowManager;
     25 import android.view.IOnKeyguardExitResult;
     26 
     27 /**
     28  * Class that can be used to lock and unlock the keyboard. Get an instance of this
     29  * class by calling {@link android.content.Context#getSystemService(java.lang.String)}
     30  * with argument {@link android.content.Context#KEYGUARD_SERVICE}. The
     31  * Actual class to control the keyboard locking is
     32  * {@link android.app.KeyguardManager.KeyguardLock}.
     33  */
     34 public class KeyguardManager {
     35     private IWindowManager mWM;
     36 
     37     /**
     38      * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
     39      * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
     40      * instead; this allows you to seamlessly hide the keyguard as your application
     41      * moves in and out of the foreground and does not require that any special
     42      * permissions be requested.
     43      *
     44      * Handle returned by {@link KeyguardManager#newKeyguardLock} that allows
     45      * you to disable / reenable the keyguard.
     46      */
     47     public class KeyguardLock {
     48         private IBinder mToken = new Binder();
     49         private String mTag;
     50 
     51         KeyguardLock(String tag) {
     52             mTag = tag;
     53         }
     54 
     55         /**
     56          * Disable the keyguard from showing.  If the keyguard is currently
     57          * showing, hide it.  The keyguard will be prevented from showing again
     58          * until {@link #reenableKeyguard()} is called.
     59          *
     60          * A good place to call this is from {@link android.app.Activity#onResume()}
     61          *
     62          * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager}
     63          * is enabled that requires a password.
     64          *
     65          * @see #reenableKeyguard()
     66          */
     67         public void disableKeyguard() {
     68             try {
     69                 mWM.disableKeyguard(mToken, mTag);
     70             } catch (RemoteException ex) {
     71             }
     72         }
     73 
     74         /**
     75          * Reenable the keyguard.  The keyguard will reappear if the previous
     76          * call to {@link #disableKeyguard()} caused it it to be hidden.
     77          *
     78          * A good place to call this is from {@link android.app.Activity#onPause()}
     79          *
     80          * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager}
     81          * is enabled that requires a password.
     82          *
     83          * @see #disableKeyguard()
     84          */
     85         public void reenableKeyguard() {
     86             try {
     87                 mWM.reenableKeyguard(mToken);
     88             } catch (RemoteException ex) {
     89             }
     90         }
     91     }
     92 
     93     /**
     94      * Callback passed to {@link KeyguardManager#exitKeyguardSecurely} to notify
     95      * caller of result.
     96      */
     97     public interface OnKeyguardExitResult {
     98 
     99         /**
    100          * @param success True if the user was able to authenticate, false if
    101          *   not.
    102          */
    103         void onKeyguardExitResult(boolean success);
    104     }
    105 
    106 
    107     KeyguardManager() {
    108         mWM = IWindowManager.Stub.asInterface(ServiceManager.getService(Context.WINDOW_SERVICE));
    109     }
    110 
    111     /**
    112      * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
    113      * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
    114      * instead; this allows you to seamlessly hide the keyguard as your application
    115      * moves in and out of the foreground and does not require that any special
    116      * permissions be requested.
    117      *
    118      * Enables you to lock or unlock the keyboard. Get an instance of this class by
    119      * calling {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.
    120      * This class is wrapped by {@link android.app.KeyguardManager KeyguardManager}.
    121      * @param tag A tag that informally identifies who you are (for debugging who
    122      *   is disabling he keyguard).
    123      *
    124      * @return A {@link KeyguardLock} handle to use to disable and reenable the
    125      *   keyguard.
    126      */
    127     @Deprecated
    128     public KeyguardLock newKeyguardLock(String tag) {
    129         return new KeyguardLock(tag);
    130     }
    131 
    132     /**
    133      * isKeyguardLocked
    134      *
    135      * Return whether the keyguard is currently locked.
    136      *
    137      * @return true if in keyguard is locked.
    138      *
    139      * @hide
    140      */
    141     public boolean isKeyguardLocked() {
    142         try {
    143             return mWM.isKeyguardLocked();
    144         } catch (RemoteException ex) {
    145             return false;
    146         }
    147     }
    148 
    149     /**
    150      * isKeyguardSecure
    151      *
    152      * Return whether the keyguard requires a password to unlock.
    153      *
    154      * @return true if in keyguard is secure.
    155      *
    156      * @hide
    157      */
    158     public boolean isKeyguardSecure() {
    159         try {
    160             return mWM.isKeyguardSecure();
    161         } catch (RemoteException ex) {
    162             return false;
    163         }
    164     }
    165 
    166     /**
    167      * If keyguard screen is showing or in restricted key input mode (i.e. in
    168      * keyguard password emergency screen). When in such mode, certain keys,
    169      * such as the Home key and the right soft keys, don't work.
    170      *
    171      * @return true if in keyguard restricted input mode.
    172      *
    173      * @see android.view.WindowManagerPolicy#inKeyguardRestrictedKeyInputMode
    174      */
    175     public boolean inKeyguardRestrictedInputMode() {
    176         try {
    177             return mWM.inKeyguardRestrictedInputMode();
    178         } catch (RemoteException ex) {
    179             return false;
    180         }
    181     }
    182 
    183     /**
    184      * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
    185      * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
    186      * instead; this allows you to seamlessly hide the keyguard as your application
    187      * moves in and out of the foreground and does not require that any special
    188      * permissions be requested.
    189      *
    190      * Exit the keyguard securely.  The use case for this api is that, after
    191      * disabling the keyguard, your app, which was granted permission to
    192      * disable the keyguard and show a limited amount of information deemed
    193      * safe without the user getting past the keyguard, needs to navigate to
    194      * something that is not safe to view without getting past the keyguard.
    195      *
    196      * This will, if the keyguard is secure, bring up the unlock screen of
    197      * the keyguard.
    198      *
    199      * @param callback Let's you know whether the operation was succesful and
    200      *   it is safe to launch anything that would normally be considered safe
    201      *   once the user has gotten past the keyguard.
    202      */
    203     @Deprecated
    204     public void exitKeyguardSecurely(final OnKeyguardExitResult callback) {
    205         try {
    206             mWM.exitKeyguardSecurely(new IOnKeyguardExitResult.Stub() {
    207                 public void onKeyguardExitResult(boolean success) throws RemoteException {
    208                     callback.onKeyguardExitResult(success);
    209                 }
    210             });
    211         } catch (RemoteException e) {
    212 
    213         }
    214     }
    215 }
    216