GM.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package com.ivalentin.margolariak;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.util.Log;
  5. import java.text.DateFormat;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. import java.util.Locale;
  10. /**
  11. * Final class that contains useful static values to be used across the app.
  12. *
  13. * @author Iñigo Valentin
  14. *
  15. */
  16. final class GM {
  17. /**
  18. * Address where the web server is
  19. */
  20. static final String SERVER = "http://margolariak.com";
  21. /**
  22. * Code for the location permission request
  23. */
  24. static final int PERMISSION_LOCATION = 1;
  25. /**
  26. * Period of time (in seconds) at the end of witch the app will perform a sync.
  27. */
  28. static final int PERIOD_SYNC = 20 * 60 * 1000;
  29. /**
  30. * Name of the preference to store the user code with.
  31. */
  32. static final String USER_CODE = "prefcode";
  33. /**
  34. * Constant to diferentiate schedules".
  35. */
  36. static final String SCHEDULE = "schedule";
  37. /**
  38. * Constant to set extras on the main intent.
  39. */
  40. static final String EXTRA_TITLE = "title";
  41. /**
  42. * Constant to set extras on the main intent.
  43. */
  44. static final String EXTRA_TEXT = "text";
  45. /**
  46. * Constant to set extras on the main intent.
  47. */
  48. static final String EXTRA_ACTION = "action";
  49. /**
  50. * Value for {@see GM.EXTRA_ACTION} that opens the lablanca section.
  51. */
  52. static final String EXTRA_ACTION_LABLANCA = "lablanca";
  53. /**
  54. * Value for {@see GM.EXTRA_ACTION} that opens the official schedule.
  55. */
  56. static final String EXTRA_ACTION_CITYSCHEDULE = "cityschedule";
  57. /**
  58. * Value for {@see GM.EXTRA_ACTION} that opens the GM schedule.
  59. */
  60. static final String EXTRA_ACTION_GMSCHEDULE = "gmschedule";
  61. /**
  62. * Value for {@see GM.EXTRA_ACTION} that opens the location secton.
  63. */
  64. static final String EXTRA_ACTION_LOCATION = "location";
  65. /**
  66. * Value for {@see GM.EXTRA_ACTION} that opens the blog.
  67. */
  68. static final String EXTRA_ACTION_BLOG = "blog";
  69. /**
  70. * Value for {@see GM.EXTRA_ACTION} that opens the activities section.
  71. */
  72. static final String EXTRA_ACTION_ACTIVITIES = "activities";
  73. /**
  74. * Value for {@see GM.EXTRA_ACTION} that opens the gallery section.
  75. */
  76. static final String EXTRA_ACTION_GALLERY = "gallery";
  77. /**
  78. * Constant for section "Home".
  79. */
  80. static final byte SECTION_HOME = 0;
  81. /**
  82. * Constant for La Blanca section "Location".
  83. */
  84. static final byte SECTION_LOCATION = 1;
  85. /**
  86. * Constant for section "La Blanca".
  87. */
  88. static final byte SECTION_LABLANCA = 2;
  89. /**
  90. * Constant for La Blanca section "Festival schedule".
  91. */
  92. static final byte SECTION_LABLANCA_SCHEDULE = 20;
  93. /**
  94. * Constant for La Blanca section "Gasteizko Margolariak Schedule".
  95. */
  96. static final byte SECTION_LABLANCA_GM_SCHEDULE = 21;
  97. /**
  98. * Constant for section "Activities".
  99. */
  100. static final byte SECTION_ACTIVITIES = 3;
  101. /**
  102. * Constant for section "Blog".
  103. */
  104. static final byte SECTION_BLOG = 4;
  105. /**
  106. * Constant for section "Gallery".
  107. */
  108. static final byte SECTION_GALLERY = 5;
  109. /**
  110. * Name of the preference group for the app.
  111. */
  112. static final String PREF = "gmpreferences";
  113. /**
  114. * Name of the preference to store the database version with.
  115. */
  116. static final String PREF_DB_VERSION = "prefDbVersion";
  117. /**
  118. * Name of the preference to store the setting that allows user to upload comments.
  119. */
  120. static final String PREF_DB_PHOTOS = "prefDbPhotos";
  121. /**
  122. * Name of the preference that indicates if we are on festivals.
  123. */
  124. static final String PREF_DB_FESTIVALS = "prefDbFestivals";
  125. /**
  126. * Name of the preference indicating if the user wants to to receive notifications intended for the general public.
  127. */
  128. static final String PREF_NOTIFICATION = "prefNotification";
  129. /**
  130. * Default value of the preference indicating if the user wants to to receive notifications intended for the general public.
  131. */
  132. static final int DEFAULT_PREF_NOTIFICATION = 1;
  133. /**
  134. * Header of a preference to indicate received notifications.
  135. */
  136. static final String NOTIFICATION_SEEN_ ="notificationSeen";
  137. /**
  138. * Default value for the {//@link GM.PREF_DB_VERSION} preference
  139. */
  140. static final int DEFAULT_PREF_DB_VERSION = -2;
  141. /**
  142. * Name of the preference to store GM location's latitude.
  143. */
  144. static final String PREF_GM_LATITUDE = "gmlat";
  145. /**
  146. * Name of the preference to store GM location's longitude.
  147. */
  148. static final String PREF_GM_LONGITUDE = "gmlon";
  149. /**
  150. * Name of the preference to store GM last location timestamp.
  151. */
  152. static final String PREF_GM_LOCATION = "gmlocationtime";
  153. /**
  154. * Name of the preference to store GM last location timestamp.
  155. */
  156. static final String DEFAULT_PREF_GM_LOCATION = "19700101000000";
  157. /**
  158. * Name of the preference indicating if the user wants to perform background syncs.
  159. */
  160. static final String PREFERENCE_SYNC_SYNC = "pref_sync_sync";
  161. /**
  162. * Default value for PREFERENCE_SYNC_SYNC.
  163. */
  164. static final boolean DEFAULT_PREFERENCE_SYNC_SYNC = true;
  165. /**
  166. * Name of the preference indicating if the user wants to to receive notifications intended for the general public.
  167. */
  168. static final String PREFERENCE_SYNC_NOTIFICATIONS = "pref_sync_notifications";
  169. /**
  170. * Default value for PREFERENCE_SYNC_NOTIFICATIONS.
  171. */
  172. static final boolean DEFAULT_PREFERENCE_SYNC_NOTIFICATIONS = true;
  173. /**
  174. * Name of the SharedPreference that stores how many kb have been sent in the foreground.
  175. */
  176. static final String STORAGE_TRAFFIC_FG_SENT = "storage_traffic_fg_sent";
  177. /**
  178. * Name of the SharedPreference that stores how many kb have been received in the foreground.
  179. */
  180. static final String STORAGE_TRAFFIC_FG_RECEIVED = "storage_traffic_fg_received";
  181. /**
  182. * Name of the SharedPreference that stores how many kb have been sent in the background.
  183. */
  184. static final String STORAGE_TRAFFIC_BG_SENT = "storage_traffic_bg_received";
  185. /**
  186. * Name of the SharedPreference that stores how many kb have been received in the background.
  187. */
  188. static final String STORAGE_TRAFFIC_BG_RECEIVED = "storage_traffic_bg_received";
  189. static final String KEY_PREFERENCE_SYNC = "preference_key_sync";
  190. static final String KEY_PREFERENCE_NOTIFICATION = "preference_key_notification";
  191. static final String KEY_PREFERENCE_DATA = "preference_key_data";
  192. static final String KEY_PREFERENCE_VERSION = "preference_key_version";
  193. static final String KEY_PREFERENCE_SOURCE = "preference_key_source";
  194. static final String KEY_PREFERENCE_FEEDBACK = "preference_key_feedback";
  195. static final int INTERVAL_LOCATION = 240000; //4 min
  196. static final int INTERVAL_SYNC_FESTIVALS = 600000; //10 min
  197. static final int INTERVAL_SYNC_NO_FESTIVALS = 3600000; //60 min
  198. /**
  199. * Name of the database
  200. */
  201. static final String DB_NAME = "gm";
  202. /**
  203. * The desired accuracy of the GPS coordinates, used to provide location updates, in meters.
  204. */
  205. static final int LOCATION_ACCURACY_SPACE = 10;
  206. /**
  207. * The desired accuracy of the GPS coordinates, used to provide location updates, in milliseconds.
  208. */
  209. static final int LOCATION_ACCURACY_TIME = 10000;
  210. /**
  211. * Dimensions of the images in the database.
  212. */
  213. static final int IMG_MINIATURE = 340;
  214. static final int IMG_PREVIEW = 600;
  215. static final int IMG_THUMB = 180;
  216. static final int IMG_VIEW = 800;
  217. /**
  218. * Gets the language code for sql queries.
  219. * Only three values can be returned: es, eu, en.
  220. * Defaults to es.
  221. *
  222. * @return Language two-letter code
  223. */
  224. public static String getLang(){
  225. String currLang = Locale.getDefault().getISO3Language();//getDisplayLanguage();
  226. switch (currLang){
  227. case "eng":
  228. currLang = "en";
  229. break;
  230. case "eus":
  231. currLang = "eu";
  232. break;
  233. default:
  234. currLang = "es";
  235. }
  236. return currLang;
  237. }
  238. /**
  239. * Formats a datetime format strings and returns a human readable date, depending on the language
  240. *
  241. * @param dateString String in a datetime format
  242. * @param lang Language (es, en, eu)
  243. * @param includeTime Also returns the hour and minutes
  244. *
  245. * @return The fragment view
  246. *
  247. * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
  248. */
  249. public static String formatDate(String dateString, String lang, boolean includeTime){
  250. String output = "";
  251. String dayNameEs[] = {"dummy", "Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"};
  252. String dayNameEn[] = {"dummy", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  253. String dayNameEu[] = {"dummy", "igandea", "astelehena", "asteartea", "asteazkena", "osteguna", "ostirala", "larumbata"};
  254. String monthNameEs[] = {"enero", " febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"};
  255. String monthNameEn[] = {"January", "February", "March", "April", "May", "June", "July", "Augost", "September", "Octuber", "November", "December"};
  256. String monthNameEu[] = {"urtarrilaren", "otsailaren", "martxoaren", "apirilaren", "maiatzaren", "ekainaren", "uztailaren", "abuztuaren", "irailaren", "urriaren", "azaroaren", "abenduaren"};
  257. if (!lang.equals("es") && !lang.equals("eu")){
  258. lang = "en";
  259. }
  260. DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH);
  261. SimpleDateFormat df;
  262. Date date;
  263. try{
  264. date = format.parse(dateString);
  265. df = new SimpleDateFormat("yyyy", Locale.US);
  266. String year = df.format(date);
  267. df = new SimpleDateFormat("hh:mm", Locale.US);
  268. String time = df.format(date);
  269. String dayName, monthName;
  270. Calendar calendar = Calendar.getInstance();
  271. calendar.setTime(date);
  272. int dayNumber = calendar.get(Calendar.DAY_OF_WEEK);
  273. int day = calendar.get(Calendar.DAY_OF_MONTH);
  274. int monthNumber = calendar.get(Calendar.MONTH);
  275. switch (lang){
  276. case "es":
  277. dayName = dayNameEs[dayNumber];
  278. monthName = monthNameEs[monthNumber];
  279. output = dayName + ", " + day + " de " + monthName + " de " + year;
  280. if (includeTime){
  281. output = output + " a las " + time;
  282. }
  283. break;
  284. case "eu":
  285. dayName = dayNameEu[dayNumber];
  286. monthName = monthNameEu[monthNumber];
  287. output = year + "ko" + monthName + " " + day + "an, " + dayName;
  288. if (includeTime){
  289. output = output + " " + time + "etan";
  290. }
  291. break;
  292. default:
  293. dayName = dayNameEn[dayNumber];
  294. monthName = monthNameEn[monthNumber];
  295. output = dayName + ", " + monthName + " " + day + ", " + year;
  296. if (includeTime){
  297. output = output + " " + time;
  298. }
  299. break;
  300. }
  301. }
  302. catch(Exception ex){
  303. Log.e("Date format error", ex.toString());
  304. }
  305. return output;
  306. }
  307. public static Bitmap decodeSampledBitmapFromFile(String path, int size) { // BEST QUALITY MATCH
  308. // First decode with inJustDecodeBounds=true to check dimensions
  309. final BitmapFactory.Options options = new BitmapFactory.Options();
  310. options.inJustDecodeBounds = true;
  311. BitmapFactory.decodeFile(path, options);
  312. // Calculate inSampleSize
  313. // Raw height and width of image
  314. final int height = options.outHeight;
  315. final int width = options.outWidth;
  316. options.inPreferredConfig = Bitmap.Config.RGB_565;
  317. int inSampleSize = 1;
  318. if (height > size) {
  319. inSampleSize = Math.round((float)height / (float)size);
  320. }
  321. int expectedWidth = width / inSampleSize;
  322. if (expectedWidth > size) {
  323. //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
  324. inSampleSize = Math.round((float)width / (float)size);
  325. }
  326. options.inSampleSize = inSampleSize;
  327. // Decode bitmap with inSampleSize set
  328. options.inJustDecodeBounds = false;
  329. return BitmapFactory.decodeFile(path, options);
  330. }
  331. }