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

http://eyes-free.googlecode.com/ · Java · 179 lines · 133 code · 30 blank · 16 comment · 22 complexity · 8c5bdac399155f68245bcd2c8bc965ab MD5 · raw file

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