From a287650f9431724a45c7687cd090021bc8350dea Mon Sep 17 00:00:00 2001 From: Heemin Seog Date: Wed, 24 Feb 2021 10:30:16 -0800 Subject: [PATCH] DO NOT MERGE Intercept back button on user switcher screen The back button on the user switcher screen should go back to the lockscreen of the current user. Bug: 178505273 Test: manual (press back button from user switcher screen while it has rotary focus) Change-Id: I30b8619a26e8b0c1ca9632b3bb656d187dad91b9 --- .../layout/car_fullscreen_user_switcher.xml | 41 +++++------ .../FullScreenUserSwitcherViewController.java | 16 +++- .../userswitcher/UserSwitcherContainer.java | 73 +++++++++++++++++++ 3 files changed, 105 insertions(+), 25 deletions(-) create mode 100644 packages/CarSystemUI/src/com/android/systemui/car/userswitcher/UserSwitcherContainer.java diff --git a/packages/CarSystemUI/res/layout/car_fullscreen_user_switcher.xml b/packages/CarSystemUI/res/layout/car_fullscreen_user_switcher.xml index 534c51e0febe3..99df6d52de073 100644 --- a/packages/CarSystemUI/res/layout/car_fullscreen_user_switcher.xml +++ b/packages/CarSystemUI/res/layout/car_fullscreen_user_switcher.xml @@ -14,36 +14,29 @@ See the License for the specific language governing permissions and limitations under the License. --> - + android:background="@color/car_user_switcher_background_color" + android:orientation="vertical"> - - - + android:theme="@android:style/Theme"/> - + - - + android:layout_height="wrap_content" + android:layout_gravity="center_vertical" + android:layout_marginTop="@dimen/car_user_switcher_margin_top"/> + - - + diff --git a/packages/CarSystemUI/src/com/android/systemui/car/userswitcher/FullScreenUserSwitcherViewController.java b/packages/CarSystemUI/src/com/android/systemui/car/userswitcher/FullScreenUserSwitcherViewController.java index aac4cfbf83c41..5fc7299f68c5a 100644 --- a/packages/CarSystemUI/src/com/android/systemui/car/userswitcher/FullScreenUserSwitcherViewController.java +++ b/packages/CarSystemUI/src/com/android/systemui/car/userswitcher/FullScreenUserSwitcherViewController.java @@ -22,6 +22,7 @@ import android.car.Car; import android.car.user.CarUserManager; import android.content.Context; import android.content.res.Resources; +import android.view.KeyEvent; import android.view.View; import androidx.recyclerview.widget.GridLayoutManager; @@ -67,6 +68,19 @@ public class FullScreenUserSwitcherViewController extends OverlayViewController @Override protected void onFinishInflate() { + // Intercept back button. + UserSwitcherContainer container = getLayout().findViewById(R.id.container); + container.setKeyEventHandler(event -> { + if (event.getKeyCode() != KeyEvent.KEYCODE_BACK) { + return false; + } + + if (event.getAction() == KeyEvent.ACTION_UP && getLayout().isVisibleToUser()) { + getLayout().setVisibility(View.GONE); + } + return true; + }); + // Initialize user grid. mUserGridView = getLayout().findViewById(R.id.user_grid); GridLayoutManager layoutManager = new GridLayoutManager(mContext, @@ -79,7 +93,7 @@ public class FullScreenUserSwitcherViewController extends OverlayViewController @Override protected boolean shouldFocusWindow() { - return false; + return true; } @Override diff --git a/packages/CarSystemUI/src/com/android/systemui/car/userswitcher/UserSwitcherContainer.java b/packages/CarSystemUI/src/com/android/systemui/car/userswitcher/UserSwitcherContainer.java new file mode 100644 index 0000000000000..5b6271107380e --- /dev/null +++ b/packages/CarSystemUI/src/com/android/systemui/car/userswitcher/UserSwitcherContainer.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.car.userswitcher; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.KeyEvent; +import android.widget.LinearLayout; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +/** Container for the user switcher which intercepts the key events. */ +public class UserSwitcherContainer extends LinearLayout { + + private KeyEventHandler mKeyEventHandler; + + public UserSwitcherContainer(@NonNull Context context) { + super(context); + } + + public UserSwitcherContainer(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + } + + public UserSwitcherContainer(@NonNull Context context, @Nullable AttributeSet attrs, + int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public UserSwitcherContainer(@NonNull Context context, @Nullable AttributeSet attrs, + int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + @Override + public boolean dispatchKeyEvent(KeyEvent event) { + if (super.dispatchKeyEvent(event)) { + return true; + } + + if (mKeyEventHandler != null) { + return mKeyEventHandler.dispatchKeyEvent(event); + } + + return false; + } + + /** Sets a {@link KeyEventHandler} to help interact with the notification panel. */ + public void setKeyEventHandler(KeyEventHandler keyEventHandler) { + mKeyEventHandler = keyEventHandler; + } + + /** An interface to help interact with the notification panel. */ + public interface KeyEventHandler { + /** Allows handling of a {@link KeyEvent} if it wasn't already handled by the superclass. */ + boolean dispatchKeyEvent(KeyEvent event); + } +}