I have a ContentProvider from a main app. The content will be shared with a consumer app. This consumer app has an app widget. I have tested the ContentProvider and ContentObserver to this consumer app in its Activity and all is well (meaning the RecyclerView of the Activity is updated whenever an update from the main app triggers changes to the database). However, registering the ContentObserver inside my AppWidgetProvider does not work as expected.
My AppWidgetProvider has the following code.
class StackWidgetProvider : AppWidgetProvider() {
override fun onEnabled(context: Context) {
Timber.i("Enabled")
if (favoriteUserProviderObserver == null) {
val appWidgetManager = AppWidgetManager.getInstance(context)
val componentName = ComponentName(context, StackWidgetProvider::class.java)
favoriteUserProviderObserver = FavoriteUserProviderObserver(appWidgetManager, componentName).let {
context.contentResolver.registerContentObserver(CONTENT_URI, true, it)
it
}
}
}
override fun onDisabled(context: Context) {
Timber.i("Disabled")
favoriteUserProviderObserver?.let {
context.contentResolver.unregisterContentObserver(it)
}
favoriteUserProviderObserver = null
}
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
}
}
....
companion object {
private var favoriteUserProviderObserver: FavoriteUserProviderObserver? = null
private fun updateAppWidget(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
val intent = Intent(context, StackWidgetService::class.java).apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
data = toUri(Intent.URI_INTENT_SCHEME).toUri()
}
val views = RemoteViews(context.packageName, R.layout.widget_favorite_user_stack).apply {
setRemoteAdapter(R.id.widget_favorite_user_stack_view, intent)
setEmptyView(R.id.widget_favorite_user_stack_view, R.id.widget_favorite_user_empty)
}
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
}
I created a simple custom ContentObserver class like below.
class FavoriteUserProviderObserver(
private val appWidgetManager: AppWidgetManager,
private val componentName: ComponentName
) : ContentObserver(null) {
override fun onChange(selfChange: Boolean) {
Timber.i("Provider observer triggered")
appWidgetManager.notifyAppWidgetViewDataChanged(
appWidgetManager.getAppWidgetIds(componentName), R.id.widget_favorite_user_stack_view
)
}
}
The above observer class is never triggered (even when I change the data in my main app). For further clarity, here's the code for my RemoteViewsService and its factory.
class StackWidgetService : RemoteViewsService() {
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory =
StackRemoteViewsFactory(this.applicationContext)
}
class StackRemoteViewsFactory(private val context: Context) :
RemoteViewsService.RemoteViewsFactory {
private var widgetItems = listOf<UserProfileSummary>()
private lateinit var repository: FavoriteUserRepository
override fun onCreate() {
repository = FavoriteUserRepository(
FavoriteUserDataSource(context.contentResolver),
Dispatchers.IO
)
}
override fun onDataSetChanged() {
GlobalScope.launch {
widgetItems = repository.favoriteUsers().toList() // Tested; working on the Activity scope of the consumer app
Timber.i(widgetItems.toString())
}
}
override fun getViewAt(position: Int): RemoteViews =
RemoteViews(context.packageName, R.layout.widget_favorite_user_item).apply {
setTextViewText(R.id.widget_favorite_user_item_text, widgetItems[position].username)
}
override fun getLoadingView(): RemoteViews? = null
override fun getItemId(position: Int): Long = 0
override fun hasStableIds(): Boolean = false
override fun getCount(): Int {
return widgetItems.size
}
override fun getViewTypeCount(): Int = 1
override fun onDestroy() {}
}
So the logic is to ask the ContentObserver to observe changes in the ContentProvider. The observer is registered on the onEnabled and onDisabled part of the AppWidgetProvider. Once the observer notices a change in ContentProvider, it will ask the AppWidgetProvider to update itself, thus calling onDataSetChanged and fetching a new list of data.
However, the observer is never called. What could be the reason it's not working as expected here? (It can't be because of a lack of permission, because the Activity part of the consumer app is able to fetch the data just fine.)
What could be the reason it's not working as expected here?
An AppWidgetProvider is a subclass of BroadcastReceiver. Your instance of AppWidgetProvider will live for (hopefully) a very short time, best measured in milliseconds. Basically, you get one onUpdate() call (or other callback), and that instance is thrown away. The next callback gets a new instance.
As a result, doing anything in an AppWidgetProvider that requires it to be around for a period of time is doomed.
The most likely solution, taking modern versions of Android into account, is to adopt more of a push solution. Bear in mind that any of your code can update the RemoteViews for an app widget, simply by working with AppWidgetManager. So, some code that is already running and knows about the data updates needs to push a new RemoteViews, rather than expecting your AppWidgetProvider to be able to react to changes.
Related
I am running a worker in a glance composable on android.
why is onReceive being continuously called in an infinite loop?
What am I missing here?
class MyWidget : GlanceAppWidget() {
#Composable
override fun Content() {
val work = OneTimeWorkRequest.Builder(MyWorker::class.java).build()
WorkManager.getInstance().enqueue(work)
}
}
class MyWorker(
private val context: Context,
private val workerParameters: WorkerParameters
) : CoroutineWorker(context, workerParameters) {
override suspend fun doWork(): Result {
return Result.success()
}
}
class GlanceReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget
get() = MyWidget()
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
}
}
//Dependencies
implementation "androidx.work:work-runtime-ktx:2.7.1" // WorkManager with Coroutines
implementation "androidx.glance:glance-appwidget:1.0.0-alpha03" //Glance
That's because WM is disabling the on boot receiver when there are no workers scheduled. When an android app disables a receiver Android OS sends the PACKAGE_CHANGED broadcast event, causing the widget onReceive to be called.
https://issuetracker.google.com/115575872
For now the recommendation is to schedule a work with a long delay (e.g 10 years). We are working on a way to improve this.
I am trying to load list of data in App Widget using jetpack compose and i have stored in Room Local database, how i can retrive the data in GlanceAppWidget class.
You need to work with GlanceAppWidgetReceiver class. You can create coroutine and access your domain layer. After that, you can find your GlanceAppWidget class and send your data your widget class. Please have look this article to full example : https://medium.com/better-programming/android-jetpack-glance-for-app-widgets-bd7a704624ba
#AndroidEntryPoint
class MarketWidgetReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = MarketWidget()
private val coroutineScope = MainScope()
#Inject
lateinit var marketInformationUseCase: MarketInformationUseCase
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
observeData(context)
}
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
if (intent.action == MarketRefreshCallback.UPDATE_ACTION) {
observeData(context)
}
}
private fun observeData(context: Context) {
coroutineScope.launch {
val marketInformation =
marketInformationUseCase.getMarketInformation(MarketInformationTimespan.TIMESPAN_1DAYS)
val glanceId =
GlanceAppWidgetManager(context).getGlanceIds(MarketWidget::class.java).firstOrNull()
glanceId?.let {
updateAppWidgetState(context, PreferencesGlanceStateDefinition, it) { pref ->
pref.toMutablePreferences().apply {
this[currentPrice] =
marketInformation.currentPrice
this[changeRate] =
marketInformation.changeRate
this[isChangeRatePositive] =
marketInformation.changeStatus == MarketInformationChangeStatus.POSITIVE
}
}
glanceAppWidget.update(context, it)
}
}
}
companion object {
val currentPrice = stringPreferencesKey("currentPrice")
val changeRate = stringPreferencesKey("changeRate")
val isChangeRatePositive = booleanPreferencesKey("isChangeRatePositive")
}
}
I'm writing tests to verify the reception of the transmitting receivers but for some reason, the receiver is never registered or the intent is never sent.
I guess there should be a problem with the Context but, no luck yet finding it.
This is the BroadcastFactory.kt:
object BroadcastFactory {
private lateinit var intent: Intent
fun build(
action: String,
flag: Int? = null,
): BroadcastFactory {
intent = Intent().apply {
this.action = action
this.flags = flag ?: 0
}
return this
}
fun send(
context: Context
): Intent {
context.sendBroadcast(intent)
return intent
}
}
And this is the test file BroadcastTest.kt:
#RunWith(AndroidJUnit4::class)
#SmallTest
class BroadcastTest {
lateinit var intents: MutableList<Intent>
lateinit var latch: CountDownLatch
private lateinit var receiver: BroadcastReceiverTester
inner class BroadcastReceiverTester : BroadcastReceiver() {
override fun onReceive(p0: Context?, intent: Intent?) {
intent?.let {
intents.add(it)
latch.countDown()
}
}
}
private val context: Context = getInstrumentation().targetContext
#Before
fun setUp() {
intents = mutableListOf()
latch = CountDownLatch(1)
receiver = BroadcastReceiverTester()
LocalBroadcastManager.getInstance(context).registerReceiver(
receiver,
IntentFilter.create(
Constants.ACTION, "text/plain"
)
)
}
#Test
fun testBroadcastReception() {
BroadcastFactory
.build(Constants.ACTION, Constants.FLAG)
.send(context)
// assert broadcast reception (NOT WORKING)
latch.await(10, TimeUnit.SECONDS)
assertThat(intents.size).isEqualTo(1)
}
#After
fun tearDown() {
LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver)
}
}
I'm using a CountDownLatch to wait 10 seconds for the receiver, plus, its value can be asserted. Besides, I set a list of Intents to check the number of registrations/receptions.
There is something I'm missing here? Different context provider? Robolectric runner?
Thanks
Is solved it by changing the receiver with this:
context.registerReceiver(
receiver,
IntentFilter(
Constants.ACTION
)
)
Thanks to #selvin and #mike-m for the help!
I have an Retrofit2 API:
interface Api {
#POST("/my/url")
suspend fun function()
}
My ViewModel can call this HTTP function:
class MainViewModel : ViewModel() {
private val retrofiClient = APIClient.client!!.create(Api::class.java)
fun test() {
viewModelScope.launch {
retrofitClient.function()
}
}
}
No problem when i call this from my activity.
But my goal is to call this test() function from my app's widget.
My basic AppWidgetProvider looks like this:
class AppWidgetProvider : AppWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
// Perform this loop procedure for each App Widget that belongs to this provider
appWidgetIds.forEach { appWidgetId ->
val intent = Intent()
intent.action = "custom-event-name"
intent.setClassName(
MainActivity::class.java.getPackage().name,
MainActivity::class.java.name
)
val pendingIntent = PendingIntent.getBroadcast(
context.applicationContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
)
val views: RemoteViews = RemoteViews(
context.packageName,
R.layout.appwidget
).apply {
setOnClickPendingIntent(R.id.test, pendingIntent)
}
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
}
MainActivity:
class MainActivity : AppCompatActivity() {
private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
println("INTENT RECEIVED")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
...
registerReceiver(receiver, IntentFilter("custom-event-name"))
...
}
The BroadcastReceiver does not receive the intent. Even if it does, can i call my app functions there?
How can i setup my app and/or widget, to call this viewModel.test() method when i click on a button in the widget?
You can use Channel(https://kotlinlang.org/docs/channels.html, for coroutines) or RxRelay (https://github.com/JakeWharton/RxRelay, for rxjava) instead of using broadcast receiver. Just put your Channel or Relay instance on your Application class so you can access it across your activities, fragments, etc.
I am new to Android Studio and my problem is my widget is not updating. Let's say I changed my data JSON. I have read some threads and says that a widget has a time interval of 30mins to refresh. But I have waited for my simulator 30mins and nothing changed. Can someone help me out on this ? Because I want my widget to update atleast 30mins default..
class TestWidget : AppWidgetProvider() {
private val httpClient = AsyncHttpClient()
private var title = String.toString()
private var imageUrl = String.toString()
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
// There may be multiple widgets active, so update all of them
httpClient.get(jsonUrl, object : JsonHttpResponseHandler() {
override fun onSuccess(statusCode: Int, headers: Array<out Header>, response: JSONArray) {
val json = response.getJSONObject(0)
title = json.getString("title")
imageUrl = json.getString("imageUrl")
val views = RemoteViews(context.packageName, R.layout.test_widget)
views.setTextViewText(R.id.text, title)
Picasso.with(context)
.load(imageUrl)
.into(views, R.id.image, appWidgetIds)
}
override fun onFailure(statusCode: Int, headers: Array<out Header>?, throwable: Throwable?, errorResponse: JSONObject?) {
println(throwable?.localizedMessage)
}
})
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
}
}
override fun onEnabled(context: Context) {
// Enter relevant functionality for when the first widget is created
}
override fun onDisabled(context: Context) {
// Enter relevant functionality for when the last widget is disabled
}
companion object {
internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager,
appWidgetId: Int) {
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.test_widget)
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
}
OH, I think i have found a solution.
There is a android:updatePeriodMillis on my Widget Info XML. That should do the trick.