-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed battery issue, improved VpnService performance
- Loading branch information
1 parent
d2bd505
commit 9fd7a40
Showing
11 changed files
with
246 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
app/src/main/java/org/bepass/oblivion/BatteryOptimization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.bepass.oblivion | ||
|
||
import android.annotation.SuppressLint | ||
import android.app.AlertDialog | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.PowerManager | ||
import android.provider.Settings | ||
import android.view.LayoutInflater | ||
import android.widget.Button | ||
import android.widget.TextView | ||
|
||
/** | ||
* Checks if the app is running in restricted background mode. | ||
* Returns true if running in restricted mode, false otherwise. | ||
*/ | ||
fun isBatteryOptimizationEnabled(context: Context): Boolean { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
val powerManager = context.getSystemService(Context.POWER_SERVICE) as? PowerManager | ||
powerManager?.isIgnoringBatteryOptimizations(context.packageName) == false | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
/** | ||
* Directly requests to ignore battery optimizations for the app. | ||
*/ | ||
@SuppressLint("BatteryLife") | ||
fun requestIgnoreBatteryOptimizations(context: Context) { | ||
val intent = Intent().apply { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS | ||
data = Uri.parse("package:${context.packageName}") | ||
} | ||
} | ||
context.startActivity(intent) | ||
} | ||
|
||
|
||
/** | ||
* Shows a dialog explaining the need for disabling battery optimization and navigates to the app's settings. | ||
*/ | ||
fun showBatteryOptimizationDialog(context: Context) { | ||
val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_battery_optimization, null) | ||
|
||
val dialog = AlertDialog.Builder(context).apply { | ||
setView(dialogView) | ||
}.create() | ||
|
||
dialogView.findViewById<TextView>(R.id.dialog_title).text = context.getString(R.string.batteryOpL) | ||
dialogView.findViewById<TextView>(R.id.dialog_message).text = context.getString(R.string.dialBtText) | ||
|
||
dialogView.findViewById<Button>(R.id.dialog_button_positive).setOnClickListener { | ||
requestIgnoreBatteryOptimizations(context) | ||
dialog.dismiss() | ||
} | ||
|
||
dialogView.findViewById<Button>(R.id.dialog_button_negative).setOnClickListener { | ||
dialog.dismiss() | ||
} | ||
|
||
dialog.show() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.