1 /* <lambda>null2 * Copyright (C) 2020 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 package com.android.alarmclock 17 18 import android.app.PendingIntent 19 import android.appwidget.AppWidgetManager 20 import android.appwidget.AppWidgetProvider 21 import android.content.ComponentName 22 import android.content.Context 23 import android.content.Intent 24 import android.widget.RemoteViews 25 26 import com.android.deskclock.DeskClock 27 import com.android.deskclock.R 28 import com.android.deskclock.Utils 29 import com.android.deskclock.data.DataModel 30 31 /** 32 * Simple widget to show an analog clock. 33 */ 34 class AnalogAppWidgetProvider : AppWidgetProvider() { 35 override fun onReceive(context: Context, intent: Intent?) { 36 super.onReceive(context, intent) 37 val wm: AppWidgetManager = AppWidgetManager.getInstance(context) ?: return 38 39 // Send events for newly created/deleted widgets. 40 val provider = ComponentName(context, javaClass) 41 val widgetCount: Int = wm.getAppWidgetIds(provider).size 42 val dm = DataModel.dataModel 43 dm.updateWidgetCount(javaClass, widgetCount, R.string.category_analog_widget) 44 } 45 46 /** 47 * Called when widgets must provide remote views. 48 */ 49 override fun onUpdate(context: Context, wm: AppWidgetManager, widgetIds: IntArray) { 50 super.onUpdate(context, wm, widgetIds) 51 widgetIds.forEach { widgetId -> 52 val packageName: String = context.getPackageName() 53 val widget = RemoteViews(packageName, R.layout.analog_appwidget) 54 55 // Tapping on the widget opens the app (if not on the lock screen). 56 if (Utils.isWidgetClickable(wm, widgetId)) { 57 val openApp = Intent(context, DeskClock::class.java) 58 val pi: PendingIntent = PendingIntent.getActivity(context, 0, openApp, 0) 59 widget.setOnClickPendingIntent(R.id.analog_appwidget, pi) 60 } 61 wm.updateAppWidget(widgetId, widget) 62 } 63 } 64 }