Skip to content

Commit

Permalink
Fixed a bug where too many pending updates could cause the widget to …
Browse files Browse the repository at this point in the history
…stop updating (this also improves performance)
  • Loading branch information
adolfintel committed Feb 5, 2023
1 parent 495d5b0 commit 1d3e0eb
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 80 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "com.dosse.clock31"
minSdk 21
targetSdk 32
versionCode 4
versionName '1.0.2'
versionCode 5
versionName '1.0.3'
}

buildTypes {
Expand Down
159 changes: 85 additions & 74 deletions app/src/main/java/com/dosse/clock31/C31Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,95 +24,106 @@ public class C31Widget extends AppWidgetProvider {

private static final String TAG="C31WidgetProvider";

public static boolean updatePending =false;

@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.v(TAG,"Intent received: "+intent.toString());
Log.v(TAG,"Intent received: "+intent.getAction());
if(updatePending){
Log.v(TAG,"Update already pending");
return;
}
updatePending=true;
onUpdate(context,AppWidgetManager.getInstance(context),AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, C31Widget.class)));
}

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Log.v(TAG,"updateAppWidget: "+appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.c31_widget);
Bundle options = AppWidgetManager.getInstance(context).getAppWidgetOptions(appWidgetId);
boolean hideAlarm=false, hideCalendar=false;
if(options!=null) {
int h = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
int w = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
Log.v(TAG, "Resize: "+w + "x" + h);
float clockFontScale = Math.max(0.4f, Math.min(1, Math.min(h, w) / 275f));
float dateFontScale = Math.max(0.7f, Math.min(1, Math.min(h, w) / 275f));
if (w < 220) {
hideAlarm = true;
dateFontScale = Math.min(1, dateFontScale * 1.25f);
}
if (h < 80) {
hideCalendar = true;
clockFontScale = Math.min(1, clockFontScale * 1.5f);
dateFontScale = Math.min(1, dateFontScale * 1.25f);
try {
Log.v(TAG, "updateAppWidget: " + appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.c31_widget);
Bundle options = AppWidgetManager.getInstance(context).getAppWidgetOptions(appWidgetId);
boolean hideAlarm = false, hideCalendar = false;
if (options != null) {
int h = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
int w = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
Log.v(TAG, "Resize: " + w + "x" + h);
float clockFontScale = Math.max(0.4f, Math.min(1, Math.min(h, w) / 275f));
float dateFontScale = Math.max(0.7f, Math.min(1, Math.min(h, w) / 275f));
if (w < 220) {
hideAlarm = true;
dateFontScale = Math.min(1, dateFontScale * 1.25f);
}
if (h < 80) {
hideCalendar = true;
clockFontScale = Math.min(1, clockFontScale * 1.5f);
dateFontScale = Math.min(1, dateFontScale * 1.25f);
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
clockFontScale = clockFontScale * 0.8f;
}
if (DateFormat.is24HourFormat(context)) {
views.setTextViewTextSize(R.id.clock, TypedValue.COMPLEX_UNIT_DIP, 80 * clockFontScale);
} else {
views.setTextViewTextSize(R.id.clock, TypedValue.COMPLEX_UNIT_DIP, 60 * clockFontScale);
}
views.setTextViewTextSize(R.id.date, TypedValue.COMPLEX_UNIT_DIP, 18 * dateFontScale);
views.setTextViewTextSize(R.id.alarm, TypedValue.COMPLEX_UNIT_DIP, 18 * dateFontScale);
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
clockFontScale = clockFontScale * 0.8f;
PackageManager pm = context.getPackageManager();
try {
PendingIntent openClockApp = PendingIntent.getActivity(context, 0, pm.getLaunchIntentForPackage("com.android.deskclock"), PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_IMMUTABLE : 0));
views.setOnClickPendingIntent(R.id.clock, openClockApp);
} catch (Throwable t) {
Log.v(TAG, "Failed to register event handler for tapping clock (no app?)");
}
if (DateFormat.is24HourFormat(context)) {
views.setTextViewTextSize(R.id.clock, TypedValue.COMPLEX_UNIT_DIP, 80 * clockFontScale);
} else {
views.setTextViewTextSize(R.id.clock, TypedValue.COMPLEX_UNIT_DIP, 60 * clockFontScale);
try {
PendingIntent openCalendarApp = PendingIntent.getActivity(context, 0, new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_CALENDAR), PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_IMMUTABLE : 0));
views.setOnClickPendingIntent(R.id.calendar_icon, openCalendarApp);
} catch (Throwable t) {
Log.v(TAG, "Failed to register event handler for calendar icon (no app?)");
}
views.setTextViewTextSize(R.id.date, TypedValue.COMPLEX_UNIT_DIP, 18 * dateFontScale);
views.setTextViewTextSize(R.id.alarm, TypedValue.COMPLEX_UNIT_DIP, 18 * dateFontScale);
}
PackageManager pm=context.getPackageManager();
try{
PendingIntent openClockApp=PendingIntent.getActivity(context, 0, pm.getLaunchIntentForPackage("com.android.deskclock"), PendingIntent.FLAG_UPDATE_CURRENT|(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M?PendingIntent.FLAG_IMMUTABLE:0));
views.setOnClickPendingIntent(R.id.clock,openClockApp);
}catch(Throwable t){
Log.v(TAG,"Failed to register event handler for tapping clock (no app?)");
}
try {
PendingIntent openCalendarApp = PendingIntent.getActivity(context, 0, new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_CALENDAR), PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_IMMUTABLE : 0));
views.setOnClickPendingIntent(R.id.calendar_icon, openCalendarApp);
}catch(Throwable t){
Log.v(TAG,"Failed to register event handler for calendar icon (no app?)");
}
if(!hideAlarm){
AlarmManager am =(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
AlarmManager.AlarmClockInfo alarmClock = am.getNextAlarmClock();
if (alarmClock != null) {
views.setViewVisibility(R.id.alarm, View.VISIBLE);
CharSequence alarm;
if(DateFormat.is24HourFormat(context)){
alarm=DateFormat.format("⏰ E HH:mm",alarmClock.getTriggerTime());
}else{
alarm=DateFormat.format("⏰ E hh:mma",alarmClock.getTriggerTime());
}
views.setTextViewText(R.id.alarm,alarm);
try{
PendingIntent openClockApp=PendingIntent.getActivity(context, 0, pm.getLaunchIntentForPackage("com.android.deskclock"), PendingIntent.FLAG_UPDATE_CURRENT|(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M?PendingIntent.FLAG_IMMUTABLE:0));
views.setOnClickPendingIntent(R.id.alarm,openClockApp);
}catch(Throwable t){
Log.v(TAG,"Failed to register event handler for tapping alarm (no app?)");
if (!hideAlarm) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
AlarmManager.AlarmClockInfo alarmClock = am.getNextAlarmClock();
if (alarmClock != null) {
views.setViewVisibility(R.id.alarm, View.VISIBLE);
CharSequence alarm;
if (DateFormat.is24HourFormat(context)) {
alarm = DateFormat.format("⏰ E HH:mm", alarmClock.getTriggerTime());
} else {
alarm = DateFormat.format("⏰ E hh:mma", alarmClock.getTriggerTime());
}
views.setTextViewText(R.id.alarm, alarm);
try {
PendingIntent openClockApp = PendingIntent.getActivity(context, 0, pm.getLaunchIntentForPackage("com.android.deskclock"), PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_IMMUTABLE : 0));
views.setOnClickPendingIntent(R.id.alarm, openClockApp);
} catch (Throwable t) {
Log.v(TAG, "Failed to register event handler for tapping alarm (no app?)");
}
} else {
views.setViewVisibility(R.id.alarm, View.GONE);
}
}else {
} else {
views.setViewVisibility(R.id.alarm, View.GONE);
}
}else{
views.setViewVisibility(R.id.alarm, View.GONE);
}
if(!hideCalendar) {
views.setViewVisibility(R.id.calendar_container, View.VISIBLE);
views.setRemoteAdapter(R.id.calendar, new Intent(context, CalendarRemoteViewsService.class));
Intent eventClickTemplate = new Intent(Intent.ACTION_VIEW);
PendingIntent eventClickPendingIntent = PendingIntent.getActivity(context, 0, eventClickTemplate, PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0));
views.setPendingIntentTemplate(R.id.calendar, eventClickPendingIntent);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.calendar);
}else{
views.setViewVisibility(R.id.calendar_container, View.GONE);
if (!hideCalendar) {
views.setViewVisibility(R.id.calendar_container, View.VISIBLE);
views.setRemoteAdapter(R.id.calendar, new Intent(context, CalendarRemoteViewsService.class));
Intent eventClickTemplate = new Intent(Intent.ACTION_VIEW);
PendingIntent eventClickPendingIntent = PendingIntent.getActivity(context, 0, eventClickTemplate, PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_MUTABLE : 0));
views.setPendingIntentTemplate(R.id.calendar, eventClickPendingIntent);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.calendar);
} else {
views.setViewVisibility(R.id.calendar_container, View.GONE);
}
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}catch(Throwable t){
Log.e(TAG,"Internal error: "+t);
}
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}

public static final String ACTION_REFRESH="com.dosse.clock31.ACTION_REFRESH";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public CalendarRemoteViewsFactory(Context context) {

private void updateCalendarInfo(){
Log.v(TAG,"Updating calendar info");
C31Widget.updatePending=false;
if(context.checkPermission(Manifest.permission.READ_CALENDAR, Process.myPid(), Process.myUid())== PackageManager.PERMISSION_GRANTED){
try {
Uri uri = Uri.withAppendedPath(CalendarContract.Instances.CONTENT_URI, String.format(Locale.ENGLISH, "%d/%d", System.currentTimeMillis(), System.currentTimeMillis() + 14 * DAY_IN_MS));
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'com.android.application' version '7.4.1' apply false
id 'com.android.library' version '7.4.1' apply false
}

task clean(type: Delete) {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Aug 02 09:38:18 CEST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit 1d3e0eb

Please sign in to comment.