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
.fragment
.FileFragment
;
44 import com
.owncloud
.android
.ui
.fragment
.FilePreviewFragment
;
46 import com
.owncloud
.android
.AccountUtils
;
47 import com
.owncloud
.android
.R
;
50 * This activity displays the details of a file like its name, its size and so
53 * @author Bartek Przybylski
56 public class FileDetailActivity
extends SherlockFragmentActivity
implements FileDetailFragment
.ContainerActivity
{
58 public static final int DIALOG_SHORT_WAIT
= 0;
60 public static final String TAG
= FileDetailActivity
.class.getSimpleName();
62 public static final String EXTRA_MODE
= "MODE";
63 public static final int MODE_DETAILS
= 0;
64 public static final int MODE_PREVIEW
= 1;
66 private boolean mConfigurationChangedToLandscape
= false
;
67 private FileDownloaderBinder mDownloaderBinder
= null
;
68 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
69 private FileUploaderBinder mUploaderBinder
= null
;
73 protected void onCreate(Bundle savedInstanceState
) {
74 super.onCreate(savedInstanceState
);
76 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
77 Configuration conf
= getResources().getConfiguration();
78 mConfigurationChangedToLandscape
= (conf
.orientation
== Configuration
.ORIENTATION_LANDSCAPE
&&
79 (conf
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
82 if (!mConfigurationChangedToLandscape
) {
83 mDownloadConnection
= new DetailsServiceConnection();
84 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
85 mUploadConnection
= new DetailsServiceConnection();
86 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
88 setContentView(R
.layout
.file_activity_details
);
90 ActionBar actionBar
= getSupportActionBar();
91 actionBar
.setDisplayHomeAsUpEnabled(true
);
93 if (savedInstanceState
== null
) {
94 createChildFragment();
98 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
105 private void createChildFragment() {
106 OCFile file
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
107 Account account
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
108 int mode
= getIntent().getIntExtra(EXTRA_MODE
, MODE_PREVIEW
);
110 Fragment newFragment
= null
;
111 if (FilePreviewFragment
.canBePreviewed(file
) && mode
== MODE_PREVIEW
) {
112 newFragment
= new FilePreviewFragment(file
, account
);
115 newFragment
= new FileDetailFragment(file
, account
);
117 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
118 ft
.replace(R
.id
.fragment
, newFragment
, FileDetailFragment
.FTAG
);
124 /** Defines callbacks for service binding, passed to bindService() */
125 private class DetailsServiceConnection
implements ServiceConnection
{
128 public void onServiceConnected(ComponentName component
, IBinder service
) {
129 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
130 Log
.d(TAG
, "Download service connected");
131 mDownloaderBinder
= (FileDownloaderBinder
) service
;
132 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
133 Log
.d(TAG
, "Upload service connected");
134 mUploaderBinder
= (FileUploaderBinder
) service
;
138 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
139 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
140 ((FileDetailFragment
) fragment
).updateFileDetails(false
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
145 public void onServiceDisconnected(ComponentName component
) {
146 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
147 Log
.d(TAG
, "Download service disconnected");
148 mDownloaderBinder
= null
;
149 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
150 Log
.d(TAG
, "Upload service disconnected");
151 mUploaderBinder
= null
;
158 public void onDestroy() {
160 if (mDownloadConnection
!= null
) {
161 unbindService(mDownloadConnection
);
162 mDownloadConnection
= null
;
164 if (mUploadConnection
!= null
) {
165 unbindService(mUploadConnection
);
166 mUploadConnection
= null
;
172 public boolean onOptionsItemSelected(MenuItem item
) {
173 boolean returnValue
= false
;
175 switch(item
.getItemId()){
176 case android
.R
.id
.home
:
177 backToDisplayActivity();
181 returnValue
= super.onOptionsItemSelected(item
);
190 protected void onResume() {
193 if (!mConfigurationChangedToLandscape
) {
194 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
195 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
196 ((FileDetailFragment
) fragment
).updateFileDetails(false
);
202 private void backToDisplayActivity() {
203 Intent intent
= new Intent(this, FileDisplayActivity
.class);
204 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
205 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
));
206 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
));
207 startActivity(intent
);
213 protected Dialog
onCreateDialog(int id
) {
214 Dialog dialog
= null
;
216 case DIALOG_SHORT_WAIT
: {
217 ProgressDialog working_dialog
= new ProgressDialog(this);
218 working_dialog
.setMessage(getResources().getString(
219 R
.string
.wait_a_moment
));
220 working_dialog
.setIndeterminate(true
);
221 working_dialog
.setCancelable(false
);
222 dialog
= working_dialog
;
236 public void onFileStateChanged() {
237 // nothing to do here!
245 public FileDownloaderBinder
getFileDownloaderBinder() {
246 return mDownloaderBinder
;
251 public FileUploaderBinder
getFileUploaderBinder() {
252 return mUploaderBinder
;
257 public void showFragmentWithDetails(OCFile file
) {
258 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
259 transaction
.replace(R
.id
.fragment
, new FileDetailFragment(file
, (Account
) getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
)), FileDetailFragment
.FTAG
);
260 transaction
.commit();