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