Advertisement
Guest User

android fileexplorer.java

a guest
Mar 27th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. public class FileDialog extends ListActivity {
  2.  
  3. private static final String ITEM_KEY = "key";
  4. private static final String ITEM_IMAGE = "image";
  5.  
  6. public static final String START_PATH = "START_PATH";
  7. public static final String RESULT_PATH = "RESULT_PATH";
  8.  
  9. private List<String> item = null;
  10. private List<String> path = null;
  11. private String root = "/";
  12. private TextView myPath;
  13. private EditText mFileName;
  14. private ArrayList<HashMap<String, Object>> mList;
  15.  
  16. private Button selectButton;
  17. private Button newButton;
  18. private Button cancelButton;
  19. private Button createButton;
  20.  
  21. private LinearLayout layoutSelect;
  22. private LinearLayout layoutCreate;
  23. private InputMethodManager inputManager;
  24. private String parentPath;
  25. private String currentPath = root;
  26.  
  27. private File selectedFile;
  28. private HashMap<String, Integer> lastPositions = new HashMap<String, Integer>();
  29.  
  30. /** Called when the activity is first created. */
  31. @Override
  32. public void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setResult(RESULT_CANCELED, getIntent());
  35.  
  36. setContentView(R.layout.file_dialog_main);
  37. myPath = (TextView) findViewById(R.id.path);
  38. mFileName = (EditText) findViewById(R.id.fdEditTextFile);
  39.  
  40. inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
  41.  
  42. selectButton = (Button) findViewById(R.id.fdButtonSelect);
  43. selectButton.setEnabled(false);
  44. selectButton.setOnClickListener(new OnClickListener() {
  45.  
  46. @Override
  47. public void onClick(View v) {
  48. if (selectedFile != null) {
  49. getIntent().putExtra(RESULT_PATH, selectedFile.getPath());
  50. setResult(RESULT_OK, getIntent());
  51. finish();
  52. }
  53. }
  54. });
  55.  
  56. newButton = (Button) findViewById(R.id.fdButtonNew);
  57. newButton.setOnClickListener(new OnClickListener() {
  58.  
  59. @Override
  60. public void onClick(View v) {
  61. layoutSelect.setVisibility(View.GONE);
  62. layoutCreate.setVisibility(View.VISIBLE);
  63.  
  64. mFileName.setText("");
  65. mFileName.requestFocus();
  66. }
  67. });
  68.  
  69. layoutSelect = (LinearLayout) findViewById(R.id.fdLinearLayoutSelect);
  70. layoutCreate = (LinearLayout) findViewById(R.id.fdLinearLayoutCreate);
  71. layoutCreate.setVisibility(View.GONE);
  72.  
  73. cancelButton = (Button) findViewById(R.id.fdButtonCancel);
  74. cancelButton.setOnClickListener(new OnClickListener() {
  75.  
  76. @Override
  77. public void onClick(View v) {
  78. layoutCreate.setVisibility(View.GONE);
  79. layoutSelect.setVisibility(View.VISIBLE);
  80.  
  81. inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
  82. unselect();
  83. }
  84.  
  85. });
  86. createButton = (Button) findViewById(R.id.fdButtonCreate);
  87. createButton.setOnClickListener(new OnClickListener() {
  88.  
  89. @Override
  90. public void onClick(View v) {
  91. if (mFileName.getText().length() > 0) {
  92. getIntent().putExtra(RESULT_PATH,
  93. currentPath + "/" + mFileName.getText());
  94. setResult(RESULT_OK, getIntent());
  95. finish();
  96. }
  97. }
  98. });
  99.  
  100. String startPath = getIntent().getStringExtra(START_PATH);
  101. if (startPath != null) {
  102. getDir(startPath);
  103. } else {
  104. getDir(root);
  105. }
  106. }
  107.  
  108. private void getDir(String dirPath) {
  109.  
  110. boolean useAutoSelection = dirPath.length() < currentPath.length();
  111.  
  112. Integer position = lastPositions.get(parentPath);
  113.  
  114. getDirImpl(dirPath);
  115.  
  116. if (position != null && useAutoSelection) {
  117. getListView().setSelection(position);
  118. }
  119.  
  120. }
  121.  
  122. private void getDirImpl(String dirPath) {
  123.  
  124. myPath.setText(getText(R.string.location) + ": " + dirPath);
  125. currentPath = dirPath;
  126.  
  127. item = new ArrayList<String>();
  128. path = new ArrayList<String>();
  129. mList = new ArrayList<HashMap<String, Object>>();
  130.  
  131. File f = new File(dirPath);
  132. File[] files = f.listFiles();
  133.  
  134. if (!dirPath.equals(root)) {
  135.  
  136. item.add(root);
  137. addItem(root, R.drawable.folder);
  138. path.add(root);
  139.  
  140. item.add("../");
  141. addItem("../", R.drawable.folder);
  142. path.add(f.getParent());
  143. parentPath = f.getParent();
  144.  
  145. }
  146.  
  147. TreeMap<String, String> dirsMap = new TreeMap<String, String>();
  148. TreeMap<String, String> dirsPathMap = new TreeMap<String, String>();
  149. TreeMap<String, String> filesMap = new TreeMap<String, String>();
  150. TreeMap<String, String> filesPathMap = new TreeMap<String, String>();
  151. for (File file : files) {
  152. if (file.isDirectory()) {
  153. String dirName = file.getName();
  154. dirsMap.put(dirName, dirName);
  155. dirsPathMap.put(dirName, file.getPath());
  156. } else {
  157. filesMap.put(file.getName(), file.getName());
  158. filesPathMap.put(file.getName(), file.getPath());
  159. }
  160. }
  161. item.addAll(dirsMap.tailMap("").values());
  162. item.addAll(filesMap.tailMap("").values());
  163. path.addAll(dirsPathMap.tailMap("").values());
  164. path.addAll(filesPathMap.tailMap("").values());
  165.  
  166. SimpleAdapter fileList = new SimpleAdapter(this, mList,
  167. R.layout.file_dialog_row,
  168. new String[] { ITEM_KEY, ITEM_IMAGE }, new int[] {
  169. R.id.fdrowtext, R.id.fdrowimage });
  170.  
  171. for (String dir : dirsMap.tailMap("").values()) {
  172. addItem(dir, R.drawable.folder);
  173. }
  174.  
  175. for (String file : filesMap.tailMap("").values()) {
  176. addItem(file, R.drawable.file);
  177. }
  178.  
  179. fileList.notifyDataSetChanged();
  180.  
  181. setListAdapter(fileList);
  182.  
  183. }
  184.  
  185. private void addItem(String fileName, int imageId) {
  186. HashMap<String, Object> item = new HashMap<String, Object>();
  187. item.put(ITEM_KEY, fileName);
  188. item.put(ITEM_IMAGE, imageId);
  189. mList.add(item);
  190. }
  191.  
  192. @Override
  193. protected void onListItemClick(ListView l, View v, int position, long id) {
  194.  
  195. File file = new File(path.get(position));
  196.  
  197. if (file.isDirectory()) {
  198. unselect();
  199. if (file.canRead()) {
  200. lastPositions.put(currentPath, position);
  201. getDir(path.get(position));
  202. } else {
  203. new AlertDialog.Builder(this).setIcon(R.drawable.icon)
  204. .setTitle(
  205. "[" + file.getName() + "] "
  206. + getText(R.string.cant_read_folder))
  207. .setPositiveButton("OK",
  208. new DialogInterface.OnClickListener() {
  209.  
  210. @Override
  211. public void onClick(DialogInterface dialog,
  212. int which) {
  213.  
  214. }
  215. }).show();
  216. }
  217. } else {
  218. selectedFile = file;
  219. v.setSelected(true);
  220. selectButton.setEnabled(true);
  221. }
  222. }
  223.  
  224. @Override
  225. public boolean onKeyDown(int keyCode, KeyEvent event) {
  226. if ((keyCode == KeyEvent.KEYCODE_BACK)) {
  227. unselect();
  228.  
  229. if (layoutCreate.getVisibility() == View.VISIBLE) {
  230. layoutCreate.setVisibility(View.GONE);
  231. layoutSelect.setVisibility(View.VISIBLE);
  232. } else {
  233. if (!currentPath.equals(root)) {
  234. getDir(parentPath);
  235. } else {
  236. return super.onKeyDown(keyCode, event);
  237. }
  238. }
  239.  
  240. return true;
  241. } else {
  242. return super.onKeyDown(keyCode, event);
  243. }
  244. }
  245.  
  246. private void unselect() {
  247. selectButton.setEnabled(false);
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement