MainActivity.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. package com.ivalentin.margolariak;
  2. import java.math.BigInteger;
  3. import java.security.SecureRandom;
  4. import android.app.Activity;
  5. import android.app.Fragment;
  6. import android.app.FragmentManager;
  7. import android.app.FragmentTransaction;
  8. import android.app.Dialog;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.content.SharedPreferences;
  12. import android.database.sqlite.SQLiteDatabase;
  13. import android.graphics.Color;
  14. import android.graphics.Typeface;
  15. import android.graphics.drawable.ColorDrawable;
  16. import android.graphics.drawable.Drawable;
  17. import android.net.Uri;
  18. import android.os.Bundle;
  19. import android.os.StrictMode;
  20. import android.util.DisplayMetrics;
  21. import android.util.Log;
  22. import android.view.Gravity;
  23. import android.view.KeyEvent;
  24. import android.view.MenuInflater;
  25. import android.view.MenuItem;
  26. import android.view.View;
  27. import android.view.Window;
  28. import android.view.View.OnClickListener;
  29. import android.view.WindowManager;
  30. import android.widget.Button;
  31. import android.widget.HorizontalScrollView;
  32. import android.widget.ImageView;
  33. import android.widget.PopupMenu;
  34. import android.widget.ProgressBar;
  35. import android.widget.RelativeLayout;
  36. import android.widget.TextView;
  37. import android.os.Handler;
  38. /**
  39. * The main Activity of the app. It's actually the only content activity, and it loads other fragments.
  40. *
  41. * @author Iñigo Valentin
  42. */
  43. public class MainActivity extends Activity {
  44. //Alarm to get location and notifications.
  45. private AlarmReceiver notificationAlarm;
  46. private TextView menuText[];
  47. private ImageView menuImage[];
  48. //Handler to periodically check for location updates
  49. private final Handler locationHandler = new Handler();
  50. // Code to check for location updates
  51. private MainActivity activity = this;
  52. private final Runnable checkForLocation = new Runnable() {
  53. @Override
  54. public void run() {
  55. new ReceiveLocation(activity).execute();
  56. //Repeat this the same runnable code block again another th specified interval
  57. locationHandler.postDelayed(checkForLocation, GM.LOCATION.INTERVAL);
  58. }
  59. };
  60. // Shareable URL
  61. private String shareURL = GM.SHARE.HOME;
  62. private String shareTitle = "";
  63. /**
  64. * Sets the displayed section url and localized title.
  65. * Thy can be retrieved later with getShareLink() to share the URL.
  66. *
  67. * @param title The title of the displayed section.
  68. * @param url The URL of the current section.
  69. *
  70. * @see MainActivity#getShareLink();
  71. */
  72. public void setShareLink(String title, String url){
  73. shareTitle = title;
  74. shareURL = url;
  75. }
  76. /**
  77. * Returns the url of the current sections and a title.
  78. * To be used with the share option.
  79. *
  80. * @return String array. Index 0 has the title of the section. Index 1 has the URL.
  81. */
  82. public String[] getShareLink(){
  83. return(new String[]{shareTitle, shareURL});
  84. }
  85. /**
  86. * Enables o disables the location section.
  87. *
  88. * Shows or hiddes the menu entry and the home fragment,
  89. *
  90. * @param report True to show, false to hide.
  91. */
  92. public void gotLocation(boolean report){
  93. if (report) {
  94. findViewById(R.id.rl_menu_location).setVisibility(View.VISIBLE);
  95. if (findViewById(R.id.ll_home_section_location) != null){
  96. findViewById(R.id.ll_home_section_location).setVisibility(View.VISIBLE);
  97. }
  98. }
  99. else {
  100. findViewById(R.id.rl_menu_location).setVisibility(View.GONE);
  101. if (findViewById(R.id.ll_home_section_location) != null) {
  102. findViewById(R.id.ll_home_section_location).setVisibility(View.GONE);
  103. }
  104. }
  105. }
  106. /**
  107. * Opens and closes the app menu.
  108. * Not referenced in code.
  109. *
  110. * @param v Menu view.
  111. */
  112. public void showMenu(View v) {
  113. PopupMenu popup = new PopupMenu(this, v);
  114. MenuInflater inflater = popup.getMenuInflater();
  115. inflater.inflate(R.menu.menu, popup.getMenu());
  116. popup.getMenu().getItem(0).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
  117. @Override
  118. public boolean onMenuItemClick(MenuItem item) {
  119. Intent sendIntent = new Intent();
  120. sendIntent.setAction(Intent.ACTION_SEND);
  121. sendIntent.putExtra(Intent.EXTRA_TEXT, getShareLink()[0] + "\n\n" + getShareLink()[1] + "\n\n");
  122. sendIntent.setType("text/plain");
  123. startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.share_with)));
  124. return true;
  125. }
  126. });
  127. popup.getMenu().getItem(1).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
  128. @Override
  129. public boolean onMenuItemClick(MenuItem item) {
  130. Intent intent = new Intent(MainActivity.this, UsActivity.class);
  131. startActivity(intent);
  132. return true;
  133. }
  134. });
  135. popup.getMenu().getItem(2).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
  136. @Override
  137. public boolean onMenuItemClick(MenuItem item) {
  138. Intent intent = new Intent(MainActivity.this, SponsorActivity.class);
  139. startActivity(intent);
  140. return true;
  141. }
  142. });
  143. popup.getMenu().getItem(3).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
  144. @Override
  145. public boolean onMenuItemClick(MenuItem item) {
  146. Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
  147. startActivity(intent);
  148. return true;
  149. }
  150. });
  151. popup.show();
  152. }
  153. /**
  154. * Loads a section in the main screen.
  155. *
  156. * @param section Section identifier {@see GM}
  157. */
  158. public void loadSection(byte section) {
  159. FragmentManager fm = MainActivity.this.getFragmentManager();
  160. FragmentTransaction ft = fm.beginTransaction();
  161. Fragment fragment = null;
  162. String title = "";
  163. String shareURL = GM.SHARE.HOME;
  164. String shareTitle = getString(R.string.share_home);
  165. Bundle bundle = new Bundle();
  166. //Get measures in dp
  167. DisplayMetrics metrics = getResources().getDisplayMetrics();
  168. int dp7 = (int) (8 * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
  169. int dp3 = (int) (4 * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
  170. //Reset all tabs to it's original state
  171. for (int i = 0; i < 6; i++) {
  172. menuText[i].setTypeface(null, Typeface.NORMAL);
  173. menuImage[i].requestLayout();
  174. menuImage[i].getLayoutParams().height = dp3;
  175. }
  176. switch (section) {
  177. case GM.SECTION.HOME:
  178. fragment = new HomeLayout();
  179. title = getString(R.string.menu_home);
  180. shareURL = GM.SHARE.HOME;
  181. shareTitle = getString(R.string.share_home);
  182. menuText[0].setTypeface(null, Typeface.BOLD);
  183. menuImage[0].getLayoutParams().height = dp7;
  184. break;
  185. case GM.SECTION.LOCATION:
  186. fragment = new LocationLayout();
  187. title = getString(R.string.menu_location);
  188. shareURL = GM.SHARE.HOME;
  189. shareTitle = getString(R.string.share_home);
  190. menuText[1].setTypeface(null, Typeface.BOLD);
  191. menuImage[1].getLayoutParams().height = dp7;
  192. break;
  193. case GM.SECTION.LABLANCA:
  194. //Get settings
  195. SharedPreferences sharedData = getSharedPreferences(GM.DATA.DATA, Context.MODE_PRIVATE);
  196. if (sharedData.getBoolean(GM.DATA.KEY.LABLANCA, GM.DATA.DEFAULT.LABLANCA)) {
  197. fragment = new LablancaLayout();
  198. } else {
  199. fragment = new LablancaNoFestivalsLayout();
  200. }
  201. title = getString(R.string.menu_lablanca);
  202. shareURL = GM.SHARE.LABLANCA;
  203. shareTitle = getString(R.string.share_lablanca);
  204. menuText[2].setTypeface(null, Typeface.BOLD);
  205. menuImage[2].getLayoutParams().height = dp7;
  206. break;
  207. case GM.SECTION.SCHEDULE:
  208. fragment = new ScheduleLayout();
  209. bundle.putInt(GM.SCHEDULE.KEY, GM.SCHEDULE.CITY);
  210. fragment.setArguments(bundle);
  211. title = getString(R.string.menu_lablanca_schedule);
  212. shareURL = GM.SHARE.LABLANCA;
  213. shareTitle = getString(R.string.share_lablanca);
  214. menuText[2].setTypeface(null, Typeface.BOLD);
  215. menuImage[2].getLayoutParams().height = dp7;
  216. break;
  217. case GM.SECTION.GM_SCHEDULE:
  218. fragment = new ScheduleLayout();
  219. bundle.putInt(GM.SCHEDULE.KEY, GM.SCHEDULE.MARGOLARIAK);
  220. fragment.setArguments(bundle);
  221. title = getString(R.string.menu_lablanca_gm_schedule);
  222. shareURL = GM.SHARE.LABLANCA;
  223. shareTitle = getString(R.string.share_lablanca);
  224. menuText[2].setTypeface(null, Typeface.BOLD);
  225. menuImage[2].getLayoutParams().height = dp7;
  226. break;
  227. case GM.SECTION.ACTIVITIES:
  228. fragment = new ActivityLayout();
  229. title = getString(R.string.menu_activities);
  230. shareURL = GM.SHARE.ACTIVITIES;
  231. shareTitle = getString(R.string.share_activities);
  232. menuText[3].setTypeface(null, Typeface.BOLD);
  233. menuImage[3].getLayoutParams().height = dp7;
  234. break;
  235. case GM.SECTION.BLOG:
  236. fragment = new BlogLayout();
  237. title = getString(R.string.menu_blog);
  238. shareURL = GM.SHARE.BLOG;
  239. shareTitle = getString(R.string.share_blog);
  240. menuText[4].setTypeface(null, Typeface.BOLD);
  241. menuImage[4].getLayoutParams().height = dp7;
  242. break;
  243. case GM.SECTION.GALLERY:
  244. fragment = new GalleryLayout();
  245. title = getString(R.string.menu_blog);
  246. shareURL = GM.SHARE.GALLERY;
  247. shareTitle = getString(R.string.share_gallery);
  248. menuText[5].setTypeface(null, Typeface.BOLD);
  249. menuImage[5].getLayoutParams().height = dp7;
  250. break;
  251. }
  252. //Replace the fragment.
  253. ft.replace(R.id.activity_main_content_fragment, fragment);
  254. ft.addToBackStack(title);
  255. ft.commit();
  256. setSectionTitle(title);
  257. //Set the shareable url
  258. setShareLink(shareTitle, shareURL);
  259. }
  260. /**
  261. * Sets the title of the current section.
  262. *
  263. * @param title The title of the current section.
  264. */
  265. public void setSectionTitle(String title) {
  266. TextView tvTitle = (TextView) findViewById(R.id.activity_main_content_title);
  267. tvTitle.setText(title);
  268. }
  269. /**
  270. * Runs when the activity is created.
  271. *
  272. * @param savedInstanceState Saved state of the activity.
  273. * @see Activity#onCreate(Bundle)
  274. */
  275. @SuppressWarnings("deprecation, ConstantConditions")
  276. @Override
  277. protected void onCreate(Bundle savedInstanceState) {
  278. //Get intent extras
  279. String action = getIntent().getStringExtra(GM.EXTRA.ACTION);
  280. String actionText = getIntent().getStringExtra(GM.EXTRA.TEXT);
  281. String actionTitle = getIntent().getStringExtra(GM.EXTRA.TITLE);
  282. //Set an alarm for notifications..
  283. //Wait a little
  284. final Intent intent = this.getIntent();
  285. final Context context = this;
  286. Thread t = new Thread() {
  287. @Override
  288. public void run() {
  289. try {
  290. Thread.sleep(20000);
  291. runOnUiThread(new Runnable() {
  292. @Override
  293. public void run() {
  294. notificationAlarm = new AlarmReceiver();
  295. notificationAlarm.setAlarm(context);
  296. notificationAlarm.onReceive(context, intent);
  297. }
  298. });
  299. } catch (InterruptedException e) {
  300. Log.e("Sleep interrupted", e.toString());
  301. }
  302. }
  303. };
  304. t.start();
  305. //Remove title bar.
  306. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  307. super.onCreate(savedInstanceState);
  308. //Set layout.
  309. setContentView(R.layout.activity_main);
  310. //Assign menu items
  311. RelativeLayout[] menuItem = new RelativeLayout[6];
  312. menuItem[0] = (RelativeLayout) findViewById(R.id.rl_menu_home);
  313. menuItem[1] = (RelativeLayout) findViewById(R.id.rl_menu_location);
  314. menuItem[2] = (RelativeLayout) findViewById(R.id.rl_menu_lablanca);
  315. menuItem[3] = (RelativeLayout) findViewById(R.id.rl_menu_activities);
  316. menuItem[4] = (RelativeLayout) findViewById(R.id.rl_menu_blog);
  317. menuItem[5] = (RelativeLayout) findViewById(R.id.rl_menu_gallery);
  318. menuText = new TextView[6];
  319. menuText[0] = (TextView) menuItem[0].findViewById(R.id.tv_menu_home);
  320. menuText[1] = (TextView) menuItem[1].findViewById(R.id.tv_menu_location);
  321. menuText[2] = (TextView) menuItem[2].findViewById(R.id.tv_menu_lablanca);
  322. menuText[3] = (TextView) menuItem[3].findViewById(R.id.tv_menu_activities);
  323. menuText[4] = (TextView) menuItem[4].findViewById(R.id.tv_menu_blog);
  324. menuText[5] = (TextView) menuItem[5].findViewById(R.id.tv_menu_gallery);
  325. menuImage = new ImageView[6];
  326. menuImage[0] = (ImageView) menuItem[0].findViewById(R.id.iv_menu_home);
  327. menuImage[1] = (ImageView) menuItem[1].findViewById(R.id.iv_menu_location);
  328. menuImage[2] = (ImageView) menuItem[2].findViewById(R.id.iv_menu_lablanca);
  329. menuImage[3] = (ImageView) menuItem[3].findViewById(R.id.iv_menu_activities);
  330. menuImage[4] = (ImageView) menuItem[4].findViewById(R.id.iv_menu_blog);
  331. menuImage[5] = (ImageView) menuItem[5].findViewById(R.id.iv_menu_gallery);
  332. //Remove scrollbars from the sections menu
  333. HorizontalScrollView svMenu = (HorizontalScrollView) findViewById(R.id.sv_menu);
  334. svMenu.setHorizontalScrollBarEnabled(false);
  335. //Set click listers for menu items
  336. menuItem[0].setOnClickListener(new OnClickListener() {
  337. @Override
  338. public void onClick(View v) {
  339. loadSection(GM.SECTION.HOME);
  340. }
  341. });
  342. menuItem[1].setOnClickListener(new OnClickListener() {
  343. @Override
  344. public void onClick(View v) {
  345. loadSection(GM.SECTION.LOCATION);
  346. }
  347. });
  348. menuItem[2].setOnClickListener(new OnClickListener() {
  349. @Override
  350. public void onClick(View v) {
  351. loadSection(GM.SECTION.LABLANCA);
  352. }
  353. });
  354. menuItem[3].setOnClickListener(new OnClickListener() {
  355. @Override
  356. public void onClick(View v) {
  357. loadSection(GM.SECTION.ACTIVITIES);
  358. }
  359. });
  360. menuItem[4].setOnClickListener(new OnClickListener() {
  361. @Override
  362. public void onClick(View v) {
  363. loadSection(GM.SECTION.BLOG);
  364. }
  365. });
  366. menuItem[5].setOnClickListener(new OnClickListener() {
  367. @Override
  368. public void onClick(View v) {
  369. loadSection(GM.SECTION.GALLERY);
  370. }
  371. });
  372. //Get preferences
  373. SharedPreferences sharedData = getSharedPreferences(GM.DATA.DATA, Context.MODE_PRIVATE);
  374. SharedPreferences.Editor dataEditor = sharedData.edit();
  375. //If the user code is not set, generate one
  376. if (sharedData.getString(GM.DATA.KEY.USER, GM.DATA.DEFAULT.USER).length() == GM.DATA.DEFAULT.USER.length()) {
  377. SecureRandom random = new SecureRandom();
  378. String newCode = new BigInteger(130, random).toString(32).substring(0, 16);
  379. dataEditor.putString(GM.DATA.KEY.USER, newCode);
  380. dataEditor.apply();
  381. }
  382. //Cerate database if it's not already created
  383. createDatabase();
  384. //If the database has just been updated, recreate the database
  385. if (sharedData.getInt(GM.DATA.KEY.PREVIOUS_APP_VERSION, GM.DATA.DEFAULT.PREVIOUS_APP_VERSION) < BuildConfig.VERSION_CODE) {
  386. Log.d("UPDATE", "App updated from version " + sharedData.getInt(GM.DATA.KEY.PREVIOUS_APP_VERSION, GM.DATA.DEFAULT.PREVIOUS_APP_VERSION) + " to " + BuildConfig.VERSION_CODE + ". Forcing a new sync...");
  387. deleteDatabase();
  388. dataEditor.putInt(GM.DATA.KEY.PREVIOUS_APP_VERSION, BuildConfig.VERSION_CODE);
  389. dataEditor.apply();
  390. createDatabase();
  391. initialSync();
  392. }
  393. else {
  394. //Sync db
  395. sync();
  396. //Load initial section
  397. loadSection(GM.SECTION.HOME);
  398. //If the intent had extras (from notifications), do something
  399. if (actionText != null) {
  400. TextView tvDialogTitle, tvDialogText;
  401. Button btDialogClose, btDialogAction;
  402. Drawable dialogIcon;
  403. if (actionTitle != null) {
  404. //Create a dialog
  405. final Dialog dialog = new Dialog(this);
  406. //Set up dialog window
  407. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  408. dialog.setContentView(R.layout.dialog_notification);
  409. //Set title
  410. tvDialogTitle = (TextView) dialog.findViewById(R.id.tv_dialog_notification_title);
  411. tvDialogTitle.setText(actionTitle);
  412. //Set text
  413. tvDialogText = (TextView) dialog.findViewById(R.id.tv_dialog_notification_text);
  414. tvDialogText.setText(actionText);
  415. //Set close button
  416. btDialogClose = (Button) dialog.findViewById(R.id.bt_dialog_notification_close);
  417. btDialogClose.setOnClickListener(new OnClickListener() {
  418. @Override
  419. public void onClick(View v) {
  420. dialog.dismiss();
  421. }
  422. });
  423. //Set the icon
  424. dialogIcon = getResources().getDrawable(R.drawable.ic_launcher);
  425. if (dialogIcon != null) {
  426. dialogIcon.setBounds(0, 0, (int) (tvDialogTitle.getTextSize() * 1.4), (int) (tvDialogTitle.getTextSize() * 1.4));
  427. }
  428. tvDialogTitle.setCompoundDrawables(dialogIcon, null, null, null);
  429. tvDialogTitle.setCompoundDrawablePadding(20);
  430. //Get preferences
  431. boolean festivals = sharedData.getBoolean(GM.DATA.KEY.LABLANCA, GM.DATA.DEFAULT.LABLANCA);
  432. //Set the action button
  433. btDialogAction = (Button) dialog.findViewById(R.id.bt_dialog_notification_action);
  434. if (action != null) {
  435. switch (action) {
  436. case GM.EXTRA.SECTION.LABLANCA:
  437. //Set up the action button
  438. btDialogAction.setVisibility(View.VISIBLE);
  439. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_lablanca));
  440. btDialogAction.setOnClickListener(new OnClickListener() {
  441. @Override
  442. public void onClick(View v) {
  443. dialog.dismiss();
  444. loadSection(GM.SECTION.LABLANCA);
  445. }
  446. });
  447. break;
  448. case GM.EXTRA.SECTION.LOCATION:
  449. //TODO: Read from database
  450. /*if (!sharedData.getString(GM.PREF_GM_LOCATION, GM.DEFAULT_PREF_GM_LOCATION).equals(GM.DEFAULT_PREF_GM_LOCATION)) {
  451. //Set up the action button if location is reported
  452. btDialogAction.setVisibility(View.VISIBLE);
  453. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_location));
  454. btDialogAction.setOnClickListener(new OnClickListener() {
  455. @Override
  456. public void onClick(View v) {
  457. dialog.dismiss();
  458. loadSection(GM.SECTION_LOCATION);
  459. }
  460. });
  461. }*/
  462. break;
  463. case GM.EXTRA.SECTION.BLOG:
  464. //Set up the action button
  465. btDialogAction.setVisibility(View.VISIBLE);
  466. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_blog));
  467. btDialogAction.setOnClickListener(new OnClickListener() {
  468. @Override
  469. public void onClick(View v) {
  470. dialog.dismiss();
  471. loadSection(GM.SECTION.BLOG);
  472. }
  473. });
  474. break;
  475. case GM.EXTRA.SECTION.ACTIVITIES:
  476. //Set up the action button
  477. btDialogAction.setVisibility(View.VISIBLE);
  478. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_activities));
  479. btDialogAction.setOnClickListener(new OnClickListener() {
  480. @Override
  481. public void onClick(View v) {
  482. dialog.dismiss();
  483. loadSection(GM.SECTION.ACTIVITIES);
  484. }
  485. });
  486. break;
  487. case GM.EXTRA.SECTION.GALLERY:
  488. //Set up the action button
  489. btDialogAction.setVisibility(View.VISIBLE);
  490. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_gallery));
  491. btDialogAction.setOnClickListener(new OnClickListener() {
  492. @Override
  493. public void onClick(View v) {
  494. dialog.dismiss();
  495. loadSection(GM.SECTION.GALLERY);
  496. }
  497. });
  498. break;
  499. case GM.EXTRA.SECTION.GMSCHEDULE:
  500. if (festivals) {
  501. //Set up the action button
  502. btDialogAction.setVisibility(View.VISIBLE);
  503. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_gmschedule));
  504. btDialogAction.setOnClickListener(new OnClickListener() {
  505. @Override
  506. public void onClick(View v) {
  507. dialog.dismiss();
  508. loadSection(GM.SECTION.GM_SCHEDULE);
  509. }
  510. });
  511. }
  512. break;
  513. case GM.EXTRA.SECTION.SCHEDULE:
  514. if (festivals) {
  515. //Set up the action button
  516. btDialogAction.setVisibility(View.VISIBLE);
  517. btDialogAction.setText(this.getApplicationContext().getString(R.string.notification_action_cityschedule));
  518. btDialogAction.setOnClickListener(new OnClickListener() {
  519. @Override
  520. public void onClick(View v) {
  521. dialog.dismiss();
  522. loadSection(GM.SECTION.SCHEDULE);
  523. }
  524. });
  525. }
  526. break;
  527. //If the notification is just text
  528. default:
  529. //Hide action button
  530. btDialogAction.setVisibility(View.GONE);
  531. }
  532. } else {
  533. btDialogAction.setVisibility(View.GONE);
  534. }
  535. //Set dialog parameters
  536. WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  537. lp.copyFrom(dialog.getWindow().getAttributes());
  538. lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  539. lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
  540. lp.gravity = Gravity.CENTER;
  541. lp.dimAmount = 0.4f;
  542. dialog.getWindow().setAttributes(lp);
  543. dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  544. //Show dialog
  545. dialog.show();
  546. }
  547. }
  548. }
  549. }
  550. /**
  551. * Creates the app database and fills it with the hard coded, default data.
  552. */
  553. private void createDatabase() {
  554. SQLiteDatabase db;
  555. try {
  556. //Create database
  557. db = openOrCreateDatabase(GM.DB.NAME, Activity.MODE_PRIVATE, null);
  558. db.execSQL(GM.DB.QUERY.CREATE.ACTIVITY);
  559. db.execSQL(GM.DB.QUERY.CREATE.ACTIVITY_COMMENT);
  560. db.execSQL(GM.DB.QUERY.CREATE.ACTIVITY_IMAGE);
  561. db.execSQL(GM.DB.QUERY.CREATE.ACTIVITY_ITINERARY);
  562. db.execSQL(GM.DB.QUERY.CREATE.ACTIVITY_TAG);
  563. db.execSQL(GM.DB.QUERY.CREATE.ALBUM);
  564. db.execSQL(GM.DB.QUERY.CREATE.FESTIVAL);
  565. db.execSQL(GM.DB.QUERY.CREATE.FESTIVAL_DAY);
  566. db.execSQL(GM.DB.QUERY.CREATE.FESTIVAL_EVENT_CITY);
  567. db.execSQL(GM.DB.QUERY.CREATE.FESTIVAL_EVENT_GM);
  568. db.execSQL(GM.DB.QUERY.CREATE.FESTIVAL_EVENT_IMAGE);
  569. db.execSQL(GM.DB.QUERY.CREATE.FESTIVAL_OFFER);
  570. db.execSQL(GM.DB.QUERY.CREATE.LOCATION);
  571. db.execSQL(GM.DB.QUERY.CREATE.NOTIFICATION);
  572. db.execSQL(GM.DB.QUERY.CREATE.PEOPLE);
  573. db.execSQL(GM.DB.QUERY.CREATE.PHOTO);
  574. db.execSQL(GM.DB.QUERY.CREATE.PHOTO_ALBUM);
  575. db.execSQL(GM.DB.QUERY.CREATE.PHOTO_COMMENT);
  576. db.execSQL(GM.DB.QUERY.CREATE.PLACE);
  577. db.execSQL(GM.DB.QUERY.CREATE.POST);
  578. db.execSQL(GM.DB.QUERY.CREATE.POST_COMMENT);
  579. db.execSQL(GM.DB.QUERY.CREATE.POST_IMAGE);
  580. db.execSQL(GM.DB.QUERY.CREATE.POST_TAG);
  581. db.execSQL(GM.DB.QUERY.CREATE.ROUTE);
  582. db.execSQL(GM.DB.QUERY.CREATE.ROUTE_POINT);
  583. //db.execSQL(GM.DB.QUERY.CREATE.SETTINGS);
  584. db.execSQL(GM.DB.QUERY.CREATE.SPONSOR);
  585. db.execSQL(GM.DB.QUERY.CREATE.VERSION);
  586. db.close();
  587. } catch (Exception ex) {
  588. Log.e("Error creating database", ex.toString());
  589. }
  590. }
  591. /**
  592. * Creates the app database and fills it with the hard coded, default data.
  593. */
  594. private void deleteDatabase() {
  595. try {
  596. getApplicationContext().deleteDatabase(GM.DB.NAME);
  597. } catch (Exception ex) {
  598. Log.e("Error deleting database", ex.toString());
  599. }
  600. }
  601. /**
  602. * Performs a full sync against the remote database.
  603. */
  604. private void sync() {
  605. ProgressBar pbSync = (ProgressBar) findViewById(R.id.pb_sync);
  606. ImageView ivSync = (ImageView) findViewById(R.id.iv_logo);
  607. new Sync(this, pbSync, ivSync).execute();
  608. }
  609. /**
  610. * Asks the activity to performa a sync.
  611. * Intended to be called from any screen.
  612. */
  613. public void bgSync(){
  614. new Sync(this).execute();
  615. }
  616. /**
  617. * Perform an initial sync before the app can be used.
  618. * A dialog will block the UI.
  619. * It is intended to be used only when the database is empty.
  620. */
  621. @SuppressWarnings("ConstantConditions")
  622. private void initialSync() {
  623. //Create a dialog
  624. Dialog dialog = new Dialog(this);
  625. dialog.setCancelable(false);
  626. //Set up the window
  627. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  628. dialog.setContentView(R.layout.dialog_sync);
  629. //Set dialog parameters
  630. WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  631. lp.copyFrom(dialog.getWindow().getAttributes());
  632. lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  633. lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
  634. lp.gravity = Gravity.CENTER;
  635. dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  636. dialog.getWindow().setAttributes(lp);
  637. //Sync
  638. ProgressBar pbSync = (ProgressBar) findViewById(R.id.pb_sync);
  639. ImageView ivSync = (ImageView) findViewById(R.id.iv_logo);
  640. new Sync(this, pbSync, ivSync, dialog, this).execute();
  641. }
  642. /**
  643. * Overrides onBackPressed().
  644. * Used to show the previous fragment instead of finishing the app.
  645. *
  646. * @see AppCompatActivity#onBackPressed()
  647. */
  648. @Override
  649. public void onBackPressed() {
  650. try {
  651. FragmentManager fm = getFragmentManager();
  652. Log.d("BACK", "StackEntryCount " + fm.getBackStackEntryCount());
  653. if (fm.getBackStackEntryCount() > 1) {
  654. fm.popBackStack();
  655. } else {
  656. finish();
  657. super.onBackPressed();
  658. }
  659. } catch (Exception ex) {
  660. Log.e("Error going back", "Finishing activity: " + ex.toString());
  661. locationHandler.removeCallbacks(checkForLocation);
  662. finish();
  663. //super.onBackPressed();
  664. }
  665. }
  666. /**
  667. * Overrides onKeyUp().
  668. * Used to toggle the menu whent the key is pressed.
  669. *
  670. * @see Activity#onKeyUp(int, KeyEvent)
  671. */
  672. @Override
  673. public boolean onKeyUp(int keyCode, KeyEvent event) {
  674. if (keyCode == KeyEvent.KEYCODE_MENU) {
  675. showMenu(this.findViewById(R.id.bt_menu));
  676. return true;
  677. }
  678. return super.onKeyUp(keyCode, event);
  679. }
  680. /**
  681. * Overrides onResume().
  682. * Resumes the location handler to check for location regularly.
  683. *
  684. * @see Activity#onResume()
  685. */
  686. @Override
  687. public void onResume() {
  688. locationHandler.post(checkForLocation);
  689. super.onResume();
  690. }
  691. /**
  692. * Overrides onPause().
  693. * Stops the location handler to check for location regularly.
  694. *
  695. * @see Activity#onPause()
  696. */
  697. @Override
  698. public void onPause() {
  699. locationHandler.removeCallbacks(checkForLocation);
  700. super.onPause();
  701. }
  702. /**
  703. * Overrides onDestroy().
  704. * Stops the location handler to check for location regularly.
  705. *
  706. * @see Activity#onDestroy()
  707. */
  708. @Override
  709. public void onDestroy() {
  710. locationHandler.removeCallbacks(checkForLocation);
  711. super.onDestroy();
  712. }
  713. }