Skip to content

Commit

Permalink
MegaStatus: add share button to screenshot in english
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Sep 16, 2024
1 parent 84a5ce8 commit 26ae3c7
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 7 deletions.
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
@@ -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);
}

}
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 @@ -65,6 +65,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
8 changes: 5 additions & 3 deletions app/src/main/res/menu/menu_mega_status.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.eveningoutpost.dexdrip.MegaStatus">
<item
android:id="@+id/action_settings"
android:id="@+id/action_screenshot"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/settings" />
android:icon="@drawable/ic_share_variant_grey600_24dp"
android:title="@string/screenshot"
app:showAsAction="always" />
</menu>

0 comments on commit 26ae3c7

Please sign in to comment.