Skip to content

Commit

Permalink
support sending battery level
Browse files Browse the repository at this point in the history
fixes #10
  • Loading branch information
ostrya committed Oct 29, 2019
1 parent 41c822d commit 0a39a3d
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import androidx.preference.PreferenceManager;
import com.hypertrack.hyperlog.HyperLog;
import org.ostrya.presencepublisher.message.Message;
import org.ostrya.presencepublisher.message.battery.BatteryMessageProvider;
import org.ostrya.presencepublisher.message.wifi.WifiMessageProvider;
import org.ostrya.presencepublisher.mqtt.MqttService;
import org.ostrya.presencepublisher.receiver.AlarmReceiver;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -53,6 +55,7 @@ public class ForegroundService extends Service {
private SharedPreferences sharedPreferences;
private long nextPing;
private WifiMessageProvider wifiMessageProvider;
private BatteryMessageProvider batteryMessageProvider;
private final OnSharedPreferenceChangeListener sharedPreferenceListener = this::onSharedPreferenceChanged;

public static void startService(Context context) {
Expand Down Expand Up @@ -107,6 +110,7 @@ public void onCreate() {
lastPing = sharedPreferences.getLong(LAST_PING, 0L);
nextPing = sharedPreferences.getLong(NEXT_PING, 0L);
wifiMessageProvider = new WifiMessageProvider(this);
batteryMessageProvider = new BatteryMessageProvider(this);
registerPreferenceCallback();
migrateOldPreference();
registerNetworkCallback();
Expand Down Expand Up @@ -154,7 +158,7 @@ private void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Stri
case PORT:
case TLS:
case CLIENT_CERT:
case TOPIC:
case PRESENCE_TOPIC:
case PING:
case LOGIN:
case PASSWORD:
Expand Down Expand Up @@ -227,6 +231,9 @@ private void doSend(List<Message> messages) {
}

private List<Message> getMessagesToSend() {
return wifiMessageProvider.getMessages();
List<Message> result = new ArrayList<>();
result.addAll(wifiMessageProvider.getMessages());
result.addAll(batteryMessageProvider.getMessages());
return Collections.unmodifiableList(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.ostrya.presencepublisher.message.battery;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.BatteryManager;
import androidx.preference.PreferenceManager;
import com.hypertrack.hyperlog.HyperLog;
import org.ostrya.presencepublisher.message.Message;

import java.util.Collections;
import java.util.List;

import static org.ostrya.presencepublisher.ui.ScheduleFragment.BATTERY_MESSAGE;
import static org.ostrya.presencepublisher.ui.ScheduleFragment.BATTERY_TOPIC;

public class BatteryMessageProvider {
private static final String TAG = "BatteryMessageProvider";

private final Context applicationContext;
private final SharedPreferences sharedPreferences;

public BatteryMessageProvider(Context context) {
this.applicationContext = context.getApplicationContext();
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
}

public List<Message> getMessages() {
if (!sharedPreferences.getBoolean(BATTERY_MESSAGE, false)) {
HyperLog.d(TAG, "Battery messages disabled, not generating any messages");
return Collections.emptyList();
}
String topic = sharedPreferences.getString(BATTERY_TOPIC, null);
if (topic == null) {
HyperLog.w(TAG, "No topic defined, not generating any messages");
return Collections.emptyList();
}

IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = applicationContext.registerReceiver(null, filter);

if (batteryStatus == null) {
HyperLog.w(TAG, "No battery status received, unable to generate message");
return Collections.emptyList();
}

int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

int batteryPct = (int) (level / (0.01f * scale));

return Collections.singletonList(Message.messageForTopic(topic)
.withContent(Integer.toString(batteryPct)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static android.content.Context.CONNECTIVITY_SERVICE;
import static android.content.Context.WIFI_SERVICE;
import static org.ostrya.presencepublisher.ui.ConnectionFragment.TOPIC;
import static org.ostrya.presencepublisher.ui.ConnectionFragment.PRESENCE_TOPIC;
import static org.ostrya.presencepublisher.ui.ContentFragment.*;
import static org.ostrya.presencepublisher.ui.ScheduleFragment.*;

Expand All @@ -37,7 +37,7 @@ public WifiMessageProvider(Context context) {
}

public List<Message> getMessages() {
String topic = sharedPreferences.getString(TOPIC, null);
String topic = sharedPreferences.getString(PRESENCE_TOPIC, null);
if (topic == null) {
HyperLog.w(TAG, "No topic defined, not generating any messages");
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ConnectionFragment extends PreferenceFragmentCompat {
public static final String PORT = "port";
public static final String TLS = "tls";
public static final String CLIENT_CERT = "client_cert";
public static final String TOPIC = "topic";
public static final String PRESENCE_TOPIC = "topic";
public static final String PING = "ping";
public static final String LOGIN = "login";
public static final String PASSWORD = "password";
Expand Down Expand Up @@ -64,7 +64,7 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
});
clientCert.setIconSpaceReserved(false);

EditTextPreference topic = getEditTextPreference(context, TOPIC, R.string.topic_title, R.string.topic_summary, new RegexValidator("[^ ]+"));
EditTextPreference presenceTopic = getEditTextPreference(context, PRESENCE_TOPIC, R.string.presence_topic_title, R.string.presence_topic_summary, new RegexValidator("[^ ]+"));

Preference checkConnection = new Preference(context);
checkConnection.setTitle(R.string.check_connection_title);
Expand All @@ -82,7 +82,7 @@ public void onCreatePreferences(final Bundle savedInstanceState, final String ro
screen.addPreference(password);
screen.addPreference(tls);
screen.addPreference(clientCert);
screen.addPreference(topic);
screen.addPreference(presenceTopic);
screen.addPreference(checkConnection);

setPreferenceScreen(screen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.preference.EditTextPreference;
import androidx.preference.MultiSelectListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
Expand All @@ -12,11 +13,13 @@
import org.ostrya.presencepublisher.R;
import org.ostrya.presencepublisher.message.wifi.SsidUtil;
import org.ostrya.presencepublisher.ui.util.ExplanationSummaryProvider;
import org.ostrya.presencepublisher.ui.util.RegexValidator;
import org.ostrya.presencepublisher.ui.util.TimestampSummaryProvider;

import java.util.List;

import static org.ostrya.presencepublisher.ui.ConnectionFragment.PING;
import static org.ostrya.presencepublisher.ui.util.EditTextPreferencesHelper.getEditTextPreference;
import static org.ostrya.presencepublisher.ui.util.ExplanationSummaryProvider.PreferenceType.LIST;

public class ScheduleFragment extends PreferenceFragmentCompat {
Expand All @@ -31,6 +34,8 @@ public class ScheduleFragment extends PreferenceFragmentCompat {
public static final String NEXT_PING = "nextPing";
public static final String OFFLINE_PING = "offlinePing";
public static final String MOBILE_NETWORK_PING = "mobileNetworkPing";
public static final String BATTERY_MESSAGE = "batteryMessage";
public static final String BATTERY_TOPIC = "batteryTopic";
private Preference lastPing;
private Preference nextPing;
private final SharedPreferences.OnSharedPreferenceChangeListener listener = this::onPreferencesChanged;
Expand Down Expand Up @@ -74,6 +79,14 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
sendMobileNetworkPing.setSummary(R.string.offlineping_via_mobile_summary);
sendMobileNetworkPing.setIconSpaceReserved(false);

SwitchPreferenceCompat sendBatteryMessage = new SwitchPreferenceCompat(context);
sendBatteryMessage.setKey(BATTERY_MESSAGE);
sendBatteryMessage.setTitle(getString(R.string.battery_message_title));
sendBatteryMessage.setSummary(R.string.battery_message_summary);
sendBatteryMessage.setIconSpaceReserved(false);

EditTextPreference batteryTopic = getEditTextPreference(context, BATTERY_TOPIC, R.string.battery_topic_title, R.string.battery_topic_summary, new RegexValidator("[^ ]+"));

SwitchPreferenceCompat autostart = new SwitchPreferenceCompat(context);
autostart.setKey(AUTOSTART);
autostart.setTitle(getString(R.string.autostart_title));
Expand All @@ -96,13 +109,16 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
screen.addPreference(ping);
screen.addPreference(sendOfflinePing);
screen.addPreference(sendMobileNetworkPing);
screen.addPreference(sendBatteryMessage);
screen.addPreference(batteryTopic);
screen.addPreference(autostart);
screen.addPreference(lastPing);
screen.addPreference(nextPing);

setPreferenceScreen(screen);

sendMobileNetworkPing.setDependency(OFFLINE_PING);
batteryTopic.setDependency(BATTERY_MESSAGE);
}

@Override
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<string name="app_name">Presence Publisher</string>
<string name="host_title">MQTT Host</string>
<string name="host_summary">IP-Adresse oder Hostname.%nAktueller Wert: %s</string>
<string name="topic_title">Topic</string>
<string name="topic_summary">Das Topic, zu dem gesendet wird.%nAktueller Wert: %s</string>
<string name="presence_topic_title">Präsenz-Topic</string>
<string name="presence_topic_summary">Das Topic, zu dem Präsenznachrichten gesendet werden.%nAktueller Wert: %s</string>
<string name="ping_title">Sendeperiode</string>
<string name="ping_summary">Zeit zwischen Benachrichtigungen in Minuten.</string>
<string name="ssid_title">Ziel-WLAN-Netzwerke</string>
Expand Down Expand Up @@ -65,4 +65,8 @@
<string name="button_reload">Neu laden</string>
<string name="button_clear">Löschen</string>
<string name="tab_licenses_title">Lizenzen</string>
<string name="battery_topic_title">Batterie-Topic</string>
<string name="battery_topic_summary">Das Topic, zu dem Batterienachrichten gesendet werden.%nAktueller Wert: %s</string>
<string name="battery_message_title">Batteriestand senden</string>
<string name="battery_message_summary">Batteriestand in Prozent zusätzlich zu Präsenznachricht senden.</string>
</resources>
8 changes: 6 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
<string name="login_summary">User Login.%nCurrent value: %s</string>
<string name="password_title">MQTT Password</string>
<string name="password_summary">User Password.</string>
<string name="topic_title">Topic</string>
<string name="topic_summary">The topic to publish messages to.%nCurrent value: %s</string>
<string name="presence_topic_title">Presence Topic</string>
<string name="presence_topic_summary">The topic to publish presence messages to.%nCurrent value: %s</string>
<string name="battery_message_title">Send battery level</string>
<string name="battery_message_summary">Send battery level percentage in addition to presence information.</string>
<string name="battery_topic_title">Battery Topic</string>
<string name="battery_topic_summary">The topic to publish battery level to.%nCurrent value: %s</string>
<string name="ping_title">Publish period</string>
<string name="ping_summary">The time between messages in minutes.</string>
<string name="ssid_title">Target WiFi networks</string>
Expand Down

0 comments on commit 0a39a3d

Please sign in to comment.