package com.tortel.deploytrack.provider;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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.ComponentName;
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) {

        // Get all ids
        ComponentName thisWidget = new ComponentName(context,
                WidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        
        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());
                
                printDeploymentInfo(info);
                //Draw everything
                remoteViews = updateWidgetView(context, remoteViews, info);
            } else {
                Log.d("Widget "+widgetId+" has null info");
            }
            
            //Update it
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }
    
    private void printDeploymentInfo(WidgetInfo info){
        Deployment deployment = info.getDeployment();
        Log.d("Deployment toString:"+deployment);
        Class<?> c = deployment.getClass();
        for(Method method : c.getDeclaredMethods()){
            if(method.getName().startsWith("get") && method.getParameterTypes().length == 0){
                try {
                    Log.d(method.getName()+": "+method.invoke(deployment, new Object[0]));
                } catch (Exception e) {
                    //Whatever, yo
                }
            }
        }
    }
    
    /**
     * 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;
    }
}

@@ -1,129 +1,639 @@
-package com.tortel.deploytrack.provider;%0A%0Aimport java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method
+/*%0A * Copyright (C) 2013 Scott Warner%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage com.tortel.deploytrack.provider
 ;%0A%0Ai
@@ -863,46 +863,8 @@
 er;%0A
-import android.content.ComponentName;%0A
 impo
@@ -1387,208 +1387,8 @@
 s) %7B
-%0A%0A        // Get all ids%0A        ComponentName thisWidget = new ComponentName(context,%0A                WidgetProvider.class);%0A        int%5B%5D allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
 %0A   
@@ -1893,67 +1893,8 @@
 ));%0A
-                %0A                printDeploymentInfo(info);
 %0A   
@@ -2226,599 +2226,8 @@
    %0A
-    private void printDeploymentInfo(WidgetInfo info)%7B%0A        Deployment deployment = info.getDeployment();%0A        Log.d(%22Deployment toString:%22+deployment);%0A        Class%3C?%3E c = deployment.getClass();%0A        for(Method method : c.getDeclaredMethods())%7B%0A            if(method.getName().startsWith(%22get%22) && method.getParameterTypes().length == 0)%7B%0A                try %7B%0A                    Log.d(method.getName()+%22: %22+method.invoke(deployment, new Object%5B0%5D));%0A                %7D catch (Exception e) %7B%0A                    //Whatever, yo%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A    %0A
     

Remove hardcore debugging code, add copyright/license