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 (PreviewMediaFragment
.canBePreviewed(file
) && mode
== MODE_PREVIEW
) {
132 int startPlaybackPosition
= getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
133 boolean autoplay
= getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
134 newFragment
= new PreviewMediaFragment(file
, account
, startPlaybackPosition
, autoplay
);
137 newFragment
= new FileDetailFragment(file
, account
);
138 mWaitingToPreview
= true
;
142 newFragment
= new FileDetailFragment(file
, account
);
144 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
145 ft
.replace(R
.id
.fragment
, newFragment
, FileDetailFragment
.FTAG
);
150 public void onActivityResult (int requestCode
, int resultCode
, Intent data
) {
151 Log_OC
.e(TAG
, "onActivityResult");
152 super.onActivityResult(requestCode
, resultCode
, data
);
156 public void onConfigurationChanged (Configuration newConfig
) {
157 super.onConfigurationChanged(newConfig
);
159 checkConfigurationChange(newConfig
);
161 mNewConfigurationChangeToApplyOnStart
= newConfig
;
167 protected void onSaveInstanceState(Bundle outState
) {
168 super.onSaveInstanceState(outState
);
169 outState
.putBoolean(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
174 public void onStart() {
176 Log_OC
.e(TAG
, "onStart");
177 if (mNewConfigurationChangeToApplyOnStart
!= null
&& !isRedirectingToSetupAccount()) {
178 checkConfigurationChange(mNewConfigurationChangeToApplyOnStart
);
179 mNewConfigurationChangeToApplyOnStart
= null
;
184 private void checkConfigurationChange(Configuration newConfig
) {
186 Intent intent
= null
;
187 OCFile file
= getFile();
188 Account account
= getAccount();
189 if ((newConfig
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
190 && newConfig
.orientation
== Configuration
.ORIENTATION_LANDSCAPE
) {
192 intent
= new Intent(this, FileDisplayActivity
.class);
193 intent
.putExtra(EXTRA_FILE
, file
);
194 intent
.putExtra(EXTRA_ACCOUNT
, account
);
195 intent
.putExtra(EXTRA_MODE
, getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
));
196 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
197 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
198 if (fragment
!= null
&& file
!= null
&& fragment
instanceof PreviewMediaFragment
&& file
.isVideo()) {
199 PreviewMediaFragment videoFragment
= (PreviewMediaFragment
)fragment
;
200 intent
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, videoFragment
.getPosition());
201 intent
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, videoFragment
.isPlaying());
205 intent
= new Intent(this, FileDetailActivity
.class);
206 intent
.putExtra(EXTRA_FILE
, file
);
207 intent
.putExtra(EXTRA_ACCOUNT
, account
);
208 intent
.putExtra(EXTRA_MODE
, getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
));
209 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
210 if (fragment
!= null
&& file
!= null
&& fragment
instanceof PreviewMediaFragment
&& file
.isVideo()) {
211 PreviewMediaFragment videoFragment
= (PreviewMediaFragment
)fragment
;
212 intent
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, videoFragment
.getPosition());
213 intent
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, videoFragment
.isPlaying());
215 // and maybe 'waiting to preview' flag
217 startActivity(intent
);
221 public void onStop() {
223 Log_OC
.e(TAG
, "onStop");
227 public void onPause() {
229 Log_OC
.e(TAG
, "onPause");
230 if (mDownloadFinishReceiver
!= null
) {
231 unregisterReceiver(mDownloadFinishReceiver
);
232 mDownloadFinishReceiver
= null
;
238 public void onResume() {
240 Log_OC
.e(TAG
, "onResume");
241 // TODO this is probably unnecessary
242 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
243 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
244 ((FileDetailFragment
) fragment
).updateFileDetails(false
, false
);
247 // Listen for download messages
248 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
249 downloadIntentFilter
.addAction(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
250 mDownloadFinishReceiver
= new DownloadFinishReceiver();
251 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
255 /** Defines callbacks for service binding, passed to bindService() */
256 private class DetailsServiceConnection
implements ServiceConnection
{
259 public void onServiceConnected(ComponentName component
, IBinder service
) {
261 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
262 Log_OC
.d(TAG
, "Download service connected");
263 mDownloaderBinder
= (FileDownloaderBinder
) service
;
264 if (mWaitingToPreview
) {
265 requestForDownload();
268 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
269 Log_OC
.d(TAG
, "Upload service connected");
270 mUploaderBinder
= (FileUploaderBinder
) service
;
275 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
276 FileDetailFragment detailsFragment
= (fragment
instanceof FileDetailFragment
) ?
(FileDetailFragment
) fragment
: null
;
277 if (detailsFragment
!= null
) {
278 detailsFragment
.listenForTransferProgress();
279 detailsFragment
.updateFileDetails(mWaitingToPreview
, false
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
284 public void onServiceDisconnected(ComponentName component
) {
285 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
286 Log_OC
.d(TAG
, "Download service disconnected");
287 mDownloaderBinder
= null
;
288 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
289 Log_OC
.d(TAG
, "Upload service disconnected");
290 mUploaderBinder
= null
;
297 public void onDestroy() {
299 Log_OC
.e(TAG
, "onDestroy");
300 if (mDownloadConnection
!= null
) {
301 unbindService(mDownloadConnection
);
302 mDownloadConnection
= null
;
304 if (mUploadConnection
!= null
) {
305 unbindService(mUploadConnection
);
306 mUploadConnection
= null
;
312 public boolean onOptionsItemSelected(MenuItem item
) {
313 boolean returnValue
= false
;
315 switch(item
.getItemId()){
316 case android
.R
.id
.home
:
317 backToDisplayActivity(true
);
321 returnValue
= super.onOptionsItemSelected(item
);
328 public void onBackPressed() {
329 backToDisplayActivity(true
);
332 private void backToDisplayActivity(boolean moveToParent
) {
333 Intent intent
= new Intent(this, FileDisplayActivity
.class);
334 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
335 OCFile targetFile
= null
;
336 OCFile file
= getFile();
338 targetFile
= moveToParent ? mStorageManager
.getFileById(file
.getParentId()) : file
;
340 intent
.putExtra(EXTRA_FILE
, targetFile
);
341 intent
.putExtra(EXTRA_ACCOUNT
, getAccount());
342 startActivity(intent
);
347 protected Dialog
onCreateDialog(int id
) {
348 Dialog dialog
= null
;
350 case DIALOG_SHORT_WAIT
: {
351 ProgressDialog working_dialog
= new ProgressDialog(this);
352 working_dialog
.setMessage(getResources().getString(
353 R
.string
.wait_a_moment
));
354 working_dialog
.setIndeterminate(true
);
355 working_dialog
.setCancelable(false
);
356 dialog
= working_dialog
;
370 public void onFileStateChanged() {
371 // nothing to do here!
379 public FileDownloaderBinder
getFileDownloaderBinder() {
380 return mDownloaderBinder
;
385 public FileUploaderBinder
getFileUploaderBinder() {
386 return mUploaderBinder
;
391 public void showFragmentWithDetails(OCFile file
) {
392 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
393 transaction
.replace(R
.id
.fragment
, new FileDetailFragment(file
, getAccount()), FileDetailFragment
.FTAG
);
394 transaction
.commit();
398 private void requestForDownload() {
399 if (!mDownloaderBinder
.isDownloading(getAccount(), getFile())) {
400 Intent i
= new Intent(this, FileDownloader
.class);
401 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, getAccount());
402 i
.putExtra(FileDownloader
.EXTRA_FILE
, getFile());
409 * Class waiting for broadcast events from the {@link FielDownloader} service.
411 * Updates the UI when a download is started or finished, provided that it is relevant for the
414 private class DownloadFinishReceiver
extends BroadcastReceiver
{
416 public void onReceive(Context context
, Intent intent
) {
417 boolean sameAccount
= isSameAccount(context
, intent
);
418 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
419 boolean samePath
= (getFile() != null
&& getFile().getRemotePath().equals(downloadedRemotePath
));
421 if (sameAccount
&& samePath
) {
422 updateChildFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
425 removeStickyBroadcast(intent
);
428 private boolean isSameAccount(Context context
, Intent intent
) {
429 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
430 return (accountName
!= null
&& accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
));
435 public void updateChildFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
436 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
437 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
438 FileDetailFragment detailsFragment
= (FileDetailFragment
) fragment
;
439 OCFile fileInFragment
= detailsFragment
.getFile();
440 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
441 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
442 mWaitingToPreview
= false
;
444 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
)) {
445 // grants that the progress bar is updated
446 detailsFragment
.listenForTransferProgress();
447 detailsFragment
.updateFileDetails(true
, false
);
449 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
450 // refresh the details fragment
451 if (success
&& mWaitingToPreview
) {
452 setFile(mStorageManager
.getFileById(getFile().getFileId())); // update the file from database, for the local storage path
453 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
454 transaction
.replace(R
.id
.fragment
, new PreviewMediaFragment(getFile(), getAccount(), 0, true
), FileDetailFragment
.FTAG
);
455 transaction
.commit();
456 mWaitingToPreview
= false
;
459 detailsFragment
.updateFileDetails(false
, (success
));
460 // TODO error message if !success ¿?
463 } // TODO else if (fragment != null && fragment )
471 protected void onAccountChanged() {
472 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
474 FileFragment fragment
= (FileFragment
) getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
475 if (fragment
!= null
&& mStorageManager
.getFileById(fragment
.getFile().getFileId()) == null
) {
476 /// the account was forced to be changed; probably was deleted from system settings
477 backToDisplayActivity(false
);