MainActivity.java 25 KB

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