Skip to content

Commit

Permalink
Merge pull request #305 from enviroCar/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
dewall authored Jun 15, 2016
2 parents 04941f6 + 41b83fa commit 95464c4
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 213 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ ext {
compileSdkVersion = 23
targetSdkVersion = 23
buildToolsVersion = '23.0.2'
versionCode = 32
versionName = "0.22.3"
versionCode = 33
versionName = "0.22.4"

javaCompileVersion = JavaVersion.VERSION_1_8

Expand Down
4 changes: 2 additions & 2 deletions org.envirocar.app/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
xmlns:android="http://schemas.android.com/apk/res/android"
package="org.envirocar.app"
android:installLocation="internalOnly"
android:versionCode="32"
android:versionName="0.22.3">
android:versionCode="33"
android:versionName="0.22.4">

<uses-sdk
android:minSdkVersion="16"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@
import com.f2prateek.rx.preferences.RxSharedPreferences;
import com.google.common.base.Preconditions;

import org.envirocar.core.entity.Car;
import org.envirocar.core.utils.CarUtils;

import rx.Observable;

import static org.envirocar.app.services.obd.OBDServiceHandler.context;

/**
* TODO JavaDoc
*
Expand Down Expand Up @@ -136,4 +141,11 @@ public static Observable<Boolean> getDieselConsumptionObservable(Context context
.getBoolean(PREF_ENABLE_DIESE_CONSUMPTION, false)
.asObservable();
}

public static Observable<Car> getSelectedCarObsevable(){
return getRxSharedPreferences(context)
.getString(PREFERENCE_TAG_CAR, null)
.asObservable()
.map(s -> s != null ? CarUtils.instantiateCar(s) : null);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* Copyright (C) 2013 - 2015 the enviroCar community
*
* <p>
* This file is part of the enviroCar app.
*
* <p>
* The enviroCar app is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* <p>
* The enviroCar app is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* <p>
* You should have received a copy of the GNU General Public License along
* with the enviroCar app. If not, see http://www.gnu.org/licenses/.
*/
Expand All @@ -24,19 +24,16 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.widget.Toast;

import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import org.envirocar.app.handler.TrackRecordingHandler;
import org.envirocar.app.handler.BluetoothHandler;
import org.envirocar.app.handler.CarPreferenceHandler;
import org.envirocar.app.handler.PreferenceConstants;
import org.envirocar.app.handler.PreferencesHandler;
import org.envirocar.app.handler.TrackRecordingHandler;
import org.envirocar.app.services.obd.OBDServiceHandler;
import org.envirocar.app.services.obd.OBDServiceState;
import org.envirocar.core.events.NewCarTypeSelectedEvent;
Expand All @@ -60,6 +57,8 @@
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

import static org.envirocar.app.services.obd.OBDServiceHandler.context;

/**
* TODO JavaDoc
*
Expand All @@ -68,11 +67,11 @@
public class SystemStartupService extends Service {
private static final Logger LOGGER = Logger.getLogger(SystemStartupService.class);

public static final void startService(Context context){
public static final void startService(Context context) {
ServiceUtils.startService(context, SystemStartupService.class);
}

public static final void stopService(Context context){
public static final void stopService(Context context) {
ServiceUtils.stopService(context, SystemStartupService.class);
}

Expand All @@ -96,8 +95,9 @@ public static final void stopService(Context context){

private Scheduler.Worker mWorkerThread = Schedulers.newThread().createWorker();
private Scheduler.Worker mMainThreadWorker = AndroidSchedulers.mainThread().createWorker();
private boolean mIsAutoconnect;
private int mDiscoveryInterval;
private boolean mIsAutoconnect = PreferencesHandler.DEFAULT_BLUETOOTH_AUTOCONNECT;
private boolean hasCarSelected = false;
private int mDiscoveryInterval = PreferencesHandler.DEFAULT_BLUETOOTH_DISCOVERY_INTERVAL;

// private member fields.
private Subscription mWorkerSubscription;
Expand Down Expand Up @@ -172,14 +172,7 @@ public void onCreate() {
this.mBus.register(this);

// Get the required preference settings.
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
this.mIsAutoconnect = preferences.getBoolean(PreferenceConstants
.PREF_BLUETOOTH_AUTOCONNECT, PreferenceConstants
.DEFAULT_BLUETOOTH_AUTOCONNECT);
this.mDiscoveryInterval = preferences.getInt(PreferenceConstants
.PREF_BLUETOOTH_DISCOVERY_INTERVAL,
PreferenceConstants.DEFAULT_BLUETOOTH_DISCOVERY_INTERVAL);
this.mDiscoveryInterval = PreferencesHandler.getDiscoveryInterval(context);

// Register a new BroadcastReceiver that waits for different incoming actions issued from
// the notification.
Expand All @@ -190,23 +183,36 @@ public void onCreate() {
notificationClickedFilter.addAction(ACTION_STOP_TRACK_RECORDING);
registerReceiver(mBroadcastReciever, notificationClickedFilter);

// if the OBDConnectionService is running, then bind the remoteService.
// bindOBDConnectionService();

// Set the Notification to
if (this.mBluetoothHandler.isBluetoothEnabled()) {
// State: No OBD device selected.
if (mBluetoothHandler.getSelectedBluetoothDevice() == null) {
OBDServiceHandler.setRecordingState(OBDServiceState.NO_OBD_SELECTED);
} else if (mCarManager.getCar() == null) {
OBDServiceHandler.setRecordingState(OBDServiceState.NO_CAR_SELECTED);
} else {
OBDServiceHandler.setRecordingState(OBDServiceState.UNCONNECTED);
}
}

subscriptions.add(
PreferencesHandler.getAutoconnectObservable(getApplicationContext())
.subscribe(aBoolean -> {
LOGGER.info(String.format("Received changed autoconnect -> [%s]",
aBoolean));
mIsAutoconnect = aBoolean;

// if autoconnect has been enabled, then schedule a new discovery.
if (mIsAutoconnect) {
scheduleDiscovery(REDISCOVERY_INTERVAL);
} else { // otherwise, unschedule
PreferencesHandler.getSelectedCarObsevable()
.map(car -> (car != null))
.subscribe(hasCar -> {
LOGGER.info(String.format("Received changed selected car -> [%s]",
hasCar));

if(!hasCarSelected){
if(hasCar){
hasCarSelected = hasCar;
scheduleDiscovery(mDiscoveryInterval);
}
} else if (hasCarSelected && !hasCar){
hasCarSelected = hasCar;
unscheduleDiscovery();
}

hasCarSelected = hasCar;
}));

subscriptions.add(
Expand All @@ -215,23 +221,23 @@ public void onCreate() {
LOGGER.info(String.format("Received changed discovery interval -> [%s]",
integer));
mDiscoveryInterval = integer;
}));

// Set the Notification to
if (this.mBluetoothHandler.isBluetoothEnabled()) {
// State: No OBD device selected.
if (mBluetoothHandler.getSelectedBluetoothDevice() == null) {
OBDServiceHandler.setRecordingState(OBDServiceState.NO_OBD_SELECTED);
} else if (mCarManager.getCar() == null) {
OBDServiceHandler.setRecordingState(OBDServiceState.NO_CAR_SELECTED);
} else {
OBDServiceHandler.setRecordingState(OBDServiceState.UNCONNECTED);
scheduleDiscovery(mDiscoveryInterval);
})
);

subscriptions.add(
PreferencesHandler.getAutoconnectObservable(getApplicationContext())
.subscribe(aBoolean -> {
LOGGER.info(String.format("Received changed autoconnect -> [%s]",
aBoolean));
mIsAutoconnect = aBoolean;

scheduleDiscovery(mDiscoveryInterval);
})
);


if (mIsAutoconnect) {
scheduleDiscovery(-1);
}
}
}
}


Expand Down Expand Up @@ -364,9 +370,9 @@ private void updateNotificationState(OBDServiceState state) {
OBDServiceHandler.setRecordingState(OBDServiceState.NO_CAR_SELECTED);
} else {
OBDServiceState currentState = OBDServiceHandler.getRecordingState();
if(currentState != OBDServiceState.DISCOVERING &&
state != OBDServiceState.DISCOVERING){
if(mIsAutoconnect){
if (currentState != OBDServiceState.DISCOVERING &&
state != OBDServiceState.DISCOVERING) {
if (mIsAutoconnect) {
scheduleDiscovery(REDISCOVERY_INTERVAL);
}
}
Expand All @@ -375,6 +381,12 @@ private void updateNotificationState(OBDServiceState state) {
}
}

/**
* Schedules the immediate discovery for the selected OBDII adapter.
*/
private void scheduleDiscovery() {
this.scheduleDiscovery(-1);
}

/**
* Schedules the discovery for the selected OBDII adapter with a specific delay.
Expand All @@ -386,10 +398,17 @@ private void scheduleDiscovery(int delay) {
// Unschedule all outstanding work.
unscheduleDiscovery();

// Reschedule a fresh discovery.
mWorkerSubscription = mWorkerThread.schedule(() -> {
startDiscoveryForSelectedDevice();
}, delay, TimeUnit.SECONDS);
// if autoconnect has been enabled and a car has been selected, then schedule a new
// discovery.
if (mIsAutoconnect && hasCarSelected && this.mBluetoothHandler.isBluetoothEnabled()) {
// Reschedule a fresh discovery.
mWorkerSubscription = mWorkerThread.schedule(() -> {
startDiscoveryForSelectedDevice();
}, delay, TimeUnit.SECONDS);

LOGGER.info("Discovery subscription has been scheduled -> [%s]", "" +
delay);
}
}

/**
Expand Down Expand Up @@ -508,7 +527,8 @@ public void onNext(BluetoothDevice device) {
// notification state to OBD_FOUND and stop the bluetooth discovery.
// TODO
// OBDServiceHandler.setRecordingState();
// mNotificationHandler.setNotificationState(SystemStartupService.this,
// mNotificationHandler.setNotificationState(SystemStartupService
// .this,
// NotificationHandler.NotificationState.OBD_FOUND);
scheduleDiscovery(REDISCOVERY_INTERVAL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ private double getEditTextDoubleValue(String input) throws ParseException {
return 0.0;
}

String toParse = yea[0].replaceAll(",", ".");
String toParse = yea[0].replace(",", ".");
try{
return Double.parseDouble(toParse);
} catch (NumberFormatException e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private void pushToEventBus(DataResponse dataResponse) {
protected PropertyKeyEvent[] createEventsFromDataResponse(DataResponse dataResponse) {
PID pid = dataResponse.getPid();
switch (pid) {
case FUEL_SYSTEM_STATUS:
// case FUEL_SYSTEM_STATUS:
case CALCULATED_ENGINE_LOAD:
case SHORT_TERM_FUEL_TRIM_BANK_1:
case LONG_TERM_FUEL_TRIM_BANK_1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static DriveDeckPID fromDefaultPID(PID p) {
case SHORT_TERM_FUEL_TRIM_BANK_1:
case LONG_TERM_FUEL_TRIM_BANK_1:
case FUEL_PRESSURE:
case FUEL_SYSTEM_STATUS:
// case FUEL_SYSTEM_STATUS:
return null;
case CALCULATED_ENGINE_LOAD:
return DriveDeckPID.ENGINE_LOAD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

public enum PID implements PIDEnumInstance {

FUEL_SYSTEM_STATUS {
@Override
public String getHexadecimalRepresentation() {
return "03";
}
},
// FUEL_SYSTEM_STATUS {
// @Override
// public String getHexadecimalRepresentation() {
// return "03";
// }
// },
CALCULATED_ENGINE_LOAD {
@Override
public String getHexadecimalRepresentation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static PIDCommand instantiateCommand(String pid) {

public static PIDCommand instantiateCommand(PID pid) {
switch (pid) {
case FUEL_SYSTEM_STATUS:
// case FUEL_SYSTEM_STATUS:
case CALCULATED_ENGINE_LOAD:
case FUEL_PRESSURE:
case INTAKE_MAP:
Expand All @@ -70,9 +70,8 @@ public static PIDCommand instantiateCommand(PID pid) {

public static Measurement.PropertyKey toPropertyKey(PID pid) {
switch (pid) {

case FUEL_SYSTEM_STATUS:
return Measurement.PropertyKey.FUEL_SYSTEM_STATUS_CODE;
// case FUEL_SYSTEM_STATUS:
// return Measurement.PropertyKey.FUEL_SYSTEM_STATUS_CODE;
case CALCULATED_ENGINE_LOAD:
return Measurement.PropertyKey.ENGINE_LOAD;
case SHORT_TERM_FUEL_TRIM_BANK_1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.envirocar.obd.commands.response.entity.EngineLoadResponse;
import org.envirocar.obd.commands.response.entity.EngineRPMResponse;
import org.envirocar.obd.commands.response.entity.FuelPressureResponse;
import org.envirocar.obd.commands.response.entity.FuelSystemStatusResponse;
import org.envirocar.obd.commands.response.entity.GenericDataResponse;
import org.envirocar.obd.commands.response.entity.IntakeAirTemperatureResponse;
import org.envirocar.obd.commands.response.entity.IntakeManifoldAbsolutePressureResponse;
Expand Down Expand Up @@ -98,8 +97,8 @@ else if (index == 2) {

private DataResponse createDataResponse(PID pid, int[] processedData, byte[] rawData) throws InvalidCommandResponseException, UnmatchedResponseException {
switch (pid) {
case FUEL_SYSTEM_STATUS:
return FuelSystemStatusResponse.fromRawData(rawData);
// case FUEL_SYSTEM_STATUS:
// return FuelSystemStatusResponse.fromRawData(rawData);
case CALCULATED_ENGINE_LOAD:
return new EngineLoadResponse((processedData[2] * 100.0f) / 255.0f);
case FUEL_PRESSURE:
Expand Down
Loading

0 comments on commit 95464c4

Please sign in to comment.