diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6aed96f5..7d649bdc 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -11,7 +11,7 @@ androidx-coordinator-layout = "1.2.0" androidx-corektx = "1.13.1" androidx-emoji2-views = "1.5.0" androidx-fragment-ktx = "1.8.4" -androidx-glance-appwidget = "1.1.0" +androidx-glance-appwidget = "1.1.1" androidx-lifecycle-compose = "2.8.6" androidx-lifecycle-runtime-compose = "2.8.6" androidx-navigation = "2.8.2" diff --git a/settings.gradle.kts b/settings.gradle.kts index f9ba4f22..22366383 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -27,4 +27,6 @@ include( ":kotlin", ":compose:snippets", ":wear", + ":views", ) + diff --git a/views/.gitignore b/views/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/views/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/views/README.md b/views/README.md new file mode 100644 index 00000000..a9256a20 --- /dev/null +++ b/views/README.md @@ -0,0 +1 @@ +This is a sample project that contains the code snippets seen on https://android.devsite.corp.google.com/develop/ui/views diff --git a/views/build.gradle.kts b/views/build.gradle.kts new file mode 100644 index 00000000..80ee4edc --- /dev/null +++ b/views/build.gradle.kts @@ -0,0 +1,56 @@ +//Copyright 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 +// +//https://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. + +plugins { + alias(libs.plugins.android.library) + alias(libs.plugins.kotlin.android) +} + +android { + namespace = "com.example.example.snippet.views" + compileSdk = 35 + + defaultConfig { + minSdk = 35 + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles("consumer-rules.pro") + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = "1.8" + } +} + +dependencies { + + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.appcompat) + implementation(libs.google.android.material) + implementation(libs.androidx.glance.appwidget) + +} \ No newline at end of file diff --git a/views/consumer-rules.pro b/views/consumer-rules.pro new file mode 100644 index 00000000..e69de29b diff --git a/views/proguard-rules.pro b/views/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/views/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/views/src/main/AndroidManifest.xml b/views/src/main/AndroidManifest.xml new file mode 100644 index 00000000..65ba8e1e --- /dev/null +++ b/views/src/main/AndroidManifest.xml @@ -0,0 +1,20 @@ + + + + + \ No newline at end of file diff --git a/views/src/main/java/com/example/example/snippet/views/appwidget/AppWidgetSnippets.kt b/views/src/main/java/com/example/example/snippet/views/appwidget/AppWidgetSnippets.kt new file mode 100644 index 00000000..cd6ac485 --- /dev/null +++ b/views/src/main/java/com/example/example/snippet/views/appwidget/AppWidgetSnippets.kt @@ -0,0 +1,73 @@ +/* + * Copyright 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 + * + * https://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. + */ + +package com.example.example.snippet.views.appwidget + +import android.appwidget.AppWidgetManager +import android.appwidget.AppWidgetProvider +import android.appwidget.AppWidgetProviderInfo +import android.content.ComponentName +import android.content.Context +import android.widget.RemoteViews +import androidx.glance.GlanceId +import androidx.glance.appwidget.GlanceAppWidget +import androidx.glance.appwidget.compose +import com.example.example.snippet.views.R + +class ExampleAppWidget:GlanceAppWidget() { + override suspend fun provideGlance(context: Context, id: GlanceId) { + TODO("Not yet implemented") + } + +} + +private object GeneratedPreviewWithoutGlance { + + lateinit var appContext: Context + + fun MyWidgetPreview() { + // [START android_view_appwidget_generatedpreview_with_remoteview] + AppWidgetManager.getInstance(appContext).setWidgetPreview( + ComponentName( + appContext, + ExampleAppWidgetReceiver::class.java + ), + AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN, + RemoteViews("com.example", R.layout.widget_preview) + ) + // [END android_view_appwidget_generatedpreview_with_remoteview] + } + + suspend fun MyGlanceWidgetPreview() { + // [START android_view_appwidget_generatedpreview_with_glance] + AppWidgetManager.getInstance(appContext).setWidgetPreview( + ComponentName( + appContext, + ExampleAppWidgetReceiver::class.java + ), + AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN, + ExampleAppWidget().compose( + context = appContext + ), + ) + + // [END android_view_appwidget_generatedpreview_with_glance] + } +} + +class ExampleAppWidgetReceiver: AppWidgetProvider() { + +} diff --git a/views/src/main/res/layout/widget_preview.xml b/views/src/main/res/layout/widget_preview.xml new file mode 100644 index 00000000..c9f449bb --- /dev/null +++ b/views/src/main/res/layout/widget_preview.xml @@ -0,0 +1,24 @@ + + + + + \ No newline at end of file