FileList.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
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 if (path.length() > 0) 121 processFile(path, true); 122 123 } 124 125 /* 126 * This function is called when the user has selected a file in the 127 * file list. The selected file could be a file or a directory. 128 * The flag indicates if this was from a selection or not. 129 */ 130 abstract void processFile(String filename, boolean selection); 131 132 /* 133 * This function is called when the file list is being built. Return 134 * true if the file is to be added to the file list. 135 */ 136 abstract boolean fileFilter(File f); 137 138 protected void updateList() { 139 setListAdapter(new SimpleAdapter(this, 140 getData(), 141 android.R.layout.simple_list_item_1, 142 new String[] {"title"}, 143 new int[] {android.R.id.text1})); 144 String title = mPath; //.substring(mBaseLength-11); // show the word LayoutTests 145 setTitle(title); 146 getListView().setSelection(mFocusIndex); 147 } 148 149 protected void setupPath() 150 { 151 mPath = "/sdcard"; 152 mBaseLength = mPath.length(); 153 } 154 155 protected String mPath; 156 protected int mBaseLength; 157 protected String mFocusFile; 158 protected int mFocusIndex; 159 160} 161