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