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

Commit 993c687d authored by Steve Kondik's avatar Steve Kondik Committed by Steve Kondik
Browse files

perf: Per-app performance profiles

 * Performance Profiles is going to grow into a much more powerful
   feature which can apply advanced optimizations or power saving
   techniques depending on both the state of the hardware as well
   as the current applications in use.
 * Refactor the original code and move it into PowerManager so
   that it's managed from the same place.
 * Implement "automatic performance profiles". This feature will
   automatically select a profile when specific activities are
   running. Currently this list is static and engages performance
   mode for several benchmarks (trollface).
 * Added support for a new power hint, POWER_HINT_SET_PROFILE.
   Currently, these profiles are fired using a property trigger
   in init. This is easy, but the PowerHAL can do more.
 * Moved the setting to Settings.Secure and also wrapped calls
   to the service in the DEVICE_POWER permission. Nobody should
   mess with this stuff except the system.
 * Powersave profile will automatically activate Android's built-in
   low power mode.
 * Original patch by Jorge Ruesga

TODO:
 * Implement API for per-app configuration by user
 * Quicksettings tile

Change-Id: I140ebc0648c4cf05900e005923247c3180be8c93
parent 5716b670
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -68,4 +68,9 @@ interface IPowerManager
    void setKeyboardLight(boolean on, int key);

	void wakeUpWithProximityCheck(long time);

	boolean setPowerProfile(String profile);
	String getPowerProfile();

    void activityResumed(String componentName);
}
+113 −0
Original line number Diff line number Diff line
@@ -18,7 +18,10 @@ package android.os;

import android.annotation.SdkConstant;
import android.annotation.SystemApi;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

/**
@@ -368,6 +371,31 @@ public final class PowerManager {
     */
    public static final String REBOOT_RECOVERY = "recovery";
    
    /**
     * Power save profile
     * @hide
     */
    public static final String PROFILE_POWER_SAVE = "0";

    /**
     * Balanced power profile
     * @hide
     */
    public static final String PROFILE_BALANCED = "1";

    /**
     * High-performance profile
     * @hide
     */
    public static final String PROFILE_HIGH_PERFORMANCE = "2";

    /**
     * Broadcast sent when profile is changed
     * @hide
     */
    public static final String POWER_PROFILE_CHANGED =
            "com.cyanogenmod.power.PROFILE_CHANGED";

    final Context mContext;
    final IPowerManager mService;
    final Handler mHandler;
@@ -864,6 +892,91 @@ public final class PowerManager {
    /** @hide */
    public static final String EXTRA_POWER_SAVE_MODE = "mode";

    /**
     * True if the system supports power profiles
     *
     * @hide
     */
    public boolean hasPowerProfiles() {
        return !TextUtils.isEmpty(getDefaultPowerProfile()) &&
               !TextUtils.isEmpty(mContext.getResources().getString(
                       com.android.internal.R.string.config_perf_profile_prop));
    }

    /**
     * Gets the default power profile for the device.
     *
     * Returns null if not enabled.
     *
     * @hide
     */
    public String getDefaultPowerProfile() {
        return mContext.getResources().getString(
                com.android.internal.R.string.config_perf_profile_default_entry);
    }

    /**
     * Set the system power profile
     *
     * @throws IllegalArgumentException if invalid
     * @hide
     */
    public boolean setPowerProfile(String profile) {
        if (!hasPowerProfiles()) {
            throw new IllegalArgumentException("Power profiles not enabled on this system!");
        }

        boolean changed = false;
        try {
            if (mService != null) {
                changed = mService.setPowerProfile(profile);
            }
        } catch (RemoteException e) {
            throw new IllegalArgumentException(e);
        }
        return changed;
    }

    /**
     * Gets the current power profile
     *
     * Returns null if power profiles are not enabled
     * @hide
     */
    public String getPowerProfile() {
        String ret = null;
        if (hasPowerProfiles()) {
            try {
                if (mService != null) {
                    ret = mService.getPowerProfile();
                }
            } catch (RemoteException e) {
                // nothing
            }
        }
        return ret;
    }

    /**
     * Update profile for resumed app, called from ActivityStack
     * @hide
     */
    public void activityResumed(Intent intent) {
        String ret = null;
        if (hasPowerProfiles()) {
            try {
                if (intent != null && mService != null) {
                    ComponentName cn = intent.getComponent();
                    if (cn != null) {
                        mService.activityResumed(cn.flattenToString());
                    }
                }
            } catch (RemoteException e) {
                // nothing
            }
        }
    }

    /**
     * A wake lock is a mechanism to indicate that your application needs
     * to have the device stay on.
+13 −0
Original line number Diff line number Diff line
@@ -5498,6 +5498,19 @@ public final class Settings {
        public static final String STATS_COLLECTION = "stats_collection";


        /**
         * Performance profile
         * @see config_perf_profile_prop in frameworks/base/core/res/res/values/config.xml
         * @hide
         */
        public static final String PERFORMANCE_PROFILE = "performance_profile";

        /**
         * App-based performance profile selection
         * @hide
         */
        public static final String APP_PERFORMANCE_PROFILES_ENABLED = "app_perf_profiles_enabled";

        /**
         * This are the settings to be backed up.
         *
+13 −0
Original line number Diff line number Diff line
@@ -105,4 +105,17 @@
        <item>@string/app_ops_alarm_wakeup</item>
    </string-array>

    <!-- Performance profiles -->
    <string-array name="perf_profile_entries" translatable="false">
        <item>@string/perf_profile_pwrsv</item>
        <item>@string/perf_profile_bal</item>
        <item>@string/perf_profile_perf</item>
    </string-array>

    <string-array name="perf_profile_values" translatable="false">
        <item>0</item>
        <item>1</item>
        <item>2</item>
    </string-array>

</resources>
+6 −0
Original line number Diff line number Diff line
@@ -211,4 +211,10 @@
    <string name="def_wifi_wifihotspot_pass" translatable="false"></string>
    <!-- Default wi-fi direct name -->
    <string name="def_wifi_direct_name" translatable="false"></string>

    <!-- Performance profile modes -->
    <string name="perf_profile_pwrsv">Power save</string>
    <string name="perf_profile_bal">Balanced</string>
    <string name="perf_profile_perf">Performance</string>

</resources>
Loading