fix #127, allow scrolling the widget vertically

This commit is contained in:
tibbi 2017-11-10 19:11:08 +01:00
parent eb496863e8
commit 3107008393
6 changed files with 136 additions and 56 deletions

View File

@ -73,6 +73,10 @@
android:label="@string/settings"
android:parentActivityName=".activities.MainActivity"/>
<service
android:name=".services.WidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS"/>
<receiver
android:name=".helpers.MyWidgetProvider"
android:icon="@drawable/img_widget_preview">

View File

@ -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() {}
}

View File

@ -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)

View File

@ -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)
}

View File

@ -1,35 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/notes_holder"
android:id="@+id/notes_widget_holder"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/notes_view_left"
<ListView
android:id="@+id/notes_widget_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="left"
android:padding="@dimen/activity_margin"
android:visibility="gone"/>
<TextView
android:id="@+id/notes_view_center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="center_horizontal"
android:padding="@dimen/activity_margin"
android:visibility="gone"/>
<TextView
android:id="@+id/notes_view_right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="right"
android:padding="@dimen/activity_margin"
android:visibility="gone"/>
android:layout_height="wrap_content"
android:divider="@null"/>
</RelativeLayout>

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_text_holder"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/widget_text_left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="left"
android:padding="@dimen/activity_margin"
android:visibility="gone"/>
<TextView
android:id="@+id/widget_text_center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="center_horizontal"
android:padding="@dimen/activity_margin"
android:visibility="gone"/>
<TextView
android:id="@+id/widget_text_right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="right"
android:padding="@dimen/activity_margin"
android:visibility="gone"/>
</RelativeLayout>