81e4fa9b284497df557d77e107485e6af45703c8
[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
208 } else {
209 intent = new Intent(this, FileDetailActivity.class);
210 intent .putExtra(EXTRA_FILE, file);
211 intent .putExtra(EXTRA_ACCOUNT, account);
212 intent.putExtra(EXTRA_MODE, getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW));
213 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
214 if (fragment != null && file != null && fragment instanceof PreviewMediaFragment && file.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 backToDisplayActivity(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 backToDisplayActivity(true);
334 }
335
336 private void backToDisplayActivity(boolean moveToParent) {
337 Intent intent = new Intent(this, FileDisplayActivity.class);
338 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
339 OCFile targetFile = null;
340 OCFile file = getFile();
341 if (file != null) {
342 targetFile = moveToParent ? mStorageManager.getFileById(file.getParentId()) : file;
343 }
344 intent.putExtra(EXTRA_FILE, targetFile);
345 intent.putExtra(EXTRA_ACCOUNT, getAccount());
346 startActivity(intent);
347 finish();
348 }
349
350 @Override
351 protected Dialog onCreateDialog(int id) {
352 Dialog dialog = null;
353 switch (id) {
354 case DIALOG_SHORT_WAIT: {
355 ProgressDialog working_dialog = new ProgressDialog(this);
356 working_dialog.setMessage(getResources().getString(
357 R.string.wait_a_moment));
358 working_dialog.setIndeterminate(true);
359 working_dialog.setCancelable(false);
360 dialog = working_dialog;
361 break;
362 }
363 default:
364 dialog = null;
365 }
366 return dialog;
367 }
368
369
370 /**
371 * {@inheritDoc}
372 */
373 @Override
374 public void onFileStateChanged() {
375 // nothing to do here!
376 }
377
378
379 /**
380 * {@inheritDoc}
381 */
382 @Override
383 public FileDownloaderBinder getFileDownloaderBinder() {
384 return mDownloaderBinder;
385 }
386
387
388 @Override
389 public FileUploaderBinder getFileUploaderBinder() {
390 return mUploaderBinder;
391 }
392
393
394 @Override
395 public void showFragmentWithDetails(OCFile file) {
396 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
397 transaction.replace(R.id.fragment, new FileDetailFragment(file, getAccount()), FileDetailFragment.FTAG);
398 transaction.commit();
399 }
400
401
402 private void requestForDownload() {
403 if (!mDownloaderBinder.isDownloading(getAccount(), getFile())) {
404 Intent i = new Intent(this, FileDownloader.class);
405 i.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount());
406 i.putExtra(FileDownloader.EXTRA_FILE, getFile());
407 startService(i);
408 }
409 }
410
411
412 /**
413 * Class waiting for broadcast events from the {@link FielDownloader} service.
414 *
415 * Updates the UI when a download is started or finished, provided that it is relevant for the
416 * current file.
417 */
418 private class DownloadFinishReceiver extends BroadcastReceiver {
419 @Override
420 public void onReceive(Context context, Intent intent) {
421 boolean sameAccount = isSameAccount(context, intent);
422 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
423 boolean samePath = (getFile() != null && getFile().getRemotePath().equals(downloadedRemotePath));
424
425 if (sameAccount && samePath) {
426 updateChildFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));
427 }
428
429 removeStickyBroadcast(intent);
430 }
431
432 private boolean isSameAccount(Context context, Intent intent) {
433 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
434 return (accountName != null && accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name));
435 }
436 }
437
438
439 public void updateChildFragment(String downloadEvent, String downloadedRemotePath, boolean success) {
440 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
441 if (fragment != null && fragment instanceof FileDetailFragment) {
442 FileDetailFragment detailsFragment = (FileDetailFragment) fragment;
443 OCFile fileInFragment = detailsFragment.getFile();
444 if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {
445 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
446 mWaitingToPreview = false;
447
448 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {
449 // grants that the progress bar is updated
450 detailsFragment.listenForTransferProgress();
451 detailsFragment.updateFileDetails(true, false);
452
453 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {
454 // refresh the details fragment
455 if (success && mWaitingToPreview) {
456 setFile(mStorageManager.getFileById(getFile().getFileId())); // update the file from database, for the local storage path
457 if (PreviewMediaFragment.canBePreviewed(getFile())) {
458 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
459 transaction.replace(R.id.fragment, new PreviewMediaFragment(getFile(), getAccount(), 0, true), FileDetailFragment.FTAG);
460 transaction.commit();
461 } else {
462 detailsFragment.updateFileDetails(false, (success));
463 }
464 mWaitingToPreview = false;
465
466 } else {
467 detailsFragment.updateFileDetails(false, (success));
468 }
469 }
470 } // TODO else if (fragment != null && fragment )
471
472 }
473
474 /**
475 * {@inheritDoc}
476 */
477 @Override
478 protected void onAccountChanged() {
479 mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
480
481 FileFragment fragment = (FileFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
482 if (fragment != null && mStorageManager.getFileById(fragment.getFile().getFileId()) == null) {
483 /// the account was forced to be changed; probably was deleted from system settings
484 backToDisplayActivity(false);
485 }
486 }
487
488 }