Mercurial > 510Connectbot
comparison app/src/main/java/com/lamerman/FileDialog.java @ 438:d29cce60f393
migrate from Eclipse to Android Studio
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 03 Dec 2015 11:23:55 -0800 |
parents | src/com/lamerman/FileDialog.java@0ce5cc452d02 |
children |
comparison
equal
deleted
inserted
replaced
437:208b31032318 | 438:d29cce60f393 |
---|---|
1 package com.lamerman; | |
2 | |
3 import java.io.File; | |
4 import java.util.ArrayList; | |
5 import java.util.HashMap; | |
6 import java.util.List; | |
7 import java.util.TreeMap; | |
8 | |
9 import com.five_ten_sg.connectbot.R; | |
10 import android.app.AlertDialog; | |
11 import android.app.ListActivity; | |
12 import android.content.DialogInterface; | |
13 import android.net.Uri; | |
14 import android.os.Bundle; | |
15 import android.view.KeyEvent; | |
16 import android.view.View; | |
17 import android.view.View.OnClickListener; | |
18 import android.view.inputmethod.InputMethodManager; | |
19 import android.widget.Button; | |
20 import android.widget.EditText; | |
21 import android.widget.LinearLayout; | |
22 import android.widget.ListView; | |
23 import android.widget.SimpleAdapter; | |
24 import android.widget.TextView; | |
25 | |
26 public class FileDialog extends ListActivity { | |
27 | |
28 private static final String ITEM_KEY = "key"; | |
29 private static final String ITEM_IMAGE = "image"; | |
30 private static final String ROOT = "/"; | |
31 | |
32 public static final String START_PATH = "START_PATH"; | |
33 public static final String RESULT_PATH = "RESULT_PATH"; | |
34 public static final String SELECTION_MODE = "SELECTION_MODE"; | |
35 public static final String TITLE = "TITLE"; | |
36 | |
37 private List<String> path = null; | |
38 private TextView myPath; | |
39 private EditText mFileName; | |
40 private ArrayList<HashMap<String, Object>> mList; | |
41 | |
42 private Button selectButton; | |
43 | |
44 private LinearLayout layoutSelect; | |
45 private LinearLayout layoutCreate; | |
46 private InputMethodManager inputManager; | |
47 private String parentPath; | |
48 private String currentPath = ROOT; | |
49 | |
50 private int selectionMode = SelectionMode.MODE_CREATE; | |
51 | |
52 private File selectedFile; | |
53 private HashMap<String, Integer> lastPositions = new HashMap<String, Integer>(); | |
54 | |
55 /** Called when the activity is first created. */ | |
56 @Override | |
57 public void onCreate(Bundle savedInstanceState) { | |
58 super.onCreate(savedInstanceState); | |
59 setResult(RESULT_CANCELED, getIntent()); | |
60 setContentView(R.layout.file_dialog_main); | |
61 this.setTitle(String.format("%s: %s", | |
62 getResources().getText(R.string.app_name), | |
63 getIntent().getStringExtra(TITLE))); | |
64 myPath = (TextView) findViewById(R.id.path); | |
65 mFileName = (EditText) findViewById(R.id.fdEditTextFile); | |
66 inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); | |
67 selectButton = (Button) findViewById(R.id.fdButtonSelect); | |
68 selectButton.setEnabled(false); | |
69 selectButton.setOnClickListener(new OnClickListener() { | |
70 public void onClick(View v) { | |
71 if (selectedFile != null) { | |
72 getIntent().setData(Uri.fromFile(selectedFile)); | |
73 setResult(RESULT_OK, getIntent()); | |
74 finish(); | |
75 } | |
76 } | |
77 }); | |
78 final Button newButton = (Button) findViewById(R.id.fdButtonNew); | |
79 newButton.setOnClickListener(new OnClickListener() { | |
80 public void onClick(View v) { | |
81 setCreateVisible(v); | |
82 mFileName.setText(""); | |
83 mFileName.requestFocus(); | |
84 } | |
85 }); | |
86 final Button cancelButton = (Button) findViewById(R.id.fdButtonCancel); | |
87 cancelButton.setOnClickListener(new OnClickListener() { | |
88 public void onClick(View v) { | |
89 setResult(RESULT_CANCELED, getIntent()); | |
90 finish(); | |
91 } | |
92 }); | |
93 selectionMode = getIntent().getIntExtra(SELECTION_MODE, | |
94 SelectionMode.MODE_CREATE); | |
95 | |
96 if (selectionMode == SelectionMode.MODE_OPEN) { | |
97 newButton.setEnabled(false); | |
98 } | |
99 | |
100 layoutSelect = (LinearLayout) findViewById(R.id.fdLinearLayoutSelect); | |
101 layoutCreate = (LinearLayout) findViewById(R.id.fdLinearLayoutCreate); | |
102 layoutCreate.setVisibility(View.GONE); | |
103 final Button cancelCreateButton = (Button) findViewById(R.id.fdButtonCancelCreate); | |
104 cancelCreateButton.setOnClickListener(new OnClickListener() { | |
105 public void onClick(View v) { | |
106 setSelectVisible(v); | |
107 } | |
108 }); | |
109 final Button createButton = (Button) findViewById(R.id.fdButtonCreate); | |
110 createButton.setOnClickListener(new OnClickListener() { | |
111 public void onClick(View v) { | |
112 if (mFileName.getText().length() > 0) { | |
113 getIntent().putExtra(RESULT_PATH, | |
114 currentPath + "/" + mFileName.getText()); | |
115 setResult(RESULT_OK, getIntent()); | |
116 finish(); | |
117 } | |
118 } | |
119 }); | |
120 String startPath = getIntent().getStringExtra(START_PATH); | |
121 | |
122 if (startPath != null) { | |
123 getDir(startPath); | |
124 } | |
125 else { | |
126 getDir(ROOT); | |
127 } | |
128 } | |
129 | |
130 @Override | |
131 protected void onPause() { | |
132 String myPathStr = myPath.getText().toString(); | |
133 int idx = myPathStr.lastIndexOf(':'); | |
134 String currPath = myPathStr.substring(idx + 2); | |
135 getIntent().putExtra(START_PATH, currPath); | |
136 super.onPause(); | |
137 } | |
138 | |
139 private void getDir(String dirPath) { | |
140 boolean useAutoSelection = dirPath.length() < currentPath.length(); | |
141 Integer position = lastPositions.get(parentPath); | |
142 getDirImpl(dirPath); | |
143 | |
144 if (position != null && useAutoSelection) { | |
145 getListView().setSelection(position); | |
146 } | |
147 } | |
148 | |
149 private void getDirImpl(final String dirPath) { | |
150 currentPath = dirPath; | |
151 final List<String> item = new ArrayList<String>(); | |
152 path = new ArrayList<String>(); | |
153 mList = new ArrayList<HashMap<String, Object>>(); | |
154 File f = new File(currentPath); | |
155 File[] files = f.listFiles(); | |
156 | |
157 if (files == null) { | |
158 currentPath = ROOT; | |
159 f = new File(currentPath); | |
160 files = f.listFiles(); | |
161 } | |
162 | |
163 myPath.setText(getText(R.string.location) + ": " + currentPath); | |
164 | |
165 if (!currentPath.equals(ROOT)) { | |
166 item.add(ROOT); | |
167 addItem(ROOT, R.drawable.folder); | |
168 path.add(ROOT); | |
169 item.add("../"); | |
170 addItem("../", R.drawable.folder); | |
171 path.add(f.getParent()); | |
172 parentPath = f.getParent(); | |
173 } | |
174 | |
175 TreeMap<String, String> dirsMap = new TreeMap<String, String>(); | |
176 TreeMap<String, String> dirsPathMap = new TreeMap<String, String>(); | |
177 TreeMap<String, String> filesMap = new TreeMap<String, String>(); | |
178 TreeMap<String, String> filesPathMap = new TreeMap<String, String>(); | |
179 | |
180 for (File file : files) { | |
181 if (file.isDirectory()) { | |
182 String dirName = file.getName(); | |
183 dirsMap.put(dirName, dirName); | |
184 dirsPathMap.put(dirName, file.getPath()); | |
185 } | |
186 else { | |
187 filesMap.put(file.getName(), file.getName()); | |
188 filesPathMap.put(file.getName(), file.getPath()); | |
189 } | |
190 } | |
191 | |
192 item.addAll(dirsMap.tailMap("").values()); | |
193 item.addAll(filesMap.tailMap("").values()); | |
194 path.addAll(dirsPathMap.tailMap("").values()); | |
195 path.addAll(filesPathMap.tailMap("").values()); | |
196 SimpleAdapter fileList = new SimpleAdapter(this, mList, | |
197 R.layout.file_dialog_row, | |
198 new String[] { ITEM_KEY, ITEM_IMAGE }, new int[] { | |
199 R.id.fdrowtext, R.id.fdrowimage | |
200 }); | |
201 | |
202 for (String dir : dirsMap.tailMap("").values()) { | |
203 addItem(dir, R.drawable.folder); | |
204 } | |
205 | |
206 for (String file : filesMap.tailMap("").values()) { | |
207 addItem(file, R.drawable.file); | |
208 } | |
209 | |
210 fileList.notifyDataSetChanged(); | |
211 setListAdapter(fileList); | |
212 } | |
213 | |
214 private void addItem(String fileName, int imageId) { | |
215 HashMap<String, Object> item = new HashMap<String, Object>(); | |
216 item.put(ITEM_KEY, fileName); | |
217 item.put(ITEM_IMAGE, imageId); | |
218 mList.add(item); | |
219 } | |
220 | |
221 @Override | |
222 protected void onListItemClick(ListView l, View v, int position, long id) { | |
223 File file = new File(path.get(position)); | |
224 setSelectVisible(v); | |
225 | |
226 if (file.isDirectory()) { | |
227 selectButton.setEnabled(false); | |
228 | |
229 if (file.canRead()) { | |
230 lastPositions.put(currentPath, position); | |
231 getDir(path.get(position)); | |
232 } | |
233 else { | |
234 new AlertDialog.Builder(this) | |
235 .setIcon(R.drawable.icon) | |
236 .setTitle( | |
237 "[" + file.getName() + "] " | |
238 + getText(R.string.cant_read_folder)) | |
239 .setPositiveButton("OK", | |
240 new DialogInterface.OnClickListener() { | |
241 public void onClick(DialogInterface dialog, | |
242 int which) { | |
243 } | |
244 }).show(); | |
245 } | |
246 } | |
247 else { | |
248 selectedFile = file; | |
249 v.setSelected(true); | |
250 selectButton.setEnabled(true); | |
251 } | |
252 } | |
253 | |
254 @Override | |
255 public boolean onKeyDown(int keyCode, KeyEvent event) { | |
256 if ((keyCode == KeyEvent.KEYCODE_BACK)) { | |
257 selectButton.setEnabled(false); | |
258 | |
259 if (layoutCreate.getVisibility() == View.VISIBLE) { | |
260 layoutCreate.setVisibility(View.GONE); | |
261 layoutSelect.setVisibility(View.VISIBLE); | |
262 } | |
263 else { | |
264 if (!currentPath.equals(ROOT)) { | |
265 getDir(parentPath); | |
266 } | |
267 else { | |
268 return super.onKeyDown(keyCode, event); | |
269 } | |
270 } | |
271 | |
272 return true; | |
273 } | |
274 else { | |
275 return super.onKeyDown(keyCode, event); | |
276 } | |
277 } | |
278 | |
279 private void setCreateVisible(View v) { | |
280 layoutCreate.setVisibility(View.VISIBLE); | |
281 layoutSelect.setVisibility(View.GONE); | |
282 inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); | |
283 selectButton.setEnabled(false); | |
284 } | |
285 | |
286 private void setSelectVisible(View v) { | |
287 layoutCreate.setVisibility(View.GONE); | |
288 layoutSelect.setVisibility(View.VISIBLE); | |
289 inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); | |
290 selectButton.setEnabled(false); | |
291 } | |
292 } |