99import android .os .IBinder ;
1010
1111import com .cooper .wheellog .utils .Constants ;
12+ import com .cooper .wheellog .utils .SettingsUtil ;
1213import com .getpebble .android .kit .PebbleKit ;
1314import com .getpebble .android .kit .util .PebbleDictionary ;
1415
@@ -28,19 +29,24 @@ public class PebbleService extends Service {
2829 static final int KEY_FAN_STATE = 3 ;
2930 static final int KEY_BT_STATE = 4 ;
3031 static final int KEY_VIBE_ALERT = 5 ;
32+ static final int KEY_USE_MPH = 6 ;
33+ static final int KEY_MAX_SPEED = 7 ;
3134
3235 private Handler mHandler = new Handler ();
3336 private static PebbleService instance = null ;
3437 private long last_message_send_time ;
38+ PebbleDictionary outgoingDictionary = new PebbleDictionary ();
3539
3640 int lastSpeed = 0 ;
3741 int lastBattery = 0 ;
3842 int lastTemperature = 0 ;
3943 int lastFanStatus = 0 ;
4044 boolean lastConnectionState = false ;
45+ int vibe_alarm = -1 ;
46+ boolean refreshAll = true ;
47+
4148 boolean message_pending = false ;
4249 boolean data_available = false ;
43- int vibe_alarm = -1 ;
4450
4551 public static boolean isInstanceCreated () {
4652 return instance != null ;
@@ -49,62 +55,76 @@ public static boolean isInstanceCreated() {
4955 private Runnable mSendPebbleData = new Runnable () {
5056 @ Override
5157 public void run () {
52- PebbleDictionary outgoing = new PebbleDictionary ();
5358
54- if (lastSpeed != WheelData .getInstance ().getSpeed ())
59+ if (refreshAll ) {
60+ outgoingDictionary .addInt32 (KEY_USE_MPH , SettingsUtil .isUseMPH (PebbleService .this ) ? 1 : 0 );
61+ outgoingDictionary .addInt32 (KEY_MAX_SPEED , SettingsUtil .getMaxSpeed (PebbleService .this ));
62+ }
63+
64+ if (refreshAll || lastSpeed != WheelData .getInstance ().getSpeed ())
5565 {
5666 lastSpeed = WheelData .getInstance ().getSpeed ();
57- outgoing .addInt32 (KEY_SPEED , lastSpeed );
67+ outgoingDictionary .addInt32 (KEY_SPEED , lastSpeed );
5868 }
5969
60- if (lastBattery != WheelData .getInstance ().getBatteryLevel ())
70+ if (refreshAll || lastBattery != WheelData .getInstance ().getBatteryLevel ())
6171 {
6272 lastBattery = WheelData .getInstance ().getBatteryLevel ();
63- outgoing .addInt32 (KEY_BATTERY , lastBattery );
73+ outgoingDictionary .addInt32 (KEY_BATTERY , lastBattery );
6474 }
6575
66- if (lastTemperature != WheelData .getInstance ().getTemperature ())
76+ if (refreshAll || lastTemperature != WheelData .getInstance ().getTemperature ())
6777 {
6878 lastTemperature = WheelData .getInstance ().getTemperature ();
69- outgoing .addInt32 (KEY_TEMPERATURE , lastTemperature );
79+ outgoingDictionary .addInt32 (KEY_TEMPERATURE , lastTemperature );
7080 }
7181
72- if (lastFanStatus != WheelData .getInstance ().getFanStatus ())
82+ if (refreshAll || lastFanStatus != WheelData .getInstance ().getFanStatus ())
7383 {
7484 lastFanStatus = WheelData .getInstance ().getFanStatus ();
75- outgoing .addInt32 (KEY_FAN_STATE , lastFanStatus );
85+ outgoingDictionary .addInt32 (KEY_FAN_STATE , lastFanStatus );
7686 }
7787
78- if (lastConnectionState != WheelData .getInstance ().isConnected ())
88+ if (refreshAll || lastConnectionState != WheelData .getInstance ().isConnected ())
7989 {
80- lastConnectionState = ! lastConnectionState ;
81- outgoing .addInt32 (KEY_BT_STATE , lastConnectionState ? 1 : 0 );
90+ lastConnectionState = WheelData . getInstance (). isConnected () ;
91+ outgoingDictionary .addInt32 (KEY_BT_STATE , lastConnectionState ? 1 : 0 );
8292 }
8393
8494 if (vibe_alarm >= 0 ) {
85- outgoing .addInt32 (KEY_VIBE_ALERT , vibe_alarm );
95+ outgoingDictionary .addInt32 (KEY_VIBE_ALERT , vibe_alarm );
8696 vibe_alarm = -1 ;
8797 }
8898
89- if (outgoing .size () > 0 )
90- {
99+ if (outgoingDictionary .size () > 0 ) {
91100 message_pending = true ;
92- PebbleKit .sendDataToPebble (getApplicationContext (), APP_UUID , outgoing );
101+ PebbleKit .sendDataToPebble (getApplicationContext (), APP_UUID , outgoingDictionary );
93102 }
94-
95103 last_message_send_time = Calendar .getInstance ().getTimeInMillis ();
96104 data_available = false ;
105+ refreshAll = false ;
97106 }
98107 };
99108
100- private final BroadcastReceiver mBreadcastReceiver = new BroadcastReceiver () {
109+ private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver () {
101110 @ Override
102111 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 ();
112+
113+
114+ switch (intent .getAction ()) {
115+ case Constants .ACTION_ALARM_TRIGGERED :
116+ if (intent .hasExtra (Constants .INTENT_EXTRA_ALARM_TYPE ))
117+ vibe_alarm = ((Constants .ALARM_TYPE ) intent .getSerializableExtra (Constants .INTENT_EXTRA_ALARM_TYPE )).getValue ();
118+ break ;
119+ case Constants .ACTION_PEBBLE_APP_READY :
120+ refreshAll = true ;
121+ break ;
122+ case Constants .ACTION_PEBBLE_AFFECTING_PREFERENCE_CHANGED :
123+ refreshAll = true ;
124+ break ;
106125 }
107126
127+ // There's something new to send, start the check
108128 if (message_pending &&
109129 last_message_send_time + MESSAGE_TIMEOUT >= Calendar .getInstance ().getTimeInMillis ())
110130 data_available = true ;
@@ -130,7 +150,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
130150 intentFilter .addAction (Constants .ACTION_BLUETOOTH_CONNECTION_STATE );
131151 intentFilter .addAction (Constants .ACTION_WHEEL_DATA_AVAILABLE );
132152 intentFilter .addAction (Constants .ACTION_ALARM_TRIGGERED );
133- registerReceiver (mBreadcastReceiver , intentFilter );
153+ intentFilter .addAction (Constants .ACTION_PEBBLE_APP_READY );
154+ intentFilter .addAction (Constants .ACTION_PEBBLE_AFFECTING_PREFERENCE_CHANGED );
155+ registerReceiver (mBroadcastReceiver , intentFilter );
134156
135157 Intent serviceStartedIntent = new Intent (Constants .ACTION_PEBBLE_SERVICE_TOGGLED )
136158 .putExtra (Constants .INTENT_EXTRA_IS_RUNNING , true );
@@ -143,7 +165,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
143165
144166 @ Override
145167 public void onDestroy () {
146- unregisterReceiver (mBreadcastReceiver );
168+ unregisterReceiver (mBroadcastReceiver );
147169 unregisterReceiver (ackReceiver );
148170 unregisterReceiver (nackReceiver );
149171 mHandler .removeCallbacksAndMessages (null );
@@ -161,21 +183,17 @@ public void onDestroy() {
161183 private PebbleKit .PebbleAckReceiver ackReceiver = new PebbleKit .PebbleAckReceiver (APP_UUID ) {
162184 @ Override
163185 public void receiveAck (Context context , int transactionId ) {
186+ outgoingDictionary = new PebbleDictionary ();
187+
164188 if (data_available )
165189 mHandler .post (mSendPebbleData );
166190 else
167- message_pending = false ;
168- }
191+ message_pending = false ;}
169192 };
170193
171194 private PebbleKit .PebbleNackReceiver nackReceiver = new PebbleKit .PebbleNackReceiver (APP_UUID ) {
172195 @ Override
173196 public void receiveNack (Context context , int transactionId ) {
174- lastSpeed = -1 ;
175- lastBattery = -1 ;
176- lastTemperature = -1 ;
177- lastConnectionState = false ;
178- lastFanStatus = -1 ;
179197 mHandler .post (mSendPebbleData );
180198 }
181199 };
0 commit comments