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

Commit cdd73e7f authored by ykhung's avatar ykhung Committed by YUKAI HUNG
Browse files

Add kill-switch to distinguish chart or non-chart design

Bug: 184807417
Bug: 180607918
Test: make SettingsRoboTests
Test: make SettingsGoogleRoboTests
Change-Id: Iacbb012209d552b58b774f90f5b0aeb60ce6d33d
parent 57f27f7a
Loading
Loading
Loading
Loading
+86 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.content.Context;
import android.util.Log;

import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.core.InstrumentedPreferenceFragment;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnDestroy;
import com.android.settingslib.core.lifecycle.events.OnPause;

import java.util.List;
import java.util.Map;

/** Cotrols the update for chart graph and the list items. */
public class BatteryChartPreferenceController extends AbstractPreferenceController
        implements PreferenceControllerMixin, LifecycleObserver, OnPause, OnDestroy {
    private static final String TAG = "BatteryChartPreferenceController";

    private final String mPreferenceKey;

    public BatteryChartPreferenceController(
            Context context, String chartPreferenceKey, String listPreferenceKey,
            Lifecycle lifecycle, SettingsActivity activity,
            InstrumentedPreferenceFragment fragment) {
        super(context);
        mPreferenceKey = listPreferenceKey;
    }


    @Override
    public void onPause() {
    }

    @Override
    public void onDestroy() {
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
    }

    @Override
    public boolean isAvailable() {
        return true;
    }

    @Override
    public String getPreferenceKey() {
        return mPreferenceKey;
    }

    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        return false;
    }

    void refreshUi(Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
        Log.d(TAG, "refreshUi:" + batteryHistoryMap.size());
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -74,7 +74,9 @@ public class BatteryChartView extends AppCompatImageView implements View.OnClick
    public BatteryChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initializeColors(context);
        // Registers the click event listener.
        setOnClickListener(this);
        setClickable(false);
        setSelectedIndex(SELECTED_INDEX_ALL);
        setTrapezoidCount(DEFAULT_TRAPEZOID_COUNT);
    }
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.content.Context;

import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.utils.AsyncLoaderCompat;

import java.util.List;
import java.util.Map;

/** Loader that can be used to load battery history information. */
public class BatteryHistoryLoader
        extends AsyncLoaderCompat<Map<Long, List<BatteryHistEntry>>> {
    private static final String TAG = "BatteryHistoryLoader";

    private final Context mContext;

    public BatteryHistoryLoader(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onDiscardResult(Map<Long, List<BatteryHistEntry>> result) {
    }

    @Override
    public Map<Long, List<BatteryHistEntry>> loadInBackground() {
        final PowerUsageFeatureProvider powerUsageFeatureProvider =
            FeatureFactory.getFactory(mContext).getPowerUsageFeatureProvider(mContext);
        return powerUsageFeatureProvider.getBatteryHistory(mContext);
    }
}
+85 −10
Original line number Diff line number Diff line
@@ -20,8 +20,12 @@ import android.content.Context;
import android.os.BatteryManager;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;

import com.android.settings.R;
import com.android.settings.SettingsActivity;
@@ -33,26 +37,38 @@ import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class PowerUsageAdvanced extends PowerUsageBase {
    private static final String TAG = "AdvancedBatteryUsage";
    private static final String KEY_REFRESH_TYPE = "refresh_type";
    private static final String KEY_BATTERY_GRAPH = "battery_graph";
    private static final String KEY_APP_LIST = "app_list";
    private static final int LOADER_BATTERY_USAGE_STATS = 2;

    @VisibleForTesting
    BatteryHistoryPreference mHistPref;
    @VisibleForTesting
    Map<Long, List<BatteryHistEntry>> mBatteryHistoryMap;
    @VisibleForTesting
    final BatteryHistoryLoaderCallbacks mBatteryHistoryLoaderCallbacks =
            new BatteryHistoryLoaderCallbacks();

    private boolean mIsChartGraphEnabled = false;
    private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
    private BatteryChartPreferenceController mBatteryChartPreferenceController;
    private BatteryAppListPreferenceController mBatteryAppListPreferenceController;

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

        refreshFeatureFlag(getContext());
        mHistPref = (BatteryHistoryPreference) findPreference(KEY_BATTERY_GRAPH);
        mPowerUsageFeatureProvider = FeatureFactory.getFactory(context)
                .getPowerUsageFeatureProvider(context);
        // Removes chart graph preference if the chart design is disabled.
        if (!mIsChartGraphEnabled) {
            removePreference(KEY_BATTERY_GRAPH);
        }
    }

    @Override
@@ -80,12 +96,21 @@ public class PowerUsageAdvanced extends PowerUsageBase {

    @Override
    protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
        refreshFeatureFlag(context);
        final List<AbstractPreferenceController> controllers = new ArrayList<>();

        mBatteryAppListPreferenceController = new BatteryAppListPreferenceController(context,
                KEY_APP_LIST, getSettingsLifecycle(), (SettingsActivity) getActivity(), this);
        // Creates based on the chart design is enabled or not.
        if (mIsChartGraphEnabled) {
            mBatteryChartPreferenceController =
                    new BatteryChartPreferenceController(context,
                        KEY_BATTERY_GRAPH, KEY_APP_LIST,
                        getSettingsLifecycle(), (SettingsActivity) getActivity(), this);
            controllers.add(mBatteryChartPreferenceController);
        } else {
            mBatteryAppListPreferenceController =
                    new BatteryAppListPreferenceController(context, KEY_APP_LIST,
                        getSettingsLifecycle(), (SettingsActivity) getActivity(), this);
            controllers.add(mBatteryAppListPreferenceController);

        }
        return controllers;
    }

@@ -101,7 +126,34 @@ public class PowerUsageAdvanced extends PowerUsageBase {
            return;
        }
        updatePreference(mHistPref);
        mBatteryAppListPreferenceController.refreshAppListGroup(mBatteryUsageStats, true);
        if (mBatteryAppListPreferenceController != null && mBatteryUsageStats != null) {
            mBatteryAppListPreferenceController.refreshAppListGroup(
                    mBatteryUsageStats, /* showAllApps */true);
        }
        if (mBatteryChartPreferenceController != null && mBatteryHistoryMap != null) {
            mBatteryChartPreferenceController.refreshUi(mBatteryHistoryMap);
        }
    }

    @Override
    protected void restartBatteryStatsLoader(int refreshType) {
        final Bundle bundle = new Bundle();
        bundle.putInt(KEY_REFRESH_TYPE, refreshType);
        // Uses customized battery history loader if chart design is enabled.
        if (mIsChartGraphEnabled) {
            getLoaderManager().restartLoader(LOADER_BATTERY_USAGE_STATS, bundle,
                    mBatteryHistoryLoaderCallbacks);
        } else {
            super.restartBatteryStatsLoader(refreshType);
        }
    }

    private void refreshFeatureFlag(Context context) {
        if (mPowerUsageFeatureProvider == null) {
            mPowerUsageFeatureProvider = FeatureFactory.getFactory(context)
                    .getPowerUsageFeatureProvider(context);
            mIsChartGraphEnabled = mPowerUsageFeatureProvider.isChartGraphEnabled(context);
        }
    }

    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
@@ -125,4 +177,27 @@ public class PowerUsageAdvanced extends PowerUsageBase {
                }
            };

    private class BatteryHistoryLoaderCallbacks
            implements LoaderManager.LoaderCallbacks<Map<Long, List<BatteryHistEntry>>> {
        private int mRefreshType;

        @Override
        @NonNull
        public Loader<Map<Long, List<BatteryHistEntry>>> onCreateLoader(int id, Bundle bundle) {
            mRefreshType = bundle.getInt(KEY_REFRESH_TYPE);
            return new BatteryHistoryLoader(getContext());
        }

        @Override
        public void onLoadFinished(Loader<Map<Long, List<BatteryHistEntry>>> loader,
                Map<Long, List<BatteryHistEntry>> batteryHistoryMap) {
            mBatteryHistoryMap = batteryHistoryMap;
            PowerUsageAdvanced.this.onLoadFinished(mRefreshType);
        }

        @Override
        public void onLoaderReset(Loader<Map<Long, List<BatteryHistEntry>>> loader) {
        }
    }

}
+2 −6
Original line number Diff line number Diff line
@@ -36,9 +36,6 @@ import com.android.settings.dashboard.DashboardFragment;
 */
public abstract class PowerUsageBase extends DashboardFragment {

    // +1 to allow ordering for PowerUsageSummary.
    @VisibleForTesting
    static final int MENU_STATS_REFRESH = Menu.FIRST + 1;
    private static final String TAG = "PowerUsageBase";
    private static final String KEY_REFRESH_TYPE = "refresh_type";
    private static final String KEY_INCLUDE_HISTORY = "include_history";
@@ -49,8 +46,8 @@ public abstract class PowerUsageBase extends DashboardFragment {
    BatteryUsageStats mBatteryUsageStats;

    protected UserManager mUm;
    private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
    protected boolean mIsBatteryPresent = true;
    private BatteryBroadcastReceiver mBatteryBroadcastReceiver;

    @VisibleForTesting
    final BatteryUsageStatsLoaderCallbacks mBatteryUsageStatsLoaderCallbacks =
@@ -65,7 +62,6 @@ public abstract class PowerUsageBase extends DashboardFragment {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setHasOptionsMenu(true);

        mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(getContext());
        mBatteryBroadcastReceiver.setBatteryChangedListener(type -> {
@@ -96,7 +92,7 @@ public abstract class PowerUsageBase extends DashboardFragment {
                mBatteryUsageStatsLoaderCallbacks);
    }

    private void onLoadFinished(@BatteryUpdateType int refreshType) {
    protected void onLoadFinished(@BatteryUpdateType int refreshType) {
        refreshUi(refreshType);
    }

Loading