LocalFileListFragment.java 8.0 KB

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