diff --git a/packages/CarSystemUI/AndroidManifest.xml b/packages/CarSystemUI/AndroidManifest.xml index 1dd02919a0931..c0482e1b3e046 100644 --- a/packages/CarSystemUI/AndroidManifest.xml +++ b/packages/CarSystemUI/AndroidManifest.xml @@ -25,6 +25,11 @@ + + + + + diff --git a/packages/CarSystemUI/res/layout/car_navigation_bar.xml b/packages/CarSystemUI/res/layout/car_navigation_bar.xml index 1418bf8604bf1..2a715d0c3494e 100644 --- a/packages/CarSystemUI/res/layout/car_navigation_bar.xml +++ b/packages/CarSystemUI/res/layout/car_navigation_bar.xml @@ -125,6 +125,7 @@ android:id="@+id/assist" style="@style/NavigationBarButton" systemui:icon="@drawable/ic_mic_white" + systemui:useDefaultAppIconForRole="true" /> diff --git a/packages/CarSystemUI/res/layout/car_navigation_button.xml b/packages/CarSystemUI/res/layout/car_navigation_button.xml index 837252b6d7160..ca4e76ee104b9 100644 --- a/packages/CarSystemUI/res/layout/car_navigation_button.xml +++ b/packages/CarSystemUI/res/layout/car_navigation_button.xml @@ -29,12 +29,14 @@ @@ -48,6 +50,7 @@ android:background="@android:color/transparent" android:scaleType="fitCenter" android:clickable="false" + android:visibility="gone" /> + + diff --git a/packages/CarSystemUI/res/values/colors.xml b/packages/CarSystemUI/res/values/colors.xml index 0e84d517759a3..d20ab49a22e61 100644 --- a/packages/CarSystemUI/res/values/colors.xml +++ b/packages/CarSystemUI/res/values/colors.xml @@ -21,7 +21,7 @@ @*android:color/car_body1_light #131313 @*android:color/car_body1_light - #8Fffffff + #8F8F8F #ffffff #131315 diff --git a/packages/CarSystemUI/res/values/dimens.xml b/packages/CarSystemUI/res/values/dimens.xml index ed0b4853994d0..cb321cdc6c4dc 100644 --- a/packages/CarSystemUI/res/values/dimens.xml +++ b/packages/CarSystemUI/res/values/dimens.xml @@ -175,6 +175,7 @@ @*android:dimen/car_padding_4 64dp + 44dp 760dp 96dp 96dp diff --git a/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/AssitantButton.java b/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/AssitantButton.java index 69ec78eab593e..ede4696a96c3e 100644 --- a/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/AssitantButton.java +++ b/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/AssitantButton.java @@ -18,6 +18,7 @@ package com.android.systemui.car.navigationbar; import static android.service.voice.VoiceInteractionSession.SHOW_SOURCE_ASSIST_GESTURE; +import android.app.role.RoleManager; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; @@ -31,7 +32,6 @@ import com.android.internal.app.IVoiceInteractionSessionShowCallback; * AssitantButton is a ui component that will trigger the Voice Interaction Service. */ public class AssitantButton extends CarNavigationButton { - private static final String TAG = "AssistantButton"; private final AssistUtils mAssistUtils; private IVoiceInteractionSessionShowCallback mShowCallback = @@ -50,9 +50,7 @@ public class AssitantButton extends CarNavigationButton { public AssitantButton(Context context, AttributeSet attrs) { super(context, attrs); mAssistUtils = new AssistUtils(context); - setOnClickListener(v -> { - showAssistant(); - }); + setOnClickListener(v -> showAssistant()); } private void showAssistant() { @@ -65,4 +63,9 @@ public class AssitantButton extends CarNavigationButton { protected void setUpIntents(TypedArray typedArray) { // left blank because for the assistant button Intent will not be passed from the layout. } + + @Override + protected String getRoleName() { + return RoleManager.ROLE_ASSISTANT; + } } diff --git a/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/ButtonRoleHolderController.java b/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/ButtonRoleHolderController.java new file mode 100644 index 0000000000000..5c83c025bc202 --- /dev/null +++ b/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/ButtonRoleHolderController.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2020 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.navigationbar; + +import android.annotation.Nullable; +import android.app.role.OnRoleHoldersChangedListener; +import android.app.role.RoleManager; +import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.graphics.drawable.Drawable; +import android.os.UserHandle; +import android.util.Log; +import android.view.View; +import android.view.ViewGroup; + +import com.android.internal.annotations.VisibleForTesting; +import com.android.systemui.car.CarDeviceProvisionedController; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Singleton; + +/** + * Some CarNavigationButtons can be associated to a {@link RoleManager} role. When they are, it is + * possible to have them display the icon of the default application (role holder) for the given + * role. + * + * This class monitors the current role holders for each role type and updates the button icon for + * this buttons with have this feature enabled. + */ +@Singleton +public class ButtonRoleHolderController { + private static final String TAG = "ButtonRoleHolderController"; + + private final Context mContext; + private final PackageManager mPackageManager; + private final RoleManager mRoleManager; + private final CarDeviceProvisionedController mDeviceController; + private final Map mButtonMap = new HashMap<>(); + private final OnRoleHoldersChangedListener mListener = this::onRoleChanged; + private boolean mRegistered; + + @Inject + public ButtonRoleHolderController(Context context, PackageManager packageManager, + RoleManager roleManager, CarDeviceProvisionedController deviceController) { + mContext = context; + mPackageManager = packageManager; + mRoleManager = roleManager; + mDeviceController = deviceController; + } + + /** + * Iterate through a view looking for CarNavigationButton and add it to this controller if it + * opted to be associated with a {@link RoleManager} role type. + * + * @param v the View that may contain CarFacetButtons + */ + void addAllButtonsWithRoleName(View v) { + if (v instanceof CarNavigationButton) { + CarNavigationButton button = (CarNavigationButton) v; + String roleName = button.getRoleName(); + if (roleName != null && button.isDefaultAppIconForRoleEnabled()) { + addButtonWithRoleName(button, roleName); + } + } else if (v instanceof ViewGroup) { + ViewGroup viewGroup = (ViewGroup) v; + for (int i = 0; i < viewGroup.getChildCount(); i++) { + addAllButtonsWithRoleName(viewGroup.getChildAt(i)); + } + } + } + + private void addButtonWithRoleName(CarNavigationButton button, String roleName) { + mButtonMap.put(roleName, button); + updateIcon(roleName); + if (!mRegistered) { + mRoleManager.addOnRoleHoldersChangedListenerAsUser(mContext.getMainExecutor(), + mListener, UserHandle.ALL); + mRegistered = true; + } + } + + void removeAll() { + mButtonMap.clear(); + if (mRegistered) { + mRoleManager.removeOnRoleHoldersChangedListenerAsUser(mListener, UserHandle.ALL); + mRegistered = false; + } + } + + @VisibleForTesting + void onRoleChanged(String roleName, UserHandle user) { + if (RoleManager.ROLE_ASSISTANT.equals(roleName) + && user.getIdentifier() == mDeviceController.getCurrentUser()) { + updateIcon(roleName); + } + } + + private void updateIcon(String roleName) { + CarNavigationButton button = mButtonMap.get(roleName); + if (button == null) { + return; + } + List holders = mRoleManager.getRoleHoldersAsUser(button.getRoleName(), + UserHandle.of(mDeviceController.getCurrentUser())); + if (holders == null || holders.isEmpty()) { + button.setAppIcon(null); + } else { + button.setAppIcon(loadIcon(holders.get(0))); + } + } + + @Nullable + private Drawable loadIcon(String packageName) { + try { + ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, + PackageManager.MATCH_ANY_USER); + return appInfo.loadIcon(mPackageManager); + } catch (PackageManager.NameNotFoundException e) { + Log.e(ButtonRoleHolderController.TAG, "Package not found: " + packageName, e); + return null; + } + } +} diff --git a/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationBar.java b/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationBar.java index 5c6472ecb4eff..4c720abb4c748 100644 --- a/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationBar.java +++ b/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationBar.java @@ -87,7 +87,6 @@ public class CarNavigationBar extends SystemUI implements CommandQueue.Callbacks private final Executor mUiBgExecutor; private final IStatusBarService mBarService; private final Lazy mKeyguardStateControllerLazy; - private final ButtonSelectionStateController mButtonSelectionStateController; private final Lazy mIconPolicyLazy; private final Lazy mIconControllerLazy; @@ -139,7 +138,6 @@ public class CarNavigationBar extends SystemUI implements CommandQueue.Callbacks @UiBackground Executor uiBgExecutor, IStatusBarService barService, Lazy keyguardStateControllerLazy, - ButtonSelectionStateController buttonSelectionStateController, Lazy iconPolicyLazy, Lazy iconControllerLazy ) { @@ -156,7 +154,6 @@ public class CarNavigationBar extends SystemUI implements CommandQueue.Callbacks mUiBgExecutor = uiBgExecutor; mBarService = barService; mKeyguardStateControllerLazy = keyguardStateControllerLazy; - mButtonSelectionStateController = buttonSelectionStateController; mIconPolicyLazy = iconPolicyLazy; mIconControllerLazy = iconControllerLazy; @@ -280,10 +277,9 @@ public class CarNavigationBar extends SystemUI implements CommandQueue.Callbacks * before and after the device is provisioned. . Also for change of density and font size. */ private void restartNavBars() { - // remove and reattach all hvac components such that we don't keep a reference to unused - // ui elements - mCarNavigationBarController.removeAllFromHvac(); - mButtonSelectionStateController.removeAll(); + // remove and reattach all components such that we don't keep a reference to unused ui + // elements + mCarNavigationBarController.removeAll(); if (mTopNavigationBarWindow != null) { mTopNavigationBarWindow.removeAllViews(); diff --git a/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationBarController.java b/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationBarController.java index 288e5cf13c2e7..ca780ae645c99 100644 --- a/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationBarController.java +++ b/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationBarController.java @@ -37,6 +37,7 @@ public class CarNavigationBarController { private final Context mContext; private final NavigationBarViewFactory mNavigationBarViewFactory; private final ButtonSelectionStateController mButtonSelectionStateController; + private final ButtonRoleHolderController mButtonRoleHolderController; private final Lazy mHvacControllerLazy; private boolean mShowTop; @@ -59,11 +60,13 @@ public class CarNavigationBarController { public CarNavigationBarController(Context context, NavigationBarViewFactory navigationBarViewFactory, ButtonSelectionStateController buttonSelectionStateController, - Lazy hvacControllerLazy) { + Lazy hvacControllerLazy, + ButtonRoleHolderController buttonRoleHolderController) { mContext = context; mNavigationBarViewFactory = navigationBarViewFactory; mButtonSelectionStateController = buttonSelectionStateController; mHvacControllerLazy = hvacControllerLazy; + mButtonRoleHolderController = buttonRoleHolderController; // Read configuration. mShowTop = mContext.getResources().getBoolean(R.bool.config_enableTopNavigationBar); @@ -101,9 +104,11 @@ public class CarNavigationBarController { mHvacControllerLazy.get().connectToCarService(); } - /** Clean up hvac. */ - public void removeAllFromHvac() { + /** Clean up */ + public void removeAll() { mHvacControllerLazy.get().removeAllComponents(); + mButtonSelectionStateController.removeAll(); + mButtonRoleHolderController.removeAll(); } /** Gets the top window if configured to do so. */ @@ -211,6 +216,7 @@ public class CarNavigationBarController { view.setStatusBarWindowTouchListener(statusBarTouchListener); view.setNotificationsPanelController(notifShadeController); mButtonSelectionStateController.addAllButtonsWithSelectionState(view); + mButtonRoleHolderController.addAllButtonsWithRoleName(view); mHvacControllerLazy.get().addTemperatureViewToController(view); } diff --git a/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationButton.java b/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationButton.java index 5f4ac2dcb141f..5e113d6366a14 100644 --- a/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationButton.java +++ b/packages/CarSystemUI/src/com/android/systemui/car/navigationbar/CarNavigationButton.java @@ -17,9 +17,11 @@ package com.android.systemui.car.navigationbar; import android.app.ActivityOptions; +import android.app.role.RoleManager; import android.content.Context; import android.content.Intent; import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; import android.os.Build; import android.os.UserHandle; import android.util.AttributeSet; @@ -29,6 +31,7 @@ import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; +import com.android.internal.annotations.VisibleForTesting; import com.android.keyguard.AlphaOptimizedImageButton; import com.android.systemui.R; @@ -62,6 +65,8 @@ public class CarNavigationButton extends LinearLayout { private float mUnselectedAlpha; private int mSelectedIconResourceId; private int mIconResourceId; + private Drawable mAppIcon; + private boolean mIsDefaultAppIconForRoleEnabled; private String[] mComponentNames; /** App categories that are to be used with this widget */ private String[] mButtonCategories; @@ -92,7 +97,9 @@ public class CarNavigationButton extends LinearLayout { super.setSelected(selected); mSelected = selected; if (mHighlightWhenSelected) { - setAlpha(mSelected ? mSelectedAlpha : mUnselectedAlpha); + // Always apply selected alpha if the button does not toggle alpha based on selection + // state. + setAlpha(!mHighlightWhenSelected || mSelected ? mSelectedAlpha : mUnselectedAlpha); } if (mShowMoreWhenSelected && mMoreIcon != null) { mMoreIcon.setVisibility(selected ? VISIBLE : GONE); @@ -108,6 +115,20 @@ public class CarNavigationButton extends LinearLayout { updateImage(); } + /** + * Sets the current icon of the default application associated with this button. + */ + public void setAppIcon(Drawable appIcon) { + mAppIcon = appIcon; + updateImage(); + } + + /** Gets the icon of the app currently associated to the role of this button. */ + @VisibleForTesting + protected Drawable getAppIcon() { + return mAppIcon; + } + /** Gets whether the icon is in an unseen state. */ public boolean getUnseen() { return mHasUnseen; @@ -143,6 +164,22 @@ public class CarNavigationButton extends LinearLayout { return mComponentNames; } + /** + * Subclasses should override this method to return the {@link RoleManager} role associated + * with this button. + */ + protected String getRoleName() { + return null; + } + + /** + * @return true if this button should show the icon of the default application for the + * role returned by {@link #getRoleName()}. + */ + protected boolean isDefaultAppIconForRoleEnabled() { + return mIsDefaultAppIconForRoleEnabled; + } + /** * @return The id of the display the button is on or Display.INVALID_DISPLAY if it's not yet on * a display. @@ -240,7 +277,6 @@ public class CarNavigationButton extends LinearLayout { }; } - /** * Initializes view-related aspects of the button. */ @@ -256,28 +292,27 @@ public class CarNavigationButton extends LinearLayout { R.styleable.CarNavigationButton_showMoreWhenSelected, mShowMoreWhenSelected); - mSelectedIconResourceId = typedArray.getResourceId( - R.styleable.CarNavigationButton_selectedIcon, mIconResourceId); mIconResourceId = typedArray.getResourceId( R.styleable.CarNavigationButton_icon, 0); - + mSelectedIconResourceId = typedArray.getResourceId( + R.styleable.CarNavigationButton_selectedIcon, mIconResourceId); + mIsDefaultAppIconForRoleEnabled = typedArray.getBoolean( + R.styleable.CarNavigationButton_useDefaultAppIconForRole, false); mIcon = findViewById(R.id.car_nav_button_icon_image); - mIcon.setScaleType(ImageView.ScaleType.CENTER); // Always apply selected alpha if the button does not toggle alpha based on selection state. mIcon.setAlpha(mHighlightWhenSelected ? mUnselectedAlpha : mSelectedAlpha); - mIcon.setImageResource(mIconResourceId); - mMoreIcon = findViewById(R.id.car_nav_button_more_icon); mMoreIcon.setAlpha(mSelectedAlpha); - mMoreIcon.setVisibility(GONE); - mUnseenIcon = findViewById(R.id.car_nav_button_unseen_icon); - - mUnseenIcon.setVisibility(mHasUnseen ? VISIBLE : GONE); + updateImage(); } private void updateImage() { - mIcon.setImageResource(mSelected ? mSelectedIconResourceId : mIconResourceId); + if (mIsDefaultAppIconForRoleEnabled && mAppIcon != null) { + mIcon.setImageDrawable(mAppIcon); + } else { + mIcon.setImageResource(mSelected ? mSelectedIconResourceId : mIconResourceId); + } mUnseenIcon.setVisibility(mHasUnseen ? VISIBLE : GONE); } diff --git a/packages/CarSystemUI/tests/res/layout/button_role_holder_controller_test.xml b/packages/CarSystemUI/tests/res/layout/button_role_holder_controller_test.xml new file mode 100644 index 0000000000000..25ec2c179c8c0 --- /dev/null +++ b/packages/CarSystemUI/tests/res/layout/button_role_holder_controller_test.xml @@ -0,0 +1,41 @@ + + + + + + + + + \ No newline at end of file diff --git a/packages/CarSystemUI/tests/res/layout/car_button_selection_state_controller_test.xml b/packages/CarSystemUI/tests/res/layout/car_button_selection_state_controller_test.xml index f0e02164a24bb..a8e83d6d7717a 100644 --- a/packages/CarSystemUI/tests/res/layout/car_button_selection_state_controller_test.xml +++ b/packages/CarSystemUI/tests/res/layout/car_button_selection_state_controller_test.xml @@ -25,7 +25,7 @@ android:paddingEnd="20dp" android:gravity="center"> - - - - - - + diff --git a/packages/CarSystemUI/tests/res/layout/car_navigation_button_test.xml b/packages/CarSystemUI/tests/res/layout/car_navigation_button_test.xml index 576928cda0891..44f8340403916 100644 --- a/packages/CarSystemUI/tests/res/layout/car_navigation_button_test.xml +++ b/packages/CarSystemUI/tests/res/layout/car_navigation_button_test.xml @@ -25,7 +25,7 @@ android:paddingEnd="20dp" android:gravity="center"> - - - - - - - - - + + \ No newline at end of file diff --git a/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/ButtonRoleHolderControllerTest.java b/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/ButtonRoleHolderControllerTest.java new file mode 100644 index 0000000000000..a57736bb3502c --- /dev/null +++ b/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/ButtonRoleHolderControllerTest.java @@ -0,0 +1,187 @@ +/* + * Copyright (C) 2020 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.navigationbar; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; + +import android.app.role.RoleManager; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.graphics.drawable.Drawable; +import android.os.UserHandle; +import android.testing.AndroidTestingRunner; +import android.testing.TestableLooper; +import android.view.LayoutInflater; +import android.widget.LinearLayout; + +import androidx.test.filters.SmallTest; + +import com.android.systemui.SysuiTestCase; +import com.android.systemui.car.CarDeviceProvisionedController; +import com.android.systemui.tests.R; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.List; + +@RunWith(AndroidTestingRunner.class) +@TestableLooper.RunWithLooper +@SmallTest +public class ButtonRoleHolderControllerTest extends SysuiTestCase { + private static final String TEST_VALID_PACKAGE_NAME = "foo"; + private static final String TEST_INVALID_PACKAGE_NAME = "bar"; + private static final UserHandle TEST_CURRENT_USER = UserHandle.of(100); + private static final UserHandle TEST_NON_CURRENT_USER = UserHandle.of(101); + + private LinearLayout mTestView; + private CarNavigationButton mNavButtonDefaultAppIconForRoleWithEnabled; + private CarNavigationButton mNavButtonDefaultAppIconForRoleWithDisabled; + private ButtonRoleHolderController mControllerUnderTest; + private Drawable mAppIcon; + + @Mock + private RoleManager mRoleManager; + @Mock + private CarDeviceProvisionedController mDeviceProvisionedController; + @Mock + private PackageManager mPackageManager; + @Mock + private ApplicationInfo mApplicationInfo; + + @Before + public void setUp() throws PackageManager.NameNotFoundException { + MockitoAnnotations.initMocks(this); + + mTestView = (LinearLayout) LayoutInflater.from(mContext).inflate( + R.layout.button_role_holder_controller_test, /* root= */ null); + mNavButtonDefaultAppIconForRoleWithEnabled = mTestView + .findViewById(R.id.assistant_role_button); + mNavButtonDefaultAppIconForRoleWithDisabled = mTestView + .findViewById(R.id.assistant_role_disabled_button); + mAppIcon = mContext.getDrawable(R.drawable.car_ic_apps); + when(mApplicationInfo.loadIcon(any())).thenReturn(mAppIcon); + doThrow(new PackageManager.NameNotFoundException()).when(mPackageManager) + .getApplicationInfo(any(), anyInt()); + doReturn(mApplicationInfo).when(mPackageManager) + .getApplicationInfo(eq(TEST_VALID_PACKAGE_NAME), anyInt()); + when(mDeviceProvisionedController + .getCurrentUser()) + .thenReturn(TEST_CURRENT_USER.getIdentifier()); + mControllerUnderTest = new ButtonRoleHolderController(mContext, + mPackageManager, mRoleManager, mDeviceProvisionedController); + } + + @Test + public void addAllButtonsWithRoleName_roleAssigned_appIconEnabled_useAssignedAppIcon() { + when(mRoleManager.getRoleHoldersAsUser(eq(RoleManager.ROLE_ASSISTANT), any())) + .thenReturn(List.of(TEST_VALID_PACKAGE_NAME)); + + mControllerUnderTest.addAllButtonsWithRoleName(mTestView); + + assertThat(mNavButtonDefaultAppIconForRoleWithEnabled.getAppIcon()).isEqualTo(mAppIcon); + } + + @Test + public void addAllButtonsWithRoleName_roleUnassigned_appIconEnabled_useDefaultIcon() { + when(mRoleManager.getRoleHoldersAsUser(eq(RoleManager.ROLE_ASSISTANT), any())) + .thenReturn(null); + + mControllerUnderTest.addAllButtonsWithRoleName(mTestView); + + assertThat(mNavButtonDefaultAppIconForRoleWithEnabled.getAppIcon()).isNull(); + } + + @Test + public void onRoleChanged_currentUser_appIconEnabled_useAssignedAppIcon() { + when(mRoleManager.getRoleHoldersAsUser(eq(RoleManager.ROLE_ASSISTANT), any())) + .thenReturn(null); + mControllerUnderTest.addAllButtonsWithRoleName(mTestView); + when(mRoleManager + .getRoleHoldersAsUser(eq(RoleManager.ROLE_ASSISTANT), any())) + .thenReturn(List.of(TEST_VALID_PACKAGE_NAME)); + + mControllerUnderTest.onRoleChanged(RoleManager.ROLE_ASSISTANT, TEST_CURRENT_USER); + + assertThat(mNavButtonDefaultAppIconForRoleWithEnabled.getAppIcon()).isEqualTo(mAppIcon); + } + + @Test + public void onRoleChanged_nonCurrentUser_appIconEnabled_iconIsNotUpdated() { + when(mRoleManager + .getRoleHoldersAsUser(eq(RoleManager.ROLE_ASSISTANT), any())) + .thenReturn(null); + mControllerUnderTest.addAllButtonsWithRoleName(mTestView); + Drawable beforeIcon = mNavButtonDefaultAppIconForRoleWithEnabled.getAppIcon(); + when(mRoleManager + .getRoleHoldersAsUser(eq(RoleManager.ROLE_ASSISTANT), any())) + .thenReturn(List.of(TEST_VALID_PACKAGE_NAME)); + + mControllerUnderTest.onRoleChanged(RoleManager.ROLE_ASSISTANT, TEST_NON_CURRENT_USER); + + Drawable afterIcon = mNavButtonDefaultAppIconForRoleWithEnabled.getAppIcon(); + assertThat(afterIcon).isEqualTo(beforeIcon); + } + + @Test + public void onRoleChanged_invalidPackage_useDefaultIcon() { + when(mRoleManager + .getRoleHoldersAsUser(eq(RoleManager.ROLE_ASSISTANT), any())) + .thenReturn(List.of(TEST_INVALID_PACKAGE_NAME)); + + mControllerUnderTest.addAllButtonsWithRoleName(mTestView); + + assertThat(mNavButtonDefaultAppIconForRoleWithEnabled.getAppIcon()).isNull(); + } + + @Test + public void addAllButtonsWithRoleName_appIconDisabled_useDefaultIcon() { + when(mRoleManager + .getRoleHoldersAsUser(eq(RoleManager.ROLE_ASSISTANT), any())) + .thenReturn(List.of(TEST_VALID_PACKAGE_NAME)); + + mControllerUnderTest.addAllButtonsWithRoleName(mTestView); + + assertThat(mNavButtonDefaultAppIconForRoleWithDisabled.getAppIcon()).isNull(); + } + + @Test + public void onRoleChanged_roleAssigned_appIconDisabled_useDefaultIcon() { + when(mRoleManager + .getRoleHoldersAsUser(eq(RoleManager.ROLE_ASSISTANT), any())) + .thenReturn(null); + mControllerUnderTest.addAllButtonsWithRoleName(mTestView); + assertThat(mNavButtonDefaultAppIconForRoleWithDisabled.getAppIcon()).isNull(); + when(mRoleManager + .getRoleHoldersAsUser(eq(RoleManager.ROLE_ASSISTANT), any())) + .thenReturn(List.of(TEST_VALID_PACKAGE_NAME)); + + mControllerUnderTest.onRoleChanged(RoleManager.ROLE_ASSISTANT, TEST_CURRENT_USER); + + assertThat(mNavButtonDefaultAppIconForRoleWithDisabled.getAppIcon()).isNull(); + } +} diff --git a/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationBarControllerTest.java b/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationBarControllerTest.java index 911f624d1fb35..e84e42c77245d 100644 --- a/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationBarControllerTest.java +++ b/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationBarControllerTest.java @@ -53,6 +53,8 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Mock private ButtonSelectionStateController mButtonSelectionStateController; @Mock + private ButtonRoleHolderController mButtonRoleHolderController; + @Mock private HvacController mHvacController; @Before @@ -66,10 +68,15 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { mDependency.injectMockDependency(StatusBarIconController.class); } + private CarNavigationBarController createNavigationBarController() { + return new CarNavigationBarController(mContext, mNavigationBarViewFactory, + mButtonSelectionStateController, () -> mHvacController, + mButtonRoleHolderController); + } + @Test public void testConnectToHvac_callsConnect() { - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); mCarNavigationBar.connectToHvac(); @@ -77,20 +84,37 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { } @Test - public void testRemoveAllFromHvac_callsRemoveAll() { - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + public void testRemoveAll_callsHvacControllerRemoveAllComponents() { + mCarNavigationBar = createNavigationBarController(); - mCarNavigationBar.removeAllFromHvac(); + mCarNavigationBar.removeAll(); verify(mHvacController).removeAllComponents(); } + + @Test + public void testRemoveAll_callsButtonRoleHolderControllerRemoveAll() { + mCarNavigationBar = createNavigationBarController(); + + mCarNavigationBar.removeAll(); + + verify(mButtonRoleHolderController).removeAll(); + } + + @Test + public void testRemoveAll_callsButtonSelectionStateControllerRemoveAll() { + mCarNavigationBar = createNavigationBarController(); + + mCarNavigationBar.removeAll(); + + verify(mButtonSelectionStateController).removeAll(); + } + @Test public void testGetTopWindow_topDisabled_returnsNull() { mTestableResources.addOverride(R.bool.config_enableTopNavigationBar, false); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getTopWindow(); @@ -100,8 +124,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testGetTopWindow_topEnabled_returnsWindow() { mTestableResources.addOverride(R.bool.config_enableTopNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getTopWindow(); @@ -111,8 +134,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testGetTopWindow_topEnabled_calledTwice_returnsSameWindow() { mTestableResources.addOverride(R.bool.config_enableTopNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window1 = mCarNavigationBar.getTopWindow(); ViewGroup window2 = mCarNavigationBar.getTopWindow(); @@ -123,8 +145,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testGetBottomWindow_bottomDisabled_returnsNull() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, false); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getBottomWindow(); @@ -134,8 +155,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testGetBottomWindow_bottomEnabled_returnsWindow() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getBottomWindow(); @@ -145,8 +165,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testGetBottomWindow_bottomEnabled_calledTwice_returnsSameWindow() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window1 = mCarNavigationBar.getBottomWindow(); ViewGroup window2 = mCarNavigationBar.getBottomWindow(); @@ -157,8 +176,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testGetLeftWindow_leftDisabled_returnsNull() { mTestableResources.addOverride(R.bool.config_enableLeftNavigationBar, false); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getLeftWindow(); assertThat(window).isNull(); } @@ -166,8 +184,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testGetLeftWindow_leftEnabled_returnsWindow() { mTestableResources.addOverride(R.bool.config_enableLeftNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getLeftWindow(); @@ -177,8 +194,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testGetLeftWindow_leftEnabled_calledTwice_returnsSameWindow() { mTestableResources.addOverride(R.bool.config_enableLeftNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window1 = mCarNavigationBar.getLeftWindow(); ViewGroup window2 = mCarNavigationBar.getLeftWindow(); @@ -189,8 +205,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testGetRightWindow_rightDisabled_returnsNull() { mTestableResources.addOverride(R.bool.config_enableRightNavigationBar, false); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getRightWindow(); @@ -200,8 +215,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testGetRightWindow_rightEnabled_returnsWindow() { mTestableResources.addOverride(R.bool.config_enableRightNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getRightWindow(); @@ -211,8 +225,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testGetRightWindow_rightEnabled_calledTwice_returnsSameWindow() { mTestableResources.addOverride(R.bool.config_enableRightNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window1 = mCarNavigationBar.getRightWindow(); ViewGroup window2 = mCarNavigationBar.getRightWindow(); @@ -223,8 +236,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testSetBottomWindowVisibility_setTrue_isVisible() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getBottomWindow(); mCarNavigationBar.setBottomWindowVisibility(View.VISIBLE); @@ -235,8 +247,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testSetBottomWindowVisibility_setFalse_isGone() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getBottomWindow(); mCarNavigationBar.setBottomWindowVisibility(View.GONE); @@ -247,8 +258,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testSetLeftWindowVisibility_setTrue_isVisible() { mTestableResources.addOverride(R.bool.config_enableLeftNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getLeftWindow(); mCarNavigationBar.setLeftWindowVisibility(View.VISIBLE); @@ -259,8 +269,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testSetLeftWindowVisibility_setFalse_isGone() { mTestableResources.addOverride(R.bool.config_enableLeftNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getLeftWindow(); mCarNavigationBar.setLeftWindowVisibility(View.GONE); @@ -271,8 +280,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testSetRightWindowVisibility_setTrue_isVisible() { mTestableResources.addOverride(R.bool.config_enableRightNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getRightWindow(); mCarNavigationBar.setRightWindowVisibility(View.VISIBLE); @@ -283,8 +291,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testSetRightWindowVisibility_setFalse_isGone() { mTestableResources.addOverride(R.bool.config_enableRightNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); ViewGroup window = mCarNavigationBar.getRightWindow(); mCarNavigationBar.setRightWindowVisibility(View.GONE); @@ -295,8 +302,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testRegisterBottomBarTouchListener_createViewFirst_registrationSuccessful() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); CarNavigationBarView bottomBar = mCarNavigationBar.getBottomBar(/* isSetUp= */ true); View.OnTouchListener controller = bottomBar.getStatusBarWindowTouchListener(); @@ -310,8 +316,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testRegisterBottomBarTouchListener_registerFirst_registrationSuccessful() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); mCarNavigationBar.registerBottomBarTouchListener(mock(View.OnTouchListener.class)); CarNavigationBarView bottomBar = mCarNavigationBar.getBottomBar(/* isSetUp= */ true); @@ -323,8 +328,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testRegisterNotificationController_createViewFirst_registrationSuccessful() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); CarNavigationBarView bottomBar = mCarNavigationBar.getBottomBar(/* isSetUp= */ true); CarNavigationBarController.NotificationsShadeController controller = @@ -340,8 +344,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testRegisterNotificationController_registerFirst_registrationSuccessful() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); mCarNavigationBar.registerNotificationController( mock(CarNavigationBarController.NotificationsShadeController.class)); @@ -355,8 +358,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testShowAllKeyguardButtons_bottomEnabled_bottomKeyguardButtonsVisible() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); CarNavigationBarView bottomBar = mCarNavigationBar.getBottomBar(/* isSetUp= */ true); View bottomKeyguardButtons = bottomBar.findViewById(R.id.lock_screen_nav_buttons); @@ -368,8 +370,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testShowAllKeyguardButtons_bottomEnabled_bottomNavButtonsGone() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); CarNavigationBarView bottomBar = mCarNavigationBar.getBottomBar(/* isSetUp= */ true); View bottomButtons = bottomBar.findViewById(R.id.nav_buttons); @@ -381,8 +382,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testHideAllKeyguardButtons_bottomEnabled_bottomKeyguardButtonsGone() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); CarNavigationBarView bottomBar = mCarNavigationBar.getBottomBar(/* isSetUp= */ true); View bottomKeyguardButtons = bottomBar.findViewById(R.id.lock_screen_nav_buttons); @@ -396,8 +396,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testHideAllKeyguardButtons_bottomEnabled_bottomNavButtonsVisible() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); CarNavigationBarView bottomBar = mCarNavigationBar.getBottomBar(/* isSetUp= */ true); View bottomButtons = bottomBar.findViewById(R.id.nav_buttons); @@ -411,8 +410,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testToggleAllNotificationsUnseenIndicator_bottomEnabled_hasUnseen_setCorrectly() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); CarNavigationBarView bottomBar = mCarNavigationBar.getBottomBar(/* isSetUp= */ true); CarNavigationButton notifications = bottomBar.findViewById(R.id.notifications); @@ -426,8 +424,7 @@ public class CarNavigationBarControllerTest extends SysuiTestCase { @Test public void testToggleAllNotificationsUnseenIndicator_bottomEnabled_noUnseen_setCorrectly() { mTestableResources.addOverride(R.bool.config_enableBottomNavigationBar, true); - mCarNavigationBar = new CarNavigationBarController(mContext, mNavigationBarViewFactory, - mButtonSelectionStateController, () -> mHvacController); + mCarNavigationBar = createNavigationBarController(); CarNavigationBarView bottomBar = mCarNavigationBar.getBottomBar(/* isSetUp= */ true); CarNavigationButton notifications = bottomBar.findViewById(R.id.notifications); diff --git a/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationBarTest.java b/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationBarTest.java index 04e0a7324adfd..0caa86f0eab28 100644 --- a/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationBarTest.java +++ b/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationBarTest.java @@ -89,6 +89,8 @@ public class CarNavigationBarTest extends SysuiTestCase { @Mock private ButtonSelectionStateListener mButtonSelectionStateListener; @Mock + private ButtonRoleHolderController mButtonRoleHolderController; + @Mock private IStatusBarService mBarService; @Mock private KeyguardStateController mKeyguardStateController; @@ -137,8 +139,8 @@ public class CarNavigationBarTest extends SysuiTestCase { mCarNavigationBarController, mLightBarController, mStatusBarIconController, mWindowManager, mDeviceProvisionedController, new CommandQueue(mContext), mAutoHideController, mButtonSelectionStateListener, mHandler, mUiBgExecutor, - mBarService, () -> mKeyguardStateController, mButtonSelectionStateController, - () -> mIconPolicy, () -> mIconController); + mBarService, () -> mKeyguardStateController, () -> mIconPolicy, + () -> mIconController); } @Test diff --git a/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationButtonTest.java b/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationButtonTest.java index 11f2fa48783f9..54282d39998b0 100644 --- a/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationButtonTest.java +++ b/packages/CarSystemUI/tests/src/com/android/systemui/car/navigationbar/CarNavigationButtonTest.java @@ -179,6 +179,56 @@ public class CarNavigationButtonTest extends SysuiTestCase { assertThat(moreIcon.getVisibility()).isEqualTo(View.GONE); } + @Test + public void onUnselected_withAppIcon_showsAppIcon() { + CarNavigationButton roleBasedButton = mTestView.findViewById(R.id.role_based_button); + Drawable appIcon = getContext().getDrawable(R.drawable.ic_android); + + roleBasedButton.setSelected(false); + roleBasedButton.setAppIcon(appIcon); + + Drawable currentDrawable = ((AlphaOptimizedImageButton) roleBasedButton.findViewById( + R.id.car_nav_button_icon_image)).getDrawable(); + + assertThat(currentDrawable).isEqualTo(appIcon); + } + + @Test + public void onUnselected_withAppIcon_applyUnselectedAlpha() { + CarNavigationButton roleBasedButton = mTestView.findViewById(R.id.role_based_button); + + roleBasedButton.setSelected(false); + roleBasedButton.setAppIcon(getContext().getDrawable(R.drawable.ic_android)); + + assertThat(roleBasedButton.getAlpha()).isEqualTo( + CarNavigationButton.DEFAULT_UNSELECTED_ALPHA); + } + + @Test + public void onSelected_withAppIcon_showsAppIconWithSelectedAlpha() { + CarNavigationButton roleBasedButton = mTestView.findViewById(R.id.role_based_button); + Drawable appIcon = getContext().getDrawable(R.drawable.ic_android); + + roleBasedButton.setSelected(true); + roleBasedButton.setAppIcon(appIcon); + + Drawable currentDrawable = ((AlphaOptimizedImageButton) roleBasedButton.findViewById( + R.id.car_nav_button_icon_image)).getDrawable(); + + assertThat(currentDrawable).isEqualTo(appIcon); + } + + @Test + public void onSelected_withAppIcon_applySelectedAlpha() { + CarNavigationButton roleBasedButton = mTestView.findViewById(R.id.role_based_button); + + roleBasedButton.setSelected(true); + roleBasedButton.setAppIcon(getContext().getDrawable(R.drawable.ic_android)); + + assertThat(roleBasedButton.getAlpha()).isEqualTo( + CarNavigationButton.DEFAULT_SELECTED_ALPHA); + } + @Test public void onClick_launchesIntentActivity() { mDefaultButton.performClick(); diff --git a/packages/SystemUI/src/com/android/systemui/dagger/SystemServicesModule.java b/packages/SystemUI/src/com/android/systemui/dagger/SystemServicesModule.java index 8daa770bcdff9..1e99a7b733e20 100644 --- a/packages/SystemUI/src/com/android/systemui/dagger/SystemServicesModule.java +++ b/packages/SystemUI/src/com/android/systemui/dagger/SystemServicesModule.java @@ -28,6 +28,7 @@ import android.app.KeyguardManager; import android.app.NotificationManager; import android.app.WallpaperManager; import android.app.admin.DevicePolicyManager; +import android.app.role.RoleManager; import android.app.trust.TrustManager; import android.content.ContentResolver; import android.content.Context; @@ -321,4 +322,9 @@ public class SystemServicesModule { return context.getSystemService(WindowManager.class); } + @Provides + @Singleton + static RoleManager provideRoleManager(Context context) { + return context.getSystemService(RoleManager.class); + } }