FileList.java revision d24b8183b93e781080b2c16c487e60d51c12da31
1/*
2 * Copyright (C) 2007 The Android Open Source Project
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 com.android.dumprendertree;
18
19import java.util.ArrayList;
20import java.util.HashMap;
21import java.util.List;
22import java.util.Map;
23import java.io.File;
24
25import android.app.ListActivity;
26import android.view.KeyEvent;
27import android.view.View;
28import android.widget.ListView;
29import android.widget.SimpleAdapter;
30import android.os.Bundle;
31
32
33public abstract class FileList extends ListActivity
34{
35	public boolean onKeyDown(int keyCode, KeyEvent event) {
36		switch (keyCode)
37		{
38			case KeyEvent.KEYCODE_DPAD_LEFT:
39				if (mPath.length() > mBaseLength) {
40					File f = new File(mPath);
41					mFocusFile = f.getName();
42					mFocusIndex = 0;
43					f = f.getParentFile();
44					mPath = f.getPath();
45					updateList();
46					return true;
47				}
48				break;
49
50			case KeyEvent.KEYCODE_DPAD_RIGHT:
51				{
52					Map map = (Map) getListView().getItemAtPosition(getListView().getSelectedItemPosition());
53					String path = (String)map.get("path");
54					if ((new File(path)).isDirectory()) {
55						mPath = path;
56				        mFocusFile = null;
57						updateList();
58					} else {
59						processFile(path, false);
60					}
61                    return true;
62				}
63
64			default:
65				break;
66		}
67		return super.onKeyDown(keyCode, event);
68	}
69
70	public void onCreate(Bundle icicle)
71    {
72        super.onCreate(icicle);
73        setupPath();
74        updateList();
75    }
76
77    protected List getData()
78    {
79        List myData = new ArrayList<HashMap>();
80
81        File f = new File(mPath);
82        if (!f.exists()) {
83        	addItem(myData, "!LayoutTests path missing!", "");
84        	return myData;
85        }
86        String[] files = f.list();
87
88        for (int i = 0; i < files.length; i++) {
89        	StringBuilder sb = new StringBuilder(mPath);
90        	sb.append(File.separatorChar);
91        	sb.append(files[i]);
92        	String path = sb.toString();
93        	File c = new File(path);
94        	if (fileFilter(c)) {
95	        	if (c.isDirectory()) {
96	        		addItem(myData, "<"+files[i]+">", path);
97	        		if (mFocusFile != null && mFocusFile.equals(files[i]))
98	        			mFocusIndex = myData.size()-1;
99	        	}
100	        	else
101	        	    addItem(myData, files[i], path);
102        	}
103        }
104
105        return myData;
106    }
107
108    protected void addItem(List<Map> data, String name, String path)
109    {
110        HashMap temp = new HashMap();
111        temp.put("title", name);
112        temp.put("path", path);
113        data.add(temp);
114    }
115
116    protected void onListItemClick(ListView l, View v, int position, long id)
117    {
118    	Map map = (Map) l.getItemAtPosition(position);
119    	String path = (String)map.get("path");
120
121        if ((new File(path)).isDirectory()) {
122            mPath = path;
123            mFocusFile = null;
124            updateList();
125        } else {
126            processFile(path, false);
127        }
128    }
129
130    /*
131     * This function is called when the user has selected a file in the
132     * file list. The selected file could be a file or a directory.
133     * The flag indicates if this was from a selection or not.
134     */
135    abstract void processFile(String filename, boolean selection);
136
137    /*
138     * This function is called when the file list is being built. Return
139     * true if the file is to be added to the file list.
140     */
141    abstract boolean fileFilter(File f);
142
143    protected void updateList() {
144        setListAdapter(new SimpleAdapter(this,
145                getData(),
146                android.R.layout.simple_list_item_1,
147                new String[] {"title"},
148                new int[] {android.R.id.text1}));
149        String title = mPath; //.substring(mBaseLength-11); // show the word LayoutTests
150        setTitle(title);
151        getListView().setSelection(mFocusIndex);
152    }
153
154    protected void setupPath()
155    {
156    	mPath = "/sdcard/android/layout_tests";
157    	mBaseLength = mPath.length();
158    }
159
160    protected String mPath;
161    protected int mBaseLength;
162    protected String mFocusFile;
163    protected int mFocusIndex;
164
165}
166