LocalFileListFragment.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2011 Bartek Przybylski
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.ui.fragment;
  22. import java.io.File;
  23. import java.util.ArrayList;
  24. import android.app.Activity;
  25. import android.os.Bundle;
  26. import android.os.Environment;
  27. import android.util.SparseBooleanArray;
  28. import android.view.LayoutInflater;
  29. import android.view.View;
  30. import android.view.ViewGroup;
  31. import android.widget.AdapterView;
  32. import android.widget.ImageView;
  33. import android.widget.ListView;
  34. import com.owncloud.android.R;
  35. import com.owncloud.android.lib.common.utils.Log_OC;
  36. import com.owncloud.android.ui.adapter.LocalFileListAdapter;
  37. /**
  38. * A Fragment that lists all files and folders in a given LOCAL path.
  39. */
  40. public class LocalFileListFragment extends ExtendedListFragment {
  41. private static final String TAG = "LocalFileListFragment";
  42. /** Reference to the Activity which this fragment is attached to. For callbacks */
  43. private LocalFileListFragment.ContainerActivity mContainerActivity;
  44. /** Directory to show */
  45. private File mDirectory = null;
  46. /** Adapter to connect the data from the directory with the View object */
  47. private LocalFileListAdapter mAdapter = null;
  48. /**
  49. * {@inheritDoc}
  50. */
  51. @Override
  52. public void onAttach(Activity activity) {
  53. super.onAttach(activity);
  54. try {
  55. mContainerActivity = (ContainerActivity) activity;
  56. } catch (ClassCastException e) {
  57. throw new ClassCastException(activity.toString() + " must implement " +
  58. LocalFileListFragment.ContainerActivity.class.getSimpleName());
  59. }
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. @Override
  65. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  66. Log_OC.i(TAG, "onCreateView() start");
  67. View v = super.onCreateView(inflater, container, savedInstanceState);
  68. setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  69. setSwipeEnabled(false); // Disable pull-to-refresh
  70. setMessageForEmptyList(getString(R.string.local_file_list_empty));
  71. Log_OC.i(TAG, "onCreateView() end");
  72. return v;
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. @Override
  78. public void onActivityCreated(Bundle savedInstanceState) {
  79. Log_OC.i(TAG, "onActivityCreated() start");
  80. super.onActivityCreated(savedInstanceState);
  81. mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity());
  82. setListAdapter(mAdapter);
  83. Log_OC.i(TAG, "onActivityCreated() stop");
  84. }
  85. /**
  86. * Checks the file clicked over. Browses inside if it is a directory.
  87. * Notifies the container activity in any case.
  88. */
  89. @Override
  90. public void onItemClick(AdapterView<?> l, View v, int position, long id) {
  91. File file = (File) mAdapter.getItem(position);
  92. if (file != null) {
  93. /// Click on a directory
  94. if (file.isDirectory()) {
  95. // just local updates
  96. listDirectory(file);
  97. // notify the click to container Activity
  98. mContainerActivity.onDirectoryClick(file);
  99. // save index and top position
  100. saveIndexAndTopPosition(position);
  101. } else { /// Click on a file
  102. ImageView checkBoxV = (ImageView) v.findViewById(R.id.custom_checkbox);
  103. if (checkBoxV != null) {
  104. if (((ListView)getListView()).isItemChecked(position)) {
  105. checkBoxV.setImageResource(R.drawable.ic_checkbox_marked);
  106. } else {
  107. checkBoxV.setImageResource(R.drawable.ic_checkbox_blank_outline);
  108. }
  109. }
  110. // notify the change to the container Activity
  111. mContainerActivity.onFileClick(file);
  112. }
  113. } else {
  114. Log_OC.w(TAG, "Null object in ListAdapter!!");
  115. }
  116. }
  117. /**
  118. * Call this, when the user presses the up button
  119. */
  120. public void onNavigateUp() {
  121. File parentDir = null;
  122. if(mDirectory != null) {
  123. parentDir = mDirectory.getParentFile(); // can be null
  124. }
  125. listDirectory(parentDir);
  126. // restore index and top position
  127. restoreIndexAndTopPosition();
  128. }
  129. /**
  130. * Use this to query the {@link File} object for the directory
  131. * that is currently being displayed by this fragment
  132. *
  133. * @return File The currently displayed directory
  134. */
  135. public File getCurrentDirectory(){
  136. return mDirectory;
  137. }
  138. /**
  139. * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
  140. * to refresh the current directory.
  141. */
  142. public void listDirectory(){
  143. listDirectory(null);
  144. }
  145. /**
  146. * Lists the given directory on the view. When the input parameter is null,
  147. * it will either refresh the last known directory. list the root
  148. * if there never was a directory.
  149. *
  150. * @param directory Directory to be listed
  151. */
  152. public void listDirectory(File directory) {
  153. // Check input parameters for null
  154. if(directory == null) {
  155. if(mDirectory != null){
  156. directory = mDirectory;
  157. } else {
  158. directory = Environment.getExternalStorageDirectory();
  159. // TODO be careful with the state of the storage; could not be available
  160. if (directory == null) return; // no files to show
  161. }
  162. }
  163. // if that's not a directory -> List its parent
  164. if(!directory.isDirectory()){
  165. Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
  166. directory = directory.getParentFile();
  167. }
  168. // by now, only files in the same directory will be kept as selected
  169. ((ListView)mCurrentListView).clearChoices();
  170. mAdapter.swapDirectory(directory);
  171. if (mDirectory == null || !mDirectory.equals(directory)) {
  172. mCurrentListView.setSelection(0);
  173. }
  174. mDirectory = directory;
  175. }
  176. /**
  177. * Returns the fule paths to the files checked by the user
  178. *
  179. * @return File paths to the files checked by the user.
  180. */
  181. public String[] getCheckedFilePaths() {
  182. ArrayList<String> result = new ArrayList<String>();
  183. SparseBooleanArray positions = ((ListView)mCurrentListView).getCheckedItemPositions();
  184. if (positions.size() > 0) {
  185. for (int i = 0; i < positions.size(); i++) {
  186. if (positions.get(positions.keyAt(i)) == true) {
  187. result.add(((File) mCurrentListView.getItemAtPosition(
  188. positions.keyAt(i))).getAbsolutePath());
  189. }
  190. }
  191. Log_OC.d(TAG, "Returning " + result.size() + " selected files");
  192. }
  193. return result.toArray(new String[result.size()]);
  194. }
  195. /**
  196. * Interface to implement by any Activity that includes some instance of LocalFileListFragment
  197. */
  198. public interface ContainerActivity {
  199. /**
  200. * Callback method invoked when a directory is clicked by the user on the files list
  201. *
  202. * @param directory
  203. */
  204. public void onDirectoryClick(File directory);
  205. /**
  206. * Callback method invoked when a file (non directory)
  207. * is clicked by the user on the files list
  208. *
  209. * @param file
  210. */
  211. public void onFileClick(File file);
  212. /**
  213. * Callback method invoked when the parent activity
  214. * is fully created to get the directory to list firstly.
  215. *
  216. * @return Directory to list firstly. Can be NULL.
  217. */
  218. public File getInitialDirectory();
  219. }
  220. }