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