package com.gebauz.Uhr;
import java.util.Timer;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.ComponentName;
import android.content.Intent;
import android.app.PendingIntent;
import android.widget.RemoteViews;
import java.util.Date;
import java.util.TimerTask;
public class UhrWidget
extends AppWidgetProvider
{
public static String ACTION_WIDGET_POPUP =
"PopupWidget";
public static String ACTION_WIDGET_RECEIVER =
"ActionReceiverWidget";
@
Override
public void onUpdate
(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds
)
{
Timer timer =
new Timer();
timer.
scheduleAtFixedRate(new MyTime
(context, appWidgetManager
),
1,
5000);
}
private class MyTime
extends TimerTask
{
RemoteViews remoteViews
;
AppWidgetManager appWidgetManager
;
ComponentName thisWidget
;
//DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
int hours0
[] =
{
R.
drawable.
hour0_0,
R.
drawable.
hour0_1,
R.
drawable.
hour0_2,
};
int hours1
[] =
{
R.
drawable.
hour1_0,
R.
drawable.
hour1_1,
R.
drawable.
hour1_2,
R.
drawable.
hour1_3,
R.
drawable.
hour1_4,
R.
drawable.
hour1_5,
R.
drawable.
hour1_6,
R.
drawable.
hour1_7,
R.
drawable.
hour1_8,
R.
drawable.
hour1_9,
};
int mins0
[] =
{
R.
drawable.
min0_0,
R.
drawable.
min0_1,
R.
drawable.
min0_2,
R.
drawable.
min0_3,
R.
drawable.
min0_4,
R.
drawable.
min0_5,
};
int mins1
[] =
{
R.
drawable.
min1_0,
R.
drawable.
min1_1,
R.
drawable.
min1_2,
R.
drawable.
min1_3,
R.
drawable.
min1_4,
R.
drawable.
min1_5,
R.
drawable.
min1_6,
R.
drawable.
min1_7,
R.
drawable.
min1_8,
R.
drawable.
min1_9,
};
int seks0
[] =
{
R.
drawable.
sek0_0,
R.
drawable.
sek0_1,
R.
drawable.
sek0_2,
R.
drawable.
sek0_3,
R.
drawable.
sek0_4,
R.
drawable.
sek0_5,
};
int seks1
[] =
{
R.
drawable.
sek1_0,
R.
drawable.
sek1_1,
R.
drawable.
sek1_2,
R.
drawable.
sek1_3,
R.
drawable.
sek1_4,
R.
drawable.
sek1_5,
R.
drawable.
sek1_6,
R.
drawable.
sek1_7,
R.
drawable.
sek1_8,
R.
drawable.
sek1_9,
};
public MyTime
(Context context, AppWidgetManager appWidgetManager
)
{
this.
appWidgetManager = appWidgetManager
;
remoteViews =
new RemoteViews
(context.
getPackageName(), R.
layout.
main);
thisWidget =
new ComponentName
(context, UhrWidget.
class);
Intent popupIntent =
new Intent
(context, PopupActivity.
class);
popupIntent.
setAction(ACTION_WIDGET_POPUP
);
PendingIntent popupPendingIntent = PendingIntent.
getActivity(context,
0, popupIntent,
0);
remoteViews.
setOnClickPendingIntent(R.
id.
widget, popupPendingIntent
);
appWidgetManager.
updateAppWidget(thisWidget, remoteViews
);
}
@
Override
public void run
()
{
Date now =
new Date();
int hours = now.
getHours();
int hour0 = hours /
10;
int hour1 = hours
% 10;
if (hour0
>=
3)
hour0 =
3;
if (hour1
>=
10)
hour1 =
10;
int mins = now.
getMinutes();
int min0 = mins /
10;
int min1 = mins
% 10;
if (min0
>=
6)
min0 =
6;
if (min1
>=
10)
min1 =
10;
/*
int seconds = now.getSeconds();
int second0 = seconds / 10;
int second1 = seconds % 10;
if (seconds >= 6)
seconds = 6;
if (second1 >= 10)
second1 = 10;*/
SetImageForDigit
(R.
id.
widget_hour0, hours0
[hour0
]);
SetImageForDigit
(R.
id.
widget_hour1, hours1
[hour1
]);
SetImageForDigit
(R.
id.
widget_min0, mins0
[min0
]);
SetImageForDigit
(R.
id.
widget_min1, mins1
[min1
]);
//SetImageForDigit(R.id.widget_sek0, seks0[second0]);
//SetImageForDigit(R.id.widget_sek1, seks1[second1]);
//remoteViews.setImageViewResource(R.id.widget_hour0, R.drawable.hour0_2);
//remoteViews.setTextViewText(R.id.widget_textview, "Time = " + format.format(new Date()));
appWidgetManager.
updateAppWidget(thisWidget, remoteViews
);
}
private void SetImageForDigit
(int digit,
int resource
)
{
remoteViews.
setImageViewResource(digit, resource
);
}
}
}