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

Commit d9360ec3 authored by mxyyiyi's avatar mxyyiyi Committed by Zaiyue Xue
Browse files

Impl dismiss action in battery tips cards.

- Use SharedPreferences to record and filter the already dimissed anomaly.

Bug: 291689623
Test: manual
Change-Id: I4fd4a39066591a4a201857f9586b6595b7d5c43b
parent c68e0385
Loading
Loading
Loading
Loading
+15 −4
Original line number Diff line number Diff line
@@ -52,10 +52,10 @@ import com.google.common.base.Objects;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -397,12 +397,23 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
        return true;
    }

    private PowerAnomalyEvent getHighestScoreAnomalyEvent(PowerAnomalyEventList anomalyEventList) {
    @VisibleForTesting
    PowerAnomalyEvent getHighestScoreAnomalyEvent(PowerAnomalyEventList anomalyEventList) {
        if (anomalyEventList == null || anomalyEventList.getPowerAnomalyEventsCount() == 0) {
            return null;
        }
        return Collections.max(anomalyEventList.getPowerAnomalyEventsList(),
                Comparator.comparing(PowerAnomalyEvent::getScore));
        final Set<String> dismissedPowerAnomalyKeys =
                DatabaseUtils.getDismissedPowerAnomalyKeys(mContext);
        Log.d(TAG, "dismissedPowerAnomalyKeys = " + dismissedPowerAnomalyKeys);

        final PowerAnomalyEvent highestScoreEvent = anomalyEventList.getPowerAnomalyEventsList()
                .stream()
                .filter(event -> event.hasKey()
                        && !dismissedPowerAnomalyKeys.contains(event.getKey().name()))
                .max(Comparator.comparing(PowerAnomalyEvent::getScore))
                .orElse(null);
        Log.d(TAG, "highestScoreAnomalyEvent = " + highestScoreEvent);
        return highestScoreEvent;
    }

    private boolean refreshUiWithNoLevelDataCase() {
+12 −0
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ public class BatteryTipsCardPreference extends Preference implements View.OnClic
    private final MetricsFeatureProvider mMetricsFeatureProvider;

    private String mAnomalyEventId;
    private PowerAnomalyKey mPowerAnomalyKey;

    @VisibleForTesting
    CharSequence mMainButtonLabel;
@@ -69,6 +70,7 @@ public class BatteryTipsCardPreference extends Preference implements View.OnClic
        final FeatureFactory featureFactory = FeatureFactory.getFeatureFactory();
        mPowerUsageFeatureProvider =  featureFactory.getPowerUsageFeatureProvider();
        mMetricsFeatureProvider = featureFactory.getMetricsFeatureProvider();
        mPowerAnomalyKey = null;
    }

    /**
@@ -98,6 +100,13 @@ public class BatteryTipsCardPreference extends Preference implements View.OnClic
        }
    }

    /**
     * Sets the power anomaly key of battery tips card.
     */
    public void setPowerAnomalyKey(final PowerAnomalyKey powerAnomalyKey) {
        mPowerAnomalyKey = powerAnomalyKey;
    }

    /**
     * Sets the info of target fragment launched by main button.
     */
@@ -133,6 +142,9 @@ public class BatteryTipsCardPreference extends Preference implements View.OnClic
            setVisible(false);
            mMetricsFeatureProvider.action(
                    getContext(), SettingsEnums.ACTION_BATTERY_TIPS_CARD_DISMISS, mAnomalyEventId);
            if (mPowerAnomalyKey != null) {
                DatabaseUtils.setDismissedPowerAnomalyKeys(getContext(), mPowerAnomalyKey.name());
            }
        }
    }

+4 −2
Original line number Diff line number Diff line
@@ -108,8 +108,9 @@ public class BatteryTipsController extends BasePreferenceController {
        }

        // Get card preference strings and navigate fragment info
        final int resourceIndex = powerAnomalyEvent.hasKey()
                ? powerAnomalyEvent.getKey().getNumber() : -1;
        final PowerAnomalyKey powerAnomalyKey = powerAnomalyEvent.hasKey()
                ? powerAnomalyEvent.getKey() : null;
        final int resourceIndex = powerAnomalyKey != null ? powerAnomalyKey.getNumber() : -1;

        String titleString = getString(powerAnomalyEvent, WarningBannerInfo::getTitleString,
                WarningItemInfo::getTitleString, R.array.power_anomaly_titles, resourceIndex);
@@ -134,6 +135,7 @@ public class BatteryTipsController extends BasePreferenceController {

        // Updated card preference and main button fragment launcher
        mCardPreference.setAnomalyEventId(powerAnomalyEvent.getEventId());
        mCardPreference.setPowerAnomalyKey(powerAnomalyKey);
        mCardPreference.setTitle(titleString);
        mCardPreference.setMainButtonLabel(mainBtnString);
        mCardPreference.setDismissButtonLabel(dismissBtnString);
+1 −0
Original line number Diff line number Diff line
@@ -74,6 +74,7 @@ public final class BatteryUsageDataLoader {
                    context, DatabaseUtils.KEY_LAST_LOAD_FULL_CHARGE_TIME);
            DatabaseUtils.sendBatteryEventData(context, ConvertUtils.convertToBatteryEvent(
                    currentTime, BatteryEventType.FULL_CHARGED, 100));
            DatabaseUtils.removeDismissedPowerAnomalyKeys(context);
        }

        // Uploads the BatteryEntry data into database.
+31 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.os.Looper;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UserManager;
import android.util.ArraySet;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
@@ -53,6 +54,7 @@ import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@@ -69,6 +71,7 @@ public final class DatabaseUtils {
    static final String KEY_LAST_LOAD_FULL_CHARGE_TIME = "last_load_full_charge_time";
    static final String KEY_LAST_UPLOAD_FULL_CHARGE_TIME = "last_upload_full_charge_time";
    static final String KEY_LAST_USAGE_SOURCE = "last_usage_source";
    static final String KEY_DISMISSED_POWER_ANOMALY_KEYS = "dismissed_power_anomaly_keys";

    /** An authority name of the battery content provider. */
    public static final String AUTHORITY = "com.android.settings.battery.usage.provider";
@@ -636,6 +639,8 @@ public final class DatabaseUtils {
                KEY_LAST_LOAD_FULL_CHARGE_TIME);
        writeString(context, writer, "LastUploadFullChargeTime",
                KEY_LAST_UPLOAD_FULL_CHARGE_TIME);
        writeString(context, writer, "DismissedPowerAnomalyKeys",
                KEY_DISMISSED_POWER_ANOMALY_KEYS);
    }

    static SharedPreferences getSharedPreferences(Context context) {
@@ -674,6 +679,32 @@ public final class DatabaseUtils {
        return usageSource;
    }

    static void removeDismissedPowerAnomalyKeys(Context context) {
        final SharedPreferences sharedPreferences = getSharedPreferences(context);
        if (sharedPreferences != null
                && sharedPreferences.contains(KEY_DISMISSED_POWER_ANOMALY_KEYS)) {
            sharedPreferences.edit().remove(KEY_DISMISSED_POWER_ANOMALY_KEYS).apply();
        }
    }

    static Set<String> getDismissedPowerAnomalyKeys(Context context) {
        final SharedPreferences sharedPreferences = getSharedPreferences(context);
        return sharedPreferences != null
                ? sharedPreferences.getStringSet(KEY_DISMISSED_POWER_ANOMALY_KEYS, new ArraySet<>())
                : new ArraySet<>();
    }

    static void setDismissedPowerAnomalyKeys(Context context, String dismissedPowerAnomalyKey) {
        final SharedPreferences sharedPreferences = getSharedPreferences(context);
        if (sharedPreferences != null) {
            final Set<String> dismissedPowerAnomalyKeys = getDismissedPowerAnomalyKeys(context);
            dismissedPowerAnomalyKeys.add(dismissedPowerAnomalyKey);
            sharedPreferences.edit()
                    .putStringSet(KEY_DISMISSED_POWER_ANOMALY_KEYS, dismissedPowerAnomalyKeys)
                    .apply();
        }
    }

    static void recordDateTime(Context context, String preferenceKey) {
        final SharedPreferences sharedPreferences = getSharedPreferences(context);
        if (sharedPreferences != null) {
Loading