Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request ignore battery optimizations [permission] #651

Merged
merged 8 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ The app benefits from the following permissions...
|POWER_OFF_ALARM|To wake the device from the power off state.|v0.14.0|
|READ_EXTERNAL_STORAGE|To play alarm sounds located on the SD card.|v0.11.5, v0.13.8 (api≤18)|
|SET_ALARM|To interact with the system AlarmClock app.|v0.1.0|
|REQUEST_IGNORE_BATTERY_OPTIMIZATIONS|To help ensure reliable delivery of alarms.|v0.14.11|
|WRITE_EXTERNAL_STORAGE|To export data (places, themes, etc.) to file.|v0.2.2 (api≤18)|

Version `v0.13.8` removed READ_EXTERNAL_STORAGE for api≥19 (replaced with persistent URI permissions).
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!-- needed by AlarmClock to restore alarms after shutdown/reboot -->

<uses-permission android:name="org.codeaurora.permission.POWER_OFF_ALARM" /> <!-- needed for hardware dependent 'power off' alarms -->


<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> <!-- needed for reliable delivery of alarms and notifications -->

<permission
android:name="suntimes.permission.READ_CALCULATOR"
android:protectionLevel="normal" /> <!-- protects suntimeswidget.calculator.provider -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.app.AlertDialog;
import android.app.KeyguardManager;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
Expand Down Expand Up @@ -1832,14 +1833,50 @@ private static Preference.OnPreferenceClickListener onBatteryOptimizationClicked
return new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (Build.VERSION.SDK_INT >= 23) {
context.startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS));
}
openBatteryOptimizationSettings(context);
return false;
}
};
}

public static void openBatteryOptimizationSettings(final Context context)
{
if (Build.VERSION.SDK_INT >= 23) {
try {
context.startActivity(getRequestIgnoreBatteryOptimizationSettingsIntent(context));
} catch (ActivityNotFoundException e) {
Log.e(LOG_TAG, "Failed to launch battery optimization settings Intent: " + e);
}
}
}
public static void requestIgnoreBatteryOptimization(final Context context)
{
if (Build.VERSION.SDK_INT >= 23) {
try {
context.startActivity( getRequestIgnoreBatteryOptimizationIntent(context));
} catch (ActivityNotFoundException e) {
Log.e(LOG_TAG, "Failed to launch battery optimization request Intent: " + e);
}
}
}

/**
* Recommended; this Intent shows the optimization list (and the user must find and select the app)
*/
@TargetApi(23)
public static Intent getRequestIgnoreBatteryOptimizationSettingsIntent(Context context) {
return new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
}

/**
* This Intent goes directly to the app's optimization settings.
* Requires permission `android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS`
*/
@TargetApi(23)
public static Intent getRequestIgnoreBatteryOptimizationIntent(Context context) {
return new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:" + context.getPackageName()));
}

protected static boolean isDeviceSecure(Context context)
{
KeyguardManager keyguard = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,7 @@ public void onClick(View view) {
{
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= 23) {
startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS));
}
SuntimesSettingsActivity.openBatteryOptimizationSettings(AlarmClockActivity.this);
}
});
batteryOptimizationWarning.show();
Expand Down