1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-29 03:55:49 +01:00

Sharing Sports Activity improvements

This commit is contained in:
vanous 2020-10-11 18:05:49 +02:00
parent d5b2818876
commit 46a1d2908a
7 changed files with 181 additions and 48 deletions

View File

@ -19,21 +19,26 @@ package nodomain.freeyourgadget.gadgetbridge.activities;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
@ -43,12 +48,16 @@ import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import androidx.core.content.FileProvider;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date;
@ -65,28 +74,49 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryItems;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryJsonSummary;
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.SwipeEvents;
//import nodomain.freeyourgadget.gadgetbridge.util.OnSwipeTouchListener;
public class ActivitySummaryDetail extends AbstractGBActivity {
private static final Logger LOG = LoggerFactory.getLogger(ActivitySummaryDetail.class);
private GBDevice gbDevice;
private boolean show_raw_data = false;
BaseActivitySummary currentItem = null;
private GBDevice gbDevice;
private boolean show_raw_data = false;
private int alternateColor;
//private Object BottomSheetBehavior;
private Menu mOptionsMenu;
public static int getAlternateColor(Context context) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.alternate_row_background, typedValue, true);
return typedValue.data;
}
public static Bitmap getScreenShot(View view, int height, int width, Context context) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
if (GBApplication.isDarkThemeEnabled()) {
canvas.drawColor(GBApplication.getBackgroundColor(context));
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return bitmap;
}
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context appContext = this.getApplicationContext();
if (appContext instanceof GBApplication) {
setContentView(R.layout.activity_summary_details);
}
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
@ -137,9 +167,9 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
currentItem = newItem;
makeSummaryHeader(newItem);
makeSummaryContent(newItem);
activitySummariesChartFragment.setDateAndGetData(gbDevice, currentItem.getStartTime().getTime()/1000, currentItem.getEndTime().getTime()/1000);
activitySummariesChartFragment.setDateAndGetData(gbDevice, currentItem.getStartTime().getTime() / 1000, currentItem.getEndTime().getTime() / 1000);
layout.startAnimation(animFadeRight);
show_hide_gpx_menu();
} else {
layout.startAnimation(animBounceRight);
}
@ -152,8 +182,9 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
currentItem = newItem;
makeSummaryHeader(newItem);
makeSummaryContent(newItem);
activitySummariesChartFragment.setDateAndGetData(gbDevice, currentItem.getStartTime().getTime()/1000, currentItem.getEndTime().getTime()/1000);
activitySummariesChartFragment.setDateAndGetData(gbDevice, currentItem.getStartTime().getTime() / 1000, currentItem.getEndTime().getTime() / 1000);
layout.startAnimation(animFadeLeft);
show_hide_gpx_menu();
} else {
layout.startAnimation(animBounceLeft);
}
@ -164,7 +195,7 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
if (currentItem != null) {
makeSummaryHeader(currentItem);
makeSummaryContent(currentItem);
activitySummariesChartFragment.setDateAndGetData(gbDevice, currentItem.getStartTime().getTime()/1000, currentItem.getEndTime().getTime()/1000);
activitySummariesChartFragment.setDateAndGetData(gbDevice, currentItem.getStartTime().getTime() / 1000, currentItem.getEndTime().getTime() / 1000);
}
@ -190,7 +221,7 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
String name = currentItem.getName();
input.setText((name != null) ? name : "");
FrameLayout container = new FrameLayout(ActivitySummaryDetail.this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = getResources().getDimensionPixelSize(R.dimen.dialog_margin);
params.rightMargin = getResources().getDimensionPixelSize(R.dimen.dialog_margin);
input.setLayoutParams(params);
@ -224,22 +255,6 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
private void makeSummaryHeader(BaseActivitySummary item) {
//make view of data from main part of item
final String gpxTrack = item.getGpxTrack();
Button show_track_btn = findViewById(R.id.showTrack);
show_track_btn.setVisibility(View.GONE);
if (gpxTrack != null) {
show_track_btn.setVisibility(View.VISIBLE);
show_track_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
AndroidUtils.viewFile(gpxTrack, Intent.ACTION_VIEW, ActivitySummaryDetail.this);
} catch (IOException e) {
GB.toast(getApplicationContext(), "Unable to display GPX track: " + e.getMessage(), Toast.LENGTH_LONG, GB.ERROR, e);
}
}
});
}
String activitykindname = ActivityKind.asString(item.getActivityKind(), getApplicationContext());
String activityname = item.getName();
Date starttime = item.getStartTime();
@ -292,7 +307,7 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
TableRow label_row = new TableRow(ActivitySummaryDetail.this);
TextView label_field = new TextView(ActivitySummaryDetail.this);
label_field.setTextSize(16);
label_field.setPadding(0,10,0,0);
label_field.setPadding(0, 10, 0, 0);
label_field.setTypeface(null, Typeface.BOLD);
label_field.setText(String.format("%s", getStringResourceByName(key)));
label_row.addView(label_field);
@ -361,14 +376,6 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
}
}
public static int getAlternateColor(Context context) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.alternate_row_background, typedValue, true);
return typedValue.data;
}
private String getStringResourceByName(String aString) {
String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, "string", packageName);
@ -387,9 +394,82 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
// back button
finish();
return true;
case R.id.activity_action_take_screenshot:
take_share_screenshot(ActivitySummaryDetail.this);
return true;
case R.id.activity_action_share_gpx:
share_gpx_track(ActivitySummaryDetail.this);
return true;
}
return super.onOptionsItemSelected(item);
}
private void take_share_screenshot(Context context) {
final ScrollView layout = findViewById(R.id.activity_summary_detail_scroll_layout);
int width = layout.getChildAt(0).getHeight();
int height = layout.getChildAt(0).getWidth();
Bitmap screenShot = getScreenShot(layout, width, height, context);
String fileName = FileUtils.makeValidFileName("Screenshot-" + ActivityKind.asString(currentItem.getActivityKind(), context).toLowerCase() + "-" + DateTimeUtils.formatIso8601(currentItem.getStartTime()) + ".png");
try {
File targetFile = new File(FileUtils.getExternalFilesDir(), fileName);
FileOutputStream fOut = new FileOutputStream(targetFile);
screenShot.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
shareScreenshot(targetFile, context);
GB.toast(getApplicationContext(), "Screenshot saved", Toast.LENGTH_LONG, GB.INFO);
} catch (IOException e) {
e.printStackTrace();
}
}
private void shareScreenshot(File targetFile, Context context) {
Uri contentUri = FileProvider.getUriForFile(context,
context.getApplicationContext().getPackageName() + ".screenshot_provider", targetFile);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
String shareBody = "Sports Activity";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sports Activity");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
try {
startActivity(Intent.createChooser(sharingIntent, "Share via"));
} catch (ActivityNotFoundException e) {
Toast.makeText(context, R.string.activity_error_no_app_for_png, Toast.LENGTH_LONG).show();
}
}
private void share_gpx_track(Context context) {
final String gpxTrack = currentItem.getGpxTrack();
if (gpxTrack != null) {
try {
AndroidUtils.viewFile(gpxTrack, Intent.ACTION_VIEW, context);
} catch (IOException e) {
GB.toast(getApplicationContext(), "Unable to display GPX track: " + e.getMessage(), Toast.LENGTH_LONG, GB.ERROR, e);
}
} else {
GB.toast(getApplicationContext(), "No GPX track in this activity", Toast.LENGTH_LONG, GB.INFO);
}
}
private void show_hide_gpx_menu() {
final String gpxTrack = currentItem.getGpxTrack();
if (gpxTrack == null) {
mOptionsMenu.findItem(R.id.activity_detail_overflowMenu).getSubMenu().findItem(R.id.activity_action_share_gpx).setVisible(false);
} else {
mOptionsMenu.findItem(R.id.activity_detail_overflowMenu).getSubMenu().findItem(R.id.activity_action_share_gpx).setVisible(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
mOptionsMenu = menu;
getMenuInflater().inflate(R.menu.activity_take_screenshot_menu, menu);
show_hide_gpx_menu();
return true;
}
}

View File

@ -31,6 +31,7 @@ import com.github.mikephil.charting.charts.Chart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -61,6 +62,14 @@ public class ActivityListingChartFragment extends AbstractChartFragment {
stepListAdapter = new ActivityListingAdapter(getContext());
stepsList.setAdapter(stepListAdapter);
stepsDateView = rootView.findViewById(R.id.stepsDateView);
LOG.debug("PETR " + getHost());
try {
getChartsHost().getClass().getMethod("enableSwipeRefresh").invoke(false);
} catch (Exception e) {
LOG.debug("dsda", e);
}
//device.getClass().getMethod("cancelBondProcess").invoke(device);
//refresh();
return rootView;
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/secondarytext"
android:pathData="M20.5,3l-0.16,0.03L15,5.1 9,3 3.36,4.9c-0.21,0.07 -0.36,0.25 -0.36,0.48L3,20.5c0,0.28 0.22,0.5 0.5,0.5l0.16,-0.03L9,18.9l6,2.1 5.64,-1.9c0.21,-0.07 0.36,-0.25 0.36,-0.48L21,3.5c0,-0.28 -0.22,-0.5 -0.5,-0.5zM10,5.47l4,1.4v11.66l-4,-1.4L10,5.47zM5,6.46l3,-1.01v11.7l-3,1.16L5,6.46zM19,17.54l-3,1.01L16,6.86l3,-1.16v11.84z"/>
</vector>

View File

@ -172,12 +172,6 @@
</FrameLayout>
<Button
android:id="@+id/showTrack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/activity_detail_show_gps_label" />
</LinearLayout>
</LinearLayout>

View File

@ -2,22 +2,33 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/activity_action_manage_timestamp"
android:icon="@drawable/ic_history"
android:title="@string/reset_index"
app:showAsAction="never" />
<item
android:id="@+id/activity_action_filter"
android:icon="@drawable/ic_filter_alt"
android:title="@string/pref_header_filter"
app:iconTint="@color/primarytext_dark"
app:showAsAction="ifRoom" />
app:showAsAction="always" />
<item
android:id="@+id/activity_action_calculate_summary_stats"
android:icon="@drawable/ic_calculate"
android:title="@string/pref_header_statistics"
app:iconTint="@color/primarytext_dark"
app:showAsAction="ifRoom" />
app:showAsAction="always" />
<item
android:id="@+id/overflowMenu"
android:icon="@drawable/ic_menu"
android:title="@string/menuitem_more"
app:iconTint="@color/primarytext_dark"
app:showAsAction="always">
<menu>
<item
android:id="@+id/activity_action_manage_timestamp"
android:icon="@drawable/ic_history"
android:title="@string/reset_index"
app:showAsAction="never" />
</menu>
</item>
</menu>

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/activity_detail_overflowMenu"
android:icon="@drawable/ic_share"
android:title="@string/share"
app:iconTint="@color/primarytext_dark"
app:showAsAction="always">
<menu>
<item
android:id="@+id/activity_action_take_screenshot"
android:icon="@drawable/ic_screenshot"
android:title="@string/controlcenter_take_screenshot"
app:showAsAction="never" />
<item
android:id="@+id/activity_action_share_gpx"
android:icon="@drawable/ic_map"
android:title="@string/activity_detail_show_gps_label"
app:showAsAction="never" />
</menu>
</item>
</menu>

View File

@ -732,6 +732,7 @@
<string name="activity_type_treadmill">Treadmill</string>
<string name="activity_type_exercise">Exercise</string>
<string name="activity_error_no_app_for_gpx">To view activity trace, install app which can handle GPX files.</string>
<string name="activity_error_no_app_for_png">To share this screenshot, install an app which can handle image files.</string>
<string name="select_all">Select all</string>
<string name="share">Share</string>
<string name="reset_index">Reset fetch date</string>