1 | package org.greenstone.android.tipple.base;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Random;
|
---|
6 |
|
---|
7 | import org.greenstone.android.tipple.R;
|
---|
8 |
|
---|
9 | import android.app.Activity;
|
---|
10 | import android.graphics.Bitmap;
|
---|
11 | import android.graphics.BitmapFactory;
|
---|
12 | import android.graphics.drawable.Drawable;
|
---|
13 | import android.location.Location;
|
---|
14 | import android.speech.tts.TextToSpeech;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Bad programming, but simplifies things.
|
---|
18 | */
|
---|
19 | public class Global {
|
---|
20 |
|
---|
21 | private static Random randomGen;
|
---|
22 | public static Activity activity;
|
---|
23 | public static TextToSpeech tts;
|
---|
24 | static HashMap<String, Bitmap> bitmapIcons;
|
---|
25 | static HashMap<String, Drawable> drawableIcons;
|
---|
26 | static ArrayList<TipLocation> locations;
|
---|
27 | private static int numGenerated = 0; // the number of points created (for name generation)
|
---|
28 |
|
---|
29 | static {
|
---|
30 | randomGen = new Random();
|
---|
31 | bitmapIcons = new HashMap<String, Bitmap>();
|
---|
32 | drawableIcons = new HashMap<String, Drawable>();
|
---|
33 | }
|
---|
34 |
|
---|
35 | public static int randomColor() {
|
---|
36 | return randomGen.nextInt(1 << 24) + 0xff000000;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public static String randomIcon() {
|
---|
40 | String[] icons = new String[] { "dinosaurpark", "house", "shop", "lecture" };
|
---|
41 | int randomIndex = randomGen.nextInt(icons.length);
|
---|
42 | return icons[randomIndex];
|
---|
43 | }
|
---|
44 |
|
---|
45 | public static Bitmap getBitmap(String resourceName) {
|
---|
46 | if (bitmapIcons.get(resourceName) == null) {
|
---|
47 | int id = 0;
|
---|
48 | if (resourceName != null) {
|
---|
49 | id = activity.getResources().getIdentifier(resourceName, "drawable",
|
---|
50 | activity.getPackageName());
|
---|
51 | }
|
---|
52 | if (id == 0) {
|
---|
53 | id = R.drawable.marker;
|
---|
54 | }
|
---|
55 | Bitmap b = BitmapFactory.decodeResource(activity.getResources(), id);
|
---|
56 | bitmapIcons.put(resourceName, b);
|
---|
57 | }
|
---|
58 | return bitmapIcons.get(resourceName);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public static Drawable getDrawable(String resourceName) {
|
---|
62 |
|
---|
63 | if (drawableIcons.get(resourceName) == null) {
|
---|
64 | int id = 0;
|
---|
65 | if (resourceName != null) {
|
---|
66 | id = activity.getResources().getIdentifier(resourceName, "drawable",
|
---|
67 | activity.getPackageName());
|
---|
68 | }
|
---|
69 | if (id == 0) {
|
---|
70 | id = R.drawable.marker;
|
---|
71 | }
|
---|
72 | Drawable d = activity.getResources().getDrawable(id);
|
---|
73 | d.setBounds(0 - d.getIntrinsicWidth() / 2, 0 - d.getIntrinsicHeight(),
|
---|
74 | d.getIntrinsicWidth() / 2, 0);
|
---|
75 | drawableIcons.put(resourceName, d);
|
---|
76 | }
|
---|
77 | return drawableIcons.get(resourceName);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public static TipLocation fixedLocation(Location current) {
|
---|
81 | return makeTipLocation(-37.78804, 175.31238, 0);
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | public static TipLocation randomLocation(Location current) {
|
---|
86 | double lat = current.getLatitude() + (randomGen.nextDouble() - 0.5) / 2000;
|
---|
87 | double lon = current.getLongitude() + (randomGen.nextDouble() - 0.5) / 2000;
|
---|
88 | double alt = current.getAltitude() + 4 * (randomGen.nextDouble() - 0.5)
|
---|
89 | - LocationFusion.getInstance().lastKnownLocation.getAltitude();
|
---|
90 |
|
---|
91 | return makeTipLocation(lat, lon, alt);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public static TipLocation makeTipLocation(Location loc) {
|
---|
95 | return makeTipLocation(loc.getLatitude(), loc.getLongitude(), loc.getAltitude()
|
---|
96 | - LocationFusion.getInstance().lastKnownLocation.getAltitude());
|
---|
97 | }
|
---|
98 |
|
---|
99 | public static TipLocation makeTipLocation(double lat, double lon, double alt) {
|
---|
100 | HashMap<String, String> data = new HashMap<String, String>();
|
---|
101 |
|
---|
102 | data.put("latitude", Double.toString(lat));
|
---|
103 | data.put("longitude", Double.toString(lon));
|
---|
104 | data.put("altitude", Double.toString(alt));
|
---|
105 | data.put("radius", "0.03");
|
---|
106 |
|
---|
107 | data.put("title", nextName());
|
---|
108 | data.put("text", "This is a randomly generated location");
|
---|
109 | data.put("tts", "This is a randomly generated location");
|
---|
110 | data.put("tts_title", "Random Location " + data.get("title"));
|
---|
111 |
|
---|
112 | data.put("colour_fill", "#" + Integer.toHexString(randomColor()));
|
---|
113 | data.put("colour_outline", "#" + Integer.toHexString(randomColor()));
|
---|
114 |
|
---|
115 | data.put("marker_type", randomIcon());
|
---|
116 |
|
---|
117 | data.put("audio", "");
|
---|
118 |
|
---|
119 | return new TipLocation(data);
|
---|
120 | }
|
---|
121 |
|
---|
122 | private static String nextName()
|
---|
123 | {
|
---|
124 | numGenerated++;
|
---|
125 |
|
---|
126 | StringBuilder name = new StringBuilder();
|
---|
127 | int i = numGenerated;
|
---|
128 | while (i > 0)
|
---|
129 | {
|
---|
130 | name.insert(0, (char) ('A' + i % 26));
|
---|
131 | i /= 26;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return name.toString();
|
---|
135 | }
|
---|
136 | }
|
---|