1 /* 2 * Copyright 2018 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 com.example.androidx.remotecallback.demos; 18 19 20 import android.appwidget.AppWidgetManager; 21 import android.content.Context; 22 import android.util.Log; 23 import android.widget.RemoteViews; 24 25 import androidx.remotecallback.AppWidgetProviderWithCallbacks; 26 import androidx.remotecallback.RemoteCallable; 27 import androidx.remotecallback.RemoteCallback; 28 29 /** 30 * Sample widget provider that hooks up a +/-/reset button for a counter. 31 */ 32 public class RemoteCallbackProvider extends AppWidgetProviderWithCallbacks<RemoteCallbackProvider> { 33 @Override onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)34 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 35 Log.d("RemoteCallbackProvider", "onUpdate"); 36 appWidgetManager.updateAppWidget(appWidgetIds, 37 createRemoteViews(context, appWidgetIds, 0)); 38 } 39 40 /** 41 * Creates a current view of the widget with actions and text hooked up. 42 */ createRemoteViews(Context context, int[] ids, int value)43 public RemoteViews createRemoteViews(Context context, int[] ids, int value) { 44 Log.d("RemoteCallbackProvider", "createRemoteViews " + value); 45 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), 46 R.layout.app_item); 47 48 remoteViews.setOnClickPendingIntent(R.id.sub, 49 createRemoteCallback(context).setValue(context, ids, value - 1).toPendingIntent()); 50 remoteViews.setOnClickPendingIntent(R.id.add, 51 createRemoteCallback(context).setValue(context, ids, value + 1).toPendingIntent()); 52 remoteViews.setOnClickPendingIntent(R.id.reset, 53 createRemoteCallback(context).setValue(context, ids, 0).toPendingIntent()); 54 remoteViews.setTextViewText(R.id.current, "Value: " + value); 55 return remoteViews; 56 } 57 58 /** 59 * Gets called when a button is pushed. 60 */ 61 @RemoteCallable setValue(Context context, int[] ids, int value)62 public RemoteCallback setValue(Context context, int[] ids, int value) { 63 Log.d("RemoteCallbackProvider", "setValue " + value); 64 AppWidgetManager appWidgetManager = (AppWidgetManager) context.getSystemService( 65 Context.APPWIDGET_SERVICE); 66 appWidgetManager.updateAppWidget(ids, createRemoteViews(context, ids, value)); 67 return RemoteCallback.LOCAL; 68 } 69 } 70