Merge branch 'master' into text_file_preview_pr_707_with_develop
[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 DisplayUtils.colorPreLollipopHorizontalProgressBar(progressBar);
135 mProgressListener = new ProgressListener(progressBar);
136 mView.findViewById(R.id.fdCancelBtn).setOnClickListener(this);
137 }
138
139 updateFileDetails(false, false);
140 return mView;
141 }
142
143 @Override
144 public void onSaveInstanceState(Bundle outState) {
145 super.onSaveInstanceState(outState);
146 outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
147 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
148 }
149
150 @Override
151 public void onStart() {
152 super.onStart();
153 listenForTransferProgress();
154 }
155
156 @Override
157 public void onStop() {
158 leaveTransferProgress();
159 super.onStop();
160 }
161
162
163 @Override
164 public View getView() {
165 return super.getView() == null ? mView : super.getView();
166 }
167
168
169 /**
170 * {@inheritDoc}
171 */
172 @Override
173 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
174 super.onCreateOptionsMenu(menu, inflater);
175 inflater.inflate(R.menu.file_actions_menu, menu);
176 }
177
178
179 /**
180 * {@inheritDoc}
181 */
182 @Override
183 public void onPrepareOptionsMenu(Menu menu) {
184 super.onPrepareOptionsMenu(menu);
185
186 if (mContainerActivity.getStorageManager() != null) {
187 FileMenuFilter mf = new FileMenuFilter(
188 getFile(),
189 mContainerActivity.getStorageManager().getAccount(),
190 mContainerActivity,
191 getActivity()
192 );
193 mf.filter(menu);
194 }
195
196 // additional restriction for this fragment
197 MenuItem item = menu.findItem(R.id.action_see_details);
198 if (item != null) {
199 item.setVisible(false);
200 item.setEnabled(false);
201 }
202
203 // additional restriction for this fragment
204 item = menu.findItem(R.id.action_move);
205 if (item != null) {
206 item.setVisible(false);
207 item.setEnabled(false);
208 }
209
210 // additional restriction for this fragment
211 item = menu.findItem(R.id.action_copy);
212 if (item != null) {
213 item.setVisible(false);
214 item.setEnabled(false);
215 }
216 }
217
218
219 /**
220 * {@inheritDoc}
221 */
222 @Override
223 public boolean onOptionsItemSelected(MenuItem item) {
224 switch (item.getItemId()) {
225 case R.id.action_share_file: {
226 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
227 return true;
228 }
229 case R.id.action_unshare_file: {
230 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
231 return true;
232 }
233 case R.id.action_open_file_with: {
234 mContainerActivity.getFileOperationsHelper().openFile(getFile());
235 return true;
236 }
237 case R.id.action_remove_file: {
238 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
239 dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
240 return true;
241 }
242 case R.id.action_rename_file: {
243 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
244 dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
245 return true;
246 }
247 case R.id.action_cancel_download:
248 case R.id.action_cancel_upload: {
249 ((FileDisplayActivity) mContainerActivity).cancelTransference(getFile());
250 return true;
251 }
252 case R.id.action_download_file:
253 case R.id.action_sync_file: {
254 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
255 return true;
256 }
257 case R.id.action_send_file: {
258 // Obtain the file
259 if (!getFile().isDown()) { // Download the file
260 Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
261 ((FileDisplayActivity) mContainerActivity).startDownloadForSending(getFile());
262
263 }
264 else {
265 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
266 }
267 return true;
268 }
269 case R.id.action_favorite_file:{
270 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), true);
271 return true;
272 }
273 case R.id.action_unfavorite_file:{
274 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), false);
275 return true;
276 }
277 default:
278 return false;
279 }
280 }
281
282 @Override
283 public void onClick(View v) {
284 switch (v.getId()) {
285 case R.id.fdFavorite: {
286 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdFavorite);
287 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(),cb.isChecked());
288 break;
289 }
290 case R.id.fdCancelBtn: {
291 ((FileDisplayActivity) mContainerActivity).cancelTransference(getFile());
292 break;
293 }
294 default:
295 Log_OC.e(TAG, "Incorrect view clicked!");
296 }
297 }
298
299
300 /**
301 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
302 *
303 * @return True when the fragment was created with the empty layout.
304 */
305 public boolean isEmpty() {
306 return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
307 }
308
309
310 /**
311 * Use this method to signal this Activity that it shall update its view.
312 *
313 * @param file : An {@link OCFile}
314 */
315 public void updateFileDetails(OCFile file, Account ocAccount) {
316 setFile(file);
317 mAccount = ocAccount;
318 updateFileDetails(false, false);
319 }
320
321 /**
322 * Updates the view with all relevant details about that file.
323 * <p/>
324 * TODO Remove parameter when the transferring state of files is kept in database.
325 *
326 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
327 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
328 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
329 * @param refresh If 'true', try to refresh the whole file from the database
330 */
331 public void updateFileDetails(boolean transferring, boolean refresh) {
332 if (readyToShow()) {
333 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
334 if (refresh && storageManager != null) {
335 setFile(storageManager.getFileByPath(getFile().getRemotePath()));
336 }
337 OCFile file = getFile();
338
339 // set file details
340 setFilename(file.getFileName());
341 setFiletype(file.getMimetype(), file.getFileName());
342 setFilesize(file.getFileLength());
343
344 setTimeModified(file.getModificationTimestamp());
345
346 CheckBox cb = (CheckBox)getView().findViewById(R.id.fdFavorite);
347 cb.setChecked(file.isFavorite());
348
349 // configure UI for depending upon local state of the file
350 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
351 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
352 if (transferring ||
353 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
354 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
355 ) {
356 setButtonsForTransferring();
357
358 } else if (file.isDown()) {
359
360 setButtonsForDown();
361
362 } else {
363 // TODO load default preview image; when the local file is removed, the preview
364 // remains there
365 setButtonsForRemote();
366 }
367 }
368 getView().invalidate();
369 }
370
371 /**
372 * Checks if the fragment is ready to show details of a OCFile
373 *
374 * @return 'True' when the fragment is ready to show details of a file
375 */
376 private boolean readyToShow() {
377 return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
378 }
379
380
381 /**
382 * Updates the filename in view
383 *
384 * @param filename to set
385 */
386 private void setFilename(String filename) {
387 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
388 if (tv != null) {
389 tv.setText(filename);
390 }
391 }
392
393 /**
394 * Updates the MIME type in view
395 * @param mimetype MIME type to set
396 * @param filename Name of the file, to deduce the icon to use in case the MIME type is not precise enough
397 */
398 private void setFiletype(String mimetype, String filename) {
399 TextView tv = (TextView) getView().findViewById(R.id.fdType);
400 if (tv != null) {
401 String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);
402 tv.setText(printableMimetype);
403 }
404 ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
405 if (iv != null) {
406 iv.setImageResource(DisplayUtils.getFileTypeIconId(mimetype, filename));
407 }
408 }
409
410 /**
411 * Updates the file size in view
412 *
413 * @param filesize in bytes to set
414 */
415 private void setFilesize(long filesize) {
416 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
417 if (tv != null) {
418 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
419 }
420 }
421
422 /**
423 * Updates the time that the file was last modified
424 *
425 * @param milliseconds Unix time to set
426 */
427 private void setTimeModified(long milliseconds) {
428 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
429 if (tv != null) {
430 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
431 }
432 }
433
434 /**
435 * Enables or disables buttons for a file being downloaded
436 */
437 private void setButtonsForTransferring() {
438 if (!isEmpty()) {
439 // let's protect the user from himself ;)
440 getView().findViewById(R.id.fdFavorite).setEnabled(false);
441
442 // show the progress bar for the transfer
443 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
444 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
445 progressText.setVisibility(View.VISIBLE);
446 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
447 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
448 //if (getFile().isDownloading()) {
449 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
450 progressText.setText(R.string.downloader_download_in_progress_ticker);
451 }
452 else {
453 if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
454 progressText.setText(R.string.uploader_upload_in_progress_ticker);
455 }
456 }
457 }
458 }
459
460 /**
461 * Enables or disables buttons for a file locally available
462 */
463 private void setButtonsForDown() {
464 if (!isEmpty()) {
465 getView().findViewById(R.id.fdFavorite).setEnabled(true);
466
467 // hides the progress bar
468 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
469 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
470 progressText.setVisibility(View.GONE);
471 }
472 }
473
474 /**
475 * Enables or disables buttons for a file not locally available
476 */
477 private void setButtonsForRemote() {
478 if (!isEmpty()) {
479 getView().findViewById(R.id.fdFavorite).setEnabled(true);
480
481 // hides the progress bar
482 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
483 TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
484 progressText.setVisibility(View.GONE);
485 }
486 }
487
488
489 public void listenForTransferProgress() {
490 if (mProgressListener != null) {
491 if (mContainerActivity.getFileDownloaderBinder() != null) {
492 mContainerActivity.getFileDownloaderBinder().
493 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
494 }
495 if (mContainerActivity.getFileUploaderBinder() != null) {
496 mContainerActivity.getFileUploaderBinder().
497 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
498 }
499 }
500 }
501
502
503 public void leaveTransferProgress() {
504 if (mProgressListener != null) {
505 if (mContainerActivity.getFileDownloaderBinder() != null) {
506 mContainerActivity.getFileDownloaderBinder().
507 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
508 }
509 if (mContainerActivity.getFileUploaderBinder() != null) {
510 mContainerActivity.getFileUploaderBinder().
511 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
512 }
513 }
514 }
515
516
517 /**
518 * Helper class responsible for updating the progress bar shown for file uploading or
519 * downloading
520 */
521 private class ProgressListener implements OnDatatransferProgressListener {
522 int mLastPercent = 0;
523 WeakReference<ProgressBar> mProgressBar = null;
524
525 ProgressListener(ProgressBar progressBar) {
526 mProgressBar = new WeakReference<ProgressBar>(progressBar);
527 }
528
529 @Override
530 public void onTransferProgress(long progressRate, long totalTransferredSoFar,
531 long totalToTransfer, String filename) {
532 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
533 if (percent != mLastPercent) {
534 ProgressBar pb = mProgressBar.get();
535 if (pb != null) {
536 pb.setProgress(percent);
537 pb.postInvalidate();
538 }
539 }
540 mLastPercent = percent;
541 }
542
543 }
544
545 }