021d583810eab0d92fbe78b91326945f7d792de2
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / OCFileListFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 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
22 import android.accounts.Account;
23 import android.app.Activity;
24 import android.content.Intent;
25 import android.media.MediaScannerConnection;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.support.v4.widget.SwipeRefreshLayout;
29 import android.view.ContextMenu;
30 import android.view.MenuInflater;
31 import android.view.MenuItem;
32 import android.view.View;
33 import android.widget.AdapterView;
34 import android.widget.AdapterView.AdapterContextMenuInfo;
35
36 import com.owncloud.android.R;
37 import com.owncloud.android.authentication.AccountUtils;
38 import com.owncloud.android.datamodel.FileDataStorageManager;
39 import com.owncloud.android.datamodel.OCFile;
40 import com.owncloud.android.files.FileMenuFilter;
41 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
42 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
43 import com.owncloud.android.lib.common.utils.Log_OC;
44 import com.owncloud.android.operations.RemoveFileOperation;
45 import com.owncloud.android.operations.RenameFileOperation;
46 import com.owncloud.android.ui.activity.FileDisplayActivity;
47 import com.owncloud.android.ui.activity.MoveActivity;
48 import com.owncloud.android.ui.activity.OnEnforceableRefreshListener;
49 import com.owncloud.android.ui.adapter.FileListListAdapter;
50 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
51 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
52 import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
53 import com.owncloud.android.ui.preview.PreviewImageFragment;
54 import com.owncloud.android.ui.preview.PreviewMediaFragment;
55
56 /**
57 * A Fragment that lists all files and folders in a given path.
58 *
59 * TODO refactorize to get rid of direct dependency on FileDisplayActivity
60 *
61 * @author Bartek Przybylski
62 * @author masensio
63 * @author David A. Velasco
64 */
65 public class OCFileListFragment extends ExtendedListFragment {
66
67 private static final String TAG = OCFileListFragment.class.getSimpleName();
68
69 private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ?
70 OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
71
72 public final static String ARG_JUST_FOLDERS = MY_PACKAGE + ".JUST_FOLDERS";
73 public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS = MY_PACKAGE + ".ALLOW_CONTEXTUAL";
74
75 private static final String KEY_FILE = MY_PACKAGE + ".extra.FILE";
76
77 private FileFragment.ContainerActivity mContainerActivity;
78
79 private OCFile mFile = null;
80 private FileListListAdapter mAdapter;
81
82 private Handler mHandler;
83 private OCFile mTargetFile;
84
85
86 /**
87 * {@inheritDoc}
88 */
89 @Override
90 public void onAttach(Activity activity) {
91 super.onAttach(activity);
92 Log_OC.e(TAG, "onAttach");
93 try {
94 mContainerActivity = (FileFragment.ContainerActivity) activity;
95
96 } catch (ClassCastException e) {
97 throw new ClassCastException(activity.toString() + " must implement " +
98 FileFragment.ContainerActivity.class.getSimpleName());
99 }
100 try {
101 setOnRefreshListener((OnEnforceableRefreshListener) activity);
102
103 } catch (ClassCastException e) {
104 throw new ClassCastException(activity.toString() + " must implement " +
105 SwipeRefreshLayout.OnRefreshListener.class.getSimpleName());
106 }
107 }
108
109
110 @Override
111 public void onDetach() {
112 setOnRefreshListener(null);
113 mContainerActivity = null;
114 super.onDetach();
115 }
116
117 /**
118 * {@inheritDoc}
119 */
120 @Override
121 public void onActivityCreated(Bundle savedInstanceState) {
122 super.onActivityCreated(savedInstanceState);
123 Log_OC.e(TAG, "onActivityCreated() start");
124
125 if (savedInstanceState != null) {
126 mFile = savedInstanceState.getParcelable(KEY_FILE);
127 }
128
129 Bundle args = getArguments();
130 boolean justFolders = (args == null) ? false : args.getBoolean(ARG_JUST_FOLDERS, false);
131 mAdapter = new FileListListAdapter(
132 justFolders,
133 getSherlockActivity(),
134 mContainerActivity
135 );
136 setListAdapter(mAdapter);
137
138 registerForContextMenu(getListView());
139 getListView().setOnCreateContextMenuListener(this);
140 }
141
142 /**
143 * Saves the current listed folder.
144 */
145 @Override
146 public void onSaveInstanceState (Bundle outState) {
147 super.onSaveInstanceState(outState);
148 outState.putParcelable(KEY_FILE, mFile);
149 }
150
151 /**
152 * Call this, when the user presses the up button.
153 *
154 * Tries to move up the current folder one level. If the parent folder was removed from the
155 * database, it continues browsing up until finding an existing folders.
156 *
157 * return Count of folder levels browsed up.
158 */
159 public int onBrowseUp() {
160 OCFile parentDir = null;
161 int moveCount = 0;
162
163 if(mFile != null){
164 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
165
166 String parentPath = null;
167 if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
168 parentPath = new File(mFile.getRemotePath()).getParent();
169 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
170 parentPath + OCFile.PATH_SEPARATOR;
171 parentDir = storageManager.getFileByPath(parentPath);
172 moveCount++;
173 } else {
174 parentDir = storageManager.getFileByPath(OCFile.ROOT_PATH);
175 }
176 while (parentDir == null) {
177 parentPath = new File(parentPath).getParent();
178 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath :
179 parentPath + OCFile.PATH_SEPARATOR;
180 parentDir = storageManager.getFileByPath(parentPath);
181 moveCount++;
182 } // exit is granted because storageManager.getFileByPath("/") never returns null
183 mFile = parentDir;
184
185 listDirectory(mFile);
186
187 onRefresh(false);
188
189 // restore index and top position
190 restoreIndexAndTopPosition();
191
192 } // else - should never happen now
193
194 return moveCount;
195 }
196
197 @Override
198 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
199 OCFile file = (OCFile) mAdapter.getItem(position);
200 if (file != null) {
201 if (file.isFolder()) {
202 // update state and view of this fragment
203 listDirectory(file);
204 // then, notify parent activity to let it update its state and view
205 mContainerActivity.onBrowsedDownTo(file);
206 // save index and top position
207 saveIndexAndTopPosition(position);
208
209 } else { /// Click on a file
210 if (PreviewImageFragment.canBePreviewed(file)) {
211 // preview image - it handles the download, if needed
212 ((FileDisplayActivity)mContainerActivity).startImagePreview(file);
213
214 } else if (file.isDown()) {
215 if (PreviewMediaFragment.canBePreviewed(file)) {
216 // media preview
217 ((FileDisplayActivity)mContainerActivity).startMediaPreview(file, 0, true);
218 } else {
219 mContainerActivity.getFileOperationsHelper().openFile(file);
220 }
221
222 } else {
223 // automatic download, preview on finish
224 ((FileDisplayActivity)mContainerActivity).startDownloadForPreview(file);
225 }
226
227 }
228
229 } else {
230 Log_OC.d(TAG, "Null object in ListAdapter!!");
231 }
232
233 }
234
235 /**
236 * {@inheritDoc}
237 */
238 @Override
239 public void onCreateContextMenu (
240 ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
241 super.onCreateContextMenu(menu, v, menuInfo);
242 Bundle args = getArguments();
243 boolean allowContextualActions =
244 (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
245 if (allowContextualActions) {
246 MenuInflater inflater = getSherlockActivity().getMenuInflater();
247 inflater.inflate(R.menu.file_actions_menu, menu);
248 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
249 OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
250
251 if (mContainerActivity.getStorageManager() != null) {
252 FileMenuFilter mf = new FileMenuFilter(
253 targetFile,
254 mContainerActivity.getStorageManager().getAccount(),
255 mContainerActivity,
256 getSherlockActivity()
257 );
258 mf.filter(menu);
259 }
260
261 /// additional restrictions for this fragment
262 // TODO allow in the future 'open with' for previewable files
263 MenuItem item = menu.findItem(R.id.action_open_file_with);
264 if (item != null) {
265 item.setVisible(false);
266 item.setEnabled(false);
267 }
268 /// TODO break this direct dependency on FileDisplayActivity... if possible
269 FileFragment frag = ((FileDisplayActivity)getSherlockActivity()).getSecondFragment();
270 if (frag != null && frag instanceof FileDetailFragment &&
271 frag.getFile().getFileId() == targetFile.getFileId()) {
272 item = menu.findItem(R.id.action_see_details);
273 if (item != null) {
274 item.setVisible(false);
275 item.setEnabled(false);
276 }
277 }
278 }
279 }
280
281
282 /**
283 * {@inhericDoc}
284 */
285 @Override
286 public boolean onContextItemSelected (MenuItem item) {
287 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
288 mTargetFile = (OCFile) mAdapter.getItem(info.position);
289 switch (item.getItemId()) {
290 case R.id.action_share_file: {
291 mContainerActivity.getFileOperationsHelper().shareFileWithLink(mTargetFile);
292 return true;
293 }
294 case R.id.action_unshare_file: {
295 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(mTargetFile);
296 return true;
297 }
298 case R.id.action_rename_file: {
299 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(mTargetFile);
300 dialog.show(getFragmentManager(), FileDetailFragment.FTAG_RENAME_FILE);
301 return true;
302 }
303 case R.id.action_remove_file: {
304 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(mTargetFile);
305 dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
306 return true;
307 }
308 case R.id.action_download_file:
309 case R.id.action_sync_file: {
310 mContainerActivity.getFileOperationsHelper().syncFile(mTargetFile);
311
312 Log_OC.d("mediascan", "path: " + mTargetFile.getRemotePath());
313
314 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
315 Log_OC.d("mediaScan", "path: "+ storageManager.getFileByPath(mTargetFile.getRemotePath()).getStoragePath());
316 // TODO triggerMediaScan
317
318 return true;
319 }
320 case R.id.action_cancel_download:
321 case R.id.action_cancel_upload: {
322 ((FileDisplayActivity)mContainerActivity).cancelTransference(mTargetFile);
323 return true;
324 }
325 case R.id.action_see_details: {
326 mContainerActivity.showDetails(mTargetFile);
327 return true;
328 }
329 case R.id.action_send_file: {
330 // Obtain the file
331 if (!mTargetFile.isDown()) { // Download the file
332 Log_OC.d(TAG, mTargetFile.getRemotePath() + " : File must be downloaded");
333 ((FileDisplayActivity)mContainerActivity).startDownloadForSending(mTargetFile);
334
335 } else {
336 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(mTargetFile);
337 }
338 return true;
339 }
340 case R.id.action_move: {
341 Intent action = new Intent(getActivity(), MoveActivity.class);
342
343 // Pass mTargetFile that contains info of selected file/folder
344 action.putExtra(MoveActivity.EXTRA_TARGET_FILE, mTargetFile);
345 getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_MOVE_FILES);
346 return true;
347 }
348 default:
349 return super.onContextItemSelected(item);
350 }
351 }
352
353
354
355 /**
356 * Use this to query the {@link OCFile} that is currently
357 * being displayed by this fragment
358 * @return The currently viewed OCFile
359 */
360 public OCFile getCurrentFile(){
361 return mFile;
362 }
363
364 /**
365 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
366 */
367 public void listDirectory(){
368 listDirectory(null);
369 }
370
371 /**
372 * Lists the given directory on the view. When the input parameter is null,
373 * it will either refresh the last known directory. list the root
374 * if there never was a directory.
375 *
376 * @param directory File to be listed
377 */
378 public void listDirectory(OCFile directory) {
379 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
380 if (storageManager != null) {
381
382 // Check input parameters for null
383 if(directory == null){
384 if(mFile != null){
385 directory = mFile;
386 } else {
387 directory = storageManager.getFileByPath("/");
388 if (directory == null) return; // no files, wait for sync
389 }
390 }
391
392
393 // If that's not a directory -> List its parent
394 if(!directory.isFolder()){
395 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
396 directory = storageManager.getFileById(directory.getParentId());
397 }
398
399 mAdapter.swapDirectory(directory, storageManager);
400 if (mFile == null || !mFile.equals(directory)) {
401 mList.setSelectionFromTop(0, 0);
402 }
403 mFile = directory;
404 }
405 }
406
407 private void triggerMediaScan(String path){
408 MediaScannerConnection.scanFile(
409 getActivity().getApplicationContext(),
410 new String[]{path},
411 null,null);
412 }
413 }