WIP: Work towards SMS replies / canned replies, round 3

- put random id/phone number pair into limited lookup list (last 16 sms messages) when sms arrives
- lookup the phone number when replying from the a device

THIS STILL DOES NOT DO ANYTHING USEFUL
This commit is contained in:
Andreas Shimokawa 2015-12-14 23:31:31 +01:00
parent 14f8929439
commit de5f30ae97
6 changed files with 49 additions and 5 deletions

View File

@ -1,7 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge; package nodomain.freeyourgadget.gadgetbridge;
import android.app.Application; import android.app.Application;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -26,11 +25,11 @@ import java.util.concurrent.locks.ReentrantLock;
import nodomain.freeyourgadget.gadgetbridge.database.ActivityDatabaseHandler; import nodomain.freeyourgadget.gadgetbridge.database.ActivityDatabaseHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBConstants; import nodomain.freeyourgadget.gadgetbridge.database.DBConstants;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.IDSenderLookup;
/** /**
* Main Application class that initializes and provides access to certain things like * Main Application class that initializes and provides access to certain things like
@ -44,6 +43,7 @@ public class GBApplication extends Application {
private static final Lock dbLock = new ReentrantLock(); private static final Lock dbLock = new ReentrantLock();
private static DeviceService deviceService; private static DeviceService deviceService;
private static SharedPreferences sharedPrefs; private static SharedPreferences sharedPrefs;
private static IDSenderLookup mIDSenderLookup = new IDSenderLookup();
public static final String ACTION_QUIT public static final String ACTION_QUIT
= "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.quit"; = "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.quit";
@ -189,6 +189,7 @@ public class GBApplication extends Application {
public static boolean isRunningOnKitkatOrLater() { public static boolean isRunningOnKitkatOrLater() {
return VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; return VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
} }
public static boolean isRunningLollipopOrLater() { public static boolean isRunningLollipopOrLater() {
return VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; return VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
} }
@ -226,6 +227,7 @@ public class GBApplication extends Application {
/** /**
* Deletes the entire Activity database and recreates it with empty tables. * Deletes the entire Activity database and recreates it with empty tables.
*
* @return true on successful deletion * @return true on successful deletion
*/ */
public static synchronized boolean deleteActivityDatabase() { public static synchronized boolean deleteActivityDatabase() {
@ -237,4 +239,8 @@ public class GBApplication extends Application {
mActivityDatabaseHandler = new ActivityDatabaseHandler(getContext()); mActivityDatabaseHandler = new ActivityDatabaseHandler(getContext());
return result; return result;
} }
public static IDSenderLookup getIDSenderLookup() {
return mIDSenderLookup;
}
} }

View File

@ -76,7 +76,7 @@ public class DebugActivity extends Activity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
NotificationSpec notificationSpec = new NotificationSpec(); NotificationSpec notificationSpec = new NotificationSpec();
notificationSpec.phoneNumber = getResources().getText(R.string.app_name).toString(); notificationSpec.phoneNumber = editContent.getText().toString();
notificationSpec.body = editContent.getText().toString(); notificationSpec.body = editContent.getText().toString();
notificationSpec.type = NotificationType.SMS; notificationSpec.type = NotificationType.SMS;
notificationSpec.id = -1; notificationSpec.id = -1;

View File

@ -20,6 +20,7 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AppManagerActivity; import nodomain.freeyourgadget.gadgetbridge.activities.AppManagerActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost; import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost;
@ -227,7 +228,10 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
action = NotificationListener.ACTION_MUTE; action = NotificationListener.ACTION_MUTE;
break; break;
case REPLY: case REPLY:
GB.toast(context, "got notfication reply: " + deviceEvent.reply, 2, GB.INFO); String phoneNumber = GBApplication.getIDSenderLookup().lookup(deviceEvent.handle);
if (phoneNumber != null) {
GB.toast(context, "got notfication reply for " + phoneNumber + " : " + deviceEvent.reply, 2, GB.INFO);
}
break; break;
} }
if (action != null) { if (action != null) {

View File

@ -21,8 +21,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver;
@ -37,6 +39,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand; import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.IDSenderLookup;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CALLSTATE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CALLSTATE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CONNECT; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CONNECT;
@ -94,6 +97,8 @@ public class DeviceCommunicationService extends Service {
private MusicPlaybackReceiver mMusicPlaybackReceiver = null; private MusicPlaybackReceiver mMusicPlaybackReceiver = null;
private TimeChangeReceiver mTimeChangeReceiver = null; private TimeChangeReceiver mTimeChangeReceiver = null;
private Random mRandom = new Random();
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
@ -218,6 +223,9 @@ public class DeviceCommunicationService extends Service {
if (notificationSpec.type == NotificationType.SMS && notificationSpec.phoneNumber != null) { if (notificationSpec.type == NotificationType.SMS && notificationSpec.phoneNumber != null) {
notificationSpec.sender = getContactDisplayNameByNumber(notificationSpec.phoneNumber); notificationSpec.sender = getContactDisplayNameByNumber(notificationSpec.phoneNumber);
notificationSpec.id = mRandom.nextInt(); // FIXME: add this in external SMS Receiver?
GBApplication.getIDSenderLookup().add(notificationSpec.id, notificationSpec.phoneNumber);
// NOTE: maybe not where it belongs // NOTE: maybe not where it belongs
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
if (sharedPrefs.getBoolean("pebble_force_untested", false)) { if (sharedPrefs.getBoolean("pebble_force_untested", false)) {

View File

@ -398,7 +398,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
@Override @Override
public byte[] encodeNotification(NotificationSpec notificationSpec) { public byte[] encodeNotification(NotificationSpec notificationSpec) {
boolean hasHandle = notificationSpec.id != -1; boolean hasHandle = notificationSpec.id != -1 && notificationSpec.phoneNumber == null;
int id = notificationSpec.id != -1 ? notificationSpec.id : mRandom.nextInt(); int id = notificationSpec.id != -1 ? notificationSpec.id : mRandom.nextInt();
String title; String title;
String subtitle = null; String subtitle = null;

View File

@ -0,0 +1,26 @@
package nodomain.freeyourgadget.gadgetbridge.util;
import android.util.Pair;
import java.util.LinkedList;
public class IDSenderLookup {
private static final int LIMIT = 16;
private LinkedList<Pair> list = new LinkedList<>();
public void add(int id, String sender) {
if (list.size() > LIMIT - 1) {
list.removeFirst();
}
list.add(new Pair<>(id, sender));
}
public String lookup(int id) {
for (Pair entry : list) {
if (id == (Integer) entry.first) {
return (String) entry.second;
}
}
return null;
}
}