12c133c52eceaa9a9f0ebe097d887562bfdfb7b1
[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 import com.owncloud.android.AccountUtils;
50 import com.owncloud.android.Log_OC;
51
52 import com.owncloud.android.R;
53
54 /**
55 * This activity displays the details of a file like its name, its size and so
56 * on.
57 *
58 * @author Bartek Przybylski
59 * @author David A. Velasco
60 */
61 public class FileDetailActivity extends SherlockFragmentActivity implements FileFragment.ContainerActivity {
62
63 public static final int DIALOG_SHORT_WAIT = 0;
64
65 public static final String TAG = FileDetailActivity.class.getSimpleName();
66
67 public static final String EXTRA_MODE = "MODE";
68 public static final int MODE_DETAILS = 0;
69 public static final int MODE_PREVIEW = 1;
70
71 public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
72
73 private boolean mConfigurationChangedToLandscape = false;
74 private FileDownloaderBinder mDownloaderBinder = null;
75 private ServiceConnection mDownloadConnection, mUploadConnection = null;
76 private FileUploaderBinder mUploaderBinder = null;
77 private boolean mWaitingToPreview;
78
79 private OCFile mFile;
80 private Account mAccount;
81
82 private FileDataStorageManager mStorageManager;
83 private DownloadFinishReceiver mDownloadFinishReceiver;
84
85
86 @Override
87 protected void onCreate(Bundle savedInstanceState) {
88 super.onCreate(savedInstanceState);
89
90 mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
91 mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
92 mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
93
94 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
95 Configuration conf = getResources().getConfiguration();
96 mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE &&
97 (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
98 );
99
100 if (!mConfigurationChangedToLandscape) {
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 } else {
120 backToDisplayActivity(false); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
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_OC.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_OC.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_OC.d(TAG, "Download service disconnected");
218 mDownloaderBinder = null;
219 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
220 Log_OC.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(true);
248 returnValue = true;
249 break;
250 default:
251 returnValue = super.onOptionsItemSelected(item);
252 }
253
254 return returnValue;
255 }
256
257 private void backToDisplayActivity(boolean moveToParent) {
258 Intent intent = new Intent(this, FileDisplayActivity.class);
259 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
260 OCFile targetFile = null;
261 if (mFile != null) {
262 targetFile = moveToParent ? mStorageManager.getFileById(mFile.getParentId()) : mFile;
263 }
264 intent.putExtra(FileDetailFragment.EXTRA_FILE, targetFile);
265 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
266 startActivity(intent);
267 finish();
268 }
269
270
271 @Override
272 protected Dialog onCreateDialog(int id) {
273 Dialog dialog = null;
274 switch (id) {
275 case DIALOG_SHORT_WAIT: {
276 ProgressDialog working_dialog = new ProgressDialog(this);
277 working_dialog.setMessage(getResources().getString(
278 R.string.wait_a_moment));
279 working_dialog.setIndeterminate(true);
280 working_dialog.setCancelable(false);
281 dialog = working_dialog;
282 break;
283 }
284 default:
285 dialog = null;
286 }
287 return dialog;
288 }
289
290
291 /**
292 * {@inheritDoc}
293 */
294 @Override
295 public void onFileStateChanged() {
296 // nothing to do here!
297 }
298
299
300 /**
301 * {@inheritDoc}
302 */
303 @Override
304 public FileDownloaderBinder getFileDownloaderBinder() {
305 return mDownloaderBinder;
306 }
307
308
309 @Override
310 public FileUploaderBinder getFileUploaderBinder() {
311 return mUploaderBinder;
312 }
313
314
315 @Override
316 public void showFragmentWithDetails(OCFile file) {
317 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
318 transaction.replace(R.id.fragment, new FileDetailFragment(file, mAccount), FileDetailFragment.FTAG);
319 transaction.commit();
320 }
321
322
323 private void requestForDownload() {
324 if (!mDownloaderBinder.isDownloading(mAccount, mFile)) {
325 Intent i = new Intent(this, FileDownloader.class);
326 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
327 i.putExtra(FileDownloader.EXTRA_FILE, mFile);
328 startService(i);
329 }
330 }
331
332
333 /**
334 * Class waiting for broadcast events from the {@link FielDownloader} service.
335 *
336 * Updates the UI when a download is started or finished, provided that it is relevant for the
337 * current file.
338 */
339 private class DownloadFinishReceiver extends BroadcastReceiver {
340 @Override
341 public void onReceive(Context context, Intent intent) {
342 boolean sameAccount = isSameAccount(context, intent);
343 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
344 boolean samePath = (mFile != null && mFile.getRemotePath().equals(downloadedRemotePath));
345
346 if (sameAccount && samePath) {
347 updateChildFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));
348 }
349
350 removeStickyBroadcast(intent);
351 }
352
353 private boolean isSameAccount(Context context, Intent intent) {
354 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
355 return (accountName != null && accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name));
356 }
357 }
358
359
360 public void updateChildFragment(String downloadEvent, String downloadedRemotePath, boolean success) {
361 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
362 if (fragment != null && fragment instanceof FileDetailFragment) {
363 FileDetailFragment detailsFragment = (FileDetailFragment) fragment;
364 OCFile fileInFragment = detailsFragment.getFile();
365 if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {
366 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
367 mWaitingToPreview = false;
368
369 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {
370 // grants that the progress bar is updated
371 detailsFragment.listenForTransferProgress();
372 detailsFragment.updateFileDetails(true, false);
373
374 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {
375 // refresh the details fragment
376 if (success && mWaitingToPreview) {
377 mFile = mStorageManager.getFileById(mFile.getFileId()); // update the file from database, for the local storage path
378 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
379 transaction.replace(R.id.fragment, new PreviewMediaFragment(mFile, mAccount), FileDetailFragment.FTAG);
380 transaction.commit();
381 mWaitingToPreview = false;
382
383 } else {
384 detailsFragment.updateFileDetails(false, (success));
385 // TODO error message if !success ¿?
386 }
387 }
388 } // TODO else if (fragment != null && fragment )
389
390 }
391
392 }