Skip to content

Commit

Permalink
Statistics pages: add print/share feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Oct 7, 2016
1 parent 401218f commit 662d218
Show file tree
Hide file tree
Showing 13 changed files with 442 additions and 33 deletions.
20 changes: 14 additions & 6 deletions app/src/main/java/com/eveningoutpost/dexdrip/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ public class Home extends ActivityWithMenu {
private TextView textBloodGlucose;
private TextView textInsulinDose;
private TextView textTime;
private final int REQ_CODE_SPEECH_INPUT = 1994;
private final int REQ_CODE_SPEECH_NOTE_INPUT = 1995;
private final int SHOWCASE_UNDO = 4;
private final int SHOWCASE_REDO = 5;
private final int SHOWCASE_NOTE_LONG = 6;
private final int SHOWCASE_VARIANT = 7;
private static final int REQ_CODE_SPEECH_INPUT = 1994;
private static final int REQ_CODE_SPEECH_NOTE_INPUT = 1995;
private static final int SHOWCASE_UNDO = 4;
private static final int SHOWCASE_REDO = 5;
private static final int SHOWCASE_NOTE_LONG = 6;
private static final int SHOWCASE_VARIANT = 7;
public static final int SHOWCASE_STATISTICS = 8;
private static double last_speech_time = 0;
private PreviewLineChartView previewChart;
private TextView dexbridgeBattery;
Expand Down Expand Up @@ -2473,6 +2474,13 @@ public static boolean getPreferencesBoolean(final String pref, boolean def) {
return false;
}

public static void togglePreferencesBoolean(final String pref) {
if ((prefs == null) && (xdrip.getAppContext() != null)) {
prefs = PreferenceManager.getDefaultSharedPreferences(xdrip.getAppContext());
}
if (prefs != null) prefs.edit().putBoolean(pref, !prefs.getBoolean(pref, false)).apply();
}

public static String getPreferencesStringDefaultBlank(final String pref) {
if ((prefs == null) && (xdrip.getAppContext() != null)) {
prefs = PreferenceManager.getDefaultSharedPreferences(xdrip.getAppContext());
Expand Down
136 changes: 135 additions & 1 deletion app/src/main/java/com/eveningoutpost/dexdrip/Models/JoH.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.media.AudioManager;
import android.media.RingtoneManager;
Expand All @@ -23,14 +28,15 @@
import android.os.Handler;
import android.os.PowerManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.util.Base64;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.View;
import android.widget.Toast;

import com.eveningoutpost.dexdrip.Home;
Expand All @@ -42,6 +48,8 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
Expand All @@ -59,6 +67,8 @@
import java.util.zip.GZIPOutputStream;
import java.util.zip.Inflater;

import static com.eveningoutpost.dexdrip.stats.StatsActivity.SHOW_STATISTICS_PRINT_COLOR;

/**
* Created by jamorham on 06/01/16.
* <p>
Expand Down Expand Up @@ -434,6 +444,10 @@ public static String dateTimeText(long timestamp) {
return android.text.format.DateFormat.format("yyyy-MM-dd HH:mm:ss", timestamp).toString();
}

public static String dateText(long timestamp) {
return android.text.format.DateFormat.format("yyyy-MM-dd", timestamp).toString();
}

public static double tolerantParseDouble(String str) throws NumberFormatException {
return Double.parseDouble(str.replace(",", "."));

Expand Down Expand Up @@ -609,6 +623,126 @@ public static Object cloneObject(Object obj) {
}
}

public static void goFullScreen(boolean fullScreen, View decorView) {

if (fullScreen) {
if (Build.VERSION.SDK_INT >= 19) {
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
} else {
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
} else {
decorView.setSystemUiVisibility(0); // TODO will this need revisiting in later android vers?
}
}


public static Bitmap screenShot(View view, String annotation) {

if (view == null) {
static_toast_long("View is null in screenshot!");
return null;
}
final int width = view.getWidth();
final int height = view.getHeight();
Log.d(TAG, "Screenshot called: " + width + "," + height);
final Bitmap bitmap = Bitmap.createBitmap(width,
height, Bitmap.Config.ARGB_8888);

final Canvas canvas = new Canvas(bitmap);
if (Home.getPreferencesBooleanDefaultFalse(SHOW_STATISTICS_PRINT_COLOR)) {
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(0, 0, width, height, paint);
}


view.destroyDrawingCache();
view.layout(0, 0, width, height);
view.draw(canvas);

if (annotation != null) {
final int offset = (annotation != null) ? 40 : 0;
final Bitmap bitmapf = Bitmap.createBitmap(width,
height + offset, Bitmap.Config.ARGB_8888);
final Canvas canvasf = new Canvas(bitmapf);

Paint paint = new Paint();
if (Home.getPreferencesBooleanDefaultFalse(SHOW_STATISTICS_PRINT_COLOR)) {
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
canvasf.drawRect(0, 0, width, offset, paint);
paint.setColor(Color.BLACK);
} else {
paint.setColor(Color.GRAY);
}
paint.setTextSize(20);
// paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
canvasf.drawBitmap(bitmap, 0, offset, paint);
canvasf.drawText(annotation, 50, (offset / 2) + 5, paint);
bitmap.recycle();
return bitmapf;
}

return bitmap;
}

public static Bitmap screenShot2(View view) {
Log.d(TAG, "Screenshot2 called: " + view.getWidth() + "," + view.getHeight());
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
final Bitmap bitmap = view.getDrawingCache(true);
return bitmap;
}


public static void bitmapToFile(Bitmap bitmap, String path, String fileName) {

if (bitmap == null) return;
Log.d(TAG, "bitmapToFile: " + bitmap.getWidth() + "x" + bitmap.getHeight());
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
final File file = new File(path, fileName);
try {
FileOutputStream output = new FileOutputStream(file);
final boolean result = bitmap.compress(Bitmap.CompressFormat.PNG, 80, output);
output.flush();
output.close();
Log.d(TAG, "Bitmap compress result: " + result);
} catch (Exception e) {
Log.e(TAG, "Got exception writing bitmap to file: " + e);
}
}

public static void shareImage(Context context, File file) {
Uri uri = Uri.fromFile(file);
final Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
try {
context.startActivity(Intent.createChooser(intent, "Share"));
} catch (ActivityNotFoundException e) {
static_toast_long("No suitable app to show an image!");
}
}


public static void showNotification(String title, String content, PendingIntent intent, int notificationId, boolean sound, boolean vibrate, boolean onetime) {
final NotificationCompat.Builder mBuilder = notificationBuilder(title, content, intent);
final long[] vibratePattern = {0, 1000, 300, 1000, 300, 1000};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public View getView() {

if (chartView == null) {
chartView = new ChartView(getActivity().getApplicationContext());
chartView.setTag(1);
}
return chartView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
myView = inflater.inflate(
R.layout.stats_general, container, false);

myView.setTag(0);

(new CalculationThread(myView, getActivity().getApplicationContext())).start();

return getView();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public View getView() {

if (percentileView == null) {
percentileView = new PercentileView(getActivity().getApplicationContext());
percentileView.setTag(2);
}
return percentileView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import android.graphics.Path;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;

import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.Models.UserError.Log;
import android.view.View;

Expand Down Expand Up @@ -126,13 +128,21 @@ private void drawGrid(Canvas canvas) {
Paint myPaint = new Paint();
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setAntiAlias(false);
myPaint.setColor(Color.LTGRAY);
if (Home.getPreferencesBooleanDefaultFalse(StatsActivity.SHOW_STATISTICS_PRINT_COLOR)) {
myPaint.setColor(Color.BLACK);
} else {
myPaint.setColor(Color.LTGRAY);
}
myPaint.setStrokeWidth(dp2px(1));

Paint myPaintText = new Paint();
myPaintText.setStyle(Paint.Style.STROKE);
myPaintText.setAntiAlias(false);
myPaintText.setColor(Color.LTGRAY);
if (Home.getPreferencesBooleanDefaultFalse(StatsActivity.SHOW_STATISTICS_PRINT_COLOR)) {
myPaintText.setColor(Color.BLACK);
} else {
myPaintText.setColor(Color.LTGRAY);
}
myPaintText.setTextSize(dp2px(10));

canvas.drawLine(dpOffset, 0, dpOffset, canvas.getHeight() - dpOffset, myPaint);
Expand Down
Loading

0 comments on commit 662d218

Please sign in to comment.