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