Video previews reworked to provide seamless playback in turns and changes between...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDetailActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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.BroadcastReceiver;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
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
35 import com.actionbarsherlock.app.ActionBar;
36 import com.actionbarsherlock.app.SherlockFragmentActivity;
37 import com.actionbarsherlock.view.MenuItem;
38 import com.owncloud.android.AccountUtils;
39 import com.owncloud.android.Log_OC;
40 import com.owncloud.android.R;
41 import com.owncloud.android.datamodel.FileDataStorageManager;
42 import com.owncloud.android.datamodel.OCFile;
43 import com.owncloud.android.files.services.FileDownloader;
44 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
45 import com.owncloud.android.files.services.FileUploader;
46 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
47 import com.owncloud.android.ui.fragment.FileDetailFragment;
48 import com.owncloud.android.ui.fragment.FileFragment;
49 import com.owncloud.android.ui.preview.PreviewMediaFragment;
50 import com.owncloud.android.ui.preview.PreviewVideoActivity;
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 FileFragment.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 public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
70
71 private FileDownloaderBinder mDownloaderBinder = null;
72 private ServiceConnection mDownloadConnection, mUploadConnection = null;
73 private FileUploaderBinder mUploaderBinder = null;
74 private boolean mWaitingToPreview;
75
76 private OCFile mFile;
77 private Account mAccount;
78
79 private FileDataStorageManager mStorageManager;
80 private DownloadFinishReceiver mDownloadFinishReceiver;
81
82 private Configuration mNewConfigurationChangeToApplyOnStart;
83
84 private boolean mStarted;
85
86
87 @Override
88 protected void onCreate(Bundle savedInstanceState) {
89 super.onCreate(savedInstanceState);
90 mStarted = false;
91
92 mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
93 mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
94 mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
95
96 setContentView(R.layout.file_activity_details);
97
98 ActionBar actionBar = getSupportActionBar();
99 actionBar.setDisplayHomeAsUpEnabled(true);
100
101 if (savedInstanceState == null) {
102 mWaitingToPreview = false;
103 createChildFragment();
104 } else {
105 mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW);
106 }
107
108 mDownloadConnection = new DetailsServiceConnection();
109 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
110 mUploadConnection = new DetailsServiceConnection();
111 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
112 }
113
114 /**
115 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
116 * the requested {@link Intent}.
117 */
118 private void createChildFragment() {
119 int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW);
120
121 Fragment newFragment = null;
122 if (PreviewMediaFragment.canBePreviewed(mFile) && mode == MODE_PREVIEW) {
123 if (mFile.isDown()) {
124 int startPlaybackPosition = getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0);
125 boolean autoplay = getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true);
126 newFragment = new PreviewMediaFragment(mFile, mAccount, startPlaybackPosition, autoplay);
127
128 } else {
129 newFragment = new FileDetailFragment(mFile, mAccount);
130 mWaitingToPreview = true;
131 }
132
133 } else {
134 newFragment = new FileDetailFragment(mFile, mAccount);
135 }
136 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
137 ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);
138 ft.commit();
139 }
140
141 @Override
142 public void onActivityResult (int requestCode, int resultCode, Intent data) {
143 Log_OC.e(TAG, "onActivityResult");
144 super.onActivityResult(requestCode, resultCode, data);
145 }
146
147 @Override
148 public void onConfigurationChanged (Configuration newConfig) {
149 super.onConfigurationChanged(newConfig);
150 if (mStarted) {
151 checkConfigurationChange(newConfig);
152 } else {
153 mNewConfigurationChangeToApplyOnStart = newConfig;
154 }
155 }
156
157
158 @Override
159 protected void onSaveInstanceState(Bundle outState) {
160 super.onSaveInstanceState(outState);
161 outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
162 }
163
164
165 @Override
166 public void onStart() {
167 super.onStart();
168 Log_OC.e(TAG, "onStart");
169 if (mNewConfigurationChangeToApplyOnStart != null) {
170 checkConfigurationChange(mNewConfigurationChangeToApplyOnStart);
171 mNewConfigurationChangeToApplyOnStart = null;
172 }
173 mStarted = true;
174 }
175
176 private void checkConfigurationChange(Configuration newConfig) {
177 finish();
178 Intent intent = null;
179 if ((newConfig.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
180 && newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
181
182 intent = new Intent(this, FileDisplayActivity.class);
183 intent .putExtra(FileDetailFragment.EXTRA_FILE, mFile);
184 intent .putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
185 intent.putExtra(EXTRA_MODE, getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW));
186 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
187 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
188 if (fragment != null && mFile != null && fragment instanceof PreviewMediaFragment && mFile.isVideo()) {
189 PreviewMediaFragment videoFragment = (PreviewMediaFragment)fragment;
190 intent.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, videoFragment.getPosition());
191 intent.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, videoFragment.isPlaying());
192 }
193
194 } else {
195 intent = new Intent(this, FileDetailActivity.class);
196 intent .putExtra(FileDetailFragment.EXTRA_FILE, mFile);
197 intent .putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
198 intent.putExtra(EXTRA_MODE, getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW));
199 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
200 if (fragment != null && mFile != null && fragment instanceof PreviewMediaFragment && mFile.isVideo()) {
201 PreviewMediaFragment videoFragment = (PreviewMediaFragment)fragment;
202 intent.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, videoFragment.getPosition());
203 intent.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, videoFragment.isPlaying());
204 }
205 // and maybe 'waiting to preview' flag
206 }
207 startActivity(intent);
208 }
209
210 @Override
211 public void onStop() {
212 super.onStop();
213 Log_OC.e(TAG, "onStop");
214 mStarted = false;
215 }
216 @Override
217 public void onPause() {
218 super.onPause();
219 Log_OC.e(TAG, "onPause");
220 if (mDownloadFinishReceiver != null) {
221 unregisterReceiver(mDownloadFinishReceiver);
222 mDownloadFinishReceiver = null;
223 }
224 }
225
226
227 @Override
228 public void onResume() {
229 super.onResume();
230 Log_OC.e(TAG, "onResume");
231 // TODO this is probably unnecessary
232 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
233 if (fragment != null && fragment instanceof FileDetailFragment) {
234 ((FileDetailFragment) fragment).updateFileDetails(false, false);
235 }
236
237 // Listen for download messages
238 IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE);
239 downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
240 mDownloadFinishReceiver = new DownloadFinishReceiver();
241 registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
242 }
243
244
245 /** Defines callbacks for service binding, passed to bindService() */
246 private class DetailsServiceConnection implements ServiceConnection {
247
248 @Override
249 public void onServiceConnected(ComponentName component, IBinder service) {
250
251 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
252 Log_OC.d(TAG, "Download service connected");
253 mDownloaderBinder = (FileDownloaderBinder) service;
254 if (mWaitingToPreview) {
255 requestForDownload();
256 }
257
258 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
259 Log_OC.d(TAG, "Upload service connected");
260 mUploaderBinder = (FileUploaderBinder) service;
261 } else {
262 return;
263 }
264
265 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
266 FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null;
267 if (detailsFragment != null) {
268 detailsFragment.listenForTransferProgress();
269 detailsFragment.updateFileDetails(mWaitingToPreview, false); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
270 }
271 }
272
273 @Override
274 public void onServiceDisconnected(ComponentName component) {
275 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
276 Log_OC.d(TAG, "Download service disconnected");
277 mDownloaderBinder = null;
278 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
279 Log_OC.d(TAG, "Upload service disconnected");
280 mUploaderBinder = null;
281 }
282 }
283 };
284
285
286 @Override
287 public void onDestroy() {
288 super.onDestroy();
289 Log_OC.e(TAG, "onDestroy");
290 if (mDownloadConnection != null) {
291 unbindService(mDownloadConnection);
292 mDownloadConnection = null;
293 }
294 if (mUploadConnection != null) {
295 unbindService(mUploadConnection);
296 mUploadConnection = null;
297 }
298 }
299
300
301 @Override
302 public boolean onOptionsItemSelected(MenuItem item) {
303 boolean returnValue = false;
304
305 switch(item.getItemId()){
306 case android.R.id.home:
307 //backToDisplayActivity();
308 onBackPressed();
309 returnValue = true;
310 break;
311 default:
312 returnValue = super.onOptionsItemSelected(item);
313 }
314
315 return returnValue;
316 }
317
318 //private void backToDisplayActivity() {
319 @Override
320 public void onBackPressed() {
321 Intent intent = new Intent(this, FileDisplayActivity.class);
322 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
323 OCFile targetFile = null;
324 if (mFile != null) {
325 targetFile = mStorageManager.getFileById(mFile.getParentId());
326 }
327 intent.putExtra(FileDetailFragment.EXTRA_FILE, targetFile);
328 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
329 startActivity(intent);
330 finish();
331 }
332
333 @Override
334 protected Dialog onCreateDialog(int id) {
335 Dialog dialog = null;
336 switch (id) {
337 case DIALOG_SHORT_WAIT: {
338 ProgressDialog working_dialog = new ProgressDialog(this);
339 working_dialog.setMessage(getResources().getString(
340 R.string.wait_a_moment));
341 working_dialog.setIndeterminate(true);
342 working_dialog.setCancelable(false);
343 dialog = working_dialog;
344 break;
345 }
346 default:
347 dialog = null;
348 }
349 return dialog;
350 }
351
352
353 /**
354 * {@inheritDoc}
355 */
356 @Override
357 public void onFileStateChanged() {
358 // nothing to do here!
359 }
360
361
362 /**
363 * {@inheritDoc}
364 */
365 @Override
366 public FileDownloaderBinder getFileDownloaderBinder() {
367 return mDownloaderBinder;
368 }
369
370
371 @Override
372 public FileUploaderBinder getFileUploaderBinder() {
373 return mUploaderBinder;
374 }
375
376
377 @Override
378 public void showFragmentWithDetails(OCFile file) {
379 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
380 transaction.replace(R.id.fragment, new FileDetailFragment(file, mAccount), FileDetailFragment.FTAG);
381 transaction.commit();
382 }
383
384
385 private void requestForDownload() {
386 if (!mDownloaderBinder.isDownloading(mAccount, mFile)) {
387 Intent i = new Intent(this, FileDownloader.class);
388 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
389 i.putExtra(FileDownloader.EXTRA_FILE, mFile);
390 startService(i);
391 }
392 }
393
394
395 /**
396 * Class waiting for broadcast events from the {@link FielDownloader} service.
397 *
398 * Updates the UI when a download is started or finished, provided that it is relevant for the
399 * current file.
400 */
401 private class DownloadFinishReceiver extends BroadcastReceiver {
402 @Override
403 public void onReceive(Context context, Intent intent) {
404 boolean sameAccount = isSameAccount(context, intent);
405 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
406 boolean samePath = (mFile != null && mFile.getRemotePath().equals(downloadedRemotePath));
407
408 if (sameAccount && samePath) {
409 updateChildFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));
410 }
411
412 removeStickyBroadcast(intent);
413 }
414
415 private boolean isSameAccount(Context context, Intent intent) {
416 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
417 return (accountName != null && accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name));
418 }
419 }
420
421
422 public void updateChildFragment(String downloadEvent, String downloadedRemotePath, boolean success) {
423 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
424 if (fragment != null && fragment instanceof FileDetailFragment) {
425 FileDetailFragment detailsFragment = (FileDetailFragment) fragment;
426 OCFile fileInFragment = detailsFragment.getFile();
427 if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {
428 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
429 mWaitingToPreview = false;
430
431 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {
432 // grants that the progress bar is updated
433 detailsFragment.listenForTransferProgress();
434 detailsFragment.updateFileDetails(true, false);
435
436 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {
437 // refresh the details fragment
438 if (success && mWaitingToPreview) {
439 mFile = mStorageManager.getFileById(mFile.getFileId()); // update the file from database, for the local storage path
440 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
441 transaction.replace(R.id.fragment, new PreviewMediaFragment(mFile, mAccount, 0, true), FileDetailFragment.FTAG);
442 transaction.commit();
443 mWaitingToPreview = false;
444
445 } else {
446 detailsFragment.updateFileDetails(false, (success));
447 // TODO error message if !success ¿?
448 }
449 }
450 } // TODO else if (fragment != null && fragment )
451
452 }
453
454 }