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
.FilePreviewFragment
;
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
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 private boolean mConfigurationChangedToLandscape
= false
;
61 private FileDownloaderBinder mDownloaderBinder
= null
;
62 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
63 private FileUploaderBinder mUploaderBinder
= null
;
67 protected void onCreate(Bundle savedInstanceState
) {
68 super.onCreate(savedInstanceState
);
70 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
71 Configuration conf
= getResources().getConfiguration();
72 mConfigurationChangedToLandscape
= (conf
.orientation
== Configuration
.ORIENTATION_LANDSCAPE
&&
73 (conf
.screenLayout
& Configuration
.SCREENLAYOUT_SIZE_MASK
) >= Configuration
.SCREENLAYOUT_SIZE_LARGE
76 if (!mConfigurationChangedToLandscape
) {
77 mDownloadConnection
= new DetailsServiceConnection();
78 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
79 mUploadConnection
= new DetailsServiceConnection();
80 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
82 setContentView(R
.layout
.file_activity_details
);
84 ActionBar actionBar
= getSupportActionBar();
85 actionBar
.setDisplayHomeAsUpEnabled(true
);
87 if (savedInstanceState
== null
) {
88 createChildFragment();
92 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
99 private void createChildFragment() {
100 OCFile file
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
101 Account account
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
102 Fragment newFragment
= null
;
103 if (FilePreviewFragment
.canBePreviewed(file
)) {
104 newFragment
= new FilePreviewFragment(file
, account
);
107 newFragment
= new FileDetailFragment(file
, account
);
109 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
110 ft
.replace(R
.id
.fragment
, newFragment
, FileDetailFragment
.FTAG
);
116 /** Defines callbacks for service binding, passed to bindService() */
117 private class DetailsServiceConnection
implements ServiceConnection
{
120 public void onServiceConnected(ComponentName component
, IBinder service
) {
121 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
122 Log
.d(TAG
, "Download service connected");
123 mDownloaderBinder
= (FileDownloaderBinder
) service
;
124 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
125 Log
.d(TAG
, "Upload service connected");
126 mUploaderBinder
= (FileUploaderBinder
) service
;
130 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
131 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
132 ((FileDetailFragment
) fragment
).updateFileDetails(false
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
137 public void onServiceDisconnected(ComponentName component
) {
138 if (component
.equals(new ComponentName(FileDetailActivity
.this, FileDownloader
.class))) {
139 Log
.d(TAG
, "Download service disconnected");
140 mDownloaderBinder
= null
;
141 } else if (component
.equals(new ComponentName(FileDetailActivity
.this, FileUploader
.class))) {
142 Log
.d(TAG
, "Upload service disconnected");
143 mUploaderBinder
= null
;
150 public void onDestroy() {
152 if (mDownloadConnection
!= null
) {
153 unbindService(mDownloadConnection
);
154 mDownloadConnection
= null
;
156 if (mUploadConnection
!= null
) {
157 unbindService(mUploadConnection
);
158 mUploadConnection
= null
;
164 public boolean onOptionsItemSelected(MenuItem item
) {
165 boolean returnValue
= false
;
167 switch(item
.getItemId()){
168 case android
.R
.id
.home
:
169 backToDisplayActivity();
173 returnValue
= super.onOptionsItemSelected(item
);
182 protected void onResume() {
185 if (!mConfigurationChangedToLandscape
) {
186 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
187 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
188 ((FileDetailFragment
) fragment
).updateFileDetails(false
);
194 private void backToDisplayActivity() {
195 Intent intent
= new Intent(this, FileDisplayActivity
.class);
196 intent
.addFlags(Intent
.FLAG_ACTIVITY_CLEAR_TOP
);
197 intent
.putExtra(FileDetailFragment
.EXTRA_FILE
, getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
));
198 intent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
));
199 startActivity(intent
);
205 protected Dialog
onCreateDialog(int id
) {
206 Dialog dialog
= null
;
208 case DIALOG_SHORT_WAIT
: {
209 ProgressDialog working_dialog
= new ProgressDialog(this);
210 working_dialog
.setMessage(getResources().getString(
211 R
.string
.wait_a_moment
));
212 working_dialog
.setIndeterminate(true
);
213 working_dialog
.setCancelable(false
);
214 dialog
= working_dialog
;
228 public void onFileStateChanged() {
229 // nothing to do here!
237 public FileDownloaderBinder
getFileDownloaderBinder() {
238 return mDownloaderBinder
;
243 public FileUploaderBinder
getFileUploaderBinder() {
244 return mUploaderBinder
;