ArViewer  Android Version
Arvos - Augmented reality viewer open source
 All Classes Namespaces Files Functions Variables
ArvosViewer.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 android.app.*;
27 import android.content.*;
28 import android.graphics.*;
29 import android.location.*;
30 import android.os.*;
31 import android.view.*;
32 import android.widget.*;
33 import com.mission_base.arviewer_android.*;
34 import com.mission_base.arviewer_android.viewer.opengl.*;
35 
44 public class ArvosViewer extends Activity implements IArvosLocationReceiver, IArvosHttpReceiver
45 {
47  public ArvosGLSurfaceView mGLSurfaceView = null;
48  public ArvosTextView mTextView = null;
49  public ArvosRadarView mRadarView = null;
50 
51  private Arvos mInstance;
52  private ArvosLocationListener mLocationListener;
53  private ArvosHttpRequest mArvosHttpRequest;
54 
55  @Override
56  protected void onCreate(Bundle savedInstanceState)
57  {
58  super.onCreate(savedInstanceState);
59 
60  mInstance = Arvos.getInstance(this);
61  mLocationListener = new ArvosLocationListener((LocationManager) getSystemService(Context.LOCATION_SERVICE), this);
62 
63  String augmentText = getIntent().getStringExtra("augmentText");
64  String augmentName = getIntent().getStringExtra("augmentName");
65 
66  Augment augment = new Augment();
67  augment.parse(augmentText);
68  if (augment.mName == null)
69  {
70  augment.mName = augmentName;
71  }
72  mInstance.mAugment = augment;
73  requestTextures();
74  }
75 
76  private void requestTextures()
77  {
78  ActionBar actionBar = getActionBar();
79  actionBar.setTitle("Retrieving textures");
80  actionBar.setSubtitle("Please wait ...");
81 
82  Augment augment = mInstance.mAugment;
83  synchronized (augment)
84  {
85  mArvosHttpRequest = null;
86  for (Poi poi : augment.mPois)
87  {
88  for (PoiObject poiObject : poi.mPoiObjects)
89  {
90  if (poiObject.mTexture != null && poiObject.mBitmap == null)
91  {
92  mArvosHttpRequest = new ArvosHttpRequest(this, this);
93  mArvosHttpRequest.getImage(poiObject.mTexture);
94  break;
95  }
96  }
97  break;
98  }
99  if (mArvosHttpRequest == null)
100  {
101  onTexturesLoaded();
102  }
103  }
104  }
105 
106  @Override
107  protected void onResume()
108  {
109  super.onResume();
110 
111  mLocationListener.onResume();
112  mInstance.onResume();
113 
114  if (mGLSurfaceView != null)
115  {
116  mGLSurfaceView.onResume();
117  }
118  if (mRadarView != null)
119  {
121  }
122  }
123 
124  @Override
125  protected void onPause()
126  {
127  super.onPause();
128  mLocationListener.onPause();
130  if (mGLSurfaceView != null)
131  {
132  mGLSurfaceView.onPause();
133  }
134  if (mRadarView != null)
135  {
137  }
138  }
139 
140  @Override
141  public void onHttpResponse(String url, String error, String text, Bitmap bitmap)
142  {
143  if (error.startsWith("ER"))
144  {
145  ActionBar actionBar = getActionBar();
146  actionBar.setSubtitle("Error: " + text);
147  mArvosHttpRequest = null;
148  return;
149  }
150 
151  String nextTexture = null;
152  Augment augment = Arvos.getInstance().mAugment;
153  synchronized (augment)
154  {
155  for (Poi poi : augment.mPois)
156  {
157  for (PoiObject poiObject : poi.mPoiObjects)
158  {
159  if (url.equals(poiObject.mTexture))
160  {
161  poiObject.mBitmap = bitmap;
162  }
163  else if (poiObject.mTexture != null && poiObject.mBitmap == null)
164  {
165  nextTexture = poiObject.mTexture;
166  }
167  }
168  }
169 
170  if (nextTexture != null)
171  {
172  mArvosHttpRequest = new ArvosHttpRequest(this, this);
173  mArvosHttpRequest.getImage(nextTexture);
174  return;
175  }
176  }
177  onTexturesLoaded();
178  }
179 
180  private void onTexturesLoaded()
181  {
182  mArvosHttpRequest = null;
183 
184  ActionBar actionBar = getActionBar();
185  actionBar.setTitle(mInstance.mAugment.mName);
186  actionBar.setSubtitle(String.format("Lon %.6f, Lat %.6f", mInstance.mLongitude, mInstance.mLatitude));
187 
188  FrameLayout frame = new FrameLayout(this);
189 
190  mCameraView = new ArvosCameraView(this);
191  frame.addView(mCameraView);
192 
193  mGLSurfaceView = new ArvosGLSurfaceView(this);
194  frame.addView(mGLSurfaceView);
195 
196  mRadarView = new ArvosRadarView(this);
197  frame.addView(mRadarView);
198 
199  mTextView = new ArvosTextView(this);
200  frame.addView(mTextView);
201 
202  setContentView(frame);
203 
204  if (mTextView != null)
205  {
207  }
208  if (mGLSurfaceView != null)
209  {
210  mGLSurfaceView.onResume();
211  }
212  if (mRadarView != null)
213  {
215  }
216  }
217 
218  @Override
219  public void onLocationChanged(boolean isNew, Location location)
220  {
221  ActionBar actionBar = getActionBar();
222  actionBar.setSubtitle(String.format("Lon %.6f, Lat %.6f", mInstance.mLongitude, mInstance.mLatitude));
223  }
224 
225  static final private int MENU_ITEM_CLOSE = Menu.FIRST;
226 
227  @Override
228  public boolean onCreateOptionsMenu(Menu menu)
229  {
230  super.onCreateOptionsMenu(menu);
231 
232  int groupId = 0;
233  int menuItemId = MENU_ITEM_CLOSE;
234  int menuItemOrder = Menu.NONE;
235  int menuItemText = R.string.menu_close;
236 
237  // Create the Menu Item and keep a reference to it
238  MenuItem menuItem = menu.add(groupId, menuItemId, menuItemOrder, menuItemText);
239  menuItem = menu.add(groupId, menuItemId++, menuItemOrder, menuItemText);
240  // menuItem.setIcon(R.drawable.ic_action_refresh_white);
241  menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
242 
243  return true;
244  }
245 
246  public boolean onOptionsItemSelected(MenuItem item)
247  {
248  super.onOptionsItemSelected(item);
249 
250  switch (item.getItemId())
251  {
252  case (MENU_ITEM_CLOSE):
253  finish();
254  return true;
255  }
256  return false;
257  }
258 }