97f719f47c86a3b9d8ee23158b71f8bdf1506f8d
[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 java.lang.ref.WeakReference;
21
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;
36
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;
47
48 import com.owncloud.android.R;
49
50 import eu.alefzero.webdav.OnDatatransferProgressListener;
51
52 /**
53 * This activity displays the details of a file like its name, its size and so
54 * on.
55 *
56 * @author Bartek Przybylski
57 * @author David A. Velasco
58 */
59 public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity {
60
61 public static final int DIALOG_SHORT_WAIT = 0;
62
63 public static final String TAG = FileDetailActivity.class.getSimpleName();
64
65 public static final String EXTRA_MODE = "MODE";
66 public static final int MODE_DETAILS = 0;
67 public static final int MODE_PREVIEW = 1;
68
69 private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
70
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;
76
77
78 @Override
79 protected void onCreate(Bundle savedInstanceState) {
80 super.onCreate(savedInstanceState);
81
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
86 );
87
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);
93
94 setContentView(R.layout.file_activity_details);
95
96 ActionBar actionBar = getSupportActionBar();
97 actionBar.setDisplayHomeAsUpEnabled(true);
98
99 if (savedInstanceState == null) {
100 mWaitingToPreview = false;
101 createChildFragment();
102 } else {
103 mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW);
104 }
105
106 } else {
107 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
108 }
109
110
111 }
112
113 /**
114 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
115 * the requested {@link Intent}.
116 */
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);
121
122 Fragment newFragment = null;
123 if (FilePreviewFragment.canBePreviewed(file) && mode == MODE_PREVIEW) {
124 if (file.isDown()) {
125 newFragment = new FilePreviewFragment(file, account);
126
127 } else {
128 newFragment = new FileDetailFragment(file, account);
129 mWaitingToPreview = true;
130 }
131
132 } else {
133 newFragment = new FileDetailFragment(file, account);
134 }
135 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
136 ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);
137 ft.commit();
138 }
139
140
141 @Override
142 protected void onSaveInstanceState(Bundle outState) {
143 super.onSaveInstanceState(outState);
144 outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
145 }
146
147
148 /** Defines callbacks for service binding, passed to bindService() */
149 private class DetailsServiceConnection implements ServiceConnection {
150
151 @Override
152 public void onServiceConnected(ComponentName component, IBinder service) {
153
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;
160 } else {
161 return;
162 }
163
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())
169 }
170 }
171
172 @Override
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;
180 }
181 }
182 };
183
184
185 @Override
186 public void onDestroy() {
187 super.onDestroy();
188 if (mDownloadConnection != null) {
189 unbindService(mDownloadConnection);
190 mDownloadConnection = null;
191 }
192 if (mUploadConnection != null) {
193 unbindService(mUploadConnection);
194 mUploadConnection = null;
195 }
196 }
197
198
199 @Override
200 public boolean onOptionsItemSelected(MenuItem item) {
201 boolean returnValue = false;
202
203 switch(item.getItemId()){
204 case android.R.id.home:
205 backToDisplayActivity();
206 returnValue = true;
207 break;
208 default:
209 returnValue = super.onOptionsItemSelected(item);
210 }
211
212 return returnValue;
213 }
214
215
216
217 @Override
218 protected void onResume() {
219
220 super.onResume();
221 if (!mConfigurationChangedToLandscape) {
222 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
223 if (fragment != null && fragment instanceof FileDetailFragment) {
224 ((FileDetailFragment) fragment).updateFileDetails(false);
225 }
226 }
227 }
228
229
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);
236 finish();
237 }
238
239
240 @Override
241 protected Dialog onCreateDialog(int id) {
242 Dialog dialog = null;
243 switch (id) {
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;
251 break;
252 }
253 default:
254 dialog = null;
255 }
256 return dialog;
257 }
258
259
260 /**
261 * {@inheritDoc}
262 */
263 @Override
264 public void onFileStateChanged() {
265 // nothing to do here!
266 }
267
268
269 /**
270 * {@inheritDoc}
271 */
272 @Override
273 public FileDownloaderBinder getFileDownloaderBinder() {
274 return mDownloaderBinder;
275 }
276
277
278 @Override
279 public FileUploaderBinder getFileUploaderBinder() {
280 return mUploaderBinder;
281 }
282
283
284 @Override
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();
289 }
290
291 }