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

Commit a947e88c authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Clean up unused AppPermissionsPreferenceController

This one is replaced by AppPermissionPreferenceController.

Bug: 234570979
Test: m Settings
Change-Id: Id9d16caf33cdea626f79a3cd142dfcde2c54769f
parent 88f9c8fe
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -8832,10 +8832,6 @@
    <!-- Label for list that shows all permissions -->
    <string name="app_permissions">Permission manager</string>
    <!-- Summary of permissions currently granted to apps [CHAR LIMIT=60] -->
    <string name="app_permissions_summary">Apps using <xliff:g id="apps" example="location">%1$s</xliff:g></string>
    <!-- Summary of permissions currently granted to apps [CHAR LIMIT=60] -->
    <string name="app_permissions_summary_more">Apps using <xliff:g id="apps" example="location">%1$s</xliff:g>, and more</string>
    <!-- Label for tap to wake setting [CHAR LIMIT=30] -->
    <string name="tap_to_wake">Tap to wake</string>
+0 −122
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.applications;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.icu.text.ListFormatter;
import android.util.ArraySet;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;

import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.applications.PermissionsSummaryHelper;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class AppPermissionsPreferenceController extends BasePreferenceController {

    private static final String TAG = "AppPermissionPrefCtrl";
    private static final int NUM_PACKAGE_TO_CHECK = 4;

    @VisibleForTesting
    static int NUM_PERMISSIONS_TO_SHOW = 3;

    private final PackageManager mPackageManager;
    private final Set<CharSequence> mPermissionGroups;

    private final PermissionsSummaryHelper.PermissionsResultCallback mPermissionsCallback =
            new PermissionsSummaryHelper.PermissionsResultCallback() {
                @Override
                public void onPermissionSummaryResult(int standardGrantedPermissionCount,
                        int requestedPermissionCount, int additionalGrantedPermissionCount,
                        List<CharSequence> grantedGroupLabels) {
                    updateSummary(grantedGroupLabels);
                }
            };

    @VisibleForTesting
    int mNumPackageChecked;

    private Preference mPreference;

    public AppPermissionsPreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
        mPackageManager = context.getPackageManager();
        mPermissionGroups = new ArraySet<>();
    }

    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }

    @Override
    public void updateState(Preference preference) {
        mPreference = preference;
        mNumPackageChecked = 0;
        queryPermissionSummary();
    }

    @VisibleForTesting
    void queryPermissionSummary() {
        final List<PackageInfo> installedPackages =
                mPackageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
        // Here we only get the first four apps and check their permissions.
        final List<PackageInfo> packagesWithPermission = installedPackages.stream()
                .filter(pInfo -> pInfo.permissions != null)
                .limit(NUM_PACKAGE_TO_CHECK)
                .collect(Collectors.toList());

        for (PackageInfo installedPackage : packagesWithPermission) {
            PermissionsSummaryHelper.getPermissionSummary(mContext,
                    installedPackage.packageName, mPermissionsCallback);
        }
    }

    @VisibleForTesting
    void updateSummary(List<CharSequence> grantedGroupLabels) {
        mPermissionGroups.addAll(grantedGroupLabels);
        mNumPackageChecked++;

        if (mNumPackageChecked < NUM_PACKAGE_TO_CHECK) {
            return;
        }

        final List<CharSequence> permissionsToShow = mPermissionGroups.stream()
                .limit(NUM_PERMISSIONS_TO_SHOW)
                .collect(Collectors.toList());
        final boolean isMoreShowed = mPermissionGroups.size() > NUM_PERMISSIONS_TO_SHOW;
        CharSequence summary;

        if (!permissionsToShow.isEmpty()) {
            if (isMoreShowed) {
                summary = mContext.getString(R.string.app_permissions_summary_more,
                        ListFormatter.getInstance().format(permissionsToShow).toLowerCase());
            } else {
                summary = mContext.getString(R.string.app_permissions_summary,
                        ListFormatter.getInstance().format(permissionsToShow).toLowerCase());
            }
        } else {
            summary = mContext.getString(
                    R.string.runtime_permissions_summary_no_permissions_granted);
        }
        mPreference.setSummary(summary);
    }
}
 No newline at end of file
+0 −146
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.applications;

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

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;

import androidx.preference.Preference;

import com.android.settings.R;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

import java.util.ArrayList;
import java.util.List;

@RunWith(RobolectricTestRunner.class)
public class AppPermissionsPreferenceControllerTest {

    private Context mContext;
    private AppPermissionsPreferenceController mController;
    private Preference mPreference;

    @Before
    public void setUp() throws NameNotFoundException {
        mContext = RuntimeEnvironment.application;
        mPreference = spy(new Preference(mContext));
        mController = spy(new AppPermissionsPreferenceController(mContext, "pref_key"));
    }

    @Test
    public void isAvailable_shouldAlwaysReturnTrue() {
        assertThat(mController.isAvailable()).isTrue();
    }

    @Test
    public void updateState_shouldResetNumPackageChecked() {
        doNothing().when(mController).queryPermissionSummary();
        mController.mNumPackageChecked = 3;

        mController.updateState(mPreference);

        assertThat(mController.mNumPackageChecked).isEqualTo(0);
    }

    @Test
    public void updateSummary_noGrantedPermission_shouldSetNoPermissionGrantedSummary() {
        doNothing().when(mController).queryPermissionSummary();
        mController.updateState(mPreference);
        mController.mNumPackageChecked = 3;

        mController.updateSummary(new ArrayList<>());

        assertThat(mPreference.getSummary()).isEqualTo(
                mContext.getString(R.string.runtime_permissions_summary_no_permissions_granted));
    }

    @Test
    public void updateSummary_hasOnePermission_shouldSetPermissionAsSummary() {
        doNothing().when(mController).queryPermissionSummary();
        mController.updateState(mPreference);
        final String permission = "location";
        final ArrayList<CharSequence> labels = new ArrayList<>();
        labels.add(permission);
        final String summary = "Apps using " + permission;
        mController.mNumPackageChecked = 3;

        mController.updateSummary(labels);

        assertThat(mPreference.getSummary()).isEqualTo(summary);
    }

    @Test
    public void updateSummary_hasThreePermissions_shouldShowThreePermissionAsSummary() {
        doNothing().when(mController).queryPermissionSummary();
        mController.updateState(mPreference);
        mController.mNumPackageChecked = 3;
        final List<CharSequence> labels = new ArrayList<>();
        labels.add("Phone");
        labels.add("SMS");
        labels.add("Microphone");

        mController.updateSummary(labels);

        final String summary = "Apps using microphone, sms, and phone";
        assertThat(mPreference.getSummary()).isEqualTo(summary);
    }

    @Test
    public void updateSummary_hasFivePermissions_shouldShowThreePermissionsAndMoreAsSummary() {
        doNothing().when(mController).queryPermissionSummary();
        mController.updateState(mPreference);
        mController.mNumPackageChecked = 3;
        final List<CharSequence> labels = new ArrayList<>();
        labels.add("Phone");
        labels.add("SMS");
        labels.add("Microphone");
        labels.add("Contacts");
        labels.add("Camera");
        labels.add("Location");

        mController.updateSummary(labels);

        final String summary = "Apps using microphone, contacts, and sms, and more";
        assertThat(mPreference.getSummary()).isEqualTo(summary);
    }

    @Test
    public void updateSummary_notReachCallbackCount_shouldNotSetSummary() {
        doNothing().when(mController).queryPermissionSummary();
        mController.updateState(mPreference);
        final String permission = "location";
        final ArrayList<CharSequence> labels = new ArrayList<>();
        labels.add(permission);

        mController.updateSummary(labels);

        verify(mPreference, never()).setSummary(anyString());
    }
}