1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-26 04:48:51 +00:00

Can request to disable battery optimisation

This commit is contained in:
Pierre HUBERT 2021-04-13 18:44:06 +02:00
parent 1400bbbeb7
commit e44a8add98
3 changed files with 46 additions and 13 deletions

View File

@ -8,13 +8,14 @@
<!-- These permissions are required to make video calls (WebRTC) -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<!-- This is required on Android 11+ for image picker -->
<!-- This is required on Android 11+ for image picker -->
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
@ -25,29 +26,32 @@
</intent>
</queries>
<!-- This is required for independent push notifications service to work
(when FCM service can not be used) -->
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:label="Comunic"
android:icon="@mipmap/ic_launcher"
android:label="Comunic"
android:roundIcon="@mipmap/ic_launcher_round"
tools:ignore="GoogleAppIndexingWarning">
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
@ -56,8 +60,7 @@
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
android:resource="@style/NormalTheme" />
<!-- Specify that the launch screen should continue being displayed -->
<!-- until Flutter renders its first frame. -->
@ -66,8 +69,8 @@
android:resource="@drawable/launch_background" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
@ -75,6 +78,6 @@
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
</application>
</manifest>

View File

@ -18,6 +18,9 @@ public class MainActivity extends FlutterActivity {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), INDEPENDENT_NOTIFICATIONS_CHANNEL)
.setMethodCallHandler(new NotificationsChannel());
.setMethodCallHandler(new NotificationsChannel(this));
}
}

View File

@ -1,15 +1,30 @@
package org.communiquons.comunic.independentnotifications;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.PowerManager;
import android.provider.Settings;
import androidx.annotation.NonNull;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class NotificationsChannel implements MethodChannel.MethodCallHandler {
private final Context context;
public NotificationsChannel(Activity context) {
this.context = context.getApplicationContext();
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
try {
if (call.method.equals("preConfigure"))
preConfigure(result);
@ -25,7 +40,19 @@ public class NotificationsChannel implements MethodChannel.MethodCallHandler {
/**
* Pre-configure notifications service
*/
private static void preConfigure(@NonNull MethodChannel.Result result) throws Exception {
System.out.println("Ready to pre-configure !");
private void preConfigure(@NonNull MethodChannel.Result result) throws Exception {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (!context.getSystemService(PowerManager.class).isIgnoringBatteryOptimizations(context.getPackageName())) {
@SuppressLint("BatteryLife")
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
Uri.parse("package:" + context.getPackageName()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
result.success(null);
}
}