Java Code Examples for android.os.PowerManager#isPowerSaveMode()

The following examples show how to use android.os.PowerManager#isPowerSaveMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BatteryInfoManager.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
protected BatteryInfoManager(Context context, XSharedPreferences prefs) {
    mContext = context;
    mBatteryData = new BatteryData();
    mListeners = new ArrayList<BatteryStatusListener>();
    mSounds = new Uri[4];
    mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
    mBatteryData.isPowerSaving = mPowerManager.isPowerSaveMode();

    setSound(BatteryInfoManager.SOUND_CHARGED,
            prefs.getString(GravityBoxSettings.PREF_KEY_BATTERY_CHARGED_SOUND, ""));
    setSound(BatteryInfoManager.SOUND_PLUGGED,
            prefs.getString(GravityBoxSettings.PREF_KEY_CHARGER_PLUGGED_SOUND, ""));
    setSound(BatteryInfoManager.SOUND_UNPLUGGED,
            prefs.getString(GravityBoxSettings.PREF_KEY_CHARGER_UNPLUGGED_SOUND, ""));

    try {
        mLowBatteryWarningPolicy = LowBatteryWarningPolicy.valueOf(prefs.getString(
            GravityBoxSettings.PREF_KEY_LOW_BATTERY_WARNING_POLICY, "DEFAULT"));
    } catch (Throwable t) {
        mLowBatteryWarningPolicy = LowBatteryWarningPolicy.DEFAULT;
    }
}
 
Example 2
Source File: Checks.java    From SEAL-Demo with MIT License 5 votes vote down vote up
/**
 * @param context the activity context
 * @return true if the power save mode is on, false otherwise
 */
public static boolean isPowerSaveMode(Context context) {
    PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (powerManager != null && powerManager.isPowerSaveMode())
        return true;
    return false;
}
 
Example 3
Source File: EqualizerView.java    From YTPlayer with GNU General Public License v3.0 5 votes vote down vote up
public boolean isInBatterySaveMode() {
    PowerManager powerManager = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
            powerManager.isPowerSaveMode()) {
        return true;
    }

    return false;
}
 
Example 4
Source File: Util.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
public static float getAnimatorSpeed(Context context) {
    PowerManager powerManager = (PowerManager)
            context.getSystemService(Context.POWER_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && powerManager.isPowerSaveMode()) {
        // Animations are disabled in power save mode, so just show a toast instead.
        return 0.0f;
    }
    return android.provider.Settings.Global.getFloat(context.getContentResolver(),
            android.provider.Settings.Global.ANIMATOR_DURATION_SCALE, 1.0f);
}
 
Example 5
Source File: BatteryUtils.java    From TowerCollector with Mozilla Public License 2.0 5 votes vote down vote up
public static boolean isPowerSaveModeEnabled(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return false;
    }
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (pm != null && pm.isPowerSaveMode()) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            return (pm.getLocationPowerSaveMode() != PowerManager.LOCATION_MODE_NO_CHANGE);
        }
        return true;
    }
    return false;
}
 
Example 6
Source File: SettingsInfo.java    From batteryhub with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if PowerSave Mode is enabled on the device
 *
 * @return True if PowerSave Mode is enabled
 */
public static boolean isPowerSaveEnabled(final Context context) {
    PowerManager manager = (PowerManager)
            context.getSystemService(Context.POWER_SERVICE);

    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && manager.isPowerSaveMode();
}
 
Example 7
Source File: RNDeviceModule.java    From react-native-device-info with MIT License 5 votes vote down vote up
private WritableMap getPowerStateFromIntent (Intent intent) {
  if(intent == null) {
    return null;
  }

  int batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
  int batteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
  int isPlugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
  int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

  float batteryPercentage = batteryLevel / (float)batteryScale;

  String batteryState = "unknown";

  if(isPlugged == 0) {
    batteryState = "unplugged";
  } else if(status == BATTERY_STATUS_CHARGING) {
    batteryState = "charging";
  } else if(status == BATTERY_STATUS_FULL) {
    batteryState = "full";
  }

  PowerManager powerManager = (PowerManager)getReactApplicationContext().getSystemService(Context.POWER_SERVICE);
  boolean powerSaveMode = false;
  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
    powerSaveMode = powerManager.isPowerSaveMode();
  }

  WritableMap powerState = Arguments.createMap();
  powerState.putString(BATTERY_STATE, batteryState);
  powerState.putDouble(BATTERY_LEVEL, batteryPercentage);
  powerState.putBoolean(LOW_POWER_MODE, powerSaveMode);

  return powerState;
}
 
Example 8
Source File: RunConditionMonitor.java    From syncthing-android with Mozilla Public License 2.0 5 votes vote down vote up
@TargetApi(21)
private boolean isPowerSaving() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        Log.e(TAG, "isPowerSaving may not be called on pre-lollipop android versions.");
        return false;
    }
    PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
    if (powerManager == null) {
        Log.e(TAG, "getSystemService(POWER_SERVICE) unexpectedly returned NULL.");
        return false;
    }
    return powerManager.isPowerSaveMode();
}
 
Example 9
Source File: AndroidUtilities.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public static boolean shouldEnableAnimation() {
    if (Build.VERSION.SDK_INT < 26 || Build.VERSION.SDK_INT >= 28) {
        return true;
    }
    PowerManager powerManager = (PowerManager) ApplicationLoader.applicationContext.getSystemService(Context.POWER_SERVICE);
    if (powerManager.isPowerSaveMode()) {
        return false;
    }
    float scale = Settings.Global.getFloat(ApplicationLoader.applicationContext.getContentResolver(), Settings.Global.ANIMATOR_DURATION_SCALE, 1.0f);
    if (scale <= 0.0f) {
        return false;
    }
    return true;
}
 
Example 10
Source File: AndroidUtilities.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public static boolean shouldEnableAnimation() {
    if (Build.VERSION.SDK_INT < 26 || Build.VERSION.SDK_INT >= 28) {
        return true;
    }
    PowerManager powerManager = (PowerManager) ApplicationLoader.applicationContext.getSystemService(Context.POWER_SERVICE);
    if (powerManager.isPowerSaveMode()) {
        return false;
    }
    float scale = Settings.Global.getFloat(ApplicationLoader.applicationContext.getContentResolver(), Settings.Global.ANIMATOR_DURATION_SCALE, 1.0f);
    if (scale <= 0.0f) {
        return false;
    }
    return true;
}
 
Example 11
Source File: Utilities.java    From LaunchEnr with GNU General Public License v3.0 4 votes vote down vote up
static boolean isPowerSaverOn(Context context) {
    PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    return powerManager.isPowerSaveMode();
}
 
Example 12
Source File: DataWrapper.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
static boolean isPowerSaveMode(Context context) {

        /*String applicationPowerSaveModeInternal = ApplicationPreferences.applicationPowerSaveModeInternal;

        if (applicationPowerSaveModeInternal.equals("1") || applicationPowerSaveModeInternal.equals("2")) {
            Intent batteryStatus = null;
            try { // Huawei devices: java.lang.IllegalArgumentException: registered too many Broadcast Receivers
                IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
                batteryStatus = context.registerReceiver(null, filter);
            } catch (Exception ignored) {
            }
            if (batteryStatus != null) {
                boolean isCharging;
                int batteryPct;

                //int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
                //PPApplication.logE("DataWrapper.isPowerSaveMode", "status=" + status);
                int plugged = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
                isCharging = plugged == BatteryManager.BATTERY_PLUGGED_AC
                        || plugged == BatteryManager.BATTERY_PLUGGED_USB
                        || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
                //isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                //             status == BatteryManager.BATTERY_STATUS_FULL;
                //PPApplication.logE("DataWrapper.isPowerSaveMode", "isCharging=" + isCharging);
                if (!isCharging) {
                    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                    int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                    //if (PPApplication.logEnabled()) {
                    //    PPApplication.logE("DataWrapper.isPowerSaveMode", "level=" + level);
                    //    PPApplication.logE("DataWrapper.isPowerSaveMode", "scale=" + scale);
                    //}

                    batteryPct = Math.round(level / (float) scale * 100);
                    //PPApplication.logE("DataWrapper.isPowerSaveMode", "batteryPct=" + batteryPct);

                    if (applicationPowerSaveModeInternal.equals("1") && (batteryPct <= 5))
                        return true;
                    if (applicationPowerSaveModeInternal.equals("2") && (batteryPct <= 15))
                        return true;
                }
            }
        }
        else
        if (applicationPowerSaveModeInternal.equals("3")) {*/
            //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                if (powerManager != null)
                    return powerManager.isPowerSaveMode();
            //}
            //return isPowerSaveMode;
        //}

        return false;
    }
 
Example 13
Source File: ActivityServiceUtils.java    From HgLauncher with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Checks if the system is currently in battery saver mode.
 *
 * @param activity The activity where getSystemService can be received.
 *
 * @return false if battery saver is not enabled.
 */
public static boolean isPowerSaving(Activity activity) {
    PowerManager powerManager = (PowerManager) activity
            .getSystemService(Context.POWER_SERVICE);
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && powerManager != null && powerManager.isPowerSaveMode();
}
 
Example 14
Source File: ActivityServiceUtils.java    From HgLauncher with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Checks if the system is currently in battery saver mode.
 *
 * @param activity The activity where getSystemService can be received.
 *
 * @return false if battery saver is not enabled.
 */
public static boolean isPowerSaving(Activity activity) {
    PowerManager powerManager = (PowerManager) activity
            .getSystemService(Context.POWER_SERVICE);
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && powerManager != null && powerManager.isPowerSaveMode();
}