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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.activity
;
21 import android
.accounts
.Account
;
22 import android
.app
.Dialog
;
23 import android
.app
.ProgressDialog
;
24 import android
.content
.BroadcastReceiver
;
25 import android
.content
.ComponentName
;
26 import android
.content
.Context
;
27 import android
.content
.Intent
;
28 import android
.content
.IntentFilter
;
29 import android
.content
.ServiceConnection
;
30 import android
.content
.res
.Configuration
;
31 import android
.os
.Bundle
;
32 import android
.os
.IBinder
;
33 import android
.support
.v4
.app
.Fragment
;
34 import android
.support
.v4
.app
.FragmentTransaction
;
35 import android
.util
.Log
;
37 import com
.actionbarsherlock
.app
.ActionBar
;
38 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
39 import com
.actionbarsherlock
.view
.MenuItem
;
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
.AccountUtils
;
50 import com
.owncloud
.android
.Log_OC
;
52 import com
.owncloud
.android
.R
;
55 * This activity displays the details of a file like its name, its size and so
58 * @author Bartek Przybylski
59 * @author David A. Velasco
61 public class FileDetailActivity
extends SherlockFragmentActivity
implements FileFragment
.ContainerActivity
{
63 public static final int DIALOG_SHORT_WAIT
= 0;
65 public static final String TAG
= FileDetailActivity
.class.getSimpleName();
67 public static final String EXTRA_MODE
= "MODE";
68 public static final int MODE_DETAILS
= 0;
69 public static final int MODE_PREVIEW
= 1;
71 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
73 private boolean mConfigurationChangedToLandscape
= false
;
74 private FileDownloaderBinder mDownloaderBinder
= null
;
75 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
76 private FileUploaderBinder mUploaderBinder
= null
;
77 private boolean mWaitingToPreview
;
80 private Account mAccount
;
82 private FileDataStorageManager mStorageManager
;
83 private DownloadFinishReceiver mDownloadFinishReceiver
;
87 protected void onCreate(Bundle savedInstanceState
) {
88 super.onCreate(savedInstanceState
);
90 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
91 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
92 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
94 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
95 Configuration conf
= getResources().getConfiguration();
96 mConfigurationChangedToLandscape
= (conf
.orientation
== Configuration
.ORIENTATION_LANDSCAPE
&&
97 (conf
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
100 if (!mConfigurationChangedToLandscape
) {
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
);
120 backToDisplayActivity(false
); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
126 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
127 * the requested {@link Intent}.
129 private void createChildFragment() {
130 int mode
= getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
);
132 Fragment newFragment
= null
;
133 if (PreviewMediaFragment
.canBePreviewed(mFile
) && mode
== MODE_PREVIEW
) {
134 if (mFile
.isDown()) {
135 newFragment
= new PreviewMediaFragment(mFile
, mAccount
);
138 newFragment
= new FileDetailFragment(mFile
, mAccount
);
139 mWaitingToPreview
= true
;
143 newFragment
= new FileDetailFragment(mFile
, mAccount
);
145 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
146 ft
.replace(R
.id
.fragment
, newFragment
, FileDetailFragment
.FTAG
);
152 protected void onSaveInstanceState(Bundle outState
) {
153 super.onSaveInstanceState(outState
);
154 outState
.putBoolean(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
159 public void onPause() {
161 if (mDownloadFinishReceiver
!= null
) {
162 unregisterReceiver(mDownloadFinishReceiver
);
163 mDownloadFinishReceiver
= null
;
169 public void onResume() {
171 if (!mConfigurationChangedToLandscape
) {
172 // TODO this is probably unnecessary
173 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
174 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
175 ((FileDetailFragment
) fragment
).updateFileDetails(false
, false
);
178 // Listen for download messages
179 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
180 downloadIntentFilter
.addAction(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
181 mDownloadFinishReceiver
= new DownloadFinishReceiver();
182 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
186 /** Defines callbacks for service binding, passed to bindService() */
187 private class DetailsServiceConnection
implements ServiceConnection
{
190 public void onServiceConnected(ComponentName component
, IBinder service
) {
192 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
193 Log_OC
.d(TAG
, "Download service connected");
194 mDownloaderBinder
= (FileDownloaderBinder
) service
;
195 if (mWaitingToPreview
) {
196 requestForDownload();
199 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
200 Log_OC
.d(TAG
, "Upload service connected");
201 mUploaderBinder
= (FileUploaderBinder
) service
;
206 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
207 FileDetailFragment detailsFragment
= (fragment
instanceof FileDetailFragment
) ?
(FileDetailFragment
) fragment
: null
;
208 if (detailsFragment
!= null
) {
209 detailsFragment
.listenForTransferProgress();
210 detailsFragment
.updateFileDetails(mWaitingToPreview
, false
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
215 public void onServiceDisconnected(ComponentName component
) {
216 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
217 Log_OC
.d(TAG
, "Download service disconnected");
218 mDownloaderBinder
= null
;
219 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
220 Log_OC
.d(TAG
, "Upload service disconnected");
221 mUploaderBinder
= null
;
228 public void onDestroy() {
230 if (mDownloadConnection
!= null
) {
231 unbindService(mDownloadConnection
);
232 mDownloadConnection
= null
;
234 if (mUploadConnection
!= null
) {
235 unbindService(mUploadConnection
);
236 mUploadConnection
= null
;
242 public boolean onOptionsItemSelected(MenuItem item
) {
243 boolean returnValue
= false
;
245 switch(item
.getItemId()){
246 case android
.R
.id
.home
:
247 backToDisplayActivity(true
);
251 returnValue
= super.onOptionsItemSelected(item
);
257 private void backToDisplayActivity(boolean moveToParent
) {
258 Intent intent
= new Intent(this, FileDisplayActivity
.class);
259 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
260 OCFile targetFile
= null
;
262 targetFile
= moveToParent ? mStorageManager
.getFileById(mFile
.getParentId()) : mFile
;
264 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, targetFile
);
265 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
266 startActivity(intent
);
272 protected Dialog
onCreateDialog(int id
) {
273 Dialog dialog
= null
;
275 case DIALOG_SHORT_WAIT
: {
276 ProgressDialog working_dialog
= new ProgressDialog(this);
277 working_dialog
.setMessage(getResources().getString(
278 R
.string
.wait_a_moment
));
279 working_dialog
.setIndeterminate(true
);
280 working_dialog
.setCancelable(false
);
281 dialog
= working_dialog
;
295 public void onFileStateChanged() {
296 // nothing to do here!
304 public FileDownloaderBinder
getFileDownloaderBinder() {
305 return mDownloaderBinder
;
310 public FileUploaderBinder
getFileUploaderBinder() {
311 return mUploaderBinder
;
316 public void showFragmentWithDetails(OCFile file
) {
317 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
318 transaction
.replace(R
.id
.fragment
, new FileDetailFragment(file
, mAccount
), FileDetailFragment
.FTAG
);
319 transaction
.commit();
323 private void requestForDownload() {
324 if (!mDownloaderBinder
.isDownloading(mAccount
, mFile
)) {
325 Intent i
= new Intent(this, FileDownloader
.class);
326 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
327 i
.putExtra(FileDownloader
.EXTRA_FILE
, mFile
);
334 * Class waiting for broadcast events from the {@link FielDownloader} service.
336 * Updates the UI when a download is started or finished, provided that it is relevant for the
339 private class DownloadFinishReceiver
extends BroadcastReceiver
{
341 public void onReceive(Context context
, Intent intent
) {
342 boolean sameAccount
= isSameAccount(context
, intent
);
343 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
344 boolean samePath
= (mFile
!= null
&& mFile
.getRemotePath().equals(downloadedRemotePath
));
346 if (sameAccount
&& samePath
) {
347 updateChildFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
350 removeStickyBroadcast(intent
);
353 private boolean isSameAccount(Context context
, Intent intent
) {
354 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
355 return (accountName
!= null
&& accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
));
360 public void updateChildFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
361 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
362 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
363 FileDetailFragment detailsFragment
= (FileDetailFragment
) fragment
;
364 OCFile fileInFragment
= detailsFragment
.getFile();
365 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
366 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
367 mWaitingToPreview
= false
;
369 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
)) {
370 // grants that the progress bar is updated
371 detailsFragment
.listenForTransferProgress();
372 detailsFragment
.updateFileDetails(true
, false
);
374 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
375 // refresh the details fragment
376 if (success
&& mWaitingToPreview
) {
377 mFile
= mStorageManager
.getFileById(mFile
.getFileId()); // update the file from database, for the local storage path
378 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
379 transaction
.replace(R
.id
.fragment
, new PreviewMediaFragment(mFile
, mAccount
), FileDetailFragment
.FTAG
);
380 transaction
.commit();
381 mWaitingToPreview
= false
;
384 detailsFragment
.updateFileDetails(false
, (success
));
385 // TODO error message if !success ¿?
388 } // TODO else if (fragment != null && fragment )