Skip to content

Commit 16d8dba

Browse files
author
JumpMaster
committed
Pebble settings moved to app.
1 parent 7d094d7 commit 16d8dba

15 files changed

Lines changed: 204 additions & 62 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 24
5-
buildToolsVersion "24.0.0"
5+
buildToolsVersion "24.0.2"
66

77
defaultConfig {
88
applicationId "com.cooper.wheellog"

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

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,29 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.media.MediaPlayer;
7-
import android.widget.Toast;
87

8+
import com.cooper.wheellog.utils.SettingsUtil;
99
import com.getpebble.android.kit.Constants;
1010
import com.getpebble.android.kit.PebbleKit;
1111
import com.getpebble.android.kit.util.PebbleDictionary;
12+
13+
import org.json.JSONArray;
1214
import org.json.JSONException;
15+
import org.json.JSONObject;
16+
17+
import java.util.HashMap;
18+
import java.util.Map;
1319
import java.util.UUID;
1420

21+
import static com.cooper.wheellog.utils.Constants.ACTION_PEBBLE_APP_READY;
22+
import static com.cooper.wheellog.utils.Constants.ACTION_REQUEST_KINGSONG_HORN;
23+
import static com.cooper.wheellog.utils.Constants.INTENT_EXTRA_LAUNCHED_FROM_PEBBLE;
24+
import static com.cooper.wheellog.utils.Constants.INTENT_EXTRA_PEBBLE_APP_VERSION;
25+
import static com.cooper.wheellog.utils.Constants.PEBBLE_APP_UUID;
26+
import static com.cooper.wheellog.utils.Constants.PEBBLE_KEY_LAUNCH_APP;
27+
import static com.cooper.wheellog.utils.Constants.PEBBLE_KEY_PLAY_HORN;
28+
import static com.cooper.wheellog.utils.Constants.PEBBLE_KEY_WATCH_READY;
29+
1530
public class PebbleBroadcastReceiver extends BroadcastReceiver {
1631

1732

@@ -20,27 +35,42 @@ public void onReceive(Context context, Intent intent) {
2035
if (intent.getAction().equals(Constants.INTENT_APP_RECEIVE)) {
2136
final UUID receivedUuid = (UUID) intent.getSerializableExtra(Constants.APP_UUID);
2237
// Pebble-enabled apps are expected to be good citizens and only inspect broadcasts containing their UUID
23-
if (!com.cooper.wheellog.utils.Constants.PEBBLE_APP_UUID.equals(receivedUuid))
38+
if (!PEBBLE_APP_UUID.equals(receivedUuid))
2439
return;
2540

2641
final int transactionId = intent.getIntExtra(Constants.TRANSACTION_ID, -1);
42+
PebbleKit.sendAckToPebble(context, transactionId);
43+
2744
final String jsonData = intent.getStringExtra(Constants.MSG_DATA);
45+
final PebbleDictionary data;
46+
2847
try {
29-
final PebbleDictionary data = PebbleDictionary.fromJson(jsonData);
30-
Toast.makeText(context,jsonData, Toast.LENGTH_SHORT).show();
31-
PebbleKit.sendAckToPebble(context, transactionId);
32-
if (data.contains(com.cooper.wheellog.utils.Constants.PEBBLE_KEY_LAUNCH_APP) && !PebbleService.isInstanceCreated()) {
33-
Intent mainActivityIntent = new Intent(context.getApplicationContext(), MainActivity.class);
34-
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
35-
mainActivityIntent.putExtra(com.cooper.wheellog.utils.Constants.INTENT_EXTRA_LAUNCHED_FROM_PEBBLE, true);
36-
context.getApplicationContext().startActivity(mainActivityIntent);
37-
38-
Intent pebbleServiceIntent = new Intent(context.getApplicationContext(), PebbleService.class);
39-
context.startService(pebbleServiceIntent);
40-
} else if (data.contains(com.cooper.wheellog.utils.Constants.PEBBLE_KEY_PLAY_HORN)) {
41-
final Intent hornIntent = new Intent(com.cooper.wheellog.utils.Constants.ACTION_REQUEST_KINGSONG_HORN);
48+
data = PebbleDictionary.fromJson(jsonData);
49+
} catch (JSONException ex) {
50+
return;
51+
}
52+
// Toast.makeText(context,jsonData, Toast.LENGTH_SHORT).show();
53+
if (data.contains(PEBBLE_KEY_LAUNCH_APP) && !PebbleService.isInstanceCreated()) {
54+
Intent mainActivityIntent = new Intent(context.getApplicationContext(), MainActivity.class);
55+
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
56+
mainActivityIntent.putExtra(INTENT_EXTRA_LAUNCHED_FROM_PEBBLE, true);
57+
context.getApplicationContext().startActivity(mainActivityIntent);
58+
59+
Intent pebbleServiceIntent = new Intent(context.getApplicationContext(), PebbleService.class);
60+
context.startService(pebbleServiceIntent);
61+
} else if (data.contains(PEBBLE_KEY_WATCH_READY)) {
62+
int watch_app_version = data.getInteger(PEBBLE_KEY_WATCH_READY).intValue();
63+
if (watch_app_version < com.cooper.wheellog.utils.Constants.PEBBLE_APP_VERSION)
64+
sendPebbleAlert(context, "A newer version of the app is available. Please upgrade to make sure the app works as expected.");
65+
Intent pebbleReadyIntent = new Intent(ACTION_PEBBLE_APP_READY);
66+
pebbleReadyIntent.putExtra(INTENT_EXTRA_PEBBLE_APP_VERSION, watch_app_version);
67+
context.sendBroadcast(pebbleReadyIntent);
68+
} else if (data.contains(PEBBLE_KEY_PLAY_HORN)) {
69+
int horn_mode = SettingsUtil.getHornMode(context);
70+
if (horn_mode == 1) {
71+
final Intent hornIntent = new Intent(ACTION_REQUEST_KINGSONG_HORN);
4272
context.sendBroadcast(hornIntent);
43-
} else if (data.contains(com.cooper.wheellog.utils.Constants.PEBBLE_KEY_PLAY_HORN_MP3)) {
73+
} else if (horn_mode == 2) {
4474
MediaPlayer mp = MediaPlayer.create(context, R.raw.bicycle_bell);
4575
mp.start();
4676
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@@ -50,8 +80,23 @@ public void onCompletion(MediaPlayer mp) {
5080
}
5181
});
5282
}
53-
} catch (JSONException ignored) { }
83+
}
5484
}
5585
}
5686

87+
88+
private void sendPebbleAlert(Context context, final String text) {
89+
// Push a notification
90+
final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION");
91+
final Map<String, String> data = new HashMap<>();
92+
data.put("title", "WheelLog");
93+
data.put("body", text);
94+
final JSONObject jsonData = new JSONObject(data);
95+
final String notificationData = new JSONArray().put(jsonData).toString();
96+
i.putExtra("messageType", "PEBBLE_ALERT");
97+
i.putExtra("sender", "PebbleKit Android");
98+
i.putExtra("notificationData", notificationData);
99+
context.sendBroadcast(i);
100+
}
101+
57102
}

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

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.os.IBinder;
1010

1111
import com.cooper.wheellog.utils.Constants;
12+
import com.cooper.wheellog.utils.SettingsUtil;
1213
import com.getpebble.android.kit.PebbleKit;
1314
import 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
};

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

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313

1414
import com.cooper.wheellog.utils.Constants;
1515
import com.cooper.wheellog.utils.SettingsUtil;
16+
import com.pavelsikun.seekbarpreference.SeekBarPreference;
1617

1718
public class PreferencesFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
1819

1920
enum SettingsScreen {
2021
Main,
2122
Speed,
2223
Logs,
23-
Alarms
24+
Alarms,
25+
Watch
2426
}
2527

2628
private boolean mDataWarningDisplayed = false;
@@ -78,6 +80,13 @@ public void onClick(DialogInterface dialog, int which) {
7880
.show();
7981
} else
8082
mDataWarningDisplayed = false;
83+
break;
84+
case "use_mph":
85+
getActivity().sendBroadcast(new Intent(Constants.ACTION_PEBBLE_AFFECTING_PREFERENCE_CHANGED));
86+
break;
87+
case "max_mph":
88+
getActivity().sendBroadcast(new Intent(Constants.ACTION_PEBBLE_AFFECTING_PREFERENCE_CHANGED));
89+
break;
8190
}
8291
getActivity().sendBroadcast(new Intent(Constants.ACTION_PREFERENCE_CHANGED));
8392
}
@@ -102,6 +111,7 @@ public void onClick(View view) {
102111
Preference speed_button = findPreference("speed_preferences");
103112
Preference logs_button = findPreference("log_preferences");
104113
Preference alarm_button = findPreference("alarm_preferences");
114+
Preference watch_button = findPreference("watch_preferences");
105115

106116
if (speed_button != null) {
107117
speed_button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@@ -139,6 +149,18 @@ public boolean onPreferenceClick(Preference preference) {
139149
}
140150
});
141151
}
152+
if (watch_button != null) {
153+
watch_button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
154+
@Override
155+
public boolean onPreferenceClick(Preference preference) {
156+
currentScreen = SettingsScreen.Watch;
157+
getPreferenceScreen().removeAll();
158+
addPreferencesFromResource(R.xml.preferences_watch);
159+
setup_screen();
160+
return true;
161+
}
162+
});
163+
}
142164
break;
143165
case Speed:
144166
tb.setTitle("Speed Settings");
@@ -150,6 +172,9 @@ public boolean onPreferenceClick(Preference preference) {
150172
tb.setTitle("Alarm Settings");
151173
hideShowSeekBars();
152174
break;
175+
case Watch:
176+
tb.setTitle("Watch Settings");
177+
break;
153178
}
154179
}
155180

@@ -175,12 +200,20 @@ private void correctCheckState(String preference) {
175200

176201
private void hideShowSeekBars() {
177202
boolean alarms_enabled = getPreferenceManager().getSharedPreferences().getBoolean("alarms_enabled", false);
178-
findPreference("alarm_1_speed").setEnabled(alarms_enabled);
179-
findPreference("alarm_2_speed").setEnabled(alarms_enabled);
180-
findPreference("alarm_3_speed").setEnabled(alarms_enabled);
181-
findPreference("alarm_1_battery").setEnabled(alarms_enabled);
182-
findPreference("alarm_2_battery").setEnabled(alarms_enabled);
183-
findPreference("alarm_3_battery").setEnabled(alarms_enabled);
203+
String[] seekbar_preferences = {
204+
"alarm_1_speed",
205+
"alarm_2_speed",
206+
"alarm_3_speed",
207+
"alarm_1_battery",
208+
"alarm_2_battery",
209+
"alarm_3_battery",
210+
"alarm_current"};
211+
212+
for (String preference : seekbar_preferences) {
213+
SeekBarPreference seekbar = (SeekBarPreference) findPreference(preference);
214+
if (seekbar != null)
215+
seekbar.setEnabled(alarms_enabled);
216+
}
184217
}
185218

186219
public boolean show_main_menu() {

0 commit comments

Comments
 (0)