SettingsActivity.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package com.ivalentin.margolariak;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.Dialog;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.graphics.Color;
  8. import android.graphics.drawable.ColorDrawable;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.Gravity;
  13. import android.view.View;
  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.LinearLayout;
  20. import android.widget.TextView;
  21. /**
  22. * Activity that allows the user to change the app settings.
  23. *
  24. * @author Iñigo Valentin
  25. * @see Activity
  26. */
  27. public class SettingsActivity extends Activity {
  28. //Assign menu items
  29. private LinearLayout llSync;
  30. private LinearLayout llNotifications;
  31. private CheckBox cbSync, cbNotifications;
  32. private TextView tvSync;
  33. private TextView tvNotifications;
  34. private SharedPreferences preferences;
  35. private SharedPreferences.Editor editor;
  36. /**
  37. * Run when the app is created. Assigns views, configures their initial
  38. * states, and sets listeners
  39. *
  40. * @param savedInstanceState Activity state.
  41. *
  42. * @see Activity#onCreate(Bundle)
  43. */
  44. @SuppressLint("CommitPrefEdits")
  45. @Override
  46. public void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. //Remove title bar.
  49. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  50. super.onCreate(savedInstanceState);
  51. //Set layout.
  52. setContentView(R.layout.activity_settings);
  53. //Assign views
  54. llSync = (LinearLayout) findViewById(R.id.ll_settings_sync);
  55. llNotifications = (LinearLayout) findViewById(R.id.ll_settings_notifications);
  56. LinearLayout llVersion = (LinearLayout) findViewById(R.id.ll_settings_version);
  57. LinearLayout llSource = (LinearLayout) findViewById(R.id.ll_settings_source);
  58. LinearLayout llFeedback = (LinearLayout) findViewById(R.id.ll_settings_feedback);
  59. tvSync = (TextView) findViewById(R.id.tv_settings_sync);
  60. tvNotifications = (TextView) findViewById(R.id.tv_settings_notifications);
  61. TextView tvVersion = (TextView) findViewById(R.id.tv_settings_version);
  62. cbSync = (CheckBox) findViewById(R.id.cb_settings_sync);
  63. cbNotifications = (CheckBox) findViewById(R.id.cb_settings_notifications);
  64. //Open preferences
  65. preferences = getSharedPreferences(GM.PREFERENCES.PREFERNCES, MODE_PRIVATE);
  66. editor = preferences.edit();
  67. //Set up sync preferences. Incidentally, also sets initial state for notifications.
  68. if (preferences.getBoolean(GM.PREFERENCES.KEY.SYNC, GM.PREFERENCES.DEFAULT.SYNC)){
  69. tvSync.setText(R.string.preferences_sync_sync_on);
  70. cbSync.setChecked(true);
  71. enableNotifications();
  72. }
  73. else{
  74. tvSync.setText(R.string.preferences_sync_sync_off);
  75. cbSync.setChecked(false);
  76. disableNotifications();
  77. }
  78. //Set up version preference
  79. tvVersion.setText(com.ivalentin.margolariak.BuildConfig.VERSION_NAME);
  80. llVersion.setOnClickListener(new View.OnClickListener() {
  81. @Override
  82. public void onClick(View v) {
  83. blink(v);
  84. showChangelog();
  85. }
  86. });
  87. //Set up source preference
  88. llSource.setOnClickListener(new View.OnClickListener() {
  89. @Override
  90. public void onClick(View v) {
  91. blink(v);
  92. Intent i = new Intent(Intent.ACTION_VIEW);
  93. i.setData(Uri.parse(GM.URL.GITHUB));
  94. startActivity(i);
  95. }
  96. });
  97. //Set up feedback preference
  98. llFeedback.setOnClickListener(new View.OnClickListener() {
  99. @Override
  100. public void onClick(View v) {
  101. blink(v);
  102. //TODO: Show dialog to enter text
  103. }
  104. });
  105. }
  106. /**
  107. * UI feedback for touched settings.
  108. * Just makes them blink.
  109. *
  110. * @param v The view to be made to blink
  111. */
  112. private void blink(View v){
  113. final View view = v;
  114. //These are the colors of the menu background
  115. final int r = 0;
  116. final int g = 120;
  117. final int b = 255;
  118. new Thread(){
  119. @Override
  120. public void run() {
  121. //Alpha
  122. int a = 250;
  123. try {
  124. while (a >= 0) {
  125. final int passAlpha = a;
  126. runOnUiThread(new Runnable() {
  127. @Override
  128. public void run() {
  129. view.setBackgroundColor(Color.argb(passAlpha, r, g, b));
  130. }
  131. });
  132. a -= 10;
  133. sleep(10);
  134. }
  135. }
  136. catch (InterruptedException e) {
  137. view.setBackgroundColor(Color.argb(0, r, g, b));
  138. }
  139. }
  140. }.start();
  141. }
  142. /**
  143. * Not called from code, but referenced from activity_settings.xml.
  144. * Toggles the value of the sync preferences, changing the actual
  145. * preference, the ui, and, if set to disabled, it also disables the
  146. * notification setting.
  147. *
  148. * @param v Ignored
  149. */
  150. public void toggleSync(View v){
  151. blink(llSync);
  152. editor.putBoolean(GM.PREFERENCES.KEY.SYNC, !preferences.getBoolean(GM.PREFERENCES.KEY.SYNC, GM.PREFERENCES.DEFAULT.SYNC));
  153. editor.apply();
  154. if (preferences.getBoolean(GM.PREFERENCES.KEY.SYNC, GM.PREFERENCES.DEFAULT.SYNC)){
  155. tvSync.setText(R.string.preferences_sync_sync_on);
  156. cbSync.setChecked(true);
  157. enableNotifications();
  158. }
  159. else{
  160. tvSync.setText(R.string.preferences_sync_sync_off);
  161. cbSync.setChecked(false);
  162. disableNotifications();
  163. }
  164. }
  165. /**
  166. * Not called from code, but referenced from activity_settings.xml.
  167. * Toggles the value of the notification preferences, changing the
  168. * actual preference and the ui.
  169. *
  170. * @param v Ignored
  171. */
  172. public void toggleNotifications(View v){
  173. if (preferences.getBoolean(GM.PREFERENCES.KEY.SYNC, GM.PREFERENCES.DEFAULT.SYNC)) {
  174. blink(llNotifications);
  175. editor.putBoolean(GM.PREFERENCES.KEY.NOTIFICATIONS, !preferences.getBoolean(GM.PREFERENCES.KEY.NOTIFICATIONS, GM.PREFERENCES.DEFAULT.NOTIFICATIONS));
  176. editor.apply();
  177. if (preferences.getBoolean(GM.PREFERENCES.KEY.NOTIFICATIONS, GM.PREFERENCES.DEFAULT.NOTIFICATIONS)) {
  178. tvNotifications.setText(R.string.preferences_sync_notifications_on);
  179. cbNotifications.setChecked(true);
  180. } else {
  181. tvNotifications.setText(R.string.preferences_sync_notifications_off);
  182. cbNotifications.setChecked(false);
  183. }
  184. }
  185. else{
  186. cbNotifications.setChecked(false);
  187. }
  188. }
  189. /**
  190. * Enables the notification setting toggler. Must be called when
  191. * the sync preference changes to true.
  192. */
  193. private void enableNotifications(){
  194. llNotifications.setAlpha(1);
  195. if (preferences.getBoolean(GM.PREFERENCES.KEY.NOTIFICATIONS, GM.PREFERENCES.DEFAULT.NOTIFICATIONS)){
  196. tvNotifications.setText(R.string.preferences_sync_notifications_on);
  197. cbNotifications.setChecked(true);
  198. }
  199. else{
  200. tvNotifications.setText(R.string.preferences_sync_notifications_off);
  201. cbNotifications.setChecked(false);
  202. }
  203. }
  204. /**
  205. * Enables the notification setting toggler. Must be called when
  206. * the sync preference changes to false.
  207. */
  208. private void disableNotifications(){
  209. llNotifications.setAlpha(0.4f);
  210. tvNotifications.setText(R.string.preferences_sync_notifications_disabled);
  211. cbNotifications.setChecked(false);
  212. }
  213. private void showChangelog(){
  214. //Create the dialog
  215. final Dialog dialog = new Dialog(SettingsActivity.this);
  216. //Set up dialog window
  217. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  218. dialog.setContentView(R.layout.dialog_changelog);
  219. //Set the web view
  220. WebView wv = (WebView) dialog.findViewById(R.id.wv_changelog);
  221. wv.getSettings().setAllowContentAccess(true);
  222. Log.e("URL", "file:///android_assets/changelog_" + GM.getLang() + ".html");
  223. wv.loadUrl("file:///android_asset/changelog_" + GM.getLang() + ".html");
  224. //Set close button
  225. Button bt = (Button) dialog.findViewById(R.id.bt_changelog);
  226. bt.setOnClickListener(new View.OnClickListener() {
  227. @Override
  228. public void onClick(View v) {
  229. dialog.dismiss();
  230. }
  231. });
  232. //Prep the dialog
  233. WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  234. try {
  235. //noinspection ConstantConditions
  236. lp.copyFrom(dialog.getWindow().getAttributes());
  237. }
  238. catch (NullPointerException e){
  239. Log.e("SETTINGS_ACTIVITY", "Error setting the dialog attributes: " + e.toString());
  240. }
  241. lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  242. lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
  243. lp.gravity = Gravity.CENTER;
  244. //lp.dimAmount = 0.0f;
  245. dialog.getWindow().setAttributes(lp);
  246. dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  247. //Show dialog
  248. dialog.show();
  249. }
  250. /**
  251. * Not called from code, but referenced from activity_settings.xml.
  252. * Finishes the activity.
  253. *
  254. * @param v Ignored
  255. *
  256. * @see Activity#finish()
  257. */
  258. public void finish(View v){
  259. finish();
  260. }
  261. }