SettingsLayout.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.ivalentin.margolariak;
  2. import android.annotation.SuppressLint;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.content.SharedPreferences;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.app.Fragment;
  9. import android.view.Gravity;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.view.ViewGroup;
  14. import android.view.Window;
  15. import android.view.WindowManager;
  16. import android.webkit.WebView;
  17. import android.widget.Button;
  18. import android.widget.CheckBox;
  19. import android.widget.RelativeLayout;
  20. import android.widget.TextView;
  21. /**
  22. * Section that allow the user to change the app settings.
  23. *
  24. * @author Inigo Valentin
  25. *
  26. */
  27. public class SettingsLayout extends Fragment{
  28. /**
  29. * Run when the fragment is inflated.
  30. * Assigns the view and the click listeners.
  31. *
  32. * @param inflater A LayoutInflater to manage views
  33. * @param container The container View
  34. * @param savedInstanceState Bundle containing the state
  35. *
  36. * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
  37. */
  38. @SuppressLint("InflateParams") //Throws unknown error when done properly.
  39. @Override
  40. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  41. //Load the layout
  42. final View view = inflater.inflate(R.layout.fragment_layout_settings, null);
  43. //Set the title
  44. ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_settings));
  45. //Get views.
  46. RelativeLayout rlNotification = (RelativeLayout) view.findViewById(R.id.rl_settings_notifications);
  47. final CheckBox cbNotification = (CheckBox) view.findViewById(R.id.cb_settings_notifications);
  48. final TextView tvNotification = (TextView) view.findViewById(R.id.tv_settings_notifications_summary);
  49. TextView tvLicense = (TextView) view.findViewById(R.id.tv_setting_license);
  50. TextView tvPrivacy = (TextView) view.findViewById(R.id.tv_setting_privacy);
  51. TextView tvAbout = (TextView) view.findViewById(R.id.tv_setting_about);
  52. TextView tvTransparency = (TextView) view.findViewById(R.id.tv_setting_transparency);
  53. //Set initial state of the notification settings
  54. SharedPreferences settings = view.getContext().getSharedPreferences(GM.PREFERENCES.PREFERNCES, Context.MODE_PRIVATE);
  55. if (settings.getBoolean(GM.PREFERENCES.KEY.NOTIFICATIONS, GM.PREFERENCES.DEFAULT.NOTIFICATIONS)){
  56. cbNotification.setChecked(true);
  57. tvNotification.setText(view.getContext().getString(R.string.settings_notification_on));
  58. }
  59. else{
  60. cbNotification.setChecked(false);
  61. tvNotification.setText(view.getContext().getString(R.string.settings_notification_off));
  62. }
  63. //Set click listener for "Notification" preference
  64. rlNotification.setOnClickListener(new OnClickListener(){
  65. @Override
  66. public void onClick(View v) {
  67. if (cbNotification.isChecked()){
  68. cbNotification.setChecked(false);
  69. tvNotification.setText(view.getContext().getString(R.string.settings_notification_off));
  70. SharedPreferences preferences = view.getContext().getSharedPreferences(GM.PREFERENCES.PREFERNCES, Context.MODE_PRIVATE);
  71. SharedPreferences.Editor editor = preferences.edit();
  72. editor.putBoolean(GM.PREFERENCES.KEY.NOTIFICATIONS, false);
  73. editor.apply();
  74. }
  75. else{
  76. cbNotification.setChecked(true);
  77. tvNotification.setText(view.getContext().getString(R.string.settings_notification_on));
  78. SharedPreferences preferences = view.getContext().getSharedPreferences(GM.PREFERENCES.PREFERNCES, Context.MODE_PRIVATE);
  79. SharedPreferences.Editor editor = preferences.edit();
  80. editor.putBoolean(GM.PREFERENCES.KEY.NOTIFICATIONS, true);
  81. editor.apply();
  82. }
  83. }
  84. });
  85. //Set listener for the other buttons
  86. tvLicense.setOnClickListener(new OnClickListener() {
  87. @Override
  88. public void onClick(View v) {
  89. String title = v.getResources().getString(R.string.settings_license);
  90. String text = v.getResources().getString(R.string.settings_license_content);
  91. showDialog(title, text);
  92. }
  93. });
  94. tvPrivacy.setOnClickListener(new OnClickListener() {
  95. @Override
  96. public void onClick(View v) {
  97. String title = v.getResources().getString(R.string.settings_privacy);
  98. String text = v.getResources().getString(R.string.settings_privacy_content);
  99. showDialog(title, text);
  100. }
  101. });
  102. tvAbout.setOnClickListener(new OnClickListener() {
  103. @Override
  104. public void onClick(View v) {
  105. String title = v.getResources().getString(R.string.settings_about);
  106. String text = v.getResources().getString(R.string.settings_about_content);
  107. showDialog(title, text);
  108. }
  109. });
  110. tvTransparency.setOnClickListener(new OnClickListener() {
  111. @Override
  112. public void onClick(View v) {
  113. String title = v.getResources().getString(R.string.settings_transparency);
  114. String text = v.getResources().getString(R.string.settings_transparency_content);
  115. showDialog(title, text);
  116. }
  117. });
  118. return view;
  119. }
  120. private void showDialog(String title, String text){
  121. //Create the dialog
  122. final Dialog dialog = new Dialog(getActivity());
  123. //Set up dialog window
  124. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  125. dialog.setContentView(R.layout.dialog_settings);
  126. //Set the custom dialog components - text, image and button
  127. TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_dialog_settings_title);
  128. WebView wvText = (WebView) dialog.findViewById(R.id.wv_dialog_settings_text);
  129. if (Build.VERSION.SDK_INT >= 19) {
  130. wvText.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  131. }
  132. else {
  133. wvText.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  134. }
  135. Button btClose = (Button) dialog.findViewById(R.id.bt_dialog_settings_close);
  136. //Set text
  137. tvTitle.setText(title);
  138. wvText.loadDataWithBaseURL(null, text, "text/html", "utf-8", null);
  139. //Set close button
  140. btClose.setOnClickListener(new OnClickListener() {
  141. @Override
  142. public void onClick(View v) {
  143. dialog.dismiss();
  144. }
  145. });
  146. //Set parameters
  147. WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  148. lp.copyFrom(dialog.getWindow().getAttributes());
  149. lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  150. lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
  151. lp.gravity = Gravity.CENTER;
  152. lp.dimAmount = 0.4f;
  153. dialog.getWindow().setAttributes(lp);
  154. //Show dialog
  155. dialog.show();
  156. }
  157. }