Copyright note fixes
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewImageActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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 version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17 package com.owncloud.android.ui.preview;
18
19 import org.apache.commons.httpclient.methods.PostMethod;
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.os.Bundle;
31 import android.os.IBinder;
32 import android.support.v4.app.Fragment;
33 import android.support.v4.view.ViewPager;
34 import android.util.Log;
35 import android.view.MotionEvent;
36 import android.view.View;
37 import android.view.View.OnTouchListener;
38
39 import com.actionbarsherlock.app.ActionBar;
40 import com.actionbarsherlock.app.SherlockFragmentActivity;
41 import com.actionbarsherlock.view.MenuItem;
42 import com.actionbarsherlock.view.Window;
43 import com.owncloud.android.datamodel.DataStorageManager;
44 import com.owncloud.android.datamodel.FileDataStorageManager;
45 import com.owncloud.android.datamodel.OCFile;
46 import com.owncloud.android.files.services.FileDownloader;
47 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
48 import com.owncloud.android.files.services.FileUploader;
49 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
50 import com.owncloud.android.ui.activity.FileDetailActivity;
51 import com.owncloud.android.ui.fragment.FileDetailFragment;
52 import com.owncloud.android.ui.fragment.FileFragment;
53
54 import com.owncloud.android.AccountUtils;
55 import com.owncloud.android.R;
56
57 /**
58 * Used as an utility to preview image files contained in an ownCloud account.
59 *
60 * @author David A. Velasco
61 */
62 public class PreviewImageActivity extends SherlockFragmentActivity implements FileFragment.ContainerActivity, ViewPager.OnPageChangeListener, OnTouchListener {
63
64 public static final int DIALOG_SHORT_WAIT = 0;
65
66 public static final String TAG = PreviewImageActivity.class.getSimpleName();
67
68 public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
69 private static final String KEY_WAITING_FOR_BINDER = "WAITING_FOR_BINDER";
70
71 private OCFile mFile;
72 private OCFile mParentFolder;
73 private Account mAccount;
74 private DataStorageManager mStorageManager;
75
76 private ViewPager mViewPager;
77 private PreviewImagePagerAdapter mPreviewImagePagerAdapter;
78
79 private FileDownloaderBinder mDownloaderBinder = null;
80 private ServiceConnection mDownloadConnection, mUploadConnection = null;
81 private FileUploaderBinder mUploaderBinder = null;
82
83 private boolean mRequestWaitingForBinder;
84
85 private DownloadFinishReceiver mDownloadFinishReceiver;
86
87 private boolean mFullScreen;
88
89
90 @Override
91 protected void onCreate(Bundle savedInstanceState) {
92 super.onCreate(savedInstanceState);
93
94 mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
95 mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
96 if (mFile == null) {
97 throw new IllegalStateException("Instanced with a NULL OCFile");
98 }
99 if (mAccount == null) {
100 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
101 }
102 if (!mFile.isImage()) {
103 throw new IllegalArgumentException("Non-image file passed as argument");
104 }
105 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
106 setContentView(R.layout.preview_image_activity);
107
108 ActionBar actionBar = getSupportActionBar();
109 actionBar.setDisplayHomeAsUpEnabled(true);
110 actionBar.setTitle(mFile.getFileName());
111 actionBar.hide();
112
113 mFullScreen = true;
114
115 mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
116 mParentFolder = mStorageManager.getFileById(mFile.getParentId());
117 if (mParentFolder == null) {
118 // should not be necessary
119 mParentFolder = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);
120 }
121
122 if (savedInstanceState != null) {
123 mRequestWaitingForBinder = savedInstanceState.getBoolean(KEY_WAITING_FOR_BINDER);
124 } else {
125 mRequestWaitingForBinder = false;
126 }
127
128 createViewPager();
129
130 }
131
132 private void createViewPager() {
133 mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), mParentFolder, mAccount, mStorageManager);
134 mViewPager = (ViewPager) findViewById(R.id.fragmentPager);
135 int position = mPreviewImagePagerAdapter.getFilePosition(mFile);
136 position = (position >= 0) ? position : 0;
137 mViewPager.setAdapter(mPreviewImagePagerAdapter);
138 mViewPager.setOnPageChangeListener(this);
139 mViewPager.setCurrentItem(position);
140 if (position == 0 && !mFile.isDown()) {
141 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
142 mRequestWaitingForBinder = true;
143 }
144 }
145
146
147 @Override
148 public void onStart() {
149 super.onStart();
150 mDownloadConnection = new PreviewImageServiceConnection();
151 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
152 mUploadConnection = new PreviewImageServiceConnection();
153 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
154 }
155
156 @Override
157 protected void onSaveInstanceState(Bundle outState) {
158 super.onSaveInstanceState(outState);
159 outState.putBoolean(KEY_WAITING_FOR_BINDER, mRequestWaitingForBinder);
160 }
161
162
163 /** Defines callbacks for service binding, passed to bindService() */
164 private class PreviewImageServiceConnection implements ServiceConnection {
165
166 @Override
167 public void onServiceConnected(ComponentName component, IBinder service) {
168
169 if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
170 mDownloaderBinder = (FileDownloaderBinder) service;
171 if (mRequestWaitingForBinder) {
172 mRequestWaitingForBinder = false;
173 Log.d(TAG, "Simulating reselection of current page after connection of download binder");
174 onPageSelected(mViewPager.getCurrentItem());
175 }
176
177 } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
178 Log.d(TAG, "Upload service connected");
179 mUploaderBinder = (FileUploaderBinder) service;
180 } else {
181 return;
182 }
183
184 }
185
186 @Override
187 public void onServiceDisconnected(ComponentName component) {
188 if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
189 Log.d(TAG, "Download service suddenly disconnected");
190 mDownloaderBinder = null;
191 } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
192 Log.d(TAG, "Upload service suddenly disconnected");
193 mUploaderBinder = null;
194 }
195 }
196 };
197
198
199 @Override
200 public void onStop() {
201 super.onStop();
202 if (mDownloadConnection != null) {
203 unbindService(mDownloadConnection);
204 mDownloadConnection = null;
205 }
206 if (mUploadConnection != null) {
207 unbindService(mUploadConnection);
208 mUploadConnection = null;
209 }
210 }
211
212
213 @Override
214 public void onDestroy() {
215 super.onDestroy();
216 }
217
218
219 @Override
220 public boolean onOptionsItemSelected(MenuItem item) {
221 boolean returnValue = false;
222
223 switch(item.getItemId()){
224 case android.R.id.home:
225 backToDisplayActivity();
226 returnValue = true;
227 break;
228 default:
229 returnValue = super.onOptionsItemSelected(item);
230 }
231
232 return returnValue;
233 }
234
235
236 @Override
237 protected void onResume() {
238 super.onResume();
239 //Log.e(TAG, "ACTIVITY, ONRESUME");
240 mDownloadFinishReceiver = new DownloadFinishReceiver();
241 IntentFilter filter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
242 filter.addAction(FileDownloader.DOWNLOAD_ADDED_MESSAGE);
243 registerReceiver(mDownloadFinishReceiver, filter);
244 }
245
246 @Override
247 protected void onPostResume() {
248 //Log.e(TAG, "ACTIVITY, ONPOSTRESUME");
249 super.onPostResume();
250 }
251
252 @Override
253 public void onPause() {
254 super.onPause();
255 unregisterReceiver(mDownloadFinishReceiver);
256 mDownloadFinishReceiver = null;
257 }
258
259
260 private void backToDisplayActivity() {
261 /*
262 Intent intent = new Intent(this, FileDisplayActivity.class);
263 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
264 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
265 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
266 startActivity(intent);
267 */
268 finish();
269 }
270
271
272 @Override
273 protected Dialog onCreateDialog(int id) {
274 Dialog dialog = null;
275 switch (id) {
276 case DIALOG_SHORT_WAIT: {
277 ProgressDialog working_dialog = new ProgressDialog(this);
278 working_dialog.setMessage(getResources().getString(
279 R.string.wait_a_moment));
280 working_dialog.setIndeterminate(true);
281 working_dialog.setCancelable(false);
282 dialog = working_dialog;
283 break;
284 }
285 default:
286 dialog = null;
287 }
288 return dialog;
289 }
290
291
292 /**
293 * {@inheritDoc}
294 */
295 @Override
296 public void onFileStateChanged() {
297 // nothing to do here!
298 }
299
300
301 /**
302 * {@inheritDoc}
303 */
304 @Override
305 public FileDownloaderBinder getFileDownloaderBinder() {
306 return mDownloaderBinder;
307 }
308
309
310 @Override
311 public FileUploaderBinder getFileUploaderBinder() {
312 return mUploaderBinder;
313 }
314
315
316 @Override
317 public void showFragmentWithDetails(OCFile file) {
318 Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
319 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
320 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
321 showDetailsIntent.putExtra(FileDetailActivity.EXTRA_MODE, FileDetailActivity.MODE_DETAILS);
322 startActivity(showDetailsIntent);
323 int pos = mPreviewImagePagerAdapter.getFilePosition(file);
324 file = mPreviewImagePagerAdapter.getFileAt(pos);
325
326 }
327
328
329 private void requestForDownload(OCFile file) {
330 if (mDownloaderBinder == null) {
331 Log.d(TAG, "requestForDownload called without binder to download service");
332
333 } else if (!mDownloaderBinder.isDownloading(mAccount, file)) {
334 Intent i = new Intent(this, FileDownloader.class);
335 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
336 i.putExtra(FileDownloader.EXTRA_FILE, file);
337 startService(i);
338 }
339 }
340
341 /**
342 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
343 *
344 * @param Position Position index of the new selected page
345 */
346 @Override
347 public void onPageSelected(int position) {
348 if (mDownloaderBinder == null) {
349 mRequestWaitingForBinder = true;
350
351 } else {
352 OCFile currentFile = mPreviewImagePagerAdapter.getFileAt(position);
353 getSupportActionBar().setTitle(currentFile.getFileName());
354 if (!currentFile.isDown()) {
355 if (!mPreviewImagePagerAdapter.pendingErrorAt(position)) {
356 requestForDownload(currentFile);
357 }
358 }
359 }
360 }
361
362 /**
363 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
364 * when the pager is automatically settling to the current page. when it is fully stopped/idle.
365 *
366 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
367 */
368 @Override
369 public void onPageScrollStateChanged(int state) {
370 }
371
372 /**
373 * This method will be invoked when the current page is scrolled, either as part of a programmatically
374 * initiated smooth scroll or a user initiated touch scroll.
375 *
376 * @param position Position index of the first page currently being displayed.
377 * Page position+1 will be visible if positionOffset is nonzero.
378 *
379 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
380 * @param positionOffsetPixels Value in pixels indicating the offset from position.
381 */
382 @Override
383 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
384 }
385
386
387 /**
388 * Class waiting for broadcast events from the {@link FielDownloader} service.
389 *
390 * Updates the UI when a download is started or finished, provided that it is relevant for the
391 * folder displayed in the gallery.
392 */
393 private class DownloadFinishReceiver extends BroadcastReceiver {
394 @Override
395 public void onReceive(Context context, Intent intent) {
396 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
397 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
398 if (mAccount.name.equals(accountName) &&
399 downloadedRemotePath != null) {
400
401 OCFile file = mStorageManager.getFileByPath(downloadedRemotePath);
402 int position = mPreviewImagePagerAdapter.getFilePosition(file);
403 boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
404 //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit();
405
406 if (position >= 0 && intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE)) {
407 if (downloadWasFine) {
408 mPreviewImagePagerAdapter.updateFile(position, file);
409
410 } else {
411 mPreviewImagePagerAdapter.updateWithDownloadError(position);
412 }
413 mPreviewImagePagerAdapter.notifyDataSetChanged(); // will trigger the creation of new fragments
414
415 } else {
416 Log.d(TAG, "Download finished, but the fragment is offscreen");
417 }
418
419 }
420 removeStickyBroadcast(intent);
421 }
422
423 }
424
425
426 @Override
427 public boolean onTouch(View v, MotionEvent event) {
428 if (event.getAction() == MotionEvent.ACTION_UP) {
429 toggleFullScreen();
430 }
431 return true;
432 }
433
434
435 private void toggleFullScreen() {
436 ActionBar actionBar = getSupportActionBar();
437 if (mFullScreen) {
438 actionBar.show();
439
440 } else {
441 actionBar.hide();
442
443 }
444 mFullScreen = !mFullScreen;
445 }
446
447
448 }