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
.datamodel
.FileDataStorageManager
;
39 import com
.owncloud
.android
.datamodel
.OCFile
;
40 import com
.owncloud
.android
.files
.services
.FileDownloader
;
41 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
42 import com
.owncloud
.android
.files
.services
.FileUploader
;
43 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
44 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
45 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
46 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
47 import com
.owncloud
.android
.AccountUtils
;
48 import com
.owncloud
.android
.Log_OC
;
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(false
); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
124 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
125 * the requested {@link Intent}.
127 private void createChildFragment() {
128 int mode
= getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
);
130 Fragment newFragment
= null
;
131 if (PreviewMediaFragment
.canBePreviewed(mFile
) && mode
== MODE_PREVIEW
) {
132 if (mFile
.isDown()) {
133 newFragment
= new PreviewMediaFragment(mFile
, mAccount
);
136 newFragment
= new FileDetailFragment(mFile
, mAccount
);
137 mWaitingToPreview
= true
;
141 newFragment
= new FileDetailFragment(mFile
, mAccount
);
143 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
144 ft
.replace(R
.id
.fragment
, newFragment
, FileDetailFragment
.FTAG
);
150 protected void onSaveInstanceState(Bundle outState
) {
151 super.onSaveInstanceState(outState
);
152 outState
.putBoolean(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
157 public void onPause() {
159 if (mDownloadFinishReceiver
!= null
) {
160 unregisterReceiver(mDownloadFinishReceiver
);
161 mDownloadFinishReceiver
= null
;
167 public void onResume() {
169 if (!mConfigurationChangedToLandscape
) {
170 // TODO this is probably unnecessary
171 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
172 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
173 ((FileDetailFragment
) fragment
).updateFileDetails(false
, false
);
176 // Listen for download messages
177 IntentFilter downloadIntentFilter
= new IntentFilter(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
178 downloadIntentFilter
.addAction(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
179 mDownloadFinishReceiver
= new DownloadFinishReceiver();
180 registerReceiver(mDownloadFinishReceiver
, downloadIntentFilter
);
184 /** Defines callbacks for service binding, passed to bindService() */
185 private class DetailsServiceConnection
implements ServiceConnection
{
188 public void onServiceConnected(ComponentName component
, IBinder service
) {
190 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
191 Log_OC
.d(TAG
, "Download service connected");
192 mDownloaderBinder
= (FileDownloaderBinder
) service
;
193 if (mWaitingToPreview
) {
194 requestForDownload();
197 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
198 Log_OC
.d(TAG
, "Upload service connected");
199 mUploaderBinder
= (FileUploaderBinder
) service
;
204 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
205 FileDetailFragment detailsFragment
= (fragment
instanceof FileDetailFragment
) ?
(FileDetailFragment
) fragment
: null
;
206 if (detailsFragment
!= null
) {
207 detailsFragment
.listenForTransferProgress();
208 detailsFragment
.updateFileDetails(mWaitingToPreview
, false
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
213 public void onServiceDisconnected(ComponentName component
) {
214 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
215 Log_OC
.d(TAG
, "Download service disconnected");
216 mDownloaderBinder
= null
;
217 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
218 Log_OC
.d(TAG
, "Upload service disconnected");
219 mUploaderBinder
= null
;
226 public void onDestroy() {
228 if (mDownloadConnection
!= null
) {
229 unbindService(mDownloadConnection
);
230 mDownloadConnection
= null
;
232 if (mUploadConnection
!= null
) {
233 unbindService(mUploadConnection
);
234 mUploadConnection
= null
;
240 public boolean onOptionsItemSelected(MenuItem item
) {
241 boolean returnValue
= false
;
243 switch(item
.getItemId()){
244 case android
.R
.id
.home
:
245 backToDisplayActivity(true
);
249 returnValue
= super.onOptionsItemSelected(item
);
255 private void backToDisplayActivity(boolean moveToParent
) {
256 Intent intent
= new Intent(this, FileDisplayActivity
.class);
257 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
258 OCFile targetFile
= null
;
260 targetFile
= moveToParent ? mStorageManager
.getFileById(mFile
.getParentId()) : mFile
;
262 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, targetFile
);
263 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
264 startActivity(intent
);
270 protected Dialog
onCreateDialog(int id
) {
271 Dialog dialog
= null
;
273 case DIALOG_SHORT_WAIT
: {
274 ProgressDialog working_dialog
= new ProgressDialog(this);
275 working_dialog
.setMessage(getResources().getString(
276 R
.string
.wait_a_moment
));
277 working_dialog
.setIndeterminate(true
);
278 working_dialog
.setCancelable(false
);
279 dialog
= working_dialog
;
293 public void onFileStateChanged() {
294 // nothing to do here!
302 public FileDownloaderBinder
getFileDownloaderBinder() {
303 return mDownloaderBinder
;
308 public FileUploaderBinder
getFileUploaderBinder() {
309 return mUploaderBinder
;
314 public void showFragmentWithDetails(OCFile file
) {
315 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
316 transaction
.replace(R
.id
.fragment
, new FileDetailFragment(file
, mAccount
), FileDetailFragment
.FTAG
);
317 transaction
.commit();
321 private void requestForDownload() {
322 if (!mDownloaderBinder
.isDownloading(mAccount
, mFile
)) {
323 Intent i
= new Intent(this, FileDownloader
.class);
324 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
325 i
.putExtra(FileDownloader
.EXTRA_FILE
, mFile
);
332 * Class waiting for broadcast events from the {@link FielDownloader} service.
334 * Updates the UI when a download is started or finished, provided that it is relevant for the
337 private class DownloadFinishReceiver
extends BroadcastReceiver
{
339 public void onReceive(Context context
, Intent intent
) {
340 boolean sameAccount
= isSameAccount(context
, intent
);
341 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
342 boolean samePath
= (mFile
!= null
&& mFile
.getRemotePath().equals(downloadedRemotePath
));
344 if (sameAccount
&& samePath
) {
345 updateChildFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
348 removeStickyBroadcast(intent
);
351 private boolean isSameAccount(Context context
, Intent intent
) {
352 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
353 return (accountName
!= null
&& accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
));
358 public void updateChildFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
359 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
360 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
361 FileDetailFragment detailsFragment
= (FileDetailFragment
) fragment
;
362 OCFile fileInFragment
= detailsFragment
.getFile();
363 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
364 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
365 mWaitingToPreview
= false
;
367 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
)) {
368 // grants that the progress bar is updated
369 detailsFragment
.listenForTransferProgress();
370 detailsFragment
.updateFileDetails(true
, false
);
372 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
373 // refresh the details fragment
374 if (success
&& mWaitingToPreview
) {
375 mFile
= mStorageManager
.getFileById(mFile
.getFileId()); // update the file from database, for the local storage path
376 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
377 transaction
.replace(R
.id
.fragment
, new PreviewMediaFragment(mFile
, mAccount
), FileDetailFragment
.FTAG
);
378 transaction
.commit();
379 mWaitingToPreview
= false
;
382 detailsFragment
.updateFileDetails(false
, (success
));
383 // TODO error message if !success ¿?
386 } // TODO else if (fragment != null && fragment )