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

Commit fa6d0031 authored by Jay Thomas Sullivan's avatar Jay Thomas Sullivan
Browse files

[Safety Labels] Filter out auto, wear and tv

This feature should never be enabled on these form factors.

Bug: 277601279
Bug: 264939792
Test: atest CtsPermission3TestCases:SafetyLabelChangesJobServiceTest
Test: atest CtsPermission3TestCases:AppDataSharingUpdatesTest
Change-Id: I89f0443c52cfc0894f2ad5398a15f2c0e1a9ad77
parent f98cfff4
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settings.privacy;
import static android.safetylabel.SafetyLabelConstants.SAFETY_LABEL_CHANGE_NOTIFICATIONS_ENABLED;

import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.DeviceConfig;

import com.android.settings.core.BasePreferenceController;
@@ -28,7 +29,6 @@ import com.android.settings.core.BasePreferenceController;
 * TODO b/264939792: Add tests
 */
public class AppDataSharingUpdatesPreferenceController extends BasePreferenceController {

    public AppDataSharingUpdatesPreferenceController(Context context,
            String preferenceKey) {
        super(context, preferenceKey);
@@ -36,8 +36,16 @@ public class AppDataSharingUpdatesPreferenceController extends BasePreferenceCon

    @Override
    public int getAvailabilityStatus() {
        return isPrivacySafetyLabelChangeNotificationsEnabled(mContext)
                ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
    }

    private boolean isPrivacySafetyLabelChangeNotificationsEnabled(Context context) {
        PackageManager packageManager = context.getPackageManager();
        return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY,
                    SAFETY_LABEL_CHANGE_NOTIFICATIONS_ENABLED, false)
                ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
                && !packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)
                && !packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK)
                && !packageManager.hasSystemFeature(PackageManager.FEATURE_WATCH);
    }
}
+116 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.settings.privacy;

import static android.content.pm.PackageManager.FEATURE_AUTOMOTIVE;
import static android.content.pm.PackageManager.FEATURE_LEANBACK;
import static android.content.pm.PackageManager.FEATURE_WATCH;

import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;

import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.DeviceConfig;
import android.provider.Settings;
import android.safetylabel.SafetyLabelConstants;

import com.android.settings.testutils.shadow.ShadowDeviceConfig;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoSession;
import org.mockito.quality.Strictness;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.Arrays;
import java.util.List;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowDeviceConfig.class})
public class AppDataSharingUpdatesPreferenceControllerTest {

    public static final String PREFERENCE_KEY = "PREFERENCE_KEY";
    private static final List<String> sUnsupportedFormFactors =
            Arrays.asList(FEATURE_AUTOMOTIVE, FEATURE_LEANBACK, FEATURE_WATCH);
    private MockitoSession mMockitoSession;
    @Mock
    private PackageManager mPackageManager;
    @Mock
    private Context mContext;
    private AppDataSharingUpdatesPreferenceController mController;

    @Before
    public void setUp() {
        mMockitoSession = Mockito.mockitoSession()
                .initMocks(this)
                .strictness(Strictness.WARN)
                .startMocking();
        doReturn(mPackageManager).when(mContext).getPackageManager();
        mController = new AppDataSharingUpdatesPreferenceController(mContext, PREFERENCE_KEY);
        for (String formFactor : sUnsupportedFormFactors) {
            doReturn(false).when(mPackageManager).hasSystemFeature(eq(formFactor));
        }
    }

    @After
    public void tearDown() {
        mMockitoSession.finishMocking();
    }

    @Test
    public void whenSafetyLabelsDisabled_thenPreferenceUnavailable()
            throws Exception {
        setSafetyLabelsDeviceConfigEnabled(false);
        assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
    }

    @Test
    public void whenSafetyLabelsEnabled_andSupportedFormFactor_thenPreferenceAvailable()
            throws Exception {
        setSafetyLabelsDeviceConfigEnabled(true);
        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }

    @Test
    public void whenSafetyLabelsEnabled_andUnsupportedFormFactor_thenPreferenceUnavailable()
            throws Exception {
        setSafetyLabelsDeviceConfigEnabled(true);
        for (String formFactor : sUnsupportedFormFactors) {
            doReturn(true).when(mPackageManager).hasSystemFeature(eq(formFactor));
            assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
            doReturn(false).when(mPackageManager).hasSystemFeature(eq(formFactor));
        }
    }

    private void setSafetyLabelsDeviceConfigEnabled(boolean newValue)
            throws Settings.SettingNotFoundException {
        DeviceConfig.setProperty(DeviceConfig.NAMESPACE_PRIVACY,
                SafetyLabelConstants.SAFETY_LABEL_CHANGE_NOTIFICATIONS_ENABLED,
                ((Boolean) newValue).toString(), /* makeDefault */ false);
    }
}