Skip to content

Commit

Permalink
OB1: further fixes and workarounds for Android 11
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Nov 26, 2020
1 parent ab3f3c4 commit cf5500f
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 42 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,9 @@
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:label="BlueJay Admin"/>
<service android:name=".watch.thinjam.BlueJayService" />
<service android:name=".watch.thinjam.BlueJayService"
android:foregroundServiceType="connectedDevice|location"
/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public static void store(final String transmitterId, final long when) {
UserError.Log.d(TAG, "Sync time updated to: " + JoH.dateTimeText(when));
}

public static void clear(final String transmitterId) {
if (PersistentStore.getLong(DEX_SYNC_STORE + transmitterId) > 0) {
UserError.Log.e(TAG, "Clearing stored timing sync information for: " + transmitterId);
PersistentStore.setLong(DEX_SYNC_STORE + transmitterId, 0);
}
}

// anticpiate next wake up from now
public static long anticipate(final String transmitterId) {
return anticipate(transmitterId, JoH.tsl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.os.Build;
import android.os.PowerManager;

import com.eveningoutpost.dexdrip.BestGlucose;
import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.ImportedLibraries.usbserial.util.HexDump;
import com.eveningoutpost.dexdrip.Models.BgReading;
Expand All @@ -20,6 +19,7 @@
import com.eveningoutpost.dexdrip.Services.Ob1G5CollectionService;
import com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder;
import com.eveningoutpost.dexdrip.UtilityModels.BroadcastGlucose;
import com.eveningoutpost.dexdrip.UtilityModels.CollectionServiceStarter;
import com.eveningoutpost.dexdrip.UtilityModels.Constants;
import com.eveningoutpost.dexdrip.UtilityModels.Inevitable;
import com.eveningoutpost.dexdrip.UtilityModels.NotificationChannels;
Expand All @@ -33,11 +33,11 @@
import com.eveningoutpost.dexdrip.wearintegration.WatchUpdaterService;
import com.eveningoutpost.dexdrip.xdrip;
import com.google.gson.reflect.TypeToken;
/*import com.polidea.rxandroidble.RxBleConnection;
import com.polidea.rxandroidble.exceptions.BleCannotSetCharacteristicNotificationException;
import com.polidea.rxandroidble.exceptions.BleDisconnectedException;
import com.polidea.rxandroidble.exceptions.BleGattCharacteristicException;
*/
import com.polidea.rxandroidble2.RxBleConnection;
import com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException;
import com.polidea.rxandroidble2.exceptions.BleDisconnectedException;
import com.polidea.rxandroidble2.exceptions.BleGattCharacteristicException;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.security.InvalidKeyException;
Expand All @@ -56,7 +56,7 @@
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

//import rx.schedulers.Schedulers;
import io.reactivex.schedulers.Schedulers;

import static com.eveningoutpost.dexdrip.G5Model.BluetoothServices.Authentication;
import static com.eveningoutpost.dexdrip.G5Model.BluetoothServices.Control;
Expand All @@ -80,13 +80,12 @@
import static com.eveningoutpost.dexdrip.UtilityModels.Constants.SECOND_IN_MS;
import static com.eveningoutpost.dexdrip.utils.bt.Helper.getStatusName;


import com.polidea.rxandroidble2.RxBleConnection;
import com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException;
import com.polidea.rxandroidble2.exceptions.BleDisconnectedException;
import com.polidea.rxandroidble2.exceptions.BleGattCharacteristicException;

import io.reactivex.schedulers.Schedulers;
/*import com.polidea.rxandroidble.RxBleConnection;
import com.polidea.rxandroidble.exceptions.BleCannotSetCharacteristicNotificationException;
import com.polidea.rxandroidble.exceptions.BleDisconnectedException;
import com.polidea.rxandroidble.exceptions.BleGattCharacteristicException;
*/
//import rx.schedulers.Schedulers;


/**
Expand Down Expand Up @@ -266,15 +265,23 @@ private static void authenticationProcessor(final Ob1G5CollectionService parent,
if (d)
UserError.Log.d(TAG, ("Authenticated: " + status.isAuthenticated() + " " + status.isBonded()));
if (status.isAuthenticated()) {
if (status.isBonded()) {
parent.msg("Authenticated");
parent.authResult(true);
parent.changeState(Ob1G5CollectionService.STATE.GET_DATA);
throw new OperationSuccess("Authenticated");

if (parent.unBondAndStop) {
UserError.Log.d(TAG,"Processing unbond and stop");
parent.changeState(Ob1G5CollectionService.STATE.UNBOND);

} else {
//parent.unBond(); // bond must be invalid or not existing // WARN
parent.changeState(Ob1G5CollectionService.STATE.PREBOND);
// TODO what to do here?

if (status.isBonded()) {
parent.msg("Authenticated");
parent.authResult(true);
parent.changeState(Ob1G5CollectionService.STATE.GET_DATA);
throw new OperationSuccess("Authenticated");
} else {
//parent.unBond(); // bond must be invalid or not existing // WARN
parent.changeState(Ob1G5CollectionService.STATE.PREBOND);
// TODO what to do here?
}
}
} else {
parent.msg("Not Authorized! (Wrong TxID?)");
Expand Down Expand Up @@ -402,6 +409,26 @@ public synchronized static boolean doKeepAliveAndBondRequest(Ob1G5CollectionServ
return true;
}

private static void localUnbondAndStop(final Ob1G5CollectionService parent, RxBleConnection connection) {
Inevitable.task("local unbond", 3000, () -> {
parent.unBond(); // remove local bond
Inevitable.task("shutdown collector",2000, () -> {
UserError.Log.d(TAG,"Shutting down collector");
DexCollectionType.setDexCollectionType(DexCollectionType.Disabled);
CollectionServiceStarter.restartCollectionServiceBackground();
parent.unBondAndStop = false;
});
});
}

@SuppressLint("CheckResult")
public static boolean doUnBond(final Ob1G5CollectionService parent, RxBleConnection connection) {
if (connection == null) return false;
parent.msg("Unbond Transmitter a:" + usingAlt());
localUnbondAndStop(parent, connection);
return true;
}

@SuppressLint("CheckResult")
public static boolean doReset(Ob1G5CollectionService parent, RxBleConnection connection) {
if (connection == null) return false;
Expand Down Expand Up @@ -752,7 +779,7 @@ private static void disconnectNow(Ob1G5CollectionService parent, RxBleConnection
UserError.Log.d(TAG, "Failed to write DisconnectTxMessage as already disconnected: " + throwable);

} else {
UserError.Log.e(TAG, "Failed to write DisconnectTxMessage: " + throwable);
UserError.Log.d(TAG, "Failed to write DisconnectTxMessage: " + throwable);

}
parent.changeState(Ob1G5CollectionService.STATE.CLOSE);
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/com/eveningoutpost/dexdrip/MegaStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
Expand Down Expand Up @@ -64,6 +65,7 @@
import com.google.android.gms.wearable.DataMap;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static com.eveningoutpost.dexdrip.Home.startWatchUpdaterService;
Expand All @@ -90,6 +92,7 @@ public class MegaStatus extends ActivityWithMenu {

private static final ArrayList<String> sectionList = new ArrayList<>();
private static final ArrayList<String> sectionTitles = new ArrayList<>();
private static final HashSet<String> sectionAlwaysOn = new HashSet<>();

public static View runnableView;

Expand Down Expand Up @@ -122,6 +125,10 @@ private void addAsection(String section, String title) {
private static final String SHARE_FOLLOW = "Dex Share Follow";
private static final String XDRIP_LIBRE2 = "Libre2";

static {
sectionAlwaysOn.add(G5_STATUS);
}

public static PendingIntent getStatusPendingIntent(String section_name) {
final Intent intent = new Intent(xdrip.getAppContext(), MegaStatus.class);
intent.setAction(section_name);
Expand Down Expand Up @@ -287,6 +294,7 @@ protected void onCreate(Bundle savedInstanceState) {
currentPage = saved_position;
mViewPager.setCurrentItem(saved_position);
autoStart = true; // run once activity becomes visible
keepScreenOn(sectionAlwaysOn.contains(sectionList.get(currentPage)));
}
mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
Expand All @@ -295,6 +303,7 @@ public void onPageSelected(int position) {
runnableView = null;
currentPage = position;
startAutoFresh();
keepScreenOn(sectionAlwaysOn.contains(sectionList.get(currentPage)));
PersistentStore.setLong("mega-status-last-page", currentPage);
}
});
Expand Down Expand Up @@ -436,6 +445,18 @@ public void run() {
, 1500);
}

private void keepScreenOn(boolean on) {
try {
if (on) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
} catch (Exception e) {
UserError.Log.d(TAG, "Exception setting window flags: " + e);
}
}

private synchronized void startAutoFresh() {
if (autoFreshRunning) return;
autoStart = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ public class Ob1G5CollectionService extends G5BaseService {
private PowerManager.WakeLock fullWakeLock;

private volatile boolean background_launch_waiting = false;
private static long last_scan_started = -1;
private static volatile long last_scan_started = -1;
private static volatile long last_connect_started = -1;
private static volatile long last_mega_status_read = -1;
private static int error_count = 0;
private static int connectNowFailures = 0;
private static int connectFailures = 0;
Expand All @@ -222,7 +223,7 @@ public class Ob1G5CollectionService extends G5BaseService {
private static volatile boolean minimize_scanning = false; // set by preference
private static volatile boolean always_scan = false;
private static volatile boolean scan_next_run = true;
private static boolean always_discover = false;
private static boolean always_discover = true;
private static boolean always_connect = false;
private static boolean do_discovery = true;
private static final boolean do_auth = true;
Expand Down Expand Up @@ -383,6 +384,7 @@ private synchronized void automata() {
case GET_DATA:
if (hardResetTransmitterNow) {
send_reset_command();
DexSyncKeeper.clear(transmitterID);
} else {
final PowerManager.WakeLock linger_wl_get_data = JoH.getWakeLock("jam-g5-get-linger", 6000);
if (!Ob1G5StateMachine.doGetData(this, connection)) resetState();
Expand Down Expand Up @@ -413,10 +415,11 @@ private boolean useMinimizeScanningStrategy() {
estimateAnticipateFromLinkedData();
alwaysMinimize = !preScanFailureMarker;
}
if (!alwaysMinimize) {
alwaysMinimize = Pref.getBooleanDefaultFalse("ob1_avoid_scanning");
}
return minimize_scanning && transmitterMAC != null && (!lastConnectFailed || (modulo == 1) || alwaysMinimize) && (DexSyncKeeper.isReady(transmitterID));
//if (!alwaysMinimize) {
// alwaysMinimize = Pref.getBooleanDefaultFalse("ob1_avoid_scanning"); // TODO this maybe should be removed now
// }
return minimize_scanning && transmitterMAC != null && (!lastConnectFailed || (modulo == 1) || alwaysMinimize)
&& (DexSyncKeeper.isReady(transmitterID));
}

private void estimateAnticipateFromLinkedData() {
Expand Down Expand Up @@ -489,7 +492,8 @@ private synchronized void scan_for_device() {
historicalTransmitterMAC = PersistentStore.getString(OB1G5_MACSTORE + transmitterID); // "" if unset

ScanFilter filter;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && historicalTransmitterMAC.length() > 5) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && historicalTransmitterMAC.length() > 5
&& Build.VERSION.SDK_INT < 29) {
filter = new ScanFilter.Builder()
.setDeviceAddress(historicalTransmitterMAC)
.build();
Expand Down Expand Up @@ -929,7 +933,11 @@ public void onCreate() {
final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
try {
registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
if (Build.VERSION.SDK_INT < 29) {
registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
} else {
UserError.Log.d(TAG, "Not registering pairing receiver on Android 10+");
}
} catch (Exception e) {
UserError.Log.e(TAG, "Could not register pairing request receiver:" + e);
}
Expand Down Expand Up @@ -1042,7 +1050,7 @@ public void unregisterPairingReceiver() {
try {
unregisterReceiver(mPairingRequestRecevier);
} catch (Exception e) {
UserError.Log.e(TAG, "Got exception unregistering pairing receiver: " + e);
UserError.Log.d(TAG, "Got exception unregistering pairing receiver: " + e);
}
}

Expand Down Expand Up @@ -1512,6 +1520,11 @@ public void onReceive(Context context, Intent intent) {
weInitiatedBondConfirmation = 2;
changeState(GET_DATA);
}
} else if (parcel_device.getBondState() == BluetoothDevice.BOND_BONDING) {
if (Build.VERSION.SDK_INT >= 29) {
JoH.playResourceAudio(R.raw.bt_meter_connect);
UserError.Log.uel(TAG, "Prompting user to notice pairing request with sound - On Android 10+ you have to manually pair when requested");
}
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -1838,8 +1851,14 @@ public static List<StatusItem> megaStatus() {

init_tx_id(); // needed if we have not passed through local INIT state

last_mega_status_read = JoH.tsl();

final List<StatusItem> l = new ArrayList<>();

if (!DexSyncKeeper.isReady(transmitterID)) {
l.add(new StatusItem("Hunting Transmitter", "Stay on this page", CRITICAL));
}

l.add(new StatusItem("Phone Service State", lastState + (BlueJayEntry.isPhoneCollectorDisabled() ? "\nDisabled by BlueJay option" : ""), JoH.msSince(lastStateUpdated) < 300000 ? (lastState.startsWith("Got data") ? Highlight.GOOD : NORMAL) : (isWatchRunning() ? Highlight.GOOD : CRITICAL)));
if (last_scan_started > 0) {
final long scanning_time = JoH.msSince(last_scan_started);
Expand Down Expand Up @@ -1952,7 +1971,7 @@ public static List<StatusItem> megaStatus() {
}
l.add(new StatusItem("Other Version", vr.other_firmware_version));
// l.add(new StatusItem("Hardware Version", vr.hardwarev));
if (vr.asic != 61440 && vr.asic != 16705 && vr.asic != 243 && vr.asic != 74)
if (vr.asic != 61440 && vr.asic != 16705 && vr.asic != 243 && vr.asic != 74 && vr.asic != 226)
l.add(new StatusItem("ASIC", vr.asic, NOTICE));
}
}
Expand Down Expand Up @@ -2000,8 +2019,8 @@ public void run() {
l.add(new StatusItem("Transmitter Days", ((bt.runtime > -1) ? bt.runtime : "") + ((timekeeperDays > -1) ? ((FirmwareCapability.isTransmitterG6Rev2(tx_id) ? " " : " / ") + timekeeperDays) : "") + ((last_transmitter_timestamp > 0) ? " / " + JoH.qs((double) last_transmitter_timestamp / 86400, 1) : "")));
l.add(new StatusItem("Voltage A", bt.voltagea, bt.voltagea < LOW_BATTERY_WARNING_LEVEL ? BAD : NORMAL));
l.add(new StatusItem("Voltage B", bt.voltageb, bt.voltageb < (LOW_BATTERY_WARNING_LEVEL - 10) ? BAD : NORMAL));
l.add(new StatusItem("Resistance", bt.resist, bt.resist > 1400 ? BAD : (bt.resist > 1000 ? NOTICE : (bt.resist > 750 ? NORMAL : Highlight.GOOD))));
if (vr != null && FirmwareCapability.isFirmwareTemperatureCapable(vr.firmware_version_string)) {
l.add(new StatusItem("Resistance", bt.resist, bt.resist > 1400 ? BAD : (bt.resist > 1000 ? NOTICE : (bt.resist > 750 ? NORMAL : Highlight.GOOD))));
l.add(new StatusItem("Temperature", bt.temperature + " \u2103"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.eveningoutpost.dexdrip.BuildConfig;
import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.UserError;
import com.eveningoutpost.dexdrip.R;
import com.eveningoutpost.dexdrip.Services.G5BaseService;
import com.eveningoutpost.dexdrip.utils.DexCollectionType;
Expand Down Expand Up @@ -125,13 +126,26 @@ public static void notifyAboutCompatibleApps() {
}

checkMemoryConstraints();

enableAndroid10Workarounds();
// TODO add pebble

// TODO add more here

}

private static final String ANDROID_10_WORKAROUND_MARKER = "ANDROID_10_WORKAROUND_MARKER";

private static void enableAndroid10Workarounds() {
if (Build.VERSION.SDK_INT >= 29) {
if (!PersistentStore.getBoolean(ANDROID_10_WORKAROUND_MARKER, false)) {
UserError.Log.ueh(CompatibleApps.class.getSimpleName(),"Enabling default workarounds for Android 10+ setting minimize scanning to enabled");
Pref.setBoolean("ob1_minimize_scanning", true);
// Pref.setBoolean("ob1_avoid_scanning", true);
PersistentStore.setBoolean(ANDROID_10_WORKAROUND_MARKER,true);
}
}
}


private static void checkMemoryConstraints() {
final ActivityManager actManager = (ActivityManager) xdrip.getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
Expand Down
Loading

0 comments on commit cf5500f

Please sign in to comment.