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