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.Arrays;
21import java.util.HashMap;
22import java.util.List;
23import java.util.Map;
24import java.io.File;
25
26import android.app.AlertDialog;
27import android.app.ListActivity;
28import android.content.DialogInterface;
29import android.view.KeyEvent;
30import android.view.View;
31import android.widget.ListView;
32import android.widget.SimpleAdapter;
33import android.os.Bundle;
34import android.os.Environment;
35
36
37public abstract class FileList extends ListActivity
38{
39	public boolean onKeyDown(int keyCode, KeyEvent event) {
40		switch (keyCode)
41		{
42			case KeyEvent.KEYCODE_DPAD_LEFT:
43				if (mPath.length() > mBaseLength) {
44					File f = new File(mPath);
45					mFocusFile = f.getName();
46					mFocusIndex = 0;
47					f = f.getParentFile();
48					mPath = f.getPath();
49					updateList();
50					return true;
51				}
52				break;
53
54			case KeyEvent.KEYCODE_DPAD_RIGHT:
55				{
56					Map map = (Map) getListView().getItemAtPosition(getListView().getSelectedItemPosition());
57					String path = (String)map.get("path");
58					if ((new File(path)).isDirectory()) {
59						mPath = path;
60				        mFocusFile = null;
61						updateList();
62					} else {
63						processFile(path, false);
64					}
65                    return true;
66				}
67
68			default:
69				break;
70		}
71		return super.onKeyDown(keyCode, event);
72	}
73
74	public void onCreate(Bundle icicle)
75    {
76        super.onCreate(icicle);
77        setupPath();
78        updateList();
79    }
80
81    protected List getData()
82    {
83        List myData = new ArrayList<HashMap>();
84
85        File f = new File(mPath);
86        if (!f.exists()) {
87        	addItem(myData, "!LayoutTests path missing!", "");
88        	return myData;
89        }
90        String[] files = f.list();
91        Arrays.sort(files);
92
93        for (int i = 0; i < files.length; i++) {
94        	StringBuilder sb = new StringBuilder(mPath);
95        	sb.append(File.separatorChar);
96        	sb.append(files[i]);
97        	String path = sb.toString();
98        	File c = new File(path);
99        	if (fileFilter(c)) {
100	        	if (c.isDirectory()) {
101	        		addItem(myData, "<"+files[i]+">", path);
102	        		if (mFocusFile != null && mFocusFile.equals(files[i]))
103	        			mFocusIndex = myData.size()-1;
104	        	}
105	        	else
106	        	    addItem(myData, files[i], path);
107        	}
108        }
109
110        return myData;
111    }
112
113    protected void addItem(List<Map> data, String name, String path)
114    {
115        HashMap temp = new HashMap();
116        temp.put("title", name);
117        temp.put("path", path);
118        data.add(temp);
119    }
120
121    protected void onListItemClick(ListView l, View v, int position, long id)
122    {
123        Map map = (Map) l.getItemAtPosition(position);
124        final String path = (String)map.get("path");
125
126        if ((new File(path)).isDirectory()) {
127            final CharSequence[] items = {"Open", "Run"};
128            AlertDialog.Builder builder = new AlertDialog.Builder(this);
129            builder.setTitle("Select an Action");
130            builder.setSingleChoiceItems(items, -1,
131                    new DialogInterface.OnClickListener(){
132                public void onClick(DialogInterface dialog, int which) {
133                    switch (which) {
134                        case OPEN_DIRECTORY:
135                            dialog.dismiss();
136                            mPath = path;
137                            mFocusFile = null;
138                            updateList();
139                            break;
140                        case RUN_TESTS:
141                            dialog.dismiss();
142                            processDirectory(path, false);
143                            break;
144                    }
145                }
146            });
147            builder.create().show();
148        } else {
149            processFile(path, false);
150        }
151    }
152
153    /*
154     * This function is called when the user has selected a directory in the
155     * list and wants to perform an action on it instead of navigating into
156     * the directory.
157     */
158    abstract void processDirectory(String path, boolean selection);
159    /*
160     * This function is called when the user has selected a file in the
161     * file list. The selected file could be a file or a directory.
162     * The flag indicates if this was from a selection or not.
163     */
164    abstract void processFile(String filename, boolean selection);
165
166    /*
167     * This function is called when the file list is being built. Return
168     * true if the file is to be added to the file list.
169     */
170    abstract boolean fileFilter(File f);
171
172    protected void updateList() {
173        setListAdapter(new SimpleAdapter(this,
174                getData(),
175                android.R.layout.simple_list_item_1,
176                new String[] {"title"},
177                new int[] {android.R.id.text1}));
178        String title = mPath; //.substring(mBaseLength-11); // show the word LayoutTests
179        setTitle(title);
180        getListView().setSelection(mFocusIndex);
181    }
182
183    protected void setupPath() {
184        mPath = Environment.getExternalStorageDirectory() + "/webkit/layout_tests";
185        mBaseLength = mPath.length();
186    }
187
188    protected String mPath;
189    protected int mBaseLength;
190    protected String mFocusFile;
191    protected int mFocusIndex;
192
193    private final static int OPEN_DIRECTORY = 0;
194    private final static int RUN_TESTS = 1;
195
196}
197