Back to project page Thrift-box.
The source code is released under:
GNU General Public License
If you think the Android project Thrift-box 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 ru.sash0k.thriftbox; /*from www. jav a 2 s. com*/ import android.app.AlarmManager; 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.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.LightingColorFilter; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.drawable.BitmapDrawable; import android.os.Build; import android.preference.PreferenceManager; import android.widget.RemoteViews; import java.util.Calendar; import ru.sash0k.thriftbox.database.DB; import ru.sash0k.thriftbox.fragments.SettingsFragment; /** * ??????????? ??????? ???? ????????????? ????????? * Created by sash0k on 15.04.14. */ public class Widget extends AppWidgetProvider { public static final String ACTION_AUTO_UPDATE_WIDGET = "ACTION_AUTO_UPDATE_WIDGET"; public static final String alpha = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) ? "setImageAlpha" : "setAlpha"; @Override public void onEnabled(Context context) { super.onEnabled(context); Intent intent = new Intent(Widget.ACTION_AUTO_UPDATE_WIDGET); PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 1); AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmMgr.setRepeating(AlarmManager.RTC, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent); // TODO: Android 4.4 } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); for (int id : appWidgetIds) { updateWidget(context, appWidgetManager, id); } } @Override public void onDisabled(Context context) { super.onDisabled(context); Intent intent = new Intent(Widget.ACTION_AUTO_UPDATE_WIDGET); AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmMgr.cancel(PendingIntent.getBroadcast(context, 0, intent, 0)); } @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); if (ACTION_AUTO_UPDATE_WIDGET.equals(intent.getAction())) { AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); int[] widgets = widgetManager.getAppWidgetIds(new ComponentName(context, Widget.class)); for (int widget : widgets) updateWidget(context, widgetManager, widget); } } public static void updateWidget(Context context, AppWidgetManager appWidgetManager, int widgetID) { RemoteViews widgetView = new RemoteViews(context.getPackageName(), R.layout.widget); long[] timestamps = Utils.getTimestamps(); final String today = Utils.formatValue(DB.getExpense(context, timestamps[0])); final String week = Utils.formatValue(DB.getExpense(context, timestamps[1])); final String month = Utils.formatValue(DB.getExpense(context, timestamps[2])); // ?????????? ????????????? ??????? SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); final int transparency = Integer.parseInt(preferences.getString( SettingsFragment.PREF_WIDGET_TRANSPARENCY_KEY, "255")); widgetView.setInt(R.id.widget_background, "setColorFilter", Color.WHITE); widgetView.setInt(R.id.widget_background, alpha, transparency); // ?????????? ????? ??????? final int color = preferences.getInt(SettingsFragment.PREF_WIDGET_TEXT_COLOR_KEY, Color.BLACK); widgetView.setImageViewBitmap(R.id.ruble_icon, changeBitmapColor(context, R.drawable.ruble, color)); widgetView.setTextColor(R.id.widget_today, color); widgetView.setTextColor(R.id.widget_week, color); widgetView.setTextColor(R.id.widget_month, color); // ?????????? ???????? widgetView.setTextViewText(R.id.widget_today, today); widgetView.setTextViewText(R.id.widget_week, context.getString(R.string.week) + " " + week + " " + context.getString(R.string.ruble_small)); widgetView.setTextViewText(R.id.widget_month, context.getString(R.string.month) + " " + month + " " + context.getString(R.string.ruble_small)); // ??????? ?? ????? ?? ?????? final Intent intent = new Intent(context, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); widgetView.setOnClickPendingIntent(R.id.widget, pendingIntent); // ?????????? ?????? appWidgetManager.updateAppWidget(widgetID, widgetView); } private static Bitmap changeBitmapColor(Context context, int resId, int color) { BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(resId); drawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY); Bitmap bmp = drawable.getBitmap().copy(Bitmap.Config.ARGB_8888, true); Canvas myCanvas = new Canvas(bmp); int myColor = bmp.getPixel(0, 0); ColorFilter filter = new LightingColorFilter(myColor, color); Paint pnt = new Paint(); pnt.setColorFilter(filter); myCanvas.drawBitmap(bmp, 0, 0, pnt); return bmp; } }