ArViewer  Android Version
Arvos - Augmented reality viewer open source
 All Classes Namespaces Files Functions Variables
Augment.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.viewer;
25 
26 import com.mission_base.arviewer_android.*;
27 import com.mission_base.arviewer_android.viewer.opengl.*;
28 import java.util.*;
29 import org.json.*;
30 
39 public class Augment
40 {
41  public String mName;
42  public String mUrl;
43  public String mAuthor;
44  public String mDescription;
45  public Float mLongitude;
46  public Float mLatitude;
47  public String mDeveloperKey;
48 
49  public LinkedList<Poi> mPois;
50 
51  public Augment()
52  {
53  mPois = new LinkedList<Poi>();
54  }
55 
65  public static String parse(String input, List<Augment> result)
66  {
67  try
68  {
69  JSONObject jsonAugmentsList = new JSONObject(input);
70  if (jsonAugmentsList.has("redirect"))
71  {
72  String redirectUrl = jsonAugmentsList.getString("redirect");
73  if (redirectUrl != null)
74  {
75  redirectUrl = redirectUrl.trim();
76  if (redirectUrl.length() > 0)
77  {
78  return "RD" + redirectUrl;
79  }
80  }
81  }
82 
83  if (jsonAugmentsList.has("sessionId"))
84  {
85  String sessionId = jsonAugmentsList.getString("sessionId");
86  if (sessionId != null)
87  {
88  sessionId = sessionId.trim();
89  if (sessionId.length() > 0)
90  {
91  Arvos.getInstance().mSessionId = sessionId;
92  }
93  }
94  }
95 
96  JSONArray jsonAugments = new JSONArray(jsonAugmentsList.getString("augments"));
97 
98  if (jsonAugments == null || jsonAugments.length() == 0)
99  {
100  return "ERNo augments found at your location. Retry ...";
101  }
102 
103  for (int i = 0; i < jsonAugments.length(); i++)
104  {
105  JSONObject jsonAugment = jsonAugments.getJSONObject(i);
106  if (jsonAugment != null)
107  {
108  Augment augment = new Augment();
109  result.add(augment);
110 
111  augment.mName = jsonAugment.getString("name");
112  augment.mUrl = jsonAugment.getString("url");
113 
114  augment.mAuthor = jsonAugment.has("author") ? jsonAugment.getString("author") : "";
115  augment.mLatitude = (float) (jsonAugment.has("lat") ? jsonAugment.getDouble("lat") : 0f);
116  augment.mLongitude = (float) (jsonAugment.has("lon") ? jsonAugment.getDouble("lon") : 0f);
117  augment.mDescription = jsonAugment.has("description") ? jsonAugment.getString("description") : "";
118  augment.mDeveloperKey = jsonAugment.has("developerKey") ? jsonAugment.getString("developerKey") : "";
119  }
120  }
121  }
122  catch (Exception e)
123  {
124  return "ERJSON parse error. " + e.getLocalizedMessage();
125  }
126 
127  return "OK";
128  }
129 
138  public String parse(String input)
139  {
140  try
141  {
142  JSONObject jsonAugment = new JSONObject(input);
143  mName = jsonAugment.getString("name");
144  mAuthor = jsonAugment.has("author") ? jsonAugment.getString("author") : "";
145  mDescription = jsonAugment.has("description") ? jsonAugment.getString("description") : "";
146 
147  JSONArray jsonPois = new JSONArray(jsonAugment.getString("pois"));
148 
149  if (jsonPois == null || jsonPois.length() == 0)
150  {
151  return "ERNo pois found in augment " + mName;
152  }
153 
154  for (int i = 0; i < jsonPois.length(); i++)
155  {
156  JSONObject jsonPoi = jsonPois.getJSONObject(i);
157  if (jsonPoi != null)
158  {
159  Poi poi = new Poi(this);
160  poi.parse(jsonPoi);
161  mPois.add(poi);
162  }
163  }
164  }
165  catch (Exception e)
166  {
167  return "ERJSON parse error. " + e.getLocalizedMessage();
168  }
169 
170  return "OK";
171  }
172 
182  public List<ArvosObject> getObjects(long time, List<ArvosObject> arvosObjects)
183  {
184  List<ArvosObject> result = new LinkedList<ArvosObject>();
185 
186  synchronized (mPois)
187  {
188  for (Poi poi : mPois)
189  {
190  poi.getObjects(time, result, arvosObjects);
191  }
192  }
193  return result;
194  }
195 
202  public void addClick(int id)
203  {
204  synchronized (this)
205  {
206  if (!mPois.isEmpty())
207  {
208  Poi poi = mPois.getFirst();
209  PoiObject poiObject = poi.findPoiObject(id);
210  if (poiObject != null)
211  {
212  poiObject.mParent.addClick(poiObject);
213  }
214  }
215  }
216  }
217 }