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
.app
.SherlockFragmentActivity
;
37 import com
.actionbarsherlock
.view
.MenuItem
;
38 import com
.owncloud
.android
.AccountUtils
;
39 import com
.owncloud
.android
.Log_OC
;
40 import com
.owncloud
.android
.R
;
41 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
42 import com
.owncloud
.android
.datamodel
.OCFile
;
43 import com
.owncloud
.android
.files
.services
.FileDownloader
;
44 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
45 import com
.owncloud
.android
.files
.services
.FileUploader
;
46 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
47 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
48 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
49 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
50 import com
.owncloud
.android
.ui
.preview
.PreviewVideoActivity
;
53 * This activity displays the details of a file like its name, its size and so
56 * @author Bartek Przybylski
57 * @author David A. Velasco
59 public class FileDetailActivity
extends SherlockFragmentActivity
implements FileFragment
.ContainerActivity
{
61 public static final int DIALOG_SHORT_WAIT
= 0;
63 public static final String TAG
= FileDetailActivity
.class.getSimpleName();
65 public static final String EXTRA_MODE
= "MODE";
66 public static final int MODE_DETAILS
= 0;
67 public static final int MODE_PREVIEW
= 1;
69 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
71 private FileDownloaderBinder mDownloaderBinder
= null
;
72 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
73 private FileUploaderBinder mUploaderBinder
= null
;
74 private boolean mWaitingToPreview
;
77 private Account mAccount
;
79 private FileDataStorageManager mStorageManager
;
80 private DownloadFinishReceiver mDownloadFinishReceiver
;
82 private Configuration mNewConfigurationChangeToApplyOnStart
;
84 private boolean mStarted
;
88 protected void onCreate(Bundle savedInstanceState
) {
89 super.onCreate(savedInstanceState
);
92 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
93 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
94 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
96 setContentView(R
.layout
.file_activity_details
);
98 ActionBar actionBar
= getSupportActionBar();
99 actionBar
.setDisplayHomeAsUpEnabled(true
);
101 if (savedInstanceState
== null
) {
102 mWaitingToPreview
= false
;
103 createChildFragment();
105 mWaitingToPreview
= savedInstanceState
.getBoolean(KEY_WAITING_TO_PREVIEW
);
108 mDownloadConnection
= new DetailsServiceConnection();
109 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
110 mUploadConnection
= new DetailsServiceConnection();
111 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
115 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
116 * the requested {@link Intent}.
118 private void createChildFragment() {
119 int mode
= getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
);
121 Fragment newFragment
= null
;
122 if (PreviewMediaFragment
.canBePreviewed(mFile
) && mode
== MODE_PREVIEW
) {
123 if (mFile
.isDown()) {
124 int startPlaybackPosition
= getIntent().getIntExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, 0);
125 boolean autoplay
= getIntent().getBooleanExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, true
);
126 newFragment
= new PreviewMediaFragment(mFile
, mAccount
, startPlaybackPosition
, autoplay
);
129 newFragment
= new FileDetailFragment(mFile
, mAccount
);
130 mWaitingToPreview
= true
;
134 newFragment
= new FileDetailFragment(mFile
, mAccount
);
136 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
137 ft
.replace(R
.id
.fragment
, newFragment
, FileDetailFragment
.FTAG
);
142 public void onActivityResult (int requestCode
, int resultCode
, Intent data
) {
143 Log_OC
.e(TAG
, "onActivityResult");
144 super.onActivityResult(requestCode
, resultCode
, data
);
148 public void onConfigurationChanged (Configuration newConfig
) {
149 super.onConfigurationChanged(newConfig
);
151 checkConfigurationChange(newConfig
);
153 mNewConfigurationChangeToApplyOnStart
= newConfig
;
159 protected void onSaveInstanceState(Bundle outState
) {
160 super.onSaveInstanceState(outState
);
161 outState
.putBoolean(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
166 public void onStart() {
168 Log_OC
.e(TAG
, "onStart");
169 if (mNewConfigurationChangeToApplyOnStart
!= null
) {
170 checkConfigurationChange(mNewConfigurationChangeToApplyOnStart
);
171 mNewConfigurationChangeToApplyOnStart
= null
;
176 private void checkConfigurationChange(Configuration newConfig
) {
178 Intent intent
= null
;
179 if ((newConfig
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
180 && newConfig
.orientation
== Configuration
.ORIENTATION_LANDSCAPE
) {
182 intent
= new Intent(this, FileDisplayActivity
.class);
183 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, mFile
);
184 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
185 intent
.putExtra(EXTRA_MODE
, getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
));
186 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
187 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
188 if (fragment
!= null
&& mFile
!= null
&& fragment
instanceof PreviewMediaFragment
&& mFile
.isVideo()) {
189 PreviewMediaFragment videoFragment
= (PreviewMediaFragment
)fragment
;
190 intent
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, videoFragment
.getPosition());
191 intent
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, videoFragment
.isPlaying());
195 intent
= new Intent(this, FileDetailActivity
.class);
196 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, mFile
);
197 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
198 intent
.putExtra(EXTRA_MODE
, getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
));
199 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
200 if (fragment
!= null
&& mFile
!= null
&& fragment
instanceof PreviewMediaFragment
&& mFile
.isVideo()) {
201 PreviewMediaFragment videoFragment
= (PreviewMediaFragment
)fragment
;
202 intent
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, videoFragment
.getPosition());
203 intent
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, videoFragment
.isPlaying());
205 // and maybe 'waiting to preview' flag
207 startActivity(intent
);
211 public void onStop() {
213 Log_OC
.e(TAG
, "onStop");
217 public void onPause() {
219 Log_OC
.e(TAG
, "onPause");
220 if (mDownloadFinishReceiver
!= null
) {
221 unregisterReceiver(mDownloadFinishReceiver
);
222 mDownloadFinishReceiver
= null
;
228 public void onResume() {
230 Log_OC
.e(TAG
, "onResume");
231 // TODO this is probably unnecessary
232 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
233 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
234 ((FileDetailFragment
) fragment
).updateFileDetails(false
, false
);
237 // Listen for download messages
238 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
239 downloadIntentFilter
.addAction(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
240 mDownloadFinishReceiver
= new DownloadFinishReceiver();
241 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
245 /** Defines callbacks for service binding, passed to bindService() */
246 private class DetailsServiceConnection
implements ServiceConnection
{
249 public void onServiceConnected(ComponentName component
, IBinder service
) {
251 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
252 Log_OC
.d(TAG
, "Download service connected");
253 mDownloaderBinder
= (FileDownloaderBinder
) service
;
254 if (mWaitingToPreview
) {
255 requestForDownload();
258 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
259 Log_OC
.d(TAG
, "Upload service connected");
260 mUploaderBinder
= (FileUploaderBinder
) service
;
265 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
266 FileDetailFragment detailsFragment
= (fragment
instanceof FileDetailFragment
) ?
(FileDetailFragment
) fragment
: null
;
267 if (detailsFragment
!= null
) {
268 detailsFragment
.listenForTransferProgress();
269 detailsFragment
.updateFileDetails(mWaitingToPreview
, false
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
274 public void onServiceDisconnected(ComponentName component
) {
275 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
276 Log_OC
.d(TAG
, "Download service disconnected");
277 mDownloaderBinder
= null
;
278 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
279 Log_OC
.d(TAG
, "Upload service disconnected");
280 mUploaderBinder
= null
;
287 public void onDestroy() {
289 Log_OC
.e(TAG
, "onDestroy");
290 if (mDownloadConnection
!= null
) {
291 unbindService(mDownloadConnection
);
292 mDownloadConnection
= null
;
294 if (mUploadConnection
!= null
) {
295 unbindService(mUploadConnection
);
296 mUploadConnection
= null
;
302 public boolean onOptionsItemSelected(MenuItem item
) {
303 boolean returnValue
= false
;
305 switch(item
.getItemId()){
306 case android
.R
.id
.home
:
307 //backToDisplayActivity();
312 returnValue
= super.onOptionsItemSelected(item
);
318 //private void backToDisplayActivity() {
320 public void onBackPressed() {
321 Intent intent
= new Intent(this, FileDisplayActivity
.class);
322 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
323 OCFile targetFile
= null
;
325 targetFile
= mStorageManager
.getFileById(mFile
.getParentId());
327 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, targetFile
);
328 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
329 startActivity(intent
);
334 protected Dialog
onCreateDialog(int id
) {
335 Dialog dialog
= null
;
337 case DIALOG_SHORT_WAIT
: {
338 ProgressDialog working_dialog
= new ProgressDialog(this);
339 working_dialog
.setMessage(getResources().getString(
340 R
.string
.wait_a_moment
));
341 working_dialog
.setIndeterminate(true
);
342 working_dialog
.setCancelable(false
);
343 dialog
= working_dialog
;
357 public void onFileStateChanged() {
358 // nothing to do here!
366 public FileDownloaderBinder
getFileDownloaderBinder() {
367 return mDownloaderBinder
;
372 public FileUploaderBinder
getFileUploaderBinder() {
373 return mUploaderBinder
;
378 public void showFragmentWithDetails(OCFile file
) {
379 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
380 transaction
.replace(R
.id
.fragment
, new FileDetailFragment(file
, mAccount
), FileDetailFragment
.FTAG
);
381 transaction
.commit();
385 private void requestForDownload() {
386 if (!mDownloaderBinder
.isDownloading(mAccount
, mFile
)) {
387 Intent i
= new Intent(this, FileDownloader
.class);
388 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
389 i
.putExtra(FileDownloader
.EXTRA_FILE
, mFile
);
396 * Class waiting for broadcast events from the {@link FielDownloader} service.
398 * Updates the UI when a download is started or finished, provided that it is relevant for the
401 private class DownloadFinishReceiver
extends BroadcastReceiver
{
403 public void onReceive(Context context
, Intent intent
) {
404 boolean sameAccount
= isSameAccount(context
, intent
);
405 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
406 boolean samePath
= (mFile
!= null
&& mFile
.getRemotePath().equals(downloadedRemotePath
));
408 if (sameAccount
&& samePath
) {
409 updateChildFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
412 removeStickyBroadcast(intent
);
415 private boolean isSameAccount(Context context
, Intent intent
) {
416 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
417 return (accountName
!= null
&& accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
));
422 public void updateChildFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
423 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
424 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
425 FileDetailFragment detailsFragment
= (FileDetailFragment
) fragment
;
426 OCFile fileInFragment
= detailsFragment
.getFile();
427 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
428 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
429 mWaitingToPreview
= false
;
431 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
)) {
432 // grants that the progress bar is updated
433 detailsFragment
.listenForTransferProgress();
434 detailsFragment
.updateFileDetails(true
, false
);
436 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
437 // refresh the details fragment
438 if (success
&& mWaitingToPreview
) {
439 mFile
= mStorageManager
.getFileById(mFile
.getFileId()); // update the file from database, for the local storage path
440 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
441 transaction
.replace(R
.id
.fragment
, new PreviewMediaFragment(mFile
, mAccount
, 0, true
), FileDetailFragment
.FTAG
);
442 transaction
.commit();
443 mWaitingToPreview
= false
;
446 detailsFragment
.updateFileDetails(false
, (success
));
447 // TODO error message if !success ¿?
450 } // TODO else if (fragment != null && fragment )