MainActivity.java 24 KB

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