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