From d9b659aa5dfa4a3af96582ae49ba9ae145854a84 Mon Sep 17 00:00:00 2001 From: Chris Wren Date: Mon, 1 Oct 2012 15:59:01 -0400 Subject: find the disappearing settings, plus some layout. Bug: 7242287 Bug: 7194713 Change-Id: I362c2ffa0d4cbc3be45d14ede1de5dbf39ec4d7f --- .../dreams/phototable/AlbumDataAdapter.java | 18 ++---- .../android/dreams/phototable/AlbumSettings.java | 75 ++++++++++++++++++---- .../android/dreams/phototable/FlipperDream.java | 6 +- .../dreams/phototable/FlipperDreamSettings.java | 27 +++++--- src/com/android/dreams/phototable/LocalSource.java | 14 +++- src/com/android/dreams/phototable/PhotoSource.java | 4 +- .../android/dreams/phototable/PhotoTableDream.java | 6 +- .../dreams/phototable/PhotoTableDreamSettings.java | 27 +++++--- .../android/dreams/phototable/PicasaSource.java | 14 +++- 9 files changed, 135 insertions(+), 56 deletions(-) (limited to 'src') diff --git a/src/com/android/dreams/phototable/AlbumDataAdapter.java b/src/com/android/dreams/phototable/AlbumDataAdapter.java index 099fd90..8682cd5 100644 --- a/src/com/android/dreams/phototable/AlbumDataAdapter.java +++ b/src/com/android/dreams/phototable/AlbumDataAdapter.java @@ -41,21 +41,18 @@ public class AlbumDataAdapter extends ArrayAdapter { public static final String ALBUM_SET = "Enabled Album Set"; - private final SharedPreferences mSettings; + private final AlbumSettings mSettings; private final LayoutInflater mInflater; private final int mLayout; private final ItemClickListener mListener; - private Set mEnabledAlbums; - public AlbumDataAdapter(Context context, SharedPreferences settings, int resource, List objects) { super(context, resource, objects); - mSettings = settings; + mSettings = AlbumSettings.getAlbumSettings(settings); mLayout = resource; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mListener = new ItemClickListener(); - mEnabledAlbums = AlbumSettings.getEnabledAlbums(mSettings); } @Override @@ -69,7 +66,7 @@ public class AlbumDataAdapter extends ArrayAdapter { View vCheckBox = item.findViewById(R.id.enabled); if (vCheckBox != null && vCheckBox instanceof CheckBox) { CheckBox checkBox = (CheckBox) vCheckBox; - checkBox.setChecked(mEnabledAlbums.contains(data.id)); + checkBox.setChecked(mSettings.isAlbumEnabled(data.id)); checkBox.setTag(R.id.data_payload, data); } @@ -155,14 +152,7 @@ public class AlbumDataAdapter extends ArrayAdapter { (PhotoSource.AlbumData) checkBox.getTag(R.id.data_payload); final boolean isChecked = !checkBox.isChecked(); checkBox.setChecked(isChecked); - - if (isChecked) { - mEnabledAlbums.add(data.id); - } else { - mEnabledAlbums.remove(data.id); - } - - AlbumSettings.setEnabledAlbums(mSettings , mEnabledAlbums); + mSettings.setAlbumEnabled(data.id, isChecked); if (DEBUG) Log.i(TAG, data.title + " is " + (isChecked ? "" : "not") + " enabled"); } else { diff --git a/src/com/android/dreams/phototable/AlbumSettings.java b/src/com/android/dreams/phototable/AlbumSettings.java index 502cd3f..a719d4f 100644 --- a/src/com/android/dreams/phototable/AlbumSettings.java +++ b/src/com/android/dreams/phototable/AlbumSettings.java @@ -17,6 +17,7 @@ package com.android.dreams.phototable; import android.content.SharedPreferences; +import java.util.HashMap; import java.util.HashSet; import java.util.Set; @@ -26,22 +27,72 @@ import java.util.Set; public class AlbumSettings { public static final String ALBUM_SET = "Enabled Album Set V2"; - public static Set getEnabledAlbums(SharedPreferences settings) { - Set enabled = settings.getStringSet(ALBUM_SET, null); - if (enabled == null) { - enabled = new HashSet(); - setEnabledAlbums(settings, enabled); + private static HashMap singletons; + + private final SharedPreferences mSettings; + private final HashSet mEnabledAlbums; + + public static AlbumSettings getAlbumSettings(SharedPreferences settings) { + if (singletons == null) { + singletons = new HashMap(); + } + if (!singletons.containsKey(settings)) { + singletons.put(settings, new AlbumSettings(settings)); + } + return singletons.get(settings); + } + + public void readEnabledAlbums() { + synchronized (mEnabledAlbums) { + readEnabledAlbumsLocked(); + } + } + + public boolean isAlbumEnabled(String albumId) { + synchronized (mEnabledAlbums) { + boolean isEnabled = mEnabledAlbums.contains(albumId); + return mEnabledAlbums.contains(albumId); } - return enabled; } - public static void setEnabledAlbums(SharedPreferences settings, Set value) { - SharedPreferences.Editor editor = settings.edit(); - editor.putStringSet(ALBUM_SET, value); - editor.apply(); + public void setAlbumEnabled(String albumId, boolean enabled) { + if (isAlbumEnabled(albumId) != enabled) { + synchronized (mEnabledAlbums) { + readEnabledAlbumsLocked(); + if (enabled) { + mEnabledAlbums.add(albumId); + } else { + mEnabledAlbums.remove(albumId); + } + writeEnabledAlbumsLocked(); + } + } + } + + public boolean isConfigured() { + synchronized (mEnabledAlbums) { + return mEnabledAlbums.size() != 0; + } + } + + private AlbumSettings(SharedPreferences settings) { + mSettings = settings; + mEnabledAlbums = new HashSet(); + readEnabledAlbums(); + } + + private void readEnabledAlbumsLocked() { + Set enabledAlbums = mSettings.getStringSet(ALBUM_SET, null); + mEnabledAlbums.clear(); + if (enabledAlbums != null) { + mEnabledAlbums.addAll(enabledAlbums); + } } - public static boolean isConfigured(SharedPreferences settings) { - return getEnabledAlbums(settings).size() != 0; + private void writeEnabledAlbumsLocked() { + SharedPreferences.Editor editor = mSettings.edit(); + // Give SharedSettings a copy, so that we are free to manipulate ours. + editor.putStringSet(ALBUM_SET, new HashSet(mEnabledAlbums)); + editor.commit(); } } diff --git a/src/com/android/dreams/phototable/FlipperDream.java b/src/com/android/dreams/phototable/FlipperDream.java index bbcdfb0..9953f32 100644 --- a/src/com/android/dreams/phototable/FlipperDream.java +++ b/src/com/android/dreams/phototable/FlipperDream.java @@ -33,9 +33,9 @@ public class FlipperDream extends DreamService { @Override public void onAttachedToWindow() { super.onAttachedToWindow(); - - SharedPreferences settings = getSharedPreferences(FlipperDreamSettings.PREFS_NAME, 0); - if (AlbumSettings.isConfigured(settings)) { + AlbumSettings settings = AlbumSettings.getAlbumSettings( + getSharedPreferences(FlipperDreamSettings.PREFS_NAME, 0)); + if (settings.isConfigured()) { setContentView(R.layout.carousel); } else { setContentView(R.layout.bummer); diff --git a/src/com/android/dreams/phototable/FlipperDreamSettings.java b/src/com/android/dreams/phototable/FlipperDreamSettings.java index cac415c..d0d37bb 100644 --- a/src/com/android/dreams/phototable/FlipperDreamSettings.java +++ b/src/com/android/dreams/phototable/FlipperDreamSettings.java @@ -17,6 +17,7 @@ package com.android.dreams.phototable; import android.content.SharedPreferences; import android.app.ListActivity; +import android.os.AsyncTask; import android.os.Bundle; import android.widget.ListAdapter; @@ -37,17 +38,25 @@ public class FlipperDreamSettings extends ListActivity { protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); - //setContentView(R.layout.custom_list_activity_view); - mSettings = getSharedPreferences(PREFS_NAME, 0); - mPhotoSource = new PhotoSourcePlexor(this, mSettings); - mAdapter = new SectionedAlbumDataAdapter(this, - mSettings, - R.layout.header, - R.layout.album, - new LinkedList(mPhotoSource.findAlbums())); - setListAdapter(mAdapter); setContentView(R.layout.settingslist); + + new AsyncTask() { + @Override + public Void doInBackground(Void... unused) { + mAdapter = new SectionedAlbumDataAdapter(FlipperDreamSettings.this, + mSettings, + R.layout.header, + R.layout.album, + new LinkedList(mPhotoSource.findAlbums())); + return null; + } + + @Override + public void onPostExecute(Void unused) { + setListAdapter(mAdapter); + } + }.execute(); } } diff --git a/src/com/android/dreams/phototable/LocalSource.java b/src/com/android/dreams/phototable/LocalSource.java index e453378..1614bb9 100644 --- a/src/com/android/dreams/phototable/LocalSource.java +++ b/src/com/android/dreams/phototable/LocalSource.java @@ -25,6 +25,7 @@ import java.io.InputStream; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; +import java.util.Set; /** * Loads images from the local store. @@ -34,6 +35,7 @@ public class LocalSource extends PhotoSource { private final String mUnknownAlbumName; private final String mLocalSourceName; + private Set mFoundAlbumIds; private int mNextPosition; public LocalSource(Context context, SharedPreferences settings) { @@ -45,6 +47,13 @@ public class LocalSource extends PhotoSource { fillQueue(); } + private Set getFoundAlbums() { + if (mFoundAlbumIds == null) { + findAlbums(); + } + return mFoundAlbumIds; + } + @Override public Collection findAlbums() { log(TAG, "finding albums"); @@ -100,6 +109,7 @@ public class LocalSource extends PhotoSource { } log(TAG, "found " + foundAlbums.size() + " items."); + mFoundAlbumIds = foundAlbums.keySet(); return foundAlbums.values(); } @@ -111,8 +121,8 @@ public class LocalSource extends PhotoSource { String[] projection = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION, MediaStore.Images.Media.BUCKET_ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME}; String selection = ""; - for (String id : AlbumSettings.getEnabledAlbums(mSettings)) { - if (id.startsWith(TAG)) { + for (String id : getFoundAlbums()) { + if (mSettings.isAlbumEnabled(id)) { String[] parts = id.split(":"); if (parts.length > 1) { if (selection.length() > 0) { diff --git a/src/com/android/dreams/phototable/PhotoSource.java b/src/com/android/dreams/phototable/PhotoSource.java index 366ce20..06dd816 100644 --- a/src/com/android/dreams/phototable/PhotoSource.java +++ b/src/com/android/dreams/phototable/PhotoSource.java @@ -79,7 +79,7 @@ public abstract class PhotoSource { protected final Context mContext; protected final Resources mResources; protected final Random mRNG; - protected final SharedPreferences mSettings; + protected final AlbumSettings mSettings; protected final ContentResolver mResolver; protected String mSourceName; @@ -91,7 +91,7 @@ public abstract class PhotoSource { public PhotoSource(Context context, SharedPreferences settings, PhotoSource fallbackSource) { mSourceName = TAG; mContext = context; - mSettings = settings; + mSettings = AlbumSettings.getAlbumSettings(settings); mResolver = mContext.getContentResolver(); mResources = context.getResources(); mImageQueue = new LinkedList(); diff --git a/src/com/android/dreams/phototable/PhotoTableDream.java b/src/com/android/dreams/phototable/PhotoTableDream.java index ba79769..20fd566 100644 --- a/src/com/android/dreams/phototable/PhotoTableDream.java +++ b/src/com/android/dreams/phototable/PhotoTableDream.java @@ -42,9 +42,9 @@ public class PhotoTableDream extends DreamService { super.onAttachedToWindow(); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); - SharedPreferences settings = getSharedPreferences(PhotoTableDreamSettings.PREFS_NAME, 0); - Set enabledAlbums = AlbumSettings.getEnabledAlbums(settings); - if (AlbumSettings.isConfigured(settings)) { + AlbumSettings settings = AlbumSettings.getAlbumSettings( + getSharedPreferences(PhotoTableDreamSettings.PREFS_NAME, 0)); + if (settings.isConfigured()) { ViewGroup view = (ViewGroup) inflater.inflate(R.layout.table, null); PhotoTable table = (PhotoTable) view.findViewById(R.id.table); table.setDream(this); diff --git a/src/com/android/dreams/phototable/PhotoTableDreamSettings.java b/src/com/android/dreams/phototable/PhotoTableDreamSettings.java index 4340eba..6f7e9f1 100644 --- a/src/com/android/dreams/phototable/PhotoTableDreamSettings.java +++ b/src/com/android/dreams/phototable/PhotoTableDreamSettings.java @@ -17,6 +17,7 @@ package com.android.dreams.phototable; import android.content.SharedPreferences; import android.app.ListActivity; +import android.os.AsyncTask; import android.os.Bundle; import android.widget.ListAdapter; @@ -37,17 +38,25 @@ public class PhotoTableDreamSettings extends ListActivity { protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); - //setContentView(R.layout.custom_list_activity_view); - mSettings = getSharedPreferences(PREFS_NAME, 0); - mPhotoSource = new PhotoSourcePlexor(this, mSettings); - mAdapter = new SectionedAlbumDataAdapter(this, - mSettings, - R.layout.header, - R.layout.album, - new LinkedList(mPhotoSource.findAlbums())); - setListAdapter(mAdapter); setContentView(R.layout.settingslist); + + new AsyncTask() { + @Override + public Void doInBackground(Void... unused) { + mAdapter = new SectionedAlbumDataAdapter(PhotoTableDreamSettings.this, + mSettings, + R.layout.header, + R.layout.album, + new LinkedList(mPhotoSource.findAlbums())); + return null; + } + + @Override + public void onPostExecute(Void unused) { + setListAdapter(mAdapter); + } + }.execute(); } } diff --git a/src/com/android/dreams/phototable/PicasaSource.java b/src/com/android/dreams/phototable/PicasaSource.java index 92adfa6..bb1f1ec 100644 --- a/src/com/android/dreams/phototable/PicasaSource.java +++ b/src/com/android/dreams/phototable/PicasaSource.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; +import java.util.Set; /** * Loads images from Picasa. @@ -65,6 +66,7 @@ public class PicasaSource extends PhotoSource { private final String mUploadsAlbumName; private final String mUnknownAlbumName; + private Set mFoundAlbumIds; private int mNextPosition; public PicasaSource(Context context, SharedPreferences settings) { @@ -85,8 +87,8 @@ public class PicasaSource extends PhotoSource { String[] projection = {PICASA_ID, PICASA_URL, PICASA_ROTATION, PICASA_ALBUM_ID}; boolean usePosts = false; LinkedList albumIds = new LinkedList(); - for (String id : AlbumSettings.getEnabledAlbums(mSettings)) { - if (id.startsWith(TAG)) { + for (String id : getFoundAlbums()) { + if (mSettings.isAlbumEnabled(id)) { String[] parts = id.split(":"); if (parts.length > 2) { albumIds.addAll(resolveAlbumIds(id)); @@ -225,6 +227,13 @@ public class PicasaSource extends PhotoSource { return albumIds; } + private Set getFoundAlbums() { + if (mFoundAlbumIds == null) { + findAlbums(); + } + return mFoundAlbumIds; + } + @Override public Collection findAlbums() { log(TAG, "finding albums"); @@ -319,6 +328,7 @@ public class PicasaSource extends PhotoSource { } log(TAG, "found " + foundAlbums.size() + " items."); + mFoundAlbumIds = foundAlbums.keySet(); return foundAlbums.values(); } -- cgit v1.2.3