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
.ComponentName
;
24 import android
.content
.Context
;
25 import android
.content
.Intent
;
26 import android
.content
.ServiceConnection
;
27 import android
.content
.res
.Configuration
;
28 import android
.os
.Bundle
;
29 import android
.os
.IBinder
;
30 import android
.support
.v4
.app
.Fragment
;
31 import android
.support
.v4
.app
.FragmentTransaction
;
32 import android
.util
.Log
;
34 import com
.actionbarsherlock
.app
.ActionBar
;
35 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
36 import com
.actionbarsherlock
.view
.MenuItem
;
37 import com
.owncloud
.android
.datamodel
.OCFile
;
38 import com
.owncloud
.android
.files
.services
.FileDownloader
;
39 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
40 import com
.owncloud
.android
.files
.services
.FileUploader
;
41 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
42 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
43 import com
.owncloud
.android
.ui
.preview
.PreviewMediaFragment
;
45 import com
.owncloud
.android
.R
;
48 * This activity displays the details of a file like its name, its size and so
51 * @author Bartek Przybylski
52 * @author David A. Velasco
54 public class FileDetailActivity
extends SherlockFragmentActivity
implements FileDetailFragment
.ContainerActivity
{
56 public static final int DIALOG_SHORT_WAIT
= 0;
58 public static final String TAG
= FileDetailActivity
.class.getSimpleName();
60 public static final String EXTRA_MODE
= "MODE";
61 public static final int MODE_DETAILS
= 0;
62 public static final int MODE_PREVIEW
= 1;
64 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
66 private boolean mConfigurationChangedToLandscape
= false
;
67 private FileDownloaderBinder mDownloaderBinder
= null
;
68 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
69 private FileUploaderBinder mUploaderBinder
= null
;
70 private boolean mWaitingToPreview
;
73 private Account mAccount
;
77 protected void onCreate(Bundle savedInstanceState
) {
78 super.onCreate(savedInstanceState
);
80 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
81 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
83 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
84 Configuration conf
= getResources().getConfiguration();
85 mConfigurationChangedToLandscape
= (conf
.orientation
== Configuration
.ORIENTATION_LANDSCAPE
&&
86 (conf
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
89 if (!mConfigurationChangedToLandscape
) {
90 setContentView(R
.layout
.file_activity_details
);
92 ActionBar actionBar
= getSupportActionBar();
93 actionBar
.setDisplayHomeAsUpEnabled(true
);
95 if (savedInstanceState
== null
) {
96 mWaitingToPreview
= false
;
97 createChildFragment();
99 mWaitingToPreview
= savedInstanceState
.getBoolean(KEY_WAITING_TO_PREVIEW
);
102 mDownloadConnection
= new DetailsServiceConnection();
103 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
104 mUploadConnection
= new DetailsServiceConnection();
105 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
109 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
116 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
117 * the requested {@link Intent}.
119 private void createChildFragment() {
120 int mode
= getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
);
122 Fragment newFragment
= null
;
123 if (PreviewMediaFragment
.canBePreviewed(mFile
) && mode
== MODE_PREVIEW
) {
124 if (mFile
.isDown()) {
125 newFragment
= new PreviewMediaFragment(mFile
, mAccount
);
128 newFragment
= new FileDetailFragment(mFile
, mAccount
);
129 mWaitingToPreview
= true
;
133 newFragment
= new FileDetailFragment(mFile
, mAccount
);
135 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
136 ft
.replace(R
.id
.fragment
, newFragment
, FileDetailFragment
.FTAG
);
142 protected void onSaveInstanceState(Bundle outState
) {
143 super.onSaveInstanceState(outState
);
144 outState
.putBoolean(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
148 /** Defines callbacks for service binding, passed to bindService() */
149 private class DetailsServiceConnection
implements ServiceConnection
{
152 public void onServiceConnected(ComponentName component
, IBinder service
) {
154 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
155 Log
.d(TAG
, "Download service connected");
156 mDownloaderBinder
= (FileDownloaderBinder
) service
;
157 if (mWaitingToPreview
) {
158 requestForDownload();
161 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
162 Log
.d(TAG
, "Upload service connected");
163 mUploaderBinder
= (FileUploaderBinder
) service
;
168 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
169 FileDetailFragment detailsFragment
= (fragment
instanceof FileDetailFragment
) ?
(FileDetailFragment
) fragment
: null
;
170 if (detailsFragment
!= null
) {
171 detailsFragment
.listenForTransferProgress();
172 detailsFragment
.updateFileDetails(mWaitingToPreview
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
177 public void onServiceDisconnected(ComponentName component
) {
178 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
179 Log
.d(TAG
, "Download service disconnected");
180 mDownloaderBinder
= null
;
181 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
182 Log
.d(TAG
, "Upload service disconnected");
183 mUploaderBinder
= null
;
190 public void onDestroy() {
192 if (mDownloadConnection
!= null
) {
193 unbindService(mDownloadConnection
);
194 mDownloadConnection
= null
;
196 if (mUploadConnection
!= null
) {
197 unbindService(mUploadConnection
);
198 mUploadConnection
= null
;
204 public boolean onOptionsItemSelected(MenuItem item
) {
205 boolean returnValue
= false
;
207 switch(item
.getItemId()){
208 case android
.R
.id
.home
:
209 backToDisplayActivity();
213 returnValue
= super.onOptionsItemSelected(item
);
222 protected void onResume() {
225 if (!mConfigurationChangedToLandscape
) {
226 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
227 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
228 ((FileDetailFragment
) fragment
).updateFileDetails(false
);
234 private void backToDisplayActivity() {
235 Intent intent
= new Intent(this, FileDisplayActivity
.class);
236 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
237 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, mFile
);
238 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
239 startActivity(intent
);
245 protected Dialog
onCreateDialog(int id
) {
246 Dialog dialog
= null
;
248 case DIALOG_SHORT_WAIT
: {
249 ProgressDialog working_dialog
= new ProgressDialog(this);
250 working_dialog
.setMessage(getResources().getString(
251 R
.string
.wait_a_moment
));
252 working_dialog
.setIndeterminate(true
);
253 working_dialog
.setCancelable(false
);
254 dialog
= working_dialog
;
268 public void onFileStateChanged() {
269 // nothing to do here!
277 public FileDownloaderBinder
getFileDownloaderBinder() {
278 return mDownloaderBinder
;
283 public FileUploaderBinder
getFileUploaderBinder() {
284 return mUploaderBinder
;
289 public void showFragmentWithDetails(OCFile file
) {
290 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
291 transaction
.replace(R
.id
.fragment
, new FileDetailFragment(file
, mAccount
), FileDetailFragment
.FTAG
);
292 transaction
.commit();
296 private void requestForDownload() {
297 if (!mDownloaderBinder
.isDownloading(mAccount
, mFile
)) {
298 Intent i
= new Intent(this, FileDownloader
.class);
299 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
300 i
.putExtra(FileDownloader
.EXTRA_FILE
, mFile
);
306 public void notifySuccessfulDownload(OCFile file
, Intent intent
, boolean success
) {
308 if (mWaitingToPreview
) {
309 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
310 transaction
.replace(R
.id
.fragment
, new PreviewMediaFragment(file
, mAccount
), FileDetailFragment
.FTAG
);
311 transaction
.commit();
312 mWaitingToPreview
= false
;