MainActivity.java 27 KB

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