Skip to content

Commit

Permalink
1.4.6
Browse files Browse the repository at this point in the history
* Support for Android 15
  • Loading branch information
TylerWilliamson committed Aug 17, 2024
1 parent 1cab0eb commit ce1b283
Show file tree
Hide file tree
Showing 30 changed files with 545 additions and 485 deletions.
52 changes: 31 additions & 21 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
/*
* Copyright 2016 - 2022 Tyler Williamson
* Copyright 2016 - 2024 Tyler Williamson
*
* This file is part of BatteryNotification.
* This file is part of BatteryNotification.
*
* BatteryNotification is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BatteryNotification is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BatteryNotification is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* BatteryNotification is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BatteryNotification. If not, see <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License
* along with BatteryNotification. If not, see <https://www.gnu.org/licenses/>.
*/

apply plugin: "com.android.application"

android {
compileSdkVersion 33
namespace "com.ominous.batterynotification"
compileSdk 35
defaultConfig {
applicationId "com.ominous.batterynotification"
minSdkVersion 17
targetSdkVersion 33
versionCode 17
versionName "1.4.5"
targetSdkVersion 35
versionCode 18
versionName "1.4.6"
}
buildTypes {
debug {
applicationIdSuffix ".debug"
versionNameSuffix "-DEBUG"
}
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
}
Expand All @@ -49,13 +51,21 @@ android {
storeFile file('../debug.keystore')
}
}
buildFeatures {
buildConfig = true
}
}

dependencies {
implementation "androidx.activity:activity:1.5.1"
implementation "androidx.lifecycle:lifecycle-viewmodel:2.5.1" //Required for androidx.preference
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1" //Required for androidx.preference
implementation "androidx.preference:preference:1.2.0"
implementation "androidx.activity:activity:1.8.2" //Latest version that targets API version 17
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.20") {
because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib")
}
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.20") {
because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
}

implementation "androidx.preference:preference:1.2.1"
//We're going deeper
implementation "com.github.tiann:FreeReflection:3.1.0"
implementation "com.github.tiann:FreeReflection:3.2.0"
}
38 changes: 23 additions & 15 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2016 - 2022 Tyler Williamson
~ Copyright 2016 - 2024 Tyler Williamson
~
~ This file is part of BatteryNotification.
~ This file is part of BatteryNotification.
~
~ BatteryNotification is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~ BatteryNotification is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ BatteryNotification is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~ BatteryNotification is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with BatteryNotification. If not, see <https://www.gnu.org/licenses/>.
~ You should have received a copy of the GNU General Public License
~ along with BatteryNotification. If not, see <https://www.gnu.org/licenses/>.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
Expand All @@ -29,17 +29,20 @@
tools:ignore="ProtectedPermissions" />

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />

<!-- We only need this for API 28+ -->
<uses-sdk tools:overrideLibrary="me.weishu.freereflection" />

<application
android:name=".application.BatteryNotification"
android:enableOnBackInvokedCallback="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_scheme"
android:fullBackupContent="@xml/backup_scheme"
android:hardwareAccelerated="false"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
Expand All @@ -54,8 +57,11 @@
</activity>
<service
android:name=".service.BatteryService"
android:exported="false" />

android:exported="false"
android:foregroundServiceType="specialUse">
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="This service is used to immediately update a persistent notification with battery information broadcast with the ACTION_BATTERY_CHANGED intent. This intent cannot be received through components declared in manifests, only by explicitly registering for it. The user has to explicitly enable a setting in order for this service to be enabled."/>
</service>
<receiver
android:name=".receiver.BatteryBroadcastReceiver"
android:enabled="true"
Expand All @@ -67,6 +73,8 @@
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<action android:name="android.intent.action.MY_PACKAGE_UNSUSPENDED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.ominous.batterynotification.UPDATE_ACTION" />
</intent-filter>
</receiver>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
* Copyright 2016 - 2022 Tyler Williamson
* Copyright 2016 - 2024 Tyler Williamson
*
* This file is part of BatteryNotification.
* This file is part of BatteryNotification.
*
* BatteryNotification is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BatteryNotification is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BatteryNotification is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* BatteryNotification is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BatteryNotification. If not, see <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License
* along with BatteryNotification. If not, see <https://www.gnu.org/licenses/>.
*/

package com.ominous.batterynotification.activity;
Expand Down Expand Up @@ -79,14 +79,23 @@ public void onCreate(Bundle savedInstanceState) {
}

public static class SettingsFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener {
private static final int RESULT_SUCCESS = 1, RESULT_FAIL_SU = 2, RESULT_FAIL_PERMISSION = 3, RESULT_FAIL_UNKNOWN = 4;
private SwitchPreference timeRemainingPreference, notificationPreference;
private TextDialog timeRemainingFailureDialog, adbInstructionsDialog, foregroundServiceDialog;
private SwitchPreference timeRemainingPreference;
private SwitchPreference notificationPreference;
private SwitchPreference updateImmediatelyPreference;
private SwitchPreference fahrenheitPreference;

private TextDialog timeRemainingFailureDialog;
private TextDialog adbInstructionsDialog;
private TextDialog foregroundServiceDialog;

private final ActivityResultLauncher<String> requestNotificationPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), r -> {
if (r) {
startNotification(getContext());

updateImmediatelyPreference.setEnabled(true);
fahrenheitPreference.setEnabled(true);
timeRemainingPreference.setEnabled(true);
} else {
notificationPreference.setChecked(false);
}
Expand Down Expand Up @@ -117,18 +126,24 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
getPreferenceManager().setSharedPreferencesName(getString(R.string.preference_filename));
addPreferencesFromResource(R.xml.settings);

timeRemainingPreference = setUpSwitchPreference(getString(R.string.preference_time_remaining));
notificationPreference = setUpSwitchPreference(getString(R.string.preference_notification));
SwitchPreference updateImmediatelyPreference = setUpSwitchPreference(getString(R.string.preference_immediate));
fahrenheitPreference = setUpSwitchPreference(getString(R.string.preference_fahrenheit));
updateImmediatelyPreference = setUpSwitchPreference(getString(R.string.preference_immediate));
timeRemainingPreference = setUpSwitchPreference(getString(R.string.preference_time_remaining));
Preference openNotificationSettings = setUpPreference(getString(R.string.preference_notification_settings));
setUpSwitchPreference(getString(R.string.preference_fahrenheit));

if (Build.VERSION.SDK_INT < 21) {
openNotificationSettings.setEnabled(false);
timeRemainingPreference.setEnabled(false);
timeRemainingPreference.setChecked(false);
}

if (!notificationPreference.isChecked()) {
updateImmediatelyPreference.setEnabled(false);
fahrenheitPreference.setEnabled(false);
timeRemainingPreference.setEnabled(false);
}

Context context = getContext();

if (context != null) {
Expand Down Expand Up @@ -163,15 +178,21 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {

if (context != null) {
if (preferenceKey.equals(getString(R.string.preference_notification))) {
//TODO disable the other preferences if disabled
if (NotificationUtils.canShowNotifications(getContext())) {
if (enabled) {
if (enabled) {
if (NotificationUtils.canShowNotifications(getContext())) {
startNotification(context);

updateImmediatelyPreference.setEnabled(true);
fahrenheitPreference.setEnabled(true);
timeRemainingPreference.setEnabled(true);
} else {
NotificationUtils.cancelBatteryNotification(context);
requestNotificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
}
} else {
requestNotificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
NotificationUtils.cancelBatteryNotification(context);
updateImmediatelyPreference.setEnabled(false);
fahrenheitPreference.setEnabled(false);
timeRemainingPreference.setEnabled(false);
}
} else if (preferenceKey.equals(getString(R.string.preference_immediate))) {
Intent batteryServiceIntent = new Intent(context, BatteryService.class);
Expand Down Expand Up @@ -207,6 +228,16 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
return true;
}

@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
if (Build.VERSION.SDK_INT > 21
&& preference.getKey().equals(getString(R.string.preference_notification_settings))) {
openNotificationSettings();
}

return true;
}

private void openNotificationSettings() {
Context context = getContext();

Expand All @@ -225,16 +256,6 @@ private void openNotificationSettings() {
}
}

@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
if (Build.VERSION.SDK_INT > 21
&& preference.getKey().equals(getString(R.string.preference_notification_settings))) {
openNotificationSettings();
}

return true;
}

private void obtainPermission() {
final FragmentActivity activity = getActivity();

Expand All @@ -243,30 +264,29 @@ private void obtainPermission() {
timeRemainingPreference.setChecked(true);
} else {
Executors.newCachedThreadPool().submit(() -> {
final int result;
final boolean successful;
final int messageRes;

switch (executeSuCommand(getString(R.string.format_command, activity.getPackageName(), PERMISSION_BATTERY_STATS))) {
case 0:
boolean hasPermission = hasPermission(activity);
result = hasPermission ? RESULT_SUCCESS : RESULT_FAIL_PERMISSION;
messageRes = hasPermission ? R.string.message_permission_granted : R.string.message_permission_failure;
successful = hasPermission(activity);
messageRes = successful ? R.string.message_permission_granted : R.string.message_permission_failure;
break;
case 1:
result = RESULT_FAIL_SU;
successful = false;
messageRes = R.string.message_root_failure;
break;
case 255:
result = RESULT_FAIL_PERMISSION;
successful = false;
messageRes = R.string.message_permission_failure;
break;
default:
result = RESULT_FAIL_UNKNOWN;
successful = false;
messageRes = R.string.message_unknown_error;
}

activity.runOnUiThread(() -> {
if (result == RESULT_SUCCESS) {
if (successful) {
Log.v(TAG, getString(messageRes));

timeRemainingPreference.setChecked(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
* Copyright 2016 - 2022 Tyler Williamson
* Copyright 2016 - 2024 Tyler Williamson
*
* This file is part of BatteryNotification.
* This file is part of BatteryNotification.
*
* BatteryNotification is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BatteryNotification is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BatteryNotification is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* BatteryNotification is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BatteryNotification. If not, see <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License
* along with BatteryNotification. If not, see <https://www.gnu.org/licenses/>.
*/

package com.ominous.batterynotification.application;
Expand Down
Loading

0 comments on commit ce1b283

Please sign in to comment.