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