Fixed bug: show the selected image in the gallery, instead of always the first in...
[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.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.ServiceConnection;
27 import android.os.Bundle;
28 import android.os.IBinder;
29 import android.support.v4.view.ViewPager;
30 import android.util.Log;
31
32 import com.actionbarsherlock.app.ActionBar;
33 import com.actionbarsherlock.app.SherlockFragmentActivity;
34 import com.actionbarsherlock.view.MenuItem;
35 import com.owncloud.android.datamodel.DataStorageManager;
36 import com.owncloud.android.datamodel.FileDataStorageManager;
37 import com.owncloud.android.datamodel.OCFile;
38 import com.owncloud.android.files.services.FileDownloader;
39 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
40 import com.owncloud.android.files.services.FileUploader;
41 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
42 import com.owncloud.android.ui.activity.FileDetailActivity;
43 import com.owncloud.android.ui.fragment.FileDetailFragment;
44 import com.owncloud.android.ui.fragment.FileFragment;
45
46 import com.owncloud.android.AccountUtils;
47 import com.owncloud.android.R;
48
49 /**
50 * Used as an utility to preview image files contained in an ownCloud account.
51 *
52 * @author David A. Velasco
53 */
54 public class PreviewImageActivity extends SherlockFragmentActivity implements FileFragment.ContainerActivity, ViewPager.OnPageChangeListener {
55
56 public static final int DIALOG_SHORT_WAIT = 0;
57
58 public static final String TAG = PreviewImageActivity.class.getSimpleName();
59
60 public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
61
62 private OCFile mFile;
63 private OCFile mParentFolder;
64 private Account mAccount;
65 private DataStorageManager mStorageManager;
66
67 private ViewPager mViewPager;
68 private PreviewImagePagerAdapter mPreviewImagePagerAdapter;
69
70 private FileDownloaderBinder mDownloaderBinder = null;
71 private ServiceConnection mDownloadConnection, mUploadConnection = null;
72 private FileUploaderBinder mUploaderBinder = null;
73 private OCFile mWaitingToPreview = null;
74
75
76 @Override
77 protected void onCreate(Bundle savedInstanceState) {
78 super.onCreate(savedInstanceState);
79
80 mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
81 mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
82 if (mFile == null) {
83 throw new IllegalStateException("Instanced with a NULL OCFile");
84 }
85 if (mAccount == null) {
86 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
87 }
88 if (!mFile.isImage()) {
89 throw new IllegalArgumentException("Non-image file passed as argument");
90 }
91
92 setContentView(R.layout.preview_image_activity);
93
94 ActionBar actionBar = getSupportActionBar();
95 actionBar.setDisplayHomeAsUpEnabled(true);
96 actionBar.setTitle(mFile.getFileName());
97
98 mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
99 mParentFolder = mStorageManager.getFileById(mFile.getParentId());
100 if (mParentFolder == null) {
101 // should not be necessary
102 mParentFolder = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);
103 }
104
105 mWaitingToPreview = null;
106 if (savedInstanceState != null) {
107 mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(KEY_WAITING_TO_PREVIEW);
108 }
109
110 createViewPager();
111
112 mDownloadConnection = new PreviewImageServiceConnection();
113 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
114 mUploadConnection = new PreviewImageServiceConnection();
115 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
116
117 }
118
119 private void createViewPager() {
120 mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), mParentFolder, mAccount, mStorageManager);
121 mViewPager = (ViewPager) findViewById(R.id.fragmentPager);
122 int position = mPreviewImagePagerAdapter.getFilePosition(mFile);
123 Log.e(TAG, "Setting initial position " + position);
124 mViewPager.setAdapter(mPreviewImagePagerAdapter);
125 mViewPager.setOnPageChangeListener(this);
126 mViewPager.setCurrentItem((position >= 0) ? position : 0);
127 }
128
129
130 @Override
131 protected void onSaveInstanceState(Bundle outState) {
132 super.onSaveInstanceState(outState);
133 outState.putParcelable(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
134 }
135
136
137 /** Defines callbacks for service binding, passed to bindService() */
138 private class PreviewImageServiceConnection implements ServiceConnection {
139
140 @Override
141 public void onServiceConnected(ComponentName component, IBinder service) {
142
143 if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
144 Log.d(TAG, "Download service connected");
145 mDownloaderBinder = (FileDownloaderBinder) service;
146
147 } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
148 Log.d(TAG, "Upload service connected");
149 mUploaderBinder = (FileUploaderBinder) service;
150 } else {
151 return;
152 }
153
154 }
155
156 @Override
157 public void onServiceDisconnected(ComponentName component) {
158 if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
159 Log.d(TAG, "Download service suddenly disconnected");
160 mDownloaderBinder = null;
161 } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
162 Log.d(TAG, "Upload service suddenly disconnected");
163 mUploaderBinder = null;
164 }
165 }
166 };
167
168
169 @Override
170 public void onDestroy() {
171 super.onDestroy();
172 if (mDownloadConnection != null) {
173 unbindService(mDownloadConnection);
174 mDownloadConnection = null;
175 }
176 if (mUploadConnection != null) {
177 unbindService(mUploadConnection);
178 mUploadConnection = null;
179 }
180 }
181
182
183 @Override
184 public boolean onOptionsItemSelected(MenuItem item) {
185 boolean returnValue = false;
186
187 switch(item.getItemId()){
188 case android.R.id.home:
189 backToDisplayActivity();
190 returnValue = true;
191 break;
192 default:
193 returnValue = super.onOptionsItemSelected(item);
194 }
195
196 return returnValue;
197 }
198
199
200 @Override
201 protected void onResume() {
202 super.onResume();
203 }
204
205
206 private void backToDisplayActivity() {
207 /*
208 Intent intent = new Intent(this, FileDisplayActivity.class);
209 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
210 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
211 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
212 startActivity(intent);
213 */
214 finish();
215 }
216
217
218 @Override
219 protected Dialog onCreateDialog(int id) {
220 Dialog dialog = null;
221 switch (id) {
222 case DIALOG_SHORT_WAIT: {
223 ProgressDialog working_dialog = new ProgressDialog(this);
224 working_dialog.setMessage(getResources().getString(
225 R.string.wait_a_moment));
226 working_dialog.setIndeterminate(true);
227 working_dialog.setCancelable(false);
228 dialog = working_dialog;
229 break;
230 }
231 default:
232 dialog = null;
233 }
234 return dialog;
235 }
236
237
238 /**
239 * {@inheritDoc}
240 */
241 @Override
242 public void onFileStateChanged() {
243 // nothing to do here!
244 }
245
246
247 /**
248 * {@inheritDoc}
249 */
250 @Override
251 public FileDownloaderBinder getFileDownloaderBinder() {
252 return mDownloaderBinder;
253 }
254
255
256 @Override
257 public FileUploaderBinder getFileUploaderBinder() {
258 return mUploaderBinder;
259 }
260
261
262 @Override
263 public void showFragmentWithDetails(OCFile file) {
264 Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
265 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
266 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
267 showDetailsIntent.putExtra(FileDetailActivity.EXTRA_MODE, FileDetailActivity.MODE_DETAILS);
268 startActivity(showDetailsIntent);
269 }
270
271
272 private void requestForDownload() {
273 Log.e(TAG, "REQUEST FOR DOWNLOAD : " + mWaitingToPreview.getFileName());
274 if (!mDownloaderBinder.isDownloading(mAccount, mWaitingToPreview)) {
275 Intent i = new Intent(this, FileDownloader.class);
276 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
277 i.putExtra(FileDownloader.EXTRA_FILE, mWaitingToPreview);
278 startService(i);
279 }
280 }
281
282 @Override
283 public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {
284 if (success) {
285 if (mWaitingToPreview != null && mWaitingToPreview.equals(file)) {
286 mWaitingToPreview = null;
287 int position = mViewPager.getCurrentItem();
288 mPreviewImagePagerAdapter.updateFile(position, file);
289 Log.e(TAG, "BEFORE NOTIFY DATA SET CHANGED");
290 mPreviewImagePagerAdapter.notifyDataSetChanged();
291 Log.e(TAG, "AFTER NOTIFY DATA SET CHANGED");
292 //Log.e(TAG, "BEFORE INVALIDATE");
293 //mViewPager.postInvalidate();
294 //Log.e(TAG, "AFTER INVALIDATE");
295 }
296 }
297 }
298
299
300 /**
301 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
302 *
303 * @param Position Position index of the new selected page
304 */
305 @Override
306 public void onPageSelected(int position) {
307 Log.e(TAG, "onPageSelected " + position);
308 OCFile currentFile = mPreviewImagePagerAdapter.getFileAt(position);
309 getSupportActionBar().setTitle(currentFile.getFileName());
310 if (currentFile.isDown()) {
311 mWaitingToPreview = null;
312 } else {
313 mWaitingToPreview = currentFile;
314 requestForDownload();
315 mViewPager.invalidate();
316 }
317 }
318
319 /**
320 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
321 * when the pager is automatically settling to the current page, or when it is fully stopped/idle.
322 *
323 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
324 */
325 @Override
326 public void onPageScrollStateChanged(int state) {
327 }
328
329 /**
330 * This method will be invoked when the current page is scrolled, either as part of a programmatically
331 * initiated smooth scroll or a user initiated touch scroll.
332 *
333 * @param position Position index of the first page currently being displayed.
334 * Page position+1 will be visible if positionOffset is nonzero.
335 *
336 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
337 * @param positionOffsetPixels Value in pixels indicating the offset from position.
338 */
339 @Override
340 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
341 }
342
343 }