add OI File Manager and AndroidSupportV2 used by it
[android_pandora.git] / apps / oi-filemanager / FileManager / src / org / openintents / filemanager / PreferenceActivity.java
CommitLineData
811a5a4a 1/*
2 * Copyright (C) 2008 OpenIntents.org
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
17package org.openintents.filemanager;
18
19import java.util.LinkedList;
20import java.util.List;
21
22import android.app.AlertDialog;
23import android.app.Dialog;
24import android.content.ContentUris;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.DialogInterface;
28import android.content.SharedPreferences;
29import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
30import android.database.Cursor;
31import android.net.Uri;
32import android.os.Bundle;
33import android.preference.ListPreference;
34import android.preference.Preference;
35import android.preference.Preference.OnPreferenceClickListener;
36import android.preference.PreferenceManager;
37import android.widget.Toast;
38
39public class PreferenceActivity extends android.preference.PreferenceActivity
40 implements OnSharedPreferenceChangeListener {
41
42 public static final String PREFS_MEDIASCAN = "mediascan";
43 /**
44 * @since 2011-09-30
45 */
46 public static final String PREFS_SHOWALLWARNING = "showallwarning";
47
48
49 public static final String PREFS_DISPLAYHIDDENFILES = "displayhiddenfiles";
50
51 public static final String PREFS_SORTBY = "sortby";
52
53 public static final String PREFS_ASCENDING = "ascending";
54
55 public static final String PREFS_DEFAULTPICKFILEPATH = "defaultpickfilepath";
56
57 private static final int DIALOG_DELETE_BOOKMARKS = 1;
58
59 private Cursor deleteBookmarksCursor;
60 private List<Uri> bookmarksToDelete = new LinkedList<Uri>();
61
62 @Override
63 protected void onCreate(Bundle icicle) {
64
65 super.onCreate(icicle);
66
67 addPreferencesFromResource(R.xml.preferences);
68
69 Preference editBookmarks = findPreference("editbookmarks");
70 editBookmarks.setOnPreferenceClickListener(new OnPreferenceClickListener() {
71 public boolean onPreferenceClick(Preference pref){
72 showDialog(DIALOG_DELETE_BOOKMARKS);
73 return false;
74 }
75 });
76
77 /* Register the onSharedPreferenceChanged listener to update the SortBy ListPreference summary */
78 getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
79 /* Set the onSharedPreferenceChanged listener summary to its initial value */
80 changeListPreferenceSummaryToCurrentValue((ListPreference)findPreference("sortby"));
81 }
82
83 @Override
84 protected void onResume() {
85 super.onResume();
86 }
87
88 static boolean getMediaScanFromPreference(Context context) {
89 return PreferenceManager.getDefaultSharedPreferences(context)
90 .getBoolean(PREFS_MEDIASCAN, false);
91 }
92
93 /**
94 * @since 2011-09-30
95 */
96 static void setShowAllWarning(Context context, boolean enabled) {
97 SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
98 SharedPreferences.Editor editor = settings.edit();
99 editor.putBoolean(PREFS_SHOWALLWARNING, enabled);
100 editor.commit();
101 }
102
103 /**
104 * @since 2011-09-30
105 */
106 static boolean getShowAllWarning(Context context) {
107 return PreferenceManager.getDefaultSharedPreferences(context)
108 .getBoolean(PREFS_SHOWALLWARNING, true);
109 }
110
111
112
113 static void setDisplayHiddenFiles(Context context, boolean enabled) {
114 SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
115 SharedPreferences.Editor editor = settings.edit();
116 editor.putBoolean(PREFS_DISPLAYHIDDENFILES, enabled);
117 editor.commit();
118 }
119
120
121 static boolean getDisplayHiddenFiles(Context context) {
122 return PreferenceManager.getDefaultSharedPreferences(context)
123 .getBoolean(PREFS_DISPLAYHIDDENFILES, true);
124 }
125
126 static void setDefaultPickFilePath(Context context, String path) {
127 SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
128 SharedPreferences.Editor editor = settings.edit();
129 editor.putString(PREFS_DEFAULTPICKFILEPATH, path);
130 editor.commit();
131 }
132
133
134 static String getDefaultPickFilePath(Context context) {
135 return PreferenceManager.getDefaultSharedPreferences(context)
136 .getString(PREFS_DEFAULTPICKFILEPATH, null);
137 }
138
139
140 @Override
141 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
142 if(key.equals("sortby")){
143 changeListPreferenceSummaryToCurrentValue((ListPreference)findPreference(key));
144 }
145 }
146
147 private void changeListPreferenceSummaryToCurrentValue(ListPreference listPref){
148 listPref.setSummary(listPref.getEntry());
149 }
150
151
152 static int getSortBy(Context context) {
153 /* entryValues must be a string-array while we need integers */
154 return Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(context)
155 .getString(PREFS_SORTBY, "1"));
156 }
157
158 static boolean getAscending(Context context) {
159 return PreferenceManager.getDefaultSharedPreferences(context)
160 .getBoolean(PREFS_ASCENDING, true);
161 }
162
163 @Override
164 protected Dialog onCreateDialog(int id) {
165 switch (id) {
166 case DIALOG_DELETE_BOOKMARKS:
167 deleteBookmarksCursor = getBookmarksCursor();
168 AlertDialog dialog =
169 new AlertDialog.Builder(this)
170 .setTitle(R.string.bookmarks_select_to_delete)
171 .setMultiChoiceItems(deleteBookmarksCursor,
172 BookmarksProvider.CHECKED, BookmarksProvider.NAME,
173 new DialogInterface.OnMultiChoiceClickListener() {
174 public void onClick(DialogInterface dialog, int item, boolean checked) {
175 if (deleteBookmarksCursor.moveToPosition(item)) {
176 Uri deleteUri = ContentUris.withAppendedId(
177 BookmarksProvider.CONTENT_URI,
178 deleteBookmarksCursor.getInt(
179 deleteBookmarksCursor.getColumnIndex(
180 BookmarksProvider._ID)));
181 if(checked)
182 bookmarksToDelete.add(deleteUri);
183 else
184 bookmarksToDelete.remove(deleteUri);
185
186
187 ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE)
188 .setEnabled((bookmarksToDelete.size() > 0) ? true : false);
189
190 ContentValues checkedValues = new ContentValues();
191 checkedValues.put(BookmarksProvider.CHECKED, checked ? 1 : 0);
192 getContentResolver().update(deleteUri, checkedValues, null, null);
193 //Have to use the deprecated requery()
194 //(see http://code.google.com/p/android/issues/detail?id=2998)
195 deleteBookmarksCursor.requery();
196 }
197 ((AlertDialog)dialog).getListView().invalidate();
198 }
199 })
200 .setPositiveButton(R.string.bookmarks_delete, new DialogInterface.OnClickListener() {
201
202 @Override
203 public void onClick(DialogInterface dialog, int which) {
204 for(Uri uri : bookmarksToDelete){
205 getContentResolver().delete(uri, null, null);
206 }
207 Toast.makeText(PreferenceActivity.this,
208 R.string.bookmarks_deleted, Toast.LENGTH_SHORT).show();
209 restartBookmarksChecked();
210 }
211 })
212 .setNegativeButton(R.string.bookmarks_cancel, new DialogInterface.OnClickListener() {
213 public void onClick(DialogInterface dialog, int item) {
214 restartBookmarksChecked();
215 }
216 }).create();
217 // TODO: need to fix
218 /* Commenting this out for now. Need another way to do this or check for SDK > 7.
219 * With this in, Android 1.5 crashes upon launch.
220 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
221 @Override
222 public void onShow(DialogInterface dialog) {
223 ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
224 }
225 });*/
226 return dialog;
227 }
228 return super.onCreateDialog(id);
229 }
230
231 private void restartBookmarksChecked(){
232 ContentValues checkedValues = new ContentValues();
233 checkedValues.put(BookmarksProvider.CHECKED, 0);
234 getContentResolver().update(BookmarksProvider.CONTENT_URI, checkedValues, null, null);
235 deleteBookmarksCursor.requery();
236 bookmarksToDelete.clear();
237 }
238
239 private Cursor getBookmarksCursor(){
240 return managedQuery(BookmarksProvider.CONTENT_URI,
241 new String[] {
242 BookmarksProvider._ID,
243 BookmarksProvider.NAME,
244 BookmarksProvider.PATH,
245 BookmarksProvider.CHECKED
246 }, null, null, null);
247 }
248}