1 /* 2 * Copyright (C) 2020 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.car.userswitcher; 18 19 import android.animation.Animator; 20 import android.animation.AnimatorListenerAdapter; 21 import android.car.Car; 22 import android.car.user.CarUserManager; 23 import android.content.Context; 24 import android.content.res.Resources; 25 import android.view.KeyEvent; 26 import android.view.View; 27 28 import androidx.recyclerview.widget.GridLayoutManager; 29 30 import com.android.systemui.R; 31 import com.android.systemui.car.CarServiceProvider; 32 import com.android.systemui.car.window.OverlayViewController; 33 import com.android.systemui.car.window.OverlayViewGlobalStateController; 34 import com.android.systemui.dagger.SysUISingleton; 35 import com.android.systemui.dagger.qualifiers.Main; 36 import com.android.systemui.settings.UserTracker; 37 38 import javax.inject.Inject; 39 40 /** 41 * Controller for {@link R.layout#car_fullscreen_user_switcher}. 42 */ 43 @SysUISingleton 44 public class FullScreenUserSwitcherViewController extends OverlayViewController { 45 private final Context mContext; 46 private final UserTracker mUserTracker; 47 private final Resources mResources; 48 private final CarServiceProvider mCarServiceProvider; 49 private final int mShortAnimationDuration; 50 private CarUserManager mCarUserManager; 51 private UserGridRecyclerView mUserGridView; 52 private UserGridRecyclerView.UserSelectionListener mUserSelectionListener; 53 54 @Inject FullScreenUserSwitcherViewController( Context context, UserTracker userTracker, @Main Resources resources, CarServiceProvider carServiceProvider, OverlayViewGlobalStateController overlayViewGlobalStateController)55 public FullScreenUserSwitcherViewController( 56 Context context, 57 UserTracker userTracker, 58 @Main Resources resources, 59 CarServiceProvider carServiceProvider, 60 OverlayViewGlobalStateController overlayViewGlobalStateController) { 61 super(R.id.fullscreen_user_switcher_stub, overlayViewGlobalStateController); 62 mContext = context; 63 mUserTracker = userTracker; 64 mResources = resources; 65 mCarServiceProvider = carServiceProvider; 66 mCarServiceProvider.addListener(car -> { 67 mCarUserManager = (CarUserManager) car.getCarManager(Car.CAR_USER_SERVICE); 68 registerCarUserManagerIfPossible(); 69 }); 70 mShortAnimationDuration = mResources.getInteger(android.R.integer.config_shortAnimTime); 71 } 72 73 @Override onFinishInflate()74 protected void onFinishInflate() { 75 // Intercept back button. 76 UserSwitcherContainer container = getLayout().findViewById(R.id.user_switcher_container); 77 container.setKeyEventHandler(event -> { 78 if (event.getKeyCode() != KeyEvent.KEYCODE_BACK) { 79 return false; 80 } 81 82 if (event.getAction() == KeyEvent.ACTION_UP && getLayout().isVisibleToUser()) { 83 stop(); 84 } 85 return true; 86 }); 87 88 // Initialize user grid. 89 mUserGridView = getLayout().findViewById(R.id.user_grid); 90 GridLayoutManager layoutManager = new GridLayoutManager(mContext, 91 mResources.getInteger(R.integer.user_fullscreen_switcher_num_col)); 92 mUserGridView.setLayoutManager(layoutManager); 93 mUserGridView.setUserTracker(mUserTracker); 94 mUserGridView.buildAdapter(); 95 mUserGridView.setUserSelectionListener(mUserSelectionListener); 96 registerCarUserManagerIfPossible(); 97 } 98 99 @Override getFocusAreaViewId()100 protected int getFocusAreaViewId() { 101 return R.id.user_switcher_focus_area; 102 } 103 104 @Override shouldFocusWindow()105 protected boolean shouldFocusWindow() { 106 return true; 107 } 108 109 @Override showInternal()110 protected void showInternal() { 111 getLayout().setVisibility(View.VISIBLE); 112 } 113 114 @Override hideInternal()115 protected void hideInternal() { 116 // Switching is about to happen, since it takes time, fade out the switcher gradually. 117 fadeOut(); 118 } 119 fadeOut()120 private void fadeOut() { 121 mUserGridView.animate() 122 .alpha(0.0f) 123 .setDuration(mShortAnimationDuration) 124 .setListener(new AnimatorListenerAdapter() { 125 @Override 126 public void onAnimationEnd(Animator animation) { 127 getLayout().setVisibility(View.GONE); 128 mUserGridView.setAlpha(1.0f); 129 } 130 }); 131 132 } 133 134 /** 135 * Set {@link UserGridRecyclerView.UserSelectionListener}. 136 */ setUserGridSelectionListener( UserGridRecyclerView.UserSelectionListener userGridSelectionListener)137 void setUserGridSelectionListener( 138 UserGridRecyclerView.UserSelectionListener userGridSelectionListener) { 139 mUserSelectionListener = userGridSelectionListener; 140 } 141 registerCarUserManagerIfPossible()142 private void registerCarUserManagerIfPossible() { 143 if (mUserGridView != null && mCarUserManager != null) { 144 mUserGridView.setCarUserManager(mCarUserManager); 145 } 146 } 147 } 148