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