mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-04-13 23:12:02 +02:00
lets just update all widgets on update
This commit is contained in:
parent
501efea07b
commit
890fc948b9
@ -25,7 +25,10 @@ import com.simplemobiletools.notes.BuildConfig
|
||||
import com.simplemobiletools.notes.R
|
||||
import com.simplemobiletools.notes.adapters.NotesPagerAdapter
|
||||
import com.simplemobiletools.notes.dialogs.*
|
||||
import com.simplemobiletools.notes.extensions.*
|
||||
import com.simplemobiletools.notes.extensions.config
|
||||
import com.simplemobiletools.notes.extensions.dbHelper
|
||||
import com.simplemobiletools.notes.extensions.getTextSize
|
||||
import com.simplemobiletools.notes.extensions.updateWidgets
|
||||
import com.simplemobiletools.notes.helpers.MIME_TEXT_PLAIN
|
||||
import com.simplemobiletools.notes.helpers.OPEN_NOTE_ID
|
||||
import com.simplemobiletools.notes.helpers.TYPE_NOTE
|
||||
@ -530,7 +533,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
||||
updateSelectedNote(firstNoteId)
|
||||
if (config.widgetNoteId == note.id) {
|
||||
config.widgetNoteId = mCurrentNote.id
|
||||
updateNoteWidget(mCurrentNote.id)
|
||||
updateWidgets()
|
||||
}
|
||||
invalidateOptionsMenu()
|
||||
initViewPager()
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.simplemobiletools.notes.extensions
|
||||
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.simplemobiletools.notes.R
|
||||
@ -17,18 +19,11 @@ fun Context.getTextSize() = when (config.fontSize) {
|
||||
}
|
||||
|
||||
fun Context.updateWidgets() {
|
||||
dbHelper.getNotes().forEach {
|
||||
updateNoteWidget(it.id)
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.updateNoteWidget(noteId: Int) {
|
||||
val widgetIds = dbHelper.getNoteWidgetIds(noteId)
|
||||
widgetIds.forEach {
|
||||
Intent(this, MyWidgetProvider::class.java).apply {
|
||||
action = UPDATE_WIDGET
|
||||
putExtra(WIDGET_ID, it)
|
||||
putExtra(NOTE_ID, noteId)
|
||||
val widgetIDs = AppWidgetManager.getInstance(applicationContext).getAppWidgetIds(ComponentName(applicationContext, MyWidgetProvider::class.java))
|
||||
if (widgetIDs.isNotEmpty()) {
|
||||
Intent(applicationContext, MyWidgetProvider::class.java).apply {
|
||||
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIDs)
|
||||
sendBroadcast(this)
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ import com.simplemobiletools.notes.activities.MainActivity
|
||||
import com.simplemobiletools.notes.extensions.config
|
||||
import com.simplemobiletools.notes.extensions.dbHelper
|
||||
import com.simplemobiletools.notes.extensions.getTextSize
|
||||
import com.simplemobiletools.notes.extensions.updateNoteWidget
|
||||
import com.simplemobiletools.notes.extensions.updateWidgets
|
||||
import com.simplemobiletools.notes.helpers.*
|
||||
import com.simplemobiletools.notes.models.Note
|
||||
import com.simplemobiletools.notes.models.TextHistory
|
||||
@ -156,7 +156,7 @@ class NoteFragment : Fragment() {
|
||||
if (newText != null && (newText != oldText || force)) {
|
||||
note.value = newText
|
||||
saveNoteValue(note)
|
||||
context!!.updateNoteWidget(note.id)
|
||||
context!!.updateWidgets()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,8 +2,6 @@ package com.simplemobiletools.notes.helpers
|
||||
|
||||
const val NOTE_ID = "note_id"
|
||||
const val OPEN_NOTE_ID = "open_note_id"
|
||||
const val UPDATE_WIDGET = "update_widget"
|
||||
const val WIDGET_ID = "widget_id"
|
||||
|
||||
// shared preferences
|
||||
const val CURRENT_NOTE_ID = "current_note_id"
|
||||
|
@ -18,7 +18,7 @@ import java.io.File
|
||||
import java.util.*
|
||||
|
||||
class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHelper(mContext, DB_NAME, null, DB_VERSION) {
|
||||
private val mDb: SQLiteDatabase = writableDatabase
|
||||
private val mDb = writableDatabase
|
||||
|
||||
companion object {
|
||||
private const val DB_NAME = "notes.db"
|
||||
@ -221,21 +221,24 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
|
||||
|
||||
fun isValidId(id: Int) = id > 0
|
||||
|
||||
fun getNoteWidgetIds(noteId: Int): ArrayList<Int> {
|
||||
val widgetIds = ArrayList<Int>()
|
||||
val cols = arrayOf(COL_WIDGET_ID)
|
||||
val selection = "$COL_NOTE_ID = ?"
|
||||
val selectionArgs = arrayOf(noteId.toString())
|
||||
fun getWidgets(): ArrayList<Widget> {
|
||||
val widgets = ArrayList<Widget>()
|
||||
val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID)
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = mDb.query(WIDGETS_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
||||
cursor = mDb.query(WIDGETS_TABLE_NAME, cols, null, null, null, null, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
val widgetId = cursor.getIntValue(COL_WIDGET_ID)
|
||||
widgetIds.add(widgetId)
|
||||
do {
|
||||
val widgetId = cursor.getIntValue(COL_WIDGET_ID)
|
||||
val noteId = cursor.getIntValue(COL_NOTE_ID)
|
||||
val widget = Widget(widgetId, noteId)
|
||||
widgets.add(widget)
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
return widgetIds
|
||||
|
||||
return widgets
|
||||
}
|
||||
}
|
||||
|
@ -12,50 +12,40 @@ import com.simplemobiletools.commons.extensions.setBackgroundColor
|
||||
import com.simplemobiletools.notes.R
|
||||
import com.simplemobiletools.notes.activities.SplashActivity
|
||||
import com.simplemobiletools.notes.extensions.config
|
||||
import com.simplemobiletools.notes.extensions.dbHelper
|
||||
import com.simplemobiletools.notes.services.WidgetService
|
||||
|
||||
class MyWidgetProvider : AppWidgetProvider() {
|
||||
private fun performUpdate(context: Context, intent: Intent) {
|
||||
val noteId = intent.getIntExtra(NOTE_ID, -1)
|
||||
val widgetId = intent.getIntExtra(WIDGET_ID, -1)
|
||||
if (noteId == -1 || widgetId == -1) {
|
||||
return
|
||||
}
|
||||
|
||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||
val views = RemoteViews(context.packageName, R.layout.widget)
|
||||
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor)
|
||||
setupAppOpenIntent(context, views, R.id.notes_widget_holder)
|
||||
|
||||
Intent(context, WidgetService::class.java).apply {
|
||||
putExtra(NOTE_ID, noteId)
|
||||
data = Uri.parse(this.toUri(Intent.URI_INTENT_SCHEME))
|
||||
views.setRemoteAdapter(R.id.notes_widget_listview, this)
|
||||
}
|
||||
|
||||
val startActivityIntent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)
|
||||
startActivityIntent.putExtra(OPEN_NOTE_ID, widgetId)
|
||||
val startActivityPendingIntent = PendingIntent.getActivity(context, widgetId, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
views.setPendingIntentTemplate(R.id.notes_widget_listview, startActivityPendingIntent)
|
||||
|
||||
appWidgetManager.updateAppWidget(widgetId, views)
|
||||
appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.notes_widget_listview)
|
||||
}
|
||||
|
||||
private fun setupAppOpenIntent(context: Context, views: RemoteViews, id: Int) {
|
||||
val widgetId = context.config.widgetNoteId
|
||||
private fun setupAppOpenIntent(context: Context, views: RemoteViews, id: Int, noteId: Int) {
|
||||
val intent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)
|
||||
intent.putExtra(OPEN_NOTE_ID, widgetId)
|
||||
val pendingIntent = PendingIntent.getActivity(context, widgetId, intent, 0)
|
||||
intent.putExtra(OPEN_NOTE_ID, noteId)
|
||||
val pendingIntent = PendingIntent.getActivity(context, noteId, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
views.setOnClickPendingIntent(id, pendingIntent)
|
||||
}
|
||||
|
||||
// use only this way of updating widgets instead of onUpdate, so that we can pass a widget ID too
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val action = intent.action
|
||||
when (action) {
|
||||
UPDATE_WIDGET -> performUpdate(context, intent)
|
||||
else -> super.onReceive(context, intent)
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds)
|
||||
val widgets = context.dbHelper.getWidgets()
|
||||
widgets.forEach {
|
||||
val widgetId = it.widgetId
|
||||
val noteId = it.noteId
|
||||
val views = RemoteViews(context.packageName, R.layout.widget)
|
||||
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor)
|
||||
setupAppOpenIntent(context, views, R.id.notes_widget_holder, noteId)
|
||||
|
||||
Intent(context, WidgetService::class.java).apply {
|
||||
putExtra(NOTE_ID, noteId)
|
||||
data = Uri.parse(this.toUri(Intent.URI_INTENT_SCHEME))
|
||||
views.setRemoteAdapter(R.id.notes_widget_listview, this)
|
||||
}
|
||||
|
||||
val startActivityIntent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)
|
||||
startActivityIntent.putExtra(OPEN_NOTE_ID, widgetId)
|
||||
val startActivityPendingIntent = PendingIntent.getActivity(context, widgetId, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
views.setPendingIntentTemplate(R.id.notes_widget_listview, startActivityPendingIntent)
|
||||
|
||||
appWidgetManager.updateAppWidget(widgetId, views)
|
||||
appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.notes_widget_listview)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user