refactor: Rename src to packages

This commit is contained in:
2022-09-01 13:39:12 +02:00
parent a280146258
commit 7f5fd68789
93 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.cxx

View File

@@ -0,0 +1,39 @@
group 'me.polynom.moxplatform_android'
version '1.0'
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
}
}
rootProject.allprojects {
repositories {
google()
mavenCentral()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 31
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
minSdkVersion 16
}
}
dependencies {
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
}

View File

@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -0,0 +1 @@
rootProject.name = 'moxplatform_android'

View File

@@ -0,0 +1,31 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.polynom.moxplatform_android">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application>
<service
android:enabled="true"
android:exported="true"
android:name=".BackgroundService"
/>
<receiver
android:name=".WatchdogReceiver"
android:enabled="true"
android:exported="true"
/>
<receiver android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>

View File

@@ -0,0 +1,275 @@
package me.polynom.moxplatform_android;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.IBinder;
import android.os.Looper;
import android.os.PowerManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.AlarmManagerCompat;
import androidx.core.app.NotificationCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import io.flutter.FlutterInjector;
import io.flutter.app.FlutterApplication;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.loader.FlutterLoader;
import io.flutter.plugin.common.JSONMethodCodec;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.FlutterCallbackInformation;
import io.flutter.view.FlutterMain;
public class BackgroundService extends Service implements MethodChannel.MethodCallHandler {
private static final String TAG = "BackgroundService";
private static final String manuallyStoppedKey = "manually_stopped";
private static final String backgroundMethodChannelKey = MoxplatformAndroidPlugin.methodChannelKey + "_bg";
/// The [FlutterEngine] executing the background service
private FlutterEngine engine;
private MethodChannel methodChannel;
private DartExecutor.DartCallback dartCallback;
/// True if the service has been stopped by hand
private boolean isManuallyStopped = false;
/// Indicate if we're running
private AtomicBoolean isRunning = new AtomicBoolean(false);
private static final String WAKE_LOCK_NAME = BackgroundService.class.getName() + ".Lock";
public static volatile PowerManager.WakeLock wakeLock = null;
/// Notification data
private String notificationBody = "Preparing...";
private static final String notificationTitle = "Moxxy";
synchronized public static PowerManager.WakeLock getLock(Context context) {
if (wakeLock == null) {
PowerManager mgr = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
wakeLock = mgr.newWakeLock(PowerManager.FULL_WAKE_LOCK, WAKE_LOCK_NAME);
wakeLock.setReferenceCounted(true);
}
return (wakeLock);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public static void enqueue(Context context) {
Intent intent = new Intent(context, WatchdogReceiver.class);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
boolean aboveS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S;
PendingIntent pending = PendingIntent.getBroadcast(
context,
111,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
// Only enable FLAG_MUTABLE when the Android version is Android S or greater
| (PendingIntent.FLAG_MUTABLE & (aboveS ? PendingIntent.FLAG_MUTABLE : 0))
);
AlarmManagerCompat.setAndAllowWhileIdle(manager, AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pending);
}
public static boolean isManuallyStopped(Context context) {
return context.getSharedPreferences(MoxplatformAndroidPlugin.sharedPrefKey, MODE_PRIVATE).getBoolean(manuallyStoppedKey, false);
}
public void setManuallyStopped(Context context, boolean value) {
context.getSharedPreferences(MoxplatformAndroidPlugin.sharedPrefKey, MODE_PRIVATE)
.edit()
.putBoolean(manuallyStoppedKey, value)
.apply();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Moxxy Background Service";
String description = "Executing Moxxy in the background";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel("FOREGROUND_DEFAULT", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
protected void updateNotificationInfo() {
String packageName = getApplicationContext().getPackageName();
Intent i = getPackageManager().getLaunchIntentForPackage(packageName);
boolean aboveS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S;
PendingIntent pending = PendingIntent.getActivity(
BackgroundService.this,
99778,
i,
PendingIntent.FLAG_CANCEL_CURRENT
// Only enable on Android S or greater
| (PendingIntent.FLAG_MUTABLE & (aboveS ? PendingIntent.FLAG_MUTABLE : 0))
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "FOREGROUND_DEFAULT")
.setSmallIcon(R.drawable.ic_service_icon)
.setAutoCancel(true)
.setOngoing(true)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setContentIntent(pending);
startForeground(99778, mBuilder.build());
}
private void runService() {
try {
if (isRunning.get() || (engine != null && !engine.getDartExecutor().isExecutingDart())) return;
if (wakeLock == null) {
Log.d(TAG, "Wakelock is null. Acquiring it...");
getLock(getApplicationContext()).acquire(MoxplatformConstants.WAKE_LOCK_DURATION);
Log.d(TAG, "Wakelock acquired...");
}
updateNotificationInfo();
// Initialize Flutter if it's not already
if (!FlutterInjector.instance().flutterLoader().initialized()) {
FlutterInjector.instance().flutterLoader().startInitialization(getApplicationContext());
}
long entrypointHandle = getSharedPreferences(MoxplatformAndroidPlugin.sharedPrefKey, MODE_PRIVATE)
.getLong(MoxplatformAndroidPlugin.entrypointKey, 0);
FlutterInjector.instance().flutterLoader().ensureInitializationComplete(getApplicationContext(), null);
FlutterCallbackInformation callback = FlutterCallbackInformation.lookupCallbackInformation(entrypointHandle);
if (callback == null) {
Log.e(TAG, "Callback handle not found");
return;
}
isRunning.set(true);
engine = new FlutterEngine(this);
engine.getServiceControlSurface().attachToService(BackgroundService.this, null, true);
methodChannel = new MethodChannel(engine.getDartExecutor().getBinaryMessenger(), backgroundMethodChannelKey);
methodChannel.setMethodCallHandler(this);
Log.d(TAG, "Method channel is set up");
dartCallback = new DartExecutor.DartCallback(getAssets(), FlutterInjector.instance().flutterLoader().findAppBundlePath(), callback);
engine.getDartExecutor().executeDartCallback(dartCallback);
} catch (UnsatisfiedLinkError e) {
Log.e(TAG, "Error: " + e.getMessage());
}
}
public void receiveData(String data) {
if (methodChannel != null) {
methodChannel.invokeMethod(MoxplatformAndroidPlugin.dataReceivedMethodName, data);
}
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
if (wakeLock != null) {
Log.d(TAG, "Wakelock is not null. Releasing it...");
wakeLock.release();
wakeLock = null;
Log.d(TAG, "Wakelock released...");
}
switch (call.method) {
case "getHandler":
result.success(MoxplatformAndroidPlugin.getHandle(this));
break;
case "getExtraData":
result.success(MoxplatformAndroidPlugin.getExtraData(this));
break;
case "setNotificationBody":
String body = ((String) ((ArrayList) call.arguments).get(0));
notificationBody = body;
updateNotificationInfo();
result.success(true);
break;
case "sendData":
LocalBroadcastManager sendDataManager = LocalBroadcastManager.getInstance(this);
Intent sendDataIntent = new Intent(MoxplatformAndroidPlugin.methodChannelKey);
sendDataIntent.putExtra("data", (String) call.arguments);
sendDataManager.sendBroadcast(sendDataIntent);
result.success(true);
break;
case "stop":
isManuallyStopped = true;
Intent stopIntent = new Intent(this, WatchdogReceiver.class);
boolean aboveS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S;
PendingIntent pending = PendingIntent.getBroadcast(
getApplicationContext(),
111,
stopIntent,
PendingIntent.FLAG_CANCEL_CURRENT
// Only enable FLAG_MUTABLE when the Android version is Android S or greater
| (PendingIntent.FLAG_MUTABLE & (aboveS ? PendingIntent.FLAG_MUTABLE : 0)));
AlarmManager stopManager = (AlarmManager) getSystemService(ALARM_SERVICE);
stopManager.cancel(pending);
stopSelf();
MoxplatformAndroidPlugin.setStartAtBoot(this, false);
result.success(true);
break;
default:
result.notImplemented();
break;
}
}
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
notificationBody = "Preparing...";
updateNotificationInfo();
}
@Override
public void onDestroy() {
if (!isManuallyStopped) {
enqueue(this);
} else {
setManuallyStopped(this,true);
}
if (engine != null) {
engine.getServiceControlSurface().detachFromService();
engine.destroy();
engine = null;
}
stopForeground(true);
isRunning.set(false);
methodChannel = null;
dartCallback = null;
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
setManuallyStopped(this,false);
enqueue(this);
runService();
return START_STICKY;
}
}

View File

@@ -0,0 +1,28 @@
package me.polynom.moxplatform_android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import androidx.core.content.ContextCompat;
import androidx.core.content.ContextCompat;
public class BootReceiver extends BroadcastReceiver {
private final String TAG = "BootReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (MoxplatformAndroidPlugin.getStartAtBoot(context)) {
if (BackgroundService.wakeLock == null) {
Log.d(TAG, "Wakelock is null. Acquiring it...");
BackgroundService.getLock(context).acquire(MoxplatformConstants.WAKE_LOCK_DURATION);
Log.d(TAG, "Wakelock acquired...");
}
ContextCompat.startForegroundService(context, new Intent(context, BackgroundService.class));
}
}
}

View File

@@ -0,0 +1,6 @@
package me.polynom.moxplatform_android;
class MoxplatformConstants {
// https://github.com/ekasetiawans/flutter_background_service/blob/e427f3b70138ec26f9671c2617f9061f25eade6f/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/BootReceiver.java#L20
public static final long WAKE_LOCK_DURATION = 10*60*1000L;
}

View File

@@ -0,0 +1,178 @@
package me.polynom.moxplatform_android;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.util.ArrayList;
import java.util.List;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.service.ServiceAware;
import io.flutter.embedding.engine.plugins.service.ServicePluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.common.JSONMethodCodec;
public class MoxplatformAndroidPlugin extends BroadcastReceiver implements FlutterPlugin, MethodCallHandler, ServiceAware {
public static final String entrypointKey = "entrypoint_handle";
public static final String extraDataKey = "extra_data";
private static final String autoStartAtBootKey = "auto_start_at_boot";
public static final String sharedPrefKey = "me.polynom.moxplatform_android";
private static final String TAG = "moxplatform_android";
public static final String methodChannelKey = "me.polynom.moxplatform_android";
public static final String dataReceivedMethodName = "dataReceived";
private static final List<MoxplatformAndroidPlugin> _instances = new ArrayList<>();
private BackgroundService service;
private MethodChannel channel;
private Context context;
public MoxplatformAndroidPlugin() {
_instances.add(this);
}
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), methodChannelKey);
channel.setMethodCallHandler(this);
context = flutterPluginBinding.getApplicationContext();
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this.context);
localBroadcastManager.registerReceiver(this, new IntentFilter(methodChannelKey));
Log.d(TAG, "Attached to engine");
}
static void registerWith(Registrar registrar) {
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(registrar.context());
final MoxplatformAndroidPlugin plugin = new MoxplatformAndroidPlugin();
localBroadcastManager.registerReceiver(plugin, new IntentFilter(methodChannelKey));
final MethodChannel channel = new MethodChannel(registrar.messenger(), "me.polynom/background_service_android", JSONMethodCodec.INSTANCE);
channel.setMethodCallHandler(plugin);
plugin.channel = channel;
Log.d(TAG, "Registered against registrar");
}
/// Store the entrypoint handle and extra data for the background service.
private void configure(long entrypointHandle, String extraData) {
SharedPreferences prefs = context.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE);
prefs.edit()
.putLong(entrypointKey, entrypointHandle)
.putString(extraDataKey, extraData)
.apply();
}
public static long getHandle(Context c) {
return c.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE).getLong(entrypointKey, 0);
}
public static String getExtraData(Context c) {
return c.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE).getString(extraDataKey, "");
}
public static void setStartAtBoot(Context c, boolean value) {
c.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE)
.edit()
.putBoolean(autoStartAtBootKey, value)
.apply();
}
public static boolean getStartAtBoot(Context c) {
return c.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE).getBoolean(autoStartAtBootKey, false);
}
private boolean isRunning() {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo info : manager.getRunningServices(Integer.MAX_VALUE)) {
if (BackgroundService.class.getName().equals(info.service.getClassName())) {
return true;
}
}
return false;
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
switch (call.method) {
case "configure":
ArrayList args = (ArrayList) call.arguments;
long handle = (long) args.get(0);
String extraData = (String) args.get(1);
configure(handle, extraData);
result.success(true);
break;
case "isRunning":
result.success(isRunning());
break;
case "start":
MoxplatformAndroidPlugin.setStartAtBoot(context, true);
BackgroundService.enqueue(context);
Intent intent = new Intent(context, BackgroundService.class);
ContextCompat.startForegroundService(context, intent);
Log.d(TAG, "Service started");
result.success(true);
break;
case "sendData":
for (MoxplatformAndroidPlugin plugin : _instances) {
if (plugin.service != null) {
plugin.service.receiveData((String) call.arguments);
break;
}
}
result.success(true);
break;
default:
result.notImplemented();
break;
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null) return;
if (intent.getAction().equalsIgnoreCase(methodChannelKey)) {
String data = intent.getStringExtra("data");
if (channel != null) {
channel.invokeMethod(dataReceivedMethodName, data);
}
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this.context);
localBroadcastManager.unregisterReceiver(this);
Log.d(TAG, "Detached from engine");
}
@Override
public void onAttachedToService(@NonNull ServicePluginBinding binding) {
Log.d(TAG, "Attached to service");
this.service = (BackgroundService) binding.getService();
}
@Override
public void onDetachedFromService() {
Log.d(TAG, "Detached from service");
this.service = null;
}
}

View File

@@ -0,0 +1,16 @@
package me.polynom.moxplatform_android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import androidx.core.content.ContextCompat;
public class WatchdogReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(!BackgroundService.isManuallyStopped(context)){
ContextCompat.startForegroundService(context, new Intent(context, BackgroundService.class));
}
}
}

View File

@@ -0,0 +1,30 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#FFFFFF">
<group android:scaleX="0.017628439"
android:scaleY="0.017628439"
android:translateX="3.3885078"
android:translateY="3.3479624">
<group>
<clip-path
android:pathData="M464.6,880.91 L114.63,919.02h0A83.2,83.2 0,0 1,31.42 835.81h0L69.48,486.3c1.67,-15.34 2.93,-30.73 3.53,-46.14C81.79,215.47 266.71,36.02 493.55,36.02h0C725.99,36.02 914.42,224.45 914.42,456.89h0c0,227 -179.7,412 -404.6,420.56C494.71,878.02 479.63,879.27 464.6,880.91Z"/>
<path
android:pathData="M464.6,880.91 L114.63,919.02h0A83.2,83.2 0,0 1,31.42 835.81h0L69.48,486.3c1.67,-15.34 2.93,-30.73 3.53,-46.14C81.79,215.47 266.71,36.02 493.55,36.02h0C725.99,36.02 914.42,224.45 914.42,456.89h0c0,227 -179.7,412 -404.6,420.56C494.71,878.02 479.63,879.27 464.6,880.91Z"
android:fillColor="#a139f0"/>
<path
android:pathData="M467.42,845.7 L122.99,918.35h0a83.2,83.2 0,0 1,-91 -74.54h0L35.12,492.25c0.14,-15.43 -0.13,-30.87 -1.06,-46.27C20.5,221.54 186.71,24.62 412.42,2.11h0C643.71,-20.98 849.91,147.85 872.98,379.14h0C895.5,605.02 735.05,806.95 512.11,837.77 497.13,839.84 482.25,842.58 467.42,845.7Z"
android:fillColor="#cf4aff"/>
</group>
<path
android:pathData="M354.58,675.55a83.9,83.9 0,1 1,-90.8 -76.37A83.91,83.91 0,0 1,354.58 675.55ZM585.27,571.43a83.9,83.9 0,1 0,90.8 76.38A83.91,83.91 0,0 0,585.27 571.43ZM291.62,272.89A83.9,83.9 0,1 0,382.42 349.27,83.9 83.9,0 0,0 291.62,272.89ZM671.29,191.69A83.9,83.9 0,1 0,762.09 268.02,83.9 83.9,0 0,0 671.29,191.69Z"
android:fillColor="#fff"/>
<path
android:pathData="M592.48,655.02 L270.99,682.77l27.62,-326.59ZM678.24,275.57L592.48,655.02"
android:strokeWidth="40.98"
android:fillColor="#00000000"
android:strokeColor="#fff"/>
</group>
</vector>

Binary file not shown.

After

Width:  |  Height:  |  Size: 236 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 B