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

Commit ab0cde6b authored by jackqdyulei's avatar jackqdyulei
Browse files

Add app restrict tip and detector

1. Add RestrictAppTip with two state(new and handled)
2. Add RestrictAppDetector, and future cl will hook up
it with anomaly database
3. Add related dialog in BatteryTipDialogFragment

Bug: 72385333
Test: RunSettingsRoboTests
Change-Id: Ic10efc6387150e62b6c6ad8d4c0d16ff75564fac
parent 36952523
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -4842,6 +4842,28 @@
    <string name="battery_tip_dialog_message" product="tablet">Your tablet was used heavily and this consumed a lot of battery. Your battery is behaving normally.\n\n Your tablet was used for about <xliff:g id="hour">%1$s</xliff:g> since last full charge.\n\n Total usage:</string>
    <!-- Message for battery tip dialog to show the status about the battery [CHAR LIMIT=NONE] -->
    <string name="battery_tip_dialog_message" product="device">Your device was used heavily and this consumed a lot of battery. Your battery is behaving normally.\n\n Your device was used for about <xliff:g id="hour">%1$s</xliff:g> since last full charge.\n\n Total usage:</string>
    <!-- Title for restricted app preference, showing how many app need to be restricted [CHAR LIMIT=NONE] -->
    <plurals name="battery_tip_restrict_title">
        <item quantity="one">Restrict %1$d app</item>
        <item quantity="other">Restrict %1$d apps</item>
    </plurals>
    <!-- Title for restricted app preference, showing how many app been restricted [CHAR LIMIT=NONE] -->
    <plurals name="battery_tip_restrict_handled_title">
        <item quantity="one">%1$d recently restricted</item>
        <item quantity="other">%1$d apps recently restricted</item>
    </plurals>
    <!-- Summary for restricted app preference, showing the impact of the apps [CHAR LIMIT=NONE] -->
    <plurals name="battery_tip_restrict_summary">
        <item quantity="one">%1$s has high battery usage</item>
        <item quantity="other">%2$d apps have high battery usage</item>
    </plurals>
    <!-- Summary for restricted app preference, showing the impact of the apps [CHAR LIMIT=NONE] -->
    <string name="battery_tip_restrict_handled_summary">App changes are in progress</string>
    <!-- Message for battery tip dialog to show the restrict app list [CHAR LIMIT=NONE] -->
    <string name="battery_tip_restrict_app_dialog_message">To save battery, you can stop this app from running in the background when it’s not being used.</string>
    <!-- OK button for battery tip dialog to show the restrict app list [CHAR LIMIT=NONE] -->
    <string name="battery_tip_restrict_app_dialog_ok">Restrict</string>
    <!-- Title for the smart battery manager preference [CHAR LIMIT=NONE] -->
    <string name="smart_battery_manager_title">Smart battery manager</string>
+20 −0
Original line number Diff line number Diff line
@@ -34,6 +34,9 @@ import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController.
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.HighUsageTip;
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;

import java.util.List;

/**
 * Dialog Fragment to show action dialog for each anomaly
@@ -84,6 +87,23 @@ public class BatteryTipDialogFragment extends InstrumentedDialogFragment impleme
                        .setView(view)
                        .setPositiveButton(android.R.string.ok, null)
                        .create();
            case BatteryTip.TipType.APP_RESTRICTION:
                final RestrictAppTip restrictAppTip = (RestrictAppTip) mBatteryTip;
                final RecyclerView restrictionView = (RecyclerView) LayoutInflater.from(
                        context).inflate(R.layout.recycler_view, null);
                final List<AppInfo> restrictedAppList = restrictAppTip.getRestrictAppList();
                final int num = restrictedAppList.size();
                restrictionView.setLayoutManager(new LinearLayoutManager(context));
                restrictionView.setAdapter(new HighUsageAdapter(context, restrictedAppList));

                return new AlertDialog.Builder(context)
                        .setTitle(context.getResources().getQuantityString(
                                R.plurals.battery_tip_restrict_title, num, num))
                        .setMessage(getString(R.string.battery_tip_restrict_app_dialog_message))
                        .setView(restrictionView)
                        .setPositiveButton(R.string.battery_tip_restrict_app_dialog_ok, this)
                        .setNegativeButton(android.R.string.cancel, null)
                        .create();
            default:
                throw new IllegalArgumentException("unknown type " + mBatteryTip.getType());
        }
+2 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import com.android.settings.fuelgauge.batterytip.detectors.EarlyWarningDetector;
import com.android.settings.fuelgauge.batterytip.detectors.HighUsageDetector;
import com.android.settings.fuelgauge.batterytip.detectors.LowBatteryDetector;
import com.android.settings.fuelgauge.batterytip.detectors.SmartBatteryDetector;
import com.android.settings.fuelgauge.batterytip.detectors.RestrictAppDetector;
import com.android.settings.fuelgauge.batterytip.detectors.SummaryDetector;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
@@ -70,6 +71,7 @@ public class BatteryTipLoader extends AsyncLoader<List<BatteryTip>> {
        tips.add(new SmartBatteryDetector(policy, context.getContentResolver()).detect());
        tips.add(new EarlyWarningDetector(policy, context).detect());
        tips.add(new SummaryDetector(policy).detect());
        tips.add(new RestrictAppDetector(policy).detect());

        Collections.sort(tips);
        return tips;
+3 −1
Original line number Diff line number Diff line
@@ -77,8 +77,10 @@ public class HighUsageAdapter extends RecyclerView.Adapter<HighUsageAdapter.View
                Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager, app.packageName,
                        UserHandle.myUserId()));
        holder.appName.setText(Utils.getApplicationLabel(mContext, app.packageName));
        if (app.screenOnTimeMs != 0) {
            holder.appTime.setText(Utils.formatElapsedTime(mContext, app.screenOnTimeMs, false));
        }
    }

    @Override
    public int getItemCount() {
+46 −0
Original line number 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.batterytip.detectors;

import com.android.settings.fuelgauge.batterytip.AppInfo;
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;

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

/**
 * Detector whether to show summary tip. This detector should be executed as the last
 * {@link BatteryTipDetector} since it need the most up-to-date {@code visibleTips}
 */
public class RestrictAppDetector implements BatteryTipDetector {
    private BatteryTipPolicy mPolicy;

    public RestrictAppDetector(BatteryTipPolicy policy) {
        mPolicy = policy;
    }

    @Override
    public BatteryTip detect() {
        // TODO(b/70570352): Detect restrict apps here, get data from database
        final List<AppInfo> highUsageApps = new ArrayList<>();
        return new RestrictAppTip(
                highUsageApps.isEmpty() ? BatteryTip.StateType.INVISIBLE : BatteryTip.StateType.NEW,
                highUsageApps);
    }
}
Loading