Skip to content

Commit

Permalink
Triggered alarms are now forwarded to the smart watch.
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpMaster committed Sep 13, 2016
1 parent b911656 commit 7d094d7
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 43 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.cooper.wheellog"
minSdkVersion 18
targetSdkVersion 24
versionCode 30
versionName "1.5.1"
versionCode 31
versionName "1.6.0"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Timber.i("Disconnected from GATT server.");
mDisconnectTime = Calendar.getInstance().getTime();
if (mConnectionState == STATE_CONNECTED)
mDisconnectTime = Calendar.getInstance().getTime();
if (!disconnectRequested &&
mBluetoothGatt != null && mBluetoothGatt.getDevice() != null) {
Timber.i("Trying to reconnect");
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/cooper/wheellog/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,14 @@ private void loadPreferences() {
int alarm1Battery = sharedPreferences.getInt("alarm_1_battery", 0);
int alarm2Battery = sharedPreferences.getInt("alarm_2_battery", 0);
int alarm3Battery = sharedPreferences.getInt("alarm_3_battery", 0);
int current_alarm = sharedPreferences.getInt("alarm_current", 0);
boolean disablePhoneVibrate = sharedPreferences.getBoolean("disable_phone_vibrate", false);

WheelData.getInstance().setSpeedAlarmSpeed(
WheelData.getInstance().setPreferences(
alarm1Speed, alarm1Battery,
alarm2Speed, alarm2Battery,
alarm3Speed, alarm3Battery);
alarm3Speed, alarm3Battery,
current_alarm, disablePhoneVibrate);
wheelView.setWarningSpeed(alarm1Speed);
} else
wheelView.setWarningSpeed(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.widget.Toast;

import com.getpebble.android.kit.Constants;
import com.getpebble.android.kit.PebbleKit;
Expand All @@ -26,7 +27,7 @@ public void onReceive(Context context, Intent intent) {
final String jsonData = intent.getStringExtra(Constants.MSG_DATA);
try {
final PebbleDictionary data = PebbleDictionary.fromJson(jsonData);
// Toast.makeText(context,jsonData, Toast.LENGTH_SHORT).show();
Toast.makeText(context,jsonData, Toast.LENGTH_SHORT).show();
PebbleKit.sendAckToPebble(context, transactionId);
if (data.contains(com.cooper.wheellog.utils.Constants.PEBBLE_KEY_LAUNCH_APP) && !PebbleService.isInstanceCreated()) {
Intent mainActivityIntent = new Intent(context.getApplicationContext(), MainActivity.class);
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/java/com/cooper/wheellog/PebbleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
public class PebbleService extends Service {

private static final UUID APP_UUID = UUID.fromString("185c8ae9-7e72-451a-a1c7-8f1e81df9a3d");
private static final int MESSAGE_TIMEOUT = 1000;
private static final int MESSAGE_TIMEOUT = 500; // milliseconds

static final int KEY_SPEED = 0;
static final int KEY_BATTERY = 1;
static final int KEY_TEMPERATURE = 2;
static final int KEY_FAN_STATE = 3;
static final int KEY_BT_STATE = 4;
static final int KEY_VIBE_ALERT = 5;

private Handler mHandler = new Handler();
private static PebbleService instance = null;
Expand All @@ -39,6 +40,7 @@ public class PebbleService extends Service {
boolean lastConnectionState = false;
boolean message_pending = false;
boolean data_available = false;
int vibe_alarm = -1;

public static boolean isInstanceCreated() {
return instance != null;
Expand Down Expand Up @@ -79,11 +81,17 @@ public void run() {
outgoing.addInt32(KEY_BT_STATE, lastConnectionState ? 1 : 0);
}

if (vibe_alarm >= 0) {
outgoing.addInt32(KEY_VIBE_ALERT, vibe_alarm);
vibe_alarm = -1;
}

if (outgoing.size() > 0)
{
message_pending = true;
PebbleKit.sendDataToPebble(getApplicationContext(), APP_UUID, outgoing);
}

last_message_send_time = Calendar.getInstance().getTimeInMillis();
data_available = false;
}
Expand All @@ -92,8 +100,13 @@ public void run() {
private final BroadcastReceiver mBreadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Constants.ACTION_ALARM_TRIGGERED.equals(intent.getAction())) {
if (intent.hasExtra(Constants.INTENT_EXTRA_ALARM_TYPE))
vibe_alarm = ((Constants.ALARM_TYPE) intent.getSerializableExtra(Constants.INTENT_EXTRA_ALARM_TYPE)).getValue();
}

if (message_pending && last_message_send_time + MESSAGE_TIMEOUT >= Calendar.getInstance().getTimeInMillis())
if (message_pending &&
last_message_send_time + MESSAGE_TIMEOUT >= Calendar.getInstance().getTimeInMillis())
data_available = true;
else
mHandler.post(mSendPebbleData);
Expand All @@ -116,6 +129,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Constants.ACTION_BLUETOOTH_CONNECTION_STATE);
intentFilter.addAction(Constants.ACTION_WHEEL_DATA_AVAILABLE);
intentFilter.addAction(Constants.ACTION_ALARM_TRIGGERED);
registerReceiver(mBreadcastReceiver, intentFilter);

Intent serviceStartedIntent = new Intent(Constants.ACTION_PEBBLE_SERVICE_TOGGLED)
Expand Down
71 changes: 43 additions & 28 deletions app/src/main/java/com/cooper/wheellog/WheelData.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.Vibrator;

import com.cooper.wheellog.utils.Constants;
import com.cooper.wheellog.utils.Constants.ALARM_TYPE;
import com.cooper.wheellog.utils.Constants.WHEEL_TYPE;

import java.text.SimpleDateFormat;
Expand All @@ -22,10 +23,6 @@ public class WheelData {
private static WheelData mInstance;
private static Context mContext;

private enum AlarmType {
speed
}

private BluetoothLeService mBluetoothLeService;

private long graph_last_update_time;
Expand Down Expand Up @@ -59,14 +56,17 @@ private enum AlarmType {
private long mStartTotalDistance;

private boolean mAlarmsEnabled = false;
private boolean mDisablePhoneVibrate = false;
private int mAlarm1Speed = 0;
private int mAlarm2Speed = 0;
private int mAlarm3Speed = 0;
private int mAlarm1Battery = 0;
private int mAlarm2Battery = 0;
private int mAlarm3Battery = 0;
private int mAlarmCurrent = 0;

private boolean mAlarmExecuted = false;
private boolean mSpeedAlarmExecuted = false;
private boolean mCurrentAlarmExecuted = false;

public static void initiate(Context context) {
if (mInstance == null)
Expand All @@ -92,10 +92,6 @@ public int getBatteryLevel() {
return mBattery;
}

public double getAverageBatteryLevel() {
return mAverageBattery;
}

public int getFanStatus() {
return mFanStatus;
}
Expand Down Expand Up @@ -188,15 +184,18 @@ public void setAlarmsEnabled(boolean enabled) {
mAlarmsEnabled = enabled;
}

public void setSpeedAlarmSpeed(int alarm1Speed, int alarm1Battery,
public void setPreferences(int alarm1Speed, int alarm1Battery,
int alarm2Speed, int alarm2Battery,
int alarm3Speed, int alarm3Battery) {
int alarm3Speed, int alarm3Battery,
int alarmCurrent, boolean disablePhoneVibrate) {
mAlarm1Speed = alarm1Speed * 100;
mAlarm2Speed = alarm2Speed * 100;
mAlarm3Speed = alarm3Speed * 100;
mAlarm1Battery = alarm1Battery;
mAlarm2Battery = alarm2Battery;
mAlarm3Battery = alarm3Battery;
mAlarmCurrent = alarmCurrent*100;
mDisablePhoneVibrate = disablePhoneVibrate;
}

private int byteArrayInt2(byte low, byte high) {
Expand Down Expand Up @@ -235,48 +234,64 @@ private void setBatteryPercent(int battery) {
}

private void checkAlarmStatus() {
if (!mAlarmExecuted) {
// SPEED ALARM
if (!mSpeedAlarmExecuted) {
if (mAlarm1Speed > 0 && mAlarm1Battery > 0 &&
mAverageBattery <= mAlarm1Battery && mSpeed >= mAlarm1Speed)
vibrate(AlarmType.speed);
raiseAlarm(ALARM_TYPE.SPEED);
else if (mAlarm2Speed > 0 && mAlarm2Battery > 0 &&
mAverageBattery <= mAlarm2Battery && mSpeed >= mAlarm2Speed)
vibrate(AlarmType.speed);
raiseAlarm(ALARM_TYPE.SPEED);
else if (mAlarm3Speed > 0 && mAlarm3Battery > 0 &&
mAverageBattery <= mAlarm3Battery && mSpeed >= mAlarm3Speed)
vibrate(AlarmType.speed);
raiseAlarm(ALARM_TYPE.SPEED);
} else {
boolean alarm_required = false;
boolean alarm_finished = false;
if (mAlarm1Speed > 0 && mAlarm1Battery > 0 &&
mAverageBattery > mAlarm1Battery && mSpeed < mAlarm1Speed)
alarm_required = true;
alarm_finished = true;
else if (mAlarm2Speed > 0 && mAlarm2Battery > 0 &&
mAverageBattery <= mAlarm2Battery && mSpeed >= mAlarm2Speed)
alarm_required = true;
alarm_finished = true;
else if (mAlarm3Speed > 0 && mAlarm3Battery > 0 &&
mAverageBattery <= mAlarm3Battery && mSpeed >= mAlarm3Speed)
alarm_required = true;
alarm_finished = true;

mAlarmExecuted = alarm_required;
mSpeedAlarmExecuted = alarm_finished;
}

// CURRENT
if (!mCurrentAlarmExecuted) {
if (mAlarmCurrent > 0 &&
mCurrent >= mAlarmCurrent) {
raiseAlarm(ALARM_TYPE.CURRENT);
}
} else {
if (mCurrent < mAlarmCurrent)
mCurrentAlarmExecuted = false;
}
}

private void vibrate(AlarmType alarmType) {
private void raiseAlarm(ALARM_TYPE alarmType) {
Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);

if (!v.hasVibrator())
return;

long[] pattern = {0};
Intent intent = new Intent(Constants.ACTION_ALARM_TRIGGERED);
intent.putExtra(Constants.INTENT_EXTRA_ALARM_TYPE, alarmType);

switch (alarmType) {
case speed:
case SPEED:
pattern = new long[]{0, 300, 150, 300, 150, 500};
mAlarmExecuted = true;
mSpeedAlarmExecuted = true;
break;
case CURRENT:
pattern = new long[]{0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100};
mCurrentAlarmExecuted = true;
break;
}

v.vibrate(pattern, -1);
mContext.sendBroadcast(intent);
if (v.hasVibrator() && !mDisablePhoneVibrate)
v.vibrate(pattern, -1);
}

public void decodeResponse(byte[] data) {
Expand Down
24 changes: 21 additions & 3 deletions app/src/main/java/com/cooper/wheellog/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Constants {
public static final String ACTION_LOGGING_SERVICE_TOGGLED = "com.cooper.wheellog.loggingServiceToggled";
public static final String ACTION_REQUEST_CONNECTION_TOGGLE = "com.cooper.wheellog.requestConnectionToggle";
public static final String ACTION_PREFERENCE_CHANGED = "com.cooper.wheellog.preferenceChanged";
public static final String ACTION_ALARM_TRIGGERED = "com.cooper.wheellog.speedAlert";

public static final String NOTIFICATION_BUTTON_CONNECTION = "com.cooper.wheellog.notificationConnectionButton";
public static final String NOTIFICATION_BUTTON_LOGGING = "com.cooper.wheellog.notificationLoggingButton";
Expand All @@ -26,16 +27,17 @@ public class Constants {
public static final String GOTWAY_SERVICE_UUID = "0000ffe0-0000-1000-8000-00805f9b34fb";

public static final UUID PEBBLE_APP_UUID = UUID.fromString("185c8ae9-7e72-451a-a1c7-8f1e81df9a3d");
public static final int PEBBLE_KEY_LAUNCH_APP = 10008;
public static final int PEBBLE_KEY_PLAY_HORN = 10009;
public static final int PEBBLE_KEY_PLAY_HORN_MP3 = 10010;
public static final int PEBBLE_KEY_LAUNCH_APP = 10006;
public static final int PEBBLE_KEY_PLAY_HORN = 10007;
public static final int PEBBLE_KEY_PLAY_HORN_MP3 = 10008;

public static final String INTENT_EXTRA_LAUNCHED_FROM_PEBBLE = "launched_from_pebble";
public static final String INTENT_EXTRA_BLE_AUTO_CONNECT = "ble_auto_connect";
public static final String INTENT_EXTRA_LOGGING_FILE_LOCATION = "logging_file_location";
public static final String INTENT_EXTRA_IS_RUNNING = "is_running";
public static final String INTENT_EXTRA_GRAPH_UPDATE_AVILABLE = "graph_update_available";
public static final String INTENT_EXTRA_CONNECTION_STATE = "connection_state";
public static final String INTENT_EXTRA_ALARM_TYPE = "alarm_type";

public enum WHEEL_TYPE {
Unknown,
Expand All @@ -44,6 +46,22 @@ public enum WHEEL_TYPE {
NINEBOT
}

public enum ALARM_TYPE {
SPEED(0),
CURRENT(1);

private final int value;

ALARM_TYPE(int value) {
this.value = value;
}

public int getValue() {
return value;
}

}

public static final int MAIN_NOTIFICATION_ID = 10;

public static final String LOG_FOLDER_NAME = "WheelLog Logs";
Expand Down
32 changes: 28 additions & 4 deletions app/src/main/res/xml/preferences_alarms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
android:title="Enable Alarms"
android:summary="Allow the phone to vibrate as a warning" />

<CheckBoxPreference
android:key="disable_phone_vibrate"
android:dependency="alarms_enabled"
android:title="Disable Phone Vibration"
android:summary="Phone will not vibrate but the alarm will be passed
to a connected Pebble Watch" />

<PreferenceCategory
android:title="Alarm 1">
android:title="Speed Alarm 1">

<com.pavelsikun.seekbarpreference.SeekBarPreference
android:key="alarm_1_speed"
android:title="Speed"
android:summary="Speed that triggers the phone to vibrate"
android:summary="Speed that triggers the alarm"
android:defaultValue="29"
sample:dependency="alarms_enabled"
sample:msbp_minValue="0"
Expand All @@ -38,7 +45,7 @@
</PreferenceCategory>

<PreferenceCategory
android:title="Alarm 2">
android:title="Speed Alarm 2">

<com.pavelsikun.seekbarpreference.SeekBarPreference
android:key="alarm_2_speed"
Expand Down Expand Up @@ -67,7 +74,7 @@
</PreferenceCategory>

<PreferenceCategory
android:title="Alarm 3">
android:title="Speed Alarm 3">

<com.pavelsikun.seekbarpreference.SeekBarPreference
android:key="alarm_3_speed"
Expand Down Expand Up @@ -95,5 +102,22 @@

</PreferenceCategory>

<PreferenceCategory
android:title="Current Alarm">

<com.pavelsikun.seekbarpreference.SeekBarPreference
android:key="alarm_current"
android:title="Current"
android:summary="Current that triggers the alarm"
android:defaultValue="35"
sample:dependency="alarms_enabled"
sample:msbp_minValue="0"
sample:msbp_maxValue="50"
sample:msbp_interval="1"
sample:msbp_measurementUnit="A"
sample:msbp_dialogEnabled="true"/>

</PreferenceCategory>


</PreferenceScreen>

0 comments on commit 7d094d7

Please sign in to comment.