diff options
author | Chris Wren <cwren@android.com> | 2012-12-04 17:35:11 -0500 |
---|---|---|
committer | Chris Wren <cwren@android.com> | 2012-12-10 10:48:47 -0500 |
commit | 2ccf92a79a2749a6fdaf5c1242f5d72de48ef111 (patch) | |
tree | f1cb3df449c936501913735d09da38ec8dd642b9 | |
parent | 387363c426e097c358bd41367e0a8f4339fc8dc3 (diff) | |
download | android_packages_screensavers_PhotoTable-2ccf92a79a2749a6fdaf5c1242f5d72de48ef111.tar.gz android_packages_screensavers_PhotoTable-2ccf92a79a2749a6fdaf5c1242f5d72de48ef111.tar.bz2 android_packages_screensavers_PhotoTable-2ccf92a79a2749a6fdaf5c1242f5d72de48ef111.zip |
Add 'select all' to photo dream settings.
Bug: 7269182
Change-Id: I78898bb9e76d6233487b4888e56f896f3c6ea72d
-rw-r--r-- | res/layout/settingslist.xml | 2 | ||||
-rw-r--r-- | res/menu/photodream_settings_menu.xml | 21 | ||||
-rw-r--r-- | res/values/strings.xml | 7 | ||||
-rw-r--r-- | src/com/android/dreams/phototable/AlbumDataAdapter.java | 28 | ||||
-rw-r--r-- | src/com/android/dreams/phototable/AlbumSettings.java | 21 | ||||
-rw-r--r-- | src/com/android/dreams/phototable/FlipperDreamSettings.java | 56 | ||||
-rw-r--r-- | src/com/android/dreams/phototable/PhotoTableDreamSettings.java | 30 | ||||
-rw-r--r-- | src/com/android/dreams/phototable/SectionedAlbumDataAdapter.java | 10 |
8 files changed, 142 insertions, 33 deletions
diff --git a/res/layout/settingslist.xml b/res/layout/settingslist.xml index 1f2128b..0ea6661 100644 --- a/res/layout/settingslist.xml +++ b/res/layout/settingslist.xml @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. --> - <merge xmlns:android="http://schemas.android.com/apk/res/android"> +<merge xmlns:android="http://schemas.android.com/apk/res/android"> <ListView android:id="@android:id/list" android:layout_width="match_parent" diff --git a/res/menu/photodream_settings_menu.xml b/res/menu/photodream_settings_menu.xml new file mode 100644 index 0000000..3ae368f --- /dev/null +++ b/res/menu/photodream_settings_menu.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2012 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<menu xmlns:android="http://schemas.android.com/apk/res/android"> + <item android:id="@+id/photodream_menu_all" + android:title="@string/photodream_select_all" + android:showAsAction="ifRoom" + /> +</menu> diff --git a/res/values/strings.xml b/res/values/strings.xml index 858d6c1..087f8b1 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -40,4 +40,11 @@ <!-- Instruct the user to configure the screensaver in settings to see photos. --> <string name="need_to_configure">No photos selected.</string> + + <!-- Click to select all albums in the album selection activity. [CHAR LIMIT=15] --> + <string name="photodream_select_all">Select All</string> + + <!-- Click to clear selection in the album selection activity. [CHAR LIMIT=15] --> + <string name="photodream_select_none">Deselect All</string> + </resources> diff --git a/src/com/android/dreams/phototable/AlbumDataAdapter.java b/src/com/android/dreams/phototable/AlbumDataAdapter.java index a0c039b..699fe14 100644 --- a/src/com/android/dreams/phototable/AlbumDataAdapter.java +++ b/src/com/android/dreams/phototable/AlbumDataAdapter.java @@ -45,6 +45,7 @@ public class AlbumDataAdapter extends ArrayAdapter<PhotoSource.AlbumData> { private final LayoutInflater mInflater; private final int mLayout; private final ItemClickListener mListener; + private final HashSet<String> mValidAlbumIds; public AlbumDataAdapter(Context context, SharedPreferences settings, int resource, List<PhotoSource.AlbumData> objects) { @@ -54,11 +55,29 @@ public class AlbumDataAdapter extends ArrayAdapter<PhotoSource.AlbumData> { mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mListener = new ItemClickListener(); - HashSet<String> validAlbumIds = new HashSet<String>(objects.size()); + mValidAlbumIds = new HashSet<String>(objects.size()); for (PhotoSource.AlbumData albumData: objects) { - validAlbumIds.add(albumData.id); + mValidAlbumIds.add(albumData.id); } - mSettings.pruneObsoleteSettings(validAlbumIds); + mSettings.pruneObsoleteSettings(mValidAlbumIds); + } + + public boolean isSelected(int position) { + PhotoSource.AlbumData data = getItem(position); + return mSettings.isAlbumEnabled(data.id); + } + + public boolean areAllSelected() { + return mSettings.areAllEnabled(mValidAlbumIds); + } + + public void selectAll(boolean select) { + if (select) { + mSettings.enableAllAlbums(mValidAlbumIds); + } else { + mSettings.disableAllAlbums(); + } + notifyDataSetChanged(); } @Override @@ -72,7 +91,7 @@ public class AlbumDataAdapter extends ArrayAdapter<PhotoSource.AlbumData> { View vCheckBox = item.findViewById(R.id.enabled); if (vCheckBox != null && vCheckBox instanceof CheckBox) { CheckBox checkBox = (CheckBox) vCheckBox; - checkBox.setChecked(mSettings.isAlbumEnabled(data.id)); + checkBox.setChecked(isSelected(position)); checkBox.setTag(R.id.data_payload, data); } @@ -159,6 +178,7 @@ public class AlbumDataAdapter extends ArrayAdapter<PhotoSource.AlbumData> { final boolean isChecked = !checkBox.isChecked(); checkBox.setChecked(isChecked); mSettings.setAlbumEnabled(data.id, isChecked); + notifyDataSetChanged(); 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 23dda46..069948b 100644 --- a/src/com/android/dreams/phototable/AlbumSettings.java +++ b/src/com/android/dreams/phototable/AlbumSettings.java @@ -56,6 +56,12 @@ public class AlbumSettings { } } + public boolean areAllEnabled(Collection<String> validAlbums) { + synchronized (mEnabledAlbums) { + return mEnabledAlbums.containsAll(validAlbums); + } + } + public void setAlbumEnabled(String albumId, boolean enabled) { if (isAlbumEnabled(albumId) != enabled) { synchronized (mEnabledAlbums) { @@ -70,6 +76,21 @@ public class AlbumSettings { } } + public void disableAllAlbums() { + synchronized (mEnabledAlbums) { + mEnabledAlbums.clear(); + writeEnabledAlbumsLocked(); + } + } + + public void enableAllAlbums(Collection<String> validAlbums) { + synchronized (mEnabledAlbums) { + mEnabledAlbums.clear(); + mEnabledAlbums.addAll(validAlbums); + writeEnabledAlbumsLocked(); + } + } + public void pruneObsoleteSettings(Collection<String> validAlbums) { if (!validAlbums.containsAll(mEnabledAlbums)) { synchronized (mEnabledAlbums) { diff --git a/src/com/android/dreams/phototable/FlipperDreamSettings.java b/src/com/android/dreams/phototable/FlipperDreamSettings.java index 1252846..8e6fac4 100644 --- a/src/com/android/dreams/phototable/FlipperDreamSettings.java +++ b/src/com/android/dreams/phototable/FlipperDreamSettings.java @@ -16,9 +16,13 @@ package com.android.dreams.phototable; import android.content.SharedPreferences; +import android.database.DataSetObserver; import android.app.ListActivity; import android.os.AsyncTask; import android.os.Bundle; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.widget.ListAdapter; @@ -31,15 +35,20 @@ public class FlipperDreamSettings extends ListActivity { private static final String TAG = "FlipperDreamSettings"; public static final String PREFS_NAME = FlipperDream.TAG; + protected SharedPreferences mSettings; + private PhotoSourcePlexor mPhotoSource; - private ListAdapter mAdapter; - private SharedPreferences mSettings; + private SectionedAlbumDataAdapter mAdapter; + private MenuItem mSelectAll; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); - mSettings = getSharedPreferences(PREFS_NAME, 0); + init(); + } + + protected void init() { mPhotoSource = new PhotoSourcePlexor(this, mSettings); setContentView(R.layout.settingslist); @@ -56,11 +65,52 @@ public class FlipperDreamSettings extends ListActivity { @Override public void onPostExecute(Void unused) { + mAdapter.registerDataSetObserver(new DataSetObserver () { + @Override + public void onChanged() { + updateActionItem(); + } + @Override + public void onInvalidated() { + updateActionItem(); + } + }); setListAdapter(mAdapter); + updateActionItem(); if (mAdapter.getCount() == 0) { findViewById(android.R.id.empty).setVisibility(View.GONE); } } }.execute(); } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.photodream_settings_menu, menu); + mSelectAll = menu.findItem(R.id.photodream_menu_all); + updateActionItem(); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.photodream_menu_all: + mAdapter.selectAll(!mAdapter.areAllSelected()); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + private void updateActionItem() { + if (mAdapter != null && mSelectAll != null) { + if (mAdapter.areAllSelected()) { + mSelectAll.setTitle(R.string.photodream_select_none); + } else { + mSelectAll.setTitle(R.string.photodream_select_all); + } + } + } } diff --git a/src/com/android/dreams/phototable/PhotoTableDreamSettings.java b/src/com/android/dreams/phototable/PhotoTableDreamSettings.java index 6f7e9f1..a42d4a6 100644 --- a/src/com/android/dreams/phototable/PhotoTableDreamSettings.java +++ b/src/com/android/dreams/phototable/PhotoTableDreamSettings.java @@ -19,6 +19,9 @@ import android.content.SharedPreferences; import android.app.ListActivity; import android.os.AsyncTask; import android.os.Bundle; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.widget.ListAdapter; import java.util.LinkedList; @@ -26,37 +29,14 @@ import java.util.LinkedList; /** * Settings panel for photo flipping dream. */ -public class PhotoTableDreamSettings extends ListActivity { +public class PhotoTableDreamSettings extends FlipperDreamSettings { private static final String TAG = "PhotoTableDreamSettings"; public static final String PREFS_NAME = PhotoTableDream.TAG; - private PhotoSourcePlexor mPhotoSource; - private ListAdapter mAdapter; - private SharedPreferences mSettings; - @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); - mSettings = getSharedPreferences(PREFS_NAME, 0); - mPhotoSource = new PhotoSourcePlexor(this, mSettings); - setContentView(R.layout.settingslist); - - new AsyncTask<Void, Void, Void>() { - @Override - public Void doInBackground(Void... unused) { - mAdapter = new SectionedAlbumDataAdapter(PhotoTableDreamSettings.this, - mSettings, - R.layout.header, - R.layout.album, - new LinkedList<PhotoSource.AlbumData>(mPhotoSource.findAlbums())); - return null; - } - - @Override - public void onPostExecute(Void unused) { - setListAdapter(mAdapter); - } - }.execute(); + init(); } } diff --git a/src/com/android/dreams/phototable/SectionedAlbumDataAdapter.java b/src/com/android/dreams/phototable/SectionedAlbumDataAdapter.java index 6dc3ddf..6c5a88a 100644 --- a/src/com/android/dreams/phototable/SectionedAlbumDataAdapter.java +++ b/src/com/android/dreams/phototable/SectionedAlbumDataAdapter.java @@ -53,6 +53,16 @@ public class SectionedAlbumDataAdapter extends DataSetObserver implements ListAd mAlbumData.registerDataSetObserver(this); } + boolean areAllSelected() { + return mAlbumData != null && mAlbumData.areAllSelected(); + } + + void selectAll(boolean select) { + if (mAlbumData != null) { + mAlbumData.selectAll(select); + } + } + // DataSetObserver @Override |