mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-02-03 15:47:40 +01:00
adding some code related to updating specific widgets only
This commit is contained in:
parent
3bd04a6eb6
commit
e9ebe65ea6
@ -25,10 +25,7 @@ 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.config
|
||||
import com.simplemobiletools.notes.extensions.dbHelper
|
||||
import com.simplemobiletools.notes.extensions.getTextSize
|
||||
import com.simplemobiletools.notes.extensions.updateWidget
|
||||
import com.simplemobiletools.notes.extensions.*
|
||||
import com.simplemobiletools.notes.helpers.MIME_TEXT_PLAIN
|
||||
import com.simplemobiletools.notes.helpers.OPEN_NOTE_ID
|
||||
import com.simplemobiletools.notes.helpers.TYPE_NOTE
|
||||
@ -40,7 +37,7 @@ import java.nio.charset.Charset
|
||||
class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
||||
private var mAdapter: NotesPagerAdapter? = null
|
||||
|
||||
lateinit var mCurrentNote: Note
|
||||
private lateinit var mCurrentNote: Note
|
||||
private var mNotes = ArrayList<Note>()
|
||||
|
||||
private var noteViewWithTextSelected: MyEditText? = null
|
||||
@ -533,7 +530,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
||||
updateSelectedNote(firstNoteId)
|
||||
if (config.widgetNoteId == note.id) {
|
||||
config.widgetNoteId = mCurrentNote.id
|
||||
updateWidget()
|
||||
updateNoteWidget(mCurrentNote.id)
|
||||
}
|
||||
invalidateOptionsMenu()
|
||||
initViewPager()
|
||||
|
@ -3,7 +3,6 @@ package com.simplemobiletools.notes.activities
|
||||
import android.content.Intent
|
||||
import android.content.res.Resources
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.IS_CUSTOMIZING_COLORS
|
||||
@ -11,7 +10,7 @@ import com.simplemobiletools.commons.models.RadioItem
|
||||
import com.simplemobiletools.notes.R
|
||||
import com.simplemobiletools.notes.extensions.config
|
||||
import com.simplemobiletools.notes.extensions.dbHelper
|
||||
import com.simplemobiletools.notes.extensions.updateWidget
|
||||
import com.simplemobiletools.notes.extensions.updateWidgets
|
||||
import com.simplemobiletools.notes.helpers.*
|
||||
import com.simplemobiletools.notes.models.Note
|
||||
import kotlinx.android.synthetic.main.activity_settings.*
|
||||
@ -45,7 +44,6 @@ class SettingsActivity : SimpleActivity() {
|
||||
setupEnableLineWrap()
|
||||
setupFontSize()
|
||||
setupGravity()
|
||||
setupWidgetNote()
|
||||
setupCursorPlacement()
|
||||
setupCustomizeWidgetColors()
|
||||
updateTextColors(settings_scrollview)
|
||||
@ -167,7 +165,7 @@ class SettingsActivity : SimpleActivity() {
|
||||
RadioGroupDialog(this@SettingsActivity, items, config.fontSize) {
|
||||
config.fontSize = it as Int
|
||||
settings_font_size.text = getFontSizeText()
|
||||
updateWidget()
|
||||
updateWidgets()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -190,7 +188,7 @@ class SettingsActivity : SimpleActivity() {
|
||||
RadioGroupDialog(this@SettingsActivity, items, config.gravity) {
|
||||
config.gravity = it as Int
|
||||
settings_gravity.text = getGravityText()
|
||||
updateWidget()
|
||||
updateWidgets()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -201,24 +199,6 @@ class SettingsActivity : SimpleActivity() {
|
||||
else -> R.string.right
|
||||
})
|
||||
|
||||
private fun setupWidgetNote() {
|
||||
if (notes.size <= 1) {
|
||||
settings_widget_note_holder.visibility = View.GONE
|
||||
return
|
||||
}
|
||||
|
||||
settings_widget_note.text = getCurrentWidgetNoteTitle(config.widgetNoteId, notes)
|
||||
settings_widget_note_holder.setOnClickListener {
|
||||
val items = notes.map { RadioItem(it.id, it.title) } as ArrayList
|
||||
|
||||
RadioGroupDialog(this@SettingsActivity, items, config.widgetNoteId) {
|
||||
config.widgetNoteId = it as Int
|
||||
settings_widget_note.text = getCurrentWidgetNoteTitle(it, notes)
|
||||
updateWidget()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupCursorPlacement() {
|
||||
settings_cursor_placement.isChecked = config.placeCursorToEnd
|
||||
settings_cursor_placement_holder.setOnClickListener {
|
||||
@ -227,9 +207,6 @@ class SettingsActivity : SimpleActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getCurrentWidgetNoteTitle(currentNoteId: Int, notes: List<Note>) =
|
||||
notes.firstOrNull { it.id == currentNoteId }?.title ?: ""
|
||||
|
||||
private fun setupCustomizeWidgetColors() {
|
||||
settings_customize_widget_colors_holder.setOnClickListener {
|
||||
Intent(this, WidgetConfigureActivity::class.java).apply {
|
||||
|
@ -14,15 +14,17 @@ import com.simplemobiletools.notes.extensions.dbHelper
|
||||
import com.simplemobiletools.notes.extensions.getTextSize
|
||||
import com.simplemobiletools.notes.helpers.GRAVITY_CENTER
|
||||
import com.simplemobiletools.notes.helpers.GRAVITY_RIGHT
|
||||
import com.simplemobiletools.notes.helpers.NOTE_ID
|
||||
import com.simplemobiletools.notes.helpers.OPEN_NOTE_ID
|
||||
|
||||
class WidgetAdapter(val context: Context) : RemoteViewsService.RemoteViewsFactory {
|
||||
class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsService.RemoteViewsFactory {
|
||||
private val textIds = arrayOf(R.id.widget_text_left, R.id.widget_text_center, R.id.widget_text_right)
|
||||
private val widgetTextColor = context.config.widgetTextColor
|
||||
|
||||
override fun getViewAt(position: Int): RemoteViews {
|
||||
val noteId = intent.getIntExtra(NOTE_ID, 1)
|
||||
val views = RemoteViews(context.packageName, R.layout.widget_text_layout).apply {
|
||||
val note = context.dbHelper.getNote(context.config.widgetNoteId)
|
||||
val note = context.dbHelper.getNoteWithId(noteId)
|
||||
if (note != null) {
|
||||
val noteText = note.getNoteStoredValue() ?: ""
|
||||
val textSize = context.getTextSize() / context.resources.displayMetrics.density
|
||||
@ -35,7 +37,7 @@ class WidgetAdapter(val context: Context) : RemoteViewsService.RemoteViewsFactor
|
||||
}
|
||||
|
||||
Intent().apply {
|
||||
putExtra(OPEN_NOTE_ID, context.config.widgetNoteId)
|
||||
putExtra(OPEN_NOTE_ID, noteId)
|
||||
setOnClickFillInIntent(widget_text_holder, this)
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
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
|
||||
@ -11,20 +9,26 @@ val Context.config: Config get() = Config.newInstance(applicationContext)
|
||||
|
||||
val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext)
|
||||
|
||||
fun Context.getTextSize() =
|
||||
when (config.fontSize) {
|
||||
FONT_SIZE_SMALL -> resources.getDimension(R.dimen.smaller_text_size)
|
||||
FONT_SIZE_LARGE -> resources.getDimension(R.dimen.big_text_size)
|
||||
FONT_SIZE_EXTRA_LARGE -> resources.getDimension(R.dimen.extra_big_text_size)
|
||||
else -> resources.getDimension(R.dimen.bigger_text_size)
|
||||
}
|
||||
fun Context.getTextSize() = when (config.fontSize) {
|
||||
FONT_SIZE_SMALL -> resources.getDimension(R.dimen.smaller_text_size)
|
||||
FONT_SIZE_LARGE -> resources.getDimension(R.dimen.big_text_size)
|
||||
FONT_SIZE_EXTRA_LARGE -> resources.getDimension(R.dimen.extra_big_text_size)
|
||||
else -> resources.getDimension(R.dimen.bigger_text_size)
|
||||
}
|
||||
|
||||
fun Context.updateWidget() {
|
||||
val widgetIDs = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, MyWidgetProvider::class.java))
|
||||
if (widgetIDs.isNotEmpty()) {
|
||||
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 = AppWidgetManager.ACTION_APPWIDGET_UPDATE
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIDs)
|
||||
action = UPDATE_WIDGET
|
||||
putExtra(WIDGET_ID, it)
|
||||
putExtra(NOTE_ID, noteId)
|
||||
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.updateWidget
|
||||
import com.simplemobiletools.notes.extensions.updateNoteWidget
|
||||
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!!.updateWidget()
|
||||
context!!.updateNoteWidget(note.id)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,8 @@ 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"
|
||||
|
@ -219,4 +219,22 @@ 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())
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = mDb.query(WIDGETS_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
val widgetId = cursor.getIntValue(COL_WIDGET_ID)
|
||||
widgetIds.add(widgetId)
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
return widgetIds
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package com.simplemobiletools.notes.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.net.Uri
|
||||
@ -16,34 +15,32 @@ import com.simplemobiletools.notes.extensions.config
|
||||
import com.simplemobiletools.notes.services.WidgetService
|
||||
|
||||
class MyWidgetProvider : AppWidgetProvider() {
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
performUpdate(context)
|
||||
}
|
||||
|
||||
private fun performUpdate(context: Context) {
|
||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
|
||||
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 {
|
||||
data = Uri.parse(this.toUri(Intent.URI_INTENT_SCHEME))
|
||||
views.setRemoteAdapter(R.id.notes_widget_listview, this)
|
||||
}
|
||||
|
||||
val widgetId = context.config.widgetNoteId
|
||||
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(it, views)
|
||||
appWidgetManager.notifyAppWidgetViewDataChanged(it, R.id.notes_widget_listview)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
private fun getComponentName(context: Context) = ComponentName(context, MyWidgetProvider::class.java)
|
||||
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
|
||||
@ -52,4 +49,13 @@ class MyWidgetProvider : AppWidgetProvider() {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,5 +5,5 @@ import android.widget.RemoteViewsService
|
||||
import com.simplemobiletools.notes.adapters.WidgetAdapter
|
||||
|
||||
class WidgetService : RemoteViewsService() {
|
||||
override fun onGetViewFactory(intent: Intent) = WidgetAdapter(applicationContext)
|
||||
override fun onGetViewFactory(intent: Intent) = WidgetAdapter(applicationContext, intent)
|
||||
}
|
||||
|
@ -483,40 +483,5 @@
|
||||
android:text="@string/customize_widget_colors"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/settings_widget_note_holder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="@dimen/medium_margin"
|
||||
android:background="?attr/selectableItemBackground"
|
||||
android:paddingBottom="@dimen/activity_margin"
|
||||
android:paddingLeft="@dimen/normal_margin"
|
||||
android:paddingRight="@dimen/normal_margin"
|
||||
android:paddingTop="@dimen/activity_margin">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
android:id="@+id/settings_widget_note_label"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_toLeftOf="@+id/settings_widget_note"
|
||||
android:layout_toStartOf="@+id/settings_widget_note"
|
||||
android:paddingLeft="@dimen/medium_margin"
|
||||
android:paddingRight="@dimen/medium_margin"
|
||||
android:text="@string/widget_note"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
android:id="@+id/settings_widget_note"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_marginEnd="@dimen/medium_margin"
|
||||
android:layout_marginRight="@dimen/medium_margin"
|
||||
android:background="@null"
|
||||
android:clickable="false"/>
|
||||
|
||||
</RelativeLayout>
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
Loading…
x
Reference in New Issue
Block a user