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 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 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
158 Log
.d(TAG
, "Upload service connected");
159 mUploaderBinder
= (FileUploaderBinder
) service
;
164 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
165 FileDetailFragment detailsFragment
= (fragment
instanceof FileDetailFragment
) ?
(FileDetailFragment
) fragment
: null
;
166 if (detailsFragment
!= null
) {
167 detailsFragment
.listenForTransferProgress();
168 detailsFragment
.updateFileDetails(false
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
173 public void onServiceDisconnected(ComponentName component
) {
174 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
175 Log
.d(TAG
, "Download service disconnected");
176 mDownloaderBinder
= null
;
177 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
178 Log
.d(TAG
, "Upload service disconnected");
179 mUploaderBinder
= null
;
186 public void onDestroy() {
188 if (mDownloadConnection
!= null
) {
189 unbindService(mDownloadConnection
);
190 mDownloadConnection
= null
;
192 if (mUploadConnection
!= null
) {
193 unbindService(mUploadConnection
);
194 mUploadConnection
= null
;
200 public boolean onOptionsItemSelected(MenuItem item
) {
201 boolean returnValue
= false
;
203 switch(item
.getItemId()){
204 case android
.R
.id
.home
:
205 backToDisplayActivity();
209 returnValue
= super.onOptionsItemSelected(item
);
218 protected void onResume() {
221 if (!mConfigurationChangedToLandscape
) {
222 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
223 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
224 ((FileDetailFragment
) fragment
).updateFileDetails(false
);
230 private void backToDisplayActivity() {
231 Intent intent
= new Intent(this, FileDisplayActivity
.class);
232 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
233 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
));
234 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
));
235 startActivity(intent
);
241 protected Dialog
onCreateDialog(int id
) {
242 Dialog dialog
= null
;
244 case DIALOG_SHORT_WAIT
: {
245 ProgressDialog working_dialog
= new ProgressDialog(this);
246 working_dialog
.setMessage(getResources().getString(
247 R
.string
.wait_a_moment
));
248 working_dialog
.setIndeterminate(true
);
249 working_dialog
.setCancelable(false
);
250 dialog
= working_dialog
;
264 public void onFileStateChanged() {
265 // nothing to do here!
273 public FileDownloaderBinder
getFileDownloaderBinder() {
274 return mDownloaderBinder
;
279 public FileUploaderBinder
getFileUploaderBinder() {
280 return mUploaderBinder
;
285 public void showFragmentWithDetails(OCFile file
) {
286 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
287 transaction
.replace(R
.id
.fragment
, new FileDetailFragment(file
, (Account
) getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
)), FileDetailFragment
.FTAG
);
288 transaction
.commit();