1 /* 2 * Copyright 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.core.widget 18 19 import android.annotation.TargetApi 20 import android.app.Activity 21 import android.appwidget.AppWidgetHost 22 import android.appwidget.AppWidgetHostView 23 import android.appwidget.AppWidgetManager 24 import android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT 25 import android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH 26 import android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT 27 import android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH 28 import android.appwidget.AppWidgetManager.OPTION_APPWIDGET_SIZES 29 import android.content.ComponentName 30 import android.os.Build 31 import android.os.Bundle 32 import android.util.SizeF 33 import android.view.WindowManager 34 import android.widget.FrameLayout 35 import androidx.core.remoteviews.test.R 36 import org.junit.Assert.fail 37 38 /** Test activity that contains an [AppWidgetHost]. */ 39 @TargetApi(29) 40 public class AppWidgetHostTestActivity : Activity() { 41 private var mHost: AppWidgetHost? = null 42 onCreatenull43 override fun onCreate(savedInstanceState: Bundle?) { 44 super.onCreate(savedInstanceState) 45 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) 46 setContentView(R.layout.app_widget_host_activity) 47 48 mHost = AppWidgetHost(this, 1).also { it.startListening() } 49 } 50 onDestroynull51 override fun onDestroy() { 52 super.onDestroy() 53 mHost?.stopListening() 54 mHost?.deleteHost() 55 mHost = null 56 } 57 bindAppWidgetnull58 public fun bindAppWidget(): AppWidgetHostView { 59 val host = mHost ?: error("App widgets can only be bound while the activity is created") 60 61 val appWidgetManager = AppWidgetManager.getInstance(this) 62 val appWidgetId = host.allocateAppWidgetId() 63 val componentName = ComponentName(this, TestAppWidgetProvider::class.java) 64 65 val wasBound = appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, componentName) 66 if (!wasBound) { 67 fail("Failed to bind the app widget") 68 } 69 70 val info = appWidgetManager.getAppWidgetInfo(appWidgetId) 71 val hostView = host.createView(this, appWidgetId, info) 72 val contentFrame = findViewById<FrameLayout>(R.id.content) 73 contentFrame.addView( 74 hostView, 75 FrameLayout.LayoutParams( 76 FrameLayout.LayoutParams.MATCH_PARENT, 77 FrameLayout.LayoutParams.MATCH_PARENT 78 ) 79 ) 80 81 fun pxToDp(px: Int): Int { 82 val density = resources.displayMetrics.density 83 return (px / density).toInt() 84 } 85 86 val width = pxToDp(contentFrame.width) 87 val height = pxToDp(contentFrame.height) 88 val optionsBundle = Bundle() 89 optionsBundle.putInt(OPTION_APPWIDGET_MIN_WIDTH, width) 90 optionsBundle.putInt(OPTION_APPWIDGET_MAX_WIDTH, width) 91 optionsBundle.putInt(OPTION_APPWIDGET_MIN_HEIGHT, height) 92 optionsBundle.putInt(OPTION_APPWIDGET_MAX_HEIGHT, height) 93 if (Build.VERSION.SDK_INT >= 31) { 94 optionsBundle.putParcelableArrayList( 95 OPTION_APPWIDGET_SIZES, 96 arrayListOf(SizeF(width.toFloat(), height.toFloat())) 97 ) 98 } 99 100 appWidgetManager.updateAppWidgetOptions(appWidgetId, optionsBundle) 101 102 return hostView 103 } 104 } 105