58ffedb886c7174d0f84a92fa636061f5b35fc4e
[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 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 import java.util.List;
23
24 import com.owncloud.android.Log_OC;
25 import com.owncloud.android.R;
26 import com.owncloud.android.authentication.AccountUtils;
27 import com.owncloud.android.datamodel.DataStorageManager;
28 import com.owncloud.android.datamodel.OCFile;
29 import com.owncloud.android.files.FileHandler;
30 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
31 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
32 import com.owncloud.android.operations.OnRemoteOperationListener;
33 import com.owncloud.android.operations.RemoteOperation;
34 import com.owncloud.android.operations.RemoteOperationResult;
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.activity.FileDisplayActivity;
39 import com.owncloud.android.ui.activity.TransferServiceGetter;
40 import com.owncloud.android.ui.adapter.FileListListAdapter;
41 import com.owncloud.android.ui.dialog.EditNameDialog;
42 import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
43 import com.owncloud.android.ui.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
44 import com.owncloud.android.ui.preview.PreviewImageFragment;
45 import com.owncloud.android.ui.preview.PreviewMediaFragment;
46
47 import android.accounts.Account;
48 import android.app.Activity;
49 import android.os.Bundle;
50 import android.os.Handler;
51 import android.view.ContextMenu;
52 import android.view.MenuInflater;
53 import android.view.MenuItem;
54 import android.view.View;
55 import android.widget.AdapterView;
56 import android.widget.AdapterView.AdapterContextMenuInfo;
57
58 /**
59 * A Fragment that lists all files and folders in a given path.
60 *
61 * @author Bartek Przybylski
62 *
63 */
64 public class OCFileListFragment extends ExtendedListFragment implements EditNameDialogListener, ConfirmationDialogFragmentListener {
65
66 private static final String TAG = OCFileListFragment.class.getSimpleName();
67
68 private static final String MY_PACKAGE = OCFileListFragment.class.getPackage() != null ? OCFileListFragment.class.getPackage().getName() : "com.owncloud.android.ui.fragment";
69 private static final String EXTRA_FILE = MY_PACKAGE + ".extra.FILE";
70
71 private OCFileListFragment.ContainerActivity mContainerActivity;
72
73 private OCFile mFile = null;
74 private FileListListAdapter mAdapter;
75
76 private Handler mHandler;
77 private OCFile mTargetFile;
78
79 /**
80 * {@inheritDoc}
81 */
82 @Override
83 public void onAttach(Activity activity) {
84 super.onAttach(activity);
85 Log_OC.e(TAG, "onAttach");
86 try {
87 mContainerActivity = (ContainerActivity) activity;
88 } catch (ClassCastException e) {
89 throw new ClassCastException(activity.toString() + " must implement " + OCFileListFragment.ContainerActivity.class.getSimpleName());
90 }
91 }
92
93
94 /**
95 * {@inheritDoc}
96 */
97 @Override
98 public void onActivityCreated(Bundle savedInstanceState) {
99 super.onActivityCreated(savedInstanceState);
100 Log_OC.e(TAG, "onActivityCreated() start");
101 mAdapter = new FileListListAdapter(getActivity(), mContainerActivity);
102 if (savedInstanceState != null) {
103 mFile = savedInstanceState.getParcelable(EXTRA_FILE);
104 }
105 setListAdapter(mAdapter);
106
107 registerForContextMenu(getListView());
108 getListView().setOnCreateContextMenuListener(this);
109
110 mHandler = new Handler();
111
112 }
113
114 /**
115 * Saves the current listed folder.
116 */
117 @Override
118 public void onSaveInstanceState (Bundle outState) {
119 super.onSaveInstanceState(outState);
120 outState.putParcelable(EXTRA_FILE, mFile);
121 }
122
123
124 /**
125 * Call this, when the user presses the up button
126 */
127 public void onBrowseUp() {
128 OCFile parentDir = null;
129
130 if(mFile != null){
131 DataStorageManager storageManager = mContainerActivity.getStorageManager();
132 parentDir = storageManager.getFileById(mFile.getParentId());
133 mFile = parentDir;
134 }
135 listDirectory(parentDir);
136
137 mContainerActivity.syncFolderOperation(mFile.getRemotePath(), mFile.getParentId());
138
139 }
140
141 @Override
142 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
143 OCFile file = (OCFile) mAdapter.getItem(position);
144 if (file != null) {
145 if (file.isDirectory()) {
146 // update state and view of this fragment
147 listDirectory(file);
148 // then, notify parent activity to let it update its state and view, and other fragments
149 mContainerActivity.onBrowsedDownTo(file);
150
151 } else { /// Click on a file
152 if (PreviewImageFragment.canBePreviewed(file)) {
153 // preview image - it handles the download, if needed
154 mContainerActivity.startImagePreview(file);
155
156 } else if (file.isDown()) {
157 if (PreviewMediaFragment.canBePreviewed(file)) {
158 // media preview
159 mContainerActivity.startMediaPreview(file, 0, true);
160 } else {
161 // open with
162 mContainerActivity.openFile(file);
163 }
164
165 } else {
166 // automatic download, preview on finish
167 mContainerActivity.startDownloadForPreview(file);
168 }
169
170 }
171
172 } else {
173 Log_OC.d(TAG, "Null object in ListAdapter!!");
174 }
175
176 }
177
178 /**
179 * {@inheritDoc}
180 */
181 @Override
182 public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
183 super.onCreateContextMenu(menu, v, menuInfo);
184 MenuInflater inflater = getActivity().getMenuInflater();
185 inflater.inflate(R.menu.file_actions_menu, menu);
186 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
187 OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
188 List<Integer> toHide = new ArrayList<Integer>();
189 List<Integer> toDisable = new ArrayList<Integer>();
190
191 MenuItem item = null;
192 if (targetFile.isDirectory()) {
193 // contextual menu for folders
194 toHide.add(R.id.action_open_file_with);
195 toHide.add(R.id.action_download_file);
196 toHide.add(R.id.action_cancel_download);
197 toHide.add(R.id.action_cancel_upload);
198 toHide.add(R.id.action_sync_file);
199 toHide.add(R.id.action_see_details);
200 if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile) ||
201 mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile) ) {
202 toDisable.add(R.id.action_rename_file);
203 toDisable.add(R.id.action_remove_file);
204
205 }
206
207 } else {
208 // contextual menu for regular files
209
210 // new design: 'download' and 'open with' won't be available anymore in context menu
211 toHide.add(R.id.action_download_file);
212 toHide.add(R.id.action_open_file_with);
213
214 if (targetFile.isDown()) {
215 toHide.add(R.id.action_cancel_download);
216 toHide.add(R.id.action_cancel_upload);
217
218 } else {
219 toHide.add(R.id.action_sync_file);
220 }
221 if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
222 toHide.add(R.id.action_cancel_upload);
223 toDisable.add(R.id.action_rename_file);
224 toDisable.add(R.id.action_remove_file);
225
226 } else if ( mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
227 toHide.add(R.id.action_cancel_download);
228 toDisable.add(R.id.action_rename_file);
229 toDisable.add(R.id.action_remove_file);
230
231 } else {
232 toHide.add(R.id.action_cancel_download);
233 toHide.add(R.id.action_cancel_upload);
234 }
235 }
236
237 for (int i : toHide) {
238 item = menu.findItem(i);
239 if (item != null) {
240 item.setVisible(false);
241 item.setEnabled(false);
242 }
243 }
244
245 for (int i : toDisable) {
246 item = menu.findItem(i);
247 if (item != null) {
248 item.setEnabled(false);
249 }
250 }
251 }
252
253
254 /**
255 * {@inhericDoc}
256 */
257 @Override
258 public boolean onContextItemSelected (MenuItem item) {
259 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
260 mTargetFile = (OCFile) mAdapter.getItem(info.position);
261 switch (item.getItemId()) {
262 case R.id.action_rename_file: {
263 String fileName = mTargetFile.getFileName();
264 int extensionStart = mTargetFile.isDirectory() ? -1 : fileName.lastIndexOf(".");
265 int selectionEnd = (extensionStart >= 0) ? extensionStart : fileName.length();
266 EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this);
267 dialog.show(getFragmentManager(), EditNameDialog.TAG);
268 return true;
269 }
270 case R.id.action_remove_file: {
271 int messageStringId = R.string.confirmation_remove_alert;
272 int posBtnStringId = R.string.confirmation_remove_remote;
273 int neuBtnStringId = -1;
274 if (mTargetFile.isDirectory()) {
275 messageStringId = R.string.confirmation_remove_folder_alert;
276 posBtnStringId = R.string.confirmation_remove_remote_and_local;
277 neuBtnStringId = R.string.confirmation_remove_folder_local;
278 } else if (mTargetFile.isDown()) {
279 posBtnStringId = R.string.confirmation_remove_remote_and_local;
280 neuBtnStringId = R.string.confirmation_remove_local;
281 }
282 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
283 messageStringId,
284 new String[]{mTargetFile.getFileName()},
285 posBtnStringId,
286 neuBtnStringId,
287 R.string.common_cancel);
288 confDialog.setOnConfirmationListener(this);
289 confDialog.show(getFragmentManager(), FileDetailFragment.FTAG_CONFIRMATION);
290 return true;
291 }
292 case R.id.action_sync_file: {
293 Account account = AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity());
294 RemoteOperation operation = new SynchronizeFileOperation(mTargetFile, null, mContainerActivity.getStorageManager(), account, true, false, getSherlockActivity());
295 operation.execute(account, getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
296 getSherlockActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
297 return true;
298 }
299 case R.id.action_cancel_download: {
300 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
301 Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity());
302 if (downloaderBinder != null && downloaderBinder.isDownloading(account, mTargetFile)) {
303 downloaderBinder.cancel(account, mTargetFile);
304 listDirectory();
305 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
306 }
307 return true;
308 }
309 case R.id.action_cancel_upload: {
310 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
311 Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity());
312 if (uploaderBinder != null && uploaderBinder.isUploading(account, mTargetFile)) {
313 uploaderBinder.cancel(account, mTargetFile);
314 listDirectory();
315 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
316 }
317 return true;
318 }
319 case R.id.action_see_details: {
320 ((FileFragment.ContainerActivity)getActivity()).showDetails(mTargetFile);
321 return true;
322 }
323 default:
324 return super.onContextItemSelected(item);
325 }
326 }
327
328
329 /**
330 * Use this to query the {@link OCFile} that is currently
331 * being displayed by this fragment
332 * @return The currently viewed OCFile
333 */
334 public OCFile getCurrentFile(){
335 return mFile;
336 }
337
338 /**
339 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
340 */
341 public void listDirectory(){
342 listDirectory(null);
343 }
344
345 /**
346 * Lists the given directory on the view. When the input parameter is null,
347 * it will either refresh the last known directory. list the root
348 * if there never was a directory.
349 *
350 * @param directory File to be listed
351 */
352 public void listDirectory(OCFile directory) {
353 DataStorageManager storageManager = mContainerActivity.getStorageManager();
354 if (storageManager != null) {
355
356 // Check input parameters for null
357 if(directory == null){
358 if(mFile != null){
359 directory = mFile;
360 } else {
361 directory = storageManager.getFileByPath("/");
362 if (directory == null) return; // no files, wait for sync
363 }
364 }
365
366
367 // If that's not a directory -> List its parent
368 if(!directory.isDirectory()){
369 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
370 directory = storageManager.getFileById(directory.getParentId());
371 }
372
373 mAdapter.swapDirectory(directory, storageManager);
374 if (mFile == null || !mFile.equals(directory)) {
375 mList.setSelectionFromTop(0, 0);
376 }
377 mFile = directory;
378 }
379 }
380
381
382
383 /**
384 * Interface to implement by any Activity that includes some instance of FileListFragment
385 *
386 * @author David A. Velasco
387 */
388 public interface ContainerActivity extends TransferServiceGetter, OnRemoteOperationListener, FileHandler {
389
390 /**
391 * Callback method invoked when a the user browsed into a different folder through the list of files
392 *
393 * @param file
394 */
395 public void onBrowsedDownTo(OCFile folder);
396
397 public void startDownloadForPreview(OCFile file);
398
399 public void startMediaPreview(OCFile file, int i, boolean b);
400
401 public void startImagePreview(OCFile file);
402
403 public void syncFolderOperation(String remotePath, long parentId);
404
405 /**
406 * Getter for the current DataStorageManager in the container activity
407 */
408 public DataStorageManager getStorageManager();
409
410
411 /**
412 * Callback method invoked when a the 'transfer state' of a file changes.
413 *
414 * This happens when a download or upload is started or ended for a file.
415 *
416 * This method is necessary by now to update the user interface of the double-pane layout in tablets
417 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
418 * won't provide the needed response before the method where this is called finishes.
419 *
420 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
421 *
422 * @param file OCFile which state changed.
423 * @param downloading Flag signaling if the file is now downloading.
424 * @param uploading Flag signaling if the file is now uploading.
425 */
426 public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading);
427
428 }
429
430
431 @Override
432 public void onDismiss(EditNameDialog dialog) {
433 if (dialog.getResult()) {
434 String newFilename = dialog.getNewFilename();
435 Log_OC.d(TAG, "name edit dialog dismissed with new name " + newFilename);
436 RemoteOperation operation = new RenameFileOperation(mTargetFile,
437 AccountUtils.getCurrentOwnCloudAccount(getActivity()),
438 newFilename,
439 mContainerActivity.getStorageManager());
440 operation.execute(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
441 getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
442 }
443 }
444
445
446 @Override
447 public void onConfirmation(String callerTag) {
448 if (callerTag.equals(FileDetailFragment.FTAG_CONFIRMATION)) {
449 if (mContainerActivity.getStorageManager().getFileById(mTargetFile.getFileId()) != null) {
450 RemoteOperation operation = new RemoveFileOperation( mTargetFile,
451 true,
452 mContainerActivity.getStorageManager());
453 operation.execute(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
454
455 getActivity().showDialog(FileDisplayActivity.DIALOG_SHORT_WAIT);
456 }
457 }
458 }
459
460 @Override
461 public void onNeutral(String callerTag) {
462 File f = null;
463 if (mTargetFile.isDirectory()) {
464 // TODO run in a secondary thread?
465 mContainerActivity.getStorageManager().removeDirectory(mTargetFile, false, true);
466
467 } else if (mTargetFile.isDown() && (f = new File(mTargetFile.getStoragePath())).exists()) {
468 f.delete();
469 mTargetFile.setStoragePath(null);
470 mContainerActivity.getStorageManager().saveFile(mTargetFile);
471 }
472 listDirectory();
473 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
474 }
475
476 @Override
477 public void onCancel(String callerTag) {
478 Log_OC.d(TAG, "REMOVAL CANCELED");
479 }
480
481
482 }