PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/CustomJersey/src/com/fernferret/customjersey/ShowJersey.java

https://gitlab.com/0072016/0072016-milk-heis
Java | 201 lines | 141 code | 34 blank | 26 comment | 6 complexity | 915831d0e7cba67db30b248c71ee1a68 MD5 | raw file
  1. package com.fernferret.customjersey;
  2. import android.app.Activity;
  3. import android.app.AlarmManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.content.res.Configuration;
  9. import android.content.res.Resources;
  10. import android.graphics.Paint;
  11. import android.graphics.Rect;
  12. import android.graphics.Typeface;
  13. import android.os.Bundle;
  14. import android.os.SystemClock;
  15. import android.util.Log;
  16. import android.view.Menu;
  17. import android.view.MenuInflater;
  18. import android.view.MenuItem;
  19. import android.view.View;
  20. import android.view.View.OnClickListener;
  21. import android.widget.ImageView;
  22. import android.widget.TextView;
  23. public class ShowJersey extends Activity {
  24. static final String PLAYER_NAME = "PLAYERNAME";
  25. static final String PLAYER_NUMBER = "PLAYERNUMBER";
  26. static final String IS_BLUE_JERSEY = "ISBLUEJERSEY";
  27. static final String JERSEY_COLOR_INDEX = "JERSEYCOLORINDEX";
  28. static final boolean DEFAULT_JERSEY_COLOR = true;
  29. static final int DEFAULT_COLOR_INDEX = 0;
  30. static final int EDIT_JERSEY_REQUEST_CODE = 0;
  31. private TextView mPlayerNameView;
  32. private TextView mPlayerNumberView;
  33. private ImageView mJerseyView;
  34. private SharedPreferences mSettings;
  35. private Resources mRes;
  36. private String mPlayerName;
  37. private int mPlayerNumber;
  38. public static final int[] JERSEY_ARRAY = new int[] { R.drawable.red_jersey, R.drawable.orange_jersey, R.drawable.blue_jersey, R.drawable.green_jersey };
  39. public static final int[] JERSEY_ARRAY_MINI = new int[] { R.drawable.red_jersey_widget, R.drawable.orange_jersey_widget, R.drawable.blue_jersey_widget, R.drawable.green_jersey_widget };
  40. private int mJerseyColorIndex = 0;
  41. @Override
  42. public void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.main);
  45. // Load the UI components
  46. mPlayerNameView = (TextView) findViewById(R.id.name);
  47. mPlayerNumberView = (TextView) findViewById(R.id.number);
  48. mJerseyView = (ImageView) findViewById(R.id.jersey);
  49. // Set custom fonts
  50. Typeface jerseyThick = Typeface.createFromAsset(getAssets(), "fonts/Jersey M54.ttf");
  51. Typeface jerseyThin = Typeface.createFromAsset(getAssets(), "fonts/sportsjersey.ttf");
  52. mPlayerNumberView.setTypeface(jerseyThick);
  53. mPlayerNameView.setTypeface(jerseyThin);
  54. // Load values from saved prefs
  55. mSettings = getSharedPreferences("ShowJersey", MODE_WORLD_READABLE);
  56. // Load resources
  57. mRes = getResources();
  58. getJerseyValuesFromPreferences();
  59. // Set the Onclick action for the Jersey
  60. mJerseyView.setOnClickListener(new OnClickListener() {
  61. @Override
  62. public void onClick(View v) {
  63. changeColorSecret();
  64. }
  65. });
  66. }
  67. private void getJerseyValuesFromPreferences() {
  68. // Pull default values from XML and UI components
  69. mPlayerName = mSettings.getString(PLAYER_NAME, mRes.getString(R.string.start_name));
  70. mPlayerNumber = mSettings.getInt(PLAYER_NUMBER, Integer.parseInt(mRes.getString(R.string.start_number)));
  71. mJerseyColorIndex = mSettings.getInt(JERSEY_COLOR_INDEX, DEFAULT_COLOR_INDEX);
  72. updateJersey();
  73. }
  74. private void updateJersey() {
  75. mPlayerNameView.setText(mPlayerName);
  76. // We need the extra ""s here because if we don't pass setText a string;
  77. // It'll assume we've passed a resource ID
  78. mPlayerNumberView.setText(mPlayerNumber + "");
  79. Log.e("SJ", "Index: " + mJerseyColorIndex);
  80. // Set the Jersy Image to the saved jersey color
  81. mJerseyView.setImageDrawable(mRes.getDrawable(JERSEY_ARRAY[mJerseyColorIndex]));
  82. int widthThreshold;
  83. if (mRes.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  84. mPlayerNameView.setTextSize(mRes.getDimension(R.dimen.name_size_port));
  85. widthThreshold = 170;
  86. } else {
  87. mPlayerNameView.setTextSize(mRes.getDimension(R.dimen.name_size_land));
  88. widthThreshold = 120;
  89. }
  90. // Resize the name
  91. Rect bounds = new Rect();
  92. Paint textPaint = mPlayerNameView.getPaint();
  93. textPaint.getTextBounds(mPlayerName, 0, mPlayerName.length(), bounds);
  94. int textWidth = bounds.width();
  95. while (textWidth > widthThreshold) {
  96. mPlayerNameView.setTextSize(mPlayerNameView.getTextSize() - 1);
  97. bounds = new Rect();
  98. textPaint = mPlayerNameView.getPaint();
  99. textPaint.getTextBounds(mPlayerName, 0, mPlayerName.length(), bounds);
  100. textWidth = bounds.width();
  101. }
  102. }
  103. @Override
  104. protected void onPause() {
  105. super.onPause();
  106. // Get editor for shared preferences
  107. // TODO: Why are we saving these values?
  108. // Ah, I think it was because of the old way of doing things
  109. SharedPreferences.Editor editor = this.mSettings.edit();
  110. // Save the values
  111. editor.putString(PLAYER_NAME, mPlayerName);
  112. editor.putInt(PLAYER_NUMBER, mPlayerNumber);
  113. editor.putInt(JERSEY_COLOR_INDEX, mJerseyColorIndex);
  114. // Commit the editor
  115. editor.commit();
  116. Log.w("Jersey", "Updating widget");
  117. }
  118. @Override
  119. protected void onStop() {
  120. super.onStop();
  121. // Create a pending intent for our JerseyWidget Factory
  122. Intent refreshIntent = new Intent(this, JerseyWidget.class);
  123. refreshIntent.setAction(JerseyWidget.UPDATE_WIDGET);
  124. PendingIntent pendingRefreshIntent = PendingIntent.getBroadcast(this, 0, refreshIntent, 0);
  125. // Set an alarm for now to refresh the widget(s)
  126. AlarmManager alarms = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
  127. alarms.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), pendingRefreshIntent);
  128. }
  129. @Override
  130. protected void onResume() {
  131. super.onResume();
  132. getJerseyValuesFromPreferences();
  133. }
  134. private void changeColorSecret() {
  135. // Increment the JersyColorIndex
  136. mJerseyColorIndex++;
  137. // If it's greater than the number of jerseys, reset to 0
  138. if (mJerseyColorIndex >= JERSEY_ARRAY.length) {
  139. mJerseyColorIndex = 0;
  140. }
  141. // Refresh the ImageView
  142. mJerseyView.setImageDrawable(mRes.getDrawable(JERSEY_ARRAY[mJerseyColorIndex]));
  143. }
  144. /**
  145. * Fired when the options menu is created
  146. */
  147. @Override
  148. public boolean onCreateOptionsMenu(Menu menu) {
  149. super.onCreateOptionsMenu(menu);
  150. MenuInflater inflater = new MenuInflater(this);
  151. inflater.inflate(R.menu.menu, menu);
  152. return true;
  153. }
  154. /**
  155. * Fired when an options menu item is selected.
  156. */
  157. @Override
  158. public boolean onOptionsItemSelected(MenuItem item) {
  159. switch (item.getItemId()) {
  160. case R.id.menu_edit:
  161. Intent editIntent = new Intent(ShowJersey.this, EditJersey.class);
  162. startActivity(editIntent);
  163. break;
  164. }
  165. return true;
  166. }
  167. }