1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.ui
.activity
;
20 import android
.accounts
.Account
;
21 import android
.app
.Dialog
;
22 import android
.app
.ProgressDialog
;
23 import android
.content
.BroadcastReceiver
;
24 import android
.content
.ComponentName
;
25 import android
.content
.Context
;
26 import android
.content
.Intent
;
27 import android
.content
.IntentFilter
;
28 import android
.content
.ServiceConnection
;
29 import android
.content
.res
.Configuration
;
30 import android
.os
.Bundle
;
31 import android
.os
.IBinder
;
32 import android
.support
.v4
.app
.Fragment
;
33 import android
.support
.v4
.app
.FragmentTransaction
;
35 import com
.actionbarsherlock
.app
.ActionBar
;
36 import com
.actionbarsherlock
.view
.MenuItem
;
37 import com
.owncloud
.android
.AccountUtils
;
38 import com
.owncloud
.android
.Log_OC
;
39 import com
.owncloud
.android
.R
;
40 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
41 import com
.owncloud
.android
.datamodel
.OCFile
;
42 import com
.owncloud
.android
.files
.services
.FileDownloader
;
43 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
44 import com
.owncloud
.android
.files
.services
.FileUploader
;
45 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
46 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
47 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
48 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
49 import com
.owncloud
.android
.ui
.preview
.PreviewVideoActivity
;
52 * This activity displays the details of a file like its name, its size and so
55 * @author Bartek Przybylski
56 * @author David A. Velasco
58 public class FileDetailActivity
extends FileActivity
implements FileFragment
.ContainerActivity
{
60 public static final int DIALOG_SHORT_WAIT
= 0;
62 public static final String TAG
= FileDetailActivity
.class.getSimpleName();
64 public static final String EXTRA_MODE
= "MODE";
65 public static final int MODE_DETAILS
= 0;
66 public static final int MODE_PREVIEW
= 1;
68 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
70 private FileDownloaderBinder mDownloaderBinder
= null
;
71 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
72 private FileUploaderBinder mUploaderBinder
= null
;
73 private boolean mWaitingToPreview
;
75 private FileDataStorageManager mStorageManager
;
76 private DownloadFinishReceiver mDownloadFinishReceiver
;
78 private Configuration mNewConfigurationChangeToApplyOnStart
;
80 private boolean mStarted
;
82 private boolean mDualPane
;
86 protected void onCreate(Bundle savedInstanceState
) {
87 super.onCreate(savedInstanceState
);
90 // check if configuration is proper for this activity; tablets in landscape should pass the torch to FileDisplayActivity
91 Configuration conf
= getResources().getConfiguration();
92 mDualPane
= (conf
.orientation
== Configuration
.ORIENTATION_LANDSCAPE
&&
93 (conf
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
97 // only happens when notifications (downloads, uploads) are clicked at the notification bar
98 backToDisplayActivity(false
);
101 setContentView(R
.layout
.file_activity_details
);
103 ActionBar actionBar
= getSupportActionBar();
104 actionBar
.setDisplayHomeAsUpEnabled(true
);
106 if (savedInstanceState
== null
) {
107 mWaitingToPreview
= false
;
108 createChildFragment();
110 mWaitingToPreview
= savedInstanceState
.getBoolean(KEY_WAITING_TO_PREVIEW
);
113 mDownloadConnection
= new DetailsServiceConnection();
114 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
115 mUploadConnection
= new DetailsServiceConnection();
116 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
121 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
122 * the requested {@link Intent}.
124 private void createChildFragment() {
125 int mode
= getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
);
127 Fragment newFragment
= null
;
128 OCFile file
= getFile();
129 Account account
= getAccount();
130 if (mode
== MODE_DETAILS
) {
131 newFragment
= new FileDetailFragment(file
, account
);
133 } else if (file
.isDown()) {
134 if (PreviewMediaFragment
.canBePreviewed(file
)) {
135 int startPlaybackPosition
= getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
136 boolean autoplay
= getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
137 newFragment
= new PreviewMediaFragment(file
, account
, startPlaybackPosition
, autoplay
);
140 newFragment
= new FileDetailFragment(file
, account
);
145 newFragment
= new FileDetailFragment(file
, account
);
146 mWaitingToPreview
= true
; // download will requested
148 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
149 ft
.replace(R
.id
.fragment
, newFragment
, FileDetailFragment
.FTAG
);
154 public void onActivityResult (int requestCode
, int resultCode
, Intent data
) {
155 Log_OC
.e(TAG
, "onActivityResult");
156 super.onActivityResult(requestCode
, resultCode
, data
);
160 public void onConfigurationChanged (Configuration newConfig
) {
161 super.onConfigurationChanged(newConfig
);
163 checkConfigurationChange(newConfig
);
165 mNewConfigurationChangeToApplyOnStart
= newConfig
;
171 protected void onSaveInstanceState(Bundle outState
) {
172 super.onSaveInstanceState(outState
);
173 outState
.putBoolean(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
178 public void onStart() {
180 Log_OC
.e(TAG
, "onStart");
181 if (mNewConfigurationChangeToApplyOnStart
!= null
&& !isRedirectingToSetupAccount()) {
182 checkConfigurationChange(mNewConfigurationChangeToApplyOnStart
);
183 mNewConfigurationChangeToApplyOnStart
= null
;
188 private void checkConfigurationChange(Configuration newConfig
) {
190 Intent intent
= null
;
191 OCFile file
= getFile();
192 Account account
= getAccount();
193 if ((newConfig
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
194 && newConfig
.orientation
== Configuration
.ORIENTATION_LANDSCAPE
) {
196 intent
= new Intent(this, FileDisplayActivity
.class);
197 intent
.putExtra(EXTRA_FILE
, file
);
198 intent
.putExtra(EXTRA_ACCOUNT
, account
);
199 intent
.putExtra(EXTRA_MODE
, getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
));
200 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
201 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
202 if (fragment
!= null
&& file
!= null
&& fragment
instanceof PreviewMediaFragment
&& file
.isVideo()) {
203 PreviewMediaFragment videoFragment
= (PreviewMediaFragment
)fragment
;
204 intent
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, videoFragment
.getPosition());
205 intent
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, videoFragment
.isPlaying());
207 if (mWaitingToPreview
) {
208 intent
.putExtra(FileDisplayActivity
.EXTRA_WAITING_TO_PREVIEW
, mWaitingToPreview
);
212 intent
= new Intent(this, FileDetailActivity
.class);
213 intent
.putExtra(EXTRA_FILE
, file
);
214 intent
.putExtra(EXTRA_ACCOUNT
, account
);
215 intent
.putExtra(EXTRA_MODE
, getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
));
216 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
217 if (fragment
!= null
&& file
!= null
&& fragment
instanceof PreviewMediaFragment
&& file
.isVideo()) {
218 PreviewMediaFragment videoFragment
= (PreviewMediaFragment
)fragment
;
219 intent
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, videoFragment
.getPosition());
220 intent
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, videoFragment
.isPlaying());
222 // and maybe 'waiting to preview' flag
224 startActivity(intent
);
228 public void onStop() {
230 Log_OC
.e(TAG
, "onStop");
234 public void onPause() {
236 Log_OC
.e(TAG
, "onPause");
237 if (mDownloadFinishReceiver
!= null
) {
238 unregisterReceiver(mDownloadFinishReceiver
);
239 mDownloadFinishReceiver
= null
;
245 public void onResume() {
247 Log_OC
.e(TAG
, "onResume");
248 // TODO this is probably unnecessary
249 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
250 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
251 ((FileDetailFragment
) fragment
).updateFileDetails(false
, false
);
254 // Listen for download messages
255 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
256 downloadIntentFilter
.addAction(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
257 mDownloadFinishReceiver
= new DownloadFinishReceiver();
258 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
262 /** Defines callbacks for service binding, passed to bindService() */
263 private class DetailsServiceConnection
implements ServiceConnection
{
266 public void onServiceConnected(ComponentName component
, IBinder service
) {
268 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
269 Log_OC
.d(TAG
, "Download service connected");
270 mDownloaderBinder
= (FileDownloaderBinder
) service
;
271 if (mWaitingToPreview
) {
272 requestForDownload();
275 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
276 Log_OC
.d(TAG
, "Upload service connected");
277 mUploaderBinder
= (FileUploaderBinder
) service
;
282 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
283 FileDetailFragment detailsFragment
= (fragment
instanceof FileDetailFragment
) ?
(FileDetailFragment
) fragment
: null
;
284 if (detailsFragment
!= null
) {
285 detailsFragment
.listenForTransferProgress();
286 detailsFragment
.updateFileDetails(mWaitingToPreview
, false
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
291 public void onServiceDisconnected(ComponentName component
) {
292 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
293 Log_OC
.d(TAG
, "Download service disconnected");
294 mDownloaderBinder
= null
;
295 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
296 Log_OC
.d(TAG
, "Upload service disconnected");
297 mUploaderBinder
= null
;
304 public void onDestroy() {
306 Log_OC
.e(TAG
, "onDestroy");
307 if (mDownloadConnection
!= null
) {
308 unbindService(mDownloadConnection
);
309 mDownloadConnection
= null
;
311 if (mUploadConnection
!= null
) {
312 unbindService(mUploadConnection
);
313 mUploadConnection
= null
;
319 public boolean onOptionsItemSelected(MenuItem item
) {
320 boolean returnValue
= false
;
322 switch(item
.getItemId()){
323 case android
.R
.id
.home
:
324 backToDisplayActivity(true
);
328 returnValue
= super.onOptionsItemSelected(item
);
335 public void onBackPressed() {
336 backToDisplayActivity(true
);
339 private void backToDisplayActivity(boolean moveToParent
) {
340 Intent intent
= new Intent(this, FileDisplayActivity
.class);
341 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
342 OCFile targetFile
= null
;
343 OCFile file
= getFile();
345 targetFile
= moveToParent ? mStorageManager
.getFileById(file
.getParentId()) : file
;
347 intent
.putExtra(EXTRA_FILE
, targetFile
);
348 intent
.putExtra(EXTRA_ACCOUNT
, getAccount());
349 startActivity(intent
);
354 protected Dialog
onCreateDialog(int id
) {
355 Dialog dialog
= null
;
357 case DIALOG_SHORT_WAIT
: {
358 ProgressDialog working_dialog
= new ProgressDialog(this);
359 working_dialog
.setMessage(getResources().getString(
360 R
.string
.wait_a_moment
));
361 working_dialog
.setIndeterminate(true
);
362 working_dialog
.setCancelable(false
);
363 dialog
= working_dialog
;
377 public void onFileStateChanged() {
378 // nothing to do here!
386 public FileDownloaderBinder
getFileDownloaderBinder() {
387 return mDownloaderBinder
;
392 public FileUploaderBinder
getFileUploaderBinder() {
393 return mUploaderBinder
;
398 public void showFragmentWithDetails(OCFile file
) {
399 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
400 transaction
.replace(R
.id
.fragment
, new FileDetailFragment(file
, getAccount()), FileDetailFragment
.FTAG
);
401 transaction
.commit();
405 private void requestForDownload() {
406 if (!mDownloaderBinder
.isDownloading(getAccount(), getFile())) {
407 Intent i
= new Intent(this, FileDownloader
.class);
408 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, getAccount());
409 i
.putExtra(FileDownloader
.EXTRA_FILE
, getFile());
416 * Class waiting for broadcast events from the {@link FielDownloader} service.
418 * Updates the UI when a download is started or finished, provided that it is relevant for the
421 private class DownloadFinishReceiver
extends BroadcastReceiver
{
423 public void onReceive(Context context
, Intent intent
) {
424 boolean sameAccount
= isSameAccount(context
, intent
);
425 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
426 boolean samePath
= (getFile() != null
&& getFile().getRemotePath().equals(downloadedRemotePath
));
428 if (sameAccount
&& samePath
) {
429 updateChildFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
432 removeStickyBroadcast(intent
);
435 private boolean isSameAccount(Context context
, Intent intent
) {
436 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
437 return (accountName
!= null
&& accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
));
442 public void updateChildFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
443 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
444 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
445 FileDetailFragment detailsFragment
= (FileDetailFragment
) fragment
;
446 OCFile fileInFragment
= detailsFragment
.getFile();
447 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
448 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
449 mWaitingToPreview
= false
;
451 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
)) {
452 // grants that the progress bar is updated
453 detailsFragment
.listenForTransferProgress();
454 detailsFragment
.updateFileDetails(true
, false
);
456 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
457 // refresh the details fragment
458 if (success
&& mWaitingToPreview
) {
459 setFile(mStorageManager
.getFileById(getFile().getFileId())); // update the file from database, for the local storage path
460 if (PreviewMediaFragment
.canBePreviewed(getFile())) {
461 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
462 transaction
.replace(R
.id
.fragment
, new PreviewMediaFragment(getFile(), getAccount(), 0, true
), FileDetailFragment
.FTAG
);
463 transaction
.commit();
465 detailsFragment
.updateFileDetails(false
, (success
));
468 mWaitingToPreview
= false
;
471 detailsFragment
.updateFileDetails(false
, (success
));
474 } // TODO else if (fragment != null && fragment )
482 protected void onAccountChanged() {
483 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
485 FileFragment fragment
= (FileFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
486 if (fragment
!= null
&& mStorageManager
.getFileById(fragment
.getFile().getFileId()) == null
) {
487 /// the account was forced to be changed; probably was deleted from system settings
488 backToDisplayActivity(false
);