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

Commit a2c2e691 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Clean up and rename PermissionUsagePreference."

parents facb9502 7e1956dc
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -228,7 +228,7 @@ public class AppPermissionUsageFragment extends SettingsWithHeader {
                        + historyEntry.getBackgroundAccessDuration();
            }

            Preference pref = new PermissionUsagePreference(context, group, null, false);
            Preference pref = new PermissionControlPreference(context, group);
            pref.setTitle(usage.getPermissionGroupLabel());
            long timeDiff = System.currentTimeMillis() - usage.getTime();
            if (totalDuration == 0) {
+4 −7
Original line number Diff line number Diff line
@@ -216,7 +216,8 @@ public final class AppPermissionsFragment extends SettingsWithHeader {

            boolean isPlatform = group.getDeclaringPackage().equals(Utils.OS_PKG);

            Preference preference = new PermissionUsagePreference(context, group, null, false);
            PermissionControlPreference preference = new PermissionControlPreference(context,
                    group);
            preference.setKey(group.getName());
            Drawable icon = Utils.loadDrawable(context.getPackageManager(),
                    group.getIconPkg(), group.getIconResId());
@@ -229,12 +230,8 @@ public final class AppPermissionsFragment extends SettingsWithHeader {
                preference.setSummary(
                        context.getString(R.string.app_permission_most_recent_summary,
                                timeDiffStr));
            } else if (group.hasPermissionWithBackgroundMode()
                    && group.areRuntimePermissionsGranted()) {
                AppPermissionGroup backgroundGroup = group.getBackgroundPermissions();
                if (backgroundGroup == null || !backgroundGroup.areRuntimePermissionsGranted()) {
                    preference.setSummary(R.string.permission_access_only_foreground);
                }
            } else {
                preference.setGroupSummary(group);
            }

            if (isPlatform) {
+6 −15
Original line number Diff line number Diff line
@@ -259,19 +259,21 @@ public final class PermissionAppsFragment extends PermissionsFrameFragment imple
            PreferenceCategory category = app.areRuntimePermissionsGranted() ? allowed : denied;

            if (existingPref != null) {
                setSummary(existingPref, group);
                if (existingPref instanceof PermissionControlPreference) {
                    ((PermissionControlPreference) existingPref).setGroupSummary(group);
                }
                category.addPreference(existingPref);
                continue;
            }

            PermissionUsagePreference pref = new PermissionUsagePreference(context, group, null,
                    true);
            PermissionControlPreference pref = new PermissionControlPreference(context, group);
            pref.setKey(key);
            pref.setIcon(app.getIcon());
            pref.setTitle(app.getAppInfo().loadSafeLabel(context.getPackageManager(), 0,
                    TextUtils.SAFE_STRING_FLAG_TRIM | TextUtils.SAFE_STRING_FLAG_FIRST_LINE));
            pref.setEllipsizeEnd();
            setSummary(pref, group);
            pref.useSmallerIcon();
            pref.setGroupSummary(group);

            if (isSystemApp && isTelevision) {
                if (mExtraScreen == null) {
@@ -335,17 +337,6 @@ public final class PermissionAppsFragment extends PermissionsFrameFragment imple
        }
    }

    private void setSummary(Preference pref, AppPermissionGroup group) {
        if (group.hasPermissionWithBackgroundMode() && group.areRuntimePermissionsGranted()) {
            AppPermissionGroup backgroundGroup = group.getBackgroundPermissions();
            if (backgroundGroup == null || !backgroundGroup.areRuntimePermissionsGranted()) {
                pref.setSummary(R.string.permission_access_only_foreground);
                return;
            }
        }
        pref.setSummary("");
    }

    public static class SystemAppsFragment extends PermissionsFrameFragment implements Callback {
        PermissionAppsFragment mOuterFragment;

+48 −13
Original line number Diff line number Diff line
@@ -34,36 +34,55 @@ import com.android.packageinstaller.permission.ui.AppPermissionActivity;
import com.android.permissioncontroller.R;

/**
 * A preference for representing a permission usage by an app.
 * A preference that links to the screen where a permission can be toggled.
 */
public class PermissionUsagePreference extends Preference {
    private final @NonNull AppPermissionGroup mGroup;
    private final @Nullable Drawable mWidgetIcon;
public class PermissionControlPreference extends Preference {
    private final @NonNull Context mContext;
    private final boolean mUseSmallerIcon;
    private @Nullable Drawable mWidgetIcon;
    private boolean mUseSmallerIcon;
    private boolean mEllipsizeEnd;

    public PermissionUsagePreference(@NonNull Context context, @NonNull AppPermissionGroup group,
            @Nullable Drawable widgetIcon, boolean useSmallerIcon) {
    public PermissionControlPreference(@NonNull Context context,
            @NonNull AppPermissionGroup group) {
        super(context);
        mGroup = group;
        mWidgetIcon = widgetIcon;
        mContext = context;
        mUseSmallerIcon = useSmallerIcon;
        mWidgetIcon = null;
        mUseSmallerIcon = false;
        mEllipsizeEnd = false;
        if (mWidgetIcon != null) {
            setWidgetLayoutResource(R.layout.image_view);
        }
        setOnPreferenceClickListener(preference -> {
            Intent intent = new Intent(context, AppPermissionActivity.class);
            intent.putExtra(Intent.EXTRA_PACKAGE_NAME, mGroup.getApp().packageName);
            intent.putExtra(Intent.EXTRA_PERMISSION_NAME, mGroup.getName());
            intent.putExtra(Intent.EXTRA_USER, mGroup.getUser());
            intent.putExtra(Intent.EXTRA_PACKAGE_NAME, group.getApp().packageName);
            intent.putExtra(Intent.EXTRA_PERMISSION_NAME, group.getName());
            intent.putExtra(Intent.EXTRA_USER, group.getUser());
            context.startActivity(intent);
            return true;
        });
    }

    /**
     * Sets this preference's right icon.
     *
     * Note that this must be called before preference layout to take effect.
     *
     * @param widgetIcon the icon to use.
     */
    public void setRightIcon(@NonNull Drawable widgetIcon) {
        mWidgetIcon = widgetIcon;
        setWidgetLayoutResource(R.layout.image_view);
    }

    /**
     * Sets this preference's left icon to be smaller than normal.
     *
     * Note that this must be called before preference layout to take effect.
     */
    public void useSmallerIcon() {
        mUseSmallerIcon = true;
    }

    /**
     * Sets this preference's title to use an ellipsis at the end.
     *
@@ -73,6 +92,22 @@ public class PermissionUsagePreference extends Preference {
        mEllipsizeEnd = true;
    }

    /**
     * Sets this preference's summary based on the group it represents, if applicable.
     *
     * @param group the permission group this preference represents.
     */
    public void setGroupSummary(@NonNull AppPermissionGroup group) {
        if (group.hasPermissionWithBackgroundMode() && group.areRuntimePermissionsGranted()) {
            AppPermissionGroup backgroundGroup = group.getBackgroundPermissions();
            if (backgroundGroup == null || !backgroundGroup.areRuntimePermissionsGranted()) {
                setSummary(R.string.permission_access_only_foreground);
                return;
            }
        }
        setSummary("");
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        if (mUseSmallerIcon) {
+4 −4
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.packageinstaller.permission.model.AppPermissionGroup;
@@ -347,16 +346,17 @@ public class PermissionUsageFragment extends PermissionsFrameFragment implements
            }
            AppPermissionGroup group = usageToGroup.get(usage);
            PermissionApp permApp = usageToApp.get(usage);
            Preference pref = new PermissionUsagePreference(context, group,
                    Utils.applyTint(context, group.getIconResId(),
                            android.R.attr.colorControlNormal), true);
            PermissionControlPreference pref = new PermissionControlPreference(context, group);
            pref.setTitle(permApp.getLabel());
            long timeDiff = System.currentTimeMillis() - usage.getTime();
            String timeDiffStr = Utils.getTimeDiffStr(context, timeDiff);
            pref.setSummary(context.getString(R.string.permission_usage_summary,
                    usage.getPermissionGroupLabel(), timeDiffStr));
            pref.setIcon(permApp.getIcon());
            pref.setRightIcon(Utils.applyTint(context, group.getIconResId(),
                    android.R.attr.colorControlNormal));
            pref.setKey(usage.getPackageName() + "," + usage.getPermissionGroupName());
            pref.useSmallerIcon();
            screen.addPreference(pref);
        }
    }