Guest User

Untitled

a guest
Jan 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. @Override
  2. public void onReceive(Context context, Intent intent) {
  3. AppWidgetManager mgr = AppWidgetManager.getInstance(context);
  4. if (intent.getAction().equals(OPEN_DETAILS)) {
  5. //We receive the Id of item which has been clicked
  6. int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
  7. int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0);
  8. Toast.makeText(context, "Touched view " + viewIndex, Toast.LENGTH_SHORT).show();
  9. // Now, we want receive all information of the item : titre, url, description, ...
  10. // ????
  11. // ????
  12.  
  13. //And after, we want to start an activity with all information of the item which has been clicked and print them in the activity
  14. Intent DetailDeal = new Intent(Intent.ACTION_VIEW);
  15. DetailDeal.setClassName("com.example.android.stackwidget", "com.example.android.stackwidget.AddDealActivity");
  16. DetailDeal.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  17. context.startActivity(DetailDeal);
  18.  
  19. }
  20. super.onReceive(context, intent);
  21. }
  22.  
  23.  
  24. @Override
  25. public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  26.  
  27. // update each of the widgets with the remote adapter
  28. for (int i = 0; i < appWidgetIds.length; ++i) {
  29.  
  30. // Here we setup the intent which points to the StackViewService which will
  31. // provide the views for this collection.
  32. Intent intent = new Intent(context, StackWidgetService.class);
  33. intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
  34. // When intents are compared, the extras are ignored, so we need to embed the extras
  35. // into the data so that the extras will not be ignored.
  36. intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
  37. RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout_white_theme);
  38. rv.setRemoteAdapter(appWidgetIds[i], R.id.stack_view, intent);
  39.  
  40. // The empty view is displayed when the collection has no items. It should be a sibling
  41. // of the collection view.
  42. rv.setEmptyView(R.id.stack_view, R.id.empty_view);
  43.  
  44. // Here we setup the a pending intent template. Individuals items of a collection
  45. // cannot setup their own pending intents, instead, the collection as a whole can
  46. // setup a pending intent template, and the individual items can set a fillInIntent
  47. // to create unique before on an item to item basis.
  48. Intent toastIntent = new Intent(context, StackWidgetProvider.class);
  49. toastIntent.setAction(StackWidgetProvider.OPEN_DETAILS);
  50. toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
  51. intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
  52. PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  53. rv.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);
  54.  
  55. appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
  56. }
  57. super.onUpdate(context, appWidgetManager, appWidgetIds);
  58. }
  59.  
  60. public RemoteViews getViewAt(int position) {
  61. // position will always range from 0 to getCount() - 1.
  62.  
  63. // We construct a remote views item based on our widget item xml file, and set the
  64. // text based on the position.
  65.  
  66. WidgetItem item = mWidgetItems.get(position);
  67. // We construct a remote views item based on our widget item xml file, and set the
  68. // text based on the position.
  69. RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item_white_theme);
  70. rv.setTextViewText(R.id.titre, item.titre);
  71. rv.setTextViewText(R.id.description, item.description);
  72. rv.setTextViewText(R.id.url, "@"+item.site.toString());
  73. rv.setTextViewText(R.id.ancienprix, item.ancienprix);
  74. rv.setTextViewText(R.id.nouveauprix, item.nouveauPrix);
  75.  
  76. //On barre l'ancien prix
  77. rv.setInt(R.id.ancienprix, "setPaintFlags", Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
  78. //On positionne la bande à gauche de l'item. Verte si le deal est encore actif, rouge sinon
  79. rv.setInt(R.id.dealtermine, "setBackgroundColor", item.dealTermine ? android.graphics.Color.rgb(204, 0, 0) : android.graphics.Color.rgb(102, 153, 0));
  80.  
  81. //On positionne l'image de l'item suivant le degré du deal
  82. if (item.qualite == "chaud") { //C'est un très bon deal
  83. rv.setImageViewResource(R.id.img, R.drawable.ic_action_lab_black);
  84. }
  85. else if (item.qualite == "moyen") { //c'est un deal moyen
  86. rv.setImageViewResource(R.id.img, R.drawable.ic_action_lab_red);
  87. }
  88. else if (item.qualite == "froid") { //c'est un deal bof
  89. rv.setImageViewResource(R.id.img, R.drawable.ic_action_lab_green);
  90. }
  91.  
  92. // Next, we set a fill-intent which will be used to fill-in the pending intent template
  93. // which is set on the collection view in StackWidgetProvider.
  94. Bundle extras = new Bundle();
  95. extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
  96. Intent fillInIntent = new Intent();
  97. fillInIntent.putExtras(extras);
  98. rv.setOnClickFillInIntent(R.id.group_layout, fillInIntent);
  99.  
  100. // You can do heaving lifting in here, synchronously. For example, if you need to
  101. // process an image, fetch something from the network, etc., it is ok to do it here,
  102. // synchronously. A loading view will show up in lieu of the actual contents in the
  103. // interim.
  104. try {
  105. System.out.println("Loading view " + position);
  106. Thread.sleep(500);
  107. } catch (InterruptedException e) {
  108. e.printStackTrace();
  109. }
  110.  
  111. // Return the remote views object.
  112. return rv;
  113. }
Add Comment
Please, Sign In to add comment