Merge branch 'develop' into oauth_login
[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.datamodel.FileDataStorageManager;
39 import com.owncloud.android.datamodel.OCFile;
40 import com.owncloud.android.files.services.FileDownloader;
41 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
42 import com.owncloud.android.files.services.FileUploader;
43 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
44 import com.owncloud.android.ui.fragment.FileDetailFragment;
45 import com.owncloud.android.ui.fragment.FileFragment;
46 import com.owncloud.android.ui.preview.PreviewMediaFragment;
47 import com.owncloud.android.AccountUtils;
48 import com.owncloud.android.Log_OC;
49
50 import com.owncloud.android.R;
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 boolean mConfigurationChangedToLandscape = false;
72 private FileDownloaderBinder mDownloaderBinder = null;
73 private ServiceConnection mDownloadConnection, mUploadConnection = null;
74 private FileUploaderBinder mUploaderBinder = null;
75 private boolean mWaitingToPreview;
76
77 private OCFile mFile;
78 private Account mAccount;
79
80 private FileDataStorageManager mStorageManager;
81 private DownloadFinishReceiver mDownloadFinishReceiver;
82
83
84 @Override
85 protected void onCreate(Bundle savedInstanceState) {
86 super.onCreate(savedInstanceState);
87
88 mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
89 mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
90 mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
91
92 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
93 Configuration conf = getResources().getConfiguration();
94 mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE &&
95 (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
96 );
97
98 if (!mConfigurationChangedToLandscape) {
99 setContentView(R.layout.file_activity_details);
100
101 ActionBar actionBar = getSupportActionBar();
102 actionBar.setDisplayHomeAsUpEnabled(true);
103
104 if (savedInstanceState == null) {
105 mWaitingToPreview = false;
106 createChildFragment();
107 } else {
108 mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW);
109 }
110
111 mDownloadConnection = new DetailsServiceConnection();
112 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
113 mUploadConnection = new DetailsServiceConnection();
114 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
115
116
117 } else {
118 backToDisplayActivity(false); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
119 }
120
121 }
122
123 /**
124 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
125 * the requested {@link Intent}.
126 */
127 private void createChildFragment() {
128 int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW);
129
130 Fragment newFragment = null;
131 if (PreviewMediaFragment.canBePreviewed(mFile) && mode == MODE_PREVIEW) {
132 if (mFile.isDown()) {
133 newFragment = new PreviewMediaFragment(mFile, mAccount);
134
135 } else {
136 newFragment = new FileDetailFragment(mFile, mAccount);
137 mWaitingToPreview = true;
138 }
139
140 } else {
141 newFragment = new FileDetailFragment(mFile, mAccount);
142 }
143 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
144 ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);
145 ft.commit();
146 }
147
148
149 @Override
150 protected void onSaveInstanceState(Bundle outState) {
151 super.onSaveInstanceState(outState);
152 outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
153 }
154
155
156 @Override
157 public void onPause() {
158 super.onPause();
159 if (mDownloadFinishReceiver != null) {
160 unregisterReceiver(mDownloadFinishReceiver);
161 mDownloadFinishReceiver = null;
162 }
163 }
164
165
166 @Override
167 public void onResume() {
168 super.onResume();
169 if (!mConfigurationChangedToLandscape) {
170 // TODO this is probably unnecessary
171 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
172 if (fragment != null && fragment instanceof FileDetailFragment) {
173 ((FileDetailFragment) fragment).updateFileDetails(false, false);
174 }
175 }
176 // Listen for download messages
177 IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE);
178 downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
179 mDownloadFinishReceiver = new DownloadFinishReceiver();
180 registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
181 }
182
183
184 /** Defines callbacks for service binding, passed to bindService() */
185 private class DetailsServiceConnection implements ServiceConnection {
186
187 @Override
188 public void onServiceConnected(ComponentName component, IBinder service) {
189
190 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
191 Log_OC.d(TAG, "Download service connected");
192 mDownloaderBinder = (FileDownloaderBinder) service;
193 if (mWaitingToPreview) {
194 requestForDownload();
195 }
196
197 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
198 Log_OC.d(TAG, "Upload service connected");
199 mUploaderBinder = (FileUploaderBinder) service;
200 } else {
201 return;
202 }
203
204 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
205 FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null;
206 if (detailsFragment != null) {
207 detailsFragment.listenForTransferProgress();
208 detailsFragment.updateFileDetails(mWaitingToPreview, false); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
209 }
210 }
211
212 @Override
213 public void onServiceDisconnected(ComponentName component) {
214 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
215 Log_OC.d(TAG, "Download service disconnected");
216 mDownloaderBinder = null;
217 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
218 Log_OC.d(TAG, "Upload service disconnected");
219 mUploaderBinder = null;
220 }
221 }
222 };
223
224
225 @Override
226 public void onDestroy() {
227 super.onDestroy();
228 if (mDownloadConnection != null) {
229 unbindService(mDownloadConnection);
230 mDownloadConnection = null;
231 }
232 if (mUploadConnection != null) {
233 unbindService(mUploadConnection);
234 mUploadConnection = null;
235 }
236 }
237
238
239 @Override
240 public boolean onOptionsItemSelected(MenuItem item) {
241 boolean returnValue = false;
242
243 switch(item.getItemId()){
244 case android.R.id.home:
245 backToDisplayActivity(true);
246 returnValue = true;
247 break;
248 default:
249 returnValue = super.onOptionsItemSelected(item);
250 }
251
252 return returnValue;
253 }
254
255 private void backToDisplayActivity(boolean moveToParent) {
256 Intent intent = new Intent(this, FileDisplayActivity.class);
257 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
258 OCFile targetFile = null;
259 if (mFile != null) {
260 targetFile = moveToParent ? mStorageManager.getFileById(mFile.getParentId()) : mFile;
261 }
262 intent.putExtra(FileDetailFragment.EXTRA_FILE, targetFile);
263 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
264 startActivity(intent);
265 finish();
266 }
267
268
269 @Override
270 protected Dialog onCreateDialog(int id) {
271 Dialog dialog = null;
272 switch (id) {
273 case DIALOG_SHORT_WAIT: {
274 ProgressDialog working_dialog = new ProgressDialog(this);
275 working_dialog.setMessage(getResources().getString(
276 R.string.wait_a_moment));
277 working_dialog.setIndeterminate(true);
278 working_dialog.setCancelable(false);
279 dialog = working_dialog;
280 break;
281 }
282 default:
283 dialog = null;
284 }
285 return dialog;
286 }
287
288
289 /**
290 * {@inheritDoc}
291 */
292 @Override
293 public void onFileStateChanged() {
294 // nothing to do here!
295 }
296
297
298 /**
299 * {@inheritDoc}
300 */
301 @Override
302 public FileDownloaderBinder getFileDownloaderBinder() {
303 return mDownloaderBinder;
304 }
305
306
307 @Override
308 public FileUploaderBinder getFileUploaderBinder() {
309 return mUploaderBinder;
310 }
311
312
313 @Override
314 public void showFragmentWithDetails(OCFile file) {
315 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
316 transaction.replace(R.id.fragment, new FileDetailFragment(file, mAccount), FileDetailFragment.FTAG);
317 transaction.commit();
318 }
319
320
321 private void requestForDownload() {
322 if (!mDownloaderBinder.isDownloading(mAccount, mFile)) {
323 Intent i = new Intent(this, FileDownloader.class);
324 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
325 i.putExtra(FileDownloader.EXTRA_FILE, mFile);
326 startService(i);
327 }
328 }
329
330
331 /**
332 * Class waiting for broadcast events from the {@link FielDownloader} service.
333 *
334 * Updates the UI when a download is started or finished, provided that it is relevant for the
335 * current file.
336 */
337 private class DownloadFinishReceiver extends BroadcastReceiver {
338 @Override
339 public void onReceive(Context context, Intent intent) {
340 boolean sameAccount = isSameAccount(context, intent);
341 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
342 boolean samePath = (mFile != null && mFile.getRemotePath().equals(downloadedRemotePath));
343
344 if (sameAccount && samePath) {
345 updateChildFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));
346 }
347
348 removeStickyBroadcast(intent);
349 }
350
351 private boolean isSameAccount(Context context, Intent intent) {
352 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
353 return (accountName != null && accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name));
354 }
355 }
356
357
358 public void updateChildFragment(String downloadEvent, String downloadedRemotePath, boolean success) {
359 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
360 if (fragment != null && fragment instanceof FileDetailFragment) {
361 FileDetailFragment detailsFragment = (FileDetailFragment) fragment;
362 OCFile fileInFragment = detailsFragment.getFile();
363 if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {
364 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
365 mWaitingToPreview = false;
366
367 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {
368 // grants that the progress bar is updated
369 detailsFragment.listenForTransferProgress();
370 detailsFragment.updateFileDetails(true, false);
371
372 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {
373 // refresh the details fragment
374 if (success && mWaitingToPreview) {
375 mFile = mStorageManager.getFileById(mFile.getFileId()); // update the file from database, for the local storage path
376 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
377 transaction.replace(R.id.fragment, new PreviewMediaFragment(mFile, mAccount), FileDetailFragment.FTAG);
378 transaction.commit();
379 mWaitingToPreview = false;
380
381 } else {
382 detailsFragment.updateFileDetails(false, (success));
383 // TODO error message if !success ¿?
384 }
385 }
386 } // TODO else if (fragment != null && fragment )
387
388 }
389
390 }