Move HideNonSystemOverlayMixin to SettingsLib

To be re-used by settings applications for other form factors.

Bug: 184967544
Test: make RunSettingsLibRoboTests -j40
ROBOTEST_FILTER=com.android.settingslib.core.lifecycle

Change-Id: I749dbd3f24bf77453d3ecc5d4374d11656498850
This commit is contained in:
Alex Stetson
2021-05-06 08:22:12 -07:00
parent 969a1a8643
commit c3f60fb944
2 changed files with 178 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
/*
* 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.settingslib.core.lifecycle;
import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
import static androidx.lifecycle.Lifecycle.Event.ON_START;
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
import android.app.Activity;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
/**
* A mixin that adds window flag to prevent non-system overlays showing on top of Settings
* activities.
*/
public class HideNonSystemOverlayMixin implements LifecycleObserver {
public static final String SECURE_OVERLAY_SETTINGS = "secure_overlay_settings";
private final Activity mActivity;
public HideNonSystemOverlayMixin(Activity activity) {
mActivity = activity;
}
@VisibleForTesting
boolean isEnabled() {
return Settings.Secure.getInt(mActivity.getContentResolver(),
SECURE_OVERLAY_SETTINGS, 0 /* defValue */) == 0;
}
/**
* Start Lifecycle event
*/
@OnLifecycleEvent(ON_START)
public void onStart() {
if (mActivity == null || !isEnabled()) {
return;
}
mActivity.getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
android.util.EventLog.writeEvent(0x534e4554, "120484087", -1, "");
}
/**
* Stop Lifecycle event
*/
@OnLifecycleEvent(ON_STOP)
public void onStop() {
if (mActivity == null || !isEnabled()) {
return;
}
final Window window = mActivity.getWindow();
final WindowManager.LayoutParams attrs = window.getAttributes();
attrs.privateFlags &= ~SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
window.setAttributes(attrs);
}
}

View File

@@ -0,0 +1,100 @@
/*
* 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.settingslib.core.lifecycle;
import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
import static com.android.settingslib.core.lifecycle.HideNonSystemOverlayMixin.SECURE_OVERLAY_SETTINGS;
import static com.google.common.truth.Truth.assertThat;
import android.os.Bundle;
import android.provider.Settings;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.android.settingslib.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.android.controller.ActivityController;
@RunWith(RobolectricTestRunner.class)
public class HideNonSystemOverlayMixinTest {
private ActivityController<TestActivity> mActivityController;
@Before
public void setUp() {
mActivityController = Robolectric.buildActivity(TestActivity.class);
}
@Test
public void startActivity_shouldHideNonSystemOverlay() {
mActivityController.setup();
TestActivity activity = mActivityController.get();
// Activity start: HIDE_NON_SYSTEM_OVERLAY should be set.
final WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
assertThat(attrs.privateFlags & SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS)
.isNotEqualTo(0);
}
@Test
public void stopActivity_shouldUnhideNonSystemOverlay() {
mActivityController.setup().stop();
TestActivity activity = mActivityController.get();
final WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
assertThat(attrs.privateFlags & SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS)
.isEqualTo(0);
}
@Test
public void isEnabled_isAllowedOverlaySettings_returnFalse() {
mActivityController.setup();
final TestActivity activity = mActivityController.get();
Settings.Secure.putInt(activity.getContentResolver(),
SECURE_OVERLAY_SETTINGS, 1);
assertThat(new HideNonSystemOverlayMixin(activity).isEnabled()).isFalse();
}
@Test
public void isEnabled_isNotAllowedOverlaySettings_returnTrue() {
mActivityController.setup();
TestActivity activity = mActivityController.get();
Settings.Secure.putInt(activity.getContentResolver(),
SECURE_OVERLAY_SETTINGS, 0);
assertThat(new HideNonSystemOverlayMixin(activity).isEnabled()).isTrue();
}
public static class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_AppCompat);
getLifecycle().addObserver(new HideNonSystemOverlayMixin(this));
}
}
}