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