1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
import java.util.ArrayList;
import java.util.List;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
public class StackWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
}
}
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private List<WidgetItem> mWidgetItems = new ArrayList<WidgetItem>();
private Context mContext;
private int mAppWidgetId;
public Boolean isFinished = false;
ImageButton btnDisplaySettings;
public StackRemoteViewsFactory(Context context, Intent intent) {
mContext = context;
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
public void onCreate() {
// In onCreate() you setup any connections / cursors to your data source. Heavy lifting,
// for example downloading or creating content etc, should be deferred to onDataSetChanged()
// or getViewAt(). Taking more than 20 seconds in this call will result in an ANR.
mWidgetItems.add(new WidgetItem("90% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "19,90", "8,99", true, "froid"));
mWidgetItems.add(new WidgetItem("80% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", false, "chaud"));
mWidgetItems.add(new WidgetItem("60% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", false, "chaud"));
mWidgetItems.add(new WidgetItem("50% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", true, "moyen"));
mWidgetItems.add(new WidgetItem("40% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", false, "froid"));
mWidgetItems.add(new WidgetItem("30% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", true, "moyen"));
mWidgetItems.add(new WidgetItem("20% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", true, "chaud"));
mWidgetItems.add(new WidgetItem("10% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", true, "chaud"));
mWidgetItems.add(new WidgetItem("90% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", false, "froid"));
mWidgetItems.add(new WidgetItem("80% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", true, "chaud"));
mWidgetItems.add(new WidgetItem("70% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", true, "froid"));
mWidgetItems.add(new WidgetItem("60% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", false, "moyen"));
mWidgetItems.add(new WidgetItem("50% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", true, "froid"));
mWidgetItems.add(new WidgetItem("40% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", true, "chaud"));
mWidgetItems.add(new WidgetItem("30% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", false, "moyen"));
mWidgetItems.add(new WidgetItem("20% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", true, "froid"));
mWidgetItems.add(new WidgetItem("10% de réduction produits Slazenger 5.99", "Code de réduction : 52154", "rueducommerce", "10", "20", false, "chaud"));
// We sleep for 3 seconds here to show how the empty view appears in the interim.
// The empty view is set in the StackWidgetProvider and should be a sibling of the
// collection view.
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void onDestroy() {
// In onDestroy() you should tear down anything that was setup for your data source,
// eg. cursors, connections, etc.
mWidgetItems.clear();
}
public int getCount() {
return mWidgetItems.size();
}
public RemoteViews getViewAt(int position) {
// position will always range from 0 to getCount() - 1.
// We construct a remote views item based on our widget item xml file, and set the
// text based on the position.
WidgetItem item = mWidgetItems.get(position);
// We construct a remote views item based on our widget item xml file, and set the
// text based on the position.
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item_white_theme);
rv.setTextViewText(R.id.titre, item.titre);
rv.setTextViewText(R.id.description, item.description);
rv.setTextViewText(R.id.url, "@"+item.site.toString());
rv.setTextViewText(R.id.ancienprix, item.ancienprix);
rv.setTextViewText(R.id.nouveauprix, item.nouveauPrix);
//On barre l'ancien prix
rv.setInt(R.id.ancienprix, "setPaintFlags", Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
//On positionne la bande à gauche de l'item. Verte si le deal est encore actif, rouge sinon
rv.setInt(R.id.dealtermine, "setBackgroundColor", item.dealTermine ? android.graphics.Color.rgb(204, 0, 0) : android.graphics.Color.rgb(102, 153, 0));
//On positionne l'image de l'item suivant le degré du deal
if (item.qualite == "chaud") { //C'est un très bon deal
rv.setImageViewResource(R.id.img, R.drawable.ic_action_lab_black);
}
else if (item.qualite == "moyen") { //c'est un deal moyen
rv.setImageViewResource(R.id.img, R.drawable.ic_action_lab_red);
}
else if (item.qualite == "froid") { //c'est un deal bof
rv.setImageViewResource(R.id.img, R.drawable.ic_action_lab_green);
}
// Next, we set a fill-intent which will be used to fill-in the pending intent template
// which is set on the collection view in StackWidgetProvider.
Bundle extras = new Bundle();
extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.group_layout, fillInIntent);
// You can do heaving lifting in here, synchronously. For example, if you need to
// process an image, fetch something from the network, etc., it is ok to do it here,
// synchronously. A loading view will show up in lieu of the actual contents in the
// interim.
try {
System.out.println("Loading view " + position);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Return the remote views object.
return rv;
}
public RemoteViews getLoadingView() {
// You can create a custom loading view (for instance when getViewAt() is slow.) If you
// return null here, you will get the default loading view.
return null;
}
public int getViewTypeCount() {
return 1;
}
public long getItemId(int position) {
return position;
}
public boolean hasStableIds() {
return true;
}
public void onDataSetChanged() {
// This is triggered when you call AppWidgetManager notifyAppWidgetViewDataChanged
// on the collection view corresponding to this factory. You can do heaving lifting in
// here, synchronously. For example, if you need to process an image, fetch something
// from the network, etc., it is ok to do it here, synchronously. The widget will remain
// in its current state while work is being done here, so you don't need to worry about
// locking up the widget.
}
} |
Partager