1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
;
34 import android
.util
.Log
;
36 import com
.actionbarsherlock
.app
.ActionBar
;
37 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
38 import com
.actionbarsherlock
.view
.MenuItem
;
39 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
40 import com
.owncloud
.android
.datamodel
.OCFile
;
41 import com
.owncloud
.android
.files
.services
.FileDownloader
;
42 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
43 import com
.owncloud
.android
.files
.services
.FileUploader
;
44 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
45 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
46 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
47 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
49 import com
.owncloud
.android
.AccountUtils
;
50 import com
.owncloud
.android
.R
;
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 boolean mConfigurationChangedToLandscape
= false
;
72 private FileDownloaderBinder mDownloaderBinder
= null
;
73 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
74 private FileUploaderBinder mUploaderBinder
= null
;
75 private boolean mWaitingToPreview
;
78 private Account mAccount
;
80 private FileDataStorageManager mStorageManager
;
81 private DownloadFinishReceiver mDownloadFinishReceiver
;
85 protected void onCreate(Bundle savedInstanceState
) {
86 super.onCreate(savedInstanceState
);
88 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
89 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
90 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
92 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
93 Configuration conf
= getResources().getConfiguration();
94 mConfigurationChangedToLandscape
= (conf
.orientation
== Configuration
.ORIENTATION_LANDSCAPE
&&
95 (conf
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
98 if (!mConfigurationChangedToLandscape
) {
99 setContentView(R
.layout
.file_activity_details
);
101 ActionBar actionBar
= getSupportActionBar();
102 actionBar
.setDisplayHomeAsUpEnabled(true
);
104 if (savedInstanceState
== null
) {
105 mWaitingToPreview
= false
;
106 createChildFragment();
108 mWaitingToPreview
= savedInstanceState
.getBoolean(KEY_WAITING_TO_PREVIEW
);
111 mDownloadConnection
= new DetailsServiceConnection();
112 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
113 mUploadConnection
= new DetailsServiceConnection();
114 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
118 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
125 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
126 * the requested {@link Intent}.
128 private void createChildFragment() {
129 int mode
= getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
);
131 Fragment newFragment
= null
;
132 if (PreviewMediaFragment
.canBePreviewed(mFile
) && mode
== MODE_PREVIEW
) {
133 if (mFile
.isDown()) {
134 newFragment
= new PreviewMediaFragment(mFile
, mAccount
);
137 newFragment
= new FileDetailFragment(mFile
, mAccount
);
138 mWaitingToPreview
= true
;
142 newFragment
= new FileDetailFragment(mFile
, mAccount
);
144 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
145 ft
.replace(R
.id
.fragment
, newFragment
, FileDetailFragment
.FTAG
);
151 protected void onSaveInstanceState(Bundle outState
) {
152 super.onSaveInstanceState(outState
);
153 outState
.putBoolean(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
158 public void onPause() {
160 if (mDownloadFinishReceiver
!= null
) {
161 unregisterReceiver(mDownloadFinishReceiver
);
162 mDownloadFinishReceiver
= null
;
168 public void onResume() {
170 if (!mConfigurationChangedToLandscape
) {
171 // TODO this is probably unnecessary
172 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
173 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
174 ((FileDetailFragment
) fragment
).updateFileDetails(false
, false
);
177 // Listen for download messages
178 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
179 downloadIntentFilter
.addAction(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
180 mDownloadFinishReceiver
= new DownloadFinishReceiver();
181 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
185 /** Defines callbacks for service binding, passed to bindService() */
186 private class DetailsServiceConnection
implements ServiceConnection
{
189 public void onServiceConnected(ComponentName component
, IBinder service
) {
191 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
192 Log
.d(TAG
, "Download service connected");
193 mDownloaderBinder
= (FileDownloaderBinder
) service
;
194 if (mWaitingToPreview
) {
195 requestForDownload();
198 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
199 Log
.d(TAG
, "Upload service connected");
200 mUploaderBinder
= (FileUploaderBinder
) service
;
205 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
206 FileDetailFragment detailsFragment
= (fragment
instanceof FileDetailFragment
) ?
(FileDetailFragment
) fragment
: null
;
207 if (detailsFragment
!= null
) {
208 detailsFragment
.listenForTransferProgress();
209 detailsFragment
.updateFileDetails(mWaitingToPreview
, false
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
214 public void onServiceDisconnected(ComponentName component
) {
215 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
216 Log
.d(TAG
, "Download service disconnected");
217 mDownloaderBinder
= null
;
218 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
219 Log
.d(TAG
, "Upload service disconnected");
220 mUploaderBinder
= null
;
227 public void onDestroy() {
229 if (mDownloadConnection
!= null
) {
230 unbindService(mDownloadConnection
);
231 mDownloadConnection
= null
;
233 if (mUploadConnection
!= null
) {
234 unbindService(mUploadConnection
);
235 mUploadConnection
= null
;
241 public boolean onOptionsItemSelected(MenuItem item
) {
242 boolean returnValue
= false
;
244 switch(item
.getItemId()){
245 case android
.R
.id
.home
:
246 backToDisplayActivity();
250 returnValue
= super.onOptionsItemSelected(item
);
258 private void backToDisplayActivity() {
259 Intent intent
= new Intent(this, FileDisplayActivity
.class);
260 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
261 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, mFile
);
262 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
263 startActivity(intent
);
269 protected Dialog
onCreateDialog(int id
) {
270 Dialog dialog
= null
;
272 case DIALOG_SHORT_WAIT
: {
273 ProgressDialog working_dialog
= new ProgressDialog(this);
274 working_dialog
.setMessage(getResources().getString(
275 R
.string
.wait_a_moment
));
276 working_dialog
.setIndeterminate(true
);
277 working_dialog
.setCancelable(false
);
278 dialog
= working_dialog
;
292 public void onFileStateChanged() {
293 // nothing to do here!
301 public FileDownloaderBinder
getFileDownloaderBinder() {
302 return mDownloaderBinder
;
307 public FileUploaderBinder
getFileUploaderBinder() {
308 return mUploaderBinder
;
313 public void showFragmentWithDetails(OCFile file
) {
314 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
315 transaction
.replace(R
.id
.fragment
, new FileDetailFragment(file
, mAccount
), FileDetailFragment
.FTAG
);
316 transaction
.commit();
320 private void requestForDownload() {
321 if (!mDownloaderBinder
.isDownloading(mAccount
, mFile
)) {
322 Intent i
= new Intent(this, FileDownloader
.class);
323 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
324 i
.putExtra(FileDownloader
.EXTRA_FILE
, mFile
);
331 * Class waiting for broadcast events from the {@link FielDownloader} service.
333 * Updates the UI when a download is started or finished, provided that it is relevant for the
336 private class DownloadFinishReceiver
extends BroadcastReceiver
{
338 public void onReceive(Context context
, Intent intent
) {
339 boolean sameAccount
= isSameAccount(context
, intent
);
340 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
341 boolean samePath
= (mFile
!= null
&& mFile
.getRemotePath().equals(downloadedRemotePath
));
343 if (sameAccount
&& samePath
) {
344 updateChildFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
347 removeStickyBroadcast(intent
);
350 private boolean isSameAccount(Context context
, Intent intent
) {
351 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
352 return (accountName
!= null
&& accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
));
357 public void updateChildFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
358 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
359 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
360 FileDetailFragment detailsFragment
= (FileDetailFragment
) fragment
;
361 OCFile fileInFragment
= detailsFragment
.getFile();
362 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
363 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
364 mWaitingToPreview
= false
;
366 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
)) {
367 // grants that the progress bar is updated
368 detailsFragment
.listenForTransferProgress();
369 detailsFragment
.updateFileDetails(true
, false
);
371 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
372 // refresh the details fragment
373 if (success
&& mWaitingToPreview
) {
374 mFile
= mStorageManager
.getFileById(mFile
.getFileId()); // update the file from database, for the local storage path
375 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
376 transaction
.replace(R
.id
.fragment
, new PreviewMediaFragment(mFile
, mAccount
), FileDetailFragment
.FTAG
);
377 transaction
.commit();
378 mWaitingToPreview
= false
;
381 detailsFragment
.updateFileDetails(false
, (success
));
382 // TODO error message if !success ¿?
385 } // TODO else if (fragment != null && fragment )