(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds)
| 35 | |
| 36 | public class WidgetSync extends AppWidgetProvider { |
| 37 | @Override |
| 38 | public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) { |
| 39 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); |
| 40 | boolean enabled = prefs.getBoolean("enabled", true); |
| 41 | boolean connected = prefs.getBoolean("connected", false); |
| 42 | |
| 43 | try { |
| 44 | Intent intent = new Intent(context, ServiceSynchronize.class) |
| 45 | .setAction("enable") |
| 46 | .putExtra("enabled", !enabled); |
| 47 | PendingIntent pi = PendingIntentCompat.getForegroundService( |
| 48 | context, ServiceSynchronize.PI_ENABLE, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
| 49 | |
| 50 | int colorWidgetForeground = context.getResources().getColor(R.color.colorWidgetForeground); |
| 51 | |
| 52 | for (int appWidgetId : appWidgetIds) { |
| 53 | boolean daynight = prefs.getBoolean("widget." + appWidgetId + ".daynight", false); |
| 54 | boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true); |
| 55 | int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT); |
| 56 | int version = prefs.getInt("widget." + appWidgetId + ".version", 0); |
| 57 | |
| 58 | if (version <= 1550) |
| 59 | semi = true; // Legacy |
| 60 | |
| 61 | RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_sync); |
| 62 | views.setOnClickPendingIntent(R.id.ivSync, pi); |
| 63 | |
| 64 | views.setImageViewResource(R.id.ivSync, enabled ? R.drawable.twotone_sync_24 : R.drawable.twotone_sync_disabled_24); |
| 65 | views.setInt(R.id.ivSync, "setImageAlpha", |
| 66 | !enabled || connected ? 255 : Math.round(Helper.LOW_LIGHT * 255)); |
| 67 | |
| 68 | if (!daynight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) |
| 69 | views.setColorStateListAttr(R.id.background, "setBackgroundTintList", 0); |
| 70 | |
| 71 | if (daynight && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { |
| 72 | views.setInt(R.id.background, "setBackgroundColor", Color.WHITE); |
| 73 | views.setColorStateListAttr(R.id.background, "setBackgroundTintList", android.R.attr.colorBackground); |
| 74 | views.setColorAttr(R.id.ivSync, "setColorFilter", android.R.attr.textColorPrimary); |
| 75 | } else if (background == Color.TRANSPARENT) { |
| 76 | if (semi) |
| 77 | views.setInt(R.id.background, "setBackgroundResource", R.drawable.widget_background); |
| 78 | else |
| 79 | views.setInt(R.id.background, "setBackgroundColor", background); |
| 80 | views.setInt(R.id.ivSync, "setColorFilter", colorWidgetForeground); |
| 81 | } else { |
| 82 | float lum = (float) ColorUtils.calculateLuminance(background); |
| 83 | |
| 84 | if (semi) |
| 85 | background = ColorUtils.setAlphaComponent(background, 127); |
| 86 | |
| 87 | views.setInt(R.id.background, "setBackgroundColor", background); |
| 88 | |
| 89 | int fg = (lum > 0.7f ? Color.BLACK : colorWidgetForeground); |
| 90 | views.setInt(R.id.ivSync, "setColorFilter", fg); |
| 91 | } |
| 92 | |
| 93 | int dp6 = Helper.dp2pixels(context, 6); |
| 94 | views.setViewPadding(R.id.content, dp6, dp6, dp6, dp6); |
nothing calls this directly
no test coverage detected