Refactoring and clean-up of fragment for media previews
[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.preview.PreviewMediaFragment;
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 * @author David A. Velasco
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 public static final String EXTRA_MODE = "MODE";
61 public static final int MODE_DETAILS = 0;
62 public static final int MODE_PREVIEW = 1;
63
64 public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
65
66 private boolean mConfigurationChangedToLandscape = false;
67 private FileDownloaderBinder mDownloaderBinder = null;
68 private ServiceConnection mDownloadConnection, mUploadConnection = null;
69 private FileUploaderBinder mUploaderBinder = null;
70 private boolean mWaitingToPreview;
71
72 private OCFile mFile;
73 private Account mAccount;
74
75
76 @Override
77 protected void onCreate(Bundle savedInstanceState) {
78 super.onCreate(savedInstanceState);
79
80 mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
81 mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
82
83 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
84 Configuration conf = getResources().getConfiguration();
85 mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE &&
86 (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
87 );
88
89 if (!mConfigurationChangedToLandscape) {
90 setContentView(R.layout.file_activity_details);
91
92 ActionBar actionBar = getSupportActionBar();
93 actionBar.setDisplayHomeAsUpEnabled(true);
94
95 if (savedInstanceState == null) {
96 mWaitingToPreview = false;
97 createChildFragment();
98 } else {
99 mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW);
100 }
101
102 mDownloadConnection = new DetailsServiceConnection();
103 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
104 mUploadConnection = new DetailsServiceConnection();
105 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
106
107
108 } else {
109 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
110 }
111
112
113 }
114
115 /**
116 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
117 * the requested {@link Intent}.
118 */
119 private void createChildFragment() {
120 int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW);
121
122 Fragment newFragment = null;
123 if (PreviewMediaFragment.canBePreviewed(mFile) && mode == MODE_PREVIEW) {
124 if (mFile.isDown()) {
125 newFragment = new PreviewMediaFragment(mFile, mAccount);
126
127 } else {
128 newFragment = new FileDetailFragment(mFile, mAccount);
129 mWaitingToPreview = true;
130 }
131
132 } else {
133 newFragment = new FileDetailFragment(mFile, mAccount);
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 if (mWaitingToPreview) {
158 requestForDownload();
159 }
160
161 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
162 Log.d(TAG, "Upload service connected");
163 mUploaderBinder = (FileUploaderBinder) service;
164 } else {
165 return;
166 }
167
168 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
169 FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null;
170 if (detailsFragment != null) {
171 detailsFragment.listenForTransferProgress();
172 detailsFragment.updateFileDetails(mWaitingToPreview); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
173 }
174 }
175
176 @Override
177 public void onServiceDisconnected(ComponentName component) {
178 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
179 Log.d(TAG, "Download service disconnected");
180 mDownloaderBinder = null;
181 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
182 Log.d(TAG, "Upload service disconnected");
183 mUploaderBinder = null;
184 }
185 }
186 };
187
188
189 @Override
190 public void onDestroy() {
191 super.onDestroy();
192 if (mDownloadConnection != null) {
193 unbindService(mDownloadConnection);
194 mDownloadConnection = null;
195 }
196 if (mUploadConnection != null) {
197 unbindService(mUploadConnection);
198 mUploadConnection = null;
199 }
200 }
201
202
203 @Override
204 public boolean onOptionsItemSelected(MenuItem item) {
205 boolean returnValue = false;
206
207 switch(item.getItemId()){
208 case android.R.id.home:
209 backToDisplayActivity();
210 returnValue = true;
211 break;
212 default:
213 returnValue = super.onOptionsItemSelected(item);
214 }
215
216 return returnValue;
217 }
218
219
220
221 @Override
222 protected void onResume() {
223
224 super.onResume();
225 if (!mConfigurationChangedToLandscape) {
226 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
227 if (fragment != null && fragment instanceof FileDetailFragment) {
228 ((FileDetailFragment) fragment).updateFileDetails(false);
229 }
230 }
231 }
232
233
234 private void backToDisplayActivity() {
235 Intent intent = new Intent(this, FileDisplayActivity.class);
236 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
237 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
238 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
239 startActivity(intent);
240 finish();
241 }
242
243
244 @Override
245 protected Dialog onCreateDialog(int id) {
246 Dialog dialog = null;
247 switch (id) {
248 case DIALOG_SHORT_WAIT: {
249 ProgressDialog working_dialog = new ProgressDialog(this);
250 working_dialog.setMessage(getResources().getString(
251 R.string.wait_a_moment));
252 working_dialog.setIndeterminate(true);
253 working_dialog.setCancelable(false);
254 dialog = working_dialog;
255 break;
256 }
257 default:
258 dialog = null;
259 }
260 return dialog;
261 }
262
263
264 /**
265 * {@inheritDoc}
266 */
267 @Override
268 public void onFileStateChanged() {
269 // nothing to do here!
270 }
271
272
273 /**
274 * {@inheritDoc}
275 */
276 @Override
277 public FileDownloaderBinder getFileDownloaderBinder() {
278 return mDownloaderBinder;
279 }
280
281
282 @Override
283 public FileUploaderBinder getFileUploaderBinder() {
284 return mUploaderBinder;
285 }
286
287
288 @Override
289 public void showFragmentWithDetails(OCFile file) {
290 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
291 transaction.replace(R.id.fragment, new FileDetailFragment(file, mAccount), FileDetailFragment.FTAG);
292 transaction.commit();
293 }
294
295
296 private void requestForDownload() {
297 if (!mDownloaderBinder.isDownloading(mAccount, mFile)) {
298 Intent i = new Intent(this, FileDownloader.class);
299 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
300 i.putExtra(FileDownloader.EXTRA_FILE, mFile);
301 startService(i);
302 }
303 }
304
305 @Override
306 public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {
307 if (success) {
308 if (mWaitingToPreview) {
309 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
310 transaction.replace(R.id.fragment, new PreviewMediaFragment(file, mAccount), FileDetailFragment.FTAG);
311 transaction.commit();
312 mWaitingToPreview = false;
313 }
314 }
315 }
316
317 }