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

Commit 6411b170 authored by Lei Yu's avatar Lei Yu Committed by Android (Google) Code Review
Browse files

Merge "Add restrict app detail page"

parents d0660d28 0fb2d68f
Loading
Loading
Loading
Loading
+24 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2018 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.
-->

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/restricted_app_title">

    <PreferenceCategory
        android:key="restrict_app_list"/>

</PreferenceScreen>
+5 −28
Original line number Original line Diff line number Diff line
@@ -54,9 +54,10 @@ public class BackgroundActivityPreferenceController extends AbstractPreferenceCo
    private final int mUid;
    private final int mUid;
    @VisibleForTesting
    @VisibleForTesting
    DevicePolicyManagerWrapper mDpm;
    DevicePolicyManagerWrapper mDpm;
    @VisibleForTesting
    BatteryUtils mBatteryUtils;
    private Fragment mFragment;
    private Fragment mFragment;
    private String mTargetPackage;
    private String mTargetPackage;
    private boolean mIsPreOApp;
    private PowerWhitelistBackend mPowerWhitelistBackend;
    private PowerWhitelistBackend mPowerWhitelistBackend;


    public BackgroundActivityPreferenceController(Context context, Fragment fragment,
    public BackgroundActivityPreferenceController(Context context, Fragment fragment,
@@ -77,7 +78,7 @@ public class BackgroundActivityPreferenceController extends AbstractPreferenceCo
        mUid = uid;
        mUid = uid;
        mFragment = fragment;
        mFragment = fragment;
        mTargetPackage = packageName;
        mTargetPackage = packageName;
        mIsPreOApp = isLegacyApp(packageName);
        mBatteryUtils = BatteryUtils.getInstance(context);
    }
    }


    @Override
    @Override
@@ -109,12 +110,7 @@ public class BackgroundActivityPreferenceController extends AbstractPreferenceCo
     * activity for this package
     * activity for this package
     */
     */
    public void setUnchecked(Preference preference) {
    public void setUnchecked(Preference preference) {
        if (mIsPreOApp) {
        mBatteryUtils.setForceAppStandby(mUid, mTargetPackage, AppOpsManager.MODE_IGNORED);
            mAppOpsManager.setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, mUid, mTargetPackage,
                    AppOpsManager.MODE_IGNORED);
        }
        mAppOpsManager.setMode(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, mUid, mTargetPackage,
                AppOpsManager.MODE_IGNORED);
        ((SwitchPreference) preference).setChecked(false);
        ((SwitchPreference) preference).setChecked(false);
        updateSummary(preference);
        updateSummary(preference);
    }
    }
@@ -133,30 +129,11 @@ public class BackgroundActivityPreferenceController extends AbstractPreferenceCo
            dialogFragment.show(mFragment.getFragmentManager(), TAG);
            dialogFragment.show(mFragment.getFragmentManager(), TAG);
            return false;
            return false;
        }
        }
        if (mIsPreOApp) {
        mBatteryUtils.setForceAppStandby(mUid, mTargetPackage, AppOpsManager.MODE_ALLOWED);
            mAppOpsManager.setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, mUid, mTargetPackage,
                    AppOpsManager.MODE_ALLOWED);
        }
        mAppOpsManager.setMode(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, mUid, mTargetPackage,
                AppOpsManager.MODE_ALLOWED);
        updateSummary(preference);
        updateSummary(preference);
        return true;
        return true;
    }
    }


    @VisibleForTesting
    boolean isLegacyApp(final String packageName) {
        try {
            ApplicationInfo info = mPackageManager.getApplicationInfo(packageName,
                    PackageManager.GET_META_DATA);

            return info.targetSdkVersion < Build.VERSION_CODES.O;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Cannot find package: " + packageName, e);
        }

        return false;
    }

    @VisibleForTesting
    @VisibleForTesting
    void updateSummary(Preference preference) {
    void updateSummary(Preference preference) {
        if (mPowerWhitelistBackend.isWhitelisted(mTargetPackage)) {
        if (mPowerWhitelistBackend.isWhitelisted(mTargetPackage)) {
+26 −0
Original line number Original line Diff line number Diff line
@@ -398,6 +398,19 @@ public class BatteryUtils {
        return timeMs * 1000;
        return timeMs * 1000;
    }
    }


    public void setForceAppStandby(int uid, String packageName,
            int mode) {
        final boolean isPreOApp = isLegacyApp(packageName);
        if (isPreOApp) {
            // Control whether app could run in the background if it is pre O app
            mAppOpsManager.setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, uid, packageName,
                    mode);
        }
        // Control whether app could run jobs in the background
        mAppOpsManager.setMode(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, uid, packageName,
                mode);
    }

    public void initBatteryStatsHelper(BatteryStatsHelper statsHelper, Bundle bundle,
    public void initBatteryStatsHelper(BatteryStatsHelper statsHelper, Bundle bundle,
            UserManager userManager) {
            UserManager userManager) {
        statsHelper.create(bundle);
        statsHelper.create(bundle);
@@ -481,5 +494,18 @@ public class BatteryUtils {
        return 0;
        return 0;
    }
    }


    public boolean isLegacyApp(final String packageName) {
        try {
            ApplicationInfo info = mPackageManager.getApplicationInfo(packageName,
                    PackageManager.GET_META_DATA);

            return info.targetSdkVersion < Build.VERSION_CODES.O;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Cannot find package: " + packageName, e);
        }

        return false;
    }

}
}
+25 −0
Original line number Original line Diff line number Diff line
@@ -20,9 +20,11 @@ package com.android.settings.fuelgauge;
import android.app.AppOpsManager;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Context;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.VisibleForTesting;
import android.support.v14.preference.PreferenceFragment;
import android.support.v7.preference.Preference;
import android.support.v7.preference.Preference;


import com.android.settings.R;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.BasePreferenceController;


@@ -37,12 +39,21 @@ public class RestrictAppPreferenceController extends BasePreferenceController {


    private AppOpsManager mAppOpsManager;
    private AppOpsManager mAppOpsManager;
    private List<AppOpsManager.PackageOps> mPackageOps;
    private List<AppOpsManager.PackageOps> mPackageOps;
    private SettingsActivity mSettingsActivity;
    private PreferenceFragment mPreferenceFragment;


    public RestrictAppPreferenceController(Context context) {
    public RestrictAppPreferenceController(Context context) {
        super(context, KEY_RESTRICT_APP);
        super(context, KEY_RESTRICT_APP);
        mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    }
    }


    public RestrictAppPreferenceController(SettingsActivity settingsActivity,
            PreferenceFragment preferenceFragment) {
        this(settingsActivity.getApplicationContext());
        mSettingsActivity = settingsActivity;
        mPreferenceFragment = preferenceFragment;
    }

    @Override
    @Override
    public int getAvailabilityStatus() {
    public int getAvailabilityStatus() {
        return AVAILABLE;
        return AVAILABLE;
@@ -51,13 +62,27 @@ public class RestrictAppPreferenceController extends BasePreferenceController {
    @Override
    @Override
    public void updateState(Preference preference) {
    public void updateState(Preference preference) {
        super.updateState(preference);
        super.updateState(preference);

        mPackageOps = mAppOpsManager.getPackagesForOps(
        mPackageOps = mAppOpsManager.getPackagesForOps(
                new int[]{AppOpsManager.OP_RUN_ANY_IN_BACKGROUND});
                new int[]{AppOpsManager.OP_RUN_ANY_IN_BACKGROUND});
        final int num = mPackageOps != null ? mPackageOps.size() : 0;
        final int num = mPackageOps != null ? mPackageOps.size() : 0;


        // Enable the preference if some apps already been restricted, otherwise disable it
        preference.setEnabled(num > 0);
        preference.setSummary(
        preference.setSummary(
                mContext.getResources().getQuantityString(R.plurals.restricted_app_summary, num,
                mContext.getResources().getQuantityString(R.plurals.restricted_app_summary, num,
                        num));
                        num));
    }
    }


    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if (getPreferenceKey().equals(preference.getKey())) {
            // start fragment
            RestrictedAppDetails.startRestrictedAppDetails(mSettingsActivity, mPreferenceFragment,
                    mPackageOps);
            return true;
        }

        return super.handlePreferenceTreeClick(preference);
    }
}
}
+147 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2018 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.fuelgauge;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.UserHandle;
import android.support.v14.preference.PreferenceFragment;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;
import android.util.IconDrawableFactory;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.Utils;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.fuelgauge.anomaly.Anomaly;
import com.android.settings.fuelgauge.anomaly.AnomalyDialogFragment;
import com.android.settings.fuelgauge.anomaly.AnomalyPreference;
import com.android.settingslib.core.AbstractPreferenceController;

import java.util.List;

/**
 * Fragment to show a list of anomaly apps, where user could handle these anomalies
 */
public class RestrictedAppDetails extends DashboardFragment {

    public static final String TAG = "RestrictedAppDetails";

    private static final String EXTRA_PACKAGE_OPS_LIST = "package_ops_list";
    private static final String KEY_PREF_RESTRICTED_APP_LIST = "restrict_app_list";

    @VisibleForTesting
    List<AppOpsManager.PackageOps> mPackageOpsList;
    @VisibleForTesting
    IconDrawableFactory mIconDrawableFactory;
    @VisibleForTesting
    PreferenceGroup mRestrictedAppListGroup;
    @VisibleForTesting
    BatteryUtils mBatteryUtils;
    @VisibleForTesting
    PackageManager mPackageManager;

    public static void startRestrictedAppDetails(SettingsActivity caller,
            PreferenceFragment fragment, List<AppOpsManager.PackageOps> packageOpsList) {
        Bundle args = new Bundle();
        args.putParcelableList(EXTRA_PACKAGE_OPS_LIST, packageOpsList);

        caller.startPreferencePanelAsUser(fragment, RestrictedAppDetails.class.getName(), args,
                R.string.restricted_app_title, null /* titleText */,
                new UserHandle(UserHandle.myUserId()));
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        final Context context = getContext();

        mRestrictedAppListGroup = (PreferenceGroup) findPreference(KEY_PREF_RESTRICTED_APP_LIST);
        mPackageOpsList = getArguments().getParcelableArrayList(EXTRA_PACKAGE_OPS_LIST);
        mPackageManager = context.getPackageManager();
        mIconDrawableFactory = IconDrawableFactory.newInstance(context);
        mBatteryUtils = BatteryUtils.getInstance(context);

        refreshUi();
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {

        return super.onPreferenceTreeClick(preference);
    }

    @Override
    protected String getLogTag() {
        return TAG;
    }

    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.restricted_apps_detail;
    }

    @Override
    protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
        return null;
    }

    @Override
    public int getMetricsCategory() {
        return MetricsProto.MetricsEvent.FUELGAUGE_RESTRICTED_APP_DETAILS;
    }

    @VisibleForTesting
    void refreshUi() {
        mRestrictedAppListGroup.removeAll();
        final Context context = getPrefContext();

        for (int i = 0, size = mPackageOpsList.size(); i < size; i++) {
            final CheckBoxPreference checkBoxPreference = new CheckBoxPreference(context);
            final AppOpsManager.PackageOps packageOps = mPackageOpsList.get(i);
            try {
                final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(
                        packageOps.getPackageName(), 0 /* flags */);
                checkBoxPreference.setChecked(true);
                checkBoxPreference.setTitle(mPackageManager.getApplicationLabel(applicationInfo));
                checkBoxPreference.setKey(packageOps.getPackageName());
                checkBoxPreference.setOnPreferenceChangeListener((pref, value) -> {
                    // change the toggle
                    final int mode = (Boolean) value ? AppOpsManager.MODE_IGNORED
                            : AppOpsManager.MODE_ALLOWED;
                    final String packageName = pref.getKey();
                    final int uid = mBatteryUtils.getPackageUid(packageName);
                    mBatteryUtils.setForceAppStandby(uid, packageName, mode);
                    return true;
                });
                mRestrictedAppListGroup.addPreference(checkBoxPreference);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

}
Loading