• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.keyguard;
18 
19 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
20 import static android.service.dreams.Flags.dismissDreamOnKeyguardDismiss;
21 import static android.view.RemoteAnimationTarget.MODE_OPENING;
22 import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_APPEARING;
23 import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY;
24 import static android.view.WindowManager.TRANSIT_KEYGUARD_GOING_AWAY;
25 import static android.view.WindowManager.TRANSIT_KEYGUARD_OCCLUDE;
26 import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
27 import static android.view.WindowManager.TRANSIT_OLD_KEYGUARD_GOING_AWAY;
28 import static android.view.WindowManager.TRANSIT_OLD_KEYGUARD_GOING_AWAY_ON_WALLPAPER;
29 import static android.view.WindowManager.TRANSIT_OLD_KEYGUARD_OCCLUDE;
30 import static android.view.WindowManager.TRANSIT_OLD_KEYGUARD_OCCLUDE_BY_DREAM;
31 import static android.view.WindowManager.TRANSIT_OLD_KEYGUARD_UNOCCLUDE;
32 import static android.view.WindowManager.TRANSIT_OLD_NONE;
33 import static android.view.WindowManager.TRANSIT_TO_BACK;
34 import static android.view.WindowManager.TransitionFlags;
35 import static android.view.WindowManager.TransitionOldType;
36 import static android.view.WindowManager.TransitionType;
37 
38 import android.annotation.NonNull;
39 import android.app.ActivityManager;
40 import android.app.ActivityTaskManager;
41 import android.app.Service;
42 import android.app.WindowConfiguration;
43 import android.content.Intent;
44 import android.os.Binder;
45 import android.os.Bundle;
46 import android.os.Debug;
47 import android.os.IBinder;
48 import android.os.PowerManager;
49 import android.os.Process;
50 import android.os.RemoteException;
51 import android.os.Trace;
52 import android.util.ArrayMap;
53 import android.util.Log;
54 import android.util.RotationUtils;
55 import android.util.Slog;
56 import android.view.IRemoteAnimationFinishedCallback;
57 import android.view.IRemoteAnimationRunner;
58 import android.view.RemoteAnimationAdapter;
59 import android.view.RemoteAnimationDefinition;
60 import android.view.RemoteAnimationTarget;
61 import android.view.SurfaceControl;
62 import android.view.WindowManagerPolicyConstants;
63 import android.window.IRemoteTransition;
64 import android.window.IRemoteTransitionFinishedCallback;
65 import android.window.RemoteTransitionStub;
66 import android.window.TransitionInfo;
67 
68 import com.android.internal.annotations.GuardedBy;
69 import com.android.internal.foldables.FoldGracePeriodProvider;
70 import com.android.internal.policy.IKeyguardDismissCallback;
71 import com.android.internal.policy.IKeyguardDrawnCallback;
72 import com.android.internal.policy.IKeyguardExitCallback;
73 import com.android.internal.policy.IKeyguardService;
74 import com.android.internal.policy.IKeyguardStateCallback;
75 import com.android.keyguard.KeyguardUpdateMonitor;
76 import com.android.keyguard.mediator.ScreenOnCoordinator;
77 import com.android.systemui.SystemUIApplication;
78 import com.android.systemui.dagger.qualifiers.Application;
79 import com.android.systemui.dagger.qualifiers.Main;
80 import com.android.systemui.deviceentry.domain.interactor.DeviceEntryInteractor;
81 import com.android.systemui.flags.FeatureFlags;
82 import com.android.systemui.keyguard.domain.interactor.KeyguardDismissInteractor;
83 import com.android.systemui.keyguard.domain.interactor.KeyguardEnabledInteractor;
84 import com.android.systemui.keyguard.domain.interactor.KeyguardInteractor;
85 import com.android.systemui.keyguard.domain.interactor.KeyguardServiceShowLockscreenInteractor;
86 import com.android.systemui.keyguard.domain.interactor.KeyguardStateCallbackInteractor;
87 import com.android.systemui.keyguard.domain.interactor.KeyguardWakeDirectlyToGoneInteractor;
88 import com.android.systemui.keyguard.ui.binder.KeyguardSurfaceBehindParamsApplier;
89 import com.android.systemui.keyguard.ui.binder.KeyguardSurfaceBehindViewBinder;
90 import com.android.systemui.keyguard.ui.binder.WindowManagerLockscreenVisibilityViewBinder;
91 import com.android.systemui.keyguard.ui.viewmodel.KeyguardSurfaceBehindViewModel;
92 import com.android.systemui.keyguard.ui.viewmodel.WindowManagerLockscreenVisibilityViewModel;
93 import com.android.systemui.power.domain.interactor.PowerInteractor;
94 import com.android.systemui.power.shared.model.ScreenPowerState;
95 import com.android.systemui.scene.domain.interactor.SceneInteractor;
96 import com.android.systemui.scene.domain.startable.KeyguardStateCallbackStartable;
97 import com.android.systemui.scene.shared.flag.SceneContainerFlag;
98 import com.android.systemui.scene.shared.model.Scenes;
99 import com.android.systemui.settings.DisplayTracker;
100 import com.android.wm.shell.shared.CounterRotator;
101 import com.android.wm.shell.shared.ShellTransitions;
102 import com.android.wm.shell.shared.TransitionUtil;
103 import com.android.wm.shell.transition.Transitions;
104 
105 import dagger.Lazy;
106 
107 import kotlinx.coroutines.CoroutineScope;
108 
109 import java.util.ArrayList;
110 import java.util.Map;
111 import java.util.WeakHashMap;
112 import java.util.concurrent.Executor;
113 
114 import javax.inject.Inject;
115 
116 public class KeyguardService extends Service {
117     static final String TAG = "KeyguardService";
118     static final String PERMISSION = android.Manifest.permission.CONTROL_KEYGUARD;
119 
120     private final FeatureFlags mFlags;
121     private final KeyguardViewMediator mKeyguardViewMediator;
122     private final KeyguardLifecyclesDispatcher mKeyguardLifecyclesDispatcher;
123     private final ScreenOnCoordinator mScreenOnCoordinator;
124     private final ShellTransitions mShellTransitions;
125     private final DisplayTracker mDisplayTracker;
126     private final PowerInteractor mPowerInteractor;
127     private final KeyguardInteractor mKeyguardInteractor;
128     private final Lazy<SceneInteractor> mSceneInteractorLazy;
129     private final Lazy<DeviceEntryInteractor> mDeviceEntryInteractorLazy;
130     private final Executor mMainExecutor;
131     private final Lazy<KeyguardStateCallbackStartable> mKeyguardStateCallbackStartableLazy;
132     private final KeyguardStateCallbackInteractor mKeyguardStateCallbackInteractor;
133 
wrap(TransitionInfo info, boolean wallpapers, SurfaceControl.Transaction t, ArrayMap<SurfaceControl, SurfaceControl> leashMap, CounterRotator counterWallpaper)134     private static RemoteAnimationTarget[] wrap(TransitionInfo info, boolean wallpapers,
135             SurfaceControl.Transaction t, ArrayMap<SurfaceControl, SurfaceControl> leashMap,
136             CounterRotator counterWallpaper) {
137         final ArrayList<RemoteAnimationTarget> out = new ArrayList<>();
138         for (int i = 0; i < info.getChanges().size(); i++) {
139             boolean changeIsWallpaper =
140                     (info.getChanges().get(i).getFlags() & TransitionInfo.FLAG_IS_WALLPAPER) != 0;
141             if (wallpapers != changeIsWallpaper) continue;
142 
143             final TransitionInfo.Change change = info.getChanges().get(i);
144             final ActivityManager.RunningTaskInfo taskInfo = change.getTaskInfo();
145             final int taskId = taskInfo != null ? change.getTaskInfo().taskId : -1;
146 
147             if (taskId != -1 && change.getParent() != null) {
148                 final TransitionInfo.Change parentChange = info.getChange(change.getParent());
149                 if (parentChange != null && parentChange.getTaskInfo() != null) {
150                     // Only adding the root task as the animation target.
151                     continue;
152                 }
153             }
154 
155             // Avoid wrapping non-task and non-wallpaper changes as they don't need to animate
156             // for keyguard unlock animation.
157             if (taskId < 0 && !wallpapers) continue;
158 
159             final RemoteAnimationTarget target = TransitionUtil.newTarget(change,
160                     // wallpapers go into the "below" layer space
161                     info.getChanges().size() - i,
162                     // keyguard treats wallpaper as translucent
163                     (change.getFlags() & TransitionInfo.FLAG_SHOW_WALLPAPER) != 0,
164                     info, t, leashMap);
165 
166             if (changeIsWallpaper) {
167                 int rotateDelta = RotationUtils.deltaRotation(change.getStartRotation(),
168                         change.getEndRotation());
169                 if (rotateDelta != 0 && change.getParent() != null
170                         && change.getMode() == TRANSIT_TO_BACK) {
171                     final TransitionInfo.Change parent = info.getChange(change.getParent());
172                     if (parent != null) {
173                         float displayW = parent.getEndAbsBounds().width();
174                         float displayH = parent.getEndAbsBounds().height();
175                         counterWallpaper.setup(t, parent.getLeash(), rotateDelta, displayW,
176                                 displayH);
177                     }
178                     if (counterWallpaper.getSurface() != null) {
179                         t.setLayer(counterWallpaper.getSurface(), -1);
180                         counterWallpaper.addChild(t, leashMap.get(change.getLeash()));
181                     }
182                 }
183             }
184 
185             out.add(target);
186         }
187         return out.toArray(new RemoteAnimationTarget[out.size()]);
188     }
189 
getTransitionOldType(@ransitionType int type, @TransitionFlags int flags, RemoteAnimationTarget[] apps)190     private static @TransitionOldType int getTransitionOldType(@TransitionType int type,
191             @TransitionFlags int flags, RemoteAnimationTarget[] apps) {
192         if (type == TRANSIT_KEYGUARD_GOING_AWAY
193                 || (flags & TRANSIT_FLAG_KEYGUARD_GOING_AWAY) != 0) {
194             return apps.length == 0 ? TRANSIT_OLD_KEYGUARD_GOING_AWAY_ON_WALLPAPER
195                     : TRANSIT_OLD_KEYGUARD_GOING_AWAY;
196         } else if (type == TRANSIT_KEYGUARD_OCCLUDE) {
197             boolean isOccludeByDream = apps.length > 0 && apps[0].taskInfo != null
198                     && apps[0].taskInfo.topActivityType == WindowConfiguration.ACTIVITY_TYPE_DREAM;
199             if (isOccludeByDream) return TRANSIT_OLD_KEYGUARD_OCCLUDE_BY_DREAM;
200             return TRANSIT_OLD_KEYGUARD_OCCLUDE;
201         } else if (type == TRANSIT_KEYGUARD_UNOCCLUDE) {
202             return TRANSIT_OLD_KEYGUARD_UNOCCLUDE;
203         } else {
204             Slog.d(TAG, "Unexpected transit type: " + type);
205             return TRANSIT_OLD_NONE;
206         }
207     }
208 
209     // Wrap Keyguard going away animation.
210     // Note: Also used for wrapping occlude by Dream animation. It works (with some redundancy).
wrap(final KeyguardViewMediator keyguardViewMediator, final IRemoteAnimationRunner runner)211     public static IRemoteTransition wrap(final KeyguardViewMediator keyguardViewMediator,
212             final IRemoteAnimationRunner runner) {
213         return new RemoteTransitionStub() {
214 
215             @GuardedBy("mLeashMap")
216             private final ArrayMap<SurfaceControl, SurfaceControl> mLeashMap = new ArrayMap<>();
217             private final CounterRotator mCounterRotator = new CounterRotator();
218 
219             @GuardedBy("mLeashMap")
220             private final Map<IBinder, IRemoteTransitionFinishedCallback> mFinishCallbacks =
221                     new WeakHashMap<>();
222 
223             @Override
224             public void startAnimation(IBinder transition, TransitionInfo info,
225                     SurfaceControl.Transaction t, IRemoteTransitionFinishedCallback finishCallback)
226                     throws RemoteException {
227                 Slog.d(TAG, "Starts IRemoteAnimationRunner: info=" + info);
228 
229                 final RemoteAnimationTarget[] apps;
230                 final RemoteAnimationTarget[] wallpapers;
231                 final RemoteAnimationTarget[] nonApps = new RemoteAnimationTarget[0];
232                 synchronized (mLeashMap) {
233                     apps = wrap(info, false /* wallpapers */, t, mLeashMap, mCounterRotator);
234                     wallpapers = wrap(info, true /* wallpapers */, t, mLeashMap, mCounterRotator);
235                     mFinishCallbacks.put(transition, finishCallback);
236                 }
237 
238                 // Set alpha back to 1 for the independent changes because we will be animating
239                 // children instead.
240                 for (TransitionInfo.Change chg : info.getChanges()) {
241                     if (TransitionInfo.isIndependent(chg, info)) {
242                         t.setAlpha(chg.getLeash(), 1.f);
243                     }
244                 }
245                 initAlphaForAnimationTargets(t, apps);
246                 initAlphaForAnimationTargets(t, wallpapers);
247 
248                 // If the keyguard is going away, hide the dream if one exists.
249                 if (dismissDreamOnKeyguardDismiss()
250                         && (info.getFlags() & TRANSIT_FLAG_KEYGUARD_GOING_AWAY) != 0) {
251                     for (RemoteAnimationTarget app : apps) {
252                         final boolean isDream = app.taskInfo != null
253                                 && app.taskInfo.getActivityType()
254                                 == WindowConfiguration.ACTIVITY_TYPE_DREAM;
255                         if (isDream && app.mode == RemoteAnimationTarget.MODE_CLOSING) {
256                             t.hide(app.leash);
257                             break;
258                         }
259                     }
260                 }
261 
262                 t.apply();
263 
264                 runner.onAnimationStart(
265                         getTransitionOldType(info.getType(), info.getFlags(), apps),
266                         apps, wallpapers, nonApps,
267                         new IRemoteAnimationFinishedCallback.Stub() {
268                             @Override
269                             public void onAnimationFinished() throws RemoteException {
270                                 Slog.d(TAG, "Finish IRemoteAnimationRunner.");
271                                 finish(transition);
272                             }
273                         });
274             }
275 
276             public void mergeAnimation(IBinder candidateTransition, TransitionInfo candidateInfo,
277                     SurfaceControl.Transaction candidateT, IBinder currentTransition,
278                     IRemoteTransitionFinishedCallback candidateFinishCallback) {
279                 if ((candidateInfo.getFlags() & TRANSIT_FLAG_KEYGUARD_APPEARING) != 0) {
280                     keyguardViewMediator.setPendingLock(true);
281                     keyguardViewMediator.cancelKeyguardExitAnimation();
282                     return;
283                 }
284 
285                 try {
286                     runner.onAnimationCancelled();
287                     finish(currentTransition);
288                 } catch (RemoteException e) {
289                     // Ignore.
290                 }
291             }
292 
293             private static void initAlphaForAnimationTargets(@NonNull SurfaceControl.Transaction t,
294                     @NonNull RemoteAnimationTarget[] targets) {
295                 for (RemoteAnimationTarget target : targets) {
296                     if (target.mode != MODE_OPENING) continue;
297                     t.setAlpha(target.leash, 0.f);
298                 }
299             }
300 
301             private void finish(IBinder transition) throws RemoteException {
302                 final IRemoteTransitionFinishedCallback finishCallback;
303                 SurfaceControl.Transaction finishTransaction = null;
304 
305                 synchronized (mLeashMap) {
306                     if (mCounterRotator.getSurface() != null
307                             && mCounterRotator.getSurface().isValid()) {
308                         finishTransaction = new SurfaceControl.Transaction();
309                         mCounterRotator.cleanUp(finishTransaction);
310                     }
311                     mLeashMap.clear();
312                     finishCallback = mFinishCallbacks.remove(transition);
313                 }
314 
315                 if (finishCallback != null) {
316                     finishCallback.onTransitionFinished(null /* wct */, finishTransaction);
317                 } else if (finishTransaction != null) {
318                     finishTransaction.apply();
319                 }
320             }
321         };
322     }
323 
324     private final WindowManagerOcclusionManager mWmOcclusionManager;
325     private final KeyguardEnabledInteractor mKeyguardEnabledInteractor;
326     private final KeyguardWakeDirectlyToGoneInteractor mKeyguardWakeDirectlyToGoneInteractor;
327     private final KeyguardDismissInteractor mKeyguardDismissInteractor;
328     private final Lazy<FoldGracePeriodProvider> mFoldGracePeriodProvider = new Lazy<>() {
329         @Override
330         public FoldGracePeriodProvider get() {
331             return new FoldGracePeriodProvider();
332         }
333     };
334     private final KeyguardServiceShowLockscreenInteractor mKeyguardServiceShowLockscreenInteractor;
335     private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
336 
337     @Inject
KeyguardService( KeyguardViewMediator keyguardViewMediator, KeyguardLifecyclesDispatcher keyguardLifecyclesDispatcher, ScreenOnCoordinator screenOnCoordinator, ShellTransitions shellTransitions, DisplayTracker displayTracker, WindowManagerLockscreenVisibilityViewModel wmLockscreenVisibilityViewModel, WindowManagerLockscreenVisibilityManager wmLockscreenVisibilityManager, KeyguardSurfaceBehindViewModel keyguardSurfaceBehindViewModel, KeyguardSurfaceBehindParamsApplier keyguardSurfaceBehindAnimator, @Application CoroutineScope scope, FeatureFlags featureFlags, PowerInteractor powerInteractor, WindowManagerOcclusionManager windowManagerOcclusionManager, Lazy<SceneInteractor> sceneInteractorLazy, @Main Executor mainExecutor, KeyguardInteractor keyguardInteractor, KeyguardEnabledInteractor keyguardEnabledInteractor, Lazy<KeyguardStateCallbackStartable> keyguardStateCallbackStartableLazy, KeyguardWakeDirectlyToGoneInteractor keyguardWakeDirectlyToGoneInteractor, KeyguardDismissInteractor keyguardDismissInteractor, Lazy<DeviceEntryInteractor> deviceEntryInteractorLazy, KeyguardStateCallbackInteractor keyguardStateCallbackInteractor, KeyguardServiceShowLockscreenInteractor keyguardServiceShowLockscreenInteractor, KeyguardUpdateMonitor keyguardUpdateMonitor)338     public KeyguardService(
339             KeyguardViewMediator keyguardViewMediator,
340             KeyguardLifecyclesDispatcher keyguardLifecyclesDispatcher,
341             ScreenOnCoordinator screenOnCoordinator,
342             ShellTransitions shellTransitions,
343             DisplayTracker displayTracker,
344             WindowManagerLockscreenVisibilityViewModel wmLockscreenVisibilityViewModel,
345             WindowManagerLockscreenVisibilityManager wmLockscreenVisibilityManager,
346             KeyguardSurfaceBehindViewModel keyguardSurfaceBehindViewModel,
347             KeyguardSurfaceBehindParamsApplier keyguardSurfaceBehindAnimator,
348             @Application CoroutineScope scope,
349             FeatureFlags featureFlags,
350             PowerInteractor powerInteractor,
351             WindowManagerOcclusionManager windowManagerOcclusionManager,
352             Lazy<SceneInteractor> sceneInteractorLazy,
353             @Main Executor mainExecutor,
354             KeyguardInteractor keyguardInteractor,
355             KeyguardEnabledInteractor keyguardEnabledInteractor,
356             Lazy<KeyguardStateCallbackStartable> keyguardStateCallbackStartableLazy,
357             KeyguardWakeDirectlyToGoneInteractor keyguardWakeDirectlyToGoneInteractor,
358             KeyguardDismissInteractor keyguardDismissInteractor,
359             Lazy<DeviceEntryInteractor> deviceEntryInteractorLazy,
360             KeyguardStateCallbackInteractor keyguardStateCallbackInteractor,
361             KeyguardServiceShowLockscreenInteractor keyguardServiceShowLockscreenInteractor,
362             KeyguardUpdateMonitor keyguardUpdateMonitor) {
363         super();
364         mKeyguardViewMediator = keyguardViewMediator;
365         mKeyguardLifecyclesDispatcher = keyguardLifecyclesDispatcher;
366         mScreenOnCoordinator = screenOnCoordinator;
367         mShellTransitions = shellTransitions;
368         mDisplayTracker = displayTracker;
369         mFlags = featureFlags;
370         mPowerInteractor = powerInteractor;
371         mKeyguardInteractor = keyguardInteractor;
372         mSceneInteractorLazy = sceneInteractorLazy;
373         mMainExecutor = mainExecutor;
374         mKeyguardStateCallbackStartableLazy = keyguardStateCallbackStartableLazy;
375         mKeyguardStateCallbackInteractor = keyguardStateCallbackInteractor;
376         mDeviceEntryInteractorLazy = deviceEntryInteractorLazy;
377 
378         if (KeyguardWmStateRefactor.isEnabled()) {
379             WindowManagerLockscreenVisibilityViewBinder.bind(
380                     wmLockscreenVisibilityViewModel,
381                     wmLockscreenVisibilityManager,
382                     scope);
383 
384             KeyguardSurfaceBehindViewBinder.bind(
385                     keyguardSurfaceBehindViewModel,
386                     keyguardSurfaceBehindAnimator,
387                     scope);
388         }
389 
390         mWmOcclusionManager = windowManagerOcclusionManager;
391         mKeyguardEnabledInteractor = keyguardEnabledInteractor;
392         mKeyguardWakeDirectlyToGoneInteractor = keyguardWakeDirectlyToGoneInteractor;
393         mKeyguardDismissInteractor = keyguardDismissInteractor;
394         mKeyguardServiceShowLockscreenInteractor = keyguardServiceShowLockscreenInteractor;
395         mKeyguardUpdateMonitor = keyguardUpdateMonitor;
396     }
397 
398     @Override
onCreate()399     public void onCreate() {
400         ((SystemUIApplication) getApplication()).startSystemUserServicesIfNeeded();
401 
402         if (mShellTransitions == null || !Transitions.ENABLE_SHELL_TRANSITIONS) {
403             RemoteAnimationDefinition definition = new RemoteAnimationDefinition();
404             final RemoteAnimationAdapter exitAnimationAdapter =
405                     new RemoteAnimationAdapter(
406                             mKeyguardViewMediator.getExitAnimationRunner(), 0, 0);
407             definition.addRemoteAnimation(TRANSIT_OLD_KEYGUARD_GOING_AWAY,
408                     exitAnimationAdapter);
409             definition.addRemoteAnimation(TRANSIT_OLD_KEYGUARD_GOING_AWAY_ON_WALLPAPER,
410                     exitAnimationAdapter);
411             final RemoteAnimationAdapter occludeAnimationAdapter =
412                     new RemoteAnimationAdapter(
413                             mKeyguardViewMediator.getOccludeAnimationRunner(), 0, 0);
414             definition.addRemoteAnimation(TRANSIT_OLD_KEYGUARD_OCCLUDE,
415                     occludeAnimationAdapter);
416 
417             final RemoteAnimationAdapter occludeByDreamAnimationAdapter =
418                     new RemoteAnimationAdapter(
419                             mKeyguardViewMediator.getOccludeByDreamAnimationRunner(), 0, 0);
420             definition.addRemoteAnimation(TRANSIT_OLD_KEYGUARD_OCCLUDE_BY_DREAM,
421                     occludeByDreamAnimationAdapter);
422 
423             final RemoteAnimationAdapter unoccludeAnimationAdapter =
424                     new RemoteAnimationAdapter(
425                             mKeyguardViewMediator.getUnoccludeAnimationRunner(), 0, 0);
426             definition.addRemoteAnimation(TRANSIT_OLD_KEYGUARD_UNOCCLUDE,
427                     unoccludeAnimationAdapter);
428             ActivityTaskManager.getInstance().registerRemoteAnimationsForDisplay(
429                     mDisplayTracker.getDefaultDisplayId(), definition);
430         }
431     }
432 
433     @Override
onBind(Intent intent)434     public IBinder onBind(Intent intent) {
435         return mBinder;
436     }
437 
checkPermission()438     void checkPermission() {
439         // Avoid deadlock by avoiding calling back into the system process.
440         if (Binder.getCallingUid() == Process.SYSTEM_UID) return;
441 
442         // Otherwise,explicitly check for caller permission ...
443         if (getBaseContext().checkCallingOrSelfPermission(PERMISSION) != PERMISSION_GRANTED) {
444             Log.w(TAG, "Caller needs permission '" + PERMISSION + "' to call " + Debug.getCaller());
445             throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
446                     + ", must have permission " + PERMISSION);
447         }
448     }
449 
450     private final IKeyguardService.Stub mBinder = new IKeyguardService.Stub() {
451         private static final String TRACK_NAME = "IKeyguardService";
452 
453         /**
454          * Helper for tracing the most-recent call on the IKeyguardService interface.
455          * IKeyguardService is oneway, so we are most interested in the order of the calls as they
456          * are received. We use an async track to make it easier to visualize in the trace.
457          * @param name name of the trace section
458          */
459         private static void trace(String name) {
460             Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_APP, TRACK_NAME, 0);
461             Trace.asyncTraceForTrackBegin(Trace.TRACE_TAG_APP, TRACK_NAME, name, 0);
462         }
463 
464         @Override // Binder interface
465         public void addStateMonitorCallback(IKeyguardStateCallback callback) {
466             trace("addStateMonitorCallback");
467             checkPermission();
468             if (SceneContainerFlag.isEnabled()) {
469                 mKeyguardStateCallbackStartableLazy.get().addCallback(callback);
470             } else if (KeyguardWmStateRefactor.isEnabled()) {
471                 mKeyguardStateCallbackInteractor.addCallback(callback);
472             } else {
473                 mKeyguardViewMediator.addStateMonitorCallback(callback);
474             }
475         }
476 
477         @Override // Binder interface
478         public void verifyUnlock(IKeyguardExitCallback callback) {
479             trace("verifyUnlock");
480             Trace.beginSection("KeyguardService.mBinder#verifyUnlock");
481             checkPermission();
482             mKeyguardViewMediator.verifyUnlock(callback);
483             Trace.endSection();
484         }
485 
486         @Override // Binder interface
487         public void setOccluded(boolean isOccluded, boolean animate) {
488             trace("setOccluded isOccluded=" + isOccluded + " animate=" + animate);
489             Log.d(TAG, "setOccluded(" + isOccluded + ")");
490 
491             Trace.beginSection("KeyguardService.mBinder#setOccluded");
492             checkPermission();
493             if (!KeyguardWmStateRefactor.isEnabled()) {
494                 mKeyguardViewMediator.setOccluded(isOccluded, animate);
495             } else {
496                 mWmOcclusionManager.onKeyguardServiceSetOccluded(isOccluded);
497             }
498             Trace.endSection();
499         }
500 
501         @Override // Binder interface
502         public void dismiss(IKeyguardDismissCallback callback, CharSequence message) {
503             trace("dismiss message=" + message);
504             checkPermission();
505             if (SceneContainerFlag.isEnabled()) {
506                 mDeviceEntryInteractorLazy.get().attemptDeviceEntry(callback);
507             } else if (KeyguardWmStateRefactor.isEnabled()) {
508                 mKeyguardDismissInteractor.dismissKeyguardWithCallback(callback);
509             } else {
510                 mKeyguardViewMediator.dismiss(callback, message);
511             }
512         }
513 
514         @Override // Binder interface
515         public void onDreamingStarted() {
516             trace("onDreamingStarted");
517             checkPermission();
518             mKeyguardWakeDirectlyToGoneInteractor.onDreamingStarted();
519             mKeyguardInteractor.setDreaming(true);
520             mKeyguardViewMediator.onDreamingStarted();
521         }
522 
523         @Override // Binder interface
524         public void onDreamingStopped() {
525             trace("onDreamingStopped");
526             checkPermission();
527             mKeyguardWakeDirectlyToGoneInteractor.onDreamingStopped();
528             mKeyguardInteractor.setDreaming(false);
529             mKeyguardViewMediator.onDreamingStopped();
530         }
531 
532         @Override // Binder interface
533         public void onStartedGoingToSleep(@PowerManager.GoToSleepReason int pmSleepReason) {
534             trace("onStartedGoingToSleep pmSleepReason=" + pmSleepReason);
535             checkPermission();
536             mKeyguardViewMediator.onStartedGoingToSleep(
537                     WindowManagerPolicyConstants.translateSleepReasonToOffReason(pmSleepReason));
538             mPowerInteractor.onStartedGoingToSleep(pmSleepReason);
539             mKeyguardLifecyclesDispatcher.dispatch(
540                     KeyguardLifecyclesDispatcher.STARTED_GOING_TO_SLEEP, pmSleepReason);
541         }
542 
543         @Override // Binder interface
544         public void onFinishedGoingToSleep(
545                 @PowerManager.GoToSleepReason int pmSleepReason, boolean
546                 powerButtonLaunchGestureTriggered) {
547             trace("onFinishedGoingToSleep pmSleepReason=" + pmSleepReason
548                     + " powerButtonLaunchTriggered=" + powerButtonLaunchGestureTriggered);
549             checkPermission();
550             mKeyguardViewMediator.onFinishedGoingToSleep(
551                     WindowManagerPolicyConstants.translateSleepReasonToOffReason(pmSleepReason),
552                     powerButtonLaunchGestureTriggered);
553             mPowerInteractor.onFinishedGoingToSleep(powerButtonLaunchGestureTriggered);
554             mKeyguardLifecyclesDispatcher.dispatch(
555                     KeyguardLifecyclesDispatcher.FINISHED_GOING_TO_SLEEP);
556         }
557 
558         @Override // Binder interface
559         public void onStartedWakingUp(
560                 @PowerManager.WakeReason int pmWakeReason,
561                 boolean powerButtonLaunchGestureTriggered) {
562             trace("onStartedWakingUp pmWakeReason=" + pmWakeReason
563                     + " powerButtonLaunchGestureTriggered=" + powerButtonLaunchGestureTriggered);
564             Trace.beginSection("KeyguardService.mBinder#onStartedWakingUp");
565             checkPermission();
566             mKeyguardViewMediator.onStartedWakingUp(pmWakeReason,
567                     powerButtonLaunchGestureTriggered);
568             mPowerInteractor.onStartedWakingUp(pmWakeReason, powerButtonLaunchGestureTriggered);
569             mKeyguardLifecyclesDispatcher.dispatch(
570                     KeyguardLifecyclesDispatcher.STARTED_WAKING_UP, pmWakeReason);
571             Trace.endSection();
572         }
573 
574         @Override // Binder interface
575         public void onFinishedWakingUp() {
576             trace("onFinishedWakingUp");
577             Trace.beginSection("KeyguardService.mBinder#onFinishedWakingUp");
578             checkPermission();
579             mPowerInteractor.onFinishedWakingUp();
580             mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.FINISHED_WAKING_UP);
581             Trace.endSection();
582         }
583 
584         @Override // Binder interface
585         public void onScreenTurningOn(IKeyguardDrawnCallback callback) {
586             trace("onScreenTurningOn");
587             Trace.beginSection("KeyguardService.mBinder#onScreenTurningOn");
588             checkPermission();
589             mPowerInteractor.onScreenPowerStateUpdated(ScreenPowerState.SCREEN_TURNING_ON);
590             mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.SCREEN_TURNING_ON,
591                     callback);
592             mKeyguardUpdateMonitor.triggerTimeUpdate();
593 
594             final String onDrawWaitingTraceTag = "Waiting for KeyguardDrawnCallback#onDrawn";
595             final int traceCookie = System.identityHashCode(callback);
596             Trace.beginAsyncSection(onDrawWaitingTraceTag, traceCookie);
597 
598             // Ensure the drawn callback is only ever called once
599             mScreenOnCoordinator.onScreenTurningOn(new Runnable() {
600                 boolean mInvoked;
601                 @Override
602                 public void run() {
603                     if (callback == null) return;
604                     if (!mInvoked) {
605                         mInvoked = true;
606                         try {
607                             Trace.endAsyncSection(onDrawWaitingTraceTag, traceCookie);
608                             callback.onDrawn();
609                         } catch (RemoteException e) {
610                             Log.w(TAG, "Exception calling onDrawn():", e);
611                         }
612                     } else {
613                         Log.w(TAG, "KeyguardDrawnCallback#onDrawn() invoked > 1 times");
614                     }
615                 }
616             });
617 
618             Trace.endSection();
619         }
620 
621         @Override // Binder interface
622         public void onScreenTurnedOn() {
623             trace("onScreenTurnedOn");
624             Trace.beginSection("KeyguardService.mBinder#onScreenTurnedOn");
625             checkPermission();
626             mPowerInteractor.onScreenPowerStateUpdated(ScreenPowerState.SCREEN_ON);
627             mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.SCREEN_TURNED_ON);
628             mKeyguardUpdateMonitor.triggerTimeUpdate();
629             mScreenOnCoordinator.onScreenTurnedOn();
630             Trace.endSection();
631         }
632 
633         @Override // Binder interface
634         public void onScreenTurningOff() {
635             trace("onScreenTurningOff");
636             checkPermission();
637             mPowerInteractor.onScreenPowerStateUpdated(ScreenPowerState.SCREEN_TURNING_OFF);
638             mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.SCREEN_TURNING_OFF);
639         }
640 
641         @Override // Binder interface
642         public void onScreenTurnedOff() {
643             trace("onScreenTurnedOff");
644             checkPermission();
645             mPowerInteractor.onScreenPowerStateUpdated(ScreenPowerState.SCREEN_OFF);
646             mKeyguardViewMediator.onScreenTurnedOff();
647             mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.SCREEN_TURNED_OFF);
648             mScreenOnCoordinator.onScreenTurnedOff();
649         }
650 
651         @Override // Binder interface
652         public void setKeyguardEnabled(boolean enabled) {
653             trace("setKeyguardEnabled enabled" + enabled);
654             checkPermission();
655             mKeyguardEnabledInteractor.notifyKeyguardEnabled(enabled);
656             mKeyguardViewMediator.setKeyguardEnabled(enabled);
657         }
658 
659         @Override // Binder interface
660         public void onSystemReady() {
661             trace("onSystemReady");
662             Trace.beginSection("KeyguardService.mBinder#onSystemReady");
663             checkPermission();
664             mKeyguardViewMediator.onSystemReady();
665             Trace.endSection();
666         }
667 
668         @Override // Binder interface
669         public void doKeyguardTimeout(Bundle options) {
670             trace("doKeyguardTimeout");
671             checkPermission();
672 
673             if (SceneContainerFlag.isEnabled()) {
674                 mDeviceEntryInteractorLazy.get().lockNow("doKeyguardTimeout");
675             } else if (KeyguardWmStateRefactor.isEnabled()) {
676                 mKeyguardServiceShowLockscreenInteractor
677                         .onKeyguardServiceDoKeyguardTimeout(options);
678             }
679 
680             mKeyguardViewMediator.doKeyguardTimeout(options);
681         }
682 
683         // Binder interface
684         public void showDismissibleKeyguard() {
685             trace("showDismissibleKeyguard");
686             checkPermission();
687             if (mFoldGracePeriodProvider.get().isEnabled()) {
688                 mKeyguardInteractor.showDismissibleKeyguard();
689             }
690 
691             if (KeyguardWmStateRefactor.isEnabled()) {
692                 mKeyguardServiceShowLockscreenInteractor.onKeyguardServiceShowDismissibleKeyguard();
693             } else {
694                 mKeyguardViewMediator.showDismissibleKeyguard();
695             }
696 
697             if (SceneContainerFlag.isEnabled() && mFoldGracePeriodProvider.get().isEnabled()) {
698                 mMainExecutor.execute(() -> mSceneInteractorLazy.get().changeScene(
699                         Scenes.Lockscreen, "KeyguardService.showDismissibleKeyguard"));
700             }
701         }
702 
703         @Override // Binder interface
704         public void setSwitchingUser(boolean switching) {
705             trace("setSwitchingUser switching=" + switching);
706             checkPermission();
707             mKeyguardViewMediator.setSwitchingUser(switching);
708         }
709 
710         /**
711          * @deprecated This binder call is not listened to anymore. Instead the current user is
712          * tracked in SelectedUserInteractor.getSelectedUserId()
713          */
714         @Override // Binder interface
715         @Deprecated
716         public void setCurrentUser(int userId) {
717             trace("Deprecated/NOT USED: setCurrentUser userId=" + userId);
718             checkPermission();
719         }
720 
721         @Override // Binder interface
722         public void onBootCompleted() {
723             trace("onBootCompleted");
724             checkPermission();
725             mKeyguardViewMediator.onBootCompleted();
726         }
727 
728         /**
729          * @deprecated When remote animation is enabled, this won't be called anymore. Use
730          * {@code IRemoteAnimationRunner#onAnimationStart} instead.
731          */
732         @Deprecated
733         @Override // Binder interface
734         public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) {
735             trace("startKeyguardExitAnimation startTime=" + startTime
736                     + " fadeoutDuration=" + fadeoutDuration);
737             Trace.beginSection("KeyguardService.mBinder#startKeyguardExitAnimation");
738             checkPermission();
739             mKeyguardViewMediator.startKeyguardExitAnimation(startTime, fadeoutDuration);
740             Trace.endSection();
741         }
742 
743         @Override // Binder interface
744         public void onShortPowerPressedGoHome() {
745             trace("onShortPowerPressedGoHome");
746             checkPermission();
747             mKeyguardViewMediator.onShortPowerPressedGoHome();
748         }
749 
750         @Override // Binder interface
751         public void dismissKeyguardToLaunch(Intent intentToLaunch) {
752             trace("dismissKeyguardToLaunch");
753             checkPermission();
754             Slog.d(TAG, "Ignoring dismissKeyguardToLaunch " + intentToLaunch);
755         }
756 
757         @Override // Binder interface
758         public void onSystemKeyPressed(int keycode) {
759             trace("onSystemKeyPressed keycode=" + keycode);
760             checkPermission();
761             mKeyguardViewMediator.onSystemKeyPressed(keycode);
762         }
763     };
764 }
765 
766