SettingsActivity.java 16 KB

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