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