/paw/src/com/google/marvin/paw/WidgetInterface.java
Java | 179 lines | 133 code | 30 blank | 16 comment | 22 complexity | 8c5bdac399155f68245bcd2c8bc965ab MD5 | raw file
1package com.google.marvin.paw; 2 3import java.io.UnsupportedEncodingException; 4import java.net.URLEncoder; 5 6import org.htmlparser.parserapplications.StringExtractor; 7import org.htmlparser.util.ParserException; 8 9import android.app.PendingIntent; 10import android.app.Service; 11import android.appwidget.AppWidgetManager; 12import android.appwidget.AppWidgetProvider; 13import android.content.ComponentName; 14import android.content.Context; 15import android.content.Intent; 16import android.content.ServiceConnection; 17import android.content.res.Resources; 18import android.graphics.Bitmap; 19import android.graphics.BitmapFactory; 20import android.net.Uri; 21import android.os.IBinder; 22import android.util.Log; 23import android.widget.RemoteViews; 24import android.widget.Toast; 25 26public class WidgetInterface extends AppWidgetProvider { 27 Context ctx; 28 29 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 30 ctx = context; 31 // To prevent any ANR timeouts, we perform the update in a service 32 context.startService(new Intent(context, UpdateService.class)); 33 } 34 35 public void onReceive(Context context, Intent intent) { 36 super.onReceive(context, intent); 37 38 ctx = context; 39 40 if (intent.getAction().indexOf("com.google.marvin.paw.idle") != -1) { 41 RemoteViews myViews = buildUpdate(context); 42 Resources res = context.getResources(); 43 44 myViews.setImageViewResource(R.id.image, R.drawable.android_idle_anim); 45 46 ComponentName thisWidget = new ComponentName(context, WidgetInterface.class); 47 AppWidgetManager manager = AppWidgetManager.getInstance(context); 48 manager.updateAppWidget(thisWidget, myViews); 49 } else if (intent.getAction().indexOf("com.google.marvin.paw.dizzy") != -1) { 50 RemoteViews myViews = buildUpdate(context); 51 Resources res = context.getResources(); 52 53 myViews.setImageViewResource(R.id.image, R.drawable.android_dizzy_anim); 54 55 ComponentName thisWidget = new ComponentName(context, WidgetInterface.class); 56 AppWidgetManager manager = AppWidgetManager.getInstance(context); 57 manager.updateAppWidget(thisWidget, myViews); 58 } else if (intent.getAction().indexOf("com.google.marvin.paw.dance") != -1) { 59 RemoteViews myViews = buildUpdate(context); 60 Resources res = context.getResources(); 61 62 myViews.setImageViewResource(R.id.image, R.drawable.android_dance_anim); 63 64 ComponentName thisWidget = new ComponentName(context, WidgetInterface.class); 65 AppWidgetManager manager = AppWidgetManager.getInstance(context); 66 manager.updateAppWidget(thisWidget, myViews); 67 } else if (intent.getAction().indexOf("com.google.marvin.paw.sleep") != -1) { 68 RemoteViews myViews = buildUpdate(context); 69 Resources res = context.getResources(); 70 71 myViews.setImageViewResource(R.id.image, R.drawable.sleep_anim); 72 73 ComponentName thisWidget = new ComponentName(context, WidgetInterface.class); 74 AppWidgetManager manager = AppWidgetManager.getInstance(context); 75 manager.updateAppWidget(thisWidget, myViews); 76 } else if (intent.getAction().indexOf("com.google.marvin.paw.action.websearch") != -1) { 77 // getSearchResults("weather san jose"); 78 getSearchResults(intent.getStringExtra("query")); 79 } 80 } 81 82 /** 83 * Build the widget here. Will block until the online API returns. 84 */ 85 public static RemoteViews buildUpdate(Context context) { 86 RemoteViews updateViews = null; 87 updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_contents_2x2); 88 loadWidget(context, updateViews); 89 return updateViews; 90 } 91 92 public static boolean loadWidget(Context context, RemoteViews targetRemoteView) { 93 // Hard code this for now... 94 // TODO: Draw widget here 95 Resources res = context.getResources(); 96 97 Intent i = new Intent("com.google.marvin.paw.startService"); 98 PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); 99 // targetRemoteView.setOnClickPendingIntent(R.id.button00, pi); 100 101 targetRemoteView.setOnClickPendingIntent(R.id.image, pi); 102 return true; 103 } 104 105 public static class UpdateService extends Service { 106 @Override 107 public void onStart(Intent intent, int startId) { 108 // Build the widget update for today 109 RemoteViews updateViews = buildUpdate(this); 110 111 // Push update for this widget to the home screen 112 ComponentName thisWidget = new ComponentName(this, WidgetInterface.class); 113 AppWidgetManager manager = AppWidgetManager.getInstance(this); 114 manager.updateAppWidget(thisWidget, updateViews); 115 } 116 117 @Override 118 public IBinder onBind(Intent intent) { 119 // We don't need to bind to this service 120 return null; 121 } 122 } 123 124 public void getSearchResults(String query) { 125 (new Thread(new searchResultsFetcher(query))).start(); 126 } 127 128 class searchResultsFetcher implements Runnable { 129 String q; 130 131 public searchResultsFetcher(String query) { 132 q = query; 133 } 134 135 public void run() { 136 // Intent i = new Intent("android.intent.action.VIEW", 137 // Uri.parse("http://www.google.com/m?q=" + q)); 138 // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 139 // ctx.startActivity(i); 140 141 String contents = OneBoxScraper.processGoogleResults(q); 142 if (contents.length() > 0) { 143 if (contents.indexOf("PAW_MAPS:") == 0) { 144 Intent mapsIntent = new Intent("android.intent.action.VIEW"); 145 mapsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 146 mapsIntent.setClassName("com.google.android.apps.maps", 147 "com.google.android.maps.MapsActivity"); 148 mapsIntent.setData(Uri.parse("http://maps.google.com/?q=" + contents.substring(9))); 149 ctx.startActivity(mapsIntent); 150 } else if (contents.indexOf("PAW_YOUTUBE:") == 0) { 151 Intent ytIntent = new Intent("android.intent.action.VIEW"); 152 ytIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 153 ytIntent.setClassName("com.google.android.youtube", 154 "com.google.android.youtube.PlayerActivity"); 155 ytIntent.setData(Uri.parse(contents.substring(12))); 156 ctx.startActivity(ytIntent); 157 } else { 158 Intent synthIntent = new Intent("com.google.marvin.paw.doSynth"); 159 synthIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 160 synthIntent.putExtra("message", contents); 161 ctx.startActivity(synthIntent); 162 } 163 } else { 164 try { 165 Intent i = 166 new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com/m?q=" 167 + URLEncoder.encode(q, "UTF-8"))); 168 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 169 ctx.startActivity(i); 170 } catch (UnsupportedEncodingException e) { 171 // TODO Auto-generated catch block 172 e.printStackTrace(); 173 } 174 } 175 176 } 177 } 178 179}