8ff2bd9cf3ed28aa9468c2456fd7da9fdb1db00b
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDetailActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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 java.lang.ref.WeakReference;
21
22 import android.accounts.Account;
23 import android.app.Dialog;
24 import android.app.ProgressDialog;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.ServiceConnection;
29 import android.content.res.Configuration;
30 import android.os.Bundle;
31 import android.os.IBinder;
32 import android.support.v4.app.Fragment;
33 import android.support.v4.app.FragmentTransaction;
34 import android.util.Log;
35 import android.widget.ProgressBar;
36
37 import com.actionbarsherlock.app.ActionBar;
38 import com.actionbarsherlock.app.SherlockFragmentActivity;
39 import com.actionbarsherlock.view.MenuItem;
40 import com.owncloud.android.datamodel.OCFile;
41 import com.owncloud.android.files.services.FileDownloader;
42 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
43 import com.owncloud.android.files.services.FileUploader;
44 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
45 import com.owncloud.android.ui.fragment.FileDetailFragment;
46 import com.owncloud.android.ui.fragment.FilePreviewFragment;
47
48 import com.owncloud.android.R;
49
50 import eu.alefzero.webdav.OnDatatransferProgressListener;
51
52 /**
53 * This activity displays the details of a file like its name, its size and so
54 * on.
55 *
56 * @author Bartek Przybylski
57 * @author David A. Velasco
58 */
59 public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity {
60
61 public static final int DIALOG_SHORT_WAIT = 0;
62
63 public static final String TAG = FileDetailActivity.class.getSimpleName();
64
65 public static final String EXTRA_MODE = "MODE";
66 public static final int MODE_DETAILS = 0;
67 public static final int MODE_PREVIEW = 1;
68
69 private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
70
71 private boolean mConfigurationChangedToLandscape = false;
72 private FileDownloaderBinder mDownloaderBinder = null;
73 private ServiceConnection mDownloadConnection, mUploadConnection = null;
74 private FileUploaderBinder mUploaderBinder = null;
75 private boolean mWaitingToPreview;
76
77
78 @Override
79 protected void onCreate(Bundle savedInstanceState) {
80 super.onCreate(savedInstanceState);
81
82 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
83 Configuration conf = getResources().getConfiguration();
84 mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE &&
85 (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
86 );
87
88 if (!mConfigurationChangedToLandscape) {
89 mDownloadConnection = new DetailsServiceConnection();
90 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
91 mUploadConnection = new DetailsServiceConnection();
92 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
93
94 setContentView(R.layout.file_activity_details);
95
96 ActionBar actionBar = getSupportActionBar();
97 actionBar.setDisplayHomeAsUpEnabled(true);
98
99 if (savedInstanceState == null) {
100 mWaitingToPreview = false;
101 createChildFragment();
102 } else {
103 mWaitingToPreview = savedInstanceState.getBoolean(KEY_WAITING_TO_PREVIEW);
104 }
105
106 } else {
107 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
108 }
109
110
111 }
112
113 /**
114 * Creates the proper fragment depending upon the state of the handled {@link OCFile} and
115 * the requested {@link Intent}.
116 */
117 private void createChildFragment() {
118 OCFile file = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
119 Account account = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
120 int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_PREVIEW);
121
122 Fragment newFragment = null;
123 if (FilePreviewFragment.canBePreviewed(file) && mode == MODE_PREVIEW) {
124 if (file.isDown()) {
125 newFragment = new FilePreviewFragment(file, account);
126
127 } else {
128 newFragment = new FileDetailFragment(file, account);
129 mWaitingToPreview = true;
130 }
131
132 } else {
133 newFragment = new FileDetailFragment(file, account);
134 }
135 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
136 ft.replace(R.id.fragment, newFragment, FileDetailFragment.FTAG);
137 ft.commit();
138 }
139
140
141 @Override
142 protected void onSaveInstanceState(Bundle outState) {
143 outState.putBoolean(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
144 }
145
146
147 /** Defines callbacks for service binding, passed to bindService() */
148 private class DetailsServiceConnection implements ServiceConnection {
149
150 @Override
151 public void onServiceConnected(ComponentName component, IBinder service) {
152
153 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
154 Log.d(TAG, "Download service connected");
155 mDownloaderBinder = (FileDownloaderBinder) service;
156 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
157 Log.d(TAG, "Upload service connected");
158 mUploaderBinder = (FileUploaderBinder) service;
159 } else {
160 return;
161 }
162
163 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
164 FileDetailFragment detailsFragment = (fragment instanceof FileDetailFragment) ? (FileDetailFragment) fragment : null;
165 if (detailsFragment != null) {
166 detailsFragment.listenForTransferProgress();
167 detailsFragment.updateFileDetails(false); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
168 }
169 }
170
171 @Override
172 public void onServiceDisconnected(ComponentName component) {
173 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
174 Log.d(TAG, "Download service disconnected");
175 mDownloaderBinder = null;
176 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
177 Log.d(TAG, "Upload service disconnected");
178 mUploaderBinder = null;
179 }
180 }
181 };
182
183
184 @Override
185 public void onDestroy() {
186 super.onDestroy();
187 if (mDownloadConnection != null) {
188 unbindService(mDownloadConnection);
189 mDownloadConnection = null;
190 }
191 if (mUploadConnection != null) {
192 unbindService(mUploadConnection);
193 mUploadConnection = null;
194 }
195 }
196
197
198 @Override
199 public boolean onOptionsItemSelected(MenuItem item) {
200 boolean returnValue = false;
201
202 switch(item.getItemId()){
203 case android.R.id.home:
204 backToDisplayActivity();
205 returnValue = true;
206 break;
207 default:
208 returnValue = super.onOptionsItemSelected(item);
209 }
210
211 return returnValue;
212 }
213
214
215
216 @Override
217 protected void onResume() {
218
219 super.onResume();
220 if (!mConfigurationChangedToLandscape) {
221 Fragment fragment = getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
222 if (fragment != null && fragment instanceof FileDetailFragment) {
223 ((FileDetailFragment) fragment).updateFileDetails(false);
224 }
225 }
226 }
227
228
229 private void backToDisplayActivity() {
230 Intent intent = new Intent(this, FileDisplayActivity.class);
231 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
232 intent.putExtra(FileDetailFragment.EXTRA_FILE, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE));
233 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT));
234 startActivity(intent);
235 finish();
236 }
237
238
239 @Override
240 protected Dialog onCreateDialog(int id) {
241 Dialog dialog = null;
242 switch (id) {
243 case DIALOG_SHORT_WAIT: {
244 ProgressDialog working_dialog = new ProgressDialog(this);
245 working_dialog.setMessage(getResources().getString(
246 R.string.wait_a_moment));
247 working_dialog.setIndeterminate(true);
248 working_dialog.setCancelable(false);
249 dialog = working_dialog;
250 break;
251 }
252 default:
253 dialog = null;
254 }
255 return dialog;
256 }
257
258
259 /**
260 * {@inheritDoc}
261 */
262 @Override
263 public void onFileStateChanged() {
264 // nothing to do here!
265 }
266
267
268 /**
269 * {@inheritDoc}
270 */
271 @Override
272 public FileDownloaderBinder getFileDownloaderBinder() {
273 return mDownloaderBinder;
274 }
275
276
277 @Override
278 public FileUploaderBinder getFileUploaderBinder() {
279 return mUploaderBinder;
280 }
281
282
283 @Override
284 public void showFragmentWithDetails(OCFile file) {
285 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
286 transaction.replace(R.id.fragment, new FileDetailFragment(file, (Account) getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT)), FileDetailFragment.FTAG);
287 transaction.commit();
288 }
289
290 }