feat(android,interface): Handle battery optimisation
This commit is contained in:
parent
497ac279cc
commit
0d4f0c59cc
@ -271,6 +271,21 @@ class MyHomePageState extends State<MyHomePage> {
|
||||
},
|
||||
child: const Text('Get cache directory'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
// ignore: avoid_print
|
||||
print(await MoxplatformPlugin.platform
|
||||
.isIgnoringBatteryOptimizations());
|
||||
},
|
||||
child: const Text('Is battery optimised?'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
await MoxplatformPlugin.platform
|
||||
.openBatteryOptimisationSettings();
|
||||
},
|
||||
child: const Text('Open battery optimisation page'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
@ -4,12 +4,13 @@
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||
|
||||
|
||||
<application>
|
||||
<provider
|
||||
android:name="me.polynom.moxplatform_android.MoxplatformFileProvider"
|
||||
android:authorities="me.polynom.moxplatform_android.fileprovider"
|
||||
android:authorities="me.polynom.moxplatform_android.fileprovider2"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
|
@ -1084,6 +1084,11 @@ public class Api {
|
||||
|
||||
@NonNull
|
||||
String getCacheDataPath();
|
||||
|
||||
void openBatteryOptimisationSettings();
|
||||
|
||||
@NonNull
|
||||
Boolean isIgnoringBatteryOptimizations();
|
||||
/** Contacts APIs */
|
||||
void recordSentMessage(@NonNull String name, @NonNull String jid, @Nullable String avatarPath, @NonNull FallbackIconType fallbackIcon);
|
||||
/** Cryptography APIs */
|
||||
@ -1282,6 +1287,50 @@ public class Api {
|
||||
String output = api.getCacheDataPath();
|
||||
wrapped.add(0, output);
|
||||
}
|
||||
catch (Throwable exception) {
|
||||
ArrayList<Object> wrappedError = wrapError(exception);
|
||||
wrapped = wrappedError;
|
||||
}
|
||||
reply.reply(wrapped);
|
||||
});
|
||||
} else {
|
||||
channel.setMessageHandler(null);
|
||||
}
|
||||
}
|
||||
{
|
||||
BasicMessageChannel<Object> channel =
|
||||
new BasicMessageChannel<>(
|
||||
binaryMessenger, "dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.openBatteryOptimisationSettings", getCodec());
|
||||
if (api != null) {
|
||||
channel.setMessageHandler(
|
||||
(message, reply) -> {
|
||||
ArrayList<Object> wrapped = new ArrayList<Object>();
|
||||
try {
|
||||
api.openBatteryOptimisationSettings();
|
||||
wrapped.add(0, null);
|
||||
}
|
||||
catch (Throwable exception) {
|
||||
ArrayList<Object> wrappedError = wrapError(exception);
|
||||
wrapped = wrappedError;
|
||||
}
|
||||
reply.reply(wrapped);
|
||||
});
|
||||
} else {
|
||||
channel.setMessageHandler(null);
|
||||
}
|
||||
}
|
||||
{
|
||||
BasicMessageChannel<Object> channel =
|
||||
new BasicMessageChannel<>(
|
||||
binaryMessenger, "dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.isIgnoringBatteryOptimizations", getCodec());
|
||||
if (api != null) {
|
||||
channel.setMessageHandler(
|
||||
(message, reply) -> {
|
||||
ArrayList<Object> wrapped = new ArrayList<Object>();
|
||||
try {
|
||||
Boolean output = api.isIgnoringBatteryOptimizations();
|
||||
wrapped.add(0, output);
|
||||
}
|
||||
catch (Throwable exception) {
|
||||
ArrayList<Object> wrappedError = wrapError(exception);
|
||||
wrapped = wrappedError;
|
||||
|
@ -25,7 +25,7 @@ const val NOTIFICATION_EXTRA_ID_KEY = "notification_id"
|
||||
const val NOTIFICATION_MESSAGE_EXTRA_MIME = "mime"
|
||||
const val NOTIFICATION_MESSAGE_EXTRA_PATH = "path"
|
||||
|
||||
const val MOXPLATFORM_FILEPROVIDER_ID = "me.polynom.moxplatform_android.fileprovider"
|
||||
const val MOXPLATFORM_FILEPROVIDER_ID = "me.polynom.moxplatform_android.fileprovider2"
|
||||
|
||||
// Shared preferences keys
|
||||
const val SHARED_PREFERENCES_KEY = "me.polynom.moxplatform_android"
|
||||
|
@ -1,6 +1,8 @@
|
||||
package me.polynom.moxplatform_android;
|
||||
|
||||
import static android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS;
|
||||
import static androidx.core.content.ContextCompat.getSystemService;
|
||||
import static androidx.core.content.ContextCompat.startActivity;
|
||||
import static me.polynom.moxplatform_android.ConstantsKt.SHARED_PREFERENCES_KEY;
|
||||
import static me.polynom.moxplatform_android.CryptoKt.*;
|
||||
import static me.polynom.moxplatform_android.RecordSentMessageKt.*;
|
||||
@ -15,6 +17,8 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
import android.os.PowerManager;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@ -257,6 +261,22 @@ public class MoxplatformAndroidPlugin extends BroadcastReceiver implements Flutt
|
||||
return context.getCacheDir().getPath();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openBatteryOptimisationSettings() {
|
||||
final Uri packageUri = Uri.parse("package:" + context.getPackageName());
|
||||
Log.d(TAG, packageUri.toString());
|
||||
final Intent intent = new Intent(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, packageUri);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Boolean isIgnoringBatteryOptimizations() {
|
||||
final PowerManager pm = context.getSystemService(PowerManager.class);
|
||||
return pm.isIgnoringBatteryOptimizations(context.getPackageName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recordSentMessage(@NonNull String name, @NonNull String jid, @Nullable String avatarPath, @NonNull FallbackIconType fallbackIcon) {
|
||||
systemRecordSentMessage(context, name, jid, avatarPath, fallbackIcon);
|
||||
|
@ -10,4 +10,14 @@ class AndroidPlatformImplementation extends PlatformImplementation {
|
||||
Future<String> getPersistentDataPath() {
|
||||
return MoxplatformInterface.api.getPersistentDataPath();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> isIgnoringBatteryOptimizations() {
|
||||
return MoxplatformInterface.api.isIgnoringBatteryOptimizations();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> openBatteryOptimisationSettings() {
|
||||
return MoxplatformInterface.api.openBatteryOptimisationSettings();
|
||||
}
|
||||
}
|
||||
|
@ -583,6 +583,55 @@ class MoxplatformApi {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> openBatteryOptimisationSettings() async {
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.openBatteryOptimisationSettings',
|
||||
codec,
|
||||
binaryMessenger: _binaryMessenger);
|
||||
final List<Object?>? replyList = await channel.send(null) as List<Object?>?;
|
||||
if (replyList == null) {
|
||||
throw PlatformException(
|
||||
code: 'channel-error',
|
||||
message: 'Unable to establish connection on channel.',
|
||||
);
|
||||
} else if (replyList.length > 1) {
|
||||
throw PlatformException(
|
||||
code: replyList[0]! as String,
|
||||
message: replyList[1] as String?,
|
||||
details: replyList[2],
|
||||
);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> isIgnoringBatteryOptimizations() async {
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.moxplatform_platform_interface.MoxplatformApi.isIgnoringBatteryOptimizations',
|
||||
codec,
|
||||
binaryMessenger: _binaryMessenger);
|
||||
final List<Object?>? replyList = await channel.send(null) as List<Object?>?;
|
||||
if (replyList == null) {
|
||||
throw PlatformException(
|
||||
code: 'channel-error',
|
||||
message: 'Unable to establish connection on channel.',
|
||||
);
|
||||
} else if (replyList.length > 1) {
|
||||
throw PlatformException(
|
||||
code: replyList[0]! as String,
|
||||
message: replyList[1] as String?,
|
||||
details: replyList[2],
|
||||
);
|
||||
} else if (replyList[0] == null) {
|
||||
throw PlatformException(
|
||||
code: 'null-error',
|
||||
message: 'Host platform returned null value for non-null return value.',
|
||||
);
|
||||
} else {
|
||||
return (replyList[0] as bool?)!;
|
||||
}
|
||||
}
|
||||
|
||||
/// Contacts APIs
|
||||
Future<void> recordSentMessage(String arg_name, String arg_jid,
|
||||
String? arg_avatarPath, FallbackIconType arg_fallbackIcon) async {
|
||||
|
@ -4,4 +4,12 @@ abstract class PlatformImplementation {
|
||||
|
||||
/// Returns the path where cache data should be stored.
|
||||
Future<String> getCacheDataPath();
|
||||
|
||||
/// Returns whether the app is battery-optimised (false) or
|
||||
/// excluded from battery savings (true).
|
||||
Future<bool> isIgnoringBatteryOptimizations();
|
||||
|
||||
/// Opens the page for battery optimisations. If not supported on the
|
||||
/// platform, does nothing.
|
||||
Future<void> openBatteryOptimisationSettings();
|
||||
}
|
||||
|
@ -8,4 +8,10 @@ class StubPlatformImplementation extends PlatformImplementation {
|
||||
/// Returns the path where cache data should be stored.
|
||||
@override
|
||||
Future<String> getCacheDataPath() async => '';
|
||||
|
||||
@override
|
||||
Future<bool> isIgnoringBatteryOptimizations() async => false;
|
||||
|
||||
@override
|
||||
Future<void> openBatteryOptimisationSettings() async {}
|
||||
}
|
||||
|
@ -181,6 +181,8 @@ abstract class MoxplatformApi {
|
||||
/// Platform APIs
|
||||
String getPersistentDataPath();
|
||||
String getCacheDataPath();
|
||||
void openBatteryOptimisationSettings();
|
||||
bool isIgnoringBatteryOptimizations();
|
||||
|
||||
/// Contacts APIs
|
||||
void recordSentMessage(String name, String jid, String? avatarPath, FallbackIconType fallbackIcon);
|
||||
|
Reference in New Issue
Block a user