1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.dreams.phototable; 17 18 import android.app.ListActivity; 19 import android.content.SharedPreferences; 20 import android.database.DataSetObserver; 21 import android.os.AsyncTask; 22 import android.os.AsyncTask.Status; 23 import android.os.Bundle; 24 import android.view.Menu; 25 import android.view.MenuInflater; 26 import android.view.MenuItem; 27 import android.view.View; 28 29 import java.util.LinkedList; 30 31 /** 32 * Settings panel for photo flipping dream. 33 */ 34 public class FlipperDreamSettings extends ListActivity { 35 @SuppressWarnings("unused") 36 private static final String TAG = "FlipperDreamSettings"; 37 public static final String PREFS_NAME = FlipperDream.TAG; 38 39 protected SharedPreferences mSettings; 40 41 private PhotoSourcePlexor mPhotoSource; 42 private SectionedAlbumDataAdapter mAdapter; 43 private MenuItem mSelectAll; 44 private AsyncTask<Void, Void, Void> mLoadingTask; 45 46 @Override 47 protected void onCreate(Bundle savedInstanceState){ 48 super.onCreate(savedInstanceState); 49 mSettings = getSharedPreferences(PREFS_NAME, 0); 50 init(); 51 } 52 53 @Override 54 protected void onResume(){ 55 super.onResume(); 56 init(); 57 } 58 59 protected void init() { 60 mPhotoSource = new PhotoSourcePlexor(this, mSettings); 61 setContentView(R.layout.settingslist); 62 if (mLoadingTask != null && mLoadingTask.getStatus() != Status.FINISHED) { 63 mLoadingTask.cancel(true); 64 } 65 showApology(false); 66 mLoadingTask = new AsyncTask<Void, Void, Void>() { 67 @Override 68 public Void doInBackground(Void... unused) { 69 mAdapter = new SectionedAlbumDataAdapter(FlipperDreamSettings.this, 70 mSettings, 71 R.layout.header, 72 R.layout.album, 73 new LinkedList<PhotoSource.AlbumData>(mPhotoSource.findAlbums())); 74 return null; 75 } 76 77 @Override 78 public void onPostExecute(Void unused) { 79 mAdapter.registerDataSetObserver(new DataSetObserver () { 80 @Override 81 public void onChanged() { 82 updateActionItem(); 83 } 84 @Override 85 public void onInvalidated() { 86 updateActionItem(); 87 } 88 }); 89 setListAdapter(mAdapter); 90 getListView().setItemsCanFocus(true); 91 updateActionItem(); 92 showApology(mAdapter.getCount() == 0); 93 } 94 }; 95 mLoadingTask.execute(); 96 } 97 98 @Override 99 public boolean onCreateOptionsMenu(Menu menu) { 100 MenuInflater inflater = getMenuInflater(); 101 inflater.inflate(R.menu.photodream_settings_menu, menu); 102 mSelectAll = menu.findItem(R.id.photodream_menu_all); 103 updateActionItem(); 104 return true; 105 } 106 107 @Override 108 public boolean onOptionsItemSelected(MenuItem item) { 109 switch (item.getItemId()) { 110 case R.id.photodream_menu_all: 111 if (mAdapter != null) { 112 mAdapter.selectAll(!mAdapter.areAllSelected()); 113 } 114 return true; 115 default: 116 return super.onOptionsItemSelected(item); 117 } 118 } 119 120 private void showApology(boolean apologize) { 121 View empty = findViewById(R.id.spinner); 122 View sorry = findViewById(R.id.sorry); 123 if (empty != null && sorry != null) { 124 empty.setVisibility(apologize ? View.GONE : View.VISIBLE); 125 sorry.setVisibility(apologize ? View.VISIBLE : View.GONE); 126 } 127 } 128 129 private void updateActionItem() { 130 if (mAdapter != null && mSelectAll != null) { 131 if (mAdapter.areAllSelected()) { 132 mSelectAll.setTitle(R.string.photodream_select_none); 133 } else { 134 mSelectAll.setTitle(R.string.photodream_select_all); 135 } 136 } 137 } 138 } 139