SettingsActivity.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package io.github.sdlpal;
  2. import android.content.DialogInterface;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Environment;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.support.v7.widget.AppCompatSpinner;
  8. import android.support.v7.widget.SwitchCompat;
  9. import android.support.v7.widget.Toolbar;
  10. import android.support.v7.app.AlertDialog;
  11. import android.view.View;
  12. import android.widget.AdapterView;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.CompoundButton;
  15. import android.widget.EditText;
  16. import android.widget.SeekBar;
  17. import android.widget.Spinner;
  18. import java.util.ArrayList;
  19. public class SettingsActivity extends AppCompatActivity {
  20. public static native boolean loadConfigFile();
  21. public static native boolean saveConfigFile();
  22. public static native boolean getConfigBoolean(String item, boolean defval);
  23. public static native int getConfigInt(String item, boolean defval);
  24. public static native String getConfigString(String item, boolean defval);
  25. public static native boolean setConfigBoolean(String item, boolean value);
  26. public static native boolean setConfigInt(String item, int value);
  27. public static native boolean setConfigString(String item, String value);
  28. public static native String getGitRevision();
  29. private static final String KeepAspectRatio = "KeepAspectRatio";
  30. private static final String LaunchSetting = "LaunchSetting";
  31. private static final String Stereo = "Stereo";
  32. private static final String UseSurroundOPL = "UseSurroundOPL";
  33. private static final String UseTouchOverlay = "UseTouchOverlay";
  34. private static final String AudioBufferSize = "AudioBufferSize";
  35. private static final String LogLevel = "LogLevel";
  36. private static final String OPLSampleRate = "OPLSampleRate";
  37. private static final String ResampleQuality = "ResampleQuality";
  38. private static final String SampleRate = "SampleRate";
  39. private static final String MusicVolume = "MusicVolume";
  40. private static final String SoundVolume = "SoundVolume";
  41. private static final String CDFormat = "CD";
  42. private static final String GamePath = "GamePath";
  43. private static final String SavePath = "SavePath";
  44. private static final String MessageFileName = "MessageFileName";
  45. private static final String LogFileName = "LogFileName";
  46. private static final String FontFileName = "FontFileName";
  47. private static final String MusicFormat = "Music";
  48. private static final String OPLFormat = "OPL";
  49. private static final int AudioSampleRates[] = { 11025, 22050, 44100 };
  50. private static final int AudioBufferSizes[] = { 512, 1024, 2048, 4096, 8192 };
  51. private static final int OPLSampleRates[] = { 12429, 24858, 49716 };
  52. private static final String CDFormats[] = { "MP3", "OGG" };
  53. private static final String MusicFormats[] = { "MIDI", "RIX", "MP3", "OGG" };
  54. private static final String OPLFormats[] = { "DOSBOX", "MAME", "DOSBOXNEW" };
  55. private SettingsActivity mInstance = this;
  56. @Override
  57. protected void onCreate(Bundle savedInstanceState) {
  58. super.onCreate(savedInstanceState);
  59. setContentView(R.layout.activity_settings);
  60. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  61. setSupportActionBar(toolbar);
  62. toolbar.setSubtitle(getResources().getString(R.string.title_settings) + " (" + getGitRevision() + ")");
  63. ((SwitchCompat)findViewById(R.id.swMsgFile)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  64. @Override
  65. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  66. findViewById(R.id.edMsgFile).setVisibility(isChecked ? View.VISIBLE : View.GONE);
  67. }
  68. });
  69. ((SwitchCompat)findViewById(R.id.swFontFile)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  70. @Override
  71. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  72. findViewById(R.id.edFontFile).setVisibility(isChecked ? View.VISIBLE : View.GONE);
  73. }
  74. });
  75. ((SwitchCompat)findViewById(R.id.swLogFile)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  76. @Override
  77. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  78. findViewById(R.id.edLogFile).setVisibility(isChecked ? View.VISIBLE : View.GONE);
  79. }
  80. });
  81. ((AppCompatSpinner)findViewById(R.id.spMusFmt)).setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
  82. @Override
  83. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  84. findViewById(R.id.layoutOPL).setVisibility(position == 1 ? View.VISIBLE : View.GONE);
  85. }
  86. @Override
  87. public void onNothingSelected(AdapterView<?> parent) {
  88. findViewById(R.id.layoutOPL).setVisibility(View.VISIBLE);
  89. }
  90. });
  91. findViewById(R.id.btnDefault).setOnClickListener(new View.OnClickListener() {
  92. @Override
  93. public void onClick(View v) {
  94. setDefaults();
  95. }
  96. });
  97. findViewById(R.id.btnReset).setOnClickListener(new View.OnClickListener() {
  98. @Override
  99. public void onClick(View view) {
  100. resetConfigs();
  101. }
  102. });
  103. findViewById(R.id.btnFinish).setOnClickListener(new View.OnClickListener() {
  104. @Override
  105. public void onClick(View v) {
  106. if (!setConfigs()) return;
  107. setConfigBoolean(LaunchSetting, false);
  108. saveConfigFile();
  109. AlertDialog.Builder builder = new AlertDialog.Builder(mInstance);
  110. builder.setMessage(R.string.msg_exit);
  111. builder.setCancelable(false);
  112. builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  113. @Override
  114. public void onClick(DialogInterface dialogInterface, int i) {
  115. Intent intent = new Intent(mInstance, PalActivity.class);
  116. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  117. startActivity(intent);
  118. finish();
  119. }
  120. });
  121. builder.create().show();
  122. }
  123. });
  124. resetConfigs();
  125. if (PalActivity.crashed) {
  126. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  127. builder.setMessage(R.string.msg_crash);
  128. builder.setCancelable(false);
  129. builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  130. @Override
  131. public void onClick(DialogInterface dialogInterface, int i) {
  132. dialogInterface.dismiss();
  133. }
  134. });
  135. builder.create().show();
  136. }
  137. }
  138. protected int findMatchedIntIndex(int value, int[] values, int defaultIndex) {
  139. for(int i = 0; i < values.length; i++) {
  140. if (values[i] == value)
  141. return i;
  142. }
  143. return defaultIndex;
  144. }
  145. protected int findMatchedStringIndex(String value, String[] values, int defaultIndex) {
  146. for(int i = 0; i < values.length; i++) {
  147. if (values[i].equals(value))
  148. return i;
  149. }
  150. return defaultIndex;
  151. }
  152. protected void setDefaults() {
  153. String sdcardState = Environment.getExternalStorageState();
  154. findViewById(R.id.edMsgFile).setVisibility(View.GONE);
  155. findViewById(R.id.edFontFile).setVisibility(View.GONE);
  156. findViewById(R.id.edLogFile).setVisibility(View.GONE);
  157. findViewById(R.id.layoutOPL).setVisibility(View.VISIBLE);
  158. ((SeekBar)findViewById(R.id.sbMusVol)).setProgress(getConfigInt(MusicVolume, true));
  159. ((SeekBar)findViewById(R.id.sbSFXVol)).setProgress(getConfigInt(SoundVolume, true));
  160. ((SeekBar)findViewById(R.id.sbQuality)).setProgress(getConfigInt(ResampleQuality, true));
  161. if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
  162. ((EditText)findViewById(R.id.edFolder)).setText(Environment.getExternalStorageDirectory().getPath() + "/sdlpal/");
  163. } else {
  164. ((EditText)findViewById(R.id.edFolder)).setText("/sdcard/sdlpal/");
  165. }
  166. ((EditText)findViewById(R.id.edMsgFile)).setText("");
  167. ((EditText)findViewById(R.id.edFontFile)).setText("");
  168. ((EditText)findViewById(R.id.edLogFile)).setText("");
  169. ((SwitchCompat)findViewById(R.id.swMsgFile)).setChecked(false);
  170. ((SwitchCompat)findViewById(R.id.swFontFile)).setChecked(false);
  171. ((SwitchCompat)findViewById(R.id.swLogFile)).setChecked(false);
  172. ((SwitchCompat)findViewById(R.id.swTouch)).setChecked(getConfigBoolean(UseTouchOverlay, true));
  173. ((SwitchCompat)findViewById(R.id.swAspect)).setChecked(getConfigBoolean(KeepAspectRatio, true));
  174. ((SwitchCompat)findViewById(R.id.swSurround)).setChecked(getConfigBoolean(UseSurroundOPL, true));
  175. ((SwitchCompat)findViewById(R.id.swStereo)).setChecked(getConfigBoolean(Stereo, true));
  176. ((AppCompatSpinner)findViewById(R.id.spLogLevel)).setSelection(getConfigInt(LogLevel, true));
  177. ((AppCompatSpinner)findViewById(R.id.spSample)).setSelection(findMatchedIntIndex(getConfigInt(SampleRate, true), AudioSampleRates, 2)); // 44100Hz
  178. ((AppCompatSpinner)findViewById(R.id.spBuffer)).setSelection(findMatchedIntIndex(getConfigInt(AudioBufferSize, true), AudioBufferSizes, 1)); // 1024
  179. ((AppCompatSpinner)findViewById(R.id.spCDFmt)).setSelection(findMatchedStringIndex(getConfigString(CDFormat, true), CDFormats, 1)); // OGG
  180. ((AppCompatSpinner)findViewById(R.id.spMusFmt)).setSelection(findMatchedStringIndex(getConfigString(MusicFormat, true), MusicFormats, 1)); // RIX
  181. ((AppCompatSpinner)findViewById(R.id.spOPL)).setSelection(findMatchedStringIndex(getConfigString(OPLFormat, true), OPLFormats, 1)); // MAME
  182. ((AppCompatSpinner)findViewById(R.id.spOPLRate)).setSelection(findMatchedIntIndex(getConfigInt(OPLSampleRate, true), OPLSampleRates, 2)); // 49716Hz
  183. }
  184. protected void resetConfigs() {
  185. findViewById(R.id.edMsgFile).setVisibility(View.GONE);
  186. findViewById(R.id.edFontFile).setVisibility(View.GONE);
  187. findViewById(R.id.edLogFile).setVisibility(View.GONE);
  188. findViewById(R.id.layoutOPL).setVisibility(View.VISIBLE);
  189. ((SeekBar)findViewById(R.id.sbMusVol)).setProgress(getConfigInt(MusicVolume, false));
  190. ((SeekBar)findViewById(R.id.sbSFXVol)).setProgress(getConfigInt(SoundVolume, false));
  191. ((SeekBar)findViewById(R.id.sbQuality)).setProgress(getConfigInt(ResampleQuality, false)); // Best quality
  192. String msgFile, fontFile, logFile;
  193. ((EditText)findViewById(R.id.edFolder)).setText(getConfigString(GamePath, false));
  194. ((EditText)findViewById(R.id.edMsgFile)).setText(msgFile = getConfigString(MessageFileName, false));
  195. ((EditText)findViewById(R.id.edFontFile)).setText(fontFile = getConfigString(FontFileName, false));
  196. ((EditText)findViewById(R.id.edLogFile)).setText(logFile = getConfigString(LogFileName, false));
  197. ((SwitchCompat)findViewById(R.id.swMsgFile)).setChecked(msgFile != null && !msgFile.isEmpty());
  198. ((SwitchCompat)findViewById(R.id.swFontFile)).setChecked(fontFile != null && !fontFile.isEmpty());
  199. ((SwitchCompat)findViewById(R.id.swLogFile)).setChecked(logFile != null && !logFile.isEmpty());
  200. ((SwitchCompat)findViewById(R.id.swTouch)).setChecked(getConfigBoolean(UseTouchOverlay, false));
  201. ((SwitchCompat)findViewById(R.id.swAspect)).setChecked(getConfigBoolean(KeepAspectRatio, false));
  202. ((SwitchCompat)findViewById(R.id.swSurround)).setChecked(getConfigBoolean(UseSurroundOPL, false));
  203. ((SwitchCompat)findViewById(R.id.swStereo)).setChecked(getConfigBoolean(Stereo, false));
  204. ((AppCompatSpinner)findViewById(R.id.spLogLevel)).setSelection(getConfigInt(LogLevel, false));
  205. ((AppCompatSpinner)findViewById(R.id.spSample)).setSelection(findMatchedIntIndex(getConfigInt(SampleRate, false), AudioSampleRates, 2)); // 44100Hz
  206. ((AppCompatSpinner)findViewById(R.id.spBuffer)).setSelection(findMatchedIntIndex(getConfigInt(AudioBufferSize, false), AudioBufferSizes, 1)); // 1024
  207. ((AppCompatSpinner)findViewById(R.id.spCDFmt)).setSelection(findMatchedStringIndex(getConfigString(CDFormat, false), CDFormats, 1)); // OGG
  208. ((AppCompatSpinner)findViewById(R.id.spMusFmt)).setSelection(findMatchedStringIndex(getConfigString(MusicFormat, false), MusicFormats, 1)); // RIX
  209. ((AppCompatSpinner)findViewById(R.id.spOPL)).setSelection(findMatchedStringIndex(getConfigString(OPLFormat, false), OPLFormats, 1)); // MAME
  210. ((AppCompatSpinner)findViewById(R.id.spOPLRate)).setSelection(findMatchedIntIndex(getConfigInt(OPLSampleRate, false), OPLSampleRates, 2)); // 49716Hz
  211. }
  212. protected boolean setConfigs() {
  213. if (((EditText)findViewById(R.id.edFolder)).getText().toString().isEmpty()) {
  214. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  215. builder.setMessage(R.string.msg_empty);
  216. builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  217. @Override
  218. public void onClick(DialogInterface dialogInterface, int i) {
  219. dialogInterface.dismiss();
  220. }
  221. });
  222. builder.create().show();
  223. return false;
  224. }
  225. setConfigInt(MusicVolume, ((SeekBar)findViewById(R.id.sbMusVol)).getProgress());
  226. setConfigInt(SoundVolume, ((SeekBar)findViewById(R.id.sbSFXVol)).getProgress());
  227. setConfigInt(ResampleQuality, ((SeekBar)findViewById(R.id.sbQuality)).getProgress());
  228. setConfigString(GamePath, ((EditText)findViewById(R.id.edFolder)).getText().toString());
  229. setConfigString(SavePath, ((EditText)findViewById(R.id.edFolder)).getText().toString());
  230. setConfigString(MessageFileName, ((SwitchCompat)findViewById(R.id.swMsgFile)).isChecked() ? ((EditText)findViewById(R.id.edMsgFile)).getText().toString() : null);
  231. setConfigString(FontFileName, ((SwitchCompat)findViewById(R.id.swFontFile)).isChecked() ? ((EditText)findViewById(R.id.edFontFile)).getText().toString() : null);
  232. setConfigString(LogFileName, ((SwitchCompat)findViewById(R.id.swLogFile)).isChecked() ? ((EditText)findViewById(R.id.edLogFile)).getText().toString() : null);
  233. setConfigBoolean(UseTouchOverlay, ((SwitchCompat)findViewById(R.id.swTouch)).isChecked());
  234. setConfigBoolean(KeepAspectRatio, ((SwitchCompat)findViewById(R.id.swAspect)).isChecked());
  235. setConfigBoolean(UseSurroundOPL, ((SwitchCompat)findViewById(R.id.swSurround)).isChecked());
  236. setConfigBoolean(Stereo, ((SwitchCompat)findViewById(R.id.swStereo)).isChecked());
  237. setConfigInt(LogLevel, ((AppCompatSpinner)findViewById(R.id.spLogLevel)).getSelectedItemPosition());
  238. setConfigInt(SampleRate, Integer.parseInt((String)((AppCompatSpinner)findViewById(R.id.spSample)).getSelectedItem()));
  239. setConfigInt(AudioBufferSize, Integer.parseInt((String)((AppCompatSpinner)findViewById(R.id.spBuffer)).getSelectedItem()));
  240. setConfigString(CDFormat, (String)((AppCompatSpinner)findViewById(R.id.spCDFmt)).getSelectedItem());
  241. setConfigString(MusicFormat, (String)((AppCompatSpinner)findViewById(R.id.spMusFmt)).getSelectedItem());
  242. setConfigString(OPLFormat, (String)((AppCompatSpinner)findViewById(R.id.spOPL)).getSelectedItem());
  243. setConfigInt(OPLSampleRate, Integer.parseInt((String)((AppCompatSpinner)findViewById(R.id.spOPLRate)).getSelectedItem()));
  244. return true;
  245. }
  246. }