5bf6bd2bba5fe02d7f6172a6bdeec09ee17aae65
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDetailActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.ui.activity;
19
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;
33
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;
44
45 import com.owncloud.android.R;
46
47 /**
48 * This activity displays the details of a file like its name, its size and so
49 * on.
50 *
51 * @author Bartek Przybylski
52 *
53 */
54 public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity {
55
56 public static final int DIALOG_SHORT_WAIT = 0;
57
58 public static final String TAG = FileDetailActivity.class.getSimpleName();
59
60 private boolean mConfigurationChangedToLandscape = false;
61 private FileDownloaderBinder mDownloaderBinder = null;
62 private ServiceConnection mDownloadConnection, mUploadConnection = null;
63 private FileUploaderBinder mUploaderBinder = null;
64
65
66 @Override
67 protected void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69
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
74 );
75
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);
81
82 setContentView(R.layout.file_activity_details);
83
84 ActionBar actionBar = getSupportActionBar();
85 actionBar.setDisplayHomeAsUpEnabled(true);
86
87 if (savedInstanceState == null) {
88 createChildFragment();
89 }
90
91 } else {
92 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
93 }
94
95
96 }
97
98
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);
105
106 } else {
107 newFragment = new FileDetailFragment(file, account);
108 }
109 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
110 ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);
111 ft.commit();
112 }
113
114
115
116 /** Defines callbacks for service binding, passed to bindService() */
117 private class DetailsServiceConnection implements ServiceConnection {
118
119 @Override
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;
127 } else {
128 return;
129 }
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())
133 }
134 }
135
136 @Override
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;
144 }
145 }
146 };
147
148
149 @Override
150 public void onDestroy() {
151 super.onDestroy();
152 if (mDownloadConnection != null) {
153 unbindService(mDownloadConnection);
154 mDownloadConnection = null;
155 }
156 if (mUploadConnection != null) {
157 unbindService(mUploadConnection);
158 mUploadConnection = null;
159 }
160 }
161
162
163 @Override
164 public boolean onOptionsItemSelected(MenuItem item) {
165 boolean returnValue = false;
166
167 switch(item.getItemId()){
168 case android.R.id.home:
169 backToDisplayActivity();
170 returnValue = true;
171 break;
172 default:
173 returnValue = super.onOptionsItemSelected(item);
174 }
175
176 return returnValue;
177 }
178
179
180
181 @Override
182 protected void onResume() {
183
184 super.onResume();
185 if (!mConfigurationChangedToLandscape) {
186 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
187 if (fragment != null && fragment instanceof FileDetailFragment) {
188 ((FileDetailFragment) fragment).updateFileDetails(false);
189 }
190 }
191 }
192
193
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);
200 finish();
201 }
202
203
204 @Override
205 protected Dialog onCreateDialog(int id) {
206 Dialog dialog = null;
207 switch (id) {
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;
215 break;
216 }
217 default:
218 dialog = null;
219 }
220 return dialog;
221 }
222
223
224 /**
225 * {@inheritDoc}
226 */
227 @Override
228 public void onFileStateChanged() {
229 // nothing to do here!
230 }
231
232
233 /**
234 * {@inheritDoc}
235 */
236 @Override
237 public FileDownloaderBinder getFileDownloaderBinder() {
238 return mDownloaderBinder;
239 }
240
241
242 @Override
243 public FileUploaderBinder getFileUploaderBinder() {
244 return mUploaderBinder;
245 }
246
247 }