Disable change log; to remove in future
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDetailActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 package com.owncloud.android.ui.activity;
20
21 import android.accounts.Account;
22 import android.app.Dialog;
23 import android.app.ProgressDialog;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.ServiceConnection;
28 import android.content.res.Configuration;
29 import android.os.Bundle;
30 import android.os.IBinder;
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
44 import com.owncloud.android.R;
45
46 /**
47 * This activity displays the details of a file like its name, its size and so
48 * on.
49 *
50 * @author Bartek Przybylski
51 *
52 */
53 public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity {
54
55 public static final int DIALOG_SHORT_WAIT = 0;
56
57 public static final String TAG = FileDetailActivity.class.getSimpleName();
58
59 private boolean mConfigurationChangedToLandscape = false;
60 private FileDownloaderBinder mDownloaderBinder = null;
61 private ServiceConnection mDownloadConnection, mUploadConnection = null;
62 private FileUploaderBinder mUploaderBinder = null;
63
64
65 @Override
66 protected void onCreate(Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68
69 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
70 Configuration conf = getResources().getConfiguration();
71 mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE &&
72 (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
73 );
74
75 if (!mConfigurationChangedToLandscape) {
76 mDownloadConnection = new DetailsServiceConnection();
77 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
78 mUploadConnection = new DetailsServiceConnection();
79 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
80
81 setContentView(R.layout.file_activity_details);
82
83 ActionBar actionBar = getSupportActionBar();
84 actionBar.setDisplayHomeAsUpEnabled(true);
85
86 OCFile file = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
87 Account account = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
88 FileDetailFragment mFileDetail = new FileDetailFragment(file, account);
89
90 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
91 ft.replace(R.id.fragment, mFileDetail, FileDetailFragment.FTAG);
92 ft.commit();
93
94 } else {
95 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
96 }
97
98
99 }
100
101
102 /** Defines callbacks for service binding, passed to bindService() */
103 private class DetailsServiceConnection implements ServiceConnection {
104
105 @Override
106 public void onServiceConnected(ComponentName component, IBinder service) {
107 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
108 Log.d(TAG, "Download service connected");
109 mDownloaderBinder = (FileDownloaderBinder) service;
110 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
111 Log.d(TAG, "Upload service connected");
112 mUploaderBinder = (FileUploaderBinder) service;
113 } else {
114 return;
115 }
116 FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
117 if (fragment != null)
118 fragment.updateFileDetails(false); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
119 }
120
121 @Override
122 public void onServiceDisconnected(ComponentName component) {
123 if (component.equals(new ComponentName(FileDetailActivity.this, FileDownloader.class))) {
124 Log.d(TAG, "Download service disconnected");
125 mDownloaderBinder = null;
126 } else if (component.equals(new ComponentName(FileDetailActivity.this, FileUploader.class))) {
127 Log.d(TAG, "Upload service disconnected");
128 mUploaderBinder = null;
129 }
130 }
131 };
132
133
134 @Override
135 public void onDestroy() {
136 super.onDestroy();
137 if (mDownloadConnection != null) {
138 unbindService(mDownloadConnection);
139 mDownloadConnection = null;
140 }
141 if (mUploadConnection != null) {
142 unbindService(mUploadConnection);
143 mUploadConnection = null;
144 }
145 }
146
147
148 @Override
149 public boolean onOptionsItemSelected(MenuItem item) {
150 boolean returnValue = false;
151
152 switch(item.getItemId()){
153 case android.R.id.home:
154 backToDisplayActivity();
155 returnValue = true;
156 break;
157 default:
158 returnValue = super.onOptionsItemSelected(item);
159 }
160
161 return returnValue;
162 }
163
164
165
166 @Override
167 protected void onResume() {
168
169 super.onResume();
170 if (!mConfigurationChangedToLandscape) {
171 FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
172 fragment.updateFileDetails(false);
173 }
174 }
175
176
177 private void backToDisplayActivity() {
178 Intent intent = new Intent(this, FileDisplayActivity.class);
179 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
180 intent.putExtra(FileDetailFragment.EXTRA_FILE, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE));
181 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT));
182 startActivity(intent);
183 finish();
184 }
185
186
187 @Override
188 protected Dialog onCreateDialog(int id) {
189 Dialog dialog = null;
190 switch (id) {
191 case DIALOG_SHORT_WAIT: {
192 ProgressDialog working_dialog = new ProgressDialog(this);
193 working_dialog.setMessage(getResources().getString(
194 R.string.wait_a_moment));
195 working_dialog.setIndeterminate(true);
196 working_dialog.setCancelable(false);
197 dialog = working_dialog;
198 break;
199 }
200 default:
201 dialog = null;
202 }
203 return dialog;
204 }
205
206
207 /**
208 * {@inheritDoc}
209 */
210 @Override
211 public void onFileStateChanged() {
212 // nothing to do here!
213 }
214
215
216 /**
217 * {@inheritDoc}
218 */
219 @Override
220 public FileDownloaderBinder getFileDownloaderBinder() {
221 return mDownloaderBinder;
222 }
223
224
225 @Override
226 public FileUploaderBinder getFileUploaderBinder() {
227 return mUploaderBinder;
228 }
229
230 }