ArViewer  Android Version
Arvos - Augmented reality viewer open source
 All Classes Namespaces Files Functions Variables
ArvosHttpRequest.java
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013, Peter Graf
3 
4  This file is part of Arvos - AR Viewer Open Source for Android.
5  Arvos is free software.
6 
7  This program is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20  For more information on the AR Viewer Open Source or Peter Graf,
21  please see: http://www.mission-base.com/.
22  */
23 
24 package com.mission_base.arviewer_android;
25 
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.io.UnsupportedEncodingException;
31 import java.net.URLEncoder;
32 
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.client.HttpClient;
36 import org.apache.http.client.methods.HttpGet;
37 import org.apache.http.impl.client.DefaultHttpClient;
38 
39 import android.content.Context;
40 import android.graphics.Bitmap;
41 import android.graphics.BitmapFactory;
42 import android.os.AsyncTask;
43 import android.util.Pair;
44 
51 public class ArvosHttpRequest
52 {
53  private Arvos mInstance;
54  private IArvosHttpReceiver mReceiver;
55  private Context mContext;
56 
65  public ArvosHttpRequest(IArvosHttpReceiver receiver, Context context)
66  {
67  mInstance = Arvos.getInstance();
68  mReceiver = receiver;
69  mContext = context;
70  }
71 
78  public void getText(String url)
79  {
80  new DownloadTextTask().execute(url);
81  }
82 
89  public void getImage(String url)
90  {
91  new DownloadImageTask().execute(url);
92  }
93 
94  private String downloadText(String url)
95  {
96  float latitude = mInstance.mLatitude;
97  float longitude = mInstance.mLongitude;
98  float azimuth = mInstance.mCorrectedAzimuth;
99 
100  InputStream inputStream = null;
101  if (mInstance.mSimulateWeb)
102  {
103  try
104  {
105  if (url.contains("augments.json"))
106  {
107  inputStream = mContext.getResources().openRawResource(R.raw.augments);
108  }
109  else if (url.contains("augment1.json"))
110  {
111  inputStream = mContext.getResources().openRawResource(R.raw.augment1);
112  }
113  else if (url.contains("augment2.json"))
114  {
115  inputStream = mContext.getResources().openRawResource(R.raw.augment2);
116  }
117  else if (url.contains("augment3.json"))
118  {
119  inputStream = mContext.getResources().openRawResource(R.raw.augment3);
120  }
121  else if (url.contains("augment4.json"))
122  {
123  inputStream = mContext.getResources().openRawResource(R.raw.augment4);
124  }
125 
126  if (inputStream != null)
127  {
128  StringBuilder stringBuilder = new StringBuilder();
129  stringBuilder.append("OK");
130 
131  BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
132  String line;
133  while ((line = reader.readLine()) != null)
134  {
135  line = line.trim();
136  if (!line.startsWith("#"))
137  {
138  stringBuilder.append(line);
139  }
140  }
141  return stringBuilder.toString();
142  }
143  }
144  catch (Exception e)
145  {
146  return "ERException. " + e.getLocalizedMessage();
147  }
148  finally
149  {
150  if (inputStream != null)
151  {
152  try
153  {
154  inputStream.close();
155  }
156  catch (IOException e)
157  {
158  }
159  }
160  }
161  }
162 
163  StringBuilder stringBuilder = new StringBuilder();
164  stringBuilder.append("id=");
165  stringBuilder.append(mInstance.mSessionId == null ? "" : mInstance.mSessionId);
166  stringBuilder.append("&lat=");
167  stringBuilder.append(latitude);
168  stringBuilder.append("&lon=");
169  stringBuilder.append(longitude);
170  stringBuilder.append("&azi=");
171  stringBuilder.append(azimuth);
172 
173  boolean isAuthor = mInstance.mIsAuthor;
174  stringBuilder.append("&aut=");
175  stringBuilder.append(isAuthor);
176  stringBuilder.append("&ver=");
177  stringBuilder.append(mInstance.mVersion);
178 
179  if (mInstance.mAugmentsUrl.equals(url))
180  {
181  String key = mInstance.mAuthorKey;
182  if (isAuthor && key != null && key.length() >= 20)
183  {
184  stringBuilder.append("&akey=");
185  stringBuilder.append(urlEncode(key));
186  }
187 
188  key = mInstance.mDeveloperKey;
189  if (key != null && key.length() > 0)
190  {
191  stringBuilder.append("&dkey=");
192  stringBuilder.append(urlEncode(key));
193  }
194  }
195 
196  if (url.contains("?"))
197  {
198  url = url.replaceFirst("?", "?" + stringBuilder.toString() + "&");
199  }
200  else if (url.contains("#"))
201  {
202  url = url.replaceFirst("#", "#" + stringBuilder.toString() + "&");
203  }
204  else
205  {
206  url = url + "#" + stringBuilder.toString();
207  }
208 
209  stringBuilder = new StringBuilder();
210  stringBuilder.append("OK");
211 
212  try
213  {
214  HttpClient httpClient = new DefaultHttpClient();
215  HttpResponse response = httpClient.execute(new HttpGet(url));
216  int statusCode = response.getStatusLine().getStatusCode();
217  if (statusCode == 200)
218  {
219  HttpEntity entity = response.getEntity();
220  inputStream = entity.getContent();
221  BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
222  String line;
223  while ((line = reader.readLine()) != null)
224  {
225  line = line.trim();
226  if (!line.startsWith("#"))
227  {
228  stringBuilder.append(line);
229  }
230  }
231  }
232  else
233  {
234  return "ERHTTP error status " + statusCode;
235  }
236  }
237  catch (Exception e)
238  {
239  return "ERNetwork error. " + e.getLocalizedMessage();
240  }
241  finally
242  {
243  if (inputStream != null)
244  {
245  try
246  {
247  inputStream.close();
248  }
249  catch (IOException e)
250  {
251  }
252  }
253  }
254  return stringBuilder.toString();
255  }
256 
264  public static String urlEncode(String in)
265  {
266  try
267  {
268  return URLEncoder.encode(in, "UTF-8");
269  }
270  catch (UnsupportedEncodingException e)
271  {
272  return "";
273  }
274  }
275 
276  private static InputStream openHttpGETConnection(String url) throws Exception
277  {
278  InputStream inputStream = null;
279 
280  HttpClient httpclient = new DefaultHttpClient();
281  HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
282  inputStream = httpResponse.getEntity().getContent();
283 
284  return inputStream;
285  }
286 
287  private Pair<String, Bitmap> downloadImage(String url)
288  {
289  Bitmap bitmap;
290  try
291  {
292  bitmap = ArvosCache.getBitmap(url);
293  if (bitmap != null)
294  {
295  return new Pair<String, Bitmap>("OK", bitmap);
296  }
297  }
298  catch (Exception e)
299  {
300  return new Pair<String, Bitmap>("ERCache read error. " + e.getLocalizedMessage(), null);
301  }
302 
303  InputStream inputStream = null;
304  if (mInstance.mSimulateWeb)
305  {
306  try
307  {
308  if (url.contains("one.png"))
309  {
310  inputStream = mContext.getResources().openRawResource(R.raw.one);
311  }
312  else if (url.contains("two.png"))
313  {
314  inputStream = mContext.getResources().openRawResource(R.raw.two);
315  }
316  else if (url.contains("three.png"))
317  {
318  inputStream = mContext.getResources().openRawResource(R.raw.three);
319  }
320 
321  if (inputStream != null)
322  {
323  bitmap = BitmapFactory.decodeStream(inputStream);
324  try
325  {
326  ArvosCache.add(url, bitmap);
327  }
328  catch (Exception e)
329  {
330  return new Pair<String, Bitmap>("ERCache write error. " + e.getLocalizedMessage(), null);
331  }
332 
333  return new Pair<String, Bitmap>("OK", bitmap);
334  }
335  }
336  catch (Exception e)
337  {
338  return new Pair<String, Bitmap>("ERException. " + e.getLocalizedMessage(), null);
339  }
340  finally
341  {
342  if (inputStream != null)
343  {
344  try
345  {
346  inputStream.close();
347  }
348  catch (IOException e)
349  {
350  }
351  }
352  }
353  }
354 
355  try
356  {
357  inputStream = openHttpGETConnection(url);
358  bitmap = BitmapFactory.decodeStream(inputStream);
359  try
360  {
361  ArvosCache.add(url, bitmap);
362  }
363  catch (Exception e)
364  {
365  return new Pair<String, Bitmap>("ERCache write error. " + e.getLocalizedMessage(), null);
366  }
367 
368  return new Pair<String, Bitmap>("OK", bitmap);
369  }
370  catch (Exception e)
371  {
372  return new Pair<String, Bitmap>("ERNetwork error. " + e.getLocalizedMessage(), null);
373  }
374  finally
375  {
376  if (inputStream != null)
377  {
378  try
379  {
380  inputStream.close();
381  }
382  catch (IOException e)
383  {
384  }
385  }
386  }
387  }
388 
389  private class DownloadTextTask extends AsyncTask<String, Void, String>
390  {
391  private String url;
392 
393  protected String doInBackground(String... urls)
394  {
395  url = urls[0];
396  return downloadText(urls[0]);
397  }
398 
399  @Override
400  protected void onPostExecute(String result)
401  {
402  mReceiver.onHttpResponse(url, result.substring(0, 2), result.substring(2), null);
403  }
404  }
405 
406  private class DownloadImageTask extends AsyncTask<String, Void, Pair<String, Bitmap>>
407  {
408  private String url;
409 
410  protected Pair<String, Bitmap> doInBackground(String... urls)
411  {
412  url = urls[0];
413  return downloadImage(urls[0]);
414  }
415 
416  protected void onPostExecute(Pair<String, Bitmap> result)
417  {
418  mReceiver.onHttpResponse(url, result.first.substring(0, 2), result.first.substring(2), result.second);
419  }
420  }
421 }