From bf36d6789e16ec143542934a71ce858537763f43 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 6 May 2019 13:59:26 +0200 Subject: [PATCH] Made notification actions pro instead of notification previews --- README.md | 2 +- .../java/eu/faircode/email/ActivitySetup.java | 1 + .../java/eu/faircode/email/ApplicationEx.java | 22 ++++- app/src/main/java/eu/faircode/email/Core.java | 86 +++++++++---------- .../email/FragmentOptionsNotifications.java | 23 +++-- .../layout/fragment_options_notifications.xml | 14 +-- 6 files changed, 82 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 068bd0f643..f7c5826292 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,8 @@ This app starts a foreground service with a low priority status bar notification * Account/identity colors * Notifications per account (requires Android 8 Oreo or later) -* Notifications with message details (requires Android 7 Nougat or later) * Notification sound per sender (requires Android 8 Oreo or later) +* Configurable notification actions * Snooze messages ([instructions](https://github.com/M66B/open-source-email/blob/master/FAQ.md#user-content-faq67)) * Send messages after selected time * Synchronization scheduling ([instructions](https://github.com/M66B/open-source-email/blob/master/FAQ.md#user-content-faq78)) diff --git a/app/src/main/java/eu/faircode/email/ActivitySetup.java b/app/src/main/java/eu/faircode/email/ActivitySetup.java index 972541f0ec..73a0f0b741 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySetup.java +++ b/app/src/main/java/eu/faircode/email/ActivitySetup.java @@ -857,6 +857,7 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On } } editor.apply(); + ApplicationEx.upgrade(context); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) if (jimport.has("channels")) { diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 947c2470b1..330061c9aa 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -34,10 +34,10 @@ import android.net.Uri; import android.os.Build; import android.os.DeadSystemException; import android.os.RemoteException; -import android.preference.PreferenceManager; import android.webkit.CookieManager; import androidx.annotation.RequiresApi; +import androidx.preference.PreferenceManager; import org.json.JSONArray; import org.json.JSONException; @@ -90,6 +90,7 @@ public class ApplicationEx extends Application { } }); + upgrade(this); createNotificationChannels(); if (Helper.hasWebView(this)) CookieManager.getInstance().setAcceptCookie(false); @@ -118,6 +119,25 @@ public class ApplicationEx extends Application { Log.i(message + " " + mb + " MB" + " " + perc + " %"); } + static void upgrade(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + int version = prefs.getInt("version", 468); + if (version < BuildConfig.VERSION_CODE) { + Log.i("Upgrading from " + version + " to " + BuildConfig.VERSION_CODE); + + SharedPreferences.Editor editor = prefs.edit(); + + editor.remove("notify_trash"); + editor.remove("notify_archive"); + editor.remove("notify_reply"); + editor.remove("notify_flag"); + editor.remove("notify_seen"); + + editor.putInt("version", BuildConfig.VERSION_CODE); + editor.apply(); + } + } + static Context getLocalizedContext(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean english = prefs.getBoolean("english", false); diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 6023ecdd86..ba95cbbd74 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -1763,22 +1763,20 @@ class Core { } else builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN); - if (pro) { - DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT); - StringBuilder sb = new StringBuilder(); - for (EntityMessage message : messages) { - sb.append("").append(messageContact.get(message).getDisplayName(true)).append(""); - if (!TextUtils.isEmpty(message.subject)) - sb.append(": ").append(message.subject); - sb.append(" ").append(df.format(message.received)); - sb.append("
"); - } - - builder.setStyle(new NotificationCompat.BigTextStyle() - .bigText(HtmlHelper.fromHtml(sb.toString())) - .setSummaryText(title)); + DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT); + StringBuilder sb = new StringBuilder(); + for (EntityMessage message : messages) { + sb.append("").append(messageContact.get(message).getDisplayName(true)).append(""); + if (!TextUtils.isEmpty(message.subject)) + sb.append(": ").append(message.subject); + sb.append(" ").append(df.format(message.received)); + sb.append("
"); } + builder.setStyle(new NotificationCompat.BigTextStyle() + .bigText(HtmlHelper.fromHtml(sb.toString())) + .setSummaryText(title)); + notifications.add(builder.build()); boolean preview = prefs.getBoolean("notify_preview", true); @@ -1886,42 +1884,40 @@ class Core { mbuilder.addAction(actionSeen.build()); } - if (pro) { - if (!TextUtils.isEmpty(message.subject)) - mbuilder.setContentText(message.subject); + if (!TextUtils.isEmpty(message.subject)) + mbuilder.setContentText(message.subject); - if (message.content && preview) - try { - String body = Helper.readText(message.getFile(context)); - StringBuilder sb = new StringBuilder(); - if (!TextUtils.isEmpty(message.subject)) - sb.append(message.subject).append("
"); - String text = Jsoup.parse(body).text(); - if (!TextUtils.isEmpty(text)) { - sb.append(""); - if (text.length() > HtmlHelper.PREVIEW_SIZE) { - sb.append(text.substring(0, HtmlHelper.PREVIEW_SIZE)); - sb.append("…"); - } else - sb.append(text); - sb.append(""); - } - mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(HtmlHelper.fromHtml(sb.toString()))); - } catch (IOException ex) { - Log.e(ex); - mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(ex.toString())); + if (message.content && preview) + try { + String body = Helper.readText(message.getFile(context)); + StringBuilder sbm = new StringBuilder(); + if (!TextUtils.isEmpty(message.subject)) + sbm.append(message.subject).append("
"); + String text = Jsoup.parse(body).text(); + if (!TextUtils.isEmpty(text)) { + sbm.append(""); + if (text.length() > HtmlHelper.PREVIEW_SIZE) { + sbm.append(text.substring(0, HtmlHelper.PREVIEW_SIZE)); + sbm.append("…"); + } else + sbm.append(text); + sbm.append(""); } + mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(HtmlHelper.fromHtml(sbm.toString()))); + } catch (IOException ex) { + Log.e(ex); + mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(ex.toString())); + } - if (info.hasPhoto()) - mbuilder.setLargeIcon(info.getPhotoBitmap()); + if (info.hasPhoto()) + mbuilder.setLargeIcon(info.getPhotoBitmap()); - if (info.hasLookupUri()) - mbuilder.addPerson(info.getLookupUri().toString()); + if (info.hasLookupUri()) + mbuilder.addPerson(info.getLookupUri().toString()); - if (message.accountColor != null) { - mbuilder.setColor(message.accountColor); - mbuilder.setColorized(true); - } + if (pro && message.accountColor != null) { + mbuilder.setColor(message.accountColor); + mbuilder.setColorized(true); } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java b/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java index d6c508eb67..25d24e5482 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsNotifications.java @@ -36,6 +36,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.SwitchCompat; import androidx.constraintlayout.widget.Group; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.preference.PreferenceManager; import static android.app.Activity.RESULT_OK; @@ -88,35 +89,35 @@ public class FragmentOptionsNotifications extends FragmentBase { cbNotifyActionTrash.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean checked) { - prefs.edit().putBoolean("notify_trash", checked).apply(); + setAction(buttonView, "notify_trash", checked); } }); cbNotifyActionArchive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean checked) { - prefs.edit().putBoolean("notify_archive", checked).apply(); + setAction(buttonView, "notify_archive", checked); } }); cbNotifyActionReply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean checked) { - prefs.edit().putBoolean("notify_reply", checked).apply(); + setAction(buttonView, "notify_reply", checked); } }); cbNotifyActionFlag.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean checked) { - prefs.edit().putBoolean("notify_flag", checked).apply(); + setAction(buttonView, "notify_flag", checked); } }); cbNotifyActionSeen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean checked) { - prefs.edit().putBoolean("notify_seen", checked).apply(); + setAction(buttonView, "notify_seen", checked); } }); @@ -144,11 +145,21 @@ public class FragmentOptionsNotifications extends FragmentBase { return view; } + private void setAction(CompoundButton cb, String key, boolean checked) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + if (Helper.isPro(getContext())) + prefs.edit().putBoolean(key, checked).apply(); + else { + cb.setChecked(!checked); + LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext()); + lbm.sendBroadcast(new Intent(ActivityView.ACTION_SHOW_PRO)); + } + } + private void setOptions() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); swNotifyPreview.setChecked(prefs.getBoolean("notify_preview", true)); - swNotifyPreview.setEnabled(Helper.isPro(getContext())); cbNotifyActionTrash.setChecked(prefs.getBoolean("notify_trash", true)); cbNotifyActionArchive.setChecked(prefs.getBoolean("notify_archive", true)); cbNotifyActionReply.setChecked(prefs.getBoolean("notify_reply", false)); diff --git a/app/src/main/res/layout/fragment_options_notifications.xml b/app/src/main/res/layout/fragment_options_notifications.xml index b342cbd0e4..c6622ec9c0 100644 --- a/app/src/main/res/layout/fragment_options_notifications.xml +++ b/app/src/main/res/layout/fragment_options_notifications.xml @@ -39,18 +39,6 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/swNotifyPreview" /> - - + app:layout_constraintTop_toBottomOf="@id/tvNotifyPreviewHint" />