Fixes the last reported bugs
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / FileDetailFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22 package com.owncloud.android.ui.fragment;
23
24 import android.accounts.Account;
25 import android.os.Bundle;
26 import android.view.LayoutInflater;
27 import android.view.Menu;
28 import android.view.MenuInflater;
29 import android.view.MenuItem;
30 import android.view.View;
31 import android.view.View.OnClickListener;
32 import android.view.ViewGroup;
33 import android.widget.CheckBox;
34 import android.widget.ImageView;
35 import android.widget.ProgressBar;
36 import android.widget.TextView;
37
38 import com.owncloud.android.R;
39 import com.owncloud.android.datamodel.FileDataStorageManager;
40 import com.owncloud.android.datamodel.OCFile;
41 import com.owncloud.android.files.FileMenuFilter;
42 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
43 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
44 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
45 import com.owncloud.android.lib.common.utils.Log_OC;
46 import com.owncloud.android.ui.activity.FileActivity;
47 import com.owncloud.android.ui.activity.FileDisplayActivity;
48 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
49 import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
50 import com.owncloud.android.utils.DisplayUtils;
51
52 import java.lang.ref.WeakReference;
53
54
55 /**
56 * This Fragment is used to display the details about a file.
57 */
58 public class FileDetailFragment extends FileFragment implements OnClickListener {
59
60 private int mLayout;
61 private View mView;
62 private Account mAccount;
63
64 public ProgressListener mProgressListener;
65
66 private static final String TAG = FileDetailFragment.class.getSimpleName();
67 public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
68 public static final String FTAG_RENAME_FILE = "RENAME_FILE_FRAGMENT";
69
70 private static final String ARG_FILE = "FILE";
71 private static final String ARG_ACCOUNT = "ACCOUNT";
72
73
74 /**
75 * Public factory method to create new FileDetailFragment instances.
76 *
77 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
78 *
79 * @param fileToDetail An {@link OCFile} to show in the fragment
80 * @param account An ownCloud account; needed to start downloads
81 * @return New fragment with arguments set
82 */
83 public static FileDetailFragment newInstance(OCFile fileToDetail, Account account) {
84 FileDetailFragment frag = new FileDetailFragment();
85 Bundle args = new Bundle();
86 args.putParcelable(ARG_FILE, fileToDetail);
87 args.putParcelable(ARG_ACCOUNT, account);
88 frag.setArguments(args);
89 return frag;
90 }
91
92 /**
93 * Creates an empty details fragment.
94 *
95 * It's necessary to keep a public constructor without parameters; the system uses it when tries
96 * to reinstantiate a fragment automatically.
97 */
98 public FileDetailFragment() {
99 super();
100 mAccount = null;
101 mLayout = R.layout.file_details_empty;
102 mProgressListener = null;
103 }
104
105
106 @Override
107 public void onActivityCreated(Bundle savedInstanceState) {
108 super.onCreate(savedInstanceState);
109 setHasOptionsMenu(true);
110 }
111
112
113 @Override
114 public View onCreateView(LayoutInflater inflater, ViewGroup container,
115 Bundle savedInstanceState) {
116
117 setFile((OCFile) getArguments().getParcelable(ARG_FILE));
118 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
119
120 if (savedInstanceState != null) {
121 setFile((OCFile) savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
122 mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
123 }
124
125 if (getFile() != null && mAccount != null) {
126 mLayout = R.layout.file_details_fragment;
127 }
128
129 mView = inflater.inflate(mLayout, null);
130
131 if (mLayout == R.layout.file_details_fragment) {
132 mView.findViewById(R.id.fdFavorite).setOnClickListener(this);
133 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.fdProgressBar);
134 mProgressListener = new ProgressListener(progressBar);
135 mView.findViewById(R.id.fdCancelBtn).setOnClickListener(this);
136 }
137
138 updateFileDetails(false, false);
139 return mView;
140 }
141
142 @Override
143 public void onSaveInstanceState(Bundle outState) {
144 super.onSaveInstanceState(outState);
145 outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
146 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
147 }
148
149 @Override
150 public void onStart() {
151 super.onStart();
152 listenForTransferProgress();
153 }
154
155 @Override
156 public void onStop() {
157 leaveTransferProgress();
158 super.onStop();
159 }
160
161
162 @Override
163 public View getView() {
164 return super.getView() == null ? mView : super.getView();
165 }
166
167
168 /**
169 * {@inheritDoc}
170 */
171 @Override
172 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
173 super.onCreateOptionsMenu(menu, inflater);
174 inflater.inflate(R.menu.file_actions_menu, menu);
175 }
176
177
178 /**
179 * {@inheritDoc}
180 */
181 @Override
182 public void onPrepareOptionsMenu(Menu menu) {
183 super.onPrepareOptionsMenu(menu);
184
185 if (mContainerActivity.getStorageManager() != null) {
186 FileMenuFilter mf = new FileMenuFilter(
187 getFile(),
188 mContainerActivity.getStorageManager().getAccount(),
189 mContainerActivity,
190 getActivity()
191 );
192 mf.filter(menu);
193 }
194
195 // additional restriction for this fragment
196 MenuItem item = menu.findItem(R.id.action_see_details);
197 if (item != null) {
198 item.setVisible(false);
199 item.setEnabled(false);
200 }
201
202 // additional restriction for this fragment
203 item = menu.findItem(R.id.action_move);
204 if (item != null) {
205 item.setVisible(false);
206 item.setEnabled(false);
207 }
208
209 // additional restriction for this fragment
210 item = menu.findItem(R.id.action_copy);
211 if (item != null) {
212 item.setVisible(false);
213 item.setEnabled(false);
214 }
215 }
216
217
218 /**
219 * {@inheritDoc}
220 */
221 @Override
222 public boolean onOptionsItemSelected(MenuItem item) {
223 switch (item.getItemId()) {
224 case R.id.action_share_file: {
225 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
226 return true;
227 }
228 case R.id.action_unshare_file: {
229 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
230 return true;
231 }
232 case R.id.action_open_file_with: {
233 mContainerActivity.getFileOperationsHelper().openFile(getFile());
234 return true;
235 }
236 case R.id.action_remove_file: {
237 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
238 dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
239 return true;
240 }
241 case R.id.action_rename_file: {
242 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
243 dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
244 return true;
245 }
246 case R.id.action_cancel_download:
247 case R.id.action_cancel_upload: {
248 ((FileDisplayActivity) mContainerActivity).cancelTransference(getFile());
249 return true;
250 }
251 case R.id.action_download_file:
252 case R.id.action_sync_file: {
253 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
254 return true;
255 }
256 case R.id.action_send_file: {
257 // Obtain the file
258 if (!getFile().isDown()) { // Download the file
259 Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
260 ((FileDisplayActivity) mContainerActivity).startDownloadForSending(getFile());
261
262 }
263 else {
264 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
265 }
266 return true;
267 }
268 case R.id.action_favorite_file:{
269 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), true);
270 return true;
271 }
272 case R.id.action_unfavorite_file:{
273 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), false);
274 return true;
275 }
276 default:
277 return false;
278 }
279 }
280
281 @Override
282 public void onClick(View v) {
283 switch (v.getId()) {
284 case R.id.fdFavorite: {
285 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdFavorite);
286 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(),cb.isChecked());
287 break;
288 }
289 case R.id.fdCancelBtn: {
290 ((FileDisplayActivity) mContainerActivity).cancelTransference(getFile());
291 break;
292 }
293 default:
294 Log_OC.e(TAG, "Incorrect view clicked!");
295 }
296 }
297
298
299 /**
300 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
301 *
302 * @return True when the fragment was created with the empty layout.
303 */
304 public boolean isEmpty() {
305 return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
306 }
307
308
309 /**
310 * Use this method to signal this Activity that it shall update its view.
311 *
312 * @param file : An {@link OCFile}
313 */
314 public void updateFileDetails(OCFile file, Account ocAccount) {
315 setFile(file);
316 mAccount = ocAccount;
317 updateFileDetails(false, false);
318 }
319
320 /**
321 * Updates the view with all relevant details about that file.
322 * <p/>
323 * TODO Remove parameter when the transferring state of files is kept in database.
324 *
325 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
326 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
327 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
328 * @param refresh If 'true', try to refresh the whole file from the database
329 */
330 public void updateFileDetails(boolean transferring, boolean refresh) {
331 if (readyToShow()) {
332 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
333 if (refresh && storageManager != null) {
334 setFile(storageManager.getFileByPath(getFile().getRemotePath()));
335 }
336 OCFile file = getFile();
337
338 // set file details
339 setFilename(file.getFileName());
340 setFiletype(file.getMimetype(), file.getFileName());
341 setFilesize(file.getFileLength());
342
343 setTimeModified(file.getModificationTimestamp());
344
345 CheckBox cb = (CheckBox)getView().findViewById(R.id.fdFavorite);
346 cb.setChecked(file.isFavorite());
347
348 // configure UI for depending upon local state of the file
349 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
350 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
351 if (transferring ||
352 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
353 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
354 ) {
355 setButtonsForTransferring();
356
357 } else if (file.isDown()) {
358
359 setButtonsForDown();
360
361 } else {
362 // TODO load default preview image; when the local file is removed, the preview
363 // remains there
364 setButtonsForRemote();
365 }
366 }
367 getView().invalidate();
368 }
369
370 /**
371 * Checks if the fragment is ready to show details of a OCFile
372 *
373 * @return 'True' when the fragment is ready to show details of a file
374 */
375 private boolean readyToShow() {
376 return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
377 }
378
379
380 /**
381 * Updates the filename in view
382 *
383 * @param filename to set
384 */
385 private void setFilename(String filename) {
386 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
387 if (tv != null) {
388 tv.setText(filename);
389 }
390 }
391
392 /**
393 * Updates the MIME type in view
394 * @param mimetype MIME type to set
395 * @param filename Name of the file, to deduce the icon to use in case the MIME type is not precise enough
396 */
397 private void setFiletype(String mimetype, String filename) {
398 TextView tv = (TextView) getView().findViewById(R.id.fdType);
399 if (tv != null) {
400 String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);
401 tv.setText(printableMimetype);
402 }
403 ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
404 if (iv != null) {
405 iv.setImageResource(DisplayUtils.getFileTypeIconId(mimetype, filename));
406 }
407 }
408
409 /**
410 * Updates the file size in view
411 *
412 * @param filesize in bytes to set
413 */
414 private void setFilesize(long filesize) {
415 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
416 if (tv != null) {
417 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
418 }
419 }
420
421 /**
422 * Updates the time that the file was last modified
423 *
424 * @param milliseconds Unix time to set
425 */
426 private void setTimeModified(long milliseconds) {
427 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
428 if (tv != null) {
429 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
430 }
431 }
432
433 /**
434 * Enables or disables buttons for a file being downloaded
435 */
436 private void setButtonsForTransferring() {
437 if (!isEmpty()) {
438 // let's protect the user from himself ;)
439 getView().findViewById(R.id.fdFavorite).setEnabled(false);
440
441 // show the progress bar for the transfer
442 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
443 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
444 progressText.setVisibility(View.VISIBLE);
445 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
446 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
447 //if (getFile().isDownloading()) {
448 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
449 progressText.setText(R.string.downloader_download_in_progress_ticker);
450 }
451 else {
452 if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
453 progressText.setText(R.string.uploader_upload_in_progress_ticker);
454 }
455 }
456 }
457 }
458
459 /**
460 * Enables or disables buttons for a file locally available
461 */
462 private void setButtonsForDown() {
463 if (!isEmpty()) {
464 getView().findViewById(R.id.fdFavorite).setEnabled(true);
465
466 // hides the progress bar
467 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
468 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
469 progressText.setVisibility(View.GONE);
470 }
471 }
472
473 /**
474 * Enables or disables buttons for a file not locally available
475 */
476 private void setButtonsForRemote() {
477 if (!isEmpty()) {
478 getView().findViewById(R.id.fdFavorite).setEnabled(true);
479
480 // hides the progress bar
481 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
482 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
483 progressText.setVisibility(View.GONE);
484 }
485 }
486
487
488 public void listenForTransferProgress() {
489 if (mProgressListener != null) {
490 if (mContainerActivity.getFileDownloaderBinder() != null) {
491 mContainerActivity.getFileDownloaderBinder().
492 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
493 }
494 if (mContainerActivity.getFileUploaderBinder() != null) {
495 mContainerActivity.getFileUploaderBinder().
496 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
497 }
498 }
499 }
500
501
502 public void leaveTransferProgress() {
503 if (mProgressListener != null) {
504 if (mContainerActivity.getFileDownloaderBinder() != null) {
505 mContainerActivity.getFileDownloaderBinder().
506 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
507 }
508 if (mContainerActivity.getFileUploaderBinder() != null) {
509 mContainerActivity.getFileUploaderBinder().
510 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
511 }
512 }
513 }
514
515
516 /**
517 * Helper class responsible for updating the progress bar shown for file uploading or
518 * downloading
519 */
520 private class ProgressListener implements OnDatatransferProgressListener {
521 int mLastPercent = 0;
522 WeakReference<ProgressBar> mProgressBar = null;
523
524 ProgressListener(ProgressBar progressBar) {
525 mProgressBar = new WeakReference<ProgressBar>(progressBar);
526 }
527
528 @Override
529 public void onTransferProgress(long progressRate, long totalTransferredSoFar,
530 long totalToTransfer, String filename) {
531 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
532 if (percent != mLastPercent) {
533 ProgressBar pb = mProgressBar.get();
534 if (pb != null) {
535 pb.setProgress(percent);
536 pb.postInvalidate();
537 }
538 }
539 mLastPercent = percent;
540 }
541
542 }
543
544 }