Update images
[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
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
138 @Override
139 public void onItemClick(AdapterView<?> l, View v, int position, long id) {
140 OCFile file = (OCFile) mAdapter.getItem(position);
141 if (file != null) {
142 if (file.isDirectory()) {
143 // update state and view of this fragment
144 listDirectory(file);
145 // then, notify parent activity to let it update its state and view, and other fragments
146 mContainerActivity.onBrowsedDownTo(file);
147
148 } else { /// Click on a file
149 if (PreviewImageFragment.canBePreviewed(file)) {
150 // preview image - it handles the download, if needed
151 mContainerActivity.startImagePreview(file);
152
153 } else if (file.isDown()) {
154 if (PreviewMediaFragment.canBePreviewed(file)) {
155 // media preview
156 mContainerActivity.startMediaPreview(file, 0, true);
157 } else {
158 // open with
159 mContainerActivity.openFile(file);
160 }
161
162 } else {
163 // automatic download, preview on finish
164 mContainerActivity.startDownloadForPreview(file);
165 }
166
167 }
168
169 } else {
170 Log_OC.d(TAG, "Null object in ListAdapter!!");
171 }
172
173 }
174
175 /**
176 * {@inheritDoc}
177 */
178 @Override
179 public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
180 super.onCreateContextMenu(menu, v, menuInfo);
181 MenuInflater inflater = getActivity().getMenuInflater();
182 inflater.inflate(R.menu.file_actions_menu, menu);
183 AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
184 OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
185 List<Integer> toHide = new ArrayList<Integer>();
186 List<Integer> toDisable = new ArrayList<Integer>();
187
188 MenuItem item = null;
189 if (targetFile.isDirectory()) {
190 // contextual menu for folders
191 toHide.add(R.id.action_open_file_with);
192 toHide.add(R.id.action_download_file);
193 toHide.add(R.id.action_cancel_download);
194 toHide.add(R.id.action_cancel_upload);
195 toHide.add(R.id.action_sync_file);
196 toHide.add(R.id.action_see_details);
197 if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile) ||
198 mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile) ) {
199 toDisable.add(R.id.action_rename_file);
200 toDisable.add(R.id.action_remove_file);
201
202 }
203
204 } else {
205 // contextual menu for regular files
206
207 // new design: 'download' and 'open with' won't be available anymore in context menu
208 toHide.add(R.id.action_download_file);
209 toHide.add(R.id.action_open_file_with);
210
211 if (targetFile.isDown()) {
212 toHide.add(R.id.action_cancel_download);
213 toHide.add(R.id.action_cancel_upload);
214
215 } else {
216 toHide.add(R.id.action_sync_file);
217 }
218 if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
219 toHide.add(R.id.action_cancel_upload);
220 toDisable.add(R.id.action_rename_file);
221 toDisable.add(R.id.action_remove_file);
222
223 } else if ( mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getActivity()), targetFile)) {
224 toHide.add(R.id.action_cancel_download);
225 toDisable.add(R.id.action_rename_file);
226 toDisable.add(R.id.action_remove_file);
227
228 } else {
229 toHide.add(R.id.action_cancel_download);
230 toHide.add(R.id.action_cancel_upload);
231 }
232 }
233
234 for (int i : toHide) {
235 item = menu.findItem(i);
236 if (item != null) {
237 item.setVisible(false);
238 item.setEnabled(false);
239 }
240 }
241
242 for (int i : toDisable) {
243 item = menu.findItem(i);
244 if (item != null) {
245 item.setEnabled(false);
246 }
247 }
248 }
249
250
251 /**
252 * {@inhericDoc}
253 */
254 @Override
255 public boolean onContextItemSelected (MenuItem item) {
256 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
257 mTargetFile = (OCFile) mAdapter.getItem(info.position);
258 switch (item.getItemId()) {
259 case R.id.action_rename_file: {
260 String fileName = mTargetFile.getFileName();
261 int extensionStart = mTargetFile.isDirectory() ? -1 : fileName.lastIndexOf(".");
262 int selectionEnd = (extensionStart >= 0) ? extensionStart : fileName.length();
263 EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this);
264 dialog.show(getFragmentManager(), EditNameDialog.TAG);
265 return true;
266 }
267 case R.id.action_remove_file: {
268 int messageStringId = R.string.confirmation_remove_alert;
269 int posBtnStringId = R.string.confirmation_remove_remote;
270 int neuBtnStringId = -1;
271 if (mTargetFile.isDirectory()) {
272 messageStringId = R.string.confirmation_remove_folder_alert;
273 posBtnStringId = R.string.confirmation_remove_remote_and_local;
274 neuBtnStringId = R.string.confirmation_remove_folder_local;
275 } else if (mTargetFile.isDown()) {
276 posBtnStringId = R.string.confirmation_remove_remote_and_local;
277 neuBtnStringId = R.string.confirmation_remove_local;
278 }
279 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
280 messageStringId,
281 new String[]{mTargetFile.getFileName()},
282 posBtnStringId,
283 neuBtnStringId,
284 R.string.common_cancel);
285 confDialog.setOnConfirmationListener(this);
286 confDialog.show(getFragmentManager(), FileDetailFragment.FTAG_CONFIRMATION);
287 return true;
288 }
289 case R.id.action_sync_file: {
290 Account account = AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity());
291 RemoteOperation operation = new SynchronizeFileOperation(mTargetFile, null, mContainerActivity.getStorageManager(), account, true, false, getSherlockActivity());
292 operation.execute(account, getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
293 ((FileDisplayActivity) getSherlockActivity()).showLoadingDialog();
294 return true;
295 }
296 case R.id.action_cancel_download: {
297 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
298 Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity());
299 if (downloaderBinder != null && downloaderBinder.isDownloading(account, mTargetFile)) {
300 downloaderBinder.cancel(account, mTargetFile);
301 listDirectory();
302 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
303 }
304 return true;
305 }
306 case R.id.action_cancel_upload: {
307 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
308 Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity());
309 if (uploaderBinder != null && uploaderBinder.isUploading(account, mTargetFile)) {
310 uploaderBinder.cancel(account, mTargetFile);
311 listDirectory();
312 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
313 }
314 return true;
315 }
316 case R.id.action_see_details: {
317 ((FileFragment.ContainerActivity)getActivity()).showDetails(mTargetFile);
318 return true;
319 }
320 default:
321 return super.onContextItemSelected(item);
322 }
323 }
324
325
326 /**
327 * Use this to query the {@link OCFile} that is currently
328 * being displayed by this fragment
329 * @return The currently viewed OCFile
330 */
331 public OCFile getCurrentFile(){
332 return mFile;
333 }
334
335 /**
336 * Calls {@link OCFileListFragment#listDirectory(OCFile)} with a null parameter
337 */
338 public void listDirectory(){
339 listDirectory(null);
340 }
341
342 /**
343 * Lists the given directory on the view. When the input parameter is null,
344 * it will either refresh the last known directory. list the root
345 * if there never was a directory.
346 *
347 * @param directory File to be listed
348 */
349 public void listDirectory(OCFile directory) {
350 DataStorageManager storageManager = mContainerActivity.getStorageManager();
351 if (storageManager != null) {
352
353 // Check input parameters for null
354 if(directory == null){
355 if(mFile != null){
356 directory = mFile;
357 } else {
358 directory = storageManager.getFileByPath("/");
359 if (directory == null) return; // no files, wait for sync
360 }
361 }
362
363
364 // If that's not a directory -> List its parent
365 if(!directory.isDirectory()){
366 Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
367 directory = storageManager.getFileById(directory.getParentId());
368 }
369
370 mAdapter.swapDirectory(directory, storageManager);
371 if (mFile == null || !mFile.equals(directory)) {
372 mList.setSelectionFromTop(0, 0);
373 }
374 mFile = directory;
375
376 }
377 }
378
379
380
381 /**
382 * Interface to implement by any Activity that includes some instance of FileListFragment
383 *
384 * @author David A. Velasco
385 */
386 public interface ContainerActivity extends TransferServiceGetter, OnRemoteOperationListener, FileHandler {
387
388 /**
389 * Callback method invoked when a the user browsed into a different folder through the list of files
390 *
391 * @param file
392 */
393 public void onBrowsedDownTo(OCFile folder);
394
395 public void startDownloadForPreview(OCFile file);
396
397 public void startMediaPreview(OCFile file, int i, boolean b);
398
399 public void startImagePreview(OCFile file);
400
401 /**
402 * Getter for the current DataStorageManager in the container activity
403 */
404 public DataStorageManager getStorageManager();
405
406
407 /**
408 * Callback method invoked when a the 'transfer state' of a file changes.
409 *
410 * This happens when a download or upload is started or ended for a file.
411 *
412 * This method is necessary by now to update the user interface of the double-pane layout in tablets
413 * because methods {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and {@link FileUploaderBinder#isUploading(Account, OCFile)}
414 * won't provide the needed response before the method where this is called finishes.
415 *
416 * TODO Remove this when the transfer state of a file is kept in the database (other thing TODO)
417 *
418 * @param file OCFile which state changed.
419 * @param downloading Flag signaling if the file is now downloading.
420 * @param uploading Flag signaling if the file is now uploading.
421 */
422 public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading);
423
424 }
425
426
427 @Override
428 public void onDismiss(EditNameDialog dialog) {
429 if (dialog.getResult()) {
430 String newFilename = dialog.getNewFilename();
431 Log_OC.d(TAG, "name edit dialog dismissed with new name " + newFilename);
432 RemoteOperation operation = new RenameFileOperation(mTargetFile,
433 AccountUtils.getCurrentOwnCloudAccount(getActivity()),
434 newFilename,
435 mContainerActivity.getStorageManager());
436 operation.execute(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
437 ((FileDisplayActivity) getActivity()).showLoadingDialog();
438 }
439 }
440
441
442 @Override
443 public void onConfirmation(String callerTag) {
444 if (callerTag.equals(FileDetailFragment.FTAG_CONFIRMATION)) {
445 if (mContainerActivity.getStorageManager().getFileById(mTargetFile.getFileId()) != null) {
446 RemoteOperation operation = new RemoveFileOperation( mTargetFile,
447 true,
448 mContainerActivity.getStorageManager());
449 operation.execute(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), getSherlockActivity(), mContainerActivity, mHandler, getSherlockActivity());
450
451 ((FileDisplayActivity) getActivity()).showLoadingDialog();
452 }
453 }
454 }
455
456 @Override
457 public void onNeutral(String callerTag) {
458 File f = null;
459 if (mTargetFile.isDirectory()) {
460 // TODO run in a secondary thread?
461 mContainerActivity.getStorageManager().removeDirectory(mTargetFile, false, true);
462
463 } else if (mTargetFile.isDown() && (f = new File(mTargetFile.getStoragePath())).exists()) {
464 f.delete();
465 mTargetFile.setStoragePath(null);
466 mContainerActivity.getStorageManager().saveFile(mTargetFile);
467 }
468 listDirectory();
469 mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
470 }
471
472 @Override
473 public void onCancel(String callerTag) {
474 Log_OC.d(TAG, "REMOVAL CANCELED");
475 }
476
477
478 }