MainActivity.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. package com.ivalentin.margolariak;
  2. import java.math.BigInteger;
  3. import java.security.SecureRandom;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Locale;
  7. import android.app.Activity;
  8. import android.app.Fragment;
  9. import android.app.FragmentManager;
  10. import android.app.FragmentTransaction;
  11. import android.app.Dialog;
  12. import android.content.Context;
  13. import android.content.Intent;
  14. import android.content.SharedPreferences;
  15. import android.database.sqlite.SQLiteDatabase;
  16. import android.graphics.Typeface;
  17. import android.graphics.drawable.Drawable;
  18. import android.os.Bundle;
  19. import android.util.Log;
  20. import android.view.Gravity;
  21. import android.view.KeyEvent;
  22. import android.view.MenuInflater;
  23. import android.view.MenuItem;
  24. import android.view.View;
  25. import android.view.Window;
  26. import android.view.View.OnClickListener;
  27. import android.view.WindowManager;
  28. import android.widget.Button;
  29. import android.widget.HorizontalScrollView;
  30. import android.widget.ImageView;
  31. import android.widget.PopupMenu;
  32. import android.widget.ProgressBar;
  33. import android.widget.RelativeLayout;
  34. import android.widget.TextView;
  35. import android.os.Handler;
  36. /**
  37. * The main Activity of the app. It's actually the only activity, and it loads other fragments.
  38. *
  39. * @author Iñigo Valentin
  40. *
  41. */
  42. public class MainActivity extends Activity{
  43. //Alarm to get location and notifications.
  44. private AlarmReceiver notificationAlarm;
  45. //The menu entries
  46. private RelativeLayout menuItem[];
  47. private TextView menuText[];
  48. private ImageView menuImage[];
  49. //Handler to periodically check for location updates
  50. private final Handler locationHandler = new Handler();
  51. // Code to check for location updates
  52. private final Runnable checkForLocation = new Runnable() {
  53. @Override
  54. public void run() {
  55. //Get location page and parse it
  56. boolean result = false;
  57. FetchURL fu = new FetchURL();
  58. fu.Run(GM.SERVER + "/app/location.php");
  59. String lat = "", lon = "";
  60. String o = fu.getOutput().toString();
  61. Log.d("Location", "Fetched location page");
  62. if (o.contains("<location>none</location>"))
  63. result = false;
  64. if (o.contains("<lat>") && o.contains("<lon>")){
  65. lat = o.substring(o.indexOf("<lat>") + 5, o.indexOf("</lat>"));
  66. lon = o.substring(o.indexOf("<lon>") + 5, o.indexOf("</lon>"));
  67. result = true;
  68. }
  69. SharedPreferences settings = getSharedPreferences(GM.PREF, Context.MODE_PRIVATE);
  70. SharedPreferences.Editor editor = settings.edit();
  71. if (result){
  72. editor.putLong(GM.PREF_GM_LATITUDE, Double.doubleToLongBits(Double.parseDouble(lat)));
  73. editor.putLong(GM.PREF_GM_LONGITUDE, Double.doubleToLongBits(Double.parseDouble(lon)));
  74. editor.putString(GM.PREF_GM_LOCATION, new SimpleDateFormat("yyyyMMddHHmmss", Locale.US).format(Calendar.getInstance().getTime()));
  75. //Show menu entry
  76. if (menuItem[1] != null){
  77. menuItem[1].setVisibility(View.VISIBLE);
  78. }
  79. }
  80. else{
  81. editor.putString(GM.PREF_GM_LOCATION, GM.DEFAULT_PREF_GM_LOCATION);
  82. if (menuItem[1] != null){
  83. menuItem[1].setVisibility(View.GONE);
  84. }
  85. }
  86. editor.apply();
  87. //Repeat this the same runnable code block again another th specified interval
  88. locationHandler.postDelayed(checkForLocation, GM.INTERVAL_LOCATION);
  89. }
  90. };
  91. //Not referenced in code.
  92. public void showMenu(View v) {
  93. PopupMenu popup = new PopupMenu(this, v);
  94. MenuInflater inflater = popup.getMenuInflater();
  95. inflater.inflate(R.menu.menu, popup.getMenu());
  96. popup.getMenu().getItem(0).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener(){
  97. @Override
  98. public boolean onMenuItemClick(MenuItem item) {
  99. Intent intent = new Intent(MainActivity.this, AboutActivity.class);
  100. startActivity(intent);
  101. return true;
  102. }
  103. });
  104. popup.getMenu().getItem(1).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener(){
  105. @Override
  106. public boolean onMenuItemClick(MenuItem item) {
  107. Intent intent = new Intent(MainActivity.this, SponsorActivity.class);
  108. startActivity(intent);
  109. return true;
  110. }
  111. });
  112. popup.getMenu().getItem(2).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener(){
  113. @Override
  114. public boolean onMenuItemClick(MenuItem item) {
  115. Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
  116. startActivity(intent);
  117. return true;
  118. }
  119. });
  120. popup.show();
  121. }
  122. /**
  123. * Loads a section in the main screen.
  124. *
  125. * @param section Section identifier {@see GM}
  126. */
  127. public void loadSection (byte section){
  128. FragmentManager fm = MainActivity.this.getFragmentManager();
  129. FragmentTransaction ft = fm.beginTransaction();
  130. Fragment fragment = null;
  131. String title = "";
  132. Bundle bundle = new Bundle();
  133. //Reset all tabs to it's original state
  134. for (int i = 0; i < 6; i ++){
  135. menuText[i].setTypeface(null, Typeface.NORMAL);
  136. menuImage[i].requestLayout();
  137. menuImage[i].getLayoutParams().height = 2;
  138. }
  139. switch (section){
  140. case GM.SECTION_HOME:
  141. fragment = new HomeLayout();
  142. title = getString(R.string.menu_home);
  143. menuText[0].setTypeface(null, Typeface.BOLD);
  144. menuImage[0].getLayoutParams().height = 8;
  145. break;
  146. case GM.SECTION_LOCATION:
  147. fragment = new LocationLayout();
  148. title = getString(R.string.menu_location);
  149. menuText[1].setTypeface(null, Typeface.BOLD);
  150. menuImage[1].getLayoutParams().height = 8;
  151. break;
  152. case GM.SECTION_LABLANCA:
  153. //Get settings
  154. SharedPreferences preferences = getSharedPreferences(GM.PREF, Context.MODE_PRIVATE);
  155. if (preferences.getInt(GM.PREF_DB_FESTIVALS, 0) == 1){
  156. fragment = new LablancaLayout();
  157. }
  158. else{
  159. fragment = new LablancaNoFestivalsLayout();
  160. }
  161. title = getString(R.string.menu_lablanca);
  162. menuText[2].setTypeface(null, Typeface.BOLD);
  163. menuImage[2].getLayoutParams().height = 8;
  164. break;
  165. case GM.SECTION_LABLANCA_SCHEDULE:
  166. fragment = new ScheduleLayout();
  167. bundle.putInt(GM.SCHEDULE, GM.SECTION_LABLANCA_SCHEDULE);
  168. fragment.setArguments(bundle);
  169. title = getString(R.string.menu_lablanca_schedule);
  170. menuText[2].setTypeface(null, Typeface.BOLD);
  171. menuImage[2].getLayoutParams().height = 8;
  172. break;
  173. case GM.SECTION_LABLANCA_GM_SCHEDULE:
  174. fragment = new ScheduleLayout();
  175. bundle.putInt(GM.SCHEDULE, GM.SECTION_LABLANCA_GM_SCHEDULE);
  176. fragment.setArguments(bundle);
  177. title = getString(R.string.menu_lablanca_gm_schedule);
  178. menuText[2].setTypeface(null, Typeface.BOLD);
  179. menuImage[2].getLayoutParams().height = 8;
  180. break;
  181. case GM.SECTION_ACTIVITIES:
  182. fragment = new ActivityLayout();
  183. title = getString(R.string.menu_activities);
  184. menuText[3].setTypeface(null, Typeface.BOLD);
  185. menuImage[3].getLayoutParams().height = 8;
  186. break;
  187. case GM.SECTION_BLOG:
  188. fragment = new BlogLayout();
  189. title = getString(R.string.menu_blog);
  190. menuText[4].setTypeface(null, Typeface.BOLD);
  191. menuImage[4].getLayoutParams().height = 8;
  192. break;
  193. case GM.SECTION_GALLERY:
  194. fragment = new GalleryLayout();
  195. title = getString(R.string.menu_blog);
  196. menuText[5].setTypeface(null, Typeface.BOLD);
  197. menuImage[5].getLayoutParams().height = 8;
  198. break;
  199. }
  200. //Replace the fragment.
  201. ft.replace(R.id.activity_main_content_fragment, fragment);
  202. //ft.addToBackStack(title);
  203. ft.commit();
  204. setSectionTitle(title);
  205. }
  206. /**
  207. * Sets the title of the current section.
  208. *
  209. * @param title The title of the current section.
  210. */
  211. public void setSectionTitle(String title){
  212. TextView tvTitle = (TextView) findViewById(R.id.activity_main_content_title);
  213. tvTitle.setText(title);
  214. }
  215. /**
  216. * Runs when the activity is created.
  217. *
  218. * @param savedInstanceState Saved state of the activity.
  219. *
  220. * @see android.app.Activity#onCreate(android.os.Bundle)
  221. */
  222. @SuppressWarnings("deprecation, ConstantConditions")
  223. @Override
  224. protected void onCreate(Bundle savedInstanceState) {
  225. //Get intent extras
  226. String action = getIntent().getStringExtra(GM.EXTRA_ACTION);
  227. String actionText = getIntent().getStringExtra(GM.EXTRA_TEXT);
  228. String actionTitle = getIntent().getStringExtra(GM.EXTRA_TITLE);
  229. //Set an alarm for notifications..
  230. //Wait a little
  231. final Intent intent = this.getIntent();
  232. final Context context = this;
  233. Thread t = new Thread() {
  234. @Override
  235. public void run() {
  236. try {
  237. Thread.sleep(20000);
  238. runOnUiThread(new Runnable() {
  239. @Override
  240. public void run() {
  241. notificationAlarm = new AlarmReceiver();
  242. notificationAlarm.setAlarm(context);
  243. notificationAlarm.onReceive(context, intent);
  244. }
  245. });
  246. } catch (InterruptedException e) {
  247. Log.e("Sleep interrupted", e.toString());
  248. }
  249. }
  250. };
  251. t.start();
  252. //Remove title bar.
  253. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  254. super.onCreate(savedInstanceState);
  255. //Set layout.
  256. setContentView(R.layout.activity_main);
  257. //Assign menu items
  258. menuItem = new RelativeLayout[6];
  259. menuItem[0] = (RelativeLayout) findViewById(R.id.rl_menu_home);
  260. menuItem[1] = (RelativeLayout) findViewById(R.id.rl_menu_location);
  261. menuItem[2] = (RelativeLayout) findViewById(R.id.rl_menu_lablanca);
  262. menuItem[3] = (RelativeLayout) findViewById(R.id.rl_menu_activities);
  263. menuItem[4] = (RelativeLayout) findViewById(R.id.rl_menu_blog);
  264. menuItem[5] = (RelativeLayout) findViewById(R.id.rl_menu_gallery);
  265. menuText = new TextView[6];
  266. menuText[0] = (TextView) menuItem[0].findViewById(R.id.tv_menu_home);
  267. menuText[1] = (TextView) menuItem[1].findViewById(R.id.tv_menu_location);
  268. menuText[2] = (TextView) menuItem[2].findViewById(R.id.tv_menu_lablanca);
  269. menuText[3] = (TextView) menuItem[3].findViewById(R.id.tv_menu_activities);
  270. menuText[4] = (TextView) menuItem[4].findViewById(R.id.tv_menu_blog);
  271. menuText[5] = (TextView) menuItem[5].findViewById(R.id.tv_menu_gallery);
  272. menuImage = new ImageView[6];
  273. menuImage[0] = (ImageView) menuItem[0].findViewById(R.id.iv_menu_home);
  274. menuImage[1] = (ImageView) menuItem[1].findViewById(R.id.iv_menu_location);
  275. menuImage[2] = (ImageView) menuItem[2].findViewById(R.id.iv_menu_lablanca);
  276. menuImage[3] = (ImageView) menuItem[3].findViewById(R.id.iv_menu_activities);
  277. menuImage[4] = (ImageView) menuItem[4].findViewById(R.id.iv_menu_blog);
  278. menuImage[5] = (ImageView) menuItem[5].findViewById(R.id.iv_menu_gallery);
  279. //Remove scrollbars from the sections menu
  280. HorizontalScrollView svMenu = (HorizontalScrollView) findViewById(R.id.sv_menu);
  281. svMenu.setHorizontalScrollBarEnabled(false);
  282. //Set click listers for menu items
  283. menuItem[0].setOnClickListener(new OnClickListener() {
  284. @Override
  285. public void onClick(View v) { loadSection(GM.SECTION_HOME); }
  286. });
  287. menuItem[1].setOnClickListener(new OnClickListener() {
  288. @Override
  289. public void onClick(View v) { loadSection(GM.SECTION_LOCATION); }
  290. });
  291. menuItem[2].setOnClickListener(new OnClickListener() {
  292. @Override
  293. public void onClick(View v) { loadSection(GM.SECTION_LABLANCA); }
  294. });
  295. menuItem[3].setOnClickListener(new OnClickListener() {
  296. @Override
  297. public void onClick(View v) { loadSection(GM.SECTION_ACTIVITIES); }
  298. });
  299. menuItem[4].setOnClickListener(new OnClickListener() {
  300. @Override
  301. public void onClick(View v) { loadSection(GM.SECTION_BLOG); }
  302. });
  303. menuItem[5].setOnClickListener(new OnClickListener() {
  304. @Override
  305. public void onClick(View v) { loadSection(GM.SECTION_GALLERY); }
  306. });
  307. //Get preferences
  308. SharedPreferences preferences = getSharedPreferences(GM.PREF, Context.MODE_PRIVATE);
  309. //TODO: Read preferences. If there is a recent location, show the menu entry
  310. //If the user code is not set, generate one
  311. if (preferences.getString(GM.USER_CODE, "").length() == 0){
  312. SecureRandom random = new SecureRandom();
  313. String newCode = new BigInteger(130, random).toString(32).substring(0, 16);
  314. SharedPreferences.Editor editor = preferences.edit();
  315. editor.putString(GM.USER_CODE, newCode);
  316. editor.apply();
  317. }
  318. //Cerate database if it's not already created
  319. createDatabase();
  320. //If its the first time
  321. if (preferences.getInt(GM.PREF_DB_VERSION, GM.DEFAULT_PREF_DB_VERSION) == GM.DEFAULT_PREF_DB_VERSION){
  322. initialSync();
  323. }
  324. //If t's not the first time
  325. else{
  326. //Sync db
  327. sync();
  328. //Load initial section
  329. loadSection(GM.SECTION_HOME);
  330. //If the intent had extras (from notifications), do something
  331. if (actionText != null){
  332. TextView tvDialogTitle, tvDialogText;
  333. Button btDialogClose, btDialogAction;
  334. Drawable dialogIcon;
  335. if (actionTitle != null){
  336. //Create a dialog
  337. final Dialog dialog = new Dialog(this);
  338. //Set up dialog window
  339. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  340. dialog.setContentView(R.layout.dialog_notification);
  341. //Set title
  342. tvDialogTitle = (TextView) dialog.findViewById(R.id.tv_dialog_notification_title);
  343. tvDialogTitle.setText(actionTitle);
  344. //Set text
  345. tvDialogText = (TextView) dialog.findViewById(R.id.tv_dialog_notification_text);
  346. tvDialogText.setText(actionText);
  347. //Set close button
  348. btDialogClose = (Button) dialog.findViewById(R.id.bt_dialog_notification_close);
  349. btDialogClose.setOnClickListener(new OnClickListener() {
  350. @Override
  351. public void onClick(View v) {
  352. dialog.dismiss();
  353. }
  354. });
  355. //Set the icon
  356. dialogIcon = getResources().getDrawable(R.drawable.ic_launcher);
  357. if (dialogIcon != null) {
  358. dialogIcon.setBounds(0, 0, (int) (tvDialogTitle.getTextSize() * 1.4), (int) (tvDialogTitle.getTextSize() * 1.4));
  359. }
  360. tvDialogTitle.setCompoundDrawables(dialogIcon, null, null, null);
  361. tvDialogTitle.setCompoundDrawablePadding(20);
  362. //Get preferences
  363. int festivals = preferences.getInt(GM.PREF_DB_FESTIVALS, 0);
  364. //Set the action button
  365. btDialogAction = (Button) dialog.findViewById(R.id.bt_dialog_notification_action);
  366. if (action != null){
  367. switch (action) {
  368. case GM.EXTRA_ACTION_LABLANCA:
  369. //Set up the action button
  370. btDialogAction.setVisibility(View.VISIBLE);
  371. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_lablanca));
  372. btDialogAction.setOnClickListener(new OnClickListener() {
  373. @Override
  374. public void onClick(View v) {
  375. dialog.dismiss();
  376. loadSection(GM.SECTION_LABLANCA);
  377. }
  378. });
  379. break;
  380. case GM.EXTRA_ACTION_LOCATION:
  381. if (!preferences.getString(GM.PREF_GM_LOCATION, GM.DEFAULT_PREF_GM_LOCATION).equals(GM.DEFAULT_PREF_GM_LOCATION)) {
  382. //Set up the action button if location is reported
  383. btDialogAction.setVisibility(View.VISIBLE);
  384. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_location));
  385. btDialogAction.setOnClickListener(new OnClickListener() {
  386. @Override
  387. public void onClick(View v) {
  388. dialog.dismiss();
  389. loadSection(GM.SECTION_LOCATION);
  390. }
  391. });
  392. }
  393. break;
  394. case GM.EXTRA_ACTION_BLOG:
  395. //Set up the action button
  396. btDialogAction.setVisibility(View.VISIBLE);
  397. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_blog));
  398. btDialogAction.setOnClickListener(new OnClickListener() {
  399. @Override
  400. public void onClick(View v) {
  401. dialog.dismiss();
  402. loadSection(GM.SECTION_BLOG);
  403. }
  404. });
  405. break;
  406. case GM.EXTRA_ACTION_ACTIVITIES:
  407. //Set up the action button
  408. btDialogAction.setVisibility(View.VISIBLE);
  409. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_activities));
  410. btDialogAction.setOnClickListener(new OnClickListener() {
  411. @Override
  412. public void onClick(View v) {
  413. dialog.dismiss();
  414. loadSection(GM.SECTION_ACTIVITIES);
  415. }
  416. });
  417. break;
  418. case GM.EXTRA_ACTION_GALLERY:
  419. //Set up the action button
  420. btDialogAction.setVisibility(View.VISIBLE);
  421. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_gallery));
  422. btDialogAction.setOnClickListener(new OnClickListener() {
  423. @Override
  424. public void onClick(View v) {
  425. dialog.dismiss();
  426. loadSection(GM.SECTION_GALLERY);
  427. }
  428. });
  429. break;
  430. case GM.EXTRA_ACTION_GMSCHEDULE:
  431. if (festivals == 1) {
  432. //Set up the action button
  433. btDialogAction.setVisibility(View.VISIBLE);
  434. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_gmschedule));
  435. btDialogAction.setOnClickListener(new OnClickListener() {
  436. @Override
  437. public void onClick(View v) {
  438. dialog.dismiss();
  439. loadSection(GM.SECTION_LABLANCA_GM_SCHEDULE);
  440. }
  441. });
  442. }
  443. break;
  444. case GM.EXTRA_ACTION_CITYSCHEDULE:
  445. if (festivals == 1) {
  446. //Set up the action button
  447. btDialogAction.setVisibility(View.VISIBLE);
  448. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_cityschedule));
  449. btDialogAction.setOnClickListener(new OnClickListener() {
  450. @Override
  451. public void onClick(View v) {
  452. dialog.dismiss();
  453. loadSection(GM.SECTION_LABLANCA_SCHEDULE);
  454. }
  455. });
  456. }
  457. break;
  458. //If the notification is just text
  459. default:
  460. //Hide action button
  461. btDialogAction.setVisibility(View.GONE);
  462. }
  463. }
  464. else{
  465. btDialogAction.setVisibility(View.GONE);
  466. }
  467. //Set dialog parameters
  468. WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  469. lp.copyFrom(dialog.getWindow().getAttributes());
  470. lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  471. lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
  472. lp.gravity = Gravity.CENTER;
  473. lp.dimAmount = 0.4f;
  474. dialog.getWindow().setAttributes(lp);
  475. //Show dialog
  476. dialog.show();
  477. }
  478. }
  479. }
  480. }
  481. /**
  482. * Creates the app database and fills it with the hard coded, default data.
  483. */
  484. private void createDatabase(){
  485. SQLiteDatabase db;
  486. try {
  487. //Create database
  488. db = openOrCreateDatabase(GM.DB_NAME, Activity.MODE_PRIVATE, null);
  489. db.execSQL("CREATE TABLE IF NOT EXISTS activity (id INT, permalink VARCHAR, date DATETIME, city VARCHAR, title_es VARCHAR, title_en VARCHAR, title_eu VARCHAR, text_es VARCHAR, text_en VARCHAR, text_eu VARCHAR, after_es VARCHAR, after_en VARCHAR, after_eu VARCHAR, price INT, inscription INT, max_people INT, album INT, dtime DATETIME, comments INT);");
  490. db.execSQL("CREATE TABLE IF NOT EXISTS activity_comment (id INT, activity INT, text VARCHAR, dtime DATETIME, username VARCHAR, lang VARCHAR);");
  491. db.execSQL("CREATE TABLE IF NOT EXISTS activity_image (id INT, activity INT, image VARCHAR, idx INT);");
  492. db.execSQL("CREATE TABLE IF NOT EXISTS activity_itinerary (id INT, activity INT, name_es VARCHAR, name_en VARCHAR, name_eu VARCHAR, description_es VARCHAR, description_en VARCHAR, description_eu VARCHAR, start DATETIME, end DATETIME, place INT);");
  493. db.execSQL("CREATE TABLE IF NOT EXISTS activity_tag (activity INT, tag VARCHAR);");
  494. db.execSQL("CREATE TABLE IF NOT EXISTS album (id INT, permalink VARCHAR, name_es VARCHAR, name_en VARCHAR, name_eu VARCHAR, description_es VARCHAR, description_en VARCHAR, description_eu VARCHAR, open INT, time DATETIME);");
  495. db.execSQL("CREATE TABLE IF NOT EXISTS festival (id INT, year INT, text_es VARCHAR, text_en VARCHAR, text_eu VARCHAR, img VARCHAR);");
  496. db.execSQL("CREATE TABLE IF NOT EXISTS festival_day (id INT, date DATETIME, name_es VARCHAR, name_en VARCHAR, name_eu VARCHAR, price INT);");
  497. db.execSQL("CREATE TABLE IF NOT EXISTS festival_event (id INT, gm INT, title_es VARCHAR, title_en VARCHAR, title_eu VARCHAR, description_es VARCHAR, description_en VARCHAR, description_eu VARCHAR, host INT, place INT, start DATETIME, end DATETIME);");
  498. db.execSQL("CREATE TABLE IF NOT EXISTS festival_event_image (id INT, event INT, image VARCHAR, idx INT);");
  499. db.execSQL("CREATE TABLE IF NOT EXISTS festival_offer (id INT, year INT, name_es VARCHAR, name_en VARCHAR, name_eu VARCHAR, description_es VARCHAR, description_en VARCHAR, description_eu VARCHAR, days INT, price INT);");
  500. db.execSQL("CREATE TABLE IF NOT EXISTS location (id INT, dtime DATETIME, lat FLOAT, lon FLOAT, manual INT);");
  501. db.execSQL("CREATE TABLE IF NOT EXISTS notification (id INT, dtime DATETIME, duration INT, action VARCHAR, title_es VARCHAR, title_en VARCHAR, title_eu VARCHAR, text_es VARCHAR, text_en VARCHAR, text_eu VARCHAR, internal INT);");
  502. db.execSQL("CREATE TABLE IF NOT EXISTS people (id INT, name_es VARCHAR, name_en VARCHAR, name_eu VARCHAR, link VARCHAR);");
  503. db.execSQL("CREATE TABLE IF NOT EXISTS photo (id INT, file VARCHAR, permalink VARCHAR, title_es VARCHAR, title_en VARCHAR, title_eu VARCHAR, description_es VARCHAR, description_en VARCHAR, description_eu VARCHAR, dtime DATETIME, uploaded DATETIME, place INT, width INT, height INT, size INT, username VARCHAR);");
  504. db.execSQL("CREATE TABLE IF NOT EXISTS photo_album (photo INT, album INT);");
  505. db.execSQL("CREATE TABLE IF NOT EXISTS photo_comment (id INT, photo INT, text VARCHAR, dtime DATETIME, username VARCHAR, lang VARCHAR);");
  506. db.execSQL("CREATE TABLE IF NOT EXISTS place (id INT, name_es VARCHAR, name_en VARCHAR, name_eu VARCHAR, address_es VARCHAR, address_en VARCHAR, address_eu VARCHAR, cp VARCHAR, lat FLOAT, lon FLOAT);");
  507. db.execSQL("CREATE TABLE IF NOT EXISTS post (id INT, permalink VARCHAR, title_es VARCHAR, title_en VARCHAR, title_eu VARCHAR, text_es VARCHAR, text_en VARCHAR, text_eu VARCHAR, dtime DATETIME, comments INT, username VARCHAR);");
  508. db.execSQL("CREATE TABLE IF NOT EXISTS post_comment (id INT, post INT, text VARCHAR, dtime DATETIME, username VARCHAR, lang VARCHAR);");
  509. db.execSQL("CREATE TABLE IF NOT EXISTS post_image (id INT, post INT, image VARCHAR, idx INT);");
  510. db.execSQL("CREATE TABLE IF NOT EXISTS post_tag (post INT, tag VARCHAR);");
  511. db.execSQL("CREATE TABLE IF NOT EXISTS settings (name VARCHAR, value VARCHAR);");
  512. db.execSQL("CREATE TABLE IF NOT EXISTS sponsor (id INT, name_es VARCHAR, name_en VARCHAR, name_eu VARCHAR, text_es VARCHAR, text_en VARCHAR, text_eu VARCHAR, image VARCHAR, address_es VARCHAR, address_en VARCHAR, address_eu VARCHAR, link VARCHAR, lat FLOAT, lon FLOAT)");
  513. db.execSQL("CREATE TABLE IF NOT EXISTS version (section VARCHAR, version INT);");
  514. db.close();
  515. }
  516. catch (Exception ex){
  517. Log.e("Error creating database", ex.toString());
  518. }
  519. }
  520. /**
  521. * Performs a full sync against the remote database.
  522. */
  523. private void sync(){
  524. ProgressBar pbSync = (ProgressBar) findViewById(R.id.pb_sync);
  525. ImageView ivSync = (ImageView) findViewById(R.id.iv_logo);
  526. new Sync(this, pbSync, ivSync).execute();
  527. }
  528. /**
  529. * Perform an initial sync before the app can be used.
  530. * A dialog will block the UI.
  531. * It is intended to be used only when the database is empty.
  532. */
  533. @SuppressWarnings("ConstantConditions")
  534. private void initialSync(){
  535. //Create a dialog
  536. Dialog dialog = new Dialog(this);
  537. dialog.setCancelable(false);
  538. //Set up the window
  539. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  540. dialog.setContentView(R.layout.dialog_sync);
  541. //Set dialog parameters
  542. WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  543. lp.copyFrom(dialog.getWindow().getAttributes());
  544. lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  545. lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
  546. lp.gravity = Gravity.CENTER;
  547. lp.dimAmount = 0.4f;
  548. dialog.getWindow().setAttributes(lp);
  549. //Sync
  550. ProgressBar pbSync = (ProgressBar) findViewById(R.id.pb_sync);
  551. ImageView ivSync = (ImageView) findViewById(R.id.iv_logo);
  552. new Sync(this, pbSync, ivSync, dialog, this).execute();
  553. }
  554. /**
  555. * Overrides onBackPressed().
  556. * Used to show the previous fragment instead of finishing the app.
  557. *
  558. * @see android.support.v7.app.AppCompatActivity#onBackPressed()
  559. */
  560. @Override
  561. public void onBackPressed(){
  562. try {
  563. FragmentManager fm = getFragmentManager();
  564. if (fm.getBackStackEntryCount() > 1) {
  565. fm.popBackStack();
  566. } else {
  567. finish();
  568. super.onBackPressed();
  569. }
  570. }
  571. catch (Exception ex){
  572. Log.e("Error going back", "Finishing activity: " + ex.toString());
  573. Log.e("RUNNABLE", "Finishing callback for location");
  574. locationHandler.removeCallbacks(checkForLocation);
  575. finish();
  576. //super.onBackPressed();
  577. }
  578. }
  579. /**
  580. * Overrides onKeyUp().
  581. * Used to toggle the menu whent the key is pressed.
  582. *
  583. * @see android.app.Activity#onKeyUp(int, KeyEvent)
  584. */
  585. @Override
  586. public boolean onKeyUp(int keyCode, KeyEvent event) {
  587. if (keyCode == KeyEvent.KEYCODE_MENU) {
  588. showMenu(this.findViewById(R.id.bt_menu));
  589. return true;
  590. }
  591. return super.onKeyUp(keyCode, event);
  592. }
  593. @Override
  594. public void onResume(){
  595. locationHandler.post(checkForLocation);
  596. super.onResume();
  597. }
  598. @Override
  599. public void onPause(){
  600. locationHandler.removeCallbacks(checkForLocation);
  601. super.onPause();
  602. }
  603. @Override
  604. public void onDestroy(){
  605. locationHandler.removeCallbacks(checkForLocation);
  606. super.onDestroy();
  607. }
  608. }