ArViewer  Android Version
Arvos - Augmented reality viewer open source
 All Classes Namespaces Files Functions Variables
ArvosMain.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.util.ArrayList;
27 
28 import android.app.ActionBar;
29 import android.app.AlertDialog;
30 import android.app.ListActivity;
31 import android.content.Context;
32 import android.content.DialogInterface;
33 import android.content.Intent;
34 import android.content.SharedPreferences;
35 import android.graphics.Bitmap;
36 import android.location.Location;
37 import android.location.LocationManager;
38 import android.os.Bundle;
39 import android.preference.PreferenceManager;
40 import android.view.Menu;
41 import android.view.MenuItem;
42 import android.view.View;
43 import android.widget.ListView;
44 import android.widget.Toast;
45 
46 import com.mission_base.arviewer_android.viewer.ArvosViewer;
47 import com.mission_base.arviewer_android.viewer.Augment;
48 
59 public class ArvosMain extends ListActivity implements IArvosLocationReceiver, IArvosHttpReceiver
60 {
61  private ArrayList<Augment> mAugments = new ArrayList<Augment>();
62 
63  private ArvosLocationListener mLocationListener;
64  private Arvos mInstance;
65  private int mMenuPreferencesCount = 0;
66 
67  private static long mMaxAge = 1000L * 60 * 60 * 24 * 30;
68  private static long mMaxSize = 64 * 1024 * 1024;
69  private static int mMaxFiles = 256;
70 
71  @Override
72  protected void onCreate(Bundle savedInstanceState)
73  {
74  super.onCreate(savedInstanceState);
75  setContentView(R.layout.activity_main);
76 
77  mInstance = Arvos.getInstance();
78  mLocationListener = new ArvosLocationListener((LocationManager) getSystemService(Context.LOCATION_SERVICE), this);
79 
80  ArvosCustomArrayAdapter adapter = new ArvosCustomArrayAdapter(this, mAugments, R.drawable.arvos_logo_black);
81  setListAdapter(adapter);
82 
83  updateFromPreferences();
84  ArvosCache.initialize(this, mMaxAge, mMaxFiles, mMaxSize);
85  }
86 
90  public void onListItemClick(ListView parent, View v, int position, long id)
91  {
92  mMenuPreferencesCount = 0;
93 
94  Augment augment = mAugments.get(position);
95  String name = augment.mName;
96  String url = augment.mUrl;
97 
98  if (name != null && name.trim().length() > 0 && url != null && url.trim().length() > 0)
99  {
100  Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
101  requestAugment(augment);
102  }
103  }
104 
105  @Override
106  protected void onResume()
107  {
108  super.onResume();
109 
110  onResumeLocal();
111  }
112 
113  private void onResumeLocal()
114  {
115  ActionBar actionBar = getActionBar();
116  if (!hasInitialAugmentsRequest)
117  {
118  actionBar.setTitle("Retrieving location");
119  actionBar.setSubtitle("Please wait ...");
120  }
121  else
122  {
123  actionBar.setTitle("Augments");
124  actionBar.setSubtitle(String.format("Lon %.6f, Lat %.6f", mInstance.mLongitude, mInstance.mLatitude));
125  }
126  mLocationListener.onResume();
127  }
128 
129  private ArvosHttpRequest mArvosHttpRequest = null;
130 
131  private void requestAugments()
132  {
133  ActionBar actionBar = getActionBar();
134  actionBar.setTitle("Retrieving augments");
135  actionBar.setSubtitle("Please wait ...");
136 
137  mArvosHttpRequest = new ArvosHttpRequest(this, this);
138  mArvosHttpRequest.getText(mInstance.mAugmentsUrl);
139  }
140 
141  private void requestAugment(Augment augment)
142  {
143  ActionBar actionBar = getActionBar();
144 
145  actionBar.setTitle("Retrieving augment " + augment.mName);
146  actionBar.setSubtitle("Please wait ...");
147 
148  mArvosHttpRequest = new ArvosHttpRequest(this, this);
149  mArvosHttpRequest.getText(augment.mUrl);
150  }
151 
152  @Override
153  protected void onPause()
154  {
155  super.onPause();
156 
157  mLocationListener.onPause();
158 
159  mArvosHttpRequest = null;
160  }
161 
165  public void onHttpResponse(String url, String error, String text, Bitmap bitmap)
166  {
167  if (error.startsWith("ER"))
168  {
169  ActionBar actionBar = getActionBar();
170  actionBar.setSubtitle("Error: " + text);
171  mArvosHttpRequest = null;
172  return;
173  }
174 
175  if (mInstance.mAugmentsUrl.equals(url))
176  {
177  mAugments.clear();
178  error = Augment.parse(text, mAugments);
179  if (error.startsWith("RD"))
180  {
181  mInstance.mAugmentsUrl = error.substring(2);
182  requestAugments();
183  return;
184  }
185  if (error.startsWith("ER"))
186  {
187  ActionBar actionBar = getActionBar();
188  actionBar.setSubtitle("Error: " + error.substring(2));
189  mArvosHttpRequest = null;
190  return;
191  }
192 
193  ((ArvosCustomArrayAdapter) (this.getListAdapter())).notifyDataSetChanged();
194 
195  ActionBar actionBar = getActionBar();
196 
197  actionBar.setTitle("Augments");
198  actionBar.setSubtitle(String.format("Lon %.6f, Lat %.6f", mInstance.mLongitude, mInstance.mLatitude));
199  return;
200  }
201 
202  for (int i = 0; i < mAugments.size(); i++)
203  {
204  Augment augment = mAugments.get(i);
205  if (url.equals(augment.mUrl))
206  {
207  String name = augment.mName;
208 
209  Augment parsedAugment = new Augment();
210  error = parsedAugment.parse(text);
211  if (error.startsWith("ER"))
212  {
213  ActionBar actionBar = getActionBar();
214  actionBar.setSubtitle("Error: " + error.substring(2));
215  return;
216  }
217 
218  Intent intent = new Intent(this, ArvosViewer.class);
219  intent.putExtra("augmentText", text);
220  intent.putExtra("augmentName", name.trim());
221 
222  startActivity(intent);
223  }
224  }
225  }
226 
227  private boolean hasInitialAugmentsRequest = false;
228 
232  public void onLocationChanged(boolean isNew, Location location)
233  {
234  if (isNew && !hasInitialAugmentsRequest)
235  {
236  hasInitialAugmentsRequest = true;
237  requestAugments();
238  }
239  else
240  {
241  ActionBar actionBar = getActionBar();
242  actionBar.setSubtitle(String.format("Lon %.6f, Lat %.6f", mInstance.mLongitude, mInstance.mLatitude));
243  }
244  }
245 
246  static final private int MENU_ITEM_REFRESH = Menu.FIRST;
247  static final private int MENU_PREFERENCES = Menu.FIRST + 1;
248  private static final int SHOW_PREFERENCES = 1;
249 
253  @Override
254  public boolean onCreateOptionsMenu(Menu menu)
255  {
256  super.onCreateOptionsMenu(menu);
257 
258  int groupId = 0;
259  int menuItemId = MENU_ITEM_REFRESH;
260  int menuItemOrder = Menu.NONE;
261  int menuItemText = R.string.menu_refresh;
262 
263  menu.add(groupId, MENU_PREFERENCES, menuItemOrder, R.string.menu_preferences);
264 
265  MenuItem menuItem = menu.add(groupId, menuItemId, menuItemOrder, menuItemText);
266  menuItem = menu.add(groupId, menuItemId++, menuItemOrder, menuItemText);
267  menuItem.setIcon(R.drawable.ic_action_refresh_white);
268  menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
269 
270  return true;
271  }
272 
276  public boolean onOptionsItemSelected(MenuItem item)
277  {
278  super.onOptionsItemSelected(item);
279 
280  switch (item.getItemId())
281  {
282  case (MENU_ITEM_REFRESH):
283  mMenuPreferencesCount = 0;
284  if (!hasInitialAugmentsRequest)
285  {
286  onResumeLocal();
287  }
288  else
289  {
290  requestAugments();
291  }
292  return true;
293 
294  case (MENU_PREFERENCES):
295  showPreferences();
296  return true;
297  }
298  return false;
299  }
300 
301  private void showPreferences()
302  {
303  Intent i = new Intent(this, ArvosPreferences.class);
304  startActivityForResult(i, SHOW_PREFERENCES);
305  }
306 
307  public void onActivityResult(int requestCode, int resultCode, Intent data)
308  {
309  super.onActivityResult(requestCode, resultCode, data);
310  if (requestCode == SHOW_PREFERENCES)
311  {
312  mMenuPreferencesCount++;
313  if (mMenuPreferencesCount >= 5)
314  {
315  mMenuPreferencesCount = 0;
316  mInstance.mIsAuthor = !mInstance.mIsAuthor;
317  if (mInstance.mIsAuthor)
318  {
319  Toast.makeText(this, "Entering author mode", Toast.LENGTH_SHORT).show();
320  }
321  else
322  {
323  Toast.makeText(this, "Leaving author mode", Toast.LENGTH_SHORT).show();
324  }
325  Context context = getApplicationContext();
326  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
327  SharedPreferences.Editor ed = prefs.edit();
328  ed.putBoolean(ArvosPreferences.PREF_IS_AUTHOR, mInstance.mIsAuthor);
329  ed.commit();
330  }
331  else
332  {
333  Toast.makeText(this, "Preferences updated", Toast.LENGTH_SHORT).show();
334  }
335  checkAuthorKey();
336  updateFromPreferences();
337  }
338  }
339 
340  private void checkAuthorKey()
341  {
342  Context context = getApplicationContext();
343  final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
344  boolean isAuthor = prefs.getBoolean(ArvosPreferences.PREF_IS_AUTHOR, false);
345  String key = prefs.getString(ArvosPreferences.PREF_AUTHOR_KEY, "").trim();
346  if (isAuthor && key.length() > 0 && (!key.equals(ArvosHttpRequest.urlEncode(key)) || key.length() < 20))
347  {
348  mMenuPreferencesCount = 0;
349  AlertDialog.Builder ad = new AlertDialog.Builder(this);
350  ad.setTitle("Error");
351  ad.setMessage("The author key must have at least 20 alphanumeric characters");
352  ad.setPositiveButton("Update", new DialogInterface.OnClickListener()
353  {
354  public void onClick(DialogInterface dialog, int arg1)
355  {
356  showPreferences();
357  }
358  });
359  ad.setNegativeButton("Erase", new DialogInterface.OnClickListener()
360  {
361  public void onClick(DialogInterface dialog, int arg1)
362  {
363  SharedPreferences.Editor ed = prefs.edit();
364  ed.putString(ArvosPreferences.PREF_AUTHOR_KEY, "");
365  ed.commit();
366  }
367  });
368  ad.setCancelable(false);
369  ad.show();
370  }
371  }
372 
373  private void updateFromPreferences()
374  {
375  Context context = getApplicationContext();
376  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
377 
378  mInstance.mUseCache = prefs.getBoolean(ArvosPreferences.PREF_USE_CACHE, true);
379  mInstance.mIsAuthor = prefs.getBoolean(ArvosPreferences.PREF_IS_AUTHOR, false);
380  mInstance.mAuthorKey = prefs.getString(ArvosPreferences.PREF_AUTHOR_KEY, "").trim();
381  mInstance.mDeveloperKey = prefs.getString(ArvosPreferences.PREF_DEVELOPER_KEY, "").trim();
382  }
383 }