mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-04-18 09:17:22 +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.R
|
||||||
import com.simplemobiletools.notes.adapters.NotesPagerAdapter
|
import com.simplemobiletools.notes.adapters.NotesPagerAdapter
|
||||||
import com.simplemobiletools.notes.dialogs.*
|
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.MIME_TEXT_PLAIN
|
||||||
import com.simplemobiletools.notes.helpers.OPEN_NOTE_ID
|
import com.simplemobiletools.notes.helpers.OPEN_NOTE_ID
|
||||||
import com.simplemobiletools.notes.helpers.TYPE_NOTE
|
import com.simplemobiletools.notes.helpers.TYPE_NOTE
|
||||||
@ -530,7 +533,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
|||||||
updateSelectedNote(firstNoteId)
|
updateSelectedNote(firstNoteId)
|
||||||
if (config.widgetNoteId == note.id) {
|
if (config.widgetNoteId == note.id) {
|
||||||
config.widgetNoteId = mCurrentNote.id
|
config.widgetNoteId = mCurrentNote.id
|
||||||
updateNoteWidget(mCurrentNote.id)
|
updateWidgets()
|
||||||
}
|
}
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
initViewPager()
|
initViewPager()
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.simplemobiletools.notes.extensions
|
package com.simplemobiletools.notes.extensions
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager
|
||||||
|
import android.content.ComponentName
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.simplemobiletools.notes.R
|
import com.simplemobiletools.notes.R
|
||||||
@ -17,18 +19,11 @@ fun Context.getTextSize() = when (config.fontSize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun Context.updateWidgets() {
|
fun Context.updateWidgets() {
|
||||||
dbHelper.getNotes().forEach {
|
val widgetIDs = AppWidgetManager.getInstance(applicationContext).getAppWidgetIds(ComponentName(applicationContext, MyWidgetProvider::class.java))
|
||||||
updateNoteWidget(it.id)
|
if (widgetIDs.isNotEmpty()) {
|
||||||
}
|
Intent(applicationContext, MyWidgetProvider::class.java).apply {
|
||||||
}
|
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
|
||||||
|
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIDs)
|
||||||
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)
|
|
||||||
sendBroadcast(this)
|
sendBroadcast(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ import com.simplemobiletools.notes.activities.MainActivity
|
|||||||
import com.simplemobiletools.notes.extensions.config
|
import com.simplemobiletools.notes.extensions.config
|
||||||
import com.simplemobiletools.notes.extensions.dbHelper
|
import com.simplemobiletools.notes.extensions.dbHelper
|
||||||
import com.simplemobiletools.notes.extensions.getTextSize
|
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.helpers.*
|
||||||
import com.simplemobiletools.notes.models.Note
|
import com.simplemobiletools.notes.models.Note
|
||||||
import com.simplemobiletools.notes.models.TextHistory
|
import com.simplemobiletools.notes.models.TextHistory
|
||||||
@ -156,7 +156,7 @@ class NoteFragment : Fragment() {
|
|||||||
if (newText != null && (newText != oldText || force)) {
|
if (newText != null && (newText != oldText || force)) {
|
||||||
note.value = newText
|
note.value = newText
|
||||||
saveNoteValue(note)
|
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 NOTE_ID = "note_id"
|
||||||
const val OPEN_NOTE_ID = "open_note_id"
|
const val OPEN_NOTE_ID = "open_note_id"
|
||||||
const val UPDATE_WIDGET = "update_widget"
|
|
||||||
const val WIDGET_ID = "widget_id"
|
|
||||||
|
|
||||||
// shared preferences
|
// shared preferences
|
||||||
const val CURRENT_NOTE_ID = "current_note_id"
|
const val CURRENT_NOTE_ID = "current_note_id"
|
||||||
|
@ -18,7 +18,7 @@ import java.io.File
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHelper(mContext, DB_NAME, null, DB_VERSION) {
|
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 {
|
companion object {
|
||||||
private const val DB_NAME = "notes.db"
|
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 isValidId(id: Int) = id > 0
|
||||||
|
|
||||||
fun getNoteWidgetIds(noteId: Int): ArrayList<Int> {
|
fun getWidgets(): ArrayList<Widget> {
|
||||||
val widgetIds = ArrayList<Int>()
|
val widgets = ArrayList<Widget>()
|
||||||
val cols = arrayOf(COL_WIDGET_ID)
|
val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID)
|
||||||
val selection = "$COL_NOTE_ID = ?"
|
|
||||||
val selectionArgs = arrayOf(noteId.toString())
|
|
||||||
var cursor: Cursor? = null
|
var cursor: Cursor? = null
|
||||||
try {
|
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) {
|
if (cursor?.moveToFirst() == true) {
|
||||||
|
do {
|
||||||
val widgetId = cursor.getIntValue(COL_WIDGET_ID)
|
val widgetId = cursor.getIntValue(COL_WIDGET_ID)
|
||||||
widgetIds.add(widgetId)
|
val noteId = cursor.getIntValue(COL_NOTE_ID)
|
||||||
|
val widget = Widget(widgetId, noteId)
|
||||||
|
widgets.add(widget)
|
||||||
|
} while (cursor.moveToNext())
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
}
|
}
|
||||||
return widgetIds
|
|
||||||
|
return widgets
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,20 +12,26 @@ import com.simplemobiletools.commons.extensions.setBackgroundColor
|
|||||||
import com.simplemobiletools.notes.R
|
import com.simplemobiletools.notes.R
|
||||||
import com.simplemobiletools.notes.activities.SplashActivity
|
import com.simplemobiletools.notes.activities.SplashActivity
|
||||||
import com.simplemobiletools.notes.extensions.config
|
import com.simplemobiletools.notes.extensions.config
|
||||||
|
import com.simplemobiletools.notes.extensions.dbHelper
|
||||||
import com.simplemobiletools.notes.services.WidgetService
|
import com.simplemobiletools.notes.services.WidgetService
|
||||||
|
|
||||||
class MyWidgetProvider : AppWidgetProvider() {
|
class MyWidgetProvider : AppWidgetProvider() {
|
||||||
private fun performUpdate(context: Context, intent: Intent) {
|
private fun setupAppOpenIntent(context: Context, views: RemoteViews, id: Int, noteId: Int) {
|
||||||
val noteId = intent.getIntExtra(NOTE_ID, -1)
|
val intent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)
|
||||||
val widgetId = intent.getIntExtra(WIDGET_ID, -1)
|
intent.putExtra(OPEN_NOTE_ID, noteId)
|
||||||
if (noteId == -1 || widgetId == -1) {
|
val pendingIntent = PendingIntent.getActivity(context, noteId, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||||
return
|
views.setOnClickPendingIntent(id, pendingIntent)
|
||||||
}
|
}
|
||||||
|
|
||||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
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)
|
val views = RemoteViews(context.packageName, R.layout.widget)
|
||||||
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor)
|
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor)
|
||||||
setupAppOpenIntent(context, views, R.id.notes_widget_holder)
|
setupAppOpenIntent(context, views, R.id.notes_widget_holder, noteId)
|
||||||
|
|
||||||
Intent(context, WidgetService::class.java).apply {
|
Intent(context, WidgetService::class.java).apply {
|
||||||
putExtra(NOTE_ID, noteId)
|
putExtra(NOTE_ID, noteId)
|
||||||
@ -41,21 +47,5 @@ class MyWidgetProvider : AppWidgetProvider() {
|
|||||||
appWidgetManager.updateAppWidget(widgetId, views)
|
appWidgetManager.updateAppWidget(widgetId, views)
|
||||||
appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.notes_widget_listview)
|
appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.notes_widget_listview)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupAppOpenIntent(context: Context, views: RemoteViews, id: Int) {
|
|
||||||
val widgetId = context.config.widgetNoteId
|
|
||||||
val intent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)
|
|
||||||
intent.putExtra(OPEN_NOTE_ID, widgetId)
|
|
||||||
val pendingIntent = PendingIntent.getActivity(context, widgetId, intent, 0)
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user