MainActivity.java 24 KB

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