Home | History | Annotate | Download | only in phone
      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 com.android.systemui.statusbar.phone;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.ServiceConnection;
     23 import android.os.IBinder;
     24 import android.os.RemoteException;
     25 import android.os.UserHandle;
     26 import android.util.Slog;
     27 import android.view.MotionEvent;
     28 
     29 import com.android.internal.policy.IKeyguardService;
     30 
     31 import java.util.ArrayList;
     32 import java.util.List;
     33 
     34 
     35 /**
     36  * Facilitates event communication between navigation bar and keyguard.  Currently used to
     37  * control WidgetPager in keyguard to expose the camera widget.
     38  *
     39  */
     40 public class KeyguardTouchDelegate {
     41     // TODO: propagate changes to these to {@link KeyguardServiceDelegate}
     42     static final String KEYGUARD_PACKAGE = "com.android.systemui";
     43     static final String KEYGUARD_CLASS = "com.android.systemui.keyguard.KeyguardService";
     44 
     45     private static KeyguardTouchDelegate sInstance;
     46     private static final List<OnKeyguardConnectionListener> sConnectionListeners =
     47             new ArrayList<OnKeyguardConnectionListener>();
     48 
     49     private volatile IKeyguardService mService;
     50 
     51     protected static final boolean DEBUG = false;
     52     protected static final String TAG = "KeyguardTouchDelegate";
     53 
     54     private final ServiceConnection mKeyguardConnection = new ServiceConnection() {
     55         @Override
     56         public void onServiceConnected(ComponentName name, IBinder service) {
     57             Slog.v(TAG, "Connected to keyguard");
     58             mService = IKeyguardService.Stub.asInterface(service);
     59 
     60             for (int i = 0; i < sConnectionListeners.size(); i++) {
     61                 OnKeyguardConnectionListener listener = sConnectionListeners.get(i);
     62                 listener.onKeyguardServiceConnected(KeyguardTouchDelegate.this);
     63             }
     64         }
     65 
     66         @Override
     67         public void onServiceDisconnected(ComponentName name) {
     68             Slog.v(TAG, "Disconnected from keyguard");
     69             mService = null;
     70             sInstance = null; // force reconnection if this goes away
     71 
     72             for (int i = 0; i < sConnectionListeners.size(); i++) {
     73                 OnKeyguardConnectionListener listener = sConnectionListeners.get(i);
     74                 listener.onKeyguardServiceDisconnected(KeyguardTouchDelegate.this);
     75             }
     76         }
     77 
     78     };
     79 
     80     private KeyguardTouchDelegate(Context context) {
     81         Intent intent = new Intent();
     82         intent.setClassName(KEYGUARD_PACKAGE, KEYGUARD_CLASS);
     83         if (!context.bindServiceAsUser(intent, mKeyguardConnection,
     84                 Context.BIND_AUTO_CREATE, UserHandle.OWNER)) {
     85             if (DEBUG) Slog.v(TAG, "*** Keyguard: can't bind to " + KEYGUARD_CLASS);
     86         } else {
     87             if (DEBUG) Slog.v(TAG, "*** Keyguard started");
     88         }
     89     }
     90 
     91     public static KeyguardTouchDelegate getInstance(Context context) {
     92         KeyguardTouchDelegate instance = sInstance;
     93         if (instance == null) {
     94             instance = sInstance = new KeyguardTouchDelegate(context);
     95         }
     96         return instance;
     97     }
     98 
     99     public boolean isSecure() {
    100         final IKeyguardService service = mService;
    101         if (service != null) {
    102             try {
    103                 return service.isSecure();
    104             } catch (RemoteException e) {
    105                 Slog.e(TAG, "RemoteException calling keyguard.isSecure()!", e);
    106             }
    107         } else {
    108             Slog.w(TAG, "isSecure(): NO SERVICE!");
    109         }
    110         return false;
    111     }
    112 
    113     public boolean dispatch(MotionEvent event) {
    114         final IKeyguardService service = mService;
    115         if (service != null) {
    116             try {
    117                 service.dispatch(event);
    118                 return true;
    119             } catch (RemoteException e) {
    120                 // What to do?
    121                 Slog.e(TAG, "RemoteException sending event to keyguard!", e);
    122             }
    123         } else {
    124             Slog.w(TAG, "dispatch(event): NO SERVICE!");
    125         }
    126         return false;
    127     }
    128 
    129     public boolean isInputRestricted() {
    130         final IKeyguardService service = mService;
    131         if (service != null) {
    132             try {
    133                 return service.isInputRestricted();
    134             } catch (RemoteException e) {
    135                 Slog.w(TAG , "Remote Exception", e);
    136             }
    137         } else {
    138             Slog.w(TAG, "isInputRestricted(): NO SERVICE!");
    139         }
    140         return false;
    141     }
    142 
    143     public boolean isShowingAndNotOccluded() {
    144         final IKeyguardService service = mService;
    145         if (service != null) {
    146             try {
    147                 return service.isShowingAndNotOccluded();
    148             } catch (RemoteException e) {
    149                 Slog.w(TAG , "Remote Exception", e);
    150             }
    151         } else {
    152             Slog.w(TAG, "isShowingAndNotOccluded(): NO SERVICE!");
    153         }
    154         return false;
    155     }
    156 
    157     public void showAssistant() {
    158         final IKeyguardService service = mService;
    159         if (service != null) {
    160             try {
    161                 service.showAssistant();
    162             } catch (RemoteException e) {
    163                 // What to do?
    164                 Slog.e(TAG, "RemoteException launching assistant!", e);
    165             }
    166         } else {
    167             Slog.w(TAG, "showAssistant(event): NO SERVICE!");
    168         }
    169     }
    170 
    171     public void launchCamera() {
    172         final IKeyguardService service = mService;
    173         if (service != null) {
    174             try {
    175                 service.launchCamera();
    176             } catch (RemoteException e) {
    177                 // What to do?
    178                 Slog.e(TAG, "RemoteException launching camera!", e);
    179             }
    180         } else {
    181             Slog.w(TAG, "launchCamera(): NO SERVICE!");
    182         }
    183     }
    184 
    185     public void dismiss() {
    186         final IKeyguardService service = mService;
    187         if (service != null) {
    188             try {
    189                 service.dismiss();
    190             } catch (RemoteException e) {
    191                 // What to do?
    192                 Slog.e(TAG, "RemoteException dismissing keyguard!", e);
    193             }
    194         } else {
    195             Slog.w(TAG, "dismiss(): NO SERVICE!");
    196         }
    197     }
    198 
    199     public static void addListener(OnKeyguardConnectionListener listener) {
    200         sConnectionListeners.add(listener);
    201     }
    202 
    203     public interface OnKeyguardConnectionListener {
    204 
    205         void onKeyguardServiceConnected(KeyguardTouchDelegate keyguardTouchDelegate);
    206         void onKeyguardServiceDisconnected(KeyguardTouchDelegate keyguardTouchDelegate);
    207     }
    208 }
    209