private void queueCommands() { if (isServiceBound) { for (ObdCommand Command : ObdConfig.getCommands()) { if (prefs.getBoolean(Command.getName(), true)) service.queueJob(new ObdCommandJob(Command)); } } }
/** * @param prefs * @return */ public static ArrayList<ObdCommand> getObdCommands(SharedPreferences prefs) { ArrayList<ObdCommand> cmds = ObdConfig.getCommands(); ArrayList<ObdCommand> ucmds = new ArrayList<>(); for (int i = 0; i < cmds.size(); i++) { ObdCommand cmd = cmds.get(i); boolean selected = prefs.getBoolean(cmd.getName(), true); if (selected) ucmds.add(cmd); } return ucmds; }
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* * Read preferences resources available at res/xml/preferences.xml */ addPreferencesFromResource(R.xml.preferences); checkGps(); ArrayList<CharSequence> pairedDeviceStrings = new ArrayList<>(); ArrayList<CharSequence> vals = new ArrayList<>(); ListPreference listBtDevices = (ListPreference) getPreferenceScreen().findPreference(BLUETOOTH_LIST_KEY); ArrayList<CharSequence> protocolStrings = new ArrayList<>(); ListPreference listProtocols = (ListPreference) getPreferenceScreen().findPreference(PROTOCOLS_LIST_KEY); String[] prefKeys = new String[] { ENGINE_DISPLACEMENT_KEY, VOLUMETRIC_EFFICIENCY_KEY, OBD_UPDATE_PERIOD_KEY, MAX_FUEL_ECON_KEY }; for (String prefKey : prefKeys) { EditTextPreference txtPref = (EditTextPreference) getPreferenceScreen().findPreference(prefKey); txtPref.setOnPreferenceChangeListener(this); } /* * Available OBD commands * * TODO This should be read from preferences database */ ArrayList<ObdCommand> cmds = ObdConfig.getCommands(); PreferenceScreen cmdScr = (PreferenceScreen) getPreferenceScreen().findPreference(COMMANDS_SCREEN_KEY); for (ObdCommand cmd : cmds) { CheckBoxPreference cpref = new CheckBoxPreference(this); cpref.setTitle(cmd.getName()); cpref.setKey(cmd.getName()); cpref.setChecked(true); cmdScr.addPreference(cpref); } /* * Available OBD protocols * */ for (ObdProtocols protocol : ObdProtocols.values()) { protocolStrings.add(protocol.name()); } listProtocols.setEntries(protocolStrings.toArray(new CharSequence[0])); listProtocols.setEntryValues(protocolStrings.toArray(new CharSequence[0])); /* * Let's use this device Bluetooth adapter to select which paired OBD-II * compliant device we'll use. */ final BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBtAdapter == null) { listBtDevices.setEntries(pairedDeviceStrings.toArray(new CharSequence[0])); listBtDevices.setEntryValues(vals.toArray(new CharSequence[0])); // we shouldn't get here, still warn user Toast.makeText(this, "This device does not support Bluetooth.", Toast.LENGTH_LONG).show(); return; } /* * Listen for preferences click. * * TODO there are so many repeated validations :-/ */ final Activity thisActivity = this; listBtDevices.setEntries(new CharSequence[1]); listBtDevices.setEntryValues(new CharSequence[1]); listBtDevices.setOnPreferenceClickListener( new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { // see what I mean in the previous comment? if (mBtAdapter == null || !mBtAdapter.isEnabled()) { Toast.makeText( thisActivity, "This device does not support Bluetooth or it is disabled.", Toast.LENGTH_LONG) .show(); return false; } return true; } }); /* * Get paired devices and populate preference list. */ Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { pairedDeviceStrings.add(device.getName() + "\n" + device.getAddress()); vals.add(device.getAddress()); } } listBtDevices.setEntries(pairedDeviceStrings.toArray(new CharSequence[0])); listBtDevices.setEntryValues(vals.toArray(new CharSequence[0])); }