-
Notifications
You must be signed in to change notification settings - Fork 211
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
base: master
Are you sure you want to change the base?
Changes from 13 commits
ab3e78a
6e1d1de
5db8f51
ca4f505
6f7a2df
8a12204
baf85b2
6331341
44b0bb6
5314316
2684df9
a433c28
101e626
07fac2b
d231998
02e62e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
}; | ||
|
@@ -55,61 +35,41 @@ public WidgetUpdateService() {} | |
|
||
@Override | ||
public void onCreate() { | ||
context = getApplicationContext(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.