PageRenderTime 49ms CodeModel.GetById 21ms app.highlight 23ms RepoModel.GetById 2ms app.codeStats 0ms

/androidsays/src/com/google/marvin/androidsays/WidgetInterface.java

http://eyes-free.googlecode.com/
Java | 135 lines | 110 code | 17 blank | 8 comment | 19 complexity | 019f7fa50a16c5ff2764360c0ed6de6b MD5 | raw file
  1
  2package com.google.marvin.androidsays;
  3
  4import android.app.PendingIntent;
  5import android.app.Service;
  6import android.appwidget.AppWidgetManager;
  7import android.appwidget.AppWidgetProvider;
  8import android.content.ComponentName;
  9import android.content.Context;
 10import android.content.Intent;
 11import android.content.ServiceConnection;
 12import android.content.res.Resources;
 13import android.graphics.Bitmap;
 14import android.graphics.BitmapFactory;
 15import android.os.IBinder;
 16import android.util.Log;
 17import android.widget.RemoteViews;
 18import android.widget.Toast;
 19
 20public class WidgetInterface extends AppWidgetProvider {
 21    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
 22        Log.e("widget", "onUpdate");
 23
 24        // To prevent any ANR timeouts, we perform the update in a service
 25        context.startService(new Intent(context, UpdateService.class));
 26    }
 27
 28    public void onReceive(Context context, Intent intent) {
 29        super.onReceive(context, intent);
 30
 31        if (intent.getAction().indexOf("com.google.marvin.androidsays.flash.0") != -1) {
 32            RemoteViews myViews = buildUpdate(context);
 33            Resources res = context.getResources();
 34
 35            if (myViews != null) {
 36                int buttonNumber = Integer.parseInt(intent.getAction().replaceAll(
 37                        "com.google.marvin.androidsays.flash.0", ""));
 38                Bitmap theImage = BitmapFactory.decodeResource(res, R.drawable.mini_flash);
 39                theImage = Bitmap.createBitmap(theImage);
 40                if (buttonNumber == 0) {
 41                    myViews.setImageViewBitmap(R.id.button00, theImage);
 42                } else if (buttonNumber == 1) {
 43                    myViews.setImageViewBitmap(R.id.button01, theImage);
 44                } else if (buttonNumber == 2) {
 45                    myViews.setImageViewBitmap(R.id.button02, theImage);
 46                } else {
 47                    myViews.setImageViewBitmap(R.id.button03, theImage);
 48                }
 49            }
 50            ComponentName thisWidget = new ComponentName(context, WidgetInterface.class);
 51            AppWidgetManager manager = AppWidgetManager.getInstance(context);
 52            manager.updateAppWidget(thisWidget, myViews);
 53        } else if (intent.getAction().indexOf("com.google.marvin.androidsays.unflash") != -1){
 54            Log.e("Widget", "unflash started");
 55            RemoteViews myViews = buildUpdate(context);
 56            Resources res = context.getResources();
 57            myViews.setImageViewResource(R.id.button00, R.drawable.mini_green);
 58            myViews.setImageViewResource(R.id.button01, R.drawable.mini_red);
 59            myViews.setImageViewResource(R.id.button02, R.drawable.mini_yellow);
 60            myViews.setImageViewResource(R.id.button03, R.drawable.mini_blue);  
 61            ComponentName thisWidget = new ComponentName(context, WidgetInterface.class);
 62            AppWidgetManager manager = AppWidgetManager.getInstance(context);
 63            manager.updateAppWidget(thisWidget, myViews);
 64            Log.e("Widget", "unflash finished");
 65        } else if (intent.getAction().indexOf("com.google.marvin.androidsays.showScore") != -1){            
 66            Toast.makeText(context, "Your score is: " + intent.getStringExtra("score"), 1).show();
 67        }
 68    }
 69
 70    /**
 71     * Build the widget here. Will block until the online API returns.
 72     */
 73    public static RemoteViews buildUpdate(Context context) {
 74        RemoteViews updateViews = null;
 75        updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_contents_2x2);
 76        loadWidget(context, updateViews);
 77        return updateViews;
 78    }
 79
 80    public static boolean loadWidget(Context context, RemoteViews targetRemoteView) {
 81        // Hard code this for now...
 82        Resources res = context.getResources();
 83
 84        Bitmap theImage = BitmapFactory.decodeResource(res, R.drawable.mini_green);
 85        theImage = Bitmap.createBitmap(theImage);
 86        targetRemoteView.setImageViewBitmap(R.id.button00, theImage);
 87        Intent i = new Intent("com.google.marvin.androidsays.00");
 88        PendingIntent pi = PendingIntent.getActivity(context, 0, i,
 89                PendingIntent.FLAG_UPDATE_CURRENT);
 90        targetRemoteView.setOnClickPendingIntent(R.id.button00, pi);
 91
 92        theImage = BitmapFactory.decodeResource(res, R.drawable.mini_red);
 93        theImage = Bitmap.createBitmap(theImage);
 94        targetRemoteView.setImageViewBitmap(R.id.button01, theImage);
 95        i = new Intent("com.google.marvin.androidsays.01");
 96        pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
 97        targetRemoteView.setOnClickPendingIntent(R.id.button01, pi);
 98
 99        theImage = BitmapFactory.decodeResource(res, R.drawable.mini_yellow);
100        theImage = Bitmap.createBitmap(theImage);
101        targetRemoteView.setImageViewBitmap(R.id.button02, theImage);
102        i = new Intent("com.google.marvin.androidsays.02");
103        pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
104        targetRemoteView.setOnClickPendingIntent(R.id.button02, pi);
105
106        theImage = BitmapFactory.decodeResource(res, R.drawable.mini_blue);
107        theImage = Bitmap.createBitmap(theImage);
108        targetRemoteView.setImageViewBitmap(R.id.button03, theImage);
109        i = new Intent("com.google.marvin.androidsays.03");
110        pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
111        targetRemoteView.setOnClickPendingIntent(R.id.button03, pi);
112
113        return true;
114    }
115
116    public static class UpdateService extends Service {
117        @Override
118        public void onStart(Intent intent, int startId) {
119            Log.e("widget", "0");
120            // Build the widget update for today
121            RemoteViews updateViews = buildUpdate(this);
122
123            // Push update for this widget to the home screen
124            ComponentName thisWidget = new ComponentName(this, WidgetInterface.class);
125            AppWidgetManager manager = AppWidgetManager.getInstance(this);
126            manager.updateAppWidget(thisWidget, updateViews);
127        }
128
129        @Override
130        public IBinder onBind(Intent intent) {
131            // We don't need to bind to this service
132            return null;
133        }
134    }
135}