ba948d36e97af90f2f6c6b5bd5f6a5e10f56b58f
[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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 package com.owncloud.android.ui.activity;
20
21 import android.accounts.Account;
22 import android.app.Dialog;
23 import android.app.ProgressDialog;
24 import android.content.BroadcastReceiver;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.content.ServiceConnection;
30 import android.content.res.Configuration;
31 import android.os.Bundle;
32 import android.os.IBinder;
33 import android.support.v4.app.Fragment;
34 import android.support.v4.app.FragmentTransaction;
35 import android.util.Log;
36
37 import com.actionbarsherlock.app.ActionBar;
38 import com.actionbarsherlock.app.SherlockFragmentActivity;
39 import com.actionbarsherlock.view.MenuItem;
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
50 import com.owncloud.android.AccountUtils;
51 import com.owncloud.android.R;
52
53 /**
54 * This activity displays the details of a file like its name, its size and so
55 * on.
56 *
57 * @author Bartek Przybylski
58 * @author David A. Velasco
59 */
60 public class FileDetailActivity extends SherlockFragmentActivity implements FileFragment.ContainerActivity {
61
62 public static final int DIALOG_SHORT_WAIT = 0;
63
64 public static final String TAG = FileDetailActivity.class.getSimpleName();
65
66 public static final String EXTRA_MODE = "MODE";
67 public static final int MODE_DETAILS = 0;
68 public static final int MODE_PREVIEW = 1;
69
70 public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
71
72 private boolean mConfigurationChangedToLandscape = false;
73 private FileDownloaderBinder mDownloaderBinder = null;
74 private ServiceConnection mDownloadConnection, mUploadConnection = null;
75 private FileUploaderBinder mUploaderBinder = null;
76 private boolean mWaitingToPreview;
77
78 private OCFile mFile;
79 private Account mAccount;
80
81 private FileDataStorageManager mStorageManager;
82 private DownloadFinishReceiver mDownloadFinishReceiver;
83
84
85 @Override
86 protected void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88
89 mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
90 mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
91 mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
92
93 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
94 Configuration conf = getResources().getConfiguration();
95 mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE &&
96 (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
97 );
98
99 if (!mConfigurationChangedToLandscape) {
100 setContentView(R.layout.file_activity_details);
101
102 ActionBar actionBar = getSupportActionBar();
103 actionBar.setDisplayHomeAsUpEnabled(true);
104
105 if (savedInstanceState == null) {
106 mWaitingToPreview = false;
107 createChildFragment();
108 } else {
109 mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW);
110 }
111
112 mDownloadConnection = new DetailsServiceConnection();
113 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
114 mUploadConnection = new DetailsServiceConnection();
115 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
116
117
118 } else {
119 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
120 }
121
122
123 }
124
125 /**
126 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
127 * the requested {@link Intent}.
128 */
129 private void createChildFragment() {
130 int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW);
131
132 Fragment newFragment = null;
133 if (PreviewMediaFragment.canBePreviewed(mFile) && mode == MODE_PREVIEW) {
134 if (mFile.isDown()) {
135 newFragment = new PreviewMediaFragment(mFile, mAccount);
136
137 } else {
138 newFragment = new FileDetailFragment(mFile, mAccount);
139 mWaitingToPreview = true;
140 }
141
142 } else {
143 newFragment = new FileDetailFragment(mFile, mAccount);
144 }
145 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
146 ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);
147 ft.commit();
148 }
149
150
151 @Override
152 protected void onSaveInstanceState(Bundle outState) {
153 super.onSaveInstanceState(outState);
154 outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
155 }
156
157
158 @Override
159 public void onPause() {
160 super.onPause();
161 if (mDownloadFinishReceiver != null) {
162 unregisterReceiver(mDownloadFinishReceiver);
163 mDownloadFinishReceiver = null;
164 }
165 }
166
167
168 @Override
169 public void onResume() {
170 super.onResume();
171 if (!mConfigurationChangedToLandscape) {
172 // TODO this is probably unnecessary
173 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
174 if (fragment != null && fragment instanceof FileDetailFragment) {
175 ((FileDetailFragment) fragment).updateFileDetails(false, false);
176 }
177 }
178 // Listen for download messages
179 IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE);
180 downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
181 mDownloadFinishReceiver = new DownloadFinishReceiver();
182 registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
183 }
184
185
186 /** Defines callbacks for service binding, passed to bindService() */
187 private class DetailsServiceConnection implements ServiceConnection {
188
189 @Override
190 public void onServiceConnected(ComponentName component, IBinder service) {
191
192 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
193 Log.d(TAG, "Download service connected");
194 mDownloaderBinder = (FileDownloaderBinder) service;
195 if (mWaitingToPreview) {
196 requestForDownload();
197 }
198
199 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
200 Log.d(TAG, "Upload service connected");
201 mUploaderBinder = (FileUploaderBinder) service;
202 } else {
203 return;
204 }
205
206 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
207 FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null;
208 if (detailsFragment != null) {
209 detailsFragment.listenForTransferProgress();
210 detailsFragment.updateFileDetails(mWaitingToPreview, false); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
211 }
212 }
213
214 @Override
215 public void onServiceDisconnected(ComponentName component) {
216 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
217 Log.d(TAG, "Download service disconnected");
218 mDownloaderBinder = null;
219 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
220 Log.d(TAG, "Upload service disconnected");
221 mUploaderBinder = null;
222 }
223 }
224 };
225
226
227 @Override
228 public void onDestroy() {
229 super.onDestroy();
230 if (mDownloadConnection != null) {
231 unbindService(mDownloadConnection);
232 mDownloadConnection = null;
233 }
234 if (mUploadConnection != null) {
235 unbindService(mUploadConnection);
236 mUploadConnection = null;
237 }
238 }
239
240
241 @Override
242 public boolean onOptionsItemSelected(MenuItem item) {
243 boolean returnValue = false;
244
245 switch(item.getItemId()){
246 case android.R.id.home:
247 backToDisplayActivity();
248 returnValue = true;
249 break;
250 default:
251 returnValue = super.onOptionsItemSelected(item);
252 }
253
254 return returnValue;
255 }
256
257
258
259 private void backToDisplayActivity() {
260 Intent intent = new Intent(this, FileDisplayActivity.class);
261 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
262 OCFile targetFile = null;
263 if (mFile != null) {
264 targetFile = mStorageManager.getFileById(mFile.getParentId());
265 }
266 intent.putExtra(FileDetailFragment.EXTRA_FILE, targetFile);
267 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
268 startActivity(intent);
269 finish();
270 }
271
272
273 @Override
274 protected Dialog onCreateDialog(int id) {
275 Dialog dialog = null;
276 switch (id) {
277 case DIALOG_SHORT_WAIT: {
278 ProgressDialog working_dialog = new ProgressDialog(this);
279 working_dialog.setMessage(getResources().getString(
280 R.string.wait_a_moment));
281 working_dialog.setIndeterminate(true);
282 working_dialog.setCancelable(false);
283 dialog = working_dialog;
284 break;
285 }
286 default:
287 dialog = null;
288 }
289 return dialog;
290 }
291
292
293 /**
294 * {@inheritDoc}
295 */
296 @Override
297 public void onFileStateChanged() {
298 // nothing to do here!
299 }
300
301
302 /**
303 * {@inheritDoc}
304 */
305 @Override
306 public FileDownloaderBinder getFileDownloaderBinder() {
307 return mDownloaderBinder;
308 }
309
310
311 @Override
312 public FileUploaderBinder getFileUploaderBinder() {
313 return mUploaderBinder;
314 }
315
316
317 @Override
318 public void showFragmentWithDetails(OCFile file) {
319 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
320 transaction.replace(R.id.fragment, new FileDetailFragment(file, mAccount), FileDetailFragment.FTAG);
321 transaction.commit();
322 }
323
324
325 private void requestForDownload() {
326 if (!mDownloaderBinder.isDownloading(mAccount, mFile)) {
327 Intent i = new Intent(this, FileDownloader.class);
328 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
329 i.putExtra(FileDownloader.EXTRA_FILE, mFile);
330 startService(i);
331 }
332 }
333
334
335 /**
336 * Class waiting for broadcast events from the {@link FielDownloader} service.
337 *
338 * Updates the UI when a download is started or finished, provided that it is relevant for the
339 * current file.
340 */
341 private class DownloadFinishReceiver extends BroadcastReceiver {
342 @Override
343 public void onReceive(Context context, Intent intent) {
344 boolean sameAccount = isSameAccount(context, intent);
345 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
346 boolean samePath = (mFile != null && mFile.getRemotePath().equals(downloadedRemotePath));
347
348 if (sameAccount && samePath) {
349 updateChildFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));
350 }
351
352 removeStickyBroadcast(intent);
353 }
354
355 private boolean isSameAccount(Context context, Intent intent) {
356 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
357 return (accountName != null && accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name));
358 }
359 }
360
361
362 public void updateChildFragment(String downloadEvent, String downloadedRemotePath, boolean success) {
363 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
364 if (fragment != null && fragment instanceof FileDetailFragment) {
365 FileDetailFragment detailsFragment = (FileDetailFragment) fragment;
366 OCFile fileInFragment = detailsFragment.getFile();
367 if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {
368 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
369 mWaitingToPreview = false;
370
371 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {
372 // grants that the progress bar is updated
373 detailsFragment.listenForTransferProgress();
374 detailsFragment.updateFileDetails(true, false);
375
376 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {
377 // refresh the details fragment
378 if (success && mWaitingToPreview) {
379 mFile = mStorageManager.getFileById(mFile.getFileId()); // update the file from database, for the local storage path
380 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
381 transaction.replace(R.id.fragment, new PreviewMediaFragment(mFile, mAccount), FileDetailFragment.FTAG);
382 transaction.commit();
383 mWaitingToPreview = false;
384
385 } else {
386 detailsFragment.updateFileDetails(false, (success));
387 // TODO error message if !success ¿?
388 }
389 }
390 } // TODO else if (fragment != null && fragment )
391
392
393 }
394
395 }