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