3/ Move wallpaper visibility listener to nav bar helper

- The bar transitions update based on the wallpaper visibility but this
  can be expensive to register/unregister frequently.  Instead, pull the
  listener out to NavBarHelper and keep it registered across the bars
  (it's a no-op for taskbar currently)
- We don't need the weak reference since nav bar helper is a singleton

Bug: 219035565
Test: atest NavBarHelperTests
Test: atest SystemUITests
Change-Id: I4b93437abd5e404981f119916557776090223cda
This commit is contained in:
Winson Chung
2023-03-09 17:34:58 +00:00
parent f07c2b4c12
commit 81d1f12f5b
5 changed files with 55 additions and 48 deletions

View File

@@ -47,6 +47,7 @@ import android.provider.Settings;
import android.provider.Settings.Secure;
import android.util.Log;
import android.view.IRotationWatcher;
import android.view.IWallpaperVisibilityListener;
import android.view.IWindowManager;
import android.view.View;
import android.view.WindowInsets;
@@ -119,6 +120,7 @@ public final class NavBarHelper implements
private int mA11yButtonState;
private int mRotationWatcherRotation;
private boolean mTogglingNavbarTaskbar;
private boolean mWallpaperVisible;
// Attributes used in NavBarHelper.CurrentSysuiState
private int mWindowStateDisplayId;
@@ -132,6 +134,19 @@ public final class NavBarHelper implements
}
};
// Listens for changes to the wallpaper visibility
private final IWallpaperVisibilityListener mWallpaperVisibilityListener =
new IWallpaperVisibilityListener.Stub() {
@Override
public void onWallpaperVisibilityChanged(boolean visible,
int displayId) throws RemoteException {
mHandler.post(() -> {
mWallpaperVisible = visible;
dispatchWallpaperVisibilityChanged(visible, displayId);
});
}
};
// Listens for changes to display rotation
private final IRotationWatcher mRotationWatcher = new IRotationWatcher.Stub() {
@Override
@@ -219,6 +234,14 @@ public final class NavBarHelper implements
} catch (Exception e) {
Log.w(TAG, "Failed to register rotation watcher", e);
}
// Setup wallpaper visibility listener
try {
mWallpaperVisible = mWm.registerWallpaperVisibilityListener(
mWallpaperVisibilityListener, mDefaultDisplayId);
} catch (Exception e) {
Log.w(TAG, "Failed to register wallpaper visibility listener", e);
}
}
/**
@@ -239,6 +262,14 @@ public final class NavBarHelper implements
} catch (Exception e) {
Log.w(TAG, "Failed to unregister rotation watcher", e);
}
// Clean up wallpaper visibility listener
try {
mWm.unregisterWallpaperVisibilityListener(mWallpaperVisibilityListener,
mDefaultDisplayId);
} catch (Exception e) {
Log.w(TAG, "Failed to register wallpaper visibility listener", e);
}
}
/**
@@ -258,6 +289,7 @@ public final class NavBarHelper implements
listener.updateAccessibilityServicesState();
listener.updateAssistantAvailable(mAssistantAvailable, mLongPressHomeEnabled);
}
listener.updateWallpaperVisibility(mWallpaperVisible, mDefaultDisplayId);
listener.updateRotationWatcherState(mRotationWatcherRotation);
}
@@ -435,6 +467,12 @@ public final class NavBarHelper implements
mWindowState = state;
}
private void dispatchWallpaperVisibilityChanged(boolean visible, int displayId) {
for (NavbarTaskbarStateUpdater listener : mStateListeners) {
listener.updateWallpaperVisibility(visible, displayId);
}
}
private void dispatchRotationChanged(int rotation) {
for (NavbarTaskbarStateUpdater listener : mStateListeners) {
listener.updateRotationWatcherState(rotation);
@@ -452,6 +490,7 @@ public final class NavBarHelper implements
public interface NavbarTaskbarStateUpdater {
void updateAccessibilityServicesState();
void updateAssistantAvailable(boolean available, boolean longPressHomeEnabled);
default void updateWallpaperVisibility(boolean visible, int displayId) {}
default void updateRotationWatcherState(int rotation) {}
}

View File

@@ -164,7 +164,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import javax.inject.Inject;
@@ -350,6 +349,12 @@ public class NavigationBar extends ViewController<NavigationBarView> implements
mLongPressHomeEnabled = longPressHomeEnabled;
updateAssistantEntrypoints(available, longPressHomeEnabled);
}
@Override
public void updateWallpaperVisibility(boolean visible, int displayId) {
mNavigationBarTransitions.setWallpaperVisibility(visible);
}
@Override
public void updateRotationWatcherState(int rotation) {
if (mIsOnDefaultDisplay && mView != null) {

View File

@@ -21,11 +21,7 @@ import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON;
import static com.android.systemui.util.Utils.isGesturalModeOnDefaultDisplay;
import android.graphics.Rect;
import android.os.Handler;
import android.os.RemoteException;
import android.util.SparseArray;
import android.view.IWallpaperVisibilityListener;
import android.view.IWindowManager;
import android.view.View;
import com.android.systemui.R;
@@ -36,7 +32,6 @@ import com.android.systemui.statusbar.phone.BarTransitions;
import com.android.systemui.statusbar.phone.LightBarTransitionsController;
import java.io.PrintWriter;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
@@ -63,8 +58,6 @@ public final class NavigationBarTransitions extends BarTransitions implements
}
private final NavigationBarView mView;
@org.jetbrains.annotations.NotNull
private final IWindowManager mWindowManagerService;
private final LightBarTransitionsController mLightTransitionsController;
private final DisplayTracker mDisplayTracker;
private final boolean mAllowAutoDimWallpaperNotVisible;
@@ -76,51 +69,20 @@ public final class NavigationBarTransitions extends BarTransitions implements
private int mNavBarMode = NAV_BAR_MODE_3BUTTON;
private List<DarkIntensityListener> mDarkIntensityListeners;
private final Handler mHandler = Handler.getMain();
static final class WallpaperVisibilityListener extends IWallpaperVisibilityListener.Stub {
private final WeakReference<NavigationBarTransitions> mSelf;
WallpaperVisibilityListener(NavigationBarTransitions self) {
mSelf = new WeakReference<>(self);
}
@Override
public void onWallpaperVisibilityChanged(boolean newVisibility,
int displayId) throws RemoteException {
NavigationBarTransitions self = mSelf.get();
if (self == null) {
return;
}
self.mWallpaperVisible = newVisibility;
self.mHandler.post(() -> self.applyLightsOut(true, false));
}
}
private final IWallpaperVisibilityListener mWallpaperVisibilityListener;
@Inject
public NavigationBarTransitions(
NavigationBarView view,
IWindowManager windowManagerService,
LightBarTransitionsController.Factory lightBarTransitionsControllerFactory,
DisplayTracker displayTracker) {
super(view, R.drawable.nav_background);
mView = view;
mWindowManagerService = windowManagerService;
mLightTransitionsController = lightBarTransitionsControllerFactory.create(this);
mDisplayTracker = displayTracker;
mAllowAutoDimWallpaperNotVisible = view.getContext().getResources()
.getBoolean(R.bool.config_navigation_bar_enable_auto_dim_no_visible_wallpaper);
mDarkIntensityListeners = new ArrayList();
mWallpaperVisibilityListener = new WallpaperVisibilityListener(this);
try {
mWallpaperVisible = mWindowManagerService.registerWallpaperVisibilityListener(
mWallpaperVisibilityListener, mDisplayTracker.getDefaultDisplayId());
} catch (RemoteException e) {
}
mView.addOnLayoutChangeListener(
(v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
View currentView = mView.getCurrentView();
@@ -142,14 +104,14 @@ public final class NavigationBarTransitions extends BarTransitions implements
@Override
public void destroy() {
try {
mWindowManagerService.unregisterWallpaperVisibilityListener(mWallpaperVisibilityListener,
mDisplayTracker.getDefaultDisplayId());
} catch (RemoteException e) {
}
mLightTransitionsController.destroy();
}
void setWallpaperVisibility(boolean visible) {
mWallpaperVisible = visible;
applyLightsOut(true, false);
}
@Override
public void setAutoDim(boolean autoDim) {
// Ensure we aren't in gestural nav if we are triggering auto dim

View File

@@ -150,6 +150,7 @@ public class NavBarHelperTest extends SysuiTestCase {
mNavBarHelper);
verify(mAssistManager, times(1)).getAssistInfoForUser(anyInt());
verify(mWm, times(1)).watchRotation(any(), anyInt());
verify(mWm, times(1)).registerWallpaperVisibilityListener(any(), anyInt());
}
@Test
@@ -161,6 +162,7 @@ public class NavBarHelperTest extends SysuiTestCase {
verify(mAccessibilityManager, times(1)).removeAccessibilityServicesStateChangeListener(
mNavBarHelper);
verify(mWm, times(1)).removeRotationWatcher(any());
verify(mWm, times(1)).unregisterWallpaperVisibilityListener(any(), anyInt());
}
@Test
@@ -183,6 +185,8 @@ public class NavBarHelperTest extends SysuiTestCase {
.updateAssistantAvailable(anyBoolean(), anyBoolean());
verify(mNavbarTaskbarStateUpdater, times(1))
.updateRotationWatcherState(anyInt());
verify(mNavbarTaskbarStateUpdater, times(1))
.updateWallpaperVisibility(anyBoolean(), anyInt());
}
@Test

View File

@@ -27,7 +27,6 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper.RunWithLooper;
import android.view.IWindowManager;
import androidx.test.filters.SmallTest;
@@ -60,8 +59,6 @@ public class NavigationBarTransitionsTest extends SysuiTestCase {
EdgeBackGestureHandler.Factory mEdgeBackGestureHandlerFactory;
@Mock
EdgeBackGestureHandler mEdgeBackGestureHandler;
@Mock
IWindowManager mIWindowManager;
private NavigationBarTransitions mTransitions;
private final FakeDisplayTracker mDisplayTracker = new FakeDisplayTracker(mContext);
@@ -88,7 +85,7 @@ public class NavigationBarTransitionsTest extends SysuiTestCase {
when(navBar.getCurrentView()).thenReturn(navBar);
when(navBar.findViewById(anyInt())).thenReturn(navBar);
mTransitions = new NavigationBarTransitions(
navBar, mIWindowManager, mLightBarTransitionsFactory, mDisplayTracker);
navBar, mLightBarTransitionsFactory, mDisplayTracker);
}
@Test