Skip to content

Commit

Permalink
Glance Widget Solution
Browse files Browse the repository at this point in the history
Change-Id: Ie75625aa5ca568f41f94ef58b98a583c79aebe0d
  • Loading branch information
secondsun committed Mar 28, 2024
1 parent 0725659 commit c2852dd
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 10 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ dependencies {
androidTestImplementation(libs.espresso.core)
baselineProfile(project(":baselineprofile"))

implementation(libs.glance.appwidget)
implementation(libs.glance.material)

val composeBom = platform(libs.compose.bom)
implementation(composeBom)
androidTestImplementation(composeBom)
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@
android:supportsRtl="true"
android:theme="@style/Theme.Social"
tools:targetApi="34">

<receiver
android:name=".widget.SociaLiteAppWidgetReceiver"
android:exported="true"
android:label="@string/favorite_contact_widget_name">


<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>


<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/socialite_widget_info" />
</receiver>


<activity
android:name=".MainActivity"
android:exported="true"
Expand All @@ -47,6 +65,7 @@
android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize"
android:supportsPictureInPicture="true">


<!-- This activity is the one that's shown in the launcher. -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.glance.appwidget.updateAll
import com.google.android.samples.socialite.ui.Main
import com.google.android.samples.socialite.ui.ShortcutParams
import com.google.android.samples.socialite.widget.SociaLiteAppWidget
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.runBlocking

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
runBlocking { SociaLiteAppWidget().updateAll(this@MainActivity) }
setContent {
Main(
shortcutParams = extractShortcutParams(intent),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,67 @@
package com.google.android.samples.socialite.widget

import android.content.Context
import android.content.Intent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.core.net.toUri
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.GlanceTheme
import androidx.glance.ImageProvider
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetManager
import androidx.glance.appwidget.action.actionStartActivity
import androidx.glance.appwidget.components.Scaffold
import androidx.glance.appwidget.components.TitleBar
import androidx.glance.appwidget.provideContent
import androidx.glance.layout.fillMaxSize
import com.google.android.samples.socialite.MainActivity
import com.google.android.samples.socialite.R
import com.google.android.samples.socialite.widget.model.WidgetModelRepository
import com.google.android.samples.socialite.widget.ui.FavoriteContact
import com.google.android.samples.socialite.widget.ui.ZeroState

class SociaLiteAppWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
TODO("Not yet implemented")
provideContent {
GlanceTheme {
Content(context, id)
}
}
}

@Composable
private fun Content(context: Context, id: GlanceId) {
val widgetId = GlanceAppWidgetManager(context).getAppWidgetId(id)
val repository = WidgetModelRepository.get(context)
val model = repository.loadModel(widgetId).collectAsState(null).value

Scaffold(
titleBar = {
TitleBar(
textColor = GlanceTheme.colors.onSurface,
startIcon = ImageProvider(R.drawable.ic_launcher_monochrome),
title = "SociaLite",
)
},
backgroundColor = GlanceTheme.colors.widgetBackground,
modifier = GlanceModifier.fillMaxSize(),
) {
when (model) {
null -> ZeroState(repository, widgetId, context)
else -> {
FavoriteContact(
model,
actionStartActivity(
Intent(context.applicationContext, MainActivity::class.java)
.setAction(Intent.ACTION_VIEW)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.setData("https://socialite.google.com/chat/${model.contactId}".toUri()),
),
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@

package com.google.android.samples.socialite.widget

import android.content.Context
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import com.google.android.samples.socialite.widget.model.WidgetModelRepository
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class SociaLiteAppWidgetReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = TODO("Create instance of SociaLiteAppWidget")
override val glanceAppWidget: GlanceAppWidget = SociaLiteAppWidget()

@Inject
lateinit var repository: WidgetModelRepository

override fun onDeleted(context: Context, appWidgetIds: IntArray) {
super.onDeleted(context, appWidgetIds)
repository.cleanupWidgetModels(context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ import com.google.android.samples.socialite.widget.model.WidgetModel
fun FavoriteContact(model: WidgetModel, onClick: Action) {
Box(
modifier = GlanceModifier.fillMaxSize().clickable(onClick)
.cornerRadius(8.dp).padding(bottom = 8.dp),
.cornerRadius(8.dp).padding(bottom = 8.dp),
contentAlignment = Alignment.TopCenter,
) {
Image(
modifier = GlanceModifier.fillMaxSize().cornerRadius( 8.dp),
modifier = GlanceModifier.fillMaxSize().cornerRadius(8.dp),
provider = ImageProvider(model.photo.toUri()),
contentScale = ContentScale.Crop,
contentDescription = model.displayName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import androidx.glance.appwidget.lazy.LazyColumn
import androidx.glance.layout.Alignment
import androidx.glance.layout.Box
import androidx.glance.layout.fillMaxSize
import androidx.glance.layout.size
import com.google.android.samples.socialite.model.Contact
import com.google.android.samples.socialite.widget.model.WidgetModel
import com.google.android.samples.socialite.widget.model.WidgetModelRepository
import kotlinx.coroutines.runBlocking

@Composable
fun ZeroState(repository: WidgetModelRepository, widgetId: Int, context: Context) {
Expand All @@ -34,11 +35,19 @@ fun ZeroState(repository: WidgetModelRepository, widgetId: Int, context: Context
items(Contact.CONTACTS.size) { contactIndex ->
val contact = Contact.CONTACTS[contactIndex]

ContactRow(
contact = contact,
profileImageUri = contact.iconUri,
onClick = TODO(),
)
ContactRow(contact, contact.iconUri) {
runBlocking {
repository.create(
WidgetModel(
widgetId,
contact.id,
contact.name,
contact.iconUri.toString(),
false,
),
)
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
<string name="pause_title">Pause</string>
<string name="ff_title">Fast forward</string>
<string name="rw_title">Rewind</string>
<string name="favorite_contact_widget_name">Favorite Contact</string>

</resources>
29 changes: 29 additions & 0 deletions app/src/main/res/xml-v31/socialite_widget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
~ Copyright (C) 2024 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:resizeMode="horizontal|vertical"
android:minHeight="128dp"
android:minWidth="128dp"
android:updatePeriodMillis="3600000"
android:minResizeHeight="128dp"
android:minResizeWidth="128dp"
android:maxResizeHeight="512dp"
android:maxResizeWidth="512dp"
android:targetCellWidth="2"
android:targetCellHeight="2"
android:initialLayout="@layout/glance_default_loading_layout">
</appwidget-provider>
9 changes: 9 additions & 0 deletions app/src/main/res/xml/socialite_widget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:resizeMode="horizontal|vertical"
android:minHeight="128dp"
android:minWidth="128dp"
android:updatePeriodMillis="3600000"
android:minResizeHeight="128dp"
android:minResizeWidth="128dp"
android:initialLayout="@layout/glance_default_loading_layout">
</appwidget-provider>

0 comments on commit c2852dd

Please sign in to comment.