I created an analog clock widget in java, and for some reason I get "Problem loading widget" all the time, the only things I get from the logcat when I try to place the widget are:
2020-11-18 19:19:13.162 9482-9482/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-11-18 19:19:13.162 9482-9482/? E/Zygote: accessInfo : 1
2020-11-18 19:19:13.166 9482-9482/? I/ckcustomizatio: Late-enabling -Xcheck:jni
2020-11-18 19:19:13.182 9482-9482/? E/ckcustomizatio: Unknown bits set in runtime_flags: 0x8000
2020-11-18 19:19:13.196 9482-9482/com.l_es.analogclockcustomization D/ActivityThread: setConscryptValidator
2020-11-18 19:19:13.196 9482-9482/com.l_es.analogclockcustomization D/ActivityThread: setConscryptValidator - put
and the widget is only a black square with "Problem loading widget" on it.
here is the widget class:
public class MainWidget extends AppWidgetProvider {
static RemoteViews views;
//preferences
private static SharedPreferences custClockPrefs;
//number of possible designs
private static int numClocks;
//IDs of Analog Clock elements
static int[] clockDesigns;
public void onReceive(Context context, Intent intent) {
numClocks = context.getResources().getInteger(R.integer.num_clocks);
clockDesigns = new int[numClocks];
for(int d = 0; d < numClocks; d++){
clockDesigns[d] = context.getResources().getIdentifier
("AnalogClock"+d, "id", context.getPackageName());
}
//find out the action
String action = intent.getAction();
//is it time to update
if(AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
views = new RemoteViews(context.getPackageName(),
R.layout.clock_widget_layout);
AppWidgetManager.getInstance(context).updateAppWidget
(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
custClockPrefs = context.getSharedPreferences("CustomClockPrefs", 0);
int chosenDesign = custClockPrefs.getInt("clockdesign", 0);
if(chosenDesign >= 0){
for(int d = 0; d < numClocks; d++){
if(d != chosenDesign)
views.setViewVisibility(clockDesigns[d], View.INVISIBLE);
}
views.setViewVisibility(clockDesigns[chosenDesign], View.VISIBLE);
}
Intent choiceIntent = new Intent(context, ClockChoice.class);
PendingIntent clickPendIntent = PendingIntent.getActivity
(context, 0, choiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.custom_clock_widget, clickPendIntent);
}
}
}
here is the draw function I use in order to draw the clock:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
final float w = (float) getWidth() / 2;
final float h = (float) getHeight() / 2;
final int saveCount = canvas.save();
canvas.translate(w, h);
final float scale = Math.min((float) w / mDial.getIntrinsicWidth(),
(float) h / mDial.getIntrinsicHeight());
if (scale < 1f) {
canvas.scale(scale, scale, 0f, 0f);
}
mDial.draw(canvas);
final float hourAngle = mTime.get(Calendar.HOUR) * 30f;
canvas.rotate(hourAngle, 0f, 0f);
mHourHand.draw(canvas);
final float minuteAngle = mTime.get(Calendar.MINUTE) * 6f;
canvas.rotate(minuteAngle - hourAngle, 0f, 0f);
mMinuteHand.draw(canvas);
if (mEnableSeconds) {
final float secondAngle = mTime.get(Calendar.SECOND) * 6f;
canvas.rotate(secondAngle - minuteAngle, 0f, 0f);
mSecondHand.draw(canvas);
}
canvas.restoreToCount(saveCount);
}
Here is my layout file of the widget, keep in mind that I set at every time 2 out of the 3 to be with visibility of "gone":
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/custom_clock_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/clock_margin"
android:gravity="center">
<com.l_es.analogclockcustomization.AnalogClockDesign
android:id="@+id/AnalogClock0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:showSecondHand="false"
app:dial="@drawable/clock_dial_2"
app:hour="@drawable/clock_hour_hand_2"
app:minute="@drawable/clock_minute_hand_2"/>
<com.l_es.analogclockcustomization.AnalogClockDesign
android:id="@+id/AnalogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:showSecondHand="false"
app:dial="@drawable/clock_dial_3"
app:hour="@drawable/clock_hour_hand_3"
app:minute="@drawable/clock_minute_hand_3"/>
<com.l_es.analogclockcustomization.AnalogClockDesign
android:id="@+id/AnalogClock2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:showSecondHand="false"
app:dial="@drawable/clock_dial_4"
app:hour="@drawable/clock_hour_hand_4"
app:minute="@drawable/clock_minute_hand_4"/>
</RelativeLayout>
clock_widget.xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/clock_widget_layout"
android:minWidth="146dp"
android:minHeight="146dp"
android:previewImage="@mipmap/ic_launcher"
android:resizeMode="vertical|horizontal"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen">
</appwidget-provider>
onUpdate()
onMainWidget
? – AlimentationonReceive
– TriplicityonReceive()
(MainWidget
) called? – AlimentationonUpdate()
called automatically? – Alimentation<action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> intent-filter
. Right? – Alimentation<meta-data android:name="android.appwidget.provider" android:resource="@xml/clock_widget" />
– Triplicity