SettingsActivity.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package com.mg.polassis.gui;
  2. import android.app.ProgressDialog;
  3. import android.content.ComponentName;
  4. import android.content.ContentResolver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.ServiceConnection;
  8. import android.content.pm.PackageManager;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. import android.net.Uri;
  12. import android.os.AsyncTask;
  13. import android.os.IBinder;
  14. import android.provider.MediaStore;
  15. import android.support.v4.app.FragmentTransaction;
  16. import android.os.Bundle;
  17. import android.support.v7.preference.Preference;
  18. import android.support.v7.preference.PreferenceFragmentCompat;
  19. import android.support.v7.preference.PreferenceManager;
  20. import android.support.v7.preference.PreferenceScreen;
  21. import android.support.v7.app.AppCompatActivity;
  22. import android.view.MenuItem;
  23. import android.widget.Toast;
  24. import com.mg.polassis.R;
  25. import com.mg.polassis.misc.Assistant;
  26. import com.mg.polassis.service.TextToSpeechService;
  27. import com.mg.polassis.misc.Translations;
  28. import java.io.File;
  29. import java.io.FileOutputStream;
  30. import java.io.InputStream;
  31. public class SettingsActivity extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
  32. public String screenKey;
  33. public TextToSpeechService textToSpeechService;
  34. public SettingsFragment settingsFragment;
  35. private ServiceConnection textToSpeechServiceConnection = new ServiceConnection() {
  36. @Override
  37. public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
  38. TextToSpeechService.TextToSpeechBinder binder = (TextToSpeechService.TextToSpeechBinder)iBinder;
  39. textToSpeechService = binder.getService();
  40. }
  41. @Override
  42. public void onServiceDisconnected(ComponentName componentName) {
  43. textToSpeechService = null;
  44. }
  45. };
  46. @Override
  47. public void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_settings);
  50. getSupportActionBar().setTitle(Translations.getStringResource(this, getSupportActionBar().getTitle().toString()));
  51. if (savedInstanceState == null) {
  52. if (getIntent().hasExtra("screen")) {
  53. screenKey = getIntent().getStringExtra("screen");
  54. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  55. settingsFragment = new SettingsFragment();
  56. Bundle arguments = new Bundle();
  57. arguments.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, screenKey);
  58. settingsFragment.setArguments(arguments);
  59. transaction.replace(R.id.settingsFragment, settingsFragment, screenKey);
  60. transaction.commit();
  61. }
  62. else {
  63. settingsFragment = (SettingsFragment) getSupportFragmentManager().findFragmentByTag(SettingsFragment.FRAGMENT_TAG);
  64. if (settingsFragment == null) settingsFragment = new SettingsFragment();
  65. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  66. transaction.replace(R.id.settingsFragment, settingsFragment, SettingsFragment.FRAGMENT_TAG);
  67. transaction.commit();
  68. }
  69. bindService(new Intent(SettingsActivity.this, TextToSpeechService.class), textToSpeechServiceConnection, BIND_AUTO_CREATE);
  70. }
  71. }
  72. @Override
  73. public boolean onOptionsItemSelected(MenuItem item) {
  74. if (item.getItemId() == android.R.id.home) {
  75. finish();
  76. return true;
  77. }
  78. else return super.onOptionsItemSelected(item);
  79. }
  80. @Override
  81. public void onRequestPermissionsResult(int code, String permissions[], int[] results) {
  82. boolean notGranted = false;
  83. for (int i = 0; i < results.length; i++)
  84. {
  85. if (results[i] != PackageManager.PERMISSION_GRANTED) {
  86. notGranted = true;
  87. i = results.length+1;
  88. }
  89. }
  90. if (!notGranted) Toast.makeText(SettingsActivity.this, Translations.getStringResource(SettingsActivity.this, "settings_permission_granted"), Toast.LENGTH_LONG).show();
  91. else Toast.makeText(SettingsActivity.this, Translations.getStringResource(SettingsActivity.this, "settings_permission_not_granted"), Toast.LENGTH_LONG).show();
  92. }
  93. private Preference backgroundImagePreference;
  94. private Preference defaultBackgroundPreferenceButton;
  95. public void setBackgroundImage(Preference p1, Preference p2) {
  96. backgroundImagePreference = p1;
  97. defaultBackgroundPreferenceButton = p2;
  98. Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  99. intent.setType("image/*");
  100. intent.putExtra("crop", "true");
  101. intent.putExtra("aspectX", Assistant.layoutWidth);
  102. intent.putExtra("aspectY", Assistant.layoutHeight);
  103. intent.putExtra("outputX", Assistant.layoutWidth);
  104. intent.putExtra("outputY", Assistant.layoutHeight);
  105. intent.putExtra("scale", false);
  106. if (PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this).getBoolean("return-currentDate", false)) intent.putExtra("return-currentDate", true);
  107. startActivityForResult(intent, 4000);
  108. }
  109. private class ProcessImage extends AsyncTask<Object, Void, Exception>
  110. {
  111. private ProgressDialog progressDialog;
  112. private Preference backgroundImagePreference;
  113. private Preference defaultBackgroundPreferenceButton;
  114. public ProcessImage(Preference backgroundImagePreference, Preference defaultBackgroundPreferenceButton) {
  115. this.backgroundImagePreference = backgroundImagePreference;
  116. this.defaultBackgroundPreferenceButton = defaultBackgroundPreferenceButton;
  117. }
  118. @Override
  119. public void onPreExecute()
  120. {
  121. progressDialog = ProgressDialog.show(SettingsActivity.this, null, Translations.getStringResource(SettingsActivity.this, "wait"));
  122. }
  123. @Override
  124. public Exception doInBackground(Object... params)
  125. {
  126. try {
  127. Bitmap bitmap;
  128. Intent intent = (Intent)params[1];
  129. if (intent.getData() != null) {
  130. InputStream inputStream = ((ContentResolver) params[0]).openInputStream(intent.getData());
  131. bitmap = BitmapFactory.decodeStream(inputStream);
  132. inputStream.close();
  133. }
  134. else {
  135. if (intent.getExtras().get(Intent.EXTRA_STREAM) != null) {
  136. Uri data = (Uri)intent.getExtras().get(Intent.EXTRA_STREAM);
  137. InputStream inputStream = ((ContentResolver) params[0]).openInputStream(data);
  138. bitmap = BitmapFactory.decodeStream(inputStream);
  139. inputStream.close();
  140. }
  141. else bitmap = (Bitmap)intent.getExtras().get("currentDate");
  142. }
  143. File backgroundFile = new File(getFilesDir(), "background");
  144. if (backgroundFile.exists()) backgroundFile.delete();
  145. FileOutputStream backgroundFileOutputStream = openFileOutput("background", Context.MODE_PRIVATE);
  146. bitmap.compress(Bitmap.CompressFormat.PNG, 100, backgroundFileOutputStream);
  147. backgroundFileOutputStream.close();
  148. return null;
  149. }
  150. catch (Exception e) {
  151. return e;
  152. }
  153. }
  154. @Override
  155. public void onPostExecute(Exception result)
  156. {
  157. progressDialog.dismiss();
  158. if (result == null) {
  159. backgroundImagePreference.setSummary(Translations.getStringResource(SettingsActivity.this, "custom"));
  160. defaultBackgroundPreferenceButton.setEnabled(true);
  161. PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this).edit().putBoolean("background_changed", true).apply();
  162. }
  163. else {
  164. if (PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this).getBoolean("debug_mode", false))
  165. Toast.makeText(SettingsActivity.this, result.toString(), Toast.LENGTH_LONG).show();
  166. else
  167. Toast.makeText(SettingsActivity.this, Translations.getStringResource(SettingsActivity.this, "change_background_error"), Toast.LENGTH_LONG).show();
  168. }
  169. }
  170. }
  171. @Override
  172. public void onActivityResult(int requestCode, int resultCode, Intent data)
  173. {
  174. if (requestCode == 4000) {
  175. if (resultCode == RESULT_OK) {
  176. new ProcessImage(backgroundImagePreference, defaultBackgroundPreferenceButton).execute(getContentResolver(), data);
  177. }
  178. }
  179. }
  180. @Override
  181. public void onResume()
  182. {
  183. super.onResume();
  184. settingsFragment.registerSharedPreferencesListener();
  185. }
  186. @Override
  187. public void onPause()
  188. {
  189. super.onPause();
  190. settingsFragment.unregisterSharedPreferencesListener();
  191. }
  192. @Override
  193. public void onDestroy()
  194. {
  195. super.onDestroy();
  196. unbindService(textToSpeechServiceConnection);
  197. }
  198. @Override
  199. public boolean onPreferenceStartScreen(PreferenceFragmentCompat fragment, PreferenceScreen screen) {
  200. Intent intent = new Intent(SettingsActivity.this, SettingsActivity.class);
  201. intent.putExtra("screen", screen.getKey());
  202. startActivity(intent);
  203. return true;
  204. }
  205. }