Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a4512eea authored by Alex Stetson's avatar Alex Stetson Committed by Automerger Merge Worker
Browse files

Merge "Move HideNonSystemOverlayMixin to SettingsLib" into sc-dev am: 74cf8e01

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14453200

Change-Id: I3d6f6e152eed36904571e70f4e66841c211f69db
parents af37a00f 74cf8e01
Loading
Loading
Loading
Loading
+78 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+100 −0
Original line number Diff line number Diff line
/*
 * 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));
        }
    }
}