Force preview of images in 'full screen'
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / 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 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.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.app.Fragment;
30 import android.support.v4.app.FragmentTransaction;
31 import android.util.Log;
32
33 import com.actionbarsherlock.app.ActionBar;
34 import com.actionbarsherlock.app.SherlockFragmentActivity;
35 import com.actionbarsherlock.view.MenuItem;
36 import com.owncloud.android.datamodel.OCFile;
37 import com.owncloud.android.files.services.FileDownloader;
38 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
39 import com.owncloud.android.files.services.FileUploader;
40 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
41 import com.owncloud.android.ui.fragment.FileDetailFragment;
42 import com.owncloud.android.ui.fragment.FileFragment;
43 import com.owncloud.android.ui.fragment.FilePreviewFragment;
44
45 import com.owncloud.android.AccountUtils;
46 import com.owncloud.android.R;
47
48 /**
49 * Used as an utility to preview image files contained in an ownCloud account.
50 *
51 * @author David A. Velasco
52 */
53 public class PreviewImageActivity extends SherlockFragmentActivity implements FileFragment.ContainerActivity {
54
55 public static final int DIALOG_SHORT_WAIT = 0;
56
57 public static final String TAG = PreviewImageActivity.class.getSimpleName();
58
59 public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
60
61 private FileDownloaderBinder mDownloaderBinder = null;
62 private ServiceConnection mDownloadConnection, mUploadConnection = null;
63 private FileUploaderBinder mUploaderBinder = null;
64 private boolean mWaitingToPreview;
65
66 private OCFile mFile;
67 private Account mAccount;
68
69
70 @Override
71 protected void onCreate(Bundle savedInstanceState) {
72 super.onCreate(savedInstanceState);
73
74 mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
75 mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
76 if (mFile == null) {
77 throw new IllegalStateException("Instanced with a NULL OCFile");
78 }
79 if (mAccount == null) {
80 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
81 }
82 if (!mFile.isImage()) {
83 throw new IllegalArgumentException("Non-image file passed as argument");
84 }
85
86 setContentView(R.layout.preview_image_activity);
87
88 ActionBar actionBar = getSupportActionBar();
89 actionBar.setDisplayHomeAsUpEnabled(true);
90 actionBar.setDisplayShowTitleEnabled(true);
91 actionBar.setTitle(mFile.getFileName());
92
93 if (savedInstanceState == null) {
94 mWaitingToPreview = false;
95 createChildFragment();
96 } else {
97 mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW);
98 }
99
100 mDownloadConnection = new DetailsServiceConnection();
101 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
102 mUploadConnection = new DetailsServiceConnection();
103 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
104
105 }
106
107 /**
108 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
109 * the requested {@link Intent}.
110 */
111 private void createChildFragment() {
112 Fragment newFragment = null;
113 if (mFile.isDown()) {
114 newFragment = new FilePreviewFragment(mFile, mAccount);
115
116 } else {
117 newFragment = new FileDetailFragment(mFile, mAccount);
118 mWaitingToPreview = true;
119 }
120
121 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
122 ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);
123 ft.commit();
124 }
125
126
127 @Override
128 protected void onSaveInstanceState(Bundle outState) {
129 super.onSaveInstanceState(outState);
130 outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
131 }
132
133
134 /** Defines callbacks for service binding, passed to bindService() */
135 private class DetailsServiceConnection implements ServiceConnection {
136
137 @Override
138 public void onServiceConnected(ComponentName component, IBinder service) {
139
140 if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
141 Log.d(TAG, "Download service connected");
142 mDownloaderBinder = (FileDownloaderBinder) service;
143 if (mWaitingToPreview) {
144 requestForDownload();
145 }
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 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
155 FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null;
156 if (detailsFragment != null) {
157 detailsFragment.listenForTransferProgress();
158 detailsFragment.updateFileDetails(mWaitingToPreview); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
159 }
160 }
161
162 @Override
163 public void onServiceDisconnected(ComponentName component) {
164 if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
165 Log.d(TAG, "Download service disconnected");
166 mDownloaderBinder = null;
167 } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
168 Log.d(TAG, "Upload service disconnected");
169 mUploaderBinder = null;
170 }
171 }
172 };
173
174
175 @Override
176 public void onDestroy() {
177 super.onDestroy();
178 if (mDownloadConnection != null) {
179 unbindService(mDownloadConnection);
180 mDownloadConnection = null;
181 }
182 if (mUploadConnection != null) {
183 unbindService(mUploadConnection);
184 mUploadConnection = null;
185 }
186 }
187
188
189 @Override
190 public boolean onOptionsItemSelected(MenuItem item) {
191 boolean returnValue = false;
192
193 switch(item.getItemId()){
194 case android.R.id.home:
195 backToDisplayActivity();
196 returnValue = true;
197 break;
198 default:
199 returnValue = super.onOptionsItemSelected(item);
200 }
201
202 return returnValue;
203 }
204
205
206 @Override
207 protected void onResume() {
208 super.onResume();
209 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
210 if (fragment != null && fragment instanceof FileDetailFragment) {
211 ((FileDetailFragment) fragment).updateFileDetails(false);
212 }
213 }
214
215
216 private void backToDisplayActivity() {
217 /*
218 Intent intent = new Intent(this, FileDisplayActivity.class);
219 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
220 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
221 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
222 startActivity(intent);
223 */
224 finish();
225 }
226
227
228 @Override
229 protected Dialog onCreateDialog(int id) {
230 Dialog dialog = null;
231 switch (id) {
232 case DIALOG_SHORT_WAIT: {
233 ProgressDialog working_dialog = new ProgressDialog(this);
234 working_dialog.setMessage(getResources().getString(
235 R.string.wait_a_moment));
236 working_dialog.setIndeterminate(true);
237 working_dialog.setCancelable(false);
238 dialog = working_dialog;
239 break;
240 }
241 default:
242 dialog = null;
243 }
244 return dialog;
245 }
246
247
248 /**
249 * {@inheritDoc}
250 */
251 @Override
252 public void onFileStateChanged() {
253 // nothing to do here!
254 }
255
256
257 /**
258 * {@inheritDoc}
259 */
260 @Override
261 public FileDownloaderBinder getFileDownloaderBinder() {
262 return mDownloaderBinder;
263 }
264
265
266 @Override
267 public FileUploaderBinder getFileUploaderBinder() {
268 return mUploaderBinder;
269 }
270
271
272 @Override
273 public void showFragmentWithDetails(OCFile file) {
274 Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
275 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
276 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
277 startActivity(showDetailsIntent);
278 }
279
280
281 private void requestForDownload() {
282 if (!mDownloaderBinder.isDownloading(mAccount, mFile)) {
283 Intent i = new Intent(this, FileDownloader.class);
284 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
285 i.putExtra(FileDownloader.EXTRA_FILE, mFile);
286 startService(i);
287 }
288 }
289
290 @Override
291 public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {
292 if (success) {
293 if (mWaitingToPreview) {
294 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
295 transaction.replace(R.id.fragment, new FilePreviewFragment(file, mAccount), FileDetailFragment.FTAG);
296 transaction.commit();
297 mWaitingToPreview = false;
298 }
299 }
300 }
301
302 }