From 310700839353fc7e997356ca6026c92d3ba75f4a Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 10 Nov 2017 19:11:08 +0100 Subject: [PATCH] fix #127, allow scrolling the widget vertically --- app/src/main/AndroidManifest.xml | 4 ++ .../notes/adapters/WidgetAdapter.kt | 70 +++++++++++++++++++ .../notes/helpers/MyWidgetProvider.kt | 43 ++++-------- .../notes/services/WidgetService.kt | 9 +++ app/src/main/res/layout/widget.xml | 31 ++------ .../main/res/layout/widget_text_layout.xml | 35 ++++++++++ 6 files changed, 136 insertions(+), 56 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/notes/adapters/WidgetAdapter.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/notes/services/WidgetService.kt create mode 100644 app/src/main/res/layout/widget_text_layout.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4e98f14f..c91c587c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -73,6 +73,10 @@ android:label="@string/settings" android:parentActivityName=".activities.MainActivity"/> + + diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/adapters/WidgetAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/adapters/WidgetAdapter.kt new file mode 100644 index 00000000..41cd1247 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/adapters/WidgetAdapter.kt @@ -0,0 +1,70 @@ +package com.simplemobiletools.notes.adapters + +import android.content.Context +import android.content.Intent +import android.view.View +import android.widget.RemoteViews +import android.widget.RemoteViewsService +import com.simplemobiletools.commons.extensions.setText +import com.simplemobiletools.commons.extensions.setTextSize +import com.simplemobiletools.notes.R +import com.simplemobiletools.notes.R.id.widget_text_holder +import com.simplemobiletools.notes.extensions.config +import com.simplemobiletools.notes.extensions.dbHelper +import com.simplemobiletools.notes.extensions.getNoteStoredValue +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.OPEN_NOTE_ID + +class WidgetAdapter(val context: Context) : RemoteViewsService.RemoteViewsFactory { + private val textIds = arrayOf(R.id.widget_text_left, R.id.widget_text_center, R.id.widget_text_right) + val config = context.config + val widgetTextColor = config.widgetTextColor + + override fun getViewAt(position: Int): RemoteViews { + val views = RemoteViews(context.packageName, R.layout.widget_text_layout).apply { + val note = context.dbHelper.getNote(context.config.widgetNoteId) + if (note != null) { + val noteText = context.getNoteStoredValue(note) ?: "" + val textSize = context.getTextSize() / context.resources.displayMetrics.density + for (id in textIds) { + setText(id, noteText) + setTextColor(id, widgetTextColor) + setTextSize(id, textSize) + setViewVisibility(id, View.GONE) + } + } + + Intent().apply { + putExtra(OPEN_NOTE_ID, context.config.widgetNoteId) + setOnClickFillInIntent(widget_text_holder, this) + } + + setViewVisibility(getProperTextView(context), View.VISIBLE) + } + return views + } + + private fun getProperTextView(context: Context) = when (context.config.gravity) { + GRAVITY_CENTER -> R.id.widget_text_center + GRAVITY_RIGHT -> R.id.widget_text_right + else -> R.id.widget_text_left + } + + override fun onCreate() {} + + override fun getLoadingView() = null + + override fun getItemId(position: Int) = position.toLong() + + override fun onDataSetChanged() {} + + override fun hasStableIds() = true + + override fun getCount() = 1 + + override fun getViewTypeCount() = 1 + + override fun onDestroy() {} +} diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/helpers/MyWidgetProvider.kt b/app/src/main/kotlin/com/simplemobiletools/notes/helpers/MyWidgetProvider.kt index 20a48f52..4333510d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/helpers/MyWidgetProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/helpers/MyWidgetProvider.kt @@ -6,17 +6,13 @@ import android.appwidget.AppWidgetProvider import android.content.ComponentName import android.content.Context import android.content.Intent -import android.view.View +import android.net.Uri import android.widget.RemoteViews 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.activities.SplashActivity import com.simplemobiletools.notes.extensions.config -import com.simplemobiletools.notes.extensions.dbHelper -import com.simplemobiletools.notes.extensions.getNoteStoredValue -import com.simplemobiletools.notes.extensions.getTextSize +import com.simplemobiletools.notes.services.WidgetService class MyWidgetProvider : AppWidgetProvider() { override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { @@ -24,42 +20,29 @@ class MyWidgetProvider : AppWidgetProvider() { } private fun performUpdate(context: Context) { - val config = context.config - val widgetBgColor = config.widgetBgColor - val widgetTextColor = config.widgetTextColor - - 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) + views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor) + setupAppOpenIntent(context, views, R.id.notes_widget_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) - } - } + Intent(context, WidgetService::class.java).apply { + data = Uri.parse(this.toUri(Intent.URI_INTENT_SCHEME)) + views.setRemoteAdapter(R.id.notes_widget_listview, this) } - views.setViewVisibility(getProperTextView(context), View.VISIBLE) + val widgetId = context.config.widgetNoteId + val startActivityIntent = Intent(context, SplashActivity::class.java) + 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 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 setupAppOpenIntent(context: Context, views: RemoteViews, id: Int) { val widgetId = context.config.widgetNoteId val intent = Intent(context, SplashActivity::class.java) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/services/WidgetService.kt b/app/src/main/kotlin/com/simplemobiletools/notes/services/WidgetService.kt new file mode 100644 index 00000000..f0670a3b --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/services/WidgetService.kt @@ -0,0 +1,9 @@ +package com.simplemobiletools.notes.services + +import android.content.Intent +import android.widget.RemoteViewsService +import com.simplemobiletools.notes.adapters.WidgetAdapter + +class WidgetService : RemoteViewsService() { + override fun onGetViewFactory(intent: Intent) = WidgetAdapter(applicationContext) +} diff --git a/app/src/main/res/layout/widget.xml b/app/src/main/res/layout/widget.xml index b7ab225c..ea3e025f 100644 --- a/app/src/main/res/layout/widget.xml +++ b/app/src/main/res/layout/widget.xml @@ -1,35 +1,14 @@ - - - - - + android:layout_height="wrap_content" + android:divider="@null"/> diff --git a/app/src/main/res/layout/widget_text_layout.xml b/app/src/main/res/layout/widget_text_layout.xml new file mode 100644 index 00000000..038132ac --- /dev/null +++ b/app/src/main/res/layout/widget_text_layout.xml @@ -0,0 +1,35 @@ + + + + + + + + + +