Skip to content

Commit

Permalink
Pebble settings moved to app.
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpMaster committed Sep 16, 2016
1 parent 7d094d7 commit 16d8dba
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 62 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
buildToolsVersion "24.0.2"

defaultConfig {
applicationId "com.cooper.wheellog"
Expand Down
79 changes: 62 additions & 17 deletions app/src/main/java/com/cooper/wheellog/PebbleBroadcastReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.widget.Toast;

import com.cooper.wheellog.utils.SettingsUtil;
import com.getpebble.android.kit.Constants;
import com.getpebble.android.kit.PebbleKit;
import com.getpebble.android.kit.util.PebbleDictionary;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.cooper.wheellog.utils.Constants.ACTION_PEBBLE_APP_READY;
import static com.cooper.wheellog.utils.Constants.ACTION_REQUEST_KINGSONG_HORN;
import static com.cooper.wheellog.utils.Constants.INTENT_EXTRA_LAUNCHED_FROM_PEBBLE;
import static com.cooper.wheellog.utils.Constants.INTENT_EXTRA_PEBBLE_APP_VERSION;
import static com.cooper.wheellog.utils.Constants.PEBBLE_APP_UUID;
import static com.cooper.wheellog.utils.Constants.PEBBLE_KEY_LAUNCH_APP;
import static com.cooper.wheellog.utils.Constants.PEBBLE_KEY_PLAY_HORN;
import static com.cooper.wheellog.utils.Constants.PEBBLE_KEY_WATCH_READY;

public class PebbleBroadcastReceiver extends BroadcastReceiver {


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

final int transactionId = intent.getIntExtra(Constants.TRANSACTION_ID, -1);
PebbleKit.sendAckToPebble(context, transactionId);

final String jsonData = intent.getStringExtra(Constants.MSG_DATA);
final PebbleDictionary data;

try {
final PebbleDictionary data = PebbleDictionary.fromJson(jsonData);
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);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainActivityIntent.putExtra(com.cooper.wheellog.utils.Constants.INTENT_EXTRA_LAUNCHED_FROM_PEBBLE, true);
context.getApplicationContext().startActivity(mainActivityIntent);

Intent pebbleServiceIntent = new Intent(context.getApplicationContext(), PebbleService.class);
context.startService(pebbleServiceIntent);
} else if (data.contains(com.cooper.wheellog.utils.Constants.PEBBLE_KEY_PLAY_HORN)) {
final Intent hornIntent = new Intent(com.cooper.wheellog.utils.Constants.ACTION_REQUEST_KINGSONG_HORN);
data = PebbleDictionary.fromJson(jsonData);
} catch (JSONException ex) {
return;
}
// Toast.makeText(context,jsonData, Toast.LENGTH_SHORT).show();
if (data.contains(PEBBLE_KEY_LAUNCH_APP) && !PebbleService.isInstanceCreated()) {
Intent mainActivityIntent = new Intent(context.getApplicationContext(), MainActivity.class);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainActivityIntent.putExtra(INTENT_EXTRA_LAUNCHED_FROM_PEBBLE, true);
context.getApplicationContext().startActivity(mainActivityIntent);

Intent pebbleServiceIntent = new Intent(context.getApplicationContext(), PebbleService.class);
context.startService(pebbleServiceIntent);
} else if (data.contains(PEBBLE_KEY_WATCH_READY)) {
int watch_app_version = data.getInteger(PEBBLE_KEY_WATCH_READY).intValue();
if (watch_app_version < com.cooper.wheellog.utils.Constants.PEBBLE_APP_VERSION)
sendPebbleAlert(context, "A newer version of the app is available. Please upgrade to make sure the app works as expected.");
Intent pebbleReadyIntent = new Intent(ACTION_PEBBLE_APP_READY);
pebbleReadyIntent.putExtra(INTENT_EXTRA_PEBBLE_APP_VERSION, watch_app_version);
context.sendBroadcast(pebbleReadyIntent);
} else if (data.contains(PEBBLE_KEY_PLAY_HORN)) {
int horn_mode = SettingsUtil.getHornMode(context);
if (horn_mode == 1) {
final Intent hornIntent = new Intent(ACTION_REQUEST_KINGSONG_HORN);
context.sendBroadcast(hornIntent);
} else if (data.contains(com.cooper.wheellog.utils.Constants.PEBBLE_KEY_PLAY_HORN_MP3)) {
} else if (horn_mode == 2) {
MediaPlayer mp = MediaPlayer.create(context, R.raw.bicycle_bell);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
Expand All @@ -50,8 +80,23 @@ public void onCompletion(MediaPlayer mp) {
}
});
}
} catch (JSONException ignored) { }
}
}
}


private void sendPebbleAlert(Context context, final String text) {
// Push a notification
final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION");
final Map<String, String> data = new HashMap<>();
data.put("title", "WheelLog");
data.put("body", text);
final JSONObject jsonData = new JSONObject(data);
final String notificationData = new JSONArray().put(jsonData).toString();
i.putExtra("messageType", "PEBBLE_ALERT");
i.putExtra("sender", "PebbleKit Android");
i.putExtra("notificationData", notificationData);
context.sendBroadcast(i);
}

}
80 changes: 49 additions & 31 deletions app/src/main/java/com/cooper/wheellog/PebbleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.IBinder;

import com.cooper.wheellog.utils.Constants;
import com.cooper.wheellog.utils.SettingsUtil;
import com.getpebble.android.kit.PebbleKit;
import com.getpebble.android.kit.util.PebbleDictionary;

Expand All @@ -28,19 +29,24 @@ public class PebbleService extends Service {
static final int KEY_FAN_STATE = 3;
static final int KEY_BT_STATE = 4;
static final int KEY_VIBE_ALERT = 5;
static final int KEY_USE_MPH = 6;
static final int KEY_MAX_SPEED = 7;

private Handler mHandler = new Handler();
private static PebbleService instance = null;
private long last_message_send_time;
PebbleDictionary outgoingDictionary = new PebbleDictionary();

int lastSpeed = 0;
int lastBattery = 0;
int lastTemperature = 0;
int lastFanStatus = 0;
boolean lastConnectionState = false;
int vibe_alarm = -1;
boolean refreshAll = true;

boolean message_pending = false;
boolean data_available = false;
int vibe_alarm = -1;

public static boolean isInstanceCreated() {
return instance != null;
Expand All @@ -49,62 +55,76 @@ public static boolean isInstanceCreated() {
private Runnable mSendPebbleData = new Runnable() {
@Override
public void run() {
PebbleDictionary outgoing = new PebbleDictionary();

if (lastSpeed != WheelData.getInstance().getSpeed())
if (refreshAll) {
outgoingDictionary.addInt32(KEY_USE_MPH, SettingsUtil.isUseMPH(PebbleService.this) ? 1 : 0);
outgoingDictionary.addInt32(KEY_MAX_SPEED, SettingsUtil.getMaxSpeed(PebbleService.this));
}

if (refreshAll || lastSpeed != WheelData.getInstance().getSpeed())
{
lastSpeed = WheelData.getInstance().getSpeed();
outgoing.addInt32(KEY_SPEED, lastSpeed);
outgoingDictionary.addInt32(KEY_SPEED, lastSpeed);
}

if (lastBattery != WheelData.getInstance().getBatteryLevel())
if (refreshAll || lastBattery != WheelData.getInstance().getBatteryLevel())
{
lastBattery = WheelData.getInstance().getBatteryLevel();
outgoing.addInt32(KEY_BATTERY, lastBattery);
outgoingDictionary.addInt32(KEY_BATTERY, lastBattery);
}

if (lastTemperature != WheelData.getInstance().getTemperature())
if (refreshAll || lastTemperature != WheelData.getInstance().getTemperature())
{
lastTemperature = WheelData.getInstance().getTemperature();
outgoing.addInt32(KEY_TEMPERATURE, lastTemperature);
outgoingDictionary.addInt32(KEY_TEMPERATURE, lastTemperature);
}

if (lastFanStatus != WheelData.getInstance().getFanStatus())
if (refreshAll || lastFanStatus != WheelData.getInstance().getFanStatus())
{
lastFanStatus = WheelData.getInstance().getFanStatus();
outgoing.addInt32(KEY_FAN_STATE, lastFanStatus);
outgoingDictionary.addInt32(KEY_FAN_STATE, lastFanStatus);
}

if (lastConnectionState != WheelData.getInstance().isConnected())
if (refreshAll || lastConnectionState != WheelData.getInstance().isConnected())
{
lastConnectionState = !lastConnectionState;
outgoing.addInt32(KEY_BT_STATE, lastConnectionState ? 1 : 0);
lastConnectionState = WheelData.getInstance().isConnected();
outgoingDictionary.addInt32(KEY_BT_STATE, lastConnectionState ? 1 : 0);
}

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

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

last_message_send_time = Calendar.getInstance().getTimeInMillis();
data_available = false;
refreshAll = false;
}
};

private final BroadcastReceiver mBreadcastReceiver = new BroadcastReceiver() {
private final BroadcastReceiver mBroadcastReceiver = 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();


switch (intent.getAction()) {
case Constants.ACTION_ALARM_TRIGGERED:
if (intent.hasExtra(Constants.INTENT_EXTRA_ALARM_TYPE))
vibe_alarm = ((Constants.ALARM_TYPE) intent.getSerializableExtra(Constants.INTENT_EXTRA_ALARM_TYPE)).getValue();
break;
case Constants.ACTION_PEBBLE_APP_READY:
refreshAll = true;
break;
case Constants.ACTION_PEBBLE_AFFECTING_PREFERENCE_CHANGED:
refreshAll = true;
break;
}

// There's something new to send, start the check
if (message_pending &&
last_message_send_time + MESSAGE_TIMEOUT >= Calendar.getInstance().getTimeInMillis())
data_available = true;
Expand All @@ -130,7 +150,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
intentFilter.addAction(Constants.ACTION_BLUETOOTH_CONNECTION_STATE);
intentFilter.addAction(Constants.ACTION_WHEEL_DATA_AVAILABLE);
intentFilter.addAction(Constants.ACTION_ALARM_TRIGGERED);
registerReceiver(mBreadcastReceiver, intentFilter);
intentFilter.addAction(Constants.ACTION_PEBBLE_APP_READY);
intentFilter.addAction(Constants.ACTION_PEBBLE_AFFECTING_PREFERENCE_CHANGED);
registerReceiver(mBroadcastReceiver, intentFilter);

Intent serviceStartedIntent = new Intent(Constants.ACTION_PEBBLE_SERVICE_TOGGLED)
.putExtra(Constants.INTENT_EXTRA_IS_RUNNING, true);
Expand All @@ -143,7 +165,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {

@Override
public void onDestroy() {
unregisterReceiver(mBreadcastReceiver);
unregisterReceiver(mBroadcastReceiver);
unregisterReceiver(ackReceiver);
unregisterReceiver(nackReceiver);
mHandler.removeCallbacksAndMessages(null);
Expand All @@ -161,21 +183,17 @@ public void onDestroy() {
private PebbleKit.PebbleAckReceiver ackReceiver = new PebbleKit.PebbleAckReceiver(APP_UUID) {
@Override
public void receiveAck(Context context, int transactionId) {
outgoingDictionary = new PebbleDictionary();

if (data_available)
mHandler.post(mSendPebbleData);
else
message_pending = false;
}
message_pending = false;}
};

private PebbleKit.PebbleNackReceiver nackReceiver = new PebbleKit.PebbleNackReceiver(APP_UUID) {
@Override
public void receiveNack(Context context, int transactionId) {
lastSpeed = -1;
lastBattery = -1;
lastTemperature = -1;
lastConnectionState = false;
lastFanStatus = -1;
mHandler.post(mSendPebbleData);
}
};
Expand Down
47 changes: 40 additions & 7 deletions app/src/main/java/com/cooper/wheellog/PreferencesFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@

import com.cooper.wheellog.utils.Constants;
import com.cooper.wheellog.utils.SettingsUtil;
import com.pavelsikun.seekbarpreference.SeekBarPreference;

public class PreferencesFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

enum SettingsScreen {
Main,
Speed,
Logs,
Alarms
Alarms,
Watch
}

private boolean mDataWarningDisplayed = false;
Expand Down Expand Up @@ -78,6 +80,13 @@ public void onClick(DialogInterface dialog, int which) {
.show();
} else
mDataWarningDisplayed = false;
break;
case "use_mph":
getActivity().sendBroadcast(new Intent(Constants.ACTION_PEBBLE_AFFECTING_PREFERENCE_CHANGED));
break;
case "max_mph":
getActivity().sendBroadcast(new Intent(Constants.ACTION_PEBBLE_AFFECTING_PREFERENCE_CHANGED));
break;
}
getActivity().sendBroadcast(new Intent(Constants.ACTION_PREFERENCE_CHANGED));
}
Expand All @@ -102,6 +111,7 @@ public void onClick(View view) {
Preference speed_button = findPreference("speed_preferences");
Preference logs_button = findPreference("log_preferences");
Preference alarm_button = findPreference("alarm_preferences");
Preference watch_button = findPreference("watch_preferences");

if (speed_button != null) {
speed_button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
Expand Down Expand Up @@ -139,6 +149,18 @@ public boolean onPreferenceClick(Preference preference) {
}
});
}
if (watch_button != null) {
watch_button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
currentScreen = SettingsScreen.Watch;
getPreferenceScreen().removeAll();
addPreferencesFromResource(R.xml.preferences_watch);
setup_screen();
return true;
}
});
}
break;
case Speed:
tb.setTitle("Speed Settings");
Expand All @@ -150,6 +172,9 @@ public boolean onPreferenceClick(Preference preference) {
tb.setTitle("Alarm Settings");
hideShowSeekBars();
break;
case Watch:
tb.setTitle("Watch Settings");
break;
}
}

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

private void hideShowSeekBars() {
boolean alarms_enabled = getPreferenceManager().getSharedPreferences().getBoolean("alarms_enabled", false);
findPreference("alarm_1_speed").setEnabled(alarms_enabled);
findPreference("alarm_2_speed").setEnabled(alarms_enabled);
findPreference("alarm_3_speed").setEnabled(alarms_enabled);
findPreference("alarm_1_battery").setEnabled(alarms_enabled);
findPreference("alarm_2_battery").setEnabled(alarms_enabled);
findPreference("alarm_3_battery").setEnabled(alarms_enabled);
String[] seekbar_preferences = {
"alarm_1_speed",
"alarm_2_speed",
"alarm_3_speed",
"alarm_1_battery",
"alarm_2_battery",
"alarm_3_battery",
"alarm_current"};

for (String preference : seekbar_preferences) {
SeekBarPreference seekbar = (SeekBarPreference) findPreference(preference);
if (seekbar != null)
seekbar.setEnabled(alarms_enabled);
}
}

public boolean show_main_menu() {
Expand Down
Loading

0 comments on commit 16d8dba

Please sign in to comment.