diff options
-rw-r--r-- | AndroidManifest.xml | 5 | ||||
-rw-r--r-- | res/layout/chooser_list.xml | 16 | ||||
-rw-r--r-- | res/layout/list_item.xml | 7 | ||||
-rw-r--r-- | res/values/strings.xml | 2 | ||||
-rw-r--r-- | res/xml/preferences.xml | 4 | ||||
-rw-r--r-- | src/org/fox/ttcomics/DirectoryPicker.java | 154 | ||||
-rw-r--r-- | src/org/fox/ttcomics/PreferencesActivity.java | 40 |
7 files changed, 222 insertions, 6 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 70e8638..c948ad6 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -38,6 +38,11 @@ android:name=".CommonActivity"
android:label="@string/title_activity_main">
</activity>
+
+ <activity
+ android:name=".DirectoryPicker"
+ android:label="@string/title_activity_main">
+ </activity>
</application>
diff --git a/res/layout/chooser_list.xml b/res/layout/chooser_list.xml new file mode 100644 index 0000000..53d28cd --- /dev/null +++ b/res/layout/chooser_list.xml @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + > + <ListView android:id="@android:id/list" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:layout_weight="1"/> + <Button android:text="Choose" + android:id="@+id/btnChoose" + android:layout_width="fill_parent" + android:layout_height="wrap_content"/> + +</LinearLayout> diff --git a/res/layout/list_item.xml b/res/layout/list_item.xml new file mode 100644 index 0000000..1fb2f8c --- /dev/null +++ b/res/layout/list_item.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="utf-8"?>
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:padding="10dp"
+ android:textSize="16sp" >
+</TextView>
\ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 53b27e2..4917c26 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -32,4 +32,6 @@ <string name="error_could_not_open_comic_archive">Could not open comic archive.</string>
<string name="error_out_of_memory">Out of memory</string>
<string name="error_loading_image">Error loading image</string>
+ <string name="error_could_not_read_folder_contents_">Could not read folder contents.</string>
+ <string name="picker_choose">Choose %1$s</string>
</resources>
\ No newline at end of file diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index 06b6544..5ccf40b 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -2,11 +2,11 @@ <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/prefs_general" >
- <EditTextPreference
+ <Preference
android:key="comics_directory"
android:hint="@string/comics_directory_default"
android:title="@string/prefs_comics_directory" >
- </EditTextPreference>
+ </Preference>
</PreferenceCategory>
<PreferenceCategory android:title="@string/prefs_reading" android:key="prefs_reading">
diff --git a/src/org/fox/ttcomics/DirectoryPicker.java b/src/org/fox/ttcomics/DirectoryPicker.java new file mode 100644 index 0000000..0f401b5 --- /dev/null +++ b/src/org/fox/ttcomics/DirectoryPicker.java @@ -0,0 +1,154 @@ +package org.fox.ttcomics; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; + +import android.app.ListActivity; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.Environment; +import android.view.View; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.ListView; +import android.widget.Toast; + +/** +Copyright (C) 2011 by Brad Greco <[email protected]> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + */ + +public class DirectoryPicker extends ListActivity { + + public static final String START_DIR = "startDir"; + public static final String ONLY_DIRS = "onlyDirs"; + public static final String SHOW_HIDDEN = "showHidden"; + public static final String CHOSEN_DIRECTORY = "chosenDir"; + public static final int PICK_DIRECTORY = 43522432; + private File dir; + private boolean showHidden = false; + private boolean onlyDirs = true ; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Bundle extras = getIntent().getExtras(); + dir = Environment.getExternalStorageDirectory(); + if (extras != null) { + String preferredStartDir = extras.getString(START_DIR); + showHidden = extras.getBoolean(SHOW_HIDDEN, false); + onlyDirs = extras.getBoolean(ONLY_DIRS, true); + if(preferredStartDir != null) { + File startDir = new File(preferredStartDir); + if(startDir.isDirectory()) { + dir = startDir; + } + } + } + + setContentView(R.layout.chooser_list); + setTitle(dir.getAbsolutePath()); + Button btnChoose = (Button) findViewById(R.id.btnChoose); + String name = dir.getName(); + if(name.length() == 0) + name = "/"; + btnChoose.setText(getString(R.string.picker_choose, name)); + btnChoose.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + returnDir(dir.getAbsolutePath()); + } + }); + + ListView lv = getListView(); + lv.setTextFilterEnabled(true); + + if(!dir.canRead()) { + Context context = getApplicationContext(); + String msg = getString(R.string.error_could_not_read_folder_contents_); + Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG); + toast.show(); + return; + } + + final ArrayList<File> files = filter(dir.listFiles(), onlyDirs, showHidden); + String[] names = names(files); + setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, names)); + + + lv.setOnItemClickListener(new OnItemClickListener() { + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + if(!files.get(position).isDirectory()) + return; + String path = files.get(position).getAbsolutePath(); + Intent intent = new Intent(DirectoryPicker.this, DirectoryPicker.class); + intent.putExtra(DirectoryPicker.START_DIR, path); + intent.putExtra(DirectoryPicker.SHOW_HIDDEN, showHidden); + intent.putExtra(DirectoryPicker.ONLY_DIRS, onlyDirs); + startActivityForResult(intent, PICK_DIRECTORY); + } + }); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if(requestCode == PICK_DIRECTORY && resultCode == RESULT_OK) { + Bundle extras = data.getExtras(); + String path = (String) extras.get(DirectoryPicker.CHOSEN_DIRECTORY); + returnDir(path); + } + } + + private void returnDir(String path) { + Intent result = new Intent(); + result.putExtra(CHOSEN_DIRECTORY, path); + setResult(RESULT_OK, result); + finish(); + } + + public ArrayList<File> filter(File[] file_list, boolean onlyDirs, boolean showHidden) { + ArrayList<File> files = new ArrayList<File>(); + + for(File file: file_list) { + if(onlyDirs && !file.isDirectory()) + continue; + if(!showHidden && file.isHidden()) + continue; + files.add(file); + } + Collections.sort(files); + + return files; + } + + public String[] names(ArrayList<File> files) { + String[] names = new String[files.size()]; + int i = 0; + for(File file: files) { + names[i] = file.getName(); + i++; + } + return names; + } +} + diff --git a/src/org/fox/ttcomics/PreferencesActivity.java b/src/org/fox/ttcomics/PreferencesActivity.java index 84029f1..b49e6fe 100644 --- a/src/org/fox/ttcomics/PreferencesActivity.java +++ b/src/org/fox/ttcomics/PreferencesActivity.java @@ -1,7 +1,9 @@ package org.fox.ttcomics;
+import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
+import android.os.Environment;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
@@ -13,15 +15,45 @@ public class PreferencesActivity extends PreferenceActivity { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- SharedPreferences prefs = PreferenceManager
+ final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
addPreferencesFromResource(R.xml.preferences);
if (CommonActivity.isCompatMode()) {
- Preference pref = findPreference("dim_status_bar");
- PreferenceCategory cat = (PreferenceCategory) findPreference("prefs_reading");
- cat.removePreference(pref);
+ Preference dimPref = findPreference("dim_status_bar");
+ PreferenceCategory readingCat = (PreferenceCategory) findPreference("prefs_reading");
+ readingCat.removePreference(dimPref);
+ }
+
+ Preference dirPref = (Preference) findPreference("comics_directory");
+ dirPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ Intent intent = new Intent(PreferencesActivity.this, DirectoryPicker.class);
+
+ //intent.putExtra(DirectoryPicker.START_DIR, prefs.getString("comics_directory",
+ // Environment.getExternalStorageDirectory().getAbsolutePath()));
+
+ startActivityForResult(intent, DirectoryPicker.PICK_DIRECTORY);
+ return true;
+ }
+ });
+ }
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ if(requestCode == DirectoryPicker.PICK_DIRECTORY && resultCode == RESULT_OK) {
+ Bundle extras = data.getExtras();
+ String path = (String) extras.get(DirectoryPicker.CHOSEN_DIRECTORY);
+
+ SharedPreferences prefs = PreferenceManager
+ .getDefaultSharedPreferences(getApplicationContext());
+
+ SharedPreferences.Editor editor = prefs.edit();
+ editor.putString("comics_directory", path);
+ editor.commit();
+
}
}
}
|