SettingsActivity.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package nl.digitalthings.mebarista;
  2. import android.content.ComponentName;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.content.SharedPreferences;
  7. import android.content.pm.PackageManager;
  8. import android.os.Bundle;
  9. import android.os.IBinder;
  10. import android.preference.PreferenceActivity;
  11. import android.preference.PreferenceManager;
  12. import android.util.Log;
  13. import java.util.Map;
  14. /**
  15. * Created by jan on 11/20/13.
  16. */
  17. public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
  18. private static final String TAG = "Settings";
  19. BaristaService mBoundService;
  20. private ServiceConnection mConnection = new ServiceConnection() {
  21. public void onServiceConnected(ComponentName className, IBinder service) {
  22. mBoundService = ((BaristaService.LocalBinder)service).getService();
  23. if( !mBoundService.legacy )
  24. addPreferencesFromResource(R.xml.preference);
  25. else
  26. addPreferencesFromResource(R.xml.preference_legacy);
  27. }
  28. public void onServiceDisconnected(ComponentName className) {
  29. mBoundService = null;
  30. }
  31. };
  32. @Override
  33. public void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
  36. bindService(new Intent(SettingsActivity.this,
  37. BaristaService.class), mConnection, Context.BIND_AUTO_CREATE);
  38. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
  39. if( findPreference("pref_bt_ble_enabled") != null ) // .setEnabled(false);
  40. System.out.println( "jopiedopie" );
  41. }
  42. // this.getApplicationContext().getSharedPreferences( "zyx " ).
  43. }
  44. @Override
  45. public void onDestroy() {
  46. super.onDestroy();
  47. unbindService(mConnection);
  48. }
  49. @Override
  50. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
  51. // handle the preference change here
  52. //showNotification(key, "changed" );
  53. // TODO: why is this here? should move to baristaservice
  54. System.out.println("Changed: " + key);
  55. String val = "";
  56. Map<String,?> keys = sharedPreferences.getAll();
  57. for( Map.Entry<String,?> entry : keys.entrySet( ) ) {
  58. if( entry.getKey().equals( key ) ) {
  59. String entry_type = entry.getValue().getClass().getName();
  60. System.out.println( "type of " + key + " is " + entry_type );
  61. // Do something
  62. if( entry_type.equals("java.lang.Integer") || entry_type.equals("java.lang.Long") )
  63. val = entry.getValue().toString();
  64. if( entry_type.equals( "java.lang.Float" ) ) {
  65. Float value = (Float)entry.getValue();
  66. val = "" + value.intValue();
  67. }
  68. if( entry_type.equals("java.lang.Boolean") )
  69. val = (Boolean)entry.getValue() ? "1" : "0";
  70. if( entry_type.equals( "java.lang.String" ) )
  71. val = entry.getValue( ).toString( );
  72. break;
  73. }
  74. }
  75. // publish the change to device
  76. String keystr = key.replace("pref_", "");
  77. if( ! ( keystr.startsWith( "bt_") || keystr.startsWith( "ui_" ) || keystr.startsWith( "support" ) ) ) {
  78. System.out.println("cmd set " + keystr + " " + val);
  79. // mBoundService.write(("cmd set " + keystr + " " + val + "\n")); // TODO: remove nl
  80. Intent i = new Intent(this, BaristaService.class);
  81. i.putExtra("action", "send" );
  82. i.putExtra("payload", "cmd set " + keystr + " " + val + "\n" );
  83. startService(i);
  84. }
  85. if( key.equals( "pref_bt_bt2_enabled" ) || key.equals( "pref_bt_ble_enabled" ) ) {
  86. // mBoundService.scan( );
  87. Log.i( TAG, "BT setting changed? Start scan");
  88. Intent i = new Intent(this, BaristaService.class);
  89. i.putExtra("action", "scan" );
  90. startService(i);
  91. }
  92. }
  93. }