source: other-projects/tipple-android/trunk/src/org/greenstone/android/tipple/TippleLogFileBrowser.java@ 23977

Last change on this file since 23977 was 23977, checked in by davidb, 13 years ago

Initial set of files for the Tipple project, to be run on an Android device

File size: 6.6 KB
Line 
1/*
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Based on FileBrowser.java from mapsforge.org
17 *
18 */
19
20package org.greenstone.android.tipple;
21
22import java.io.File;
23import java.io.FileFilter;
24import java.util.Arrays;
25import java.util.Comparator;
26
27import android.app.Activity;
28import android.app.AlertDialog;
29import android.app.Dialog;
30import android.content.Intent;
31import android.content.SharedPreferences;
32import android.content.SharedPreferences.Editor;
33import android.os.Bundle;
34import android.preference.PreferenceManager;
35import android.view.KeyEvent;
36import android.view.View;
37import android.view.WindowManager;
38import android.widget.AdapterView;
39import android.widget.GridView;
40
41/**
42 * Simple file browser activity to select a log file from the file system.
43 */
44public class TippleLogFileBrowser extends Activity implements AdapterView.OnItemClickListener
45{
46 private static final String DEFAULT_DIRECTORY = "/";
47 private static final int DIALOG_LOG_FILE_INVALID = 0;
48 private static final int DIALOG_LOG_FILE_SELECT = 1;
49 private static final String PREFERENCES_FILE = "FileBrowser";
50 private File currentDirectory;
51 private TippleLogFileBrowserIconAdapter fileBrowserIconAdapter;
52 private Comparator<File> fileComparator;
53 private FileFilter fileFilter;
54 private File[] files;
55 private File[] filesWithParentFolder;
56 private GridView gridView;
57 private File selectedFile;
58
59 @Override
60 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
61 this.selectedFile = this.files[(int) id];
62 if (this.selectedFile.isDirectory()) {
63 this.currentDirectory = this.selectedFile;
64 browseToCurrentDirectory();
65 } else if (this.selectedFile.getAbsolutePath().endsWith(".log")) {
66 String log_filename = selectedFile.getAbsolutePath();
67 Intent ok_intent = new Intent();
68 ok_intent.putExtra("logFile", log_filename);
69
70 setResult(RESULT_OK, ok_intent);
71 finish();
72 } else {
73 showDialog(DIALOG_LOG_FILE_INVALID);
74 }
75 }
76
77 @Override
78 public boolean onKeyDown(int keyCode, KeyEvent event) {
79 if (keyCode == KeyEvent.KEYCODE_BACK) {
80 // quit the application
81 setResult(RESULT_CANCELED);
82 finish();
83 return true;
84 }
85 return false;
86 }
87
88 /**
89 * Browse to the current directory.
90 */
91 private void browseToCurrentDirectory() {
92 setTitle(this.currentDirectory.getAbsolutePath());
93
94 // read and filter files and subfolders in the current folder
95 this.files = this.currentDirectory.listFiles(this.fileFilter);
96
97 // order all files by type and alphabetically by name
98 Arrays.sort(this.files, this.fileComparator);
99
100 // if a parent directory exists, add it at the first position
101 if (this.currentDirectory.getParentFile() != null) {
102 this.filesWithParentFolder = new File[this.files.length + 1];
103 this.filesWithParentFolder[0] = this.currentDirectory.getParentFile();
104 System.arraycopy(this.files, 0, this.filesWithParentFolder, 1, this.files.length);
105 this.files = this.filesWithParentFolder;
106 this.fileBrowserIconAdapter.updateFiles(this.files, true);
107 } else {
108 this.fileBrowserIconAdapter.updateFiles(this.files, false);
109 }
110 this.fileBrowserIconAdapter.notifyDataSetChanged();
111 }
112
113 @Override
114 protected void onCreate(Bundle savedInstanceState) {
115 super.onCreate(savedInstanceState);
116 setContentView(R.layout.filebrowser);
117 this.fileBrowserIconAdapter = new TippleLogFileBrowserIconAdapter(this);
118 this.gridView = (GridView) findViewById(R.id.fileBrowserView);
119 this.gridView.setOnItemClickListener(this);
120 this.gridView.setAdapter(this.fileBrowserIconAdapter);
121
122 this.fileFilter = new FileFilter() {
123 @Override
124 public boolean accept(File pathname) {
125 if (pathname.isDirectory()) {
126 // accept all readable folders
127 return pathname.canRead();
128 }
129 // accept all readable files with the correct extension
130 return pathname.canRead() && pathname.getName().endsWith(".log");
131 }
132 };
133
134 this.fileComparator = new Comparator<File>() {
135 @Override
136 public int compare(File o1, File o2) {
137 if (o1.isDirectory() && !o2.isDirectory()) {
138 return -1;
139 } else if (!o1.isDirectory() && o2.isDirectory()) {
140 return 1;
141 } else {
142 return o1.getName().compareToIgnoreCase(o2.getName());
143 }
144 }
145 };
146
147 /*
148 if (savedInstanceState == null) {
149 // first start of this instance
150 showDialog(DIALOG_LOG_FILE_SELECT);
151 }
152
153 */
154 }
155
156 @Override
157 protected Dialog onCreateDialog(int id) {
158 AlertDialog.Builder builder = new AlertDialog.Builder(this);
159 switch (id) {
160 case DIALOG_LOG_FILE_INVALID:
161 builder.setMessage(getString(R.string.log_file_invalid)).setTitle(
162 getString(R.string.error)).setPositiveButton(getString(R.string.ok),
163 null);
164 return builder.create();
165 case DIALOG_LOG_FILE_SELECT:
166 builder.setMessage(getString(R.string.log_file_select)).setPositiveButton(
167 getString(R.string.ok), null);
168 return builder.create();
169 default:
170 return null;
171 }
172 }
173
174 @Override
175 protected void onPause() {
176 super.onPause();
177 // save the current directory
178 Editor editor = getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE).edit();
179 editor.clear();
180 if (this.currentDirectory != null) {
181 editor.putString("currentDirectory", this.currentDirectory.getAbsolutePath());
182 }
183 editor.commit();
184 }
185
186 @Override
187 protected void onResume() {
188 super.onResume();
189 // check if the full screen mode should be activated
190 if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("fullscreen", false)) {
191 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
192 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
193 } else {
194 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
195 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
196 }
197
198 // restore the current directory
199 SharedPreferences preferences = getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE);
200 this.currentDirectory = new File(preferences.getString("currentDirectory",
201 DEFAULT_DIRECTORY));
202 if (!this.currentDirectory.exists() || !this.currentDirectory.canRead()) {
203 this.currentDirectory = new File(DEFAULT_DIRECTORY);
204 }
205 browseToCurrentDirectory();
206 }
207}
Note: See TracBrowser for help on using the repository browser.