/*
 * Copyright (C) 2013 Scott Warner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tortel.deploytrack.provider;

import com.tortel.deploytrack.Log;
import com.tortel.deploytrack.R;
import com.tortel.deploytrack.data.*;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.widget.RemoteViews;

public class WidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        
        for (int widgetId : appWidgetIds) {
            // Get the deployment
            WidgetInfo info = DatabaseManager.getInstance(context).getWidgetInfo(widgetId);

            Log.d("Updating widget "+widgetId);
            
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget_layout);
            
            if(info != null){
                Log.d("Widget "+info.getId()+" with deployment "+info.getDeployment().getId());

                //Draw everything
                remoteViews = updateWidgetView(context, remoteViews, info);
            } else {
                Log.d("Widget "+widgetId+" has null info");
            }
            
            //Update it
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }
    
    /**
     * Sets up and fills the RemoteViews with the data provided in the WidgetInfo class
     * @param context
     * @param remoteViews
     * @param info
     */
    public static RemoteViews updateWidgetView(Context context, RemoteViews remoteViews, WidgetInfo info){
        Deployment deployment = info.getDeployment();
        Resources resources = context.getResources();
        
        //Set up the pie chart image
        Bitmap.Config conf = Bitmap.Config.ARGB_8888;
        Bitmap bmp = Bitmap.createBitmap(200,200,conf);
        Canvas canvas = new Canvas(bmp);
        
        //Configure the paint
        Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
        mPaint.setDither(true);
        mPaint.setColor(deployment.getCompletedColor());
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(10);
        
        //Pad the pie by 5px
        RectF box = new RectF(5,5,bmp.getWidth() - 5,bmp.getHeight() - 5);
        
        //The length of the arc to use for completed
        float sweep = 360* deployment.getPercentage() * 0.01f;

        //Draw the completed
        canvas.drawArc(box, -90f, sweep, true, mPaint);
        
        //Draw the remaining
        mPaint.setColor(deployment.getRemainingColor());
        canvas.drawArc(box, sweep - 90f, 360f - sweep, true, mPaint);
        
        //Draw a blank circle in the middle
        RectF smallbox = new RectF(100 - 20, 100 - 20, 100 + 20, 100 + 20);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawArc(smallbox, 0f, 360f, false, mPaint);
        
        // Set the text
        remoteViews.setTextViewText(R.id.widget_name, deployment.getName());
        remoteViews.setTextViewText(
                R.id.widget_info,
                resources.getString(R.string.days_left,
                        deployment.getRemaining()));
        remoteViews.setImageViewBitmap(R.id.widget_pie, bmp);

        // Register an onClickListener
        Intent intent = new Intent(context, WidgetProvider.class);

        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        int array[] = new int[1];
        array[0] = info.getId();
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, array);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews
                .setOnClickPendingIntent(R.id.widget_pie, pendingIntent);
        
        return remoteViews;
    }
}

@@ -1257,16 +1257,65 @@
 ovider %7B
+%0A    private static final int DEFAULT_SIZE = 200;
 %0A%0A    @O
@@ -2267,24 +2267,369 @@
 %0A    %7D%0A    %0A
+    @Override%0A    public void onDeleted(Context context, int%5B%5D appWidgetIds) %7B%0A        //Remove them from the database%0A        for(int id: appWidgetIds)%7B%0A            Log.d(%22Deleting widget %22+id);%0A            DatabaseManager.getInstance(context).deleteWidgetInfo(id);%0A        %7D%0A        %0A        super.onDeleted(context, appWidgetIds);%0A    %7D%0A    %0A
     /**%0A    
@@ -3147,15 +3147,33 @@
 map(
-200,200
+DEFAULT_SIZE,DEFAULT_SIZE
 ,con
@@ -4186,73 +4186,189 @@
     
-RectF smallbox = new RectF(100 - 20, 100 - 20, 100 + 20, 100 + 20
+int middle = bmp.getWidth() / 2;%0A        int padding = bmp.getWidth() / 10;%0A        RectF smallbox = new RectF(middle - padding, middle - padding, middle + padding, middle + padding
 );%0A 

Make size a static variable, clean up deleted widgets