public class MyWidgetProvider extends AppWidgetProvider{
public static final String TOAST_ACTION = "com.zgc.listwidget.TOAST_ACTION" ;
public static final String EXTRA_ITEM = "com.zgc.listwidget.EXTRA_ITEM" ;
public static final String TO_SITE = "com.zgc.listwidget.TO_SITE" ;
public static final String SITE = "com.zgc.listwidget.SITE" ;
public static final String BACK_HOME = "com.zgc.listwidget.BACK_HOME" ;
public static String PicName = "" ;
public static final String REFRESH = "com.zgc.listwidget.REFRESH" ;
public static final String ITEM = "com.zgc.AppWidget6.ITEM" ;
@Override
public IBinder peekService(Context myContext, Intent service) {
// TODO Auto-generated method stub
System.out.println( "peekService in provider" );
return super .peekService(myContext, service);
}
@Override
public void onDeleted(Context context, int [] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println( "onDeleted in Provider" );
super .onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
System.out.println( "onDisabled in Provider" );
super .onDisabled(context);
}
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
System.out.println( "onEnabled in Provider" );
super .onEnabled(context);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
ComponentName cmpName = new ComponentName(context, MyWidgetProvider. class );
if (intent.getAction().equals(ITEM)) {
System.out.println( "item action" );
int pageNum = intent.getIntExtra( "page" , 1 );
int itemPos = intent.getIntExtra(EXTRA_ITEM, 0 );
if ( 0 == pageNum){
System.out.println( "item action 0 page" );
ListRemoteViewsFactory.secPageId = - 1 ;
ListRemoteViewsFactory.whichPage = ListRemoteViewsFactory.secPage;
int [] appIds = mgr.getAppWidgetIds(cmpName);
mgr.notifyAppWidgetViewDataChanged(appIds, R.id.myListView);
//change refresh to commit button ,here no need to reload listview
RemoteViews rv2 = new RemoteViews(context.getPackageName(), R.layout.listview);
rv2.setTextViewText(R.id.refresh, "commit" );
Intent commitIntent = new Intent(context, MyWidgetProvider. class );
commitIntent.setData(Uri.parse(commitIntent.toUri(Intent.URI_INTENT_SCHEME)));
commitIntent.setAction(SITE);
PendingIntent commitPendingIntent = PendingIntent.getBroadcast(context, 0 ,
commitIntent, PendingIntent.FLAG_UPDATE_CURRENT);
rv2.setOnClickPendingIntent(R.id.refresh, commitPendingIntent);
mgr.updateAppWidget(appIds, rv2);
}
else if ( 1 == pageNum){
System.out.println( "item action 1 page" );
ListRemoteViewsFactory.secPageId = itemPos;
if (- 1 == ListRemoteViewsFactory.checkPos.indexOf(itemPos)){
ListRemoteViewsFactory.checkPos.add(itemPos);
}
else {
ListRemoteViewsFactory.checkPos.remove(ListRemoteViewsFactory.checkPos.indexOf(itemPos));
}
int [] appIds = mgr.getAppWidgetIds(cmpName);
mgr.notifyAppWidgetViewDataChanged(appIds, R.id.myListView);
}
}
else if (intent.getAction().equals(SITE)){
System.out.println( "in receive commit SITE action" );
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.img);
int id = R.drawable.uliuli;
if (PicName.equals( "google" )){
id = R.drawable.uliuli;
}
else if (PicName.equals( "douban" )){
id = R.drawable.uliuli;
}
rv.setImageViewResource(R.id.displayImage, id);
Intent homeIntent = new Intent(context, MyWidgetProvider. class );
homeIntent.setAction(BACK_HOME);
//homeIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
homeIntent.setData(Uri.parse(homeIntent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0 , homeIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
rv.setOnClickPendingIntent(R.id.backHome, pendingIntent);
mgr.updateAppWidget(cmpName, rv);
//Toast.makeText(context, "Touched view zhang", Toast.LENGTH_SHORT).show();
}
else if (intent.getAction().equals(BACK_HOME)){
System.out.println( "back_home " );
int [] appWidgetIds = mgr.getAppWidgetIds(cmpName);
//mgr.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.myListView);
Intent serviceIntent = new Intent(context, MyWidgetService. class ); //
//intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.listview);
rv.setRemoteAdapter(R.id.myListView, serviceIntent);
Intent toastIntent = new Intent(context, MyWidgetProvider. class );
toastIntent.setAction(MyWidgetProvider.ITEM);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0 , toastIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.myListView, toastPendingIntent);
mgr.updateAppWidget(appWidgetIds, rv);
Intent refreshIntent = new Intent(context, MyWidgetProvider. class );
refreshIntent.setAction(REFRESH);
PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0 ,
refreshIntent, 0 );
rv.setOnClickPendingIntent(R.id.refresh, refreshPendingIntent);
ListRemoteViewsFactory.whichPage = ListRemoteViewsFactory.mainPage;
ListRemoteViewsFactory.mainPageId = - 1 ;
mgr.updateAppWidget(cmpName, rv);
mgr.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.myListView);
System.out.println( "zhanggui" );
//Toast.makeText(context, "Touched view back home", Toast.LENGTH_SHORT).show();
}
else if (intent.getAction().equals(REFRESH)){
System.out.println( "refresh button begin" );
int [] appWidgetIds = mgr.getAppWidgetIds(cmpName);
mgr.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.myListView);
System.out.println( "refresh button end" );
}
super .onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int [] appWidgetIds) {
System.out.println( "onUpdate" );
// TODO Auto-generated method stub
for ( int i = 0 ; i < appWidgetIds.length; i++){
Intent intent = new Intent(context, MyWidgetService. class );
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.listview);
rv.setRemoteAdapter(R.id.myListView, intent);
//rv.setEmptyView(R.id.myListView, R.id.empty);
Intent refreshIntent = new Intent(context, MyWidgetProvider. class );
refreshIntent.setAction(REFRESH);
PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0 ,
refreshIntent, 0 );
rv.setOnClickPendingIntent(R.id.refresh, refreshPendingIntent);
Intent toastIntent = new Intent(context, MyWidgetProvider. class );
toastIntent.setAction(MyWidgetProvider.ITEM);
//toastIntent.putExtra("page", 0); // main page
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0 , toastIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.myListView, toastPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
super .onUpdate(context, appWidgetManager, appWidgetIds);
}
}
|