Commit 9a3c46d1 authored by Roumen Petrov's avatar Roumen Petrov

allow application to ignore battery optimizations

Requires API Level 23, i.e. Android 6.0 (Marshmallow).

Also refactor management into new class.
parent b8ae0d9e
/*
* Copyright (C) 2019 Roumen Petrov. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.termoneplus.utils;
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import com.termoneplus.Application;
import com.termoneplus.R;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
public class WakeLock {
private static PowerManager.WakeLock lock = null;
public static void create(Context context) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm == null) return;
try {
lock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Application.APP_TAG + ":");
} catch (Exception ignore) {
}
}
public static void release() {
if (lock == null) return;
if (lock.isHeld()) lock.release();
lock = null;
}
public static boolean isHeld() {
return (lock != null) && lock.isHeld();
}
@SuppressLint("WakelockTimeout")
public static void toggle(Context context) {
if (lock == null) return;
if (lock.isHeld()) {
lock.release();
} else {
lock.acquire();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M /*API Level 23*/)
batteryOptimizations(context);
}
}
@RequiresApi(23)
private static void batteryOptimizations(Context context) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm == null) return;
String package_name = context.getPackageName();
if (pm.isIgnoringBatteryOptimizations(package_name)) return;
Resources res = context.getResources();
String app_name = res.getString(R.string.application_terminal);
String msg = res.getString(R.string.ignore_battery_optimizations, app_name);
new AlertDialog.Builder(context)
.setMessage(msg)
.setPositiveButton(android.R.string.yes,
(dialog, which) -> openPowerSettings(context)
)
.setNegativeButton(android.R.string.cancel, null)
.create().show();
}
@RequiresApi(23)
private static void openPowerSettings(Context context) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException ignore) {
}
}
}
......@@ -33,7 +33,6 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
......@@ -63,6 +62,7 @@
import com.termoneplus.WindowListActivity;
import com.termoneplus.WindowListAdapter;
import com.termoneplus.utils.SimpleClipboardManager;
import com.termoneplus.utils.WakeLock;
import com.termoneplus.utils.WrapOpenURL;
import java.io.IOException;
......@@ -107,7 +107,6 @@
private boolean mStopServiceOnFinish = false;
private Intent TSIntent;
private int onResumeSelectWindow = -1;
private PowerManager.WakeLock mWakeLock;
private WifiManager.WifiLock mWifiLock;
private boolean path_collected;
private TermService mTermService;
......@@ -256,8 +255,7 @@ public void onCreate(Bundle icicle) {
Context app = getApplicationContext();
PowerManager pm = (PowerManager) app.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Application.APP_TAG + ":");
WakeLock.create(this);
WifiManager wm = (WifiManager) app.getSystemService(Context.WIFI_SERVICE);
mWifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, Application.APP_TAG);
......@@ -341,9 +339,7 @@ public void onDestroy() {
}
mTermService = null;
mTSConnection = null;
if (mWakeLock.isHeld()) {
mWakeLock.release();
}
WakeLock.release();
if (mWifiLock.isHeld()) {
mWifiLock.release();
}
......@@ -678,7 +674,7 @@ protected void onNewIntent(Intent intent) {
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem wakeLockItem = menu.findItem(R.id.menu_toggle_wakelock);
MenuItem wifiLockItem = menu.findItem(R.id.menu_toggle_wifilock);
if (mWakeLock.isHeld()) {
if (WakeLock.isHeld()) {
wakeLockItem.setTitle(R.string.disable_wakelock);
} else {
wakeLockItem.setTitle(R.string.enable_wakelock);
......@@ -943,11 +939,7 @@ private void doToggleSoftKeyboard() {
}
private void doToggleWakeLock() {
if (mWakeLock.isHeld()) {
mWakeLock.release();
} else {
mWakeLock.acquire();
}
WakeLock.toggle(this);
invalidateOptionsMenu();
}
......
......@@ -49,6 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<string name="enable_wakelock">Прихващане на съживяването</string>
<string name="disable_wakelock">Освобождаване на съживяването</string>
<string name="ignore_battery_optimizations">Разрешаване на \"%1$s\" да пренебрегва оптимизации за батерията?</string>
<string name="enable_wifilock">Прихващане на безжичната мрежа</string>
<string name="disable_wifilock">Освобождаване на безжичната мрежа</string>
......
......@@ -36,6 +36,7 @@
<string name="enable_wakelock">Take WakeLock</string>
<string name="disable_wakelock">Drop WakeLock</string>
<string name="ignore_battery_optimizations">Allow \"%1$s\" to ignore battery optimizations?</string>
<string name="enable_wifilock">Take WifiLock</string>
<string name="disable_wifilock">Drop WifiLock</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment