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
;
50 import com
.owncloud
.android
.AccountUtils
;
51 import com
.owncloud
.android
.R
;
54 * This activity displays the details of a file like its name, its size and so
57 * @author Bartek Przybylski
58 * @author David A. Velasco
60 public class FileDetailActivity
extends SherlockFragmentActivity
implements FileFragment
.ContainerActivity
{
62 public static final int DIALOG_SHORT_WAIT
= 0;
64 public static final String TAG
= FileDetailActivity
.class.getSimpleName();
66 public static final String EXTRA_MODE
= "MODE";
67 public static final int MODE_DETAILS
= 0;
68 public static final int MODE_PREVIEW
= 1;
70 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
72 private boolean mConfigurationChangedToLandscape
= false
;
73 private FileDownloaderBinder mDownloaderBinder
= null
;
74 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
75 private FileUploaderBinder mUploaderBinder
= null
;
76 private boolean mWaitingToPreview
;
79 private Account mAccount
;
81 private FileDataStorageManager mStorageManager
;
82 private DownloadFinishReceiver mDownloadFinishReceiver
;
86 protected void onCreate(Bundle savedInstanceState
) {
87 super.onCreate(savedInstanceState
);
89 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
90 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
91 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
93 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
94 Configuration conf
= getResources().getConfiguration();
95 mConfigurationChangedToLandscape
= (conf
.orientation
== Configuration
.ORIENTATION_LANDSCAPE
&&
96 (conf
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
99 if (!mConfigurationChangedToLandscape
) {
100 setContentView(R
.layout
.file_activity_details
);
102 ActionBar actionBar
= getSupportActionBar();
103 actionBar
.setDisplayHomeAsUpEnabled(true
);
105 if (savedInstanceState
== null
) {
106 mWaitingToPreview
= false
;
107 createChildFragment();
109 mWaitingToPreview
= savedInstanceState
.getBoolean(KEY_WAITING_TO_PREVIEW
);
112 mDownloadConnection
= new DetailsServiceConnection();
113 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
114 mUploadConnection
= new DetailsServiceConnection();
115 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
119 backToDisplayActivity(); // 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
.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
.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
.d(TAG
, "Download service disconnected");
218 mDownloaderBinder
= null
;
219 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
220 Log
.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();
251 returnValue
= super.onOptionsItemSelected(item
);
259 private void backToDisplayActivity() {
260 Intent intent
= new Intent(this, FileDisplayActivity
.class);
261 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
262 OCFile targetFile
= null
;
264 targetFile
= mStorageManager
.getFileById(mFile
.getParentId());
266 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, targetFile
);
267 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
268 startActivity(intent
);
274 protected Dialog
onCreateDialog(int id
) {
275 Dialog dialog
= null
;
277 case DIALOG_SHORT_WAIT
: {
278 ProgressDialog working_dialog
= new ProgressDialog(this);
279 working_dialog
.setMessage(getResources().getString(
280 R
.string
.wait_a_moment
));
281 working_dialog
.setIndeterminate(true
);
282 working_dialog
.setCancelable(false
);
283 dialog
= working_dialog
;
297 public void onFileStateChanged() {
298 // nothing to do here!
306 public FileDownloaderBinder
getFileDownloaderBinder() {
307 return mDownloaderBinder
;
312 public FileUploaderBinder
getFileUploaderBinder() {
313 return mUploaderBinder
;
318 public void showFragmentWithDetails(OCFile file
) {
319 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
320 transaction
.replace(R
.id
.fragment
, new FileDetailFragment(file
, mAccount
), FileDetailFragment
.FTAG
);
321 transaction
.commit();
325 private void requestForDownload() {
326 if (!mDownloaderBinder
.isDownloading(mAccount
, mFile
)) {
327 Intent i
= new Intent(this, FileDownloader
.class);
328 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
329 i
.putExtra(FileDownloader
.EXTRA_FILE
, mFile
);
336 * Class waiting for broadcast events from the {@link FielDownloader} service.
338 * Updates the UI when a download is started or finished, provided that it is relevant for the
341 private class DownloadFinishReceiver
extends BroadcastReceiver
{
343 public void onReceive(Context context
, Intent intent
) {
344 boolean sameAccount
= isSameAccount(context
, intent
);
345 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
346 boolean samePath
= (mFile
!= null
&& mFile
.getRemotePath().equals(downloadedRemotePath
));
348 if (sameAccount
&& samePath
) {
349 updateChildFragment(intent
.getAction(), downloadedRemotePath
, intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
));
352 removeStickyBroadcast(intent
);
355 private boolean isSameAccount(Context context
, Intent intent
) {
356 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
357 return (accountName
!= null
&& accountName
.equals(AccountUtils
.getCurrentOwnCloudAccount(context
).name
));
362 public void updateChildFragment(String downloadEvent
, String downloadedRemotePath
, boolean success
) {
363 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
364 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
365 FileDetailFragment detailsFragment
= (FileDetailFragment
) fragment
;
366 OCFile fileInFragment
= detailsFragment
.getFile();
367 if (fileInFragment
!= null
&& !downloadedRemotePath
.equals(fileInFragment
.getRemotePath())) {
368 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
369 mWaitingToPreview
= false
;
371 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
)) {
372 // grants that the progress bar is updated
373 detailsFragment
.listenForTransferProgress();
374 detailsFragment
.updateFileDetails(true
, false
);
376 } else if (downloadEvent
.equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
377 // refresh the details fragment
378 if (success
&& mWaitingToPreview
) {
379 mFile
= mStorageManager
.getFileById(mFile
.getFileId()); // update the file from database, for the local storage path
380 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
381 transaction
.replace(R
.id
.fragment
, new PreviewMediaFragment(mFile
, mAccount
), FileDetailFragment
.FTAG
);
382 transaction
.commit();
383 mWaitingToPreview
= false
;
386 detailsFragment
.updateFileDetails(false
, (success
));
387 // TODO error message if !success ¿?
390 } // TODO else if (fragment != null && fragment )