package com.yubico.yubitotp;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class TotpWidgetProvider extends AppWidgetProvider {
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// clear up our settings
		for(int appWidgetId : appWidgetIds) {
			TotpWidgetConfigure.deleteSelectedSlot(context, appWidgetId);
		}
		
		super.onDeleted(context, appWidgetIds);
	}
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		
		for(int appWidgetId : appWidgetIds) {
			updateAppWidget(context, appWidgetManager, appWidgetId, null);
		}
		
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}
	
	

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		
		if(intent.getAction() == "UPDATE_ONE") {
			int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
			if(appWidgetId != -1) {
				updateAppWidget(context, AppWidgetManager.getInstance(context), appWidgetId, null);
			}
		}
	}

	public static void updateAppWidget(Context context,
			AppWidgetManager appWidgetManager, int appWidgetId, String totp) {
		Intent totpIntent = new Intent(context, TotpWidgetActivity.class);
		totpIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, totpIntent, PendingIntent.FLAG_CANCEL_CURRENT);
		
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.totp_widget);
		views.setOnClickPendingIntent(R.id.totp_widget, pendingIntent);
		
		int imageId = R.drawable.widget_back;
		if(totp == null) {
			totp = context.getText(R.string.totp_default).toString();
			imageId = R.drawable.widget_empty;
		} else { // after 30 seconds we want to remove the totp again..
			AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
			Intent updateIntent = new Intent(context, TotpWidgetProvider.class);
			updateIntent.setAction("UPDATE_ONE");
			updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
			PendingIntent pendingUpdate = PendingIntent.getBroadcast(context, 0, updateIntent, PendingIntent.FLAG_CANCEL_CURRENT);
			alarm.set(AlarmManager.RTC, System.currentTimeMillis() + 30 * 1000, pendingUpdate);
		}
		
		views.setTextViewText(R.id.totp_text, totp);
		views.setImageViewResource(R.id.widget_background, imageId);
		
		appWidgetManager.updateAppWidget(appWidgetId, views);
	}
}

@@ -23,16 +23,123 @@
 itotp;%0A%0A
+import java.util.Map;%0Aimport java.util.concurrent.ConcurrentHashMap;%0A%0Aimport android.annotation.TargetApi;%0A
 import a
@@ -155,32 +155,32 @@
 p.AlarmManager;%0A
-
 import android.a
@@ -339,32 +339,115 @@
 content.Intent;%0A
+import android.os.Build;%0Aimport android.os.Bundle;%0Aimport android.util.TypedValue;%0A
 import android.w
@@ -526,16 +526,115 @@
 vider %7B%0A
+%09private static final Map%3CInteger, Integer%3E sizeMap = new ConcurrentHashMap%3CInteger, Integer%3E();%0A%09%0A
 %09@Overri
@@ -830,16 +830,48 @@
 getId);%0A
+%09%09%09sizeMap.remove(appWidgetId);%0A
 %09%09%7D%0A%09%09%0A%09
@@ -1569,16 +1569,595 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09@TargetApi(Build.VERSION_CODES.JELLY_BEAN)%0A%09@Override%0A%09public void onAppWidgetOptionsChanged(Context context,%0A%09%09%09AppWidgetManager appWidgetManager, int appWidgetId,%0A%09%09%09Bundle newOptions) %7B%0A%09%09super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,%0A%09%09%09%09newOptions);%0A%09%09%0A%09%09// find out the new width and put it away in a map for later usage%0A%09%09int width = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);%0A%09%09sizeMap.put(appWidgetId, width);%0A%09%09updateAppWidget(context, appWidgetManager, appWidgetId, null);%0A%09%7D%0A%0A%09@TargetApi(Build.VERSION_CODES.JELLY_BEAN)%0A
 %09public 
@@ -3318,16 +3318,16 @@
 RRENT);%0A
-
 %09%09%09alarm
@@ -3412,16 +3412,308 @@
 %0A%09%09%7D%0A%09%09%0A
+%09%09if(Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN &&%0A%09%09%09%09sizeMap.containsKey(appWidgetId)) %7B%0A%09%09%09// make a guess on the text size based on the width%0A%09%09%09int width = sizeMap.get(appWidgetId);%0A%09%09%09views.setTextViewTextSize(R.id.totp_text, TypedValue.COMPLEX_UNIT_SP, width / 5);%0A%09%09%7D%0A%09%09%0A
 %09%09views.

change text size on the widget when it resizes