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 java
.lang
.ref
.WeakReference
;
22 import android
.accounts
.Account
;
23 import android
.app
.Dialog
;
24 import android
.app
.ProgressDialog
;
25 import android
.content
.ComponentName
;
26 import android
.content
.Context
;
27 import android
.content
.Intent
;
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
;
35 import android
.widget
.ProgressBar
;
37 import com
.actionbarsherlock
.app
.ActionBar
;
38 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
39 import com
.actionbarsherlock
.view
.MenuItem
;
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
.FilePreviewFragment
;
48 import com
.owncloud
.android
.R
;
50 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
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 FileDetailFragment
.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 private 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
;
79 protected void onCreate(Bundle savedInstanceState
) {
80 super.onCreate(savedInstanceState
);
82 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
83 Configuration conf
= getResources().getConfiguration();
84 mConfigurationChangedToLandscape
= (conf
.orientation
== Configuration
.ORIENTATION_LANDSCAPE
&&
85 (conf
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
88 if (!mConfigurationChangedToLandscape
) {
89 mDownloadConnection
= new DetailsServiceConnection();
90 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
91 mUploadConnection
= new DetailsServiceConnection();
92 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
94 setContentView(R
.layout
.file_activity_details
);
96 ActionBar actionBar
= getSupportActionBar();
97 actionBar
.setDisplayHomeAsUpEnabled(true
);
99 if (savedInstanceState
== null
) {
100 mWaitingToPreview
= false
;
101 createChildFragment();
103 mWaitingToPreview
= savedInstanceState
.getBoolean(KEY_WAITING_TO_PREVIEW
);
107 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
114 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
115 * the requested {@link Intent}.
117 private void createChildFragment() {
118 OCFile file
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
119 Account account
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
120 int mode
= getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
);
122 Fragment newFragment
= null
;
123 if (FilePreviewFragment
.canBePreviewed(file
) && mode
== MODE_PREVIEW
) {
125 newFragment
= new FilePreviewFragment(file
, account
);
128 newFragment
= new FileDetailFragment(file
, account
);
129 mWaitingToPreview
= true
;
133 newFragment
= new FileDetailFragment(file
, account
);
135 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
136 ft
.replace(R
.id
.fragment
, newFragment
, FileDetailFragment
.FTAG
);
142 protected void onSaveInstanceState(Bundle outState
) {
143 outState
.putBoolean(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
147 /** Defines callbacks for service binding, passed to bindService() */
148 private class DetailsServiceConnection
implements ServiceConnection
{
151 public void onServiceConnected(ComponentName component
, IBinder service
) {
153 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
154 Log
.d(TAG
, "Download service connected");
155 mDownloaderBinder
= (FileDownloaderBinder
) service
;
156 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
157 Log
.d(TAG
, "Upload service connected");
158 mUploaderBinder
= (FileUploaderBinder
) service
;
163 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
164 FileDetailFragment detailsFragment
= (fragment
instanceof FileDetailFragment
) ?
(FileDetailFragment
) fragment
: null
;
165 if (detailsFragment
!= null
) {
166 detailsFragment
.listenForTransferProgress();
167 detailsFragment
.updateFileDetails(false
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
172 public void onServiceDisconnected(ComponentName component
) {
173 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
174 Log
.d(TAG
, "Download service disconnected");
175 mDownloaderBinder
= null
;
176 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
177 Log
.d(TAG
, "Upload service disconnected");
178 mUploaderBinder
= null
;
185 public void onDestroy() {
187 if (mDownloadConnection
!= null
) {
188 unbindService(mDownloadConnection
);
189 mDownloadConnection
= null
;
191 if (mUploadConnection
!= null
) {
192 unbindService(mUploadConnection
);
193 mUploadConnection
= null
;
199 public boolean onOptionsItemSelected(MenuItem item
) {
200 boolean returnValue
= false
;
202 switch(item
.getItemId()){
203 case android
.R
.id
.home
:
204 backToDisplayActivity();
208 returnValue
= super.onOptionsItemSelected(item
);
217 protected void onResume() {
220 if (!mConfigurationChangedToLandscape
) {
221 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
222 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
223 ((FileDetailFragment
) fragment
).updateFileDetails(false
);
229 private void backToDisplayActivity() {
230 Intent intent
= new Intent(this, FileDisplayActivity
.class);
231 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
232 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
));
233 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
));
234 startActivity(intent
);
240 protected Dialog
onCreateDialog(int id
) {
241 Dialog dialog
= null
;
243 case DIALOG_SHORT_WAIT
: {
244 ProgressDialog working_dialog
= new ProgressDialog(this);
245 working_dialog
.setMessage(getResources().getString(
246 R
.string
.wait_a_moment
));
247 working_dialog
.setIndeterminate(true
);
248 working_dialog
.setCancelable(false
);
249 dialog
= working_dialog
;
263 public void onFileStateChanged() {
264 // nothing to do here!
272 public FileDownloaderBinder
getFileDownloaderBinder() {
273 return mDownloaderBinder
;
278 public FileUploaderBinder
getFileUploaderBinder() {
279 return mUploaderBinder
;
284 public void showFragmentWithDetails(OCFile file
) {
285 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
286 transaction
.replace(R
.id
.fragment
, new FileDetailFragment(file
, (Account
) getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
)), FileDetailFragment
.FTAG
);
287 transaction
.commit();