2ab8bcc950f2faa2ab81d0e65c64bc182619ff72
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDetailActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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 import android.util.Log;
35
36 import com.actionbarsherlock.app.ActionBar;
37 import com.actionbarsherlock.app.SherlockFragmentActivity;
38 import com.actionbarsherlock.view.MenuItem;
39 import com.owncloud.android.datamodel.FileDataStorageManager;
40 import com.owncloud.android.datamodel.OCFile;
41 import com.owncloud.android.files.services.FileDownloader;
42 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
43 import com.owncloud.android.files.services.FileUploader;
44 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
45 import com.owncloud.android.ui.fragment.FileDetailFragment;
46 import com.owncloud.android.ui.fragment.FileFragment;
47 import com.owncloud.android.ui.preview.PreviewMediaFragment;
48
49 import com.owncloud.android.AccountUtils;
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(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
119 }
120
121
122 }
123
124 /**
125 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
126 * the requested {@link Intent}.
127 */
128 private void createChildFragment() {
129 int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW);
130
131 Fragment newFragment = null;
132 if (PreviewMediaFragment.canBePreviewed(mFile) && mode == MODE_PREVIEW) {
133 if (mFile.isDown()) {
134 newFragment = new PreviewMediaFragment(mFile, mAccount);
135
136 } else {
137 newFragment = new FileDetailFragment(mFile, mAccount);
138 mWaitingToPreview = true;
139 }
140
141 } else {
142 newFragment = new FileDetailFragment(mFile, mAccount);
143 }
144 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
145 ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);
146 ft.commit();
147 }
148
149
150 @Override
151 protected void onSaveInstanceState(Bundle outState) {
152 super.onSaveInstanceState(outState);
153 outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
154 }
155
156
157 @Override
158 public void onPause() {
159 super.onPause();
160 if (mDownloadFinishReceiver != null) {
161 unregisterReceiver(mDownloadFinishReceiver);
162 mDownloadFinishReceiver = null;
163 }
164 }
165
166
167 @Override
168 public void onResume() {
169 super.onResume();
170 if (!mConfigurationChangedToLandscape) {
171 // TODO this is probably unnecessary
172 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
173 if (fragment != null && fragment instanceof FileDetailFragment) {
174 ((FileDetailFragment) fragment).updateFileDetails(false, false);
175 }
176 }
177 // Listen for download messages
178 IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.DOWNLOAD_ADDED_MESSAGE);
179 downloadIntentFilter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
180 mDownloadFinishReceiver = new DownloadFinishReceiver();
181 registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
182 }
183
184
185 /** Defines callbacks for service binding, passed to bindService() */
186 private class DetailsServiceConnection implements ServiceConnection {
187
188 @Override
189 public void onServiceConnected(ComponentName component, IBinder service) {
190
191 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
192 Log.d(TAG, "Download service connected");
193 mDownloaderBinder = (FileDownloaderBinder) service;
194 if (mWaitingToPreview) {
195 requestForDownload();
196 }
197
198 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
199 Log.d(TAG, "Upload service connected");
200 mUploaderBinder = (FileUploaderBinder) service;
201 } else {
202 return;
203 }
204
205 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
206 FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null;
207 if (detailsFragment != null) {
208 detailsFragment.listenForTransferProgress();
209 detailsFragment.updateFileDetails(mWaitingToPreview, false); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
210 }
211 }
212
213 @Override
214 public void onServiceDisconnected(ComponentName component) {
215 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
216 Log.d(TAG, "Download service disconnected");
217 mDownloaderBinder = null;
218 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
219 Log.d(TAG, "Upload service disconnected");
220 mUploaderBinder = null;
221 }
222 }
223 };
224
225
226 @Override
227 public void onDestroy() {
228 super.onDestroy();
229 if (mDownloadConnection != null) {
230 unbindService(mDownloadConnection);
231 mDownloadConnection = null;
232 }
233 if (mUploadConnection != null) {
234 unbindService(mUploadConnection);
235 mUploadConnection = null;
236 }
237 }
238
239
240 @Override
241 public boolean onOptionsItemSelected(MenuItem item) {
242 boolean returnValue = false;
243
244 switch(item.getItemId()){
245 case android.R.id.home:
246 backToDisplayActivity();
247 returnValue = true;
248 break;
249 default:
250 returnValue = super.onOptionsItemSelected(item);
251 }
252
253 return returnValue;
254 }
255
256
257
258 private void backToDisplayActivity() {
259 Intent intent = new Intent(this, FileDisplayActivity.class);
260 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
261 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
262 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
263 startActivity(intent);
264 finish();
265 }
266
267
268 @Override
269 protected Dialog onCreateDialog(int id) {
270 Dialog dialog = null;
271 switch (id) {
272 case DIALOG_SHORT_WAIT: {
273 ProgressDialog working_dialog = new ProgressDialog(this);
274 working_dialog.setMessage(getResources().getString(
275 R.string.wait_a_moment));
276 working_dialog.setIndeterminate(true);
277 working_dialog.setCancelable(false);
278 dialog = working_dialog;
279 break;
280 }
281 default:
282 dialog = null;
283 }
284 return dialog;
285 }
286
287
288 /**
289 * {@inheritDoc}
290 */
291 @Override
292 public void onFileStateChanged() {
293 // nothing to do here!
294 }
295
296
297 /**
298 * {@inheritDoc}
299 */
300 @Override
301 public FileDownloaderBinder getFileDownloaderBinder() {
302 return mDownloaderBinder;
303 }
304
305
306 @Override
307 public FileUploaderBinder getFileUploaderBinder() {
308 return mUploaderBinder;
309 }
310
311
312 @Override
313 public void showFragmentWithDetails(OCFile file) {
314 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
315 transaction.replace(R.id.fragment, new FileDetailFragment(file, mAccount), FileDetailFragment.FTAG);
316 transaction.commit();
317 }
318
319
320 private void requestForDownload() {
321 if (!mDownloaderBinder.isDownloading(mAccount, mFile)) {
322 Intent i = new Intent(this, FileDownloader.class);
323 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
324 i.putExtra(FileDownloader.EXTRA_FILE, mFile);
325 startService(i);
326 }
327 }
328
329
330 /**
331 * Class waiting for broadcast events from the {@link FielDownloader} service.
332 *
333 * Updates the UI when a download is started or finished, provided that it is relevant for the
334 * current file.
335 */
336 private class DownloadFinishReceiver extends BroadcastReceiver {
337 @Override
338 public void onReceive(Context context, Intent intent) {
339 boolean sameAccount = isSameAccount(context, intent);
340 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
341 boolean samePath = (mFile != null && mFile.getRemotePath().equals(downloadedRemotePath));
342
343 if (sameAccount && samePath) {
344 updateChildFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));
345 }
346
347 removeStickyBroadcast(intent);
348 }
349
350 private boolean isSameAccount(Context context, Intent intent) {
351 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
352 return (accountName != null && accountName.equals(AccountUtils.getCurrentOwnCloudAccount(context).name));
353 }
354 }
355
356
357 public void updateChildFragment(String downloadEvent, String downloadedRemotePath, boolean success) {
358 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
359 if (fragment != null && fragment instanceof FileDetailFragment) {
360 FileDetailFragment detailsFragment = (FileDetailFragment) fragment;
361 OCFile fileInFragment = detailsFragment.getFile();
362 if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {
363 // this never should happen; fileInFragment should be always equals to mFile, that was compared to downloadedRemotePath in DownloadReceiver
364 mWaitingToPreview = false;
365
366 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {
367 // grants that the progress bar is updated
368 detailsFragment.listenForTransferProgress();
369 detailsFragment.updateFileDetails(true, false);
370
371 } else if (downloadEvent.equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {
372 // refresh the details fragment
373 if (success && mWaitingToPreview) {
374 mFile = mStorageManager.getFileById(mFile.getFileId()); // update the file from database, for the local storage path
375 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
376 transaction.replace(R.id.fragment, new PreviewMediaFragment(mFile, mAccount), FileDetailFragment.FTAG);
377 transaction.commit();
378 mWaitingToPreview = false;
379
380 } else {
381 detailsFragment.updateFileDetails(false, (success));
382 // TODO error message if !success ¿?
383 }
384 }
385 } // TODO else if (fragment != null && fragment )
386
387
388 }
389
390 }