SettingsActivity.java 16 KB

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