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

Commit e33639f1 authored by Lucas Silva's avatar Lucas Silva
Browse files

Only show communal settings to the primary user

Fixes: 264284347
Test: flashed device, and verified settings don't appear for secondary
user. Setting still appears for primary user
Test: atest CommunalPreferenceControllerTest

Change-Id: I6686ce4dbc7d708a93131620ae93c5e0dc323995
parent 9f8b28ea
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.communal;

import android.content.Context;
import android.os.UserManager;

import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
@@ -31,7 +32,14 @@ public class CommunalPreferenceController extends BasePreferenceController {

    @Override
    public int getAvailabilityStatus() {
        return mContext.getResources().getBoolean(R.bool.config_show_communal_settings)
        // TODO(b/257333623): Allow the communal user to be non-SystemUser user in HSUM.
        return (mContext.getResources().getBoolean(R.bool.config_show_communal_settings)
                && isSystemUser())
                ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
    }

    private boolean isSystemUser() {
        final UserManager userManager = mContext.getSystemService(UserManager.class);
        return userManager != null && userManager.isSystemUser();
    }
}
+88 −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.communal;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.res.Resources;
import android.os.UserManager;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.android.settings.testutils.ResourcesUtils;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@RunWith(AndroidJUnit4.class)
public class CommunalPreferenceControllerTest {
    @Mock
    private UserManager mUserManager;

    private Resources mResources;
    private CommunalPreferenceController mController;

    private static final String PREF_KEY = "test_key";

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        final Context context = spy(ApplicationProvider.getApplicationContext());
        mResources = spy(context.getResources());

        mController = new CommunalPreferenceController(context, PREF_KEY);

        when(context.getResources()).thenReturn(mResources);
        when(context.getSystemService(UserManager.class)).thenReturn(mUserManager);
    }

    @Test
    public void isAvailable_communalEnabled_shouldBeTrueForSystemUser() {
        setCommunalEnabled(true);
        when(mUserManager.isSystemUser()).thenReturn(true);
        assertTrue(mController.isAvailable());
    }

    @Test
    public void isAvailable_communalEnabled_shouldBeFalseForNonSystemUser() {
        setCommunalEnabled(true);
        when(mUserManager.isSystemUser()).thenReturn(false);
        assertFalse(mController.isAvailable());
    }

    @Test
    public void isAvailable_communalDisabled_shouldBeFalseForSystemUser() {
        setCommunalEnabled(false);
        when(mUserManager.isSystemUser()).thenReturn(true);
        assertFalse(mController.isAvailable());
    }

    private void setCommunalEnabled(boolean enabled) {
        final int boolId = ResourcesUtils.getResourcesId(
                ApplicationProvider.getApplicationContext(), "bool",
                "config_show_communal_settings");
        when(mResources.getBoolean(boolId)).thenReturn(enabled);
    }
}