Skip to content

Commit

Permalink
Add customizable heartbeat alerts & battery %
Browse files Browse the repository at this point in the history
- add preference to settings allowing custom or standard notifications
- mod first message to include explanatory frequency text
- concatenate strings for translation and downstream use
  • Loading branch information
lukeswitz committed Feb 8, 2019
1 parent 272b341 commit a45e395
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 19 deletions.
21 changes: 21 additions & 0 deletions src/main/java/org/havenapp/main/PreferenceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class PreferenceManager {
public static final String CONFIG_MOVEMENT ="config_movement";
public static final String HEARTBEAT_MONITOR_ACTIVE="heartbeat_monitor_active";
public static final String HEARTBEAT_MONITOR_DELAY="heartbeat_monitor_delay";
public static final String HEARTBEAT_MONITOR_MESSAGE="heartbeat_monitor_message";
public static final String MONITOR_SERVICE_ACTIVE="monitor_service_active";
private static final String FLASH_ACTIVE="flash_active";
private static final String MICROPHONE_ACTIVE="microphone_active";
Expand Down Expand Up @@ -351,6 +352,26 @@ public int getHeartbeatNotificationTimeMs () {
return appSharedPrefs.getInt(HEARTBEAT_MONITOR_DELAY,300000);
}

public String getHeartbeatMonitorMessage ()
{
return appSharedPrefs.getString(HEARTBEAT_MONITOR_MESSAGE,null);
}

public void setHeartbeatMonitorMessage (String hearbeatMessage)
{
prefsEditor.putString(HEARTBEAT_MONITOR_MESSAGE, hearbeatMessage);
prefsEditor.commit();
}

public String getHearbeatPrefix() {
return context.getString(R.string.hearbeat_monitor_initial_message_1);
}

public String getHeartbeatSuffix() {
return context.getString(R.string.hearbeat_monitor_initial_message_2);
}


/**
* Set the {@link org.havenapp.main.model.Event#startTime} for the ongoing event.
* Sets a string with the format {@link Utils#DATE_TIME_PATTERN}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/havenapp/main/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.havenapp.main;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
Expand Down Expand Up @@ -46,4 +51,23 @@ static String getTimerText(long milliseconds) {
public static String getDateTime(Date date) {
return new SimpleDateFormat(DATE_TIME_PATTERN, Locale.getDefault()).format(date);
}

/**
* Get the battery level from the device, from official docs:
* https://developer.android.com/training/monitoring-device-state/battery-monitoring#MonitorLevel
* @param context
* @return an integer corresponding to the battery percentage without any symbols
*/
public static int getBatteryPercentage(Context context) {

IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, iFilter);

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

float batteryPct = level / (float) scale;

return (int) (batteryPct * 100);
}
}
77 changes: 58 additions & 19 deletions src/main/java/org/havenapp/main/service/SignalSender.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package org.havenapp.main.service;

import android.content.Context;

import android.os.CountDownTimer;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.util.Log;

import net.sourceforge.argparse4j.inf.Namespace;

import org.asamk.signal.Main;
import org.havenapp.main.PreferenceManager;
import org.havenapp.main.R;
import org.havenapp.main.Utils;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -25,11 +27,23 @@ public class SignalSender {
private static SignalSender mInstance;
private String mUsername; //aka your signal phone number
private CountDownTimer mCountdownTimer;
private PreferenceManager preferences;
private String messageString;
private String prefix;
private String suffix;
private int interval;
private int mAlertCount;

private SignalSender(Context context, String username)
{
mContext = context;
mUsername = username;
mAlertCount = 0;
preferences = new PreferenceManager(mContext);
prefix = preferences.getHearbeatPrefix();
suffix = preferences.getHeartbeatSuffix();
messageString = preferences.getHeartbeatMonitorMessage();
interval = preferences.getHeartbeatNotificationTimeMs() / 60000;
}

public static synchronized SignalSender getInstance (Context context, String username)
Expand Down Expand Up @@ -93,50 +107,75 @@ public void run() {

public void stopHeartbeatTimer ()
{
mCountdownTimer.cancel();
mCountdownTimer = null;
Log.d("HEARTBEAT TIMER", "Stopped" );
mAlertCount = 0;

if (mCountdownTimer != null) {
mCountdownTimer.cancel();
mCountdownTimer = null;
Log.d("HEARTBEAT MONITOR", "Stopped" );
} else
Log.d("HEARTBEAT MONITOR", "null");

}

public void startHeartbeatTimer (int countMs)
{
if (countMs <= 10000) //Default if '0' setting
if (countMs <= 10000)
countMs = 300000;

mCountdownTimer = new CountDownTimer(countMs,1000) {

public void onTick(long millisUntilFinished) {
Log.d("HEARTBEAT TIMER"," seconds remaining: " + millisUntilFinished / 1000);
// Log.d("HEARTBEAT MONITOR," seconds remaining: " + millisUntilFinished / 1000);
}

public void onFinish() {
Log.d("HEARTBEAT TIMER"," Done, update message sent!");
beatingHeart();
try {
beatingHeart();
} catch(Throwable e) {
e.printStackTrace();
}
start();
}
}.start();
}

private void beatingHeart ()
{
PreferenceManager preferences = new PreferenceManager(mContext);
private void beatingHeart () {
int unicodeBeat = 0x1F493;
String emojiString = new String(Character.toChars(unicodeBeat));
messageString = preferences.getHeartbeatMonitorMessage();

/**
* Use compiler for optimized concatenation.
* Send an explanatory message first, then the unicode symbol.
* Ensure above message sent before updating count.
* Check for a custom message, send that instead.
**/

if (mAlertCount < 1 )
messageString = prefix + " " + interval + " " + suffix + "\n" + mContext.getString(R.string.battery_level_msg_text) + ": " + Utils.getBatteryPercentage(mContext) + "%";
else if (messageString != null)
messageString = messageString + "\n" + mContext.getString(R.string.battery_level_msg_text) + ": " + Utils.getBatteryPercentage(mContext) + "%";
else
messageString = emojiString + "\n" + mContext.getString(R.string.battery_level_msg_text) + ": " + Utils.getBatteryPercentage(mContext) + "%";

initHbMessage(messageString);
}

private void initHbMessage (String message)
{
if (!TextUtils.isEmpty(mUsername)) {
getInstance(mContext, mUsername.trim());
ArrayList<String> recipient = new ArrayList<>();
recipient.add(preferences.getSmsNumber());
sendMessage(recipient, emojiString,null);
}
else if (!TextUtils.isEmpty(preferences.getSmsNumber())) {

sendMessage(recipient, message,null);
} else if (!TextUtils.isEmpty(preferences.getSmsNumber())) {
SmsManager manager = SmsManager.getDefault();

StringTokenizer st = new StringTokenizer(preferences.getSmsNumber(),",");
while (st.hasMoreTokens())
manager.sendTextMessage(st.nextToken(), null, emojiString, null, null);
manager.sendTextMessage(st.nextToken(), null, message, null, null);
}

mAlertCount ++; //moved outside of the send functions for now
Log.d("HEARTBEAT MONITOR", "Sent: " + message);
}

public void sendMessage (final ArrayList<String> recipients, final String message, final String attachment)
Expand Down
8 changes: 8 additions & 0 deletions src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@
android:summary="@string/hearbeat_monitor_dialog"
android:title="@string/hearbeat_monitor_summary"/>

<EditTextPreference
android:dialogLayout="@layout/pref_dialog_edit_text"
android:dialogMessage="@string/hearbeat_message_dialog"
android:inputType="text"
android:key="heartbeat_monitor_message"
android:title="@string/hearbeat_message_title"
android:summary="@string/hearbeat_message_summary" />

</PreferenceCategory>
<PreferenceCategory android:title="@string/remote_access">

Expand Down

0 comments on commit a45e395

Please sign in to comment.