SettingsActivity.java 10 KB

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