From 04795828d97727a4514487adc58f330aa950485e Mon Sep 17 00:00:00 2001 From: Ben Komalo Date: Sat, 25 Jun 2011 17:24:19 -0700 Subject: [PATCH] Persist trusted sender set to preferences This makes it so that "always show pictures" from a sender works Change-Id: I6e6c6d8dc78746d0828428b6c6256c4fff3eb111 --- src/com/android/email/Preferences.java | 65 +++++++++++++++++-- .../activity/MessageViewFragmentBase.java | 10 ++- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/com/android/email/Preferences.java b/src/com/android/email/Preferences.java index d866a030b..087a5d798 100644 --- a/src/com/android/email/Preferences.java +++ b/src/com/android/email/Preferences.java @@ -16,12 +16,18 @@ package com.android.email; -import com.android.emailcommon.Logging; - import android.content.Context; import android.content.SharedPreferences; +import android.text.TextUtils; import android.util.Log; +import com.android.emailcommon.Logging; +import com.google.common.annotations.VisibleForTesting; + +import org.json.JSONArray; +import org.json.JSONException; + +import java.util.HashSet; import java.util.UUID; public class Preferences { @@ -42,6 +48,7 @@ public class Preferences { private static final String AUTO_ADVANCE_DIRECTION = "autoAdvance"; private static final String TEXT_ZOOM = "textZoom"; private static final String BACKGROUND_ATTACHMENTS = "backgroundAttachments"; + private static final String TRUSTED_SENDERS = "trustedSenders"; public static final int AUTO_ADVANCE_NEWER = 0; public static final int AUTO_ADVANCE_OLDER = 1; @@ -60,7 +67,14 @@ public class Preferences { private static Preferences sPreferences; - final SharedPreferences mSharedPreferences; + private final SharedPreferences mSharedPreferences; + + /** + * A set of trusted senders for whom images and external resources should automatically be + * loaded for. + * Lazilly created. + */ + private HashSet mTrustedSenders = null; private Preferences(Context context) { mSharedPreferences = context.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE); @@ -181,7 +195,50 @@ public class Preferences { mSharedPreferences.edit().putBoolean(BACKGROUND_ATTACHMENTS, allowed).apply(); } - public void save() { + /** + * Determines whether or not a sender should be trusted and images should automatically be + * shown for messages by that sender. + */ + public boolean shouldShowImagesFor(String email) { + if (mTrustedSenders == null) { + try { + mTrustedSenders = parseEmailSet(mSharedPreferences.getString(TRUSTED_SENDERS, "")); + } catch (JSONException e) { + // Something went wrong, and the data is corrupt. Just clear it to be safe. + Log.w(Logging.LOG_TAG, "Trusted sender set corrupted. Clearing"); + mSharedPreferences.edit().putString(TRUSTED_SENDERS, "").apply(); + mTrustedSenders = new HashSet(); + } + } + return mTrustedSenders.contains(email); + } + + /** + * Marks a sender as trusted so that images from that sender will automatically be shown. + */ + public void setSenderAsTrusted(String email) { + if (!mTrustedSenders.contains(email)) { + mTrustedSenders.add(email); + mSharedPreferences + .edit() + .putString(TRUSTED_SENDERS, packEmailSet(mTrustedSenders)) + .apply(); + } + } + + HashSet parseEmailSet(String serialized) throws JSONException { + HashSet result = new HashSet(); + if (!TextUtils.isEmpty(serialized)) { + JSONArray arr = new JSONArray(serialized); + for (int i = 0, len = arr.length(); i < len; i++) { + result.add((String) arr.get(i)); + } + } + return result; + } + + String packEmailSet(HashSet set) { + return new JSONArray(set).toString(); } public void clear() { diff --git a/src/com/android/email/activity/MessageViewFragmentBase.java b/src/com/android/email/activity/MessageViewFragmentBase.java index 5ff08aabd..6bdcbc9bc 100644 --- a/src/com/android/email/activity/MessageViewFragmentBase.java +++ b/src/com/android/email/activity/MessageViewFragmentBase.java @@ -1879,8 +1879,7 @@ public abstract class MessageViewFragmentBase extends Fragment implements View.O } private boolean shouldShowImagesFor(String senderEmail) { - // TODO: read from prefs. - return false; + return Preferences.getPreferences(getActivity()).shouldShowImagesFor(senderEmail); } private void setShowImagesForSender() { @@ -1889,7 +1888,12 @@ public abstract class MessageViewFragmentBase extends Fragment implements View.O // Force redraw of the container. updateTabs(mTabFlags); - // TODO: write to prefs. + Address[] fromList = Address.unpack(mMessage.mFrom); + Preferences prefs = Preferences.getPreferences(getActivity()); + for (Address sender : fromList) { + String email = sender.getAddress(); + prefs.setSenderAsTrusted(email); + } } public boolean isMessageLoadedForTest() {