Home | History | Annotate | Download | only in appwidget
      1 /*
      2  * Copyright (C) 2008 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.android.apis.appwidget;
     18 
     19 import android.appwidget.AppWidgetManager;
     20 import android.appwidget.AppWidgetProvider;
     21 import android.content.ComponentName;
     22 import android.content.Context;
     23 import android.content.pm.PackageManager;
     24 import android.os.SystemClock;
     25 import android.util.Log;
     26 import android.widget.RemoteViews;
     27 
     28 // Need the following import to get access to the app resources, since this
     29 // class is in a sub-package.
     30 import com.example.android.apis.R;
     31 
     32 /**
     33  * A widget provider.  We have a string that we pull from a preference in order to show
     34  * the configuration settings and the current time when the widget was updated.  We also
     35  * register a BroadcastReceiver for time-changed and timezone-changed broadcasts, and
     36  * update then too.
     37  *
     38  * <p>See also the following files:
     39  * <ul>
     40  *   <li>ExampleAppWidgetConfigure.java</li>
     41  *   <li>ExampleBroadcastReceiver.java</li>
     42  *   <li>res/layout/appwidget_configure.xml</li>
     43  *   <li>res/layout/appwidget_provider.xml</li>
     44  *   <li>res/xml/appwidget_provider.xml</li>
     45  * </ul>
     46  */
     47 public class ExampleAppWidgetProvider extends AppWidgetProvider {
     48     // log tag
     49     private static final String TAG = "ExampleAppWidgetProvider";
     50 
     51     @Override
     52     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
     53         Log.d(TAG, "onUpdate");
     54         // For each widget that needs an update, get the text that we should display:
     55         //   - Create a RemoteViews object for it
     56         //   - Set the text in the RemoteViews object
     57         //   - Tell the AppWidgetManager to show that views object for the widget.
     58         final int N = appWidgetIds.length;
     59         for (int i=0; i<N; i++) {
     60             int appWidgetId = appWidgetIds[i];
     61             String titlePrefix = ExampleAppWidgetConfigure.loadTitlePref(context, appWidgetId);
     62             updateAppWidget(context, appWidgetManager, appWidgetId, titlePrefix);
     63         }
     64     }
     65 
     66     @Override
     67     public void onDeleted(Context context, int[] appWidgetIds) {
     68         Log.d(TAG, "onDeleted");
     69         // When the user deletes the widget, delete the preference associated with it.
     70         final int N = appWidgetIds.length;
     71         for (int i=0; i<N; i++) {
     72             ExampleAppWidgetConfigure.deleteTitlePref(context, appWidgetIds[i]);
     73         }
     74     }
     75 
     76     @Override
     77     public void onEnabled(Context context) {
     78         Log.d(TAG, "onEnabled");
     79         // When the first widget is created, register for the TIMEZONE_CHANGED and TIME_CHANGED
     80         // broadcasts.  We don't want to be listening for these if nobody has our widget active.
     81         // This setting is sticky across reboots, but that doesn't matter, because this will
     82         // be called after boot if there is a widget instance for this provider.
     83         PackageManager pm = context.getPackageManager();
     84         pm.setComponentEnabledSetting(
     85                 new ComponentName("com.example.android.apis", ".appwidget.ExampleBroadcastReceiver"),
     86                 PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
     87                 PackageManager.DONT_KILL_APP);
     88     }
     89 
     90     @Override
     91     public void onDisabled(Context context) {
     92         // When the first widget is created, stop listening for the TIMEZONE_CHANGED and
     93         // TIME_CHANGED broadcasts.
     94         Log.d(TAG, "onDisabled");
     95         PackageManager pm = context.getPackageManager();
     96         pm.setComponentEnabledSetting(
     97                 new ComponentName("com.example.android.apis", ".appwidget.ExampleBroadcastReceiver"),
     98                 PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
     99                 PackageManager.DONT_KILL_APP);
    100     }
    101 
    102     static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
    103             int appWidgetId, String titlePrefix) {
    104         Log.d(TAG, "updateAppWidget appWidgetId=" + appWidgetId + " titlePrefix=" + titlePrefix);
    105         // Getting the string this way allows the string to be localized.  The format
    106         // string is filled in using java.util.Formatter-style format strings.
    107         CharSequence text = context.getString(R.string.appwidget_text_format,
    108                 ExampleAppWidgetConfigure.loadTitlePref(context, appWidgetId),
    109                 "0x" + Long.toHexString(SystemClock.elapsedRealtime()));
    110 
    111         // Construct the RemoteViews object.  It takes the package name (in our case, it's our
    112         // package, but it needs this because on the other side it's the widget host inflating
    113         // the layout from our package).
    114         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider);
    115         views.setTextViewText(R.id.appwidget_text, text);
    116 
    117         // Tell the widget manager
    118         appWidgetManager.updateAppWidget(appWidgetId, views);
    119     }
    120 }
    121 
    122 
    123