Xiyuan Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 1 | /* |
| 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 Iyer | 2790a37 | 2016-01-22 15:33:39 -0800 | [diff] [blame] | 17 | package com.android.systemui.statusbar.car; |
Xiyuan Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 18 | |
Rakesh Iyer | 33d6ce4 | 2017-05-30 11:02:18 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Xiyuan Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 22 | import android.view.View; |
| 23 | import android.view.ViewStub; |
| 24 | |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 25 | import android.support.v7.widget.GridLayoutManager; |
| 26 | import android.support.v7.widget.RecyclerView; |
jovanak | b803051 | 2018-04-11 15:20:16 -0700 | [diff] [blame] | 27 | |
| 28 | import com.android.settingslib.users.UserManagerHelper; |
Xiyuan Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 29 | import com.android.systemui.R; |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 30 | import com.android.systemui.statusbar.phone.StatusBar; |
Xiyuan Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Manages the fullscreen user switcher. |
| 34 | */ |
| 35 | public class FullscreenUserSwitcher { |
Rakesh Iyer | 97ff858 | 2017-05-22 17:47:52 -0700 | [diff] [blame] | 36 | private final View mContainer; |
Rakesh Iyer | 33d6ce4 | 2017-05-30 11:02:18 -0700 | [diff] [blame] | 37 | private final View mParent; |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 38 | private final UserGridRecyclerView mUserGridView; |
Rakesh Iyer | 33d6ce4 | 2017-05-30 11:02:18 -0700 | [diff] [blame] | 39 | private final int mShortAnimDuration; |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 40 | private final StatusBar mStatusBar; |
jovanak | b803051 | 2018-04-11 15:20:16 -0700 | [diff] [blame] | 41 | private final UserManagerHelper mUserManagerHelper; |
| 42 | private int mCurrentForegroundUserId; |
Rakesh Iyer | 33d6ce4 | 2017-05-30 11:02:18 -0700 | [diff] [blame] | 43 | private boolean mShowing; |
Xiyuan Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 44 | |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 45 | public FullscreenUserSwitcher(StatusBar statusBar, ViewStub containerStub, Context context) { |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 46 | mStatusBar = statusBar; |
Rakesh Iyer | 33d6ce4 | 2017-05-30 11:02:18 -0700 | [diff] [blame] | 47 | mParent = containerStub.inflate(); |
| 48 | mContainer = mParent.findViewById(R.id.container); |
Rakesh Iyer | 5b3278f | 2017-04-19 11:28:26 -0700 | [diff] [blame] | 49 | mUserGridView = mContainer.findViewById(R.id.user_grid); |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 50 | GridLayoutManager layoutManager = new GridLayoutManager(context, |
| 51 | context.getResources().getInteger(R.integer.user_fullscreen_switcher_num_col)); |
| 52 | mUserGridView.setLayoutManager(layoutManager); |
| 53 | mUserGridView.buildAdapter(); |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 54 | mUserGridView.setUserSelectionListener(this::onUserSelected); |
Rakesh Iyer | 5b3278f | 2017-04-19 11:28:26 -0700 | [diff] [blame] | 55 | |
jovanak | b803051 | 2018-04-11 15:20:16 -0700 | [diff] [blame] | 56 | mUserManagerHelper = new UserManagerHelper(context); |
| 57 | updateCurrentForegroundUser(); |
| 58 | |
Aarthi Balachander | d8bf249 | 2018-03-30 11:15:59 -0700 | [diff] [blame] | 59 | mShortAnimDuration = mContainer.getResources() |
| 60 | .getInteger(android.R.integer.config_shortAnimTime); |
Xiyuan Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 61 | } |
| 62 | |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 63 | 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 Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 77 | public void onUserSwitched(int newUserId) { |
jovanak | b803051 | 2018-04-11 15:20:16 -0700 | [diff] [blame] | 78 | // 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(); |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private void onUserSelected(UserGridRecyclerView.UserRecord record) { |
| 96 | if (record.mIsForeground) { |
jovanak | b803051 | 2018-04-11 15:20:16 -0700 | [diff] [blame] | 97 | dismissKeyguard(); |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | toggleSwitchInProgress(true); |
| 101 | } |
| 102 | |
jovanak | b803051 | 2018-04-11 15:20:16 -0700 | [diff] [blame] | 103 | // Dismisses the keyguard and shows bouncer if authentication is necessary. |
| 104 | private void dismissKeyguard() { |
jovanak | 0535abc | 2018-04-10 15:14:50 -0700 | [diff] [blame] | 105 | mStatusBar.executeRunnableDismissingKeyguard(null/* runnable */, null /* cancelAction */, |
| 106 | true /* dismissShade */, true /* afterKeyguardGone */, true /* deferred */); |
Xiyuan Xia | cc3a74f6 | 2015-07-22 14:16:34 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Rakesh Iyer | 33d6ce4 | 2017-05-30 11:02:18 -0700 | [diff] [blame] | 109 | private void toggleSwitchInProgress(boolean inProgress) { |
| 110 | if (inProgress) { |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 111 | crossFade(mParent, mContainer); |
Rakesh Iyer | 33d6ce4 | 2017-05-30 11:02:18 -0700 | [diff] [blame] | 112 | } else { |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 113 | crossFade(mContainer, mParent); |
Rakesh Iyer | 33d6ce4 | 2017-05-30 11:02:18 -0700 | [diff] [blame] | 114 | } |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 115 | |
Rakesh Iyer | 33d6ce4 | 2017-05-30 11:02:18 -0700 | [diff] [blame] | 116 | } |
| 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 Iyer | 97ff858 | 2017-05-22 17:47:52 -0700 | [diff] [blame] | 140 | } |