Android 9.0 (P版本) 原生的电池-Google省电模式

1. UI 界面

Android 9.0 (P版本) 原生的电池-Google省电模式_第1张图片

2. 源码

2.1 界面位置

Z:\9.1\vendor\mediatek\proprietary\packages\apps\MtkSettings_Eclipse\res\xml\power_usage_summary.xml

    

从上面看,具体界面控制器在于 settings:controller=“com.android.settings.fuelgauge.BatterySaverController”,点击时调整在于android:fragment=“com.android.settings.fuelgauge.batterysaver.BatterySaverSettings”

2.2 查看 BatterySaverController.java

Z:\9.1\vendor\mediatek\proprietary\packages\apps\MtkSettings_Eclipse\src\com\android\settings\fuelgauge\BatterySaverController.java

主要处理选项框中的显示信息

package com.android.settings.fuelgauge;

public class BatterySaverController extends BasePreferenceController
        implements LifecycleObserver, OnStart, OnStop, BatterySaverReceiver.BatterySaverListener {
        
    @Override
    public CharSequence getSummary() {
        final boolean isPowerSaveOn = mPowerManager.isPowerSaveMode();
        final int percent = Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
        if (isPowerSaveOn) {
            return mContext.getString(R.string.battery_saver_on_summary);
        } else if (percent != 0) {
            return mContext.getString(R.string.battery_saver_off_scheduled_summary,
                    Utils.formatPercentage(percent));
        } else {
            return mContext.getString(R.string.battery_saver_off_summary);
        }
    }
    
    "开启"
    "在电量降到 %1$s 时开启"
    "关闭"

2.3 查看 BatterySaverSettings.java

package com.android.settings.fuelgauge.batterysaver;

/**
 * Battery saver settings page
 */
public class BatterySaverSettings extends DashboardFragment {

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

    

上述看BatterySaverButtonPreferenceController有逻辑处理

2.4 查看 BatterySaverButtonPreferenceController.java

核心方法 BatterySaverUtils.setPowerSaveMode

package com.android.settings.fuelgauge.batterysaver;

import com.android.settingslib.fuelgauge.BatterySaverUtils;


/**
 * Controller to update the battery saver button
 */
public class BatterySaverButtonPreferenceController extends
        TogglePreferenceController implements
        LifecycleObserver, OnStart, OnStop, BatterySaverReceiver.BatterySaverListener {
        
    @Override
    public boolean setChecked(boolean stateOn) {
        // This screen already shows a warning, so we don't need another warning.
        return BatterySaverUtils.setPowerSaveMode(mContext, stateOn,
                false /* needFirstTimeWarning */);
    }

2.5 查看 BatterySaverUtils.setPowerSaveMode

frameworks/base/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatterySaverUtils.java

package com.android.settingslib.fuelgauge;

/**
 * Utilities related to battery saver.
 */
public class BatterySaverUtils {
    private static final String TAG = "BatterySaverUtils";
    
        /**
     * Enable / disable battery saver by user request.
     * - If it's the first time and needFirstTimeWarning, show the first time dialog.
     * - If it's 4th time through 8th time, show the schedule suggestion notification.
     *
     * @param enable true to disable battery saver.
     *
     * @return true if the request succeeded.
     */
    public static synchronized boolean setPowerSaveMode(Context context,
            boolean enable, boolean needFirstTimeWarning) {
        if (DEBUG) {
            Log.d(TAG, "Battery saver turning " + (enable ? "ON" : "OFF"));
        }
        final ContentResolver cr = context.getContentResolver();

        if (enable && needFirstTimeWarning && maybeShowBatterySaverConfirmation(context)) {
            return false;
        }
        if (enable && !needFirstTimeWarning) {
            setBatterySaverConfirmationAcknowledged(context);
        }

        if (context.getSystemService(PowerManager.class).setPowerSaveMode(enable)) {
            if (enable) {
                final int count =
                        Secure.getInt(cr, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, 0) + 1;
                Secure.putInt(cr, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, count);

                final Parameters parameters = new Parameters(context);

                if ((count >= parameters.startNth)
                        && (count <= parameters.endNth)
                        && Global.getInt(cr, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0) == 0
                        && Secure.getInt(cr,
                        Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, 0) == 0) {
                    showAutoBatterySaverSuggestion(context);
                }
            }

            return true;
        }
        return false;
    }

这里有个熟悉的老接口 getSystemService(PowerManager.class).setPowerSaveMode(enable)

3. Google省电模式的接口

    public static boolean isGooglePowerSaveMode(Context mContext) {
        PowerManager mPowerManager = ((PowerManager) mContext.getSystemService(Context.POWER_SERVICE));
        return mPowerManager.isPowerSaveMode();
    }
    
    /**
     * android.Manifest.permission.DEVICE_POWER
     *
     * @param mContext
     * @param value
     */
    public static void setGooglePowerSaveMode(Context mContext, boolean value) {
        PowerManager mPowerManager = ((PowerManager) mContext.getSystemService(Context.POWER_SERVICE));
        mPowerManager.setPowerSaveMode(value);
    }

你可能感兴趣的:(省电续航,Android,P)