feat: Make i18n data a bit more persistent
This commit is contained in:
parent
8f93821617
commit
30ef477999
@ -1,5 +1,7 @@
|
||||
package me.polynom.moxplatform_android;
|
||||
|
||||
import static me.polynom.moxplatform_android.ConstantsKt.SHARED_PREFERENCES_KEY;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
@ -85,10 +87,10 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa
|
||||
}
|
||||
|
||||
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) {
|
||||
context.getSharedPreferences(MoxplatformAndroidPlugin.sharedPrefKey, MODE_PRIVATE)
|
||||
context.getSharedPreferences(SHARED_PREFERENCES_KEY, MODE_PRIVATE)
|
||||
.edit()
|
||||
.putBoolean(manuallyStoppedKey, value)
|
||||
.apply();
|
||||
@ -151,7 +153,7 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa
|
||||
FlutterInjector.instance().flutterLoader().startInitialization(getApplicationContext());
|
||||
}
|
||||
|
||||
long entrypointHandle = getSharedPreferences(MoxplatformAndroidPlugin.sharedPrefKey, MODE_PRIVATE)
|
||||
long entrypointHandle = getSharedPreferences(SHARED_PREFERENCES_KEY, MODE_PRIVATE)
|
||||
.getLong(MoxplatformAndroidPlugin.entrypointKey, 0);
|
||||
FlutterInjector.instance().flutterLoader().ensureInitializationComplete(getApplicationContext(), null);
|
||||
FlutterCallbackInformation callback = FlutterCallbackInformation.lookupCallbackInformation(entrypointHandle);
|
||||
|
@ -27,6 +27,12 @@ const val NOTIFICATION_MESSAGE_EXTRA_PATH = "path"
|
||||
|
||||
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
|
||||
//const val METHOD_CHANNEL_KEY = "me.polynom.moxplatform_android"
|
||||
//const val BACKGROUND_METHOD_CHANNEL_KEY = METHOD_CHANNEL_KEY + "_bg"
|
||||
|
@ -1,6 +1,7 @@
|
||||
package me.polynom.moxplatform_android;
|
||||
|
||||
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.CryptoKt.*;
|
||||
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 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";
|
||||
@ -104,7 +104,7 @@ import kotlin.jvm.functions.Function1;
|
||||
|
||||
/// 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);
|
||||
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE);
|
||||
prefs.edit()
|
||||
.putLong(entrypointKey, entrypointHandle)
|
||||
.putString(extraDataKey, extraData)
|
||||
@ -112,21 +112,21 @@ import kotlin.jvm.functions.Function1;
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
c.getSharedPreferences(sharedPrefKey, Context.MODE_PRIVATE)
|
||||
c.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
|
||||
.edit()
|
||||
.putBoolean(autoStartAtBootKey, value)
|
||||
.apply();
|
||||
}
|
||||
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() {
|
||||
@ -313,9 +313,9 @@ import kotlin.jvm.functions.Function1;
|
||||
@Override
|
||||
public void setNotificationI18n(@NonNull NotificationI18nData data) {
|
||||
// Configure i18n
|
||||
NotificationDataManager.INSTANCE.setYou(data.getYou());
|
||||
NotificationDataManager.INSTANCE.setReply(data.getReply());
|
||||
NotificationDataManager.INSTANCE.setMarkAsRead(data.getMarkAsRead());
|
||||
NotificationDataManager.INSTANCE.setYou(context, data.getYou());
|
||||
NotificationDataManager.INSTANCE.setReply(context, data.getReply());
|
||||
NotificationDataManager.INSTANCE.setMarkAsRead(context, data.getMarkAsRead());
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
@ -74,7 +74,7 @@ class NotificationReceiver : BroadcastReceiver() {
|
||||
|
||||
val notification = findActiveNotification(context, id)
|
||||
if (notification == null) {
|
||||
Log.e(TAG, "Failed to find notification for id ${id}")
|
||||
Log.e(TAG, "Failed to find notification for id $id")
|
||||
return
|
||||
}
|
||||
|
||||
@ -83,7 +83,7 @@ class NotificationReceiver : BroadcastReceiver() {
|
||||
val newStyle = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
|
||||
Notification.MessagingStyle(
|
||||
android.app.Person.Builder().apply {
|
||||
setName(NotificationDataManager.you)
|
||||
setName(NotificationDataManager.getYou(context))
|
||||
|
||||
// Set an avatar, if we have one
|
||||
if (NotificationDataManager.avatarPath != null) {
|
||||
@ -95,7 +95,7 @@ class NotificationReceiver : BroadcastReceiver() {
|
||||
}
|
||||
}.build()
|
||||
)
|
||||
else Notification.MessagingStyle(NotificationDataManager.you)
|
||||
else Notification.MessagingStyle(NotificationDataManager.getYou(context))
|
||||
|
||||
newStyle.apply {
|
||||
conversationTitle = recoveredStyle.conversationTitle
|
||||
|
@ -14,11 +14,56 @@ import androidx.core.content.FileProvider
|
||||
import androidx.core.graphics.drawable.IconCompat
|
||||
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 {
|
||||
var you: String = "You"
|
||||
var markAsRead: String = "Mark as read"
|
||||
var reply: String = "Reply"
|
||||
private var you: String? = null
|
||||
private var markAsRead: String? = null
|
||||
private var reply: 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.
|
||||
@ -26,7 +71,7 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
|
||||
// Build the actions
|
||||
// -> Reply action
|
||||
val remoteInput = RemoteInput.Builder(REPLY_TEXT_KEY).apply {
|
||||
setLabel(NotificationDataManager.reply)
|
||||
setLabel(NotificationDataManager.getReply(context))
|
||||
}.build()
|
||||
val replyIntent = Intent(context, NotificationReceiver::class.java).apply {
|
||||
action = REPLY_ACTION
|
||||
@ -41,7 +86,7 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
|
||||
)
|
||||
val replyAction = NotificationCompat.Action.Builder(
|
||||
R.drawable.reply,
|
||||
NotificationDataManager.reply,
|
||||
NotificationDataManager.getReply(context),
|
||||
replyPendingIntent,
|
||||
).apply {
|
||||
addRemoteInput(remoteInput)
|
||||
@ -62,7 +107,7 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
|
||||
)
|
||||
val markAsReadAction = NotificationCompat.Action.Builder(
|
||||
R.drawable.mark_as_read,
|
||||
NotificationDataManager.markAsRead,
|
||||
NotificationDataManager.getMarkAsRead(context),
|
||||
markAsReadPendingIntent,
|
||||
).build()
|
||||
|
||||
@ -82,7 +127,7 @@ fun showMessagingNotification(context: Context, notification: Api.MessagingNotif
|
||||
|
||||
// Build the notification
|
||||
val selfPerson = Person.Builder().apply {
|
||||
setName(NotificationDataManager.you)
|
||||
setName(NotificationDataManager.getYou(context))
|
||||
|
||||
// Set an avatar, if we have one
|
||||
if (NotificationDataManager.avatarPath != null) {
|
||||
|
Reference in New Issue
Block a user