Advertisement
Guest User

Untitled

a guest
May 28th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 KB | None | 0 0
  1. // DirectoryChooserDialog.java
  2.  
  3. package com.example.directorychooser;
  4.  
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Comparator;
  10. import java.util.List;
  11.  
  12. import android.app.AlertDialog;
  13. import android.content.Context;
  14. import android.content.DialogInterface;
  15. import android.content.DialogInterface.OnClickListener;
  16. import android.content.DialogInterface.OnKeyListener;
  17. import android.os.Environment;
  18. import android.text.Editable;
  19. import android.view.Gravity;
  20. import android.view.KeyEvent;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.view.ViewGroup.LayoutParams;
  24. import android.widget.ArrayAdapter;
  25. import android.widget.Button;
  26. import android.widget.EditText;
  27. import android.widget.LinearLayout;
  28. import android.widget.TextView;
  29. import android.widget.Toast;
  30.  
  31. public class DirectoryChooserDialog
  32. {
  33. private boolean m_isNewFolderEnabled = true;
  34. private String m_sdcardDirectory = "";
  35. private Context m_context;
  36. private TextView m_titleView;
  37.  
  38. private String m_dir = "";
  39. private List<String> m_subdirs = null;
  40. private ChosenDirectoryListener m_chosenDirectoryListener = null;
  41. private ArrayAdapter<String> m_listAdapter = null;
  42.  
  43. //////////////////////////////////////////////////////
  44. // Callback interface for selected directory
  45. //////////////////////////////////////////////////////
  46. public interface ChosenDirectoryListener
  47. {
  48. public void onChosenDir(String chosenDir);
  49. }
  50.  
  51. public DirectoryChooserDialog(Context context, ChosenDirectoryListener chosenDirectoryListener)
  52. {
  53. m_context = context;
  54. m_sdcardDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
  55. m_chosenDirectoryListener = chosenDirectoryListener;
  56.  
  57. try
  58. {
  59. m_sdcardDirectory = new File(m_sdcardDirectory).getCanonicalPath();
  60. }
  61. catch (IOException ioe)
  62. {
  63. }
  64. }
  65.  
  66. ///////////////////////////////////////////////////////////////////////
  67. // setNewFolderEnabled() - enable/disable new folder button
  68. ///////////////////////////////////////////////////////////////////////
  69.  
  70. public void setNewFolderEnabled(boolean isNewFolderEnabled)
  71. {
  72. m_isNewFolderEnabled = isNewFolderEnabled;
  73. }
  74.  
  75. public boolean getNewFolderEnabled()
  76. {
  77. return m_isNewFolderEnabled;
  78. }
  79.  
  80. ///////////////////////////////////////////////////////////////////////
  81. // chooseDirectory() - load directory chooser dialog for initial
  82. // default sdcard directory
  83. ///////////////////////////////////////////////////////////////////////
  84.  
  85. public void chooseDirectory()
  86. {
  87. // Initial directory is sdcard directory
  88. chooseDirectory(m_sdcardDirectory);
  89. }
  90.  
  91. ////////////////////////////////////////////////////////////////////////////////
  92. // chooseDirectory(String dir) - load directory chooser dialog for initial
  93. // input 'dir' directory
  94. ////////////////////////////////////////////////////////////////////////////////
  95.  
  96. public void chooseDirectory(String dir)
  97. {
  98. File dirFile = new File(dir);
  99. if (! dirFile.exists() || ! dirFile.isDirectory())
  100. {
  101. dir = m_sdcardDirectory;
  102. }
  103.  
  104. try
  105. {
  106. dir = new File(dir).getCanonicalPath();
  107. }
  108. catch (IOException ioe)
  109. {
  110. return;
  111. }
  112.  
  113. m_dir = dir;
  114. m_subdirs = getDirectories(dir);
  115.  
  116. class DirectoryOnClickListener implements DialogInterface.OnClickListener
  117. {
  118. public void onClick(DialogInterface dialog, int item)
  119. {
  120. // Navigate into the sub-directory
  121. m_dir += "/" + ((AlertDialog) dialog).getListView().getAdapter().getItem(item);
  122. updateDirectory();
  123. }
  124. }
  125.  
  126. AlertDialog.Builder dialogBuilder =
  127. createDirectoryChooserDialog(dir, m_subdirs, new DirectoryOnClickListener());
  128.  
  129. dialogBuilder.setPositiveButton("OK", new OnClickListener()
  130. {
  131. @Override
  132. public void onClick(DialogInterface dialog, int which)
  133. {
  134. // Current directory chosen
  135. if (m_chosenDirectoryListener != null)
  136. {
  137. // Call registered listener supplied with the chosen directory
  138. m_chosenDirectoryListener.onChosenDir(m_dir);
  139. }
  140. }
  141. }).setNegativeButton("Cancel", null);
  142.  
  143. final AlertDialog dirsDialog = dialogBuilder.create();
  144.  
  145. dirsDialog.setOnKeyListener(new OnKeyListener()
  146. {
  147. @Override
  148. public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
  149. {
  150. if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
  151. {
  152. // Back button pressed
  153. if ( m_dir.equals(m_sdcardDirectory) )
  154. {
  155. // The very top level directory, do nothing
  156. return false;
  157. }
  158. else
  159. {
  160. // Navigate back to an upper directory
  161. m_dir = new File(m_dir).getParent();
  162. updateDirectory();
  163. }
  164.  
  165. return true;
  166. }
  167. else
  168. {
  169. return false;
  170. }
  171. }
  172. });
  173.  
  174. // Show directory chooser dialog
  175. dirsDialog.show();
  176. }
  177.  
  178. private boolean createSubDir(String newDir)
  179. {
  180. File newDirFile = new File(newDir);
  181. if (! newDirFile.exists() )
  182. {
  183. return newDirFile.mkdir();
  184. }
  185.  
  186. return false;
  187. }
  188.  
  189. private List<String> getDirectories(String dir)
  190. {
  191. List<String> dirs = new ArrayList<String>();
  192.  
  193. try
  194. {
  195. File dirFile = new File(dir);
  196. if (! dirFile.exists() || ! dirFile.isDirectory())
  197. {
  198. return dirs;
  199. }
  200.  
  201. for (File file : dirFile.listFiles())
  202. {
  203. if ( file.isDirectory() )
  204. {
  205. dirs.add( file.getName() );
  206. }
  207. }
  208. }
  209. catch (Exception e)
  210. {
  211. }
  212.  
  213. Collections.sort(dirs, new Comparator<String>()
  214. {
  215. public int compare(String o1, String o2)
  216. {
  217. return o1.compareTo(o2);
  218. }
  219. });
  220.  
  221. return dirs;
  222. }
  223.  
  224. private AlertDialog.Builder createDirectoryChooserDialog(String title, List<String> listItems,
  225. DialogInterface.OnClickListener onClickListener)
  226. {
  227. AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(m_context);
  228.  
  229. // Create custom view for AlertDialog title containing
  230. // current directory TextView and possible 'New folder' button.
  231. // Current directory TextView allows long directory path to be wrapped to multiple lines.
  232. LinearLayout titleLayout = new LinearLayout(m_context);
  233. titleLayout.setOrientation(LinearLayout.VERTICAL);
  234.  
  235. m_titleView = new TextView(m_context);
  236. m_titleView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  237. m_titleView.setTextAppearance(m_context, android.R.style.TextAppearance_Large);
  238. m_titleView.setTextColor( m_context.getResources().getColor(android.R.color.white) );
  239. m_titleView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
  240. m_titleView.setText(title);
  241.  
  242. Button newDirButton = new Button(m_context);
  243. newDirButton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  244. newDirButton.setText("New folder");
  245. newDirButton.setOnClickListener(new View.OnClickListener()
  246. {
  247. @Override
  248. public void onClick(View v)
  249. {
  250. final EditText input = new EditText(m_context);
  251.  
  252. // Show new folder name input dialog
  253. new AlertDialog.Builder(m_context).
  254. setTitle("New folder name").
  255. setView(input).setPositiveButton("OK", new DialogInterface.OnClickListener()
  256. {
  257. public void onClick(DialogInterface dialog, int whichButton)
  258. {
  259. Editable newDir = input.getText();
  260. String newDirName = newDir.toString();
  261. // Create new directory
  262. if ( createSubDir(m_dir + "/" + newDirName) )
  263. {
  264. // Navigate into the new directory
  265. m_dir += "/" + newDirName;
  266. updateDirectory();
  267. }
  268. else
  269. {
  270. Toast.makeText(
  271. m_context, "Failed to create '" + newDirName +
  272. "' folder", Toast.LENGTH_SHORT).show();
  273. }
  274. }
  275. }).setNegativeButton("Cancel", null).show();
  276. }
  277. });
  278.  
  279. if (! m_isNewFolderEnabled)
  280. {
  281. newDirButton.setVisibility(View.GONE);
  282. }
  283.  
  284. titleLayout.addView(m_titleView);
  285. titleLayout.addView(newDirButton);
  286.  
  287. dialogBuilder.setCustomTitle(titleLayout);
  288.  
  289. m_listAdapter = createListAdapter(listItems);
  290.  
  291. dialogBuilder.setSingleChoiceItems(m_listAdapter, -1, onClickListener);
  292. dialogBuilder.setCancelable(false);
  293.  
  294. return dialogBuilder;
  295. }
  296.  
  297. private void updateDirectory()
  298. {
  299. m_subdirs.clear();
  300. m_subdirs.addAll( getDirectories(m_dir) );
  301. m_titleView.setText(m_dir);
  302.  
  303. m_listAdapter.notifyDataSetChanged();
  304. }
  305.  
  306. private ArrayAdapter<String> createListAdapter(List<String> items)
  307. {
  308. return new ArrayAdapter<String>(m_context,
  309. android.R.layout.select_dialog_item, android.R.id.text1, items)
  310. {
  311. @Override
  312. public View getView(int position, View convertView,
  313. ViewGroup parent)
  314. {
  315. View v = super.getView(position, convertView, parent);
  316.  
  317. if (v instanceof TextView)
  318. {
  319. // Enable list item (directory) text wrapping
  320. TextView tv = (TextView) v;
  321. tv.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
  322. tv.setEllipsize(null);
  323. }
  324. return v;
  325. }
  326. };
  327. }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement