OSDN Git Service

Enforce policy for camera gesture in keyguard
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / statusbar / phone / KeyguardBottomAreaView.java
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 static android.view.accessibility.AccessibilityNodeInfo.ACTION_CLICK;
20 import static android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
21
22 import android.app.ActivityManager;
23 import android.app.ActivityManagerNative;
24 import android.app.ActivityOptions;
25 import android.app.admin.DevicePolicyManager;
26 import android.content.BroadcastReceiver;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.content.IntentFilter;
31 import android.content.ServiceConnection;
32 import android.content.pm.ActivityInfo;
33 import android.content.pm.PackageManager;
34 import android.content.pm.ResolveInfo;
35 import android.content.res.Configuration;
36 import android.os.AsyncTask;
37 import android.os.Bundle;
38 import android.os.IBinder;
39 import android.os.Message;
40 import android.os.Messenger;
41 import android.os.RemoteException;
42 import android.os.UserHandle;
43 import android.provider.MediaStore;
44 import android.service.media.CameraPrewarmService;
45 import android.telecom.TelecomManager;
46 import android.util.AttributeSet;
47 import android.util.Log;
48 import android.util.TypedValue;
49 import android.view.View;
50 import android.view.ViewGroup;
51 import android.view.WindowManager;
52 import android.view.accessibility.AccessibilityNodeInfo;
53 import android.widget.FrameLayout;
54 import android.widget.TextView;
55
56 import com.android.internal.widget.LockPatternUtils;
57 import com.android.keyguard.KeyguardUpdateMonitor;
58 import com.android.keyguard.KeyguardUpdateMonitorCallback;
59 import com.android.systemui.EventLogConstants;
60 import com.android.systemui.EventLogTags;
61 import com.android.systemui.Interpolators;
62 import com.android.systemui.R;
63 import com.android.systemui.assist.AssistManager;
64 import com.android.systemui.statusbar.CommandQueue;
65 import com.android.systemui.statusbar.KeyguardAffordanceView;
66 import com.android.systemui.statusbar.KeyguardIndicationController;
67 import com.android.systemui.statusbar.policy.AccessibilityController;
68 import com.android.systemui.statusbar.policy.FlashlightController;
69 import com.android.systemui.statusbar.policy.PreviewInflater;
70
71 /**
72  * Implementation for the bottom area of the Keyguard, including camera/phone affordance and status
73  * text.
74  */
75 public class KeyguardBottomAreaView extends FrameLayout implements View.OnClickListener,
76         UnlockMethodCache.OnUnlockMethodChangedListener,
77         AccessibilityController.AccessibilityStateChangedCallback, View.OnLongClickListener {
78
79     final static String TAG = "PhoneStatusBar/KeyguardBottomAreaView";
80
81     public static final String CAMERA_LAUNCH_SOURCE_AFFORDANCE = "lockscreen_affordance";
82     public static final String CAMERA_LAUNCH_SOURCE_WIGGLE = "wiggle_gesture";
83     public static final String CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP = "power_double_tap";
84
85     public static final String EXTRA_CAMERA_LAUNCH_SOURCE
86             = "com.android.systemui.camera_launch_source";
87
88     private static final Intent SECURE_CAMERA_INTENT =
89             new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE)
90                     .addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
91     public static final Intent INSECURE_CAMERA_INTENT =
92             new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
93     private static final Intent PHONE_INTENT = new Intent(Intent.ACTION_DIAL);
94     private static final int DOZE_ANIMATION_STAGGER_DELAY = 48;
95     private static final int DOZE_ANIMATION_ELEMENT_DURATION = 250;
96
97     private KeyguardAffordanceView mCameraImageView;
98     private KeyguardAffordanceView mLeftAffordanceView;
99     private LockIcon mLockIcon;
100     private TextView mIndicationText;
101     private ViewGroup mPreviewContainer;
102
103     private View mLeftPreview;
104     private View mCameraPreview;
105
106     private ActivityStarter mActivityStarter;
107     private UnlockMethodCache mUnlockMethodCache;
108     private LockPatternUtils mLockPatternUtils;
109     private FlashlightController mFlashlightController;
110     private PreviewInflater mPreviewInflater;
111     private KeyguardIndicationController mIndicationController;
112     private AccessibilityController mAccessibilityController;
113     private PhoneStatusBar mPhoneStatusBar;
114     private KeyguardAffordanceHelper mAffordanceHelper;
115
116     private boolean mUserSetupComplete;
117     private boolean mPrewarmBound;
118     private Messenger mPrewarmMessenger;
119     private final ServiceConnection mPrewarmConnection = new ServiceConnection() {
120
121         @Override
122         public void onServiceConnected(ComponentName name, IBinder service) {
123             mPrewarmMessenger = new Messenger(service);
124         }
125
126         @Override
127         public void onServiceDisconnected(ComponentName name) {
128             mPrewarmMessenger = null;
129         }
130     };
131
132     private boolean mLeftIsVoiceAssist;
133     private AssistManager mAssistManager;
134
135     public KeyguardBottomAreaView(Context context) {
136         this(context, null);
137     }
138
139     public KeyguardBottomAreaView(Context context, AttributeSet attrs) {
140         this(context, attrs, 0);
141     }
142
143     public KeyguardBottomAreaView(Context context, AttributeSet attrs, int defStyleAttr) {
144         this(context, attrs, defStyleAttr, 0);
145     }
146
147     public KeyguardBottomAreaView(Context context, AttributeSet attrs, int defStyleAttr,
148             int defStyleRes) {
149         super(context, attrs, defStyleAttr, defStyleRes);
150     }
151
152     private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
153         @Override
154         public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
155             super.onInitializeAccessibilityNodeInfo(host, info);
156             String label = null;
157             if (host == mLockIcon) {
158                 label = getResources().getString(R.string.unlock_label);
159             } else if (host == mCameraImageView) {
160                 label = getResources().getString(R.string.camera_label);
161             } else if (host == mLeftAffordanceView) {
162                 if (mLeftIsVoiceAssist) {
163                     label = getResources().getString(R.string.voice_assist_label);
164                 } else {
165                     label = getResources().getString(R.string.phone_label);
166                 }
167             }
168             info.addAction(new AccessibilityAction(ACTION_CLICK, label));
169         }
170
171         @Override
172         public boolean performAccessibilityAction(View host, int action, Bundle args) {
173             if (action == ACTION_CLICK) {
174                 if (host == mLockIcon) {
175                     mPhoneStatusBar.animateCollapsePanels(
176                             CommandQueue.FLAG_EXCLUDE_RECENTS_PANEL, true /* force */);
177                     return true;
178                 } else if (host == mCameraImageView) {
179                     launchCamera(CAMERA_LAUNCH_SOURCE_AFFORDANCE);
180                     return true;
181                 } else if (host == mLeftAffordanceView) {
182                     launchLeftAffordance();
183                     return true;
184                 }
185             }
186             return super.performAccessibilityAction(host, action, args);
187         }
188     };
189
190     @Override
191     protected void onFinishInflate() {
192         super.onFinishInflate();
193         mLockPatternUtils = new LockPatternUtils(mContext);
194         mPreviewContainer = (ViewGroup) findViewById(R.id.preview_container);
195         mCameraImageView = (KeyguardAffordanceView) findViewById(R.id.camera_button);
196         mLeftAffordanceView = (KeyguardAffordanceView) findViewById(R.id.left_button);
197         mLockIcon = (LockIcon) findViewById(R.id.lock_icon);
198         mIndicationText = (TextView) findViewById(R.id.keyguard_indication_text);
199         watchForCameraPolicyChanges();
200         updateCameraVisibility();
201         mUnlockMethodCache = UnlockMethodCache.getInstance(getContext());
202         mUnlockMethodCache.addListener(this);
203         mLockIcon.update();
204         setClipChildren(false);
205         setClipToPadding(false);
206         mPreviewInflater = new PreviewInflater(mContext, new LockPatternUtils(mContext));
207         inflateCameraPreview();
208         mLockIcon.setOnClickListener(this);
209         mLockIcon.setOnLongClickListener(this);
210         mCameraImageView.setOnClickListener(this);
211         mLeftAffordanceView.setOnClickListener(this);
212         initAccessibility();
213     }
214
215     private void initAccessibility() {
216         mLockIcon.setAccessibilityDelegate(mAccessibilityDelegate);
217         mLeftAffordanceView.setAccessibilityDelegate(mAccessibilityDelegate);
218         mCameraImageView.setAccessibilityDelegate(mAccessibilityDelegate);
219     }
220
221     @Override
222     protected void onConfigurationChanged(Configuration newConfig) {
223         super.onConfigurationChanged(newConfig);
224         int indicationBottomMargin = getResources().getDimensionPixelSize(
225                 R.dimen.keyguard_indication_margin_bottom);
226         MarginLayoutParams mlp = (MarginLayoutParams) mIndicationText.getLayoutParams();
227         if (mlp.bottomMargin != indicationBottomMargin) {
228             mlp.bottomMargin = indicationBottomMargin;
229             mIndicationText.setLayoutParams(mlp);
230         }
231
232         // Respect font size setting.
233         mIndicationText.setTextSize(TypedValue.COMPLEX_UNIT_PX,
234                 getResources().getDimensionPixelSize(
235                         com.android.internal.R.dimen.text_size_small_material));
236
237         ViewGroup.LayoutParams lp = mCameraImageView.getLayoutParams();
238         lp.width = getResources().getDimensionPixelSize(R.dimen.keyguard_affordance_width);
239         lp.height = getResources().getDimensionPixelSize(R.dimen.keyguard_affordance_height);
240         mCameraImageView.setLayoutParams(lp);
241         mCameraImageView.setImageDrawable(mContext.getDrawable(R.drawable.ic_camera_alt_24dp));
242
243         lp = mLockIcon.getLayoutParams();
244         lp.width = getResources().getDimensionPixelSize(R.dimen.keyguard_affordance_width);
245         lp.height = getResources().getDimensionPixelSize(R.dimen.keyguard_affordance_height);
246         mLockIcon.setLayoutParams(lp);
247         mLockIcon.update(true /* force */);
248
249         lp = mLeftAffordanceView.getLayoutParams();
250         lp.width = getResources().getDimensionPixelSize(R.dimen.keyguard_affordance_width);
251         lp.height = getResources().getDimensionPixelSize(R.dimen.keyguard_affordance_height);
252         mLeftAffordanceView.setLayoutParams(lp);
253         updateLeftAffordanceIcon();
254     }
255
256     public void setActivityStarter(ActivityStarter activityStarter) {
257         mActivityStarter = activityStarter;
258     }
259
260     public void setFlashlightController(FlashlightController flashlightController) {
261         mFlashlightController = flashlightController;
262     }
263
264     public void setAccessibilityController(AccessibilityController accessibilityController) {
265         mAccessibilityController = accessibilityController;
266         mLockIcon.setAccessibilityController(accessibilityController);
267         accessibilityController.addStateChangedCallback(this);
268     }
269
270     public void setPhoneStatusBar(PhoneStatusBar phoneStatusBar) {
271         mPhoneStatusBar = phoneStatusBar;
272         updateCameraVisibility(); // in case onFinishInflate() was called too early
273     }
274
275     public void setAffordanceHelper(KeyguardAffordanceHelper affordanceHelper) {
276         mAffordanceHelper = affordanceHelper;
277     }
278
279     public void setUserSetupComplete(boolean userSetupComplete) {
280         mUserSetupComplete = userSetupComplete;
281         updateCameraVisibility();
282         updateLeftAffordanceIcon();
283     }
284
285     private Intent getCameraIntent() {
286         KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
287         boolean canSkipBouncer = updateMonitor.getUserCanSkipBouncer(
288                 KeyguardUpdateMonitor.getCurrentUser());
289         boolean secure = mLockPatternUtils.isSecure(KeyguardUpdateMonitor.getCurrentUser());
290         return (secure && !canSkipBouncer) ? SECURE_CAMERA_INTENT : INSECURE_CAMERA_INTENT;
291     }
292
293     /**
294      * Resolves the intent to launch the camera application.
295      */
296     public ResolveInfo resolveCameraIntent() {
297         return mContext.getPackageManager().resolveActivityAsUser(getCameraIntent(),
298                 PackageManager.MATCH_DEFAULT_ONLY,
299                 KeyguardUpdateMonitor.getCurrentUser());
300     }
301
302     private void updateCameraVisibility() {
303         if (mCameraImageView == null) {
304             // Things are not set up yet; reply hazy, ask again later
305             return;
306         }
307         ResolveInfo resolved = resolveCameraIntent();
308         boolean isCameraDisabled =
309                 (mPhoneStatusBar != null) && !mPhoneStatusBar.isCameraAllowedByAdmin();
310         boolean visible = !isCameraDisabled
311                 && resolved != null
312                 && getResources().getBoolean(R.bool.config_keyguardShowCameraAffordance)
313                 && mUserSetupComplete;
314         mCameraImageView.setVisibility(visible ? View.VISIBLE : View.GONE);
315     }
316
317     private void updateLeftAffordanceIcon() {
318         mLeftIsVoiceAssist = canLaunchVoiceAssist();
319         int drawableId;
320         int contentDescription;
321         boolean visible = mUserSetupComplete;
322         if (mLeftIsVoiceAssist) {
323             drawableId = R.drawable.ic_mic_26dp;
324             contentDescription = R.string.accessibility_voice_assist_button;
325         } else {
326             visible &= isPhoneVisible();
327             drawableId = R.drawable.ic_phone_24dp;
328             contentDescription = R.string.accessibility_phone_button;
329         }
330         mLeftAffordanceView.setVisibility(visible ? View.VISIBLE : View.GONE);
331         mLeftAffordanceView.setImageDrawable(mContext.getDrawable(drawableId));
332         mLeftAffordanceView.setContentDescription(mContext.getString(contentDescription));
333     }
334
335     public boolean isLeftVoiceAssist() {
336         return mLeftIsVoiceAssist;
337     }
338
339     private boolean isPhoneVisible() {
340         PackageManager pm = mContext.getPackageManager();
341         return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
342                 && pm.resolveActivity(PHONE_INTENT, 0) != null;
343     }
344
345     private void watchForCameraPolicyChanges() {
346         final IntentFilter filter = new IntentFilter();
347         filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
348         getContext().registerReceiverAsUser(mDevicePolicyReceiver,
349                 UserHandle.ALL, filter, null, null);
350         KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mUpdateMonitorCallback);
351     }
352
353     @Override
354     public void onStateChanged(boolean accessibilityEnabled, boolean touchExplorationEnabled) {
355         mCameraImageView.setClickable(touchExplorationEnabled);
356         mLeftAffordanceView.setClickable(touchExplorationEnabled);
357         mCameraImageView.setFocusable(accessibilityEnabled);
358         mLeftAffordanceView.setFocusable(accessibilityEnabled);
359         mLockIcon.update();
360     }
361
362     @Override
363     public void onClick(View v) {
364         if (v == mCameraImageView) {
365             launchCamera(CAMERA_LAUNCH_SOURCE_AFFORDANCE);
366         } else if (v == mLeftAffordanceView) {
367             launchLeftAffordance();
368         } if (v == mLockIcon) {
369             if (!mAccessibilityController.isAccessibilityEnabled()) {
370                 handleTrustCircleClick();
371             } else {
372                 mPhoneStatusBar.animateCollapsePanels(
373                         CommandQueue.FLAG_EXCLUDE_NONE, true /* force */);
374             }
375         }
376     }
377
378     @Override
379     public boolean onLongClick(View v) {
380         handleTrustCircleClick();
381         return true;
382     }
383
384     private void handleTrustCircleClick() {
385         EventLogTags.writeSysuiLockscreenGesture(
386                 EventLogConstants.SYSUI_LOCKSCREEN_GESTURE_TAP_LOCK, 0 /* lengthDp - N/A */,
387                 0 /* velocityDp - N/A */);
388         mIndicationController.showTransientIndication(
389                 R.string.keyguard_indication_trust_disabled);
390         mLockPatternUtils.requireCredentialEntry(KeyguardUpdateMonitor.getCurrentUser());
391     }
392
393     public void bindCameraPrewarmService() {
394         Intent intent = getCameraIntent();
395         ActivityInfo targetInfo = PreviewInflater.getTargetActivityInfo(mContext, intent,
396                 KeyguardUpdateMonitor.getCurrentUser(), true /* onlyDirectBootAware */);
397         if (targetInfo != null && targetInfo.metaData != null) {
398             String clazz = targetInfo.metaData.getString(
399                     MediaStore.META_DATA_STILL_IMAGE_CAMERA_PREWARM_SERVICE);
400             if (clazz != null) {
401                 Intent serviceIntent = new Intent();
402                 serviceIntent.setClassName(targetInfo.packageName, clazz);
403                 serviceIntent.setAction(CameraPrewarmService.ACTION_PREWARM);
404                 try {
405                     if (getContext().bindServiceAsUser(serviceIntent, mPrewarmConnection,
406                             Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
407                             new UserHandle(UserHandle.USER_CURRENT))) {
408                         mPrewarmBound = true;
409                     }
410                 } catch (SecurityException e) {
411                     Log.w(TAG, "Unable to bind to prewarm service package=" + targetInfo.packageName
412                             + " class=" + clazz, e);
413                 }
414             }
415         }
416     }
417
418     public void unbindCameraPrewarmService(boolean launched) {
419         if (mPrewarmBound) {
420             if (mPrewarmMessenger != null && launched) {
421                 try {
422                     mPrewarmMessenger.send(Message.obtain(null /* handler */,
423                             CameraPrewarmService.MSG_CAMERA_FIRED));
424                 } catch (RemoteException e) {
425                     Log.w(TAG, "Error sending camera fired message", e);
426                 }
427             }
428             mContext.unbindService(mPrewarmConnection);
429             mPrewarmBound = false;
430         }
431     }
432
433     public void launchCamera(String source) {
434         final Intent intent = getCameraIntent();
435         intent.putExtra(EXTRA_CAMERA_LAUNCH_SOURCE, source);
436         boolean wouldLaunchResolverActivity = PreviewInflater.wouldLaunchResolverActivity(
437                 mContext, intent, KeyguardUpdateMonitor.getCurrentUser());
438         if (intent == SECURE_CAMERA_INTENT && !wouldLaunchResolverActivity) {
439             AsyncTask.execute(new Runnable() {
440                 @Override
441                 public void run() {
442                     int result = ActivityManager.START_CANCELED;
443
444                     // Normally an activity will set it's requested rotation
445                     // animation on its window. However when launching an activity
446                     // causes the orientation to change this is too late. In these cases
447                     // the default animation is used. This doesn't look good for
448                     // the camera (as it rotates the camera contents out of sync
449                     // with physical reality). So, we ask the WindowManager to
450                     // force the crossfade animation if an orientation change
451                     // happens to occur during the launch.
452                     ActivityOptions o = ActivityOptions.makeBasic();
453                     o.setRotationAnimationHint(
454                             WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS);
455                     try {
456                         result = ActivityManagerNative.getDefault().startActivityAsUser(
457                                 null, getContext().getBasePackageName(),
458                                 intent,
459                                 intent.resolveTypeIfNeeded(getContext().getContentResolver()),
460                                 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, o.toBundle(),
461                                 UserHandle.CURRENT.getIdentifier());
462                     } catch (RemoteException e) {
463                         Log.w(TAG, "Unable to start camera activity", e);
464                     }
465                     mActivityStarter.preventNextAnimation();
466                     final boolean launched = isSuccessfulLaunch(result);
467                     post(new Runnable() {
468                         @Override
469                         public void run() {
470                             unbindCameraPrewarmService(launched);
471                         }
472                     });
473                 }
474             });
475         } else {
476
477             // We need to delay starting the activity because ResolverActivity finishes itself if
478             // launched behind lockscreen.
479             mActivityStarter.startActivity(intent, false /* dismissShade */,
480                     new ActivityStarter.Callback() {
481                         @Override
482                         public void onActivityStarted(int resultCode) {
483                             unbindCameraPrewarmService(isSuccessfulLaunch(resultCode));
484                         }
485                     });
486         }
487     }
488
489     private static boolean isSuccessfulLaunch(int result) {
490         return result == ActivityManager.START_SUCCESS
491                 || result == ActivityManager.START_DELIVERED_TO_TOP
492                 || result == ActivityManager.START_TASK_TO_FRONT;
493     }
494
495     public void launchLeftAffordance() {
496         if (mLeftIsVoiceAssist) {
497             launchVoiceAssist();
498         } else {
499             launchPhone();
500         }
501     }
502
503     private void launchVoiceAssist() {
504         Runnable runnable = new Runnable() {
505             @Override
506             public void run() {
507                 mAssistManager.launchVoiceAssistFromKeyguard();
508                 mActivityStarter.preventNextAnimation();
509             }
510         };
511         if (mPhoneStatusBar.isKeyguardCurrentlySecure()) {
512             AsyncTask.execute(runnable);
513         } else {
514             mPhoneStatusBar.executeRunnableDismissingKeyguard(runnable, null /* cancelAction */,
515                     false /* dismissShade */, false /* afterKeyguardGone */, true /* deferred */);
516         }
517     }
518
519     private boolean canLaunchVoiceAssist() {
520         return mAssistManager.canVoiceAssistBeLaunchedFromKeyguard();
521     }
522
523     private void launchPhone() {
524         final TelecomManager tm = TelecomManager.from(mContext);
525         if (tm.isInCall()) {
526             AsyncTask.execute(new Runnable() {
527                 @Override
528                 public void run() {
529                     tm.showInCallScreen(false /* showDialpad */);
530                 }
531             });
532         } else {
533             mActivityStarter.startActivity(PHONE_INTENT, false /* dismissShade */);
534         }
535     }
536
537
538     @Override
539     protected void onVisibilityChanged(View changedView, int visibility) {
540         super.onVisibilityChanged(changedView, visibility);
541         if (changedView == this && visibility == VISIBLE) {
542             mLockIcon.update();
543             updateCameraVisibility();
544         }
545     }
546
547     public KeyguardAffordanceView getLeftView() {
548         return mLeftAffordanceView;
549     }
550
551     public KeyguardAffordanceView getRightView() {
552         return mCameraImageView;
553     }
554
555     public View getLeftPreview() {
556         return mLeftPreview;
557     }
558
559     public View getRightPreview() {
560         return mCameraPreview;
561     }
562
563     public LockIcon getLockIcon() {
564         return mLockIcon;
565     }
566
567     public View getIndicationView() {
568         return mIndicationText;
569     }
570
571     @Override
572     public boolean hasOverlappingRendering() {
573         return false;
574     }
575
576     @Override
577     public void onUnlockMethodStateChanged() {
578         mLockIcon.update();
579         updateCameraVisibility();
580     }
581
582     private void inflateCameraPreview() {
583         View previewBefore = mCameraPreview;
584         boolean visibleBefore = false;
585         if (previewBefore != null) {
586             mPreviewContainer.removeView(previewBefore);
587             visibleBefore = previewBefore.getVisibility() == View.VISIBLE;
588         }
589         mCameraPreview = mPreviewInflater.inflatePreview(getCameraIntent());
590         if (mCameraPreview != null) {
591             mPreviewContainer.addView(mCameraPreview);
592             mCameraPreview.setVisibility(visibleBefore ? View.VISIBLE : View.INVISIBLE);
593         }
594         if (mAffordanceHelper != null) {
595             mAffordanceHelper.updatePreviews();
596         }
597     }
598
599     private void updateLeftPreview() {
600         View previewBefore = mLeftPreview;
601         if (previewBefore != null) {
602             mPreviewContainer.removeView(previewBefore);
603         }
604         if (mLeftIsVoiceAssist) {
605             mLeftPreview = mPreviewInflater.inflatePreviewFromService(
606                     mAssistManager.getVoiceInteractorComponentName());
607         } else {
608             mLeftPreview = mPreviewInflater.inflatePreview(PHONE_INTENT);
609         }
610         if (mLeftPreview != null) {
611             mPreviewContainer.addView(mLeftPreview);
612             mLeftPreview.setVisibility(View.INVISIBLE);
613         }
614         if (mAffordanceHelper != null) {
615             mAffordanceHelper.updatePreviews();
616         }
617     }
618
619     public void startFinishDozeAnimation() {
620         long delay = 0;
621         if (mLeftAffordanceView.getVisibility() == View.VISIBLE) {
622             startFinishDozeAnimationElement(mLeftAffordanceView, delay);
623             delay += DOZE_ANIMATION_STAGGER_DELAY;
624         }
625         startFinishDozeAnimationElement(mLockIcon, delay);
626         delay += DOZE_ANIMATION_STAGGER_DELAY;
627         if (mCameraImageView.getVisibility() == View.VISIBLE) {
628             startFinishDozeAnimationElement(mCameraImageView, delay);
629         }
630         mIndicationText.setAlpha(0f);
631         mIndicationText.animate()
632                 .alpha(1f)
633                 .setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN)
634                 .setDuration(NotificationPanelView.DOZE_ANIMATION_DURATION);
635     }
636
637     private void startFinishDozeAnimationElement(View element, long delay) {
638         element.setAlpha(0f);
639         element.setTranslationY(element.getHeight() / 2);
640         element.animate()
641                 .alpha(1f)
642                 .translationY(0f)
643                 .setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN)
644                 .setStartDelay(delay)
645                 .setDuration(DOZE_ANIMATION_ELEMENT_DURATION);
646     }
647
648     private final BroadcastReceiver mDevicePolicyReceiver = new BroadcastReceiver() {
649         @Override
650         public void onReceive(Context context, Intent intent) {
651             post(new Runnable() {
652                 @Override
653                 public void run() {
654                     updateCameraVisibility();
655                 }
656             });
657         }
658     };
659
660     private final KeyguardUpdateMonitorCallback mUpdateMonitorCallback =
661             new KeyguardUpdateMonitorCallback() {
662         @Override
663         public void onUserSwitchComplete(int userId) {
664             updateCameraVisibility();
665         }
666
667         @Override
668         public void onStartedWakingUp() {
669             mLockIcon.setDeviceInteractive(true);
670         }
671
672         @Override
673         public void onFinishedGoingToSleep(int why) {
674             mLockIcon.setDeviceInteractive(false);
675         }
676
677         @Override
678         public void onScreenTurnedOn() {
679             mLockIcon.setScreenOn(true);
680         }
681
682         @Override
683         public void onScreenTurnedOff() {
684             mLockIcon.setScreenOn(false);
685         }
686
687         @Override
688         public void onKeyguardVisibilityChanged(boolean showing) {
689             mLockIcon.update();
690         }
691
692         @Override
693         public void onFingerprintRunningStateChanged(boolean running) {
694             mLockIcon.update();
695         }
696
697         @Override
698         public void onStrongAuthStateChanged(int userId) {
699             mLockIcon.update();
700         }
701
702         @Override
703         public void onUserUnlocked() {
704             inflateCameraPreview();
705             updateCameraVisibility();
706             updateLeftAffordance();
707         }
708     };
709
710     public void setKeyguardIndicationController(
711             KeyguardIndicationController keyguardIndicationController) {
712         mIndicationController = keyguardIndicationController;
713     }
714
715     public void setAssistManager(AssistManager assistManager) {
716         mAssistManager = assistManager;
717         updateLeftAffordance();
718     }
719
720     public void updateLeftAffordance() {
721         updateLeftAffordanceIcon();
722         updateLeftPreview();
723     }
724
725     public void onKeyguardShowingChanged() {
726         updateLeftAffordance();
727         inflateCameraPreview();
728     }
729 }