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
| override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { for (appWidgetId in appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId) AppWidgetManager.getInstance(context).notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_list) } }
override fun onEnabled(context: Context) { }
override fun onDisabled(context: Context) { }
override fun onReceive(context: Context, intent: Intent) { super.onReceive(context, intent)
if (intent.hasExtra(WIDGET_IDS_KEY)) { val ids = intent.extras.getIntArray(WIDGET_IDS_KEY) this.onUpdate(context, AppWidgetManager.getInstance(context), ids)
if (intent.getBooleanExtra("isShowToast", false)) ToastUtils.showShortToast("成功刷新") } else if (intent.action == FINISH_TASK) { val extras = intent.extras if (extras != null) {
if (extras.getBoolean("canBeFinish", false)) { val taskId = extras.getLong("taskId") val teamId = extras.getLong("teamId") val item = todoService.getATodoItem(taskId)
if (teamId == -1L) { todoService.finishTodoItem(taskId) ToastUtils.showShortToast("成功完成事项")
if (item?.taskFrequency != 0) todoService.repeatTask(taskId) } else { val activityVO = ActivityVO() item?.let { teamNetworkImpl.finishTeamTask(it, activityVO) ToastUtils.showShortToast("成功完成事项") } }
} else { ToastUtils.showShortToast("尚未到开始时间") }
WidgetUtils.updateWidgets(context)
} } }
companion object {
const val WIDGET_IDS_KEY = "lifeupwidgetidskey" const val FINISH_TASK = "net.sarasarasa.lifeup.action.FINISH_TASK" private val todoService = TodoServiceImpl()
internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
val views = RemoteViews(context.packageName, R.layout.life_up_widget) views.setTextViewText(R.id.appwidget_text, "今日事项 0/0")
val intent = Intent(context, LifeUpRemoteViewsService::class.java) intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
views.setRemoteAdapter(R.id.widget_list, intent) views.setEmptyView(R.id.widget_list, R.id.tv_empty)
val finishTaskIntent = Intent(FINISH_TASK) finishTaskIntent.setClass(context, LifeUpWidget::class.java) val pendingIntent = PendingIntent.getBroadcast(context, 200, finishTaskIntent, PendingIntent.FLAG_UPDATE_CURRENT) views.setPendingIntentTemplate(R.id.widget_list, pendingIntent)
val startActivityIntent = Intent(context, MainActivity::class.java) val startActivityPendingIntent = PendingIntent.getActivity(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT) views.setOnClickPendingIntent(R.id.iv_home, startActivityPendingIntent)
val addItemIntent = Intent(context, AddToDoItemActivity::class.java) val addItemPendingIntent = PendingIntent.getActivity(context, 0, addItemIntent, PendingIntent.FLAG_UPDATE_CURRENT) views.setOnClickPendingIntent(R.id.iv_add, addItemPendingIntent)
val man = AppWidgetManager.getInstance(context)
val ids = man.getAppWidgetIds( ComponentName(context, LifeUpWidget::class.java)) val refreshIntent = Intent() refreshIntent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE refreshIntent.putExtra(LifeUpWidget.WIDGET_IDS_KEY, ids) refreshIntent.putExtra("isShowToast", true) val refreshPendingIntent = PendingIntent.getBroadcast(context, 199, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT) views.setOnClickPendingIntent(R.id.iv_refresh, refreshPendingIntent)
val finishCnt = todoService.getTodayFinishCount() val taskCnt = todoService.getTodayTaskCount() views.setTextViewText(R.id.appwidget_text, "今日事项 ${finishCnt}/${taskCnt}")
appWidgetManager.updateAppWidget(appWidgetId, views) } }
|