1 /* 2 * Copyright (C) 2009 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.android.buildwidget; 18 19 import android.app.PendingIntent; 20 import android.app.Service; 21 import android.appwidget.AppWidgetManager; 22 import android.appwidget.AppWidgetProvider; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.res.Resources; 27 import android.net.Uri; 28 import android.os.IBinder; 29 import android.text.format.DateUtils; 30 import android.util.Log; 31 import android.widget.RemoteViews; 32 33 import java.util.regex.Matcher; 34 import java.util.regex.Pattern; 35 36 /** 37 * Define a simple widget that shows the Wiktionary "Word of the day." To build 38 * an update we spawn a background {@link Service} to perform the API queries. 39 */ 40 public class BuildWidget extends AppWidgetProvider { 41 @Override onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)42 public void onUpdate(Context context, AppWidgetManager appWidgetManager, 43 int[] appWidgetIds) { 44 // To prevent any ANR timeouts, we perform the update in a service 45 context.startService(new Intent(context, UpdateService.class)); 46 } 47 48 public static class UpdateService extends Service { 49 @Override onStart(Intent intent, int startId)50 public void onStart(Intent intent, int startId) { 51 // Build the widget update 52 RemoteViews updateViews = buildUpdate(this); 53 54 // Push update for this widget to the home screen 55 ComponentName thisWidget = new ComponentName(this, BuildWidget.class); 56 AppWidgetManager manager = AppWidgetManager.getInstance(this); 57 manager.updateAppWidget(thisWidget, updateViews); 58 } 59 buildUpdate(Context context)60 public RemoteViews buildUpdate(Context context) { 61 // Pick out month names from resources 62 Resources res = context.getResources(); 63 RemoteViews updateViews = new RemoteViews( 64 context.getPackageName(), R.layout.widget); 65 66 PendingIntent pendingIntent = PendingIntent.getActivity(context, 67 0 /* no requestCode */, 68 new Intent(android.provider.Settings.ACTION_DEVICE_INFO_SETTINGS), 69 0 /* no flags */); 70 updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent); 71 72 updateViews.setTextViewText(R.id.build_info, android.os.Build.ID); 73 updateViews.setTextViewText(R.id.build_date, 74 DateUtils.formatDateTime(context, android.os.Build.TIME, 75 DateUtils.FORMAT_NUMERIC_DATE)); 76 updateViews.setTextViewText(R.id.build_extra, android.os.Build.FINGERPRINT); 77 return updateViews; 78 } 79 80 @Override onBind(Intent intent)81 public IBinder onBind(Intent intent) { 82 // We don't need to bind to this service 83 return null; 84 } 85 } 86 } 87