source: other-projects/tipple-android/trunk-restructured/workspace/tipple-lib/src/org/greenstone/android/tipple/base/TippleLogFileBrowser.java@ 24807

Last change on this file since 24807 was 24807, checked in by davidb, 12 years ago

Reloating base files into 'base' package

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