1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.ui
.activity
;
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
;
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
;
45 import com
.owncloud
.android
.AccountUtils
;
46 import com
.owncloud
.android
.R
;
49 * Used as an utility to preview image files contained in an ownCloud account.
51 * @author David A. Velasco
53 public class PreviewImageActivity
extends SherlockFragmentActivity
implements FileFragment
.ContainerActivity
{
55 public static final int DIALOG_SHORT_WAIT
= 0;
57 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
59 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
61 private FileDownloaderBinder mDownloaderBinder
= null
;
62 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
63 private FileUploaderBinder mUploaderBinder
= null
;
64 private boolean mWaitingToPreview
;
67 private Account mAccount
;
71 protected void onCreate(Bundle savedInstanceState
) {
72 super.onCreate(savedInstanceState
);
74 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
75 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
77 throw new IllegalStateException("Instanced with a NULL OCFile");
79 if (mAccount
== null
) {
80 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
82 if (!mFile
.isImage()) {
83 throw new IllegalArgumentException("Non-image file passed as argument");
86 setContentView(R
.layout
.preview_image_activity
);
88 ActionBar actionBar
= getSupportActionBar();
89 actionBar
.setDisplayHomeAsUpEnabled(true
);
90 actionBar
.setDisplayShowTitleEnabled(true
);
91 actionBar
.setTitle(mFile
.getFileName());
93 if (savedInstanceState
== null
) {
94 mWaitingToPreview
= false
;
95 createChildFragment();
97 mWaitingToPreview
= savedInstanceState
.getBoolean(KEY_WAITING_TO_PREVIEW
);
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
);
108 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
109 * the requested {@link Intent}.
111 private void createChildFragment() {
112 Fragment newFragment
= null
;
113 if (mFile
.isDown()) {
114 newFragment
= new FilePreviewFragment(mFile
, mAccount
);
117 newFragment
= new FileDetailFragment(mFile
, mAccount
);
118 mWaitingToPreview
= true
;
121 FragmentTransaction ft
= getSupportFragmentManager().beginTransaction();
122 ft
.replace(R
.id
.fragment
, newFragment
, FileDetailFragment
.FTAG
);
128 protected void onSaveInstanceState(Bundle outState
) {
129 super.onSaveInstanceState(outState
);
130 outState
.putBoolean(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
134 /** Defines callbacks for service binding, passed to bindService() */
135 private class DetailsServiceConnection
implements ServiceConnection
{
138 public void onServiceConnected(ComponentName component
, IBinder service
) {
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();
147 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
148 Log
.d(TAG
, "Upload service connected");
149 mUploaderBinder
= (FileUploaderBinder
) service
;
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())
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
;
176 public void onDestroy() {
178 if (mDownloadConnection
!= null
) {
179 unbindService(mDownloadConnection
);
180 mDownloadConnection
= null
;
182 if (mUploadConnection
!= null
) {
183 unbindService(mUploadConnection
);
184 mUploadConnection
= null
;
190 public boolean onOptionsItemSelected(MenuItem item
) {
191 boolean returnValue
= false
;
193 switch(item
.getItemId()){
194 case android
.R
.id
.home
:
195 backToDisplayActivity();
199 returnValue
= super.onOptionsItemSelected(item
);
207 protected void onResume() {
209 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
210 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
211 ((FileDetailFragment
) fragment
).updateFileDetails(false
);
216 private void backToDisplayActivity() {
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);
229 protected Dialog
onCreateDialog(int id
) {
230 Dialog dialog
= null
;
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
;
252 public void onFileStateChanged() {
253 // nothing to do here!
261 public FileDownloaderBinder
getFileDownloaderBinder() {
262 return mDownloaderBinder
;
267 public FileUploaderBinder
getFileUploaderBinder() {
268 return mUploaderBinder
;
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
);
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
);
291 public void notifySuccessfulDownload(OCFile file
, Intent intent
, boolean 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
;