blob: 5a02d18c32d4f0ba535989cee932a972f8e29cee [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;
Rakesh Iyer97ff8582017-05-22 17:47:52 -070024import android.widget.ProgressBar;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070025
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070026import android.support.v7.widget.GridLayoutManager;
27import android.support.v7.widget.RecyclerView;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070028import com.android.systemui.R;
Jason Monk2a6ea9c2017-01-26 11:14:51 -050029import com.android.systemui.statusbar.phone.StatusBar;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070030
31/**
32 * Manages the fullscreen user switcher.
33 */
34public class FullscreenUserSwitcher {
Rakesh Iyer97ff8582017-05-22 17:47:52 -070035 private final View mContainer;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070036 private final View mParent;
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070037 private final UserGridRecyclerView mUserGridView;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070038 private final ProgressBar mSwitchingUsers;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070039 private final int mShortAnimDuration;
jovanak0535abc2018-04-10 15:14:50 -070040 private final StatusBar mStatusBar;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070041
42 private boolean mShowing;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070043
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070044 public FullscreenUserSwitcher(StatusBar statusBar, ViewStub containerStub, Context context) {
jovanak0535abc2018-04-10 15:14:50 -070045 mStatusBar = statusBar;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070046 mParent = containerStub.inflate();
47 mContainer = mParent.findViewById(R.id.container);
Rakesh Iyer5b3278f2017-04-19 11:28:26 -070048 mUserGridView = mContainer.findViewById(R.id.user_grid);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070049 GridLayoutManager layoutManager = new GridLayoutManager(context,
50 context.getResources().getInteger(R.integer.user_fullscreen_switcher_num_col));
51 mUserGridView.setLayoutManager(layoutManager);
52 mUserGridView.buildAdapter();
jovanak0535abc2018-04-10 15:14:50 -070053 mUserGridView.setUserSelectionListener(this::onUserSelected);
Rakesh Iyer5b3278f2017-04-19 11:28:26 -070054
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070055 mShortAnimDuration = mContainer.getResources()
56 .getInteger(android.R.integer.config_shortAnimTime);
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070057
58 mSwitchingUsers = mParent.findViewById(R.id.switching_users);
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070059 }
60
jovanak0535abc2018-04-10 15:14:50 -070061 public void show() {
62 if (mShowing) {
63 return;
64 }
65 mShowing = true;
66 mParent.setVisibility(View.VISIBLE);
67 }
68
69 public void hide() {
70 mShowing = false;
71 toggleSwitchInProgress(false);
72 mParent.setVisibility(View.GONE);
73 }
74
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070075 public void onUserSwitched(int newUserId) {
jovanak0535abc2018-04-10 15:14:50 -070076 mParent.post(this::showOfflineAuthUi);
77 }
78
79 private void onUserSelected(UserGridRecyclerView.UserRecord record) {
80 if (record.mIsForeground) {
81 showOfflineAuthUi();
82 return;
83 }
84 toggleSwitchInProgress(true);
85 }
86
87 private void showOfflineAuthUi() {
88 mStatusBar.executeRunnableDismissingKeyguard(null/* runnable */, null /* cancelAction */,
89 true /* dismissShade */, true /* afterKeyguardGone */, true /* deferred */);
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070090 }
91
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070092 private void toggleSwitchInProgress(boolean inProgress) {
93 if (inProgress) {
94 crossFade(mSwitchingUsers, mContainer);
95 } else {
96 crossFade(mContainer, mSwitchingUsers);
97 }
98 }
99
100 private void crossFade(View incoming, View outgoing) {
101 incoming.animate()
102 .alpha(1.0f)
103 .setDuration(mShortAnimDuration)
104 .setListener(new AnimatorListenerAdapter() {
105 @Override
106 public void onAnimationStart(Animator animator) {
107 incoming.setAlpha(0.0f);
108 incoming.setVisibility(View.VISIBLE);
109 }
110 });
111
112 outgoing.animate()
113 .alpha(0.0f)
114 .setDuration(mShortAnimDuration)
115 .setListener(new AnimatorListenerAdapter() {
116 @Override
117 public void onAnimationEnd(Animator animation) {
118 outgoing.setVisibility(View.GONE);
119 }
120 });
121 }
Rakesh Iyer97ff8582017-05-22 17:47:52 -0700122}