Merge remote-tracking branch 'upstream/develop' into us4_view_text_files
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / LocalFileListFragment.java
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
20 import java.io.File;
21 import java.util.ArrayList;
22
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 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
39 /**
40 * A Fragment that lists all files and folders in a given LOCAL path.
41 *
42 * @author David A. Velasco
43 *
44 */
45 public class LocalFileListFragment extends ExtendedListFragment {
46 private static final String TAG = "LocalFileListFragment";
47
48 /** Reference to the Activity which this fragment is attached to. For callbacks */
49 private LocalFileListFragment.ContainerActivity mContainerActivity;
50
51 /** Directory to show */
52 private File mDirectory = null;
53
54 /** Adapter to connect the data from the directory with the View object */
55 private LocalFileListAdapter mAdapter = null;
56
57
58 /**
59 * {@inheritDoc}
60 */
61 @Override
62 public void onAttach(Activity activity) {
63 super.onAttach(activity);
64 try {
65 mContainerActivity = (ContainerActivity) activity;
66 } catch (ClassCastException e) {
67 throw new ClassCastException(activity.toString() + " must implement " + LocalFileListFragment.ContainerActivity.class.getSimpleName());
68 }
69 }
70
71
72 /**
73 * {@inheritDoc}
74 */
75 @Override
76 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
77 Log_OC.i(TAG, "onCreateView() start");
78 View v = super.onCreateView(inflater, container, savedInstanceState);
79 getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
80 disableSwipe(); // Disable pull refresh
81 setMessageForEmptyList(getString(R.string.local_file_list_empty));
82 Log_OC.i(TAG, "onCreateView() end");
83 return v;
84 }
85
86
87 /**
88 * {@inheritDoc}
89 */
90 @Override
91 public void onActivityCreated(Bundle savedInstanceState) {
92 Log_OC.i(TAG, "onActivityCreated() start");
93
94 super.onActivityCreated(savedInstanceState);
95 mAdapter = new LocalFileListAdapter(mContainerActivity.getInitialDirectory(), getActivity());
96 setListAdapter(mAdapter);
97
98 Log_OC.i(TAG, "onActivityCreated() stop");
99 }
100
101
102 /**
103 * Checks the file clicked over. Browses inside if it is a directory. Notifies the container activity in any case.
104 */
105 @Override
106 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
107 File file = (File) mAdapter.getItem(position);
108 if (file != null) {
109 /// Click on a directory
110 if (file.isDirectory()) {
111 // just local updates
112 listDirectory(file);
113 // notify the click to container Activity
114 mContainerActivity.onDirectoryClick(file);
115 // save index and top position
116 saveIndexAndTopPosition(position);
117
118 } else { /// Click on a file
119 ImageView checkBoxV = (ImageView) v.findViewById(R.id.custom_checkbox);
120 if (checkBoxV != null) {
121 if (getListView().isItemChecked(position)) {
122 checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
123 } else {
124 checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
125 }
126 }
127 // notify the change to the container Activity
128 mContainerActivity.onFileClick(file);
129 }
130
131 } else {
132 Log_OC.w(TAG, "Null object in ListAdapter!!");
133 }
134 }
135
136
137 /**
138 * Call this, when the user presses the up button
139 */
140 public void onNavigateUp() {
141 File parentDir = null;
142 if(mDirectory != null) {
143 parentDir = mDirectory.getParentFile(); // can be null
144 }
145 listDirectory(parentDir);
146
147 // restore index and top position
148 restoreIndexAndTopPosition();
149 }
150
151
152 /**
153 * Use this to query the {@link File} object for the directory
154 * that is currently being displayed by this fragment
155 *
156 * @return File The currently displayed directory
157 */
158 public File getCurrentDirectory(){
159 return mDirectory;
160 }
161
162
163 /**
164 * Calls {@link LocalFileListFragment#listDirectory(File)} with a null parameter
165 * to refresh the current directory.
166 */
167 public void listDirectory(){
168 listDirectory(null);
169 }
170
171
172 /**
173 * Lists the given directory on the view. When the input parameter is null,
174 * it will either refresh the last known directory. list the root
175 * if there never was a directory.
176 *
177 * @param directory Directory to be listed
178 */
179 public void listDirectory(File directory) {
180
181 // Check input parameters for null
182 if(directory == null) {
183 if(mDirectory != null){
184 directory = mDirectory;
185 } else {
186 directory = Environment.getExternalStorageDirectory(); // TODO be careful with the state of the storage; could not be available
187 if (directory == null) return; // no files to show
188 }
189 }
190
191
192 // if that's not a directory -> List its parent
193 if(!directory.isDirectory()){
194 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
195 directory = directory.getParentFile();
196 }
197
198 mList.clearChoices(); // by now, only files in the same directory will be kept as selected
199 mAdapter.swapDirectory(directory);
200 if (mDirectory == null || !mDirectory.equals(directory)) {
201 mList.setSelectionFromTop(0, 0);
202 }
203 mDirectory = directory;
204 }
205
206
207 /**
208 * Returns the fule paths to the files checked by the user
209 *
210 * @return File paths to the files checked by the user.
211 */
212 public String[] getCheckedFilePaths() {
213 ArrayList<String> result = new ArrayList<String>();
214 SparseBooleanArray positions = mList.getCheckedItemPositions();
215 if (positions.size() > 0) {
216 for (int i = 0; i < positions.size(); i++) {
217 if (positions.get(positions.keyAt(i)) == true) {
218 result.add(((File) mList.getItemAtPosition(positions.keyAt(i))).getAbsolutePath());
219 }
220 }
221
222 Log_OC.d(TAG, "Returning " + result.size() + " selected files");
223 }
224 return result.toArray(new String[result.size()]);
225 }
226
227
228 /**
229 * Interface to implement by any Activity that includes some instance of LocalFileListFragment
230 *
231 * @author David A. Velasco
232 */
233 public interface ContainerActivity {
234
235 /**
236 * Callback method invoked when a directory is clicked by the user on the files list
237 *
238 * @param file
239 */
240 public void onDirectoryClick(File directory);
241
242 /**
243 * Callback method invoked when a file (non directory) is clicked by the user on the files list
244 *
245 * @param file
246 */
247 public void onFileClick(File file);
248
249
250 /**
251 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
252 *
253 * @return Directory to list firstly. Can be NULL.
254 */
255 public File getInitialDirectory();
256
257 }
258
259
260 }