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