Add simple android client.
[odfkit:webodf.git] / android / src / org / webodf / WebODFSelector.java
1 package org.webodf;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FilenameFilter;
7 import java.io.IOException;
8
9 import android.app.ListActivity;
10 import android.content.Intent;
11 import android.net.Uri;
12 import android.os.Bundle;
13 import android.os.Environment;
14 import android.view.View;
15 import android.widget.AdapterView;
16 import android.widget.AdapterView.OnItemClickListener;
17 import android.widget.ArrayAdapter;
18 import android.widget.ListView;
19 import android.widget.TextView;
20
21 public class WebODFSelector extends ListActivity {
22
23         public void onCreate(Bundle savedInstanceState) {
24                 super.onCreate(savedInstanceState);
25                 updateList();
26                 ListView lv = getListView();
27                 lv.setTextFilterEnabled(true);
28
29                 lv.setOnItemClickListener(new OnItemClickListener() {
30                         public void onItemClick(AdapterView<?> parent, View view,
31                                         int position, long id) {
32                                 String filename = ((TextView) view).getText().toString();
33                                 File f = new File(mPath, filename);
34                                 if (f.isDirectory()) {
35                                         mPath = f;
36                                         updateList();
37                                         return;
38                                 }
39                                 Intent intent = new Intent(Intent.ACTION_VIEW);
40                                 intent.setDataAndType(Uri.fromFile(f),
41                                                 "application/vnd.oasis.opendocument.text");
42                                 startActivity(intent);
43                         }
44                 });
45 /* code to immediatly open document which is handy when debugging
46                 Intent intent = new Intent(Intent.ACTION_VIEW);
47                 intent.setDataAndType(Uri.fromFile(new File(new File(mPath, "download"), "DanskTest08.odt")),
48                                 "application/vnd.oasis.opendocument.text");
49                 startActivity(intent);
50 */
51         }
52
53         private void updateList() {
54                 setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem,
55                                 loadFileList()));
56         }
57
58         final private File mRoot = Environment.getExternalStorageDirectory();
59         private File mPath = mRoot;
60
61         final private String odfmime = "mimetypeapplication/vnd.oasis.opendocument.";
62
63         private boolean seemsODFFile(File file) {
64                 if (!file.canRead() || file.length() < 100) {
65                         return false;
66                 }
67                 boolean valid = false;
68                 try {
69                         FileInputStream input = new FileInputStream(file);
70                         byte[] buffer = new byte[96];
71                         if (input.read(buffer) == buffer.length) {
72                                 String mime = new String(buffer, 30, odfmime.length(),
73                                                 "US-ASCII");
74                                 valid = mime.equals(odfmime);
75                         }
76                         input.close();
77                 } catch (FileNotFoundException e) {
78                 } catch (IOException e) {
79                 }
80                 return valid;
81         }
82
83         private String[] loadFileList() {
84                 if (mPath.exists()) {
85                         FilenameFilter filter = new FilenameFilter() {
86                                 public boolean accept(File dir, String filename) {
87                                         File sel = new File(dir, filename);
88                                         return sel.isDirectory() || seemsODFFile(sel);
89                                 }
90                         };
91                         String[] list = mPath.list(filter);
92                         if (mPath.equals(mRoot)) {
93                                 return list;
94                         }
95                         String[] newlist = new String[list.length + 1];
96                         newlist[0] = "..";
97                         for (int i = 0; i < list.length; ++i) {
98                                 newlist[i + 1] = list[i];
99                         }
100                         return newlist;
101                 } else {
102                         return new String[0];
103                 }
104         }
105 }