Back to project page twawm2.
The source code is released under:
Copyright (c) 2014, afnf All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistr...
If you think the Android project twawm2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.appspot.afnf4199ga.twawm.app; /* w w w .j av a 2s. c om*/ import net.afnf.and.twawm2.R; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.net.wifi.WifiManager; import android.widget.RemoteViews; import com.appspot.afnf4199ga.twawm.Const; import com.appspot.afnf4199ga.twawm.StateMachine; import com.appspot.afnf4199ga.twawm.TwawmUtils; import com.appspot.afnf4199ga.utils.AndroidUtils; public class DefaultWidgetProvider extends AppWidgetProvider { private static final long MIN_UPDATE_INTERVAL = 1000; private static long lastUpdated = -1; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Logger.v("Widget onUpdate"); super.onUpdate(context, appWidgetManager, appWidgetIds); // ??????????????? BackgroundService service = BackgroundService.getInstance(); if (service == null) { // ???????????????????????????? // Wifi????????????????? WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); boolean wifi_disabled = AndroidUtils.isWifiDisabled(wifi); // WifiOFF?????????????? RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_default); rv.setImageViewResource(R.id.widgetImage, wifi_disabled ? R.drawable.icon_wimax_gray_batt_na : R.drawable.icon_wimax_white_batt_na); rv.setTextViewText(R.id.widgetText, context.getString(wifi_disabled ? R.string.wifi_off : R.string.processing)); // updateAppWidget for (int id : appWidgetIds) { setClickIntent(context, id, rv); appWidgetManager.updateAppWidget(id, rv); } } // ??????????????? else { // ?????? StateMachine stateMachine = service.getStateMachine(); String wdText = stateMachine.getWdText(); int wdImageId = stateMachine.getWdImageId(); RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_default); if (wdImageId > 0) { rv.setImageViewResource(R.id.widgetImage, wdImageId); } if (wdText != null) { rv.setTextViewText(R.id.widgetText, wdText); } // updateAppWidget for (int id : appWidgetIds) { setClickIntent(context, id, rv); appWidgetManager.updateAppWidget(id, rv); } } } @Override public void onReceive(Context context, Intent intent) { // Logger.v("Widget onReceive intent=" + AndroidUtils.getActionForLog(intent)); // ???????????? super.onReceive(context, intent); boolean forceUpdate = false; long now = System.currentTimeMillis(); if (now >= lastUpdated + MIN_UPDATE_INTERVAL) { lastUpdated = now; forceUpdate = true; } // ?? AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); int[] appWidgetIds = getWidgetIds(appWidgetManager, context); // ACTION_APPWIDGET_UPDATE(super.onReceive????onUpdate?????????)?????????????????????? // ?????????INTENT_WD_CHANGE_STYLE // ?????????INTENT_WD_UPDATE boolean update = (AndroidUtils.isActionEquals(intent, AppWidgetManager.ACTION_APPWIDGET_UPDATE) == false && forceUpdate) || AndroidUtils.isActionEquals(intent, Const.INTENT_WD_STYLE_CHANGED) || AndroidUtils.isActionEquals(intent, Const.INTENT_WD_UPDATED); // onUpdate????? if (update) { onUpdate(context, appWidgetManager, appWidgetIds); } // Style?? if (forceUpdate || AndroidUtils.isActionEquals(intent, Const.INTENT_WD_STYLE_CHANGED)) { // Logger.v("Widget style changed"); RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_default); int tc = TwawmUtils.getValue2ColorIndex(context, Const.getPrefWidgetStrColor(context)); rv.setTextColor(R.id.widgetText, context.getResources().getColor(tc)); int[] bg = TwawmUtils.getValue2ResourceIndex(context, Const.getPrefWidgetBackground(context)); rv.setInt(R.id.widgetDefaultLayout, "setBackgroundResource", bg[0]); // updateAppWidget for (int id : appWidgetIds) { setClickIntent(context, id, rv); appWidgetManager.updateAppWidget(id, rv); } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void setClickIntent(Context context) { AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName thisAppWidget = new ComponentName(context.getPackageName(), DefaultWidgetProvider.class.getName()); int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget); for (int id : appWidgetIds) { RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_default); setClickIntent(context, id, rv); } } public static void setClickIntent(Context context, int appWidgetId, RemoteViews rv) { Intent intent = new Intent(context, BackgroundService.class); intent.setAction(Const.INTENT_WD_CLICKED); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); //intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); // ??? PendingIntent pendingIntent = PendingIntent.getService(context, appWidgetId, intent, 0); // putExtra???????????2??(requestCode)?????????? // rv.setOnClickPendingIntent(R.id.widgetImage, pendingIntent); // ??????????? // rv.setOnClickPendingIntent(R.id.widgetText, pendingIntent); // ??????????? rv.setOnClickPendingIntent(R.id.widgetDefaultLayout, pendingIntent); } public static void showClickAnimation(Context context, int widgetId) { ClickAnimateThread thread = new ClickAnimateThread(); thread.context = context; thread.widgetId = widgetId; thread.start(); } static class ClickAnimateThread extends Thread { Context context; int widgetId; @Override public void run() { try { String bgstr = Const.getPrefWidgetBackground(context); int[] bg = TwawmUtils.getValue2ResourceIndex(context, bgstr); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_default); rv.setInt(R.id.widgetDefaultLayout, "setBackgroundResource", bg[1]); appWidgetManager.updateAppWidget(widgetId, rv); AndroidUtils.sleep(250); rv.setInt(R.id.widgetDefaultLayout, "setBackgroundResource", bg[0]); setClickIntent(context, widgetId, rv); appWidgetManager.updateAppWidget(widgetId, rv); } catch (Throwable e) { // do nothing } } } public static void update(Context context) { Intent intent = new Intent(context, DefaultWidgetProvider.class); intent.setAction(Const.INTENT_WD_UPDATED); context.sendBroadcast(intent); } public static void changeStyle(Context context) { Intent intent = new Intent(context, DefaultWidgetProvider.class); intent.setAction(Const.INTENT_WD_STYLE_CHANGED); context.sendBroadcast(intent); } public static boolean hasWidget(Context context) { AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); int[] appWidgetIds = getWidgetIds(appWidgetManager, context); return (appWidgetIds == null || appWidgetIds.length == 0) == false; } private static int[] getWidgetIds(AppWidgetManager appWidgetManager, Context context) { ComponentName thisAppWidget = new ComponentName(context.getPackageName(), DefaultWidgetProvider.class.getName()); int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget); return appWidgetIds; } public static void updateAsWorkingOrPausing(Context context, boolean working) { AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName thisAppWidget = new ComponentName(context.getPackageName(), DefaultWidgetProvider.class.getName()); int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget); RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_default); rv.setImageViewResource(R.id.widgetImage, R.drawable.icon_wimax_gray_batt_na); rv.setTextViewText(R.id.widgetText, context.getString(working ? R.string.processing : R.string.pausing_en)); // updateAppWidget for (int id : appWidgetIds) { setClickIntent(context, id, rv); appWidgetManager.updateAppWidget(id, rv); } } }