mirror of
				https://github.com/SimpleMobileTools/Simple-Notes.git
				synced 2025-06-05 17:00:23 +02:00 
			
		
		
		
	rewrite the widget to the more modern way of handling
This commit is contained in:
		| @@ -35,10 +35,10 @@ val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext) | ||||
|  | ||||
| fun Context.getNoteStoredValue(note: Note): String? { | ||||
|     return if (note.path.isNotEmpty()) { | ||||
|         try { | ||||
|             return File(note.path).readText() | ||||
|         return try { | ||||
|             File(note.path).readText() | ||||
|         } catch (e: FileNotFoundException) { | ||||
|             return null | ||||
|             null | ||||
|         } | ||||
|     } else { | ||||
|         note.value | ||||
|   | ||||
| @@ -3,6 +3,7 @@ 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.view.View | ||||
| @@ -11,7 +12,6 @@ import com.simplemobiletools.commons.extensions.setBackgroundColor | ||||
| import com.simplemobiletools.commons.extensions.setText | ||||
| import com.simplemobiletools.commons.extensions.setTextSize | ||||
| import com.simplemobiletools.notes.R | ||||
| import com.simplemobiletools.notes.R.layout.widget | ||||
| import com.simplemobiletools.notes.activities.SplashActivity | ||||
| import com.simplemobiletools.notes.extensions.config | ||||
| import com.simplemobiletools.notes.extensions.dbHelper | ||||
| @@ -19,63 +19,52 @@ import com.simplemobiletools.notes.extensions.getNoteStoredValue | ||||
| import com.simplemobiletools.notes.extensions.getTextSize | ||||
|  | ||||
| class MyWidgetProvider : AppWidgetProvider() { | ||||
|     lateinit var mDb: DBHelper | ||||
|     var textIds = arrayOf(R.id.notes_view_left, R.id.notes_view_center, R.id.notes_view_right) | ||||
|  | ||||
|     companion object { | ||||
|         lateinit var mRemoteViews: RemoteViews | ||||
|     override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { | ||||
|         performUpdate(context) | ||||
|     } | ||||
|  | ||||
|     override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { | ||||
|         initVariables(context) | ||||
|     private fun performUpdate(context: Context) { | ||||
|         val config = context.config | ||||
|         val widgetBgColor = config.widgetBgColor | ||||
|         val widgetTextColor = config.widgetTextColor | ||||
|  | ||||
|         for (id in textIds) { | ||||
|             mRemoteViews.apply { | ||||
|                 setBackgroundColor(id, widgetBgColor) | ||||
|                 setTextColor(id, widgetTextColor) | ||||
|                 setTextSize(id, context.getTextSize() / context.resources.displayMetrics.density) | ||||
|                 setViewVisibility(id, View.GONE) | ||||
|         val textIds = arrayOf(R.id.notes_view_left, R.id.notes_view_center, R.id.notes_view_right) | ||||
|         val appWidgetManager = AppWidgetManager.getInstance(context) | ||||
|         appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach { | ||||
|             val views = RemoteViews(context.packageName, R.layout.widget) | ||||
|             setupAppOpenIntent(context, views, R.id.notes_holder) | ||||
|  | ||||
|             val note = context.dbHelper.getNote(context.config.widgetNoteId) | ||||
|             for (id in textIds) { | ||||
|                 if (note != null) { | ||||
|                     views.apply { | ||||
|                         setText(id, context.getNoteStoredValue(note)!!) | ||||
|                         setBackgroundColor(id, widgetBgColor) | ||||
|                         setTextColor(id, widgetTextColor) | ||||
|                         setTextSize(id, context.getTextSize() / context.resources.displayMetrics.density) | ||||
|                         setViewVisibility(id, View.GONE) | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         mRemoteViews.setViewVisibility(getProperTextView(context), View.VISIBLE) | ||||
|  | ||||
|         for (widgetId in appWidgetIds) { | ||||
|             updateWidget(appWidgetManager, widgetId, mRemoteViews, context) | ||||
|             views.setViewVisibility(getProperTextView(context), View.VISIBLE) | ||||
|             appWidgetManager.updateAppWidget(it, views) | ||||
|         } | ||||
|         super.onUpdate(context, appWidgetManager, appWidgetIds) | ||||
|     } | ||||
|  | ||||
|     private fun getComponentName(context: Context) = ComponentName(context, MyWidgetProvider::class.java) | ||||
|  | ||||
|     private fun getProperTextView(context: Context) = when (context.config.gravity) { | ||||
|         GRAVITY_CENTER -> R.id.notes_view_center | ||||
|         GRAVITY_RIGHT -> R.id.notes_view_right | ||||
|         else -> R.id.notes_view_left | ||||
|     } | ||||
|  | ||||
|     private fun initVariables(context: Context) { | ||||
|         mDb = context.dbHelper | ||||
|         mRemoteViews = RemoteViews(context.packageName, widget) | ||||
|         setupAppOpenIntent(R.id.notes_holder, context) | ||||
|     } | ||||
|  | ||||
|     private fun setupAppOpenIntent(id: Int, context: Context) { | ||||
|     private fun setupAppOpenIntent(context: Context, views: RemoteViews, id: Int) { | ||||
|         val widgetId = context.config.widgetNoteId | ||||
|         Intent(context, SplashActivity::class.java).apply { | ||||
|             putExtra(OPEN_NOTE_ID, widgetId) | ||||
|             val pendingIntent = PendingIntent.getActivity(context, widgetId, this, 0) | ||||
|             mRemoteViews.setOnClickPendingIntent(id, pendingIntent) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private fun updateWidget(widgetManager: AppWidgetManager, widgetId: Int, remoteViews: RemoteViews, context: Context) { | ||||
|         val note = mDb.getNote(context.config.widgetNoteId) | ||||
|         for (id in textIds) { | ||||
|             if (note != null) | ||||
|                 remoteViews.setText(id, context.getNoteStoredValue(note)!!) | ||||
|         } | ||||
|         widgetManager.updateAppWidget(widgetId, remoteViews) | ||||
|         val intent = Intent(context, SplashActivity::class.java) | ||||
|         intent.putExtra(OPEN_NOTE_ID, widgetId) | ||||
|         val pendingIntent = PendingIntent.getActivity(context, widgetId, intent, 0) | ||||
|         views.setOnClickPendingIntent(id, pendingIntent) | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user