Skip to content

Commit

Permalink
Add support for Android 12 and later
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Jul 4, 2024
1 parent db827b9 commit ea9cdc0
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/debug_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: Build with Gradle
run: ./gradlew packageDebugUniversalApk
- name: Rename the APK file
run: mv ./app/build/outputs/universal_apk/debug/app-debug-universal.apk ./${{ env.APP_NAME }}.apk
run: mv ./app/build/outputs/apk_from_bundle/debug/app-debug-universal.apk ./${{ env.APP_NAME }}.apk
- name: Store generated APK file
uses: actions/upload-artifact@v1
with:
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
android:label="@string/control_battery_charge_permission_label"
android:description="@string/control_battery_charge_permission_description" />

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!--<uses-permission android:name="android.permission.BATTERY_STATS"/>-->
<uses-permission android:name="android.permission.RESET_BATTERY_STATS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:allowBackup="false"
Expand Down Expand Up @@ -62,14 +63,14 @@

<receiver
android:name=".receivers.BootReceiver"
android:exported="true">
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".receivers.PowerConnectionReceiver"
android:exported="true">
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
Expand All @@ -87,7 +88,7 @@
</receiver>
<receiver
android:name=".EnableWidget"
android:exported="true">
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="${applicationId}.action.TOGGLE" />
Expand All @@ -100,6 +101,7 @@

<service
android:name=".ForegroundService"
android:foregroundServiceType="dataSync"
android:exported="false" />

</application>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.github.muntashirakon.bcl

import android.Manifest
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.media.RingtoneManager
import android.os.Build
import android.os.IBinder
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
Expand Down Expand Up @@ -115,6 +118,10 @@ class ForegroundService : Service() {
}

fun updateNotification() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// No permission
return
}
notificationManager.notify(notifyID, mNotifyBuilder.build())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class LimitChangeActivity : AppCompatActivity() {

// handle data sent by intent
val batteryLimitMime = this.getString(R.string.mime_battery_limit)
val intent = intent
if (Intent.ACTION_SEND == intent.action && batteryLimitMime == intent.type) {
Utils.handleLimitChange(this, intent.extras?.get(Intent.EXTRA_TEXT))
} else {
Expand Down
28 changes: 15 additions & 13 deletions app/src/main/java/io/github/muntashirakon/bcl/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.muntashirakon.bcl

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
Expand All @@ -14,6 +15,7 @@ import android.view.View
import android.widget.Toast
import androidx.annotation.WorkerThread
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.preference.PreferenceManager
Expand Down Expand Up @@ -246,20 +248,19 @@ object Utils {
}

fun startServiceIfLimitEnabled(context: Context) {
if (getSettings(context).getBoolean(CHARGE_LIMIT_ENABLED, false)) {
if (getPrefs(context).getBoolean(PrefsFragment.KEY_DISABLE_AUTO_RECHARGE, false)) {
changeState(context, CHARGE_ON)
}
Handler(Looper.getMainLooper()).postDelayed({
if (isPhonePluggedIn(context)) {
context.startService(Intent(context, ForegroundService::class.java))
// display service enabled Toast message if not disabled in settings
if (!getPrefs(context).getBoolean("hide_toast_on_service_changes", false)) {
Toast.makeText(context, R.string.service_enabled, Toast.LENGTH_SHORT).show()
}
}
}, CHARGING_CHANGE_TOLERANCE_MS)
if (!getSettings(context).getBoolean(CHARGE_LIMIT_ENABLED, false)) {
return
}
if (getPrefs(context).getBoolean(PrefsFragment.KEY_DISABLE_AUTO_RECHARGE, false)) {
changeState(context, CHARGE_ON)
}
Handler(Looper.getMainLooper()).postDelayed({
ContextCompat.startForegroundService(context, Intent(context, ForegroundService::class.java))
// display service enabled Toast message if not disabled in settings
if (!getPrefs(context).getBoolean("hide_toast_on_service_changes", false)) {
Toast.makeText(context, R.string.service_enabled, Toast.LENGTH_SHORT).show()
}
}, CHARGING_CHANGE_TOLERANCE_MS)
}

fun getPrefs(context: Context): SharedPreferences {
Expand Down Expand Up @@ -419,6 +420,7 @@ object Utils {
}

// Copied from App Manager
@SuppressLint("RestrictedApi")
@Suppress("DEPRECATION")
fun applyWindowInsetsAsPaddingNoTop(v: View) {
doOnApplyWindowInsets(v) { view, insets, initialPadding ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.github.muntashirakon.bcl.activities

import android.Manifest
import android.content.*
import android.content.pm.PackageManager
import android.os.*
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.*
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.google.android.material.switchmaterial.SwitchMaterial
Expand All @@ -33,6 +37,11 @@ class MainFragment: Fragment() {
private lateinit var currentThreshold: String
private val mHandler = MainHandler(this)
private var prefs: SharedPreferences? = null
private val notificationPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
if (granted) {
Utils.startServiceIfLimitEnabled(requireContext())
} else requireActivity().finishAndRemoveTask()
}

private class MainHandler(fragment: MainFragment) : Handler(Looper.getMainLooper()) {
private val mFragment by lazy(LazyThreadSafetyMode.NONE) { WeakReference(fragment) }
Expand Down Expand Up @@ -101,12 +110,6 @@ class MainFragment: Fragment() {
}
}

val isChargeLimitEnabled = settings?.getBoolean(Constants.CHARGE_LIMIT_ENABLED, false)

if (isChargeLimitEnabled == true && Utils.isPhonePluggedIn(requireContext())) {
context?.startService(Intent(requireContext(), ForegroundService::class.java))
}

val resetBatteryStatsButton = view.findViewById<Button>(R.id.reset_battery_stats)
// val autoResetSwitch = view.findViewById(R.id.auto_stats_reset) as CheckBox
// val notificationSound = view.findViewById(R.id.notification_sound) as CheckBox
Expand Down Expand Up @@ -145,6 +148,12 @@ class MainFragment: Fragment() {
// settings.edit().putBoolean(NOTIFICATION_SOUND, isChecked).apply() }

setStatusCTRLFileData()

if (ActivityCompat.checkSelfPermission(requireContext(), Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
notificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
}

override fun onAttach(context: Context) {
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-ta/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
<string name="intent_invalid">வழங்கப்பட்ட நோக்கம் தவறானது.</string>
<string name="intent_limit_invalid">வழங்கப்பட்ட வரம்பு தவறானது.</string>
<string name="intent_limit_accepted">பேட்டரி சார்ஜிங் வரம்பு %1$d%% ஆக மாற்றப்பட்டது.</string>
<string name="mime_battery_limit" translatable="false">text/x-battery-limit</string>

<string name="service_enabled"> பேட்டரி வரம்பு சேவை இயக்கப்பட்டது! </string>
<string name="service_disabled"> பேட்டரி வரம்பு சேவை முடக்கப்பட்டுள்ளது! </string>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
<string name="intent_invalid">提供的 intend 无效。</string>
<string name="intent_limit_invalid">提供的限制无效。</string>
<string name="intent_limit_accepted">电池充电限制更改为 %1$d%%。</string>
<string name="mime_battery_limit" translatable="false">text/x-battery-limit</string>

<string name="service_enabled"> 电池限制服务已启用! </string>
<string name="service_disabled"> 电池限制服务已禁用! </string>
Expand Down

0 comments on commit ea9cdc0

Please sign in to comment.