convert MyWidgetProvider to kotlin
This commit is contained in:
parent
577a016a72
commit
ba767b83d4
|
@ -55,7 +55,7 @@
|
|||
android:parentActivityName=".activities.MainActivity"/>
|
||||
|
||||
<receiver
|
||||
android:name=".MyWidgetProvider"
|
||||
android:name=".helpers.MyWidgetProvider"
|
||||
android:icon="@mipmap/widget_preview">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
|
||||
|
|
|
@ -1,224 +0,0 @@
|
|||
package com.simplemobiletools.calendar;
|
||||
|
||||
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.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ColorFilter;
|
||||
import android.graphics.LightingColorFilter;
|
||||
import android.graphics.Paint;
|
||||
import android.text.SpannableString;
|
||||
import android.text.style.UnderlineSpan;
|
||||
import android.view.View;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import com.simplemobiletools.calendar.activities.DayActivity;
|
||||
import com.simplemobiletools.calendar.activities.MainActivity;
|
||||
import com.simplemobiletools.calendar.helpers.Config;
|
||||
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar;
|
||||
import com.simplemobiletools.calendar.models.Day;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MyWidgetProvider extends AppWidgetProvider implements MonthlyCalendar {
|
||||
private static final String PREV = "prev";
|
||||
private static final String NEXT = "next";
|
||||
|
||||
private static RemoteViews mRemoteViews;
|
||||
private static AppWidgetManager mWidgetManager;
|
||||
private static Intent mIntent;
|
||||
private static Context mContext;
|
||||
private static MonthlyCalendarImpl mCalendar;
|
||||
private static Resources mRes;
|
||||
|
||||
private static float mDayTextSize;
|
||||
private static float mTodayTextSize;
|
||||
private static int mTextColor;
|
||||
private static int mWeakTextColor;
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
initVariables(context);
|
||||
updateWidget();
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
||||
}
|
||||
|
||||
private void initVariables(Context context) {
|
||||
mContext = context;
|
||||
mRes = mContext.getResources();
|
||||
|
||||
final SharedPreferences prefs = initPrefs(mContext);
|
||||
final int storedTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
|
||||
mTextColor = Utils.adjustAlpha(storedTextColor, Constants.HIGH_ALPHA);
|
||||
mWeakTextColor = Utils.adjustAlpha(storedTextColor, Constants.LOW_ALPHA);
|
||||
|
||||
mDayTextSize = mRes.getDimension(R.dimen.day_text_size) / mRes.getDisplayMetrics().density;
|
||||
mTodayTextSize = mRes.getDimension(R.dimen.today_text_size) / mRes.getDisplayMetrics().density;
|
||||
mWidgetManager = AppWidgetManager.getInstance(mContext);
|
||||
|
||||
mRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.month_fragment);
|
||||
mIntent = new Intent(mContext, MyWidgetProvider.class);
|
||||
setupButtons();
|
||||
updateLabelColor();
|
||||
updateTopViews();
|
||||
|
||||
final int bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, Color.BLACK);
|
||||
mRemoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor);
|
||||
|
||||
mCalendar = new MonthlyCalendarImpl(this, mContext);
|
||||
mCalendar.updateMonthlyCalendar(new DateTime());
|
||||
}
|
||||
|
||||
private void updateWidget() {
|
||||
final ComponentName thisWidget = new ComponentName(mContext, MyWidgetProvider.class);
|
||||
AppWidgetManager.getInstance(mContext).updateAppWidget(thisWidget, mRemoteViews);
|
||||
}
|
||||
|
||||
private void setupIntent(String action, int id) {
|
||||
mIntent.setAction(action);
|
||||
final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent, 0);
|
||||
mRemoteViews.setOnClickPendingIntent(id, pendingIntent);
|
||||
}
|
||||
|
||||
private void setupAppOpenIntent(int id) {
|
||||
final Intent intent = new Intent(mContext, MainActivity.class);
|
||||
final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
|
||||
mRemoteViews.setOnClickPendingIntent(id, pendingIntent);
|
||||
}
|
||||
|
||||
private void setupDayOpenIntent(int id, final String dayCode) {
|
||||
final Intent intent = new Intent(mContext, DayActivity.class);
|
||||
intent.putExtra(Constants.DAY_CODE, dayCode);
|
||||
final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, Integer.parseInt(dayCode), intent, 0);
|
||||
mRemoteViews.setOnClickPendingIntent(id, pendingIntent);
|
||||
}
|
||||
|
||||
private void setupButtons() {
|
||||
setupIntent(PREV, R.id.top_left_arrow);
|
||||
setupIntent(NEXT, R.id.top_right_arrow);
|
||||
setupAppOpenIntent(R.id.top_value);
|
||||
}
|
||||
|
||||
private SharedPreferences initPrefs(Context context) {
|
||||
return context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (mRemoteViews == null || mWidgetManager == null || mCalendar == null || mContext == null)
|
||||
initVariables(context);
|
||||
|
||||
final String action = intent.getAction();
|
||||
switch (action) {
|
||||
case PREV:
|
||||
mCalendar.getPrevMonth();
|
||||
break;
|
||||
case NEXT:
|
||||
mCalendar.getNextMonth();
|
||||
break;
|
||||
default:
|
||||
super.onReceive(context, intent);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateDays(List<Day> days) {
|
||||
final boolean displayWeekNumbers = Config.Companion.newInstance(mContext).getDisplayWeekNumbers();
|
||||
final int len = days.size();
|
||||
final String packageName = mContext.getPackageName();
|
||||
mRemoteViews.setInt(R.id.week_num, "setTextColor", mWeakTextColor);
|
||||
mRemoteViews.setViewVisibility(R.id.week_num, displayWeekNumbers ? View.VISIBLE : View.GONE);
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
final int id = mRes.getIdentifier("week_num_" + i, "id", packageName);
|
||||
mRemoteViews.setTextViewText(id, days.get(i * 7).getWeekOfYear() + ":");
|
||||
mRemoteViews.setInt(id, "setTextColor", mWeakTextColor);
|
||||
mRemoteViews.setViewVisibility(id, displayWeekNumbers ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
final Day day = days.get(i);
|
||||
final int id = mRes.getIdentifier("day_" + i, "id", packageName);
|
||||
int curTextColor = mWeakTextColor;
|
||||
float curTextSize = mDayTextSize;
|
||||
|
||||
if (day.isThisMonth()) {
|
||||
curTextColor = mTextColor;
|
||||
}
|
||||
|
||||
if (day.isToday()) {
|
||||
curTextSize = mTodayTextSize;
|
||||
}
|
||||
|
||||
final String text = String.valueOf(day.getValue());
|
||||
if (day.getHasEvent()) {
|
||||
final SpannableString underlinedText = new SpannableString(text);
|
||||
underlinedText.setSpan(new UnderlineSpan(), 0, text.length(), 0);
|
||||
mRemoteViews.setTextViewText(id, underlinedText);
|
||||
} else {
|
||||
mRemoteViews.setTextViewText(id, text);
|
||||
}
|
||||
mRemoteViews.setInt(id, "setTextColor", curTextColor);
|
||||
mRemoteViews.setFloat(id, "setTextSize", curTextSize);
|
||||
setupDayOpenIntent(id, day.getCode());
|
||||
}
|
||||
}
|
||||
|
||||
private void updateTopViews() {
|
||||
mRemoteViews.setInt(R.id.top_value, "setTextColor", mTextColor);
|
||||
|
||||
Bitmap bmp = getColoredIcon(mContext, mTextColor, R.mipmap.arrow_left);
|
||||
mRemoteViews.setImageViewBitmap(R.id.top_left_arrow, bmp);
|
||||
|
||||
bmp = getColoredIcon(mContext, mTextColor, R.mipmap.arrow_right);
|
||||
mRemoteViews.setImageViewBitmap(R.id.top_right_arrow, bmp);
|
||||
}
|
||||
|
||||
public void updateMonth(String month) {
|
||||
mRemoteViews.setTextViewText(R.id.top_value, month);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateMonthlyCalendar(String month, List<Day> days) {
|
||||
updateMonth(month);
|
||||
updateDays(days);
|
||||
updateWidget();
|
||||
}
|
||||
|
||||
private void updateLabelColor() {
|
||||
final boolean mSundayFirst = Config.Companion.newInstance(mContext).isSundayFirst();
|
||||
final String packageName = mContext.getPackageName();
|
||||
int letters[] = Utils.getLetterIDs();
|
||||
for (int i = 0; i < 7; i++) {
|
||||
final int id = mRes.getIdentifier("label_" + i, "id", packageName);
|
||||
mRemoteViews.setInt(id, "setTextColor", mTextColor);
|
||||
|
||||
int index = i;
|
||||
if (!mSundayFirst)
|
||||
index = (index + 1) % letters.length;
|
||||
|
||||
mRemoteViews.setTextViewText(id, mContext.getResources().getString(letters[index]));
|
||||
}
|
||||
}
|
||||
|
||||
private Bitmap getColoredIcon(Context context, int newTextColor, int id) {
|
||||
final BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inMutable = true;
|
||||
final Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), id, options);
|
||||
final Paint paint = new Paint();
|
||||
final ColorFilter filter = new LightingColorFilter(newTextColor, 1);
|
||||
paint.setColorFilter(filter);
|
||||
final Canvas canvas = new Canvas(bmp);
|
||||
canvas.drawBitmap(bmp, 0, 0, paint);
|
||||
return bmp;
|
||||
}
|
||||
}
|
|
@ -12,8 +12,12 @@ import android.support.v7.app.AppCompatActivity
|
|||
import android.view.View
|
||||
import android.widget.SeekBar
|
||||
import android.widget.TextView
|
||||
import com.simplemobiletools.calendar.*
|
||||
import com.simplemobiletools.calendar.Constants
|
||||
import com.simplemobiletools.calendar.MonthlyCalendarImpl
|
||||
import com.simplemobiletools.calendar.R
|
||||
import com.simplemobiletools.calendar.Utils
|
||||
import com.simplemobiletools.calendar.helpers.Config
|
||||
import com.simplemobiletools.calendar.helpers.MyWidgetProvider
|
||||
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
|
||||
import com.simplemobiletools.calendar.models.Day
|
||||
import kotlinx.android.synthetic.main.first_row.*
|
||||
|
@ -25,7 +29,7 @@ import yuku.ambilwarna.AmbilWarnaDialog
|
|||
class WidgetConfigureActivity : AppCompatActivity(), MonthlyCalendar {
|
||||
lateinit var mRes: Resources
|
||||
private var mDays: List<Day>? = null
|
||||
private var mPackageName = packageName
|
||||
private var mPackageName = ""
|
||||
|
||||
private var mBgAlpha = 0f
|
||||
private var mWidgetId = 0
|
||||
|
@ -41,6 +45,7 @@ class WidgetConfigureActivity : AppCompatActivity(), MonthlyCalendar {
|
|||
super.onCreate(savedInstanceState)
|
||||
setResult(Activity.RESULT_CANCELED)
|
||||
setContentView(R.layout.widget_config)
|
||||
mPackageName = packageName
|
||||
initVariables()
|
||||
|
||||
val extras = intent.extras
|
||||
|
|
|
@ -5,7 +5,7 @@ import android.content.ComponentName
|
|||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.widget.Toast
|
||||
import com.simplemobiletools.calendar.MyWidgetProvider
|
||||
import com.simplemobiletools.calendar.helpers.MyWidgetProvider
|
||||
import com.simplemobiletools.calendar.R
|
||||
|
||||
fun Context.updateWidget() {
|
||||
|
|
|
@ -0,0 +1,210 @@
|
|||
package com.simplemobiletools.calendar.helpers
|
||||
|
||||
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.content.res.Resources
|
||||
import android.graphics.*
|
||||
import android.text.SpannableString
|
||||
import android.text.style.UnderlineSpan
|
||||
import android.view.View
|
||||
import android.widget.RemoteViews
|
||||
import com.simplemobiletools.calendar.Constants
|
||||
import com.simplemobiletools.calendar.MonthlyCalendarImpl
|
||||
import com.simplemobiletools.calendar.R
|
||||
import com.simplemobiletools.calendar.Utils
|
||||
import com.simplemobiletools.calendar.activities.DayActivity
|
||||
import com.simplemobiletools.calendar.activities.MainActivity
|
||||
import com.simplemobiletools.calendar.interfaces.MonthlyCalendar
|
||||
import com.simplemobiletools.calendar.models.Day
|
||||
import org.joda.time.DateTime
|
||||
|
||||
class MyWidgetProvider : AppWidgetProvider(), MonthlyCalendar {
|
||||
companion object {
|
||||
private val PREV = "prev"
|
||||
private val NEXT = "next"
|
||||
|
||||
private var mDayTextSize = 0f
|
||||
private var mTodayTextSize = 0f
|
||||
private var mTextColor = 0
|
||||
private var mWeakTextColor = 0
|
||||
|
||||
lateinit var mCalendar: MonthlyCalendarImpl
|
||||
lateinit var mRemoteViews: RemoteViews
|
||||
lateinit var mRes: Resources
|
||||
lateinit var mContext: Context
|
||||
lateinit var mWidgetManager: AppWidgetManager
|
||||
lateinit var mIntent: Intent
|
||||
}
|
||||
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
initVariables(context)
|
||||
updateWidget()
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds)
|
||||
}
|
||||
|
||||
private fun initVariables(context: Context) {
|
||||
mContext = context
|
||||
mRes = mContext.resources
|
||||
|
||||
val prefs = initPrefs(context)
|
||||
val storedTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE)
|
||||
mTextColor = Utils.adjustAlpha(storedTextColor, Constants.HIGH_ALPHA)
|
||||
mWeakTextColor = Utils.adjustAlpha(storedTextColor, Constants.LOW_ALPHA)
|
||||
|
||||
mDayTextSize = mRes.getDimension(R.dimen.day_text_size) / mRes.displayMetrics.density
|
||||
mTodayTextSize = mRes.getDimension(R.dimen.today_text_size) / mRes.displayMetrics.density
|
||||
mWidgetManager = AppWidgetManager.getInstance(mContext)
|
||||
|
||||
mRemoteViews = RemoteViews(mContext.packageName, R.layout.month_fragment)
|
||||
mIntent = Intent(mContext, MyWidgetProvider::class.java)
|
||||
setupButtons()
|
||||
updateLabelColor()
|
||||
updateTopViews()
|
||||
|
||||
val bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, Color.BLACK)
|
||||
mRemoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor)
|
||||
|
||||
mCalendar = MonthlyCalendarImpl(this, mContext)
|
||||
mCalendar.updateMonthlyCalendar(DateTime())
|
||||
}
|
||||
|
||||
private fun updateWidget() {
|
||||
val thisWidget = ComponentName(mContext, MyWidgetProvider::class.java)
|
||||
AppWidgetManager.getInstance(mContext).updateAppWidget(thisWidget, mRemoteViews)
|
||||
}
|
||||
|
||||
private fun setupIntent(action: String, id: Int) {
|
||||
mIntent.action = action
|
||||
val pendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent, 0)
|
||||
mRemoteViews.setOnClickPendingIntent(id, pendingIntent)
|
||||
}
|
||||
|
||||
private fun setupAppOpenIntent(id: Int) {
|
||||
val intent = Intent(mContext, MainActivity::class.java)
|
||||
val pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0)
|
||||
mRemoteViews.setOnClickPendingIntent(id, pendingIntent)
|
||||
}
|
||||
|
||||
private fun setupDayOpenIntent(id: Int, dayCode: String) {
|
||||
val intent = Intent(mContext, DayActivity::class.java)
|
||||
intent.putExtra(Constants.DAY_CODE, dayCode)
|
||||
val pendingIntent = PendingIntent.getActivity(mContext, Integer.parseInt(dayCode), intent, 0)
|
||||
mRemoteViews.setOnClickPendingIntent(id, pendingIntent)
|
||||
}
|
||||
|
||||
private fun setupButtons() {
|
||||
setupIntent(PREV, R.id.top_left_arrow)
|
||||
setupIntent(NEXT, R.id.top_right_arrow)
|
||||
setupAppOpenIntent(R.id.top_value)
|
||||
}
|
||||
|
||||
private fun initPrefs(context: Context): SharedPreferences {
|
||||
return context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE)
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val action = intent.action
|
||||
when (action) {
|
||||
PREV -> mCalendar.getPrevMonth()
|
||||
NEXT -> mCalendar.getNextMonth()
|
||||
else -> super.onReceive(context, intent)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateDays(days: List<Day>) {
|
||||
val displayWeekNumbers = Config.newInstance(mContext).displayWeekNumbers
|
||||
val len = days.size
|
||||
val packageName = mContext.packageName
|
||||
mRemoteViews.setInt(R.id.week_num, "setTextColor", mWeakTextColor)
|
||||
mRemoteViews.setViewVisibility(R.id.week_num, if (displayWeekNumbers) View.VISIBLE else View.GONE)
|
||||
|
||||
for (i in 0..5) {
|
||||
val id = mRes.getIdentifier("week_num_" + i, "id", packageName)
|
||||
mRemoteViews.apply {
|
||||
setTextViewText(id, days[i * 7].weekOfYear.toString() + ":")
|
||||
setInt(id, "setTextColor", mWeakTextColor)
|
||||
setViewVisibility(id, if (displayWeekNumbers) View.VISIBLE else View.GONE)
|
||||
}
|
||||
}
|
||||
|
||||
for (i in 0..len - 1) {
|
||||
val day = days[i]
|
||||
val id = mRes.getIdentifier("day_" + i, "id", packageName)
|
||||
var curTextColor = mWeakTextColor
|
||||
var curTextSize = mDayTextSize
|
||||
|
||||
if (day.isThisMonth) {
|
||||
curTextColor = mTextColor
|
||||
}
|
||||
|
||||
if (day.isToday) {
|
||||
curTextSize = mTodayTextSize
|
||||
}
|
||||
|
||||
val text = day.value.toString()
|
||||
if (day.hasEvent) {
|
||||
val underlinedText = SpannableString(text)
|
||||
underlinedText.setSpan(UnderlineSpan(), 0, text.length, 0)
|
||||
mRemoteViews.setTextViewText(id, underlinedText)
|
||||
} else {
|
||||
mRemoteViews.setTextViewText(id, text)
|
||||
}
|
||||
mRemoteViews.setInt(id, "setTextColor", curTextColor)
|
||||
mRemoteViews.setFloat(id, "setTextSize", curTextSize)
|
||||
setupDayOpenIntent(id, day.code)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateTopViews() {
|
||||
mRemoteViews.setInt(R.id.top_value, "setTextColor", mTextColor)
|
||||
|
||||
var bmp = getColoredIcon(mContext, mTextColor, R.mipmap.arrow_left)
|
||||
mRemoteViews.setImageViewBitmap(R.id.top_left_arrow, bmp)
|
||||
|
||||
bmp = getColoredIcon(mContext, mTextColor, R.mipmap.arrow_right)
|
||||
mRemoteViews.setImageViewBitmap(R.id.top_right_arrow, bmp)
|
||||
}
|
||||
|
||||
fun updateMonth(month: String) {
|
||||
mRemoteViews.setTextViewText(R.id.top_value, month)
|
||||
}
|
||||
|
||||
override fun updateMonthlyCalendar(month: String, days: List<Day>) {
|
||||
updateMonth(month)
|
||||
updateDays(days)
|
||||
updateWidget()
|
||||
}
|
||||
|
||||
private fun updateLabelColor() {
|
||||
val mSundayFirst = Config.newInstance(mContext).isSundayFirst
|
||||
val packageName = mContext.packageName
|
||||
val letters = Utils.getLetterIDs()
|
||||
for (i in 0..6) {
|
||||
val id = mRes.getIdentifier("label_" + i, "id", packageName)
|
||||
mRemoteViews.setInt(id, "setTextColor", mTextColor)
|
||||
|
||||
var index = i
|
||||
if (!mSundayFirst)
|
||||
index = (index + 1) % letters.size
|
||||
|
||||
mRemoteViews.setTextViewText(id, mContext.resources.getString(letters[index]))
|
||||
}
|
||||
}
|
||||
|
||||
private fun getColoredIcon(context: Context, newTextColor: Int, id: Int): Bitmap {
|
||||
val options = BitmapFactory.Options()
|
||||
options.inMutable = true
|
||||
val bmp = BitmapFactory.decodeResource(context.resources, id, options)
|
||||
val paint = Paint()
|
||||
val filter = LightingColorFilter(newTextColor, 1)
|
||||
paint.colorFilter = filter
|
||||
val canvas = Canvas(bmp)
|
||||
canvas.drawBitmap(bmp, 0f, 0f, paint)
|
||||
return bmp
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue