Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.app.ActivityManagerNative; |
| 20 | import android.app.admin.DevicePolicyManager; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
Jorim Jaggi | 97b63c4 | 2014-05-02 23:03:34 +0200 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 26 | import android.content.pm.ResolveInfo; |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 27 | import android.os.RemoteException; |
| 28 | import android.os.UserHandle; |
| 29 | import android.provider.MediaStore; |
| 30 | import android.util.AttributeSet; |
| 31 | import android.util.Log; |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 32 | import android.view.View; |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 33 | import android.view.accessibility.AccessibilityManager; |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 34 | import android.widget.FrameLayout; |
Adrian Roos | 2fe592d | 2014-05-17 03:11:59 +0200 | [diff] [blame] | 35 | import android.widget.ImageView; |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 36 | |
| 37 | import com.android.internal.widget.LockPatternUtils; |
| 38 | import com.android.keyguard.KeyguardUpdateMonitor; |
| 39 | import com.android.keyguard.KeyguardUpdateMonitorCallback; |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 40 | import com.android.systemui.R; |
Adrian Roos | 1e1d6ac | 2014-07-22 17:18:55 +0200 | [diff] [blame^] | 41 | import com.android.systemui.statusbar.policy.FlashlightController; |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Implementation for the bottom area of the Keyguard, including camera/phone affordance and status |
| 45 | * text. |
| 46 | */ |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 47 | public class KeyguardBottomAreaView extends FrameLayout implements View.OnClickListener, |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 48 | UnlockMethodCache.OnUnlockMethodChangedListener { |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 49 | |
| 50 | final static String TAG = "PhoneStatusBar/KeyguardBottomAreaView"; |
| 51 | |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 52 | private static final Intent SECURE_CAMERA_INTENT = |
| 53 | new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE) |
| 54 | .addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 55 | private static final Intent INSECURE_CAMERA_INTENT = |
| 56 | new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); |
Jorim Jaggi | 97b63c4 | 2014-05-02 23:03:34 +0200 | [diff] [blame] | 57 | private static final Intent PHONE_INTENT = new Intent(Intent.ACTION_DIAL); |
| 58 | |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 59 | private ImageView mCameraImageView; |
| 60 | private ImageView mPhoneImageView; |
Adrian Roos | 2fe592d | 2014-05-17 03:11:59 +0200 | [diff] [blame] | 61 | private ImageView mLockIcon; |
Selim Cinek | f99d000 | 2014-06-13 07:36:01 +0200 | [diff] [blame] | 62 | private View mIndicationText; |
Jorim Jaggi | 97b63c4 | 2014-05-02 23:03:34 +0200 | [diff] [blame] | 63 | |
Jorim Jaggi | 97b63c4 | 2014-05-02 23:03:34 +0200 | [diff] [blame] | 64 | private ActivityStarter mActivityStarter; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 65 | private UnlockMethodCache mUnlockMethodCache; |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 66 | private LockPatternUtils mLockPatternUtils; |
Adrian Roos | 1e1d6ac | 2014-07-22 17:18:55 +0200 | [diff] [blame^] | 67 | private FlashlightController mFlashlightController; |
Adrian Roos | 2fe592d | 2014-05-17 03:11:59 +0200 | [diff] [blame] | 68 | |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 69 | public KeyguardBottomAreaView(Context context) { |
| 70 | super(context); |
| 71 | } |
| 72 | |
| 73 | public KeyguardBottomAreaView(Context context, AttributeSet attrs) { |
| 74 | super(context, attrs); |
| 75 | } |
| 76 | |
| 77 | public KeyguardBottomAreaView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 78 | super(context, attrs, defStyleAttr); |
| 79 | } |
| 80 | |
| 81 | public KeyguardBottomAreaView(Context context, AttributeSet attrs, int defStyleAttr, |
| 82 | int defStyleRes) { |
| 83 | super(context, attrs, defStyleAttr, defStyleRes); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | protected void onFinishInflate() { |
| 88 | super.onFinishInflate(); |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 89 | mLockPatternUtils = new LockPatternUtils(mContext); |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 90 | mCameraImageView = (ImageView) findViewById(R.id.camera_button); |
| 91 | mPhoneImageView = (ImageView) findViewById(R.id.phone_button); |
Adrian Roos | 2fe592d | 2014-05-17 03:11:59 +0200 | [diff] [blame] | 92 | mLockIcon = (ImageView) findViewById(R.id.lock_icon); |
Selim Cinek | f99d000 | 2014-06-13 07:36:01 +0200 | [diff] [blame] | 93 | mIndicationText = findViewById(R.id.keyguard_indication_text); |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 94 | watchForCameraPolicyChanges(); |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 95 | watchForAccessibilityChanges(); |
| 96 | updateCameraVisibility(); |
Jorim Jaggi | 97b63c4 | 2014-05-02 23:03:34 +0200 | [diff] [blame] | 97 | updatePhoneVisibility(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 98 | mUnlockMethodCache = UnlockMethodCache.getInstance(getContext()); |
| 99 | mUnlockMethodCache.addListener(this); |
Adrian Roos | 2fe592d | 2014-05-17 03:11:59 +0200 | [diff] [blame] | 100 | updateTrust(); |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Jorim Jaggi | 97b63c4 | 2014-05-02 23:03:34 +0200 | [diff] [blame] | 103 | public void setActivityStarter(ActivityStarter activityStarter) { |
| 104 | mActivityStarter = activityStarter; |
| 105 | } |
| 106 | |
Adrian Roos | 1e1d6ac | 2014-07-22 17:18:55 +0200 | [diff] [blame^] | 107 | public void setFlashlightController(FlashlightController flashlightController) { |
| 108 | mFlashlightController = flashlightController; |
| 109 | } |
| 110 | |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 111 | private Intent getCameraIntent() { |
| 112 | KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext); |
| 113 | boolean currentUserHasTrust = updateMonitor.getUserHasTrust( |
| 114 | mLockPatternUtils.getCurrentUser()); |
| 115 | return mLockPatternUtils.isSecure() && !currentUserHasTrust |
| 116 | ? SECURE_CAMERA_INTENT : INSECURE_CAMERA_INTENT; |
| 117 | } |
| 118 | |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 119 | private void updateCameraVisibility() { |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 120 | ResolveInfo resolved = mContext.getPackageManager().resolveActivityAsUser(getCameraIntent(), |
| 121 | PackageManager.MATCH_DEFAULT_ONLY, |
| 122 | mLockPatternUtils.getCurrentUser()); |
| 123 | boolean visible = !isCameraDisabledByDpm() && resolved != null; |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 124 | mCameraImageView.setVisibility(visible ? View.VISIBLE : View.GONE); |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Jorim Jaggi | 97b63c4 | 2014-05-02 23:03:34 +0200 | [diff] [blame] | 127 | private void updatePhoneVisibility() { |
| 128 | boolean visible = isPhoneVisible(); |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 129 | mPhoneImageView.setVisibility(visible ? View.VISIBLE : View.GONE); |
Jorim Jaggi | 97b63c4 | 2014-05-02 23:03:34 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | private boolean isPhoneVisible() { |
| 133 | PackageManager pm = mContext.getPackageManager(); |
| 134 | return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) |
| 135 | && pm.resolveActivity(PHONE_INTENT, 0) != null; |
| 136 | } |
| 137 | |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 138 | private boolean isCameraDisabledByDpm() { |
| 139 | final DevicePolicyManager dpm = |
| 140 | (DevicePolicyManager) getContext().getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 141 | if (dpm != null) { |
| 142 | try { |
| 143 | final int userId = ActivityManagerNative.getDefault().getCurrentUser().id; |
| 144 | final int disabledFlags = dpm.getKeyguardDisabledFeatures(null, userId); |
| 145 | final boolean disabledBecauseKeyguardSecure = |
| 146 | (disabledFlags & DevicePolicyManager.KEYGUARD_DISABLE_SECURE_CAMERA) != 0 |
| 147 | && KeyguardTouchDelegate.getInstance(getContext()).isSecure(); |
| 148 | return dpm.getCameraDisabled(null) || disabledBecauseKeyguardSecure; |
| 149 | } catch (RemoteException e) { |
| 150 | Log.e(TAG, "Can't get userId", e); |
| 151 | } |
| 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 156 | private void watchForCameraPolicyChanges() { |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 157 | final IntentFilter filter = new IntentFilter(); |
| 158 | filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 159 | getContext().registerReceiverAsUser(mDevicePolicyReceiver, |
| 160 | UserHandle.ALL, filter, null, null); |
| 161 | KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mUpdateMonitorCallback); |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | private void watchForAccessibilityChanges() { |
| 165 | final AccessibilityManager am = |
| 166 | (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 167 | |
| 168 | // Set the initial state |
| 169 | enableAccessibility(am.isTouchExplorationEnabled()); |
| 170 | |
| 171 | // Watch for changes |
| 172 | am.addTouchExplorationStateChangeListener( |
| 173 | new AccessibilityManager.TouchExplorationStateChangeListener() { |
| 174 | @Override |
| 175 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 176 | enableAccessibility(enabled); |
| 177 | } |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | private void enableAccessibility(boolean touchExplorationEnabled) { |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 182 | mCameraImageView.setOnClickListener(touchExplorationEnabled ? this : null); |
| 183 | mCameraImageView.setClickable(touchExplorationEnabled); |
| 184 | mPhoneImageView.setOnClickListener(touchExplorationEnabled ? this : null); |
| 185 | mPhoneImageView.setClickable(touchExplorationEnabled); |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 188 | @Override |
| 189 | public void onClick(View v) { |
| 190 | if (v == mCameraImageView) { |
| 191 | launchCamera(); |
| 192 | } else if (v == mPhoneImageView) { |
| 193 | launchPhone(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | public void launchCamera() { |
Adrian Roos | 1e1d6ac | 2014-07-22 17:18:55 +0200 | [diff] [blame^] | 198 | mFlashlightController.killFlashlight(); |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 199 | Intent intent = getCameraIntent(); |
| 200 | if (intent == SECURE_CAMERA_INTENT) { |
| 201 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 202 | } else { |
| 203 | mActivityStarter.startActivity(intent); |
| 204 | } |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 207 | public void launchPhone() { |
Jorim Jaggi | 97b63c4 | 2014-05-02 23:03:34 +0200 | [diff] [blame] | 208 | mActivityStarter.startActivity(PHONE_INTENT); |
| 209 | } |
| 210 | |
Adrian Roos | 2fe592d | 2014-05-17 03:11:59 +0200 | [diff] [blame] | 211 | |
| 212 | @Override |
| 213 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 214 | super.onVisibilityChanged(changedView, visibility); |
| 215 | if (changedView == this && visibility == VISIBLE) { |
| 216 | updateTrust(); |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 217 | updateCameraVisibility(); |
Adrian Roos | 2fe592d | 2014-05-17 03:11:59 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | private void updateTrust() { |
| 222 | if (getVisibility() != VISIBLE) { |
| 223 | return; |
| 224 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 225 | int iconRes = mUnlockMethodCache.isMethodInsecure() |
| 226 | ? R.drawable.ic_lock_open_24dp |
| 227 | : R.drawable.ic_lock_24dp; |
Adrian Roos | 2fe592d | 2014-05-17 03:11:59 +0200 | [diff] [blame] | 228 | mLockIcon.setImageResource(iconRes); |
| 229 | } |
| 230 | |
Selim Cinek | 4c6969a | 2014-05-26 19:22:17 +0200 | [diff] [blame] | 231 | public ImageView getPhoneImageView() { |
| 232 | return mPhoneImageView; |
| 233 | } |
| 234 | |
| 235 | public ImageView getCameraImageView() { |
| 236 | return mCameraImageView; |
| 237 | } |
| 238 | |
| 239 | public ImageView getLockIcon() { |
| 240 | return mLockIcon; |
| 241 | } |
| 242 | |
Selim Cinek | f99d000 | 2014-06-13 07:36:01 +0200 | [diff] [blame] | 243 | public View getIndicationView() { |
| 244 | return mIndicationText; |
| 245 | } |
| 246 | |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 247 | @Override |
Jorim Jaggi | 2580a976 | 2014-06-25 03:08:25 +0200 | [diff] [blame] | 248 | public boolean hasOverlappingRendering() { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | @Override |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 253 | public void onMethodSecureChanged(boolean methodSecure) { |
| 254 | updateTrust(); |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 255 | updateCameraVisibility(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 256 | } |
Adrian Roos | a06905b | 2014-05-28 12:13:48 +0200 | [diff] [blame] | 257 | |
| 258 | private final BroadcastReceiver mDevicePolicyReceiver = new BroadcastReceiver() { |
| 259 | public void onReceive(Context context, Intent intent) { |
| 260 | post(new Runnable() { |
| 261 | @Override |
| 262 | public void run() { |
| 263 | updateCameraVisibility(); |
| 264 | } |
| 265 | }); |
| 266 | } |
| 267 | }; |
| 268 | |
| 269 | private final KeyguardUpdateMonitorCallback mUpdateMonitorCallback = |
| 270 | new KeyguardUpdateMonitorCallback() { |
| 271 | @Override |
| 272 | public void onUserSwitchComplete(int userId) { |
| 273 | updateCameraVisibility(); |
| 274 | } |
| 275 | }; |
Jorim Jaggi | a631029 | 2014-04-16 14:11:52 +0200 | [diff] [blame] | 276 | } |