Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Widget for Galaxy Gear S2/3 watchface using Wearable Widgets #47

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,16 @@
android:resource="@xml/x_drip_widget_info" />
</receiver>

<receiver android:name=".gearWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/gear_widget_info" />
</receiver>

<service
android:name=".WidgetUpdateService"
android:enabled="true"
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/eveningoutpost/dexdrip/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,6 @@ protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setTheme(R.style.AppThemeToolBarLite); // for toolbar mode


Injectors.getHomeShelfComponent().inject(this);

if (!Experience.gotData()) {
Expand Down
21 changes: 13 additions & 8 deletions app/src/main/java/com/eveningoutpost/dexdrip/Models/BgReading.java
Original file line number Diff line number Diff line change
Expand Up @@ -697,24 +697,29 @@ public static String activeSlopeArrow() {
return slopeToArrowSymbol(slope);
}

public static String slopeToArrowSymbol(double slope) {
public static int slopeToArrowIndex(double slope) {
if (slope <= (-3.5)) {
return "\u21ca";// ⇊
return 0;// ⇊
} else if (slope <= (-2)) {
return "\u2193"; // ↓
return 1; // ↓
} else if (slope <= (-1)) {
return "\u2198"; // ↘
return 2; // ↘
} else if (slope <= (1)) {
return "\u2192"; // →
return 3; // →
} else if (slope <= (2)) {
return "\u2197"; // ↗
return 4; // ↗
} else if (slope <= (3.5)) {
return "\u2191"; // ↑
return 5; // ↑
} else {
return "\u21c8"; // ⇈
return 6; // ⇈
}
}

public static String slopeToArrowSymbol(double slope) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the reason for refactoring this method?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The up-right and down-right arrow symbols from the font are visually very different than the normal up, down, and right arrows. I did this refactoring in preparation to add some pure images for the arrows, and was going to refactor the gear widget to use them. Never got around to finishing it, but I intend to in the near future.

I've got the code in a local branch, I'll remove it for now so it's not confusing.

String[] arrow = { "\u21ca", "\u2193", "\u2198", "\u2192", "\u2197", "\u2191", "\u21c8" };
return arrow[slopeToArrowIndex(slope)];
}

public String slopeArrow() {
return slopeToArrowSymbol(this.calculated_value_slope * 60000);
}
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/eveningoutpost/dexdrip/SnoozeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public class SnoozeActivity extends ActivityWithMenu {

static final int infiniteSnoozeValueInMinutes = 5256000;//10 years
//static final int snoozeValues[] = new int []{5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 75, 90, 105, 120, 150, 180, 240, 300, 360, 420, 480, 540, 600};
static final int snoozeValues[] = new int []{ 10, 15, 20, 30, 40, 50, 60, 75, 90, 120, 150, 180, 240, 300, 360, 420, 480, 540, 600};
static final int snoozeValues[] = new int []{ 1, 2, 5, 10, 15, 20, 30, 40, 50, 60, 75, 90, 120, 150, 180, 240, 300, 360, 420, 480, 540, 600};

static int getSnoozeLocatoin(int time) {
static int getSnoozeLocation(int time) {
for (int i=0; i < snoozeValues.length; i++) {
if(time == snoozeValues[i]) {
return i;
Expand Down Expand Up @@ -96,9 +96,9 @@ static void SetSnoozePickerValues(NumberPicker picker, boolean above, int defaul
picker.setDisplayedValues(values);
picker.setWrapSelectorWheel(false);
if(default_snooze != 0) {
picker.setValue(getSnoozeLocatoin(default_snooze));
picker.setValue(getSnoozeLocation(default_snooze));
} else {
picker.setValue(getSnoozeLocatoin(getDefaultSnooze(above)));
picker.setValue(getSnoozeLocation(getDefaultSnooze(above)));
}
}

Expand Down Expand Up @@ -226,7 +226,7 @@ public void onClick(View v) {
snoozeValue.setMinValue(0);
snoozeValue.setDisplayedValues(values);
snoozeValue.setWrapSelectorWheel(false);
snoozeValue.setValue(getSnoozeLocatoin(60));
snoozeValue.setValue(getSnoozeLocation(60));

b1.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected void onDialogClosed(boolean positiveResult) {

// TODO probably should check whether data has actually changed before updating all the graphics
Home.staticRefreshBGCharts(true);
WidgetUpdateService.staticRefreshWidgets();
WidgetUpdateService.updateCurrentBgInfo();
Notifications.staticUpdateNotification();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected void onHandleIntent(Intent intent) {
ActivityRecognizedService.reStartActivityRecogniser(context);
}

ReadPerfs(context);
ReadPrefs(context);
unclearReading = notificationSetter(context);
ArmTimer(context, unclearReading);
context.startService(new Intent(context, MissedReadingService.class));
Expand All @@ -149,7 +149,7 @@ protected void onHandleIntent(Intent intent) {



public void ReadPerfs(Context context) {
public void ReadPrefs(Context context) {
mContext = context;
prefs = PreferenceManager.getDefaultSharedPreferences(context);
bg_notifications = prefs.getBoolean("bg_notifications", true);
Expand Down Expand Up @@ -178,7 +178,7 @@ public void ReadPerfs(Context context) {

// TODO REFACTOR
private void FileBasedNotifications(Context context) {
ReadPerfs(context);
ReadPrefs(context);
Sensor sensor = Sensor.currentSensor();

final BgReading bgReading = BgReading.last();
Expand Down Expand Up @@ -302,7 +302,7 @@ boolean trendingToAlertEnd(Context context, Boolean newAlert, AlertType Alert) {

// returns weather unclear bg reading was detected
private boolean notificationSetter(Context context) {
ReadPerfs(context);
ReadPrefs(context);
final long end = System.currentTimeMillis() + (60000 * 5);
final long start = end - (60000 * 60 * 3) - (60000 * 10);
BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(context, start, end);
Expand Down Expand Up @@ -556,7 +556,7 @@ private Bitmap createWearBitmap(long hours) {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public synchronized Notification createOngoingNotification(BgGraphBuilder bgGraphBuilder, Context context) {
mContext = context;
ReadPerfs(mContext);
ReadPrefs(mContext);
Intent intent = new Intent(mContext, Home.class);
List<BgReading> lastReadings = BgReading.latest(2);
BgReading lastReading = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,16 @@
import com.eveningoutpost.dexdrip.Models.UserError.Log;

public class WidgetUpdateService extends Service {
static Context context;
private static final String TAG = "WidgetUpdateService";

private boolean isRegistered = false;

public static void staticRefreshWidgets()
{
try {
Context context = xdrip.getAppContext();
if (AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, xDripWidget.class)).length > 0) {
context.startService(new Intent(context, WidgetUpdateService.class));
}
} catch (Exception e)
{
Log.e(TAG,"Got exception in staticRefreshWidgets: "+e);
}
}
private static Class widgetClasses[] = { xDripWidget.class, gearWidget.class };

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent intent) {
final PowerManager.WakeLock wl = JoH.getWakeLock("xdrip-widget-bcast", 20000);
//Log.d(TAG, "onReceive("+intent.getAction()+")");
if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0) {
updateCurrentBgInfo();
} else if (intent.getAction().compareTo(Intent.ACTION_SCREEN_ON) == 0) {
enableClockTicks();
updateCurrentBgInfo();
} else if (intent.getAction().compareTo(Intent.ACTION_SCREEN_OFF) == 0) {
disableClockTicks();
}
if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0) updateCurrentBgInfo();
JoH.releaseWakeLock(wl);
}
};
Expand All @@ -55,61 +35,41 @@ public WidgetUpdateService() {}

@Override
public void onCreate() {
context = getApplicationContext();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't store the application context in a static field here, just use xdrip.getAppContext() if you need it from static, otherwise just store it in a local variable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make the change.

super.onCreate();
PowerManager pm = (PowerManager) getSystemService(Service.POWER_SERVICE);
Log.d(TAG, "onCreate");
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && pm.isInteractive()) ||
(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && pm.isScreenOn()))
enableClockTicks();
else
disableClockTicks();
}

private void enableClockTicks() {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Gear widget needs clock ticks all the time to keep time updated in widget
Log.d(TAG, "enableClockTicks");
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_TICK);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks from the diff like this will always be listening for clock ticks. This isn't good for anyone who isn't using wearable widgets from a power saving point of view so I would suggest that the original behavior is restored but with it always enabling listening for clock ticks when the wearable widgets preference item I suggested is enabled. I can add that item in today so you have something to work with easily.

intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
if (isRegistered)
unregisterReceiver(broadcastReceiver);
registerReceiver(broadcastReceiver, intentFilter);
isRegistered = true;
}

private void disableClockTicks() {
Log.d(TAG, "disableClockTicks");
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
if (isRegistered)
unregisterReceiver(broadcastReceiver);
registerReceiver(broadcastReceiver, intentFilter);
isRegistered = true;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
updateCurrentBgInfo();
return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
if (broadcastReceiver != null) {
unregisterReceiver(broadcastReceiver);
isRegistered = false;
}
unregisterReceiver(broadcastReceiver);
}

public void updateCurrentBgInfo() {
Log.d(TAG, "Sending update flag to widget");
int ids[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), xDripWidget.class));
Log.d(TAG, "Updating " + ids.length + " widgets");
Intent intent = new Intent(this,xDripWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
sendBroadcast(intent);
public static void updateCurrentBgInfo() {
Log.d(TAG, "Sending update flag to widgets");
int ids[];
Intent intent;
//iterate each widget type, get IDs of all instances, update
for (Class widgetClass : widgetClasses) {
ids = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, widgetClass));
if (ids.length > 0) {
Log.d(TAG, "Updating " + ids.length + " " + widgetClass.getName() + " instances");
intent = new Intent(context, widgetClass);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
context.sendBroadcast(intent);
}
}
}
}
Loading