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