feat: Make i18n data a bit more persistent

This commit is contained in:
PapaTutuWawa 2023-07-29 12:50:50 +02:00
parent 8f93821617
commit 30ef477999
5 changed files with 75 additions and 22 deletions

View File

@ -1,5 +1,7 @@
package me.polynom.moxplatform_android; package me.polynom.moxplatform_android;
import static me.polynom.moxplatform_android.ConstantsKt.SHARED_PREFERENCES_KEY;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
@ -85,10 +87,10 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa
} }
public static boolean isManuallyStopped(Context context) { public static boolean isManuallyStopped(Context context) {
return context.getSharedPreferences(MoxplatformAndroidPlugin.sharedPrefKey, MODE_PRIVATE).getBoolean(manuallyStoppedKey, false); return context.getSharedPreferences(SHARED_PREFERENCES_KEY, MODE_PRIVATE).getBoolean(manuallyStoppedKey, false);
} }
public void setManuallyStopped(Context context, boolean value) { public void setManuallyStopped(Context context, boolean value) {
context.getSharedPreferences(MoxplatformAndroidPlugin.sharedPrefKey, MODE_PRIVATE) context.getSharedPreferences(SHARED_PREFERENCES_KEY, MODE_PRIVATE)
.edit() .edit()
.putBoolean(manuallyStoppedKey, value) .putBoolean(manuallyStoppedKey, value)
.apply(); .apply();
@ -151,7 +153,7 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa
FlutterInjector.instance().flutterLoader().startInitialization(getApplicationContext()); FlutterInjector.instance().flutterLoader().startInitialization(getApplicationContext());
} }
long entrypointHandle = getSharedPreferences(MoxplatformAndroidPlugin.sharedPrefKey, MODE_PRIVATE) long entrypointHandle = getSharedPreferences(SHARED_PREFERENCES_KEY, MODE_PRIVATE)
.getLong(MoxplatformAndroidPlugin.entrypointKey, 0); .getLong(MoxplatformAndroidPlugin.entrypointKey, 0);
FlutterInjector.instance().flutterLoader().ensureInitializationComplete(getApplicationContext(), null); FlutterInjector.instance().flutterLoader().ensureInitializationComplete(getApplicationContext(), null);
FlutterCallbackInformation callback = FlutterCallbackInformation.lookupCallbackInformation(entrypointHandle); FlutterCallbackInformation callback = FlutterCallbackInformation.lookupCallbackInformation(entrypointHandle);

View File

@ -27,6 +27,12 @@ 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.fileprovider"
// Shared preferences keys
const val SHARED_PREFERENCES_KEY = "me.polynom.moxplatform_android"
const val SHARED_PREFERENCES_YOU_KEY = "you"
const val SHARED_PREFERENCES_MARK_AS_READ_KEY = "mark_as_read"
const val SHARED_PREFERENCES_REPLY_KEY = "reply"
// TODO: Maybe try again to rewrite the entire plugin in Kotlin // TODO: Maybe try again to rewrite the entire plugin in Kotlin
//const val METHOD_CHANNEL_KEY = "me.polynom.moxplatform_android" //const val METHOD_CHANNEL_KEY = "me.polynom.moxplatform_android"
//const val BACKGROUND_METHOD_CHANNEL_KEY = METHOD_CHANNEL_KEY + "_bg" //const val BACKGROUND_METHOD_CHANNEL_KEY = METHOD_CHANNEL_KEY + "_bg"

View File

@ -1,6 +1,7 @@
package me.polynom.moxplatform_android; package me.polynom.moxplatform_android;
import static androidx.core.content.ContextCompat.getSystemService; import static androidx.core.content.ContextCompat.getSystemService;
import static me.polynom.moxplatform_android.ConstantsKt.SHARED_PREFERENCES_KEY;
import static me.polynom.moxplatform_android.RecordSentMessageKt.recordSentMessage; import static me.polynom.moxplatform_android.RecordSentMessageKt.recordSentMessage;
import static me.polynom.moxplatform_android.CryptoKt.*; import static me.polynom.moxplatform_android.CryptoKt.*;
import me.polynom.moxplatform_android.Api.*; import me.polynom.moxplatform_android.Api.*;
@ -41,7 +42,6 @@ import kotlin.jvm.functions.Function1;
public static final String entrypointKey = "entrypoint_handle"; public static final String entrypointKey = "entrypoint_handle";
public static final String extraDataKey = "extra_data"; public static final String extraDataKey = "extra_data";
private static final String autoStartAtBootKey = "auto_start_at_boot"; 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"; private static final String TAG = "moxplatform_android";
public static final String methodChannelKey = "me.polynom.moxplatform_android"; public static final String methodChannelKey = "me.polynom.moxplatform_android";
public static final String dataReceivedMethodName = "dataReceived"; public static final String dataReceivedMethodName = "dataReceived";
@ -104,7 +104,7 @@ import kotlin.jvm.functions.Function1;
/// Store the entrypoint handle and extra data for the background service. /// Store the entrypoint handle and extra data for the background service.
private void configure(long entrypointHandle, String extraData) { private void configure(long entrypointHandle, String extraData) {
SharedPreferences prefs = context.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE); SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE);
prefs.edit() prefs.edit()
.putLong(entrypointKey, entrypointHandle) .putLong(entrypointKey, entrypointHandle)
.putString(extraDataKey, extraData) .putString(extraDataKey, extraData)
@ -112,21 +112,21 @@ import kotlin.jvm.functions.Function1;
} }
public static long getHandle(Context c) { public static long getHandle(Context c) {
return c.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE).getLong(entrypointKey, 0); return c.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).getLong(entrypointKey, 0);
} }
public static String getExtraData(Context c) { public static String getExtraData(Context c) {
return c.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE).getString(extraDataKey, ""); return c.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).getString(extraDataKey, "");
} }
public static void setStartAtBoot(Context c, boolean value) { public static void setStartAtBoot(Context c, boolean value) {
c.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE) c.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
.edit() .edit()
.putBoolean(autoStartAtBootKey, value) .putBoolean(autoStartAtBootKey, value)
.apply(); .apply();
} }
public static boolean getStartAtBoot(Context c) { public static boolean getStartAtBoot(Context c) {
return c.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE).getBoolean(autoStartAtBootKey, false); return c.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE).getBoolean(autoStartAtBootKey, false);
} }
private boolean isRunning() { private boolean isRunning() {
@ -313,9 +313,9 @@ import kotlin.jvm.functions.Function1;
@Override @Override
public void setNotificationI18n(@NonNull NotificationI18nData data) { public void setNotificationI18n(@NonNull NotificationI18nData data) {
// Configure i18n // Configure i18n
NotificationDataManager.INSTANCE.setYou(data.getYou()); NotificationDataManager.INSTANCE.setYou(context, data.getYou());
NotificationDataManager.INSTANCE.setReply(data.getReply()); NotificationDataManager.INSTANCE.setReply(context, data.getReply());
NotificationDataManager.INSTANCE.setMarkAsRead(data.getMarkAsRead()); NotificationDataManager.INSTANCE.setMarkAsRead(context, data.getMarkAsRead());
} }
@NonNull @NonNull

View File

@ -74,7 +74,7 @@ class NotificationReceiver : BroadcastReceiver() {
val notification = findActiveNotification(context, id) val notification = findActiveNotification(context, id)
if (notification == null) { if (notification == null) {
Log.e(TAG, "Failed to find notification for id ${id}") Log.e(TAG, "Failed to find notification for id $id")
return return
} }
@ -83,7 +83,7 @@ class NotificationReceiver : BroadcastReceiver() {
val newStyle = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) val newStyle = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
Notification.MessagingStyle( Notification.MessagingStyle(
android.app.Person.Builder().apply { android.app.Person.Builder().apply {
setName(NotificationDataManager.you) setName(NotificationDataManager.getYou(context))
// Set an avatar, if we have one // Set an avatar, if we have one
if (NotificationDataManager.avatarPath != null) { if (NotificationDataManager.avatarPath != null) {
@ -95,7 +95,7 @@ class NotificationReceiver : BroadcastReceiver() {
} }
}.build() }.build()
) )
else Notification.MessagingStyle(NotificationDataManager.you) else Notification.MessagingStyle(NotificationDataManager.getYou(context))
newStyle.apply { newStyle.apply {
conversationTitle = recoveredStyle.conversationTitle conversationTitle = recoveredStyle.conversationTitle

View File

@ -14,11 +14,56 @@ import androidx.core.content.FileProvider
import androidx.core.graphics.drawable.IconCompat import androidx.core.graphics.drawable.IconCompat
import java.io.File import java.io.File
/*
* Holds "persistent" data for notifications, like i18n strings. While not useful now, this is
* useful for when the app is dead and we receive a notification.
* */
object NotificationDataManager { object NotificationDataManager {
var you: String = "You" private var you: String? = null
var markAsRead: String = "Mark as read" private var markAsRead: String? = null
var reply: String = "Reply" private var reply: String? = null
var avatarPath: String? = null var avatarPath: String? = null
private fun getString(context: Context, key: String, fallback: String): String {
return context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)!!.getString(key, fallback)!!
}
private fun setString(context: Context, key: String, value: String) {
val prefs = context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
prefs.edit()
.putString(key, value)
.apply()
}
fun getYou(context: Context): String {
if (you == null) you = getString(context, SHARED_PREFERENCES_YOU_KEY, "You")
return you!!
}
fun setYou(context: Context, value: String) {
setString(context, SHARED_PREFERENCES_YOU_KEY, value)
you = value
}
fun getMarkAsRead(context: Context): String {
if (markAsRead == null) markAsRead = getString(context, SHARED_PREFERENCES_MARK_AS_READ_KEY, "Mark as read")
return markAsRead!!
}
fun setMarkAsRead(context: Context, value: String) {
setString(context, SHARED_PREFERENCES_MARK_AS_READ_KEY, value)
markAsRead = value
}
fun getReply(context: Context): String {
if (reply != null) reply = getString(context, SHARED_PREFERENCES_REPLY_KEY, "Reply")
return reply!!
}
fun setReply(context: Context, value: String) {
setString(context, SHARED_PREFERENCES_REPLY_KEY, value)
reply = value
}
} }
/// Show a messaging style notification described by @notification. /// Show a messaging style notification described by @notification.
@ -26,7 +71,7 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
// Build the actions // Build the actions
// -> Reply action // -> Reply action
val remoteInput = RemoteInput.Builder(REPLY_TEXT_KEY).apply { val remoteInput = RemoteInput.Builder(REPLY_TEXT_KEY).apply {
setLabel(NotificationDataManager.reply) setLabel(NotificationDataManager.getReply(context))
}.build() }.build()
val replyIntent = Intent(context, NotificationReceiver::class.java).apply { val replyIntent = Intent(context, NotificationReceiver::class.java).apply {
action = REPLY_ACTION action = REPLY_ACTION
@ -41,7 +86,7 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
) )
val replyAction = NotificationCompat.Action.Builder( val replyAction = NotificationCompat.Action.Builder(
R.drawable.reply, R.drawable.reply,
NotificationDataManager.reply, NotificationDataManager.getReply(context),
replyPendingIntent, replyPendingIntent,
).apply { ).apply {
addRemoteInput(remoteInput) addRemoteInput(remoteInput)
@ -62,7 +107,7 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
) )
val markAsReadAction = NotificationCompat.Action.Builder( val markAsReadAction = NotificationCompat.Action.Builder(
R.drawable.mark_as_read, R.drawable.mark_as_read,
NotificationDataManager.markAsRead, NotificationDataManager.getMarkAsRead(context),
markAsReadPendingIntent, markAsReadPendingIntent,
).build() ).build()
@ -82,7 +127,7 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
// Build the notification // Build the notification
val selfPerson = Person.Builder().apply { val selfPerson = Person.Builder().apply {
setName(NotificationDataManager.you) setName(NotificationDataManager.getYou(context))
// Set an avatar, if we have one // Set an avatar, if we have one
if (NotificationDataManager.avatarPath != null) { if (NotificationDataManager.avatarPath != null) {