blob: c1af1fa93df47a55cdec78b92f56e4c87545d1d6 [file] [log] [blame]
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -07001/*
2 * Copyright (C) 2015 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
Rakesh Iyer2790a372016-01-22 15:33:39 -080017package com.android.systemui.statusbar.car;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070018
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070021import android.content.Context;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070022import android.view.View;
23import android.view.ViewStub;
24
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070025import android.support.v7.widget.GridLayoutManager;
26import android.support.v7.widget.RecyclerView;
jovanakb8030512018-04-11 15:20:16 -070027
28import com.android.settingslib.users.UserManagerHelper;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070029import com.android.systemui.R;
Jason Monk2a6ea9c2017-01-26 11:14:51 -050030import com.android.systemui.statusbar.phone.StatusBar;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070031
32/**
33 * Manages the fullscreen user switcher.
34 */
35public class FullscreenUserSwitcher {
Rakesh Iyer97ff8582017-05-22 17:47:52 -070036 private final View mContainer;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070037 private final View mParent;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070038 private final UserGridRecyclerView mUserGridView;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070039 private final int mShortAnimDuration;
jovanak0535abc2018-04-10 15:14:50 -070040 private final StatusBar mStatusBar;
jovanakb8030512018-04-11 15:20:16 -070041 private final UserManagerHelper mUserManagerHelper;
42 private int mCurrentForegroundUserId;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070043 private boolean mShowing;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070044
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070045 public FullscreenUserSwitcher(StatusBar statusBar, ViewStub containerStub, Context context) {
jovanak0535abc2018-04-10 15:14:50 -070046 mStatusBar = statusBar;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070047 mParent = containerStub.inflate();
48 mContainer = mParent.findViewById(R.id.container);
Rakesh Iyer5b3278f2017-04-19 11:28:26 -070049 mUserGridView = mContainer.findViewById(R.id.user_grid);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070050 GridLayoutManager layoutManager = new GridLayoutManager(context,
51 context.getResources().getInteger(R.integer.user_fullscreen_switcher_num_col));
52 mUserGridView.setLayoutManager(layoutManager);
53 mUserGridView.buildAdapter();
jovanak0535abc2018-04-10 15:14:50 -070054 mUserGridView.setUserSelectionListener(this::onUserSelected);
Rakesh Iyer5b3278f2017-04-19 11:28:26 -070055
jovanakb8030512018-04-11 15:20:16 -070056 mUserManagerHelper = new UserManagerHelper(context);
57 updateCurrentForegroundUser();
58
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070059 mShortAnimDuration = mContainer.getResources()
60 .getInteger(android.R.integer.config_shortAnimTime);
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070061 }
62
jovanak0535abc2018-04-10 15:14:50 -070063 public void show() {
64 if (mShowing) {
65 return;
66 }
67 mShowing = true;
68 mParent.setVisibility(View.VISIBLE);
69 }
70
71 public void hide() {
72 mShowing = false;
73 toggleSwitchInProgress(false);
74 mParent.setVisibility(View.GONE);
75 }
76
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070077 public void onUserSwitched(int newUserId) {
jovanakb8030512018-04-11 15:20:16 -070078 // The logic for foreground user change is needed here to exclude the reboot case. On
79 // reboot, system fires ACTION_USER_SWITCHED change from -1 to 0 user. This is not an actual
80 // user switch. We only want to trigger keyguard dismissal when foreground user changes.
81 if (foregroundUserChanged()) {
82 updateCurrentForegroundUser();
83 mParent.post(this::dismissKeyguard);
84 }
85 }
86
87 private boolean foregroundUserChanged() {
88 return mCurrentForegroundUserId != mUserManagerHelper.getForegroundUserId();
89 }
90
91 private void updateCurrentForegroundUser() {
92 mCurrentForegroundUserId = mUserManagerHelper.getForegroundUserId();
jovanak0535abc2018-04-10 15:14:50 -070093 }
94
95 private void onUserSelected(UserGridRecyclerView.UserRecord record) {
96 if (record.mIsForeground) {
jovanakb8030512018-04-11 15:20:16 -070097 dismissKeyguard();
jovanak0535abc2018-04-10 15:14:50 -070098 return;
99 }
100 toggleSwitchInProgress(true);
101 }
102
jovanakb8030512018-04-11 15:20:16 -0700103 // Dismisses the keyguard and shows bouncer if authentication is necessary.
104 private void dismissKeyguard() {
jovanak0535abc2018-04-10 15:14:50 -0700105 mStatusBar.executeRunnableDismissingKeyguard(null/* runnable */, null /* cancelAction */,
106 true /* dismissShade */, true /* afterKeyguardGone */, true /* deferred */);
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -0700107 }
108
Rakesh Iyer33d6ce42017-05-30 11:02:18 -0700109 private void toggleSwitchInProgress(boolean inProgress) {
110 if (inProgress) {
Aarthi Balachandercf6ca0c2018-04-10 19:26:45 -0700111 crossFade(mParent, mContainer);
Rakesh Iyer33d6ce42017-05-30 11:02:18 -0700112 } else {
Aarthi Balachandercf6ca0c2018-04-10 19:26:45 -0700113 crossFade(mContainer, mParent);
Rakesh Iyer33d6ce42017-05-30 11:02:18 -0700114 }
Aarthi Balachandercf6ca0c2018-04-10 19:26:45 -0700115
Rakesh Iyer33d6ce42017-05-30 11:02:18 -0700116 }
117
118 private void crossFade(View incoming, View outgoing) {
119 incoming.animate()
120 .alpha(1.0f)
121 .setDuration(mShortAnimDuration)
122 .setListener(new AnimatorListenerAdapter() {
123 @Override
124 public void onAnimationStart(Animator animator) {
125 incoming.setAlpha(0.0f);
126 incoming.setVisibility(View.VISIBLE);
127 }
128 });
129
130 outgoing.animate()
131 .alpha(0.0f)
132 .setDuration(mShortAnimDuration)
133 .setListener(new AnimatorListenerAdapter() {
134 @Override
135 public void onAnimationEnd(Animator animation) {
136 outgoing.setVisibility(View.GONE);
137 }
138 });
139 }
Rakesh Iyer97ff8582017-05-22 17:47:52 -0700140}