Skip to content

Commit 7d094d7

Browse files
author
JumpMaster
committed
Triggered alarms are now forwarded to the smart watch.
1 parent b911656 commit 7d094d7

8 files changed

Lines changed: 119 additions & 43 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "com.cooper.wheellog"
99
minSdkVersion 18
1010
targetSdkVersion 24
11-
versionCode 30
12-
versionName "1.5.1"
11+
versionCode 31
12+
versionName "1.6.0"
1313
}
1414
buildTypes {
1515
release {

app/src/main/java/com/cooper/wheellog/BluetoothLeService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
150150
mBluetoothGatt.discoverServices());
151151
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
152152
Timber.i("Disconnected from GATT server.");
153-
mDisconnectTime = Calendar.getInstance().getTime();
153+
if (mConnectionState == STATE_CONNECTED)
154+
mDisconnectTime = Calendar.getInstance().getTime();
154155
if (!disconnectRequested &&
155156
mBluetoothGatt != null && mBluetoothGatt.getDevice() != null) {
156157
Timber.i("Trying to reconnect");

app/src/main/java/com/cooper/wheellog/MainActivity.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,11 +742,14 @@ private void loadPreferences() {
742742
int alarm1Battery = sharedPreferences.getInt("alarm_1_battery", 0);
743743
int alarm2Battery = sharedPreferences.getInt("alarm_2_battery", 0);
744744
int alarm3Battery = sharedPreferences.getInt("alarm_3_battery", 0);
745+
int current_alarm = sharedPreferences.getInt("alarm_current", 0);
746+
boolean disablePhoneVibrate = sharedPreferences.getBoolean("disable_phone_vibrate", false);
745747

746-
WheelData.getInstance().setSpeedAlarmSpeed(
748+
WheelData.getInstance().setPreferences(
747749
alarm1Speed, alarm1Battery,
748750
alarm2Speed, alarm2Battery,
749-
alarm3Speed, alarm3Battery);
751+
alarm3Speed, alarm3Battery,
752+
current_alarm, disablePhoneVibrate);
750753
wheelView.setWarningSpeed(alarm1Speed);
751754
} else
752755
wheelView.setWarningSpeed(0);

app/src/main/java/com/cooper/wheellog/PebbleBroadcastReceiver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.media.MediaPlayer;
7+
import android.widget.Toast;
78

89
import com.getpebble.android.kit.Constants;
910
import com.getpebble.android.kit.PebbleKit;
@@ -26,7 +27,7 @@ public void onReceive(Context context, Intent intent) {
2627
final String jsonData = intent.getStringExtra(Constants.MSG_DATA);
2728
try {
2829
final PebbleDictionary data = PebbleDictionary.fromJson(jsonData);
29-
// Toast.makeText(context,jsonData, Toast.LENGTH_SHORT).show();
30+
Toast.makeText(context,jsonData, Toast.LENGTH_SHORT).show();
3031
PebbleKit.sendAckToPebble(context, transactionId);
3132
if (data.contains(com.cooper.wheellog.utils.Constants.PEBBLE_KEY_LAUNCH_APP) && !PebbleService.isInstanceCreated()) {
3233
Intent mainActivityIntent = new Intent(context.getApplicationContext(), MainActivity.class);

app/src/main/java/com/cooper/wheellog/PebbleService.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
public class PebbleService extends Service {
2121

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

2525
static final int KEY_SPEED = 0;
2626
static final int KEY_BATTERY = 1;
2727
static final int KEY_TEMPERATURE = 2;
2828
static final int KEY_FAN_STATE = 3;
2929
static final int KEY_BT_STATE = 4;
30+
static final int KEY_VIBE_ALERT = 5;
3031

3132
private Handler mHandler = new Handler();
3233
private static PebbleService instance = null;
@@ -39,6 +40,7 @@ public class PebbleService extends Service {
3940
boolean lastConnectionState = false;
4041
boolean message_pending = false;
4142
boolean data_available = false;
43+
int vibe_alarm = -1;
4244

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

84+
if (vibe_alarm >= 0) {
85+
outgoing.addInt32(KEY_VIBE_ALERT, vibe_alarm);
86+
vibe_alarm = -1;
87+
}
88+
8289
if (outgoing.size() > 0)
8390
{
8491
message_pending = true;
8592
PebbleKit.sendDataToPebble(getApplicationContext(), APP_UUID, outgoing);
8693
}
94+
8795
last_message_send_time = Calendar.getInstance().getTimeInMillis();
8896
data_available = false;
8997
}
@@ -92,8 +100,13 @@ public void run() {
92100
private final BroadcastReceiver mBreadcastReceiver = new BroadcastReceiver() {
93101
@Override
94102
public void onReceive(Context context, Intent intent) {
103+
if (Constants.ACTION_ALARM_TRIGGERED.equals(intent.getAction())) {
104+
if (intent.hasExtra(Constants.INTENT_EXTRA_ALARM_TYPE))
105+
vibe_alarm = ((Constants.ALARM_TYPE) intent.getSerializableExtra(Constants.INTENT_EXTRA_ALARM_TYPE)).getValue();
106+
}
95107

96-
if (message_pending && last_message_send_time + MESSAGE_TIMEOUT >= Calendar.getInstance().getTimeInMillis())
108+
if (message_pending &&
109+
last_message_send_time + MESSAGE_TIMEOUT >= Calendar.getInstance().getTimeInMillis())
97110
data_available = true;
98111
else
99112
mHandler.post(mSendPebbleData);
@@ -116,6 +129,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
116129
IntentFilter intentFilter = new IntentFilter();
117130
intentFilter.addAction(Constants.ACTION_BLUETOOTH_CONNECTION_STATE);
118131
intentFilter.addAction(Constants.ACTION_WHEEL_DATA_AVAILABLE);
132+
intentFilter.addAction(Constants.ACTION_ALARM_TRIGGERED);
119133
registerReceiver(mBreadcastReceiver, intentFilter);
120134

121135
Intent serviceStartedIntent = new Intent(Constants.ACTION_PEBBLE_SERVICE_TOGGLED)

app/src/main/java/com/cooper/wheellog/WheelData.java

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.os.Vibrator;
99

1010
import com.cooper.wheellog.utils.Constants;
11+
import com.cooper.wheellog.utils.Constants.ALARM_TYPE;
1112
import com.cooper.wheellog.utils.Constants.WHEEL_TYPE;
1213

1314
import java.text.SimpleDateFormat;
@@ -22,10 +23,6 @@ public class WheelData {
2223
private static WheelData mInstance;
2324
private static Context mContext;
2425

25-
private enum AlarmType {
26-
speed
27-
}
28-
2926
private BluetoothLeService mBluetoothLeService;
3027

3128
private long graph_last_update_time;
@@ -59,14 +56,17 @@ private enum AlarmType {
5956
private long mStartTotalDistance;
6057

6158
private boolean mAlarmsEnabled = false;
59+
private boolean mDisablePhoneVibrate = false;
6260
private int mAlarm1Speed = 0;
6361
private int mAlarm2Speed = 0;
6462
private int mAlarm3Speed = 0;
6563
private int mAlarm1Battery = 0;
6664
private int mAlarm2Battery = 0;
6765
private int mAlarm3Battery = 0;
66+
private int mAlarmCurrent = 0;
6867

69-
private boolean mAlarmExecuted = false;
68+
private boolean mSpeedAlarmExecuted = false;
69+
private boolean mCurrentAlarmExecuted = false;
7070

7171
public static void initiate(Context context) {
7272
if (mInstance == null)
@@ -92,10 +92,6 @@ public int getBatteryLevel() {
9292
return mBattery;
9393
}
9494

95-
public double getAverageBatteryLevel() {
96-
return mAverageBattery;
97-
}
98-
9995
public int getFanStatus() {
10096
return mFanStatus;
10197
}
@@ -188,15 +184,18 @@ public void setAlarmsEnabled(boolean enabled) {
188184
mAlarmsEnabled = enabled;
189185
}
190186

191-
public void setSpeedAlarmSpeed(int alarm1Speed, int alarm1Battery,
187+
public void setPreferences(int alarm1Speed, int alarm1Battery,
192188
int alarm2Speed, int alarm2Battery,
193-
int alarm3Speed, int alarm3Battery) {
189+
int alarm3Speed, int alarm3Battery,
190+
int alarmCurrent, boolean disablePhoneVibrate) {
194191
mAlarm1Speed = alarm1Speed * 100;
195192
mAlarm2Speed = alarm2Speed * 100;
196193
mAlarm3Speed = alarm3Speed * 100;
197194
mAlarm1Battery = alarm1Battery;
198195
mAlarm2Battery = alarm2Battery;
199196
mAlarm3Battery = alarm3Battery;
197+
mAlarmCurrent = alarmCurrent*100;
198+
mDisablePhoneVibrate = disablePhoneVibrate;
200199
}
201200

202201
private int byteArrayInt2(byte low, byte high) {
@@ -235,48 +234,64 @@ private void setBatteryPercent(int battery) {
235234
}
236235

237236
private void checkAlarmStatus() {
238-
if (!mAlarmExecuted) {
237+
// SPEED ALARM
238+
if (!mSpeedAlarmExecuted) {
239239
if (mAlarm1Speed > 0 && mAlarm1Battery > 0 &&
240240
mAverageBattery <= mAlarm1Battery && mSpeed >= mAlarm1Speed)
241-
vibrate(AlarmType.speed);
241+
raiseAlarm(ALARM_TYPE.SPEED);
242242
else if (mAlarm2Speed > 0 && mAlarm2Battery > 0 &&
243243
mAverageBattery <= mAlarm2Battery && mSpeed >= mAlarm2Speed)
244-
vibrate(AlarmType.speed);
244+
raiseAlarm(ALARM_TYPE.SPEED);
245245
else if (mAlarm3Speed > 0 && mAlarm3Battery > 0 &&
246246
mAverageBattery <= mAlarm3Battery && mSpeed >= mAlarm3Speed)
247-
vibrate(AlarmType.speed);
247+
raiseAlarm(ALARM_TYPE.SPEED);
248248
} else {
249-
boolean alarm_required = false;
249+
boolean alarm_finished = false;
250250
if (mAlarm1Speed > 0 && mAlarm1Battery > 0 &&
251251
mAverageBattery > mAlarm1Battery && mSpeed < mAlarm1Speed)
252-
alarm_required = true;
252+
alarm_finished = true;
253253
else if (mAlarm2Speed > 0 && mAlarm2Battery > 0 &&
254254
mAverageBattery <= mAlarm2Battery && mSpeed >= mAlarm2Speed)
255-
alarm_required = true;
255+
alarm_finished = true;
256256
else if (mAlarm3Speed > 0 && mAlarm3Battery > 0 &&
257257
mAverageBattery <= mAlarm3Battery && mSpeed >= mAlarm3Speed)
258-
alarm_required = true;
258+
alarm_finished = true;
259259

260-
mAlarmExecuted = alarm_required;
260+
mSpeedAlarmExecuted = alarm_finished;
261+
}
262+
263+
// CURRENT
264+
if (!mCurrentAlarmExecuted) {
265+
if (mAlarmCurrent > 0 &&
266+
mCurrent >= mAlarmCurrent) {
267+
raiseAlarm(ALARM_TYPE.CURRENT);
268+
}
269+
} else {
270+
if (mCurrent < mAlarmCurrent)
271+
mCurrentAlarmExecuted = false;
261272
}
262273
}
263274

264-
private void vibrate(AlarmType alarmType) {
275+
private void raiseAlarm(ALARM_TYPE alarmType) {
265276
Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
266277

267-
if (!v.hasVibrator())
268-
return;
269-
270278
long[] pattern = {0};
279+
Intent intent = new Intent(Constants.ACTION_ALARM_TRIGGERED);
280+
intent.putExtra(Constants.INTENT_EXTRA_ALARM_TYPE, alarmType);
271281

272282
switch (alarmType) {
273-
case speed:
283+
case SPEED:
274284
pattern = new long[]{0, 300, 150, 300, 150, 500};
275-
mAlarmExecuted = true;
285+
mSpeedAlarmExecuted = true;
286+
break;
287+
case CURRENT:
288+
pattern = new long[]{0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100};
289+
mCurrentAlarmExecuted = true;
276290
break;
277291
}
278-
279-
v.vibrate(pattern, -1);
292+
mContext.sendBroadcast(intent);
293+
if (v.hasVibrator() && !mDisablePhoneVibrate)
294+
v.vibrate(pattern, -1);
280295
}
281296

282297
public void decodeResponse(byte[] data) {

app/src/main/java/com/cooper/wheellog/utils/Constants.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Constants {
1313
public static final String ACTION_LOGGING_SERVICE_TOGGLED = "com.cooper.wheellog.loggingServiceToggled";
1414
public static final String ACTION_REQUEST_CONNECTION_TOGGLE = "com.cooper.wheellog.requestConnectionToggle";
1515
public static final String ACTION_PREFERENCE_CHANGED = "com.cooper.wheellog.preferenceChanged";
16+
public static final String ACTION_ALARM_TRIGGERED = "com.cooper.wheellog.speedAlert";
1617

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

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

3334
public static final String INTENT_EXTRA_LAUNCHED_FROM_PEBBLE = "launched_from_pebble";
3435
public static final String INTENT_EXTRA_BLE_AUTO_CONNECT = "ble_auto_connect";
3536
public static final String INTENT_EXTRA_LOGGING_FILE_LOCATION = "logging_file_location";
3637
public static final String INTENT_EXTRA_IS_RUNNING = "is_running";
3738
public static final String INTENT_EXTRA_GRAPH_UPDATE_AVILABLE = "graph_update_available";
3839
public static final String INTENT_EXTRA_CONNECTION_STATE = "connection_state";
40+
public static final String INTENT_EXTRA_ALARM_TYPE = "alarm_type";
3941

4042
public enum WHEEL_TYPE {
4143
Unknown,
@@ -44,6 +46,22 @@ public enum WHEEL_TYPE {
4446
NINEBOT
4547
}
4648

49+
public enum ALARM_TYPE {
50+
SPEED(0),
51+
CURRENT(1);
52+
53+
private final int value;
54+
55+
ALARM_TYPE(int value) {
56+
this.value = value;
57+
}
58+
59+
public int getValue() {
60+
return value;
61+
}
62+
63+
}
64+
4765
public static final int MAIN_NOTIFICATION_ID = 10;
4866

4967
public static final String LOG_FOLDER_NAME = "WheelLog Logs";

app/src/main/res/xml/preferences_alarms.xml

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88
android:title="Enable Alarms"
99
android:summary="Allow the phone to vibrate as a warning" />
1010

11+
<CheckBoxPreference
12+
android:key="disable_phone_vibrate"
13+
android:dependency="alarms_enabled"
14+
android:title="Disable Phone Vibration"
15+
android:summary="Phone will not vibrate but the alarm will be passed
16+
to a connected Pebble Watch" />
17+
1118
<PreferenceCategory
12-
android:title="Alarm 1">
19+
android:title="Speed Alarm 1">
1320

1421
<com.pavelsikun.seekbarpreference.SeekBarPreference
1522
android:key="alarm_1_speed"
1623
android:title="Speed"
17-
android:summary="Speed that triggers the phone to vibrate"
24+
android:summary="Speed that triggers the alarm"
1825
android:defaultValue="29"
1926
sample:dependency="alarms_enabled"
2027
sample:msbp_minValue="0"
@@ -38,7 +45,7 @@
3845
</PreferenceCategory>
3946

4047
<PreferenceCategory
41-
android:title="Alarm 2">
48+
android:title="Speed Alarm 2">
4249

4350
<com.pavelsikun.seekbarpreference.SeekBarPreference
4451
android:key="alarm_2_speed"
@@ -67,7 +74,7 @@
6774
</PreferenceCategory>
6875

6976
<PreferenceCategory
70-
android:title="Alarm 3">
77+
android:title="Speed Alarm 3">
7178

7279
<com.pavelsikun.seekbarpreference.SeekBarPreference
7380
android:key="alarm_3_speed"
@@ -95,5 +102,22 @@
95102

96103
</PreferenceCategory>
97104

105+
<PreferenceCategory
106+
android:title="Current Alarm">
107+
108+
<com.pavelsikun.seekbarpreference.SeekBarPreference
109+
android:key="alarm_current"
110+
android:title="Current"
111+
android:summary="Current that triggers the alarm"
112+
android:defaultValue="35"
113+
sample:dependency="alarms_enabled"
114+
sample:msbp_minValue="0"
115+
sample:msbp_maxValue="50"
116+
sample:msbp_interval="1"
117+
sample:msbp_measurementUnit="A"
118+
sample:msbp_dialogEnabled="true"/>
119+
120+
</PreferenceCategory>
121+
98122

99123
</PreferenceScreen>

0 commit comments

Comments
 (0)