Merge branch 'develop' into loggingtool
[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-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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 package com.owncloud.android.ui.fragment;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import com.owncloud.android.AccountUtils;
26 import com.owncloud.android.Log_OC;
27 import com.owncloud.android.R;
28 import com.owncloud.android.datamodel.DataStorageManager;
29 import com.owncloud.android.datamodel.OCFile;
30 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
31 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
32 import com.owncloud.android.network.OwnCloudClientUtils;
33 import com.owncloud.android.operations.OnRemoteOperationListener;
34 import com.owncloud.android.operations.RemoteOperation;
35 import com.owncloud.android.operations.RemoveFileOperation;
36 import com.owncloud.android.operations.RenameFileOperation;
37 import com.owncloud.android.operations.SynchronizeFileOperation;
38 import com.owncloud.android.ui.FragmentListView;
39 import com.owncloud.android.ui.activity.FileDisplayActivity;
40 import com.owncloud.android.ui.activity.TransferServiceGetter;
41 import com.owncloud.android.ui.adapter.FileListListAdapter;
42 import com.owncloud.android.ui.dialog.EditNameDialog;
43 import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
44 import com.owncloud.android.ui.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
45
46 import eu.alefzero.webdav.WebdavClient;
47 import eu.alefzero.webdav.WebdavUtils;
48
49 import android.accounts.Account;
50 import android.app.Activity;
51 import android.content.ActivityNotFoundException;
52 import android.content.Intent;
53 import android.net.Uri;
54 import android.os.Bundle;
55 import android.os.Handler;
56 import android.support.v4.app.DialogFragment;
57 import android.util.Log;
58 import android.view.ContextMenu;
59 import android.view.MenuInflater;
60 import android.view.MenuItem;
61 import android.view.View;
62 import android.webkit.MimeTypeMap;
63 import android.widget.AdapterView;
64 import android.widget.Toast;
65 import android.widget.AdapterView.AdapterContextMenuInfo;
66
67 /**
68 * A Fragment that lists all files and folders in a given path.
69 *
70 * @author Bartek Przybylski
71 *
72 */
73 public class OCFileListFragment extends FragmentListView implements EditNameDialogListener, ConfirmationDialogFragmentListener {
74 private static final String TAG = "FileListFragment";
75 private static final String SAVED_LIST_POSITION = "LIST_POSITION";
76
77 private OCFileListFragment.ContainerActivity mContainerActivity;
78
79 private OCFile mFile = null;
80 private FileListListAdapter mAdapter;
81
82 private Handler mHandler;
83 private OCFile mTargetFile;
84
85 /**
86 * {@inheritDoc}
87 */
88 @Override
89 public void onAttach(Activity activity) {
90 super.onAttach(activity);
91 try {
92 mContainerActivity = (ContainerActivity) activity;
93 } catch (ClassCastException e) {
94 throw new ClassCastException(activity.toString() + " must implement " + OCFileListFragment.ContainerActivity.class.getSimpleName());
95 }
96 }
97
98
99 /**
100 * {@inheritDoc}
101 */
102 @Override
103 public void onActivityCreated(Bundle savedInstanceState) {
104 Log_OC.i(TAG, "onActivityCreated() start");
105
106 super.onActivityCreated(savedInstanceState);
107 mAdapter = new FileListListAdapter(mContainerActivity.getInitialDirectory(), mContainerActivity.getStorageManager(), getActivity(), mContainerActivity);
108 setListAdapter(mAdapter);
109
110 if (savedInstanceState != null) {
111 Log_OC.i(TAG, "savedInstanceState is not null");
112 int position = savedInstanceState.getInt(SAVED_LIST_POSITION);
113 setReferencePosition(position);
114 }
115
116 registerForContextMenu(getListView());
117 getListView().setOnCreateContextMenuListener(this);
118
119 mHandler = new Handler();
120
121 Log_OC.i(TAG, "onActivityCreated() stop");
122 }
123
124
125
126 @Override
127 public void onSaveInstanceState(Bundle savedInstanceState) {
128 Log_OC.i(TAG, "onSaveInstanceState() start");
129
130 savedInstanceState.putInt(SAVED_LIST_POSITION, getReferencePosition());
131
132 Log_OC.i(TAG, "onSaveInstanceState() stop");
133 }
134
135
136 @Override
137 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
138 OCFile file = (OCFile) mAdapter.getItem(position);
139 if (file != null) {
140 /// Click on a directory
141 if (file.getMimetype().equals("DIR")) {
142 // just local updates
143 mFile = file;
144 listDirectory(file);
145 // any other updates are let to the container Activity
146 mContainerActivity.onDirectoryClick(file);
147
148 } else { /// Click on a file
149 mContainerActivity.onFileClick(file);
150 }
151
152 } else {
153 Log_OC.d(TAG, "Null object in ListAdapter!!");
154 }
155
156 }
157
158 /**
159 * {@inheritDoc}
160 */
161 @Override
162 public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
163 super.onCreateContextMenu(menu, v, menuInfo);
164 MenuInflater inflater = getActivity().getMenuInflater();
165 inflater.inflate(R.menu.file_actions_menu, menu);
166 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
167 OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
168 List<Integer> toHide = new ArrayList<Integer>();
169 List<Integer> toDisable = new ArrayList<Integer>();
170
171 MenuItem item = null;
172 if (targetFile.isDirectory()) {
173 // contextual menu for folders
174 toHide.add(R.id.action_open_file_with);
175 toHide.add(R.id.action_download_file);
176 toHide.add(R.id.action_cancel_download);
177 toHide.add(R.id.action_cancel_upload);
178 toHide.add(R.id.action_see_details);
179 if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile) ||
180 mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile) ) {
181 toDisable.add(R.id.action_rename_file);
182 toDisable.add(R.id.action_remove_file);
183
184 }
185
186 } else {
187 // contextual menu for regular files
188 if (targetFile.isDown()) {
189 toHide.add(R.id.action_cancel_download);
190 toHide.add(R.id.action_cancel_upload);
191 item = menu.findItem(R.id.action_download_file);
192 if (item != null) {
193 item.setTitle(R.string.filedetails_sync_file);
194 }
195 } else {
196 toHide.add(R.id.action_open_file_with);
197 }
198 if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
199 toHide.add(R.id.action_download_file);
200 toHide.add(R.id.action_cancel_upload);
201 toDisable.add(R.id.action_open_file_with);
202 toDisable.add(R.id.action_rename_file);
203 toDisable.add(R.id.action_remove_file);
204
205 } else if ( mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
206 toHide.add(R.id.action_download_file);
207 toHide.add(R.id.action_cancel_download);
208 toDisable.add(R.id.action_open_file_with);
209 toDisable.add(R.id.action_rename_file);
210 toDisable.add(R.id.action_remove_file);
211
212 } else {
213 toHide.add(R.id.action_cancel_download);
214 toHide.add(R.id.action_cancel_upload);
215 }
216 }
217
218 for (int i : toHide) {
219 item = menu.findItem(i);
220 if (item != null) {
221 item.setVisible(false);
222 item.setEnabled(false);
223 }
224 }
225
226 for (int i : toDisable) {
227 item = menu.findItem(i);
228 if (item != null) {
229 item.setEnabled(false);
230 }
231 }
232 }
233
234
235 /**
236 * {@inhericDoc}
237 */
238 @Override
239 public boolean onContextItemSelected (MenuItem item) {
240 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
241 mTargetFile = (OCFile) mAdapter.getItem(info.position);
242 switch (item.getItemId()) {
243 case R.id.action_rename_file: {
244 EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), mTargetFile.getFileName(), this);
245 dialog.show(getFragmentManager(), EditNameDialog.TAG);
246 return true;
247 }
248 case R.id.action_remove_file: {
249 int messageStringId = R.string.confirmation_remove_alert;
250 int posBtnStringId = R.string.confirmation_remove_remote;
251 int neuBtnStringId = -1;
252 if (mTargetFile.isDirectory()) {
253 messageStringId = R.string.confirmation_remove_folder_alert;
254 posBtnStringId = R.string.confirmation_remove_remote_and_local;
255 neuBtnStringId = R.string.confirmation_remove_folder_local;
256 } else if (mTargetFile.isDown()) {
257 posBtnStringId = R.string.confirmation_remove_remote_and_local;
258 neuBtnStringId = R.string.confirmation_remove_local;
259 }
260 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
261 messageStringId,
262 new String[]{mTargetFile.getFileName()},
263 posBtnStringId,
264 neuBtnStringId,
265 R.string.common_cancel);
266 confDialog.setOnConfirmationListener(this);
267 confDialog.show(getFragmentManager(), FileDetailFragment.FTAG_CONFIRMATION);
268 return true;
269 }
270 case R.id.action_open_file_with: {
271 String storagePath = mTargetFile.getStoragePath();
272 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
273 try {
274 Intent i = new Intent(Intent.ACTION_VIEW);
275 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mTargetFile.getMimetype());
276 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
277 startActivity(i);
278
279 } catch (Throwable t) {
280 Log_OC.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mTargetFile.getMimetype());
281 boolean toastIt = true;
282 String mimeType = "";
283 try {
284 Intent i = new Intent(Intent.ACTION_VIEW);
285 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
286 if (mimeType == null || !mimeType.equals(mTargetFile.getMimetype())) {
287 if (mimeType != null) {
288 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);
289 } else {
290 // desperate try
291 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*/*");
292 }
293 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
294 startActivity(i);
295 toastIt = false;
296 }
297
298 } catch (IndexOutOfBoundsException e) {
299 Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);
300
301 } catch (ActivityNotFoundException e) {
302 Log_OC.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");
303
304 } catch (Throwable th) {
305 Log_OC.e(TAG, "Unexpected problem when opening: " + storagePath, th);
306
307 } finally {
308 if (toastIt) {
309 Toast.makeText(getActivity(), "There is no application to handle file " + mTargetFile.getFileName(), Toast.LENGTH_SHORT).show();
310 }
311 }
312
313 }
314 return true;
315 }
316 case R.id.action_download_file: {
317 Account account = AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity());
318 RemoteOperation operation = new SynchronizeFileOperation(mTargetFile, null, mContainerActivity.getStorageManager(), account, true, false, getSherlockActivity());
319 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(account, getSherlockActivity().getApplicationContext());
320 operation.execute(wc, mContainerActivity, mHandler);
321 getSherlockActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
322 return true;
323 }
324 case R.id.action_cancel_download: {
325 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
326 Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity());
327 if (downloaderBinder != null && downloaderBinder.isDownloading(account, mTargetFile)) {
328 downloaderBinder.cancel(account, mTargetFile);
329 listDirectory();
330 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
331 }
332 return true;
333 }
334 case R.id.action_cancel_upload: {
335 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
336 Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity());
337 if (uploaderBinder != null && uploaderBinder.isUploading(account, mTargetFile)) {
338 uploaderBinder.cancel(account, mTargetFile);
339 listDirectory();
340 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
341 }
342 return true;
343 }
344 case R.id.action_see_details: {
345 ((FileFragment.ContainerActivity)getActivity()).showFragmentWithDetails(mTargetFile);
346 return true;
347 }
348 default:
349 return super.onContextItemSelected(item);
350 }
351 }
352
353
354 /**
355 * Call this, when the user presses the up button
356 */
357 public void onNavigateUp() {
358 OCFile parentDir = null;
359
360 if(mFile != null){
361 DataStorageManager storageManager = mContainerActivity.getStorageManager();
362 parentDir = storageManager.getFileById(mFile.getParentId());
363 mFile = parentDir;
364 }
365 listDirectory(parentDir);
366 }
367
368 /**
369 * Use this to query the {@link OCFile} that is currently
370 * being displayed by this fragment
371 * @return The currently viewed OCFile
372 */
373 public OCFile getCurrentFile(){
374 return mFile;
375 }
376
377 /**
378 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
379 */
380 public void listDirectory(){
381 listDirectory(null);
382 }
383
384 /**
385 * Lists the given directory on the view. When the input parameter is null,
386 * it will either refresh the last known directory, or list the root
387 * if there never was a directory.
388 *
389 * @param directory File to be listed
390 */
391 public void listDirectory(OCFile directory) {
392 DataStorageManager storageManager = mContainerActivity.getStorageManager();
393 if (storageManager != null) {
394
395 // Check input parameters for null
396 if(directory == null){
397 if(mFile != null){
398 directory = mFile;
399 } else {
400 directory = storageManager.getFileByPath("/");
401 if (directory == null) return; // no files, wait for sync
402 }
403 }
404
405
406 // If that's not a directory -> List its parent
407 if(!directory.isDirectory()){
408 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
409 directory = storageManager.getFileById(directory.getParentId());
410 }
411
412 mAdapter.swapDirectory(directory, storageManager);
413 if (mFile == null || !mFile.equals(directory)) {
414 mList.setSelectionFromTop(0, 0);
415 }
416 mFile = directory;
417 }
418 }
419
420
421
422 /**
423 * Interface to implement by any Activity that includes some instance of FileListFragment
424 *
425 * @author David A. Velasco
426 */
427 public interface ContainerActivity extends TransferServiceGetter, OnRemoteOperationListener {
428
429 /**
430 * Callback method invoked when a directory is clicked by the user on the files list
431 *
432 * @param file
433 */
434 public void onDirectoryClick(OCFile file);
435
436 /**
437 * Callback method invoked when a file (non directory) is clicked by the user on the files list
438 *
439 * @param file
440 */
441 public void onFileClick(OCFile file);
442
443 /**
444 * Getter for the current DataStorageManager in the container activity
445 */
446 public DataStorageManager getStorageManager();
447
448
449 /**
450 * Callback method invoked when the parent activity is fully created to get the directory to list firstly.
451 *
452 * @return Directory to list firstly. Can be NULL.
453 */
454 public OCFile getInitialDirectory();
455
456
457 /**
458 * Callback method invoked when a the 'transfer state' of a file changes.
459 *
460 * This happens when a download or upload is started or ended for a file.
461 *
462 * This method is necessary by now to update the user interface of the double-pane layout in tablets
463 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
464 * won't provide the needed response before the method where this is called finishes.
465 *
466 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
467 *
468 * @param file OCFile which state changed.
469 * @param downloading Flag signaling if the file is now downloading.
470 * @param uploading Flag signaling if the file is now uploading.
471 */
472 public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading);
473
474 }
475
476
477 @Override
478 public void onDismiss(EditNameDialog dialog) {
479 if (dialog.getResult()) {
480 String newFilename = dialog.getNewFilename();
481 Log_OC.d(TAG, "name edit dialog dismissed with new name " + newFilename);
482 RemoteOperation operation = new RenameFileOperation(mTargetFile,
483 AccountUtils.getCurrentOwnCloudAccount(getActivity()),
484 newFilename,
485 mContainerActivity.getStorageManager());
486 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
487 operation.execute(wc, mContainerActivity, mHandler);
488 getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
489 }
490 }
491
492
493 @Override
494 public void onConfirmation(String callerTag) {
495 if (callerTag.equals(FileDetailFragment.FTAG_CONFIRMATION)) {
496 if (mContainerActivity.getStorageManager().getFileById(mTargetFile.getFileId()) != null) {
497 RemoteOperation operation = new RemoveFileOperation( mTargetFile,
498 true,
499 mContainerActivity.getStorageManager());
500 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity().getApplicationContext());
501 operation.execute(wc, mContainerActivity, mHandler);
502
503 getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
504 }
505 }
506 }
507
508 @Override
509 public void onNeutral(String callerTag) {
510 File f = null;
511 if (mTargetFile.isDirectory()) {
512 // TODO run in a secondary thread?
513 mContainerActivity.getStorageManager().removeDirectory(mTargetFile, false, true);
514
515 } else if (mTargetFile.isDown() && (f = new File(mTargetFile.getStoragePath())).exists()) {
516 f.delete();
517 mTargetFile.setStoragePath(null);
518 mContainerActivity.getStorageManager().saveFile(mTargetFile);
519 }
520 listDirectory();
521 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
522 }
523
524 @Override
525 public void onCancel(String callerTag) {
526 Log_OC.d(TAG, "REMOVAL CANCELED");
527 }
528
529
530 }