blob: 0a167d9acf982fe040a568baece3b890b6678589 [file] [log] [blame]
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -07001/*
Brad Stenning8d1a51c2018-11-20 17:34:16 -08002 * Copyright (C) 2018 The Android Open Source Project
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -07003 *
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
Brad Stenning8d1a51c2018-11-20 17:34:16 -080014 * limitations under the License.
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070015 */
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
Aurimas Liutikasfd52c142018-04-17 09:50:46 -070025import androidx.recyclerview.widget.GridLayoutManager;
jovanakb8030512018-04-11 15:20:16 -070026
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070027import com.android.systemui.R;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070028
29/**
30 * Manages the fullscreen user switcher.
31 */
32public class FullscreenUserSwitcher {
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070033 private final UserGridRecyclerView mUserGridView;
jovanak604ad512018-08-14 18:41:27 -070034 private final View mParent;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070035 private final int mShortAnimDuration;
jovanak604ad512018-08-14 18:41:27 -070036 private final CarStatusBar mStatusBar;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070037
jovanak604ad512018-08-14 18:41:27 -070038 public FullscreenUserSwitcher(CarStatusBar statusBar, ViewStub containerStub, Context context) {
jovanak0535abc2018-04-10 15:14:50 -070039 mStatusBar = statusBar;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070040 mParent = containerStub.inflate();
Brad Stenning3b0d7642019-03-28 11:04:30 -070041 // Hide the user grid by default. It will only be made visible by clicking on a cancel
42 // button in a bouncer.
43 hide();
jovanak604ad512018-08-14 18:41:27 -070044 View container = mParent.findViewById(R.id.container);
45
46 // Initialize user grid.
47 mUserGridView = container.findViewById(R.id.user_grid);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070048 GridLayoutManager layoutManager = new GridLayoutManager(context,
Brad Stenning8d1a51c2018-11-20 17:34:16 -080049 context.getResources().getInteger(R.integer.user_fullscreen_switcher_num_col));
Heemin Seog0d5e0182019-03-13 13:49:24 -070050 mUserGridView.setLayoutManager(layoutManager);
Aarthi Balachanderd8bf2492018-03-30 11:15:59 -070051 mUserGridView.buildAdapter();
jovanak0535abc2018-04-10 15:14:50 -070052 mUserGridView.setUserSelectionListener(this::onUserSelected);
Rakesh Iyer5b3278f2017-04-19 11:28:26 -070053
jovanak604ad512018-08-14 18:41:27 -070054 mShortAnimDuration = container.getResources()
Brad Stenning8d1a51c2018-11-20 17:34:16 -080055 .getInteger(android.R.integer.config_shortAnimTime);
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070056 }
57
jovanak604ad512018-08-14 18:41:27 -070058 /**
59 * Makes user grid visible.
60 */
jovanak0535abc2018-04-10 15:14:50 -070061 public void show() {
Brad Stenning3b0d7642019-03-28 11:04:30 -070062 mParent.setVisibility(View.VISIBLE);
Aarthi Balachander0a427ef2018-07-13 15:00:58 -070063 }
64
jovanak604ad512018-08-14 18:41:27 -070065 /**
66 * Hides the user grid.
67 */
68 public void hide() {
Brad Stenning3b0d7642019-03-28 11:04:30 -070069 mParent.setVisibility(View.INVISIBLE);
Aarthi Balachander0a427ef2018-07-13 15:00:58 -070070 }
71
jovanak604ad512018-08-14 18:41:27 -070072 /**
73 * @return {@code true} if user grid is visible, {@code false} otherwise.
74 */
75 public boolean isVisible() {
Brad Stenning3b0d7642019-03-28 11:04:30 -070076 return mParent.getVisibility() == View.VISIBLE;
Xiyuan Xiacc3a74f62015-07-22 14:16:34 -070077 }
78
jovanak604ad512018-08-14 18:41:27 -070079 /**
80 * Every time user clicks on an item in the switcher, we hide the switcher, either
81 * gradually or immediately.
82 *
83 * We dismiss the entire keyguard if user clicked on the foreground user (user we're already
84 * logged in as).
85 */
86 private void onUserSelected(UserGridRecyclerView.UserRecord record) {
87 if (record.mIsForeground) {
88 hide();
89 mStatusBar.dismissKeyguard();
90 return;
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070091 }
jovanak604ad512018-08-14 18:41:27 -070092 // Switching is about to happen, since it takes time, fade out the switcher gradually.
93 fadeOut();
Rakesh Iyer33d6ce42017-05-30 11:02:18 -070094 }
95
jovanak604ad512018-08-14 18:41:27 -070096 private void fadeOut() {
97 mUserGridView.animate()
jovanak9c177202018-04-20 12:35:09 -070098 .alpha(0.0f)
99 .setDuration(mShortAnimDuration)
100 .setListener(new AnimatorListenerAdapter() {
101 @Override
102 public void onAnimationEnd(Animator animation) {
jovanak604ad512018-08-14 18:41:27 -0700103 hide();
104 mUserGridView.setAlpha(1.0f);
jovanak9c177202018-04-20 12:35:09 -0700105 }
106 });
Rakesh Iyer33d6ce42017-05-30 11:02:18 -0700107
Rakesh Iyer33d6ce42017-05-30 11:02:18 -0700108 }
Brad Stenning8d1a51c2018-11-20 17:34:16 -0800109}