Skip to content

Commit

Permalink
Merge branch 'master' into add-custom-notification
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyShepelev authored Sep 20, 2024
2 parents db6f9bb + aee9315 commit 4aea550
Show file tree
Hide file tree
Showing 18 changed files with 271 additions and 28 deletions.
4 changes: 3 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,6 @@

-keep class com.newrelic.** { *; }
-dontwarn com.newrelic.**
-keepattributes Exceptions, Signature, InnerClasses, LineNumberTable
-keepattributes Exceptions, Signature, InnerClasses, LineNumberTable

-keep class com.eveningoutpost.dexdrip.cgm.carelinkfollow.message.*
19 changes: 15 additions & 4 deletions app/src/main/java/com/eveningoutpost/dexdrip/MegaStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.eveningoutpost.dexdrip.services.G5CollectionService;
import com.eveningoutpost.dexdrip.services.Ob1G5CollectionService;
import com.eveningoutpost.dexdrip.services.WifiCollectionService;
import com.eveningoutpost.dexdrip.ui.helpers.FloatingLocaleActivityWithScreenshot;
import com.eveningoutpost.dexdrip.utilitymodels.JamorhamShowcaseDrawer;
import com.eveningoutpost.dexdrip.utilitymodels.PersistentStore;
import com.eveningoutpost.dexdrip.utilitymodels.Pref;
Expand All @@ -64,7 +65,6 @@
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.CareLinkFollowService;
import com.eveningoutpost.dexdrip.insulin.inpen.InPenEntry;
import com.eveningoutpost.dexdrip.insulin.inpen.InPenService;
import com.eveningoutpost.dexdrip.utils.ActivityWithMenu;
import com.eveningoutpost.dexdrip.utils.DexCollectionType;
import com.eveningoutpost.dexdrip.watch.lefun.LeFunEntry;
import com.eveningoutpost.dexdrip.watch.lefun.LeFunService;
Expand All @@ -81,7 +81,9 @@
import java.util.HashSet;
import java.util.List;

public class MegaStatus extends ActivityWithMenu {
import lombok.val;

public class MegaStatus extends FloatingLocaleActivityWithScreenshot {


private static Activity mActivity;
Expand Down Expand Up @@ -451,7 +453,10 @@ protected void onResume() {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_mega_status, menu);
if (FloatingLocaleActivityWithScreenshot.localeString == null) {
// only if we are not forced to a locale
getMenuInflater().inflate(R.menu.menu_mega_status, menu);
}
return true;
}

Expand All @@ -463,7 +468,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
if (id == R.id.action_screenshot) {
val lang = "en"; // force in this language
val intent = JoH.getStartActivityIntent(MegaStatus.class);
intent.putExtra(FORCE_ACTIVITY_LANGUAGE,lang);
intent.putExtra(SCREENSHOT_AND_EXIT, true);
FloatingLocaleActivityWithScreenshot.localeString = lang;
xdrip.getAppContext().startActivity(intent);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class Pusher {
private static final PowerManager.WakeLock wakeLock = getWakeLock("pusher-wake", 1000);

private static volatile boolean running;
private static volatile boolean reconnect;

private static volatile Pusher instance;

Expand All @@ -88,6 +89,7 @@ public static synchronized Pusher immortality() {
running = true;
final Pusher client = new Pusher();
instance = client;
reconnect = false; // already new
final Thread t = new Thread(() -> {
client.start();
running = false;
Expand All @@ -113,6 +115,11 @@ public static synchronized Pusher getInstance() {
}
}

public static synchronized void requestReconnect() {
reconnect = true;
log("Reconnect requested");
}

public static boolean enabled() {
return Pref.getBoolean("use_xdrip_cloud_sync", false);
}
Expand Down Expand Up @@ -273,6 +280,11 @@ private void listenForMessages() throws IOException {
} else {
reconnectTimer = Math.max(0, reconnectTimer - 1000); // For each success reduce the reconnect timer by 1 second
receivedStatistics.onMessage();
if (reconnect) {
reconnect = false;
log("Initiating reconnection");
closeConnection();
}
}
}
} catch (SocketTimeoutException te) {
Expand Down
24 changes: 18 additions & 6 deletions app/src/main/java/com/eveningoutpost/dexdrip/nfc/NFControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ private static int getReaderFlags() { // if 0 then nothing enabled
}

if (GlucoMen.isEnabled()) {
flags |= NfcAdapter.FLAG_READER_NFC_V
| NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK
| NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS;
flags |= NfcAdapter.FLAG_READER_NFC_V
| NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK
| NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS;
}

return flags;
Expand All @@ -58,7 +58,11 @@ private static Bundle getOptionsBundle() {
}
}

public static void initNFC(final Activity context, final boolean disable) {
public static void initNFCbackground(final Activity context, final boolean disable) {
new Thread(() -> NFControl.initNFC(context, disable)).start();
}

public static synchronized void initNFC(final Activity context, final boolean disable) {
UserError.Log.d(TAG, "InitNFC start");
val mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
val flags = disable ? 0 : getReaderFlags();
Expand Down Expand Up @@ -93,11 +97,19 @@ public static void initNFC(final Activity context, final boolean disable) {
}

UserError.Log.d(TAG, "Enabling reader mode with flags: " + flags);
mNfcAdapter.enableReaderMode(context, new TagMultiplexer(context), flags, getOptionsBundle());
try {
mNfcAdapter.enableReaderMode(context, new TagMultiplexer(context), flags, getOptionsBundle());
} catch (Exception e) {
UserError.Log.e(TAG, "Got exception enabling reader mode: " + e);
}
} else {
if (mNfcAdapter != null) {
UserError.Log.d(TAG, "Disabling reader mode");
mNfcAdapter.disableReaderMode(context);
try {
mNfcAdapter.disableReaderMode(context);
} catch (Exception e) {
UserError.Log.e(TAG, "Got exception disabling reader mode: " + e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,10 @@ public static List<StatusItem> megaStatus() {
val due = Pusher.getInstance().getNextReconnectionDue();
l.add(new StatusItem("Next reconnection", due != -1 ? niceTimeScalar(Math.max(msTill(due), 0)) : "Unknown"));
}
l.add(new StatusItem("Sent hour / total", Pusher.sentLastHour() + " (" + Pusher.sentTotal() + ")"));
l.add(new StatusItem("Recv hour / total", Pusher.receivedLastHour() + " (" + Pusher.receivedTotal() + ")"));
if (Home.get_engineering_mode()) {
l.add(new StatusItem("Sent hour / total", Pusher.sentLastHour() + " (" + Pusher.sentTotal() + ")"));
l.add(new StatusItem("Recv hour / total", Pusher.receivedLastHour() + " (" + Pusher.receivedTotal() + ")"));
}


} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.eveningoutpost.dexdrip.GcmActivity;
import com.eveningoutpost.dexdrip.GoogleDriveInterface;
import com.eveningoutpost.dexdrip.cloud.jamcm.Pusher;
import com.eveningoutpost.dexdrip.utilitymodels.UpdateActivity;
import com.eveningoutpost.dexdrip.xdrip;

Expand All @@ -35,6 +36,7 @@ public static void clearandRestartSyncService(Context context) {
GoogleDriveInterface.invalidate();
GcmActivity.token = null; // invalidate
speedup();
Pusher.requestReconnect();
startSyncService(context, "clearAndRestart");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.eveningoutpost.dexdrip.ui.helpers;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.utilitymodels.Inevitable;
import com.eveningoutpost.dexdrip.utils.ActivityWithMenu;
import com.eveningoutpost.dexdrip.xdrip;

import java.io.File;
import java.util.Locale;

/**
* JamOrHam
*/
public abstract class FloatingLocaleActivityWithScreenshot extends ActivityWithMenu {

public static final String FORCE_ACTIVITY_LANGUAGE = "FORCE_ACTIVITY_LANGUAGE";
public static final String SCREENSHOT_AND_EXIT = "SCREENSHOT_AND_EXIT";

private static final int MY_PERMISSIONS_REQUEST_STORAGE_SCREENSHOT = 39032;

public static volatile String localeString = null;
private Locale locale;


private Context specialContext;

private Context oldContext;

@Override
protected void attachBaseContext(Context newBase) {
if (localeString != null) {
locale = new Locale(localeString);
specialContext = LocaleHelper.setLocale(newBase, locale);
super.attachBaseContext(specialContext);
} else {
super.attachBaseContext(newBase);
}
}

@Override
protected void onResume() {
if (localeString != null) {
oldContext = xdrip.getAppContext();
xdrip.setContextAlways(specialContext);
if (checkPermissions()) {
doScreenShot();
}
}
super.onResume();
}

private boolean checkPermissions() {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_STORAGE_SCREENSHOT);
return false;
}
return true;
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_PERMISSIONS_REQUEST_STORAGE_SCREENSHOT) {
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
doScreenShot();
} else {
JoH.static_toast_long(this, "Cannot download without storage permission");
}
}
}

private void doScreenShot() {
Inevitable.task("screenshot-screen", 1500, () -> JoH.runOnUiThread(() -> {
View rootView = getWindow().getDecorView().getRootView();
// TODO probably should centralize this code
String file_name = "xDrip-Status-Screenshot-" + JoH.dateTimeText(JoH.tsl()).replace(" ", "-").replace(":", "-").replace(".", "-") + ".png";
final String dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/Screenshots";
JoH.bitmapToFile(JoH.screenShot(rootView, "xDrip+ Status @ " + JoH.dateText(JoH.tsl())), dirPath, file_name);
JoH.shareImage(getApplicationContext(), new File(dirPath + "/" + file_name));
finish();
}));
}

@Override
protected void onPause() {
super.onPause();
if (localeString != null) {
xdrip.setContextAlways(oldContext);
}
}

@Override
protected void onDestroy() {
localeString = null;
super.onDestroy();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.eveningoutpost.dexdrip.ui.helpers;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;

import java.util.Locale;

/**
* JamOrHam
*/
public class LocaleHelper {

public static Context setLocale(Context context, Locale locale) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.eveningoutpost.dexdrip.models.UserError;
import com.eveningoutpost.dexdrip.xdrip;

import lombok.val;

/**
* Created by jamorham on 01/01/2018.
* <p>
Expand Down Expand Up @@ -100,6 +102,39 @@ public static String getStringTrimmed(final String pref, final String def) {
return str;
}

public static Object getValue(String key) {
initializePrefs();
if (!prefs.contains(key)) {
return null;
}

try {
return prefs.getBoolean(key, false);
} catch (ClassCastException e) {
//
}

try {
return prefs.getString(key, null);
} catch (ClassCastException e) {
//
}

return null;
}

public static String getAsString(final String pref, String def) {
initializePrefs();
val v = getValue(pref);
if (v instanceof Boolean) {
return Boolean.toString((Boolean) v);
} else if (v instanceof String) {
return (String)v;
} else {
return def;
}
}

public static boolean setString(final String pref, final String str) {
initializePrefs();
if (prefs != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
import com.eveningoutpost.dexdrip.cgm.nsfollow.NightscoutFollow;
import com.eveningoutpost.dexdrip.cgm.sharefollow.ShareFollowService;
import com.eveningoutpost.dexdrip.cgm.webfollow.Cpref;
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.auth.CareLinkAuthenticator;
import com.eveningoutpost.dexdrip.cgm.carelinkfollow.auth.CareLinkCredentialStore;
import com.eveningoutpost.dexdrip.cloud.jamcm.Pusher;
import com.eveningoutpost.dexdrip.g5model.DexSyncKeeper;
import com.eveningoutpost.dexdrip.g5model.Ob1G5StateMachine;
import com.eveningoutpost.dexdrip.healthconnect.HealthConnectEntry;
Expand Down Expand Up @@ -571,6 +574,7 @@ public void showSearch(MenuItem item) {

private final SharedPreferences.OnSharedPreferenceChangeListener xDripCloudListener = (sharedPreferences, key) -> {
if (key!= null && key.equals("use_xdrip_cloud_sync")) {
Pusher.requestReconnect();
CollectionServiceStarter.restartCollectionServiceBackground();
}
};
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/eveningoutpost/dexdrip/xdrip.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public static void setContext(final Context context) {
}
}

public static void setContextAlways(final Context context) {
if (context == null) return;
Log.d(TAG, "Set context: " + context.getResources().getConfiguration().getLocales().get(0).getLanguage()
+ " was: " + xdrip.context.getResources().getConfiguration().getLocales().get(0).getLanguage());
xdrip.context = context;
}


@Override
public void onCreate() {
xdrip.context = getApplicationContext();
Expand Down
Loading

0 comments on commit 4aea550

Please sign in to comment.