blob: 65f8b34bb7f9239d22ff849c3002d6896022fce3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
17package com.android.server;
18
Winson Chung81f39eb2011-01-11 18:05:01 -080019import java.io.File;
20import java.io.FileDescriptor;
21import java.io.FileInputStream;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.io.PrintWriter;
25import java.util.ArrayList;
26import java.util.HashMap;
27import java.util.HashSet;
28import java.util.Iterator;
29import java.util.List;
30import java.util.Locale;
31
Dianne Hackborn2f0b1752011-05-31 17:59:49 -070032import org.apache.commons.logging.impl.SimpleLog;
Winson Chung81f39eb2011-01-11 18:05:01 -080033import org.xmlpull.v1.XmlPullParser;
34import org.xmlpull.v1.XmlPullParserException;
35import org.xmlpull.v1.XmlSerializer;
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.app.AlarmManager;
38import android.app.PendingIntent;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070039import android.appwidget.AppWidgetManager;
40import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.BroadcastReceiver;
42import android.content.ComponentName;
43import android.content.Context;
44import android.content.Intent;
Winson Chung81f39eb2011-01-11 18:05:01 -080045import android.content.Intent.FilterComparison;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.IntentFilter;
Winson Chung81f39eb2011-01-11 18:05:01 -080047import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.pm.ActivityInfo;
Joe Onorato331fbdc72010-08-24 17:02:09 -040049import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.pm.PackageInfo;
Winson Chung81f39eb2011-01-11 18:05:01 -080051import android.content.pm.PackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.pm.ResolveInfo;
Winson Chung81f39eb2011-01-11 18:05:01 -080053import android.content.pm.ServiceInfo;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080054import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.res.TypedArray;
56import android.content.res.XmlResourceParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.net.Uri;
58import android.os.Binder;
59import android.os.Bundle;
Winson Chung81f39eb2011-01-11 18:05:01 -080060import android.os.IBinder;
Marco Nelissen54796e72009-04-30 15:16:30 -070061import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.RemoteException;
63import android.os.SystemClock;
64import android.util.AttributeSet;
Winson Chung16c8d8a2011-01-20 16:19:33 -080065import android.util.Log;
Winson Chung81f39eb2011-01-11 18:05:01 -080066import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080067import android.util.Slog;
Mitsuru Oshima8f25c422009-07-01 00:10:43 -070068import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.Xml;
70import android.widget.RemoteViews;
Winson Chung84bbb022011-02-21 13:57:45 -080071import android.widget.RemoteViewsService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070073import com.android.internal.appwidget.IAppWidgetHost;
Winson Chung81f39eb2011-01-11 18:05:01 -080074import com.android.internal.appwidget.IAppWidgetService;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080075import com.android.internal.util.FastXmlSerializer;
Winson Chung81f39eb2011-01-11 18:05:01 -080076import com.android.internal.widget.IRemoteViewsAdapterConnection;
Winson Chung84bbb022011-02-21 13:57:45 -080077import com.android.internal.widget.IRemoteViewsFactory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070079class AppWidgetService extends IAppWidgetService.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070081 private static final String TAG = "AppWidgetService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070083 private static final String SETTINGS_FILENAME = "appwidgets.xml";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 private static final String SETTINGS_TMP_FILENAME = SETTINGS_FILENAME + ".tmp";
Joe Onoratobe96b3a2009-07-14 19:49:27 -070085 private static final int MIN_UPDATE_PERIOD = 30 * 60 * 1000; // 30 minutes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87 /*
88 * When identifying a Host or Provider based on the calling process, use the uid field.
89 * When identifying a Host or Provider based on a package manager broadcast, use the
90 * package given.
91 */
92
93 static class Provider {
94 int uid;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070095 AppWidgetProviderInfo info;
Romain Guya5475592009-07-01 17:20:08 -070096 ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 PendingIntent broadcast;
98 boolean zombie; // if we're in safe mode, don't prune this just because nobody references it
99
100 int tag; // for use while saving state (the index)
101 }
102
103 static class Host {
104 int uid;
105 int hostId;
106 String packageName;
Romain Guya5475592009-07-01 17:20:08 -0700107 ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700108 IAppWidgetHost callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 boolean zombie; // if we're in safe mode, don't prune this just because nobody references it
110
111 int tag; // for use while saving state (the index)
112 }
113
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700114 static class AppWidgetId {
115 int appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 Provider provider;
117 RemoteViews views;
118 Host host;
119 }
120
Winson Chung81f39eb2011-01-11 18:05:01 -0800121 /**
122 * Acts as a proxy between the ServiceConnection and the RemoteViewsAdapterConnection.
123 * This needs to be a static inner class since a reference to the ServiceConnection is held
124 * globally and may lead us to leak AppWidgetService instances (if there were more than one).
125 */
126 static class ServiceConnectionProxy implements ServiceConnection {
Winson Chung81f39eb2011-01-11 18:05:01 -0800127 private final Pair<Integer, Intent.FilterComparison> mKey;
128 private final IBinder mConnectionCb;
129
Winson Chung16c8d8a2011-01-20 16:19:33 -0800130 ServiceConnectionProxy(Pair<Integer, Intent.FilterComparison> key, IBinder connectionCb) {
Winson Chung81f39eb2011-01-11 18:05:01 -0800131 mKey = key;
132 mConnectionCb = connectionCb;
133 }
134 public void onServiceConnected(ComponentName name, IBinder service) {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800135 final IRemoteViewsAdapterConnection cb =
Winson Chung81f39eb2011-01-11 18:05:01 -0800136 IRemoteViewsAdapterConnection.Stub.asInterface(mConnectionCb);
137 try {
138 cb.onServiceConnected(service);
Adam Cohenc2be22c2011-03-16 16:33:53 -0700139 } catch (Exception e) {
Winson Chung81f39eb2011-01-11 18:05:01 -0800140 e.printStackTrace();
141 }
142 }
143 public void onServiceDisconnected(ComponentName name) {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800144 disconnect();
145 }
146 public void disconnect() {
147 final IRemoteViewsAdapterConnection cb =
Winson Chung81f39eb2011-01-11 18:05:01 -0800148 IRemoteViewsAdapterConnection.Stub.asInterface(mConnectionCb);
149 try {
150 cb.onServiceDisconnected();
Adam Cohenc2be22c2011-03-16 16:33:53 -0700151 } catch (Exception e) {
Winson Chung81f39eb2011-01-11 18:05:01 -0800152 e.printStackTrace();
153 }
154 }
155 }
156
Winson Chung84bbb022011-02-21 13:57:45 -0800157 // Manages active connections to RemoteViewsServices
Winson Chung81f39eb2011-01-11 18:05:01 -0800158 private final HashMap<Pair<Integer, FilterComparison>, ServiceConnection>
159 mBoundRemoteViewsServices = new HashMap<Pair<Integer,FilterComparison>,ServiceConnection>();
Winson Chung84bbb022011-02-21 13:57:45 -0800160 // Manages persistent references to RemoteViewsServices from different App Widgets
161 private final HashMap<FilterComparison, HashSet<Integer>>
162 mRemoteViewsServicesAppWidgets = new HashMap<FilterComparison, HashSet<Integer>>();
Winson Chung81f39eb2011-01-11 18:05:01 -0800163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 Context mContext;
Eric Fischer63c2d9e2009-10-22 15:22:50 -0700165 Locale mLocale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 PackageManager mPackageManager;
167 AlarmManager mAlarmManager;
Romain Guya5475592009-07-01 17:20:08 -0700168 ArrayList<Provider> mInstalledProviders = new ArrayList<Provider>();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700169 int mNextAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID + 1;
Romain Guya5475592009-07-01 17:20:08 -0700170 final ArrayList<AppWidgetId> mAppWidgetIds = new ArrayList<AppWidgetId>();
171 ArrayList<Host> mHosts = new ArrayList<Host>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 boolean mSafeMode;
173
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700174 AppWidgetService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 mContext = context;
176 mPackageManager = context.getPackageManager();
177 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
178 }
179
180 public void systemReady(boolean safeMode) {
181 mSafeMode = safeMode;
182
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700183 loadAppWidgetList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 loadStateLocked();
185
186 // Register for the boot completed broadcast, so we can send the
187 // ENABLE broacasts. If we try to send them now, they time out,
188 // because the system isn't ready to handle them yet.
189 mContext.registerReceiver(mBroadcastReceiver,
190 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
191
Eric Fischer63c2d9e2009-10-22 15:22:50 -0700192 // Register for configuration changes so we can update the names
193 // of the widgets when the locale changes.
194 mContext.registerReceiver(mBroadcastReceiver,
195 new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED), null, null);
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 // Register for broadcasts about package install, etc., so we can
198 // update the provider list.
199 IntentFilter filter = new IntentFilter();
200 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
Joe Onoratod070e892011-01-07 20:50:37 -0800201 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
203 filter.addDataScheme("package");
204 mContext.registerReceiver(mBroadcastReceiver, filter);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800205 // Register for events related to sdcard installation.
206 IntentFilter sdFilter = new IntentFilter();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800207 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
208 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800209 mContext.registerReceiver(mBroadcastReceiver, sdFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211
212 @Override
213 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
214 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
215 != PackageManager.PERMISSION_GRANTED) {
216 pw.println("Permission Denial: can't dump from from pid="
217 + Binder.getCallingPid()
218 + ", uid=" + Binder.getCallingUid());
219 return;
220 }
221
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700222 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 int N = mInstalledProviders.size();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700224 pw.println("Providers:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 for (int i=0; i<N; i++) {
226 Provider p = mInstalledProviders.get(i);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700227 AppWidgetProviderInfo info = p.info;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700228 pw.print(" ["); pw.print(i); pw.print("] provider ");
229 pw.print(info.provider.flattenToShortString());
230 pw.println(':');
231 pw.print(" min=("); pw.print(info.minWidth);
232 pw.print("x"); pw.print(info.minHeight);
233 pw.print(") updatePeriodMillis=");
234 pw.print(info.updatePeriodMillis);
235 pw.print(" initialLayout=#");
236 pw.print(Integer.toHexString(info.initialLayout));
237 pw.print(" zombie="); pw.println(p.zombie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
239
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700240 N = mAppWidgetIds.size();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700241 pw.println(" ");
242 pw.println("AppWidgetIds:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700244 AppWidgetId id = mAppWidgetIds.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700245 pw.print(" ["); pw.print(i); pw.print("] id=");
Romain Guya5475592009-07-01 17:20:08 -0700246 pw.println(id.appWidgetId);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700247 pw.print(" hostId=");
248 pw.print(id.host.hostId); pw.print(' ');
249 pw.print(id.host.packageName); pw.print('/');
250 pw.println(id.host.uid);
251 if (id.provider != null) {
252 pw.print(" provider=");
253 pw.println(id.provider.info.provider.flattenToShortString());
254 }
255 if (id.host != null) {
256 pw.print(" host.callbacks="); pw.println(id.host.callbacks);
257 }
258 if (id.views != null) {
259 pw.print(" views="); pw.println(id.views);
260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 }
262
263 N = mHosts.size();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700264 pw.println(" ");
265 pw.println("Hosts:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 for (int i=0; i<N; i++) {
267 Host host = mHosts.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700268 pw.print(" ["); pw.print(i); pw.print("] hostId=");
269 pw.print(host.hostId); pw.print(' ');
270 pw.print(host.packageName); pw.print('/');
271 pw.print(host.uid); pw.println(':');
272 pw.print(" callbacks="); pw.println(host.callbacks);
273 pw.print(" instances.size="); pw.print(host.instances.size());
274 pw.print(" zombie="); pw.println(host.zombie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276 }
277 }
278
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700279 public int allocateAppWidgetId(String packageName, int hostId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 int callingUid = enforceCallingUid(packageName);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700281 synchronized (mAppWidgetIds) {
282 int appWidgetId = mNextAppWidgetId++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
284 Host host = lookupOrAddHostLocked(callingUid, packageName, hostId);
285
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700286 AppWidgetId id = new AppWidgetId();
287 id.appWidgetId = appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 id.host = host;
289
290 host.instances.add(id);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700291 mAppWidgetIds.add(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
293 saveStateLocked();
294
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700295 return appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 }
297 }
298
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700299 public void deleteAppWidgetId(int appWidgetId) {
300 synchronized (mAppWidgetIds) {
301 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 if (id != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700303 deleteAppWidgetLocked(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 saveStateLocked();
305 }
306 }
307 }
308
309 public void deleteHost(int hostId) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700310 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 int callingUid = getCallingUid();
312 Host host = lookupHostLocked(callingUid, hostId);
313 if (host != null) {
314 deleteHostLocked(host);
315 saveStateLocked();
316 }
317 }
318 }
319
320 public void deleteAllHosts() {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700321 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 int callingUid = getCallingUid();
323 final int N = mHosts.size();
324 boolean changed = false;
325 for (int i=N-1; i>=0; i--) {
326 Host host = mHosts.get(i);
327 if (host.uid == callingUid) {
328 deleteHostLocked(host);
329 changed = true;
330 }
331 }
332 if (changed) {
333 saveStateLocked();
334 }
335 }
336 }
337
338 void deleteHostLocked(Host host) {
339 final int N = host.instances.size();
340 for (int i=N-1; i>=0; i--) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700341 AppWidgetId id = host.instances.get(i);
342 deleteAppWidgetLocked(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
344 host.instances.clear();
345 mHosts.remove(host);
346 // it's gone or going away, abruptly drop the callback connection
347 host.callbacks = null;
348 }
349
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700350 void deleteAppWidgetLocked(AppWidgetId id) {
Winson Chung81f39eb2011-01-11 18:05:01 -0800351 // We first unbind all services that are bound to this id
352 unbindAppWidgetRemoteViewsServicesLocked(id);
353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 Host host = id.host;
355 host.instances.remove(id);
356 pruneHostLocked(host);
357
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700358 mAppWidgetIds.remove(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359
360 Provider p = id.provider;
361 if (p != null) {
362 p.instances.remove(id);
363 if (!p.zombie) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700364 // send the broacast saying that this appWidgetId has been deleted
365 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_DELETED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 intent.setComponent(p.info.provider);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700367 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id.appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 mContext.sendBroadcast(intent);
369 if (p.instances.size() == 0) {
370 // cancel the future updates
371 cancelBroadcasts(p);
372
373 // send the broacast saying that the provider is not in use any more
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700374 intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_DISABLED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 intent.setComponent(p.info.provider);
376 mContext.sendBroadcast(intent);
377 }
378 }
379 }
380 }
381
382 void cancelBroadcasts(Provider p) {
383 if (p.broadcast != null) {
384 mAlarmManager.cancel(p.broadcast);
385 long token = Binder.clearCallingIdentity();
386 try {
387 p.broadcast.cancel();
388 } finally {
389 Binder.restoreCallingIdentity(token);
390 }
391 p.broadcast = null;
392 }
393 }
394
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700395 public void bindAppWidgetId(int appWidgetId, ComponentName provider) {
396 mContext.enforceCallingPermission(android.Manifest.permission.BIND_APPWIDGET,
397 "bindGagetId appWidgetId=" + appWidgetId + " provider=" + provider);
398 synchronized (mAppWidgetIds) {
399 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 if (id == null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700401 throw new IllegalArgumentException("bad appWidgetId");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 }
403 if (id.provider != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700404 throw new IllegalArgumentException("appWidgetId " + appWidgetId + " already bound to "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 + id.provider.info.provider);
406 }
407 Provider p = lookupProviderLocked(provider);
408 if (p == null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700409 throw new IllegalArgumentException("not a appwidget provider: " + provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411 if (p.zombie) {
412 throw new IllegalArgumentException("can't bind to a 3rd party provider in"
413 + " safe mode: " + provider);
414 }
415
416 id.provider = p;
417 p.instances.add(id);
418 int instancesSize = p.instances.size();
419 if (instancesSize == 1) {
420 // tell the provider that it's ready
421 sendEnableIntentLocked(p);
422 }
423
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700424 // send an update now -- We need this update now, and just for this appWidgetId.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 // It's less critical when the next one happens, so when we schdule the next one,
426 // we add updatePeriodMillis to its start time. That time will have some slop,
427 // but that's okay.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700428 sendUpdateIntentLocked(p, new int[] { appWidgetId });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429
430 // schedule the future updates
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700431 registerForBroadcastsLocked(p, getAppWidgetIds(p));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 saveStateLocked();
433 }
434 }
435
Winson Chung84bbb022011-02-21 13:57:45 -0800436 // Binds to a specific RemoteViewsService
Winson Chung81f39eb2011-01-11 18:05:01 -0800437 public void bindRemoteViewsService(int appWidgetId, Intent intent, IBinder connection) {
438 synchronized (mAppWidgetIds) {
439 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
440 if (id == null) {
441 throw new IllegalArgumentException("bad appWidgetId");
442 }
443 final ComponentName componentName = intent.getComponent();
444 try {
445 final ServiceInfo si = mContext.getPackageManager().getServiceInfo(componentName,
446 PackageManager.GET_PERMISSIONS);
447 if (!android.Manifest.permission.BIND_REMOTEVIEWS.equals(si.permission)) {
448 throw new SecurityException("Selected service does not require "
449 + android.Manifest.permission.BIND_REMOTEVIEWS
450 + ": " + componentName);
451 }
452 } catch (PackageManager.NameNotFoundException e) {
453 throw new IllegalArgumentException("Unknown component " + componentName);
454 }
455
Winson Chung16c8d8a2011-01-20 16:19:33 -0800456 // If there is already a connection made for this service intent, then disconnect from
457 // that first. (This does not allow multiple connections to the same service under
458 // the same key)
459 ServiceConnectionProxy conn = null;
Winson Chung84bbb022011-02-21 13:57:45 -0800460 FilterComparison fc = new FilterComparison(intent);
461 Pair<Integer, FilterComparison> key = Pair.create(appWidgetId, fc);
Winson Chung16c8d8a2011-01-20 16:19:33 -0800462 if (mBoundRemoteViewsServices.containsKey(key)) {
463 conn = (ServiceConnectionProxy) mBoundRemoteViewsServices.get(key);
464 conn.disconnect();
465 mContext.unbindService(conn);
466 mBoundRemoteViewsServices.remove(key);
467 }
468
469 // Bind to the RemoteViewsService (which will trigger a callback to the
470 // RemoteViewsAdapter.onServiceConnected())
Winson Chung81f39eb2011-01-11 18:05:01 -0800471 final long token = Binder.clearCallingIdentity();
472 try {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800473 conn = new ServiceConnectionProxy(key, connection);
Winson Chung81f39eb2011-01-11 18:05:01 -0800474 mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE);
475 mBoundRemoteViewsServices.put(key, conn);
476 } finally {
477 Binder.restoreCallingIdentity(token);
478 }
Winson Chung84bbb022011-02-21 13:57:45 -0800479
480 // Add it to the mapping of RemoteViewsService to appWidgetIds so that we can determine
481 // when we can call back to the RemoteViewsService later to destroy associated
482 // factories.
Winson Chung22bc69d2011-02-25 15:13:38 -0800483 incrementAppWidgetServiceRefCount(appWidgetId, fc);
Winson Chung81f39eb2011-01-11 18:05:01 -0800484 }
485 }
486
Winson Chung84bbb022011-02-21 13:57:45 -0800487 // Unbinds from a specific RemoteViewsService
Winson Chung81f39eb2011-01-11 18:05:01 -0800488 public void unbindRemoteViewsService(int appWidgetId, Intent intent) {
489 synchronized (mAppWidgetIds) {
Winson Chung81f39eb2011-01-11 18:05:01 -0800490 // Unbind from the RemoteViewsService (which will trigger a callback to the bound
491 // RemoteViewsAdapter)
492 Pair<Integer, FilterComparison> key = Pair.create(appWidgetId,
493 new FilterComparison(intent));
494 if (mBoundRemoteViewsServices.containsKey(key)) {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800495 // We don't need to use the appWidgetId until after we are sure there is something
496 // to unbind. Note that this may mask certain issues with apps calling unbind()
497 // more than necessary.
498 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
499 if (id == null) {
500 throw new IllegalArgumentException("bad appWidgetId");
501 }
502
503 ServiceConnectionProxy conn =
504 (ServiceConnectionProxy) mBoundRemoteViewsServices.get(key);
505 conn.disconnect();
Winson Chung81f39eb2011-01-11 18:05:01 -0800506 mContext.unbindService(conn);
Winson Chung16c8d8a2011-01-20 16:19:33 -0800507 mBoundRemoteViewsServices.remove(key);
508 } else {
509 Log.e("AppWidgetService", "Error (unbindRemoteViewsService): Connection not bound");
Winson Chung81f39eb2011-01-11 18:05:01 -0800510 }
511 }
512 }
513
Winson Chung84bbb022011-02-21 13:57:45 -0800514 // Unbinds from a RemoteViewsService when we delete an app widget
Winson Chung81f39eb2011-01-11 18:05:01 -0800515 private void unbindAppWidgetRemoteViewsServicesLocked(AppWidgetId id) {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800516 int appWidgetId = id.appWidgetId;
517 // Unbind all connections to Services bound to this AppWidgetId
Winson Chung81f39eb2011-01-11 18:05:01 -0800518 Iterator<Pair<Integer, Intent.FilterComparison>> it =
519 mBoundRemoteViewsServices.keySet().iterator();
Winson Chung81f39eb2011-01-11 18:05:01 -0800520 while (it.hasNext()) {
521 final Pair<Integer, Intent.FilterComparison> key = it.next();
522 if (key.first.intValue() == appWidgetId) {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800523 final ServiceConnectionProxy conn = (ServiceConnectionProxy)
524 mBoundRemoteViewsServices.get(key);
525 conn.disconnect();
Winson Chung81f39eb2011-01-11 18:05:01 -0800526 mContext.unbindService(conn);
Winson Chung16c8d8a2011-01-20 16:19:33 -0800527 it.remove();
Winson Chung81f39eb2011-01-11 18:05:01 -0800528 }
529 }
Winson Chung84bbb022011-02-21 13:57:45 -0800530
531 // Check if we need to destroy any services (if no other app widgets are
532 // referencing the same service)
533 decrementAppWidgetServiceRefCount(appWidgetId);
534 }
535
536 // Destroys the cached factory on the RemoteViewsService's side related to the specified intent
537 private void destroyRemoteViewsService(final Intent intent) {
538 final ServiceConnection conn = new ServiceConnection() {
539 @Override
540 public void onServiceConnected(ComponentName name, IBinder service) {
541 final IRemoteViewsFactory cb =
542 IRemoteViewsFactory.Stub.asInterface(service);
543 try {
544 cb.onDestroy(intent);
Adam Cohenc2be22c2011-03-16 16:33:53 -0700545 } catch (Exception e) {
Winson Chung84bbb022011-02-21 13:57:45 -0800546 e.printStackTrace();
547 }
548 mContext.unbindService(this);
549 }
550 @Override
551 public void onServiceDisconnected(android.content.ComponentName name) {
552 // Do nothing
553 }
554 };
555
556 // Bind to the service and remove the static intent->factory mapping in the
557 // RemoteViewsService.
558 final long token = Binder.clearCallingIdentity();
559 try {
560 mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE);
561 } finally {
562 Binder.restoreCallingIdentity(token);
563 }
564 }
565
566 // Adds to the ref-count for a given RemoteViewsService intent
567 private void incrementAppWidgetServiceRefCount(int appWidgetId, FilterComparison fc) {
568 HashSet<Integer> appWidgetIds = null;
569 if (mRemoteViewsServicesAppWidgets.containsKey(fc)) {
570 appWidgetIds = mRemoteViewsServicesAppWidgets.get(fc);
571 } else {
572 appWidgetIds = new HashSet<Integer>();
573 mRemoteViewsServicesAppWidgets.put(fc, appWidgetIds);
574 }
575 appWidgetIds.add(appWidgetId);
576 }
577
578 // Subtracts from the ref-count for a given RemoteViewsService intent, prompting a delete if
579 // the ref-count reaches zero.
580 private void decrementAppWidgetServiceRefCount(int appWidgetId) {
581 Iterator<FilterComparison> it =
582 mRemoteViewsServicesAppWidgets.keySet().iterator();
583 while (it.hasNext()) {
584 final FilterComparison key = it.next();
585 final HashSet<Integer> ids = mRemoteViewsServicesAppWidgets.get(key);
586 if (ids.remove(appWidgetId)) {
587 // If we have removed the last app widget referencing this service, then we
588 // should destroy it and remove it from this set
589 if (ids.isEmpty()) {
590 destroyRemoteViewsService(key.getIntent());
591 it.remove();
592 }
593 }
594 }
Winson Chung81f39eb2011-01-11 18:05:01 -0800595 }
596
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700597 public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId) {
598 synchronized (mAppWidgetIds) {
599 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 if (id != null && id.provider != null && !id.provider.zombie) {
601 return id.provider.info;
602 }
603 return null;
604 }
605 }
606
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700607 public RemoteViews getAppWidgetViews(int appWidgetId) {
608 synchronized (mAppWidgetIds) {
609 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 if (id != null) {
611 return id.views;
612 }
613 return null;
614 }
615 }
616
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700617 public List<AppWidgetProviderInfo> getInstalledProviders() {
618 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 final int N = mInstalledProviders.size();
Romain Guya5475592009-07-01 17:20:08 -0700620 ArrayList<AppWidgetProviderInfo> result = new ArrayList<AppWidgetProviderInfo>(N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 for (int i=0; i<N; i++) {
622 Provider p = mInstalledProviders.get(i);
623 if (!p.zombie) {
624 result.add(p.info);
625 }
626 }
627 return result;
628 }
629 }
630
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700631 public void updateAppWidgetIds(int[] appWidgetIds, RemoteViews views) {
632 if (appWidgetIds == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 return;
634 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700635 if (appWidgetIds.length == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 return;
637 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700638 final int N = appWidgetIds.length;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700640 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700642 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetIds[i]);
643 updateAppWidgetInstanceLocked(id, views);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 }
645 }
646 }
647
Adam Cohen2dd21972010-08-15 18:20:04 -0700648 public void partiallyUpdateAppWidgetIds(int[] appWidgetIds, RemoteViews views) {
649 if (appWidgetIds == null) {
650 return;
651 }
652 if (appWidgetIds.length == 0) {
653 return;
654 }
655 final int N = appWidgetIds.length;
656
657 synchronized (mAppWidgetIds) {
658 for (int i=0; i<N; i++) {
659 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetIds[i]);
660 updateAppWidgetInstanceLocked(id, views, true);
661 }
662 }
663 }
664
Winson Chung6394c0e2010-08-16 10:14:56 -0700665 public void notifyAppWidgetViewDataChanged(int[] appWidgetIds, int viewId) {
Winson Chung499cb9f2010-07-16 11:18:17 -0700666 if (appWidgetIds == null) {
667 return;
668 }
669 if (appWidgetIds.length == 0) {
670 return;
671 }
672 final int N = appWidgetIds.length;
673
674 synchronized (mAppWidgetIds) {
675 for (int i=0; i<N; i++) {
676 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetIds[i]);
Winson Chung6394c0e2010-08-16 10:14:56 -0700677 notifyAppWidgetViewDataChangedInstanceLocked(id, viewId);
Winson Chung499cb9f2010-07-16 11:18:17 -0700678 }
679 }
680 }
681
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700682 public void updateAppWidgetProvider(ComponentName provider, RemoteViews views) {
683 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 Provider p = lookupProviderLocked(provider);
685 if (p == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800686 Slog.w(TAG, "updateAppWidgetProvider: provider doesn't exist: " + provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 return;
688 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700689 ArrayList<AppWidgetId> instances = p.instances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 final int N = instances.size();
691 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700692 AppWidgetId id = instances.get(i);
693 updateAppWidgetInstanceLocked(id, views);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 }
695 }
696 }
697
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700698 void updateAppWidgetInstanceLocked(AppWidgetId id, RemoteViews views) {
Adam Cohen2dd21972010-08-15 18:20:04 -0700699 updateAppWidgetInstanceLocked(id, views, false);
700 }
701
702 void updateAppWidgetInstanceLocked(AppWidgetId id, RemoteViews views, boolean isPartialUpdate) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700703 // allow for stale appWidgetIds and other badness
704 // lookup also checks that the calling process can access the appWidgetId
705 // drop unbound appWidgetIds (shouldn't be possible under normal circumstances)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 if (id != null && id.provider != null && !id.provider.zombie && !id.host.zombie) {
Adam Cohen2dd21972010-08-15 18:20:04 -0700707
708 // We do not want to save this RemoteViews
709 if (!isPartialUpdate) id.views = views;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710
711 // is anyone listening?
712 if (id.host.callbacks != null) {
713 try {
714 // the lock is held, but this is a oneway call
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700715 id.host.callbacks.updateAppWidget(id.appWidgetId, views);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 } catch (RemoteException e) {
717 // It failed; remove the callback. No need to prune because
718 // we know that this host is still referenced by this instance.
719 id.host.callbacks = null;
720 }
721 }
722 }
723 }
724
Winson Chung6394c0e2010-08-16 10:14:56 -0700725 void notifyAppWidgetViewDataChangedInstanceLocked(AppWidgetId id, int viewId) {
Winson Chung499cb9f2010-07-16 11:18:17 -0700726 // allow for stale appWidgetIds and other badness
727 // lookup also checks that the calling process can access the appWidgetId
728 // drop unbound appWidgetIds (shouldn't be possible under normal circumstances)
729 if (id != null && id.provider != null && !id.provider.zombie && !id.host.zombie) {
Winson Chung499cb9f2010-07-16 11:18:17 -0700730 // is anyone listening?
731 if (id.host.callbacks != null) {
732 try {
733 // the lock is held, but this is a oneway call
Winson Chung6394c0e2010-08-16 10:14:56 -0700734 id.host.callbacks.viewDataChanged(id.appWidgetId, viewId);
Winson Chung499cb9f2010-07-16 11:18:17 -0700735 } catch (RemoteException e) {
736 // It failed; remove the callback. No need to prune because
737 // we know that this host is still referenced by this instance.
738 id.host.callbacks = null;
739 }
740 }
741 }
742 }
743
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700744 public int[] startListening(IAppWidgetHost callbacks, String packageName, int hostId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 List<RemoteViews> updatedViews) {
746 int callingUid = enforceCallingUid(packageName);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700747 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 Host host = lookupOrAddHostLocked(callingUid, packageName, hostId);
749 host.callbacks = callbacks;
750
751 updatedViews.clear();
752
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700753 ArrayList<AppWidgetId> instances = host.instances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 int N = instances.size();
755 int[] updatedIds = new int[N];
756 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700757 AppWidgetId id = instances.get(i);
758 updatedIds[i] = id.appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 updatedViews.add(id.views);
760 }
761 return updatedIds;
762 }
763 }
764
765 public void stopListening(int hostId) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700766 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 Host host = lookupHostLocked(getCallingUid(), hostId);
Ken Shirriffe21167a2009-09-23 16:42:53 -0700768 if (host != null) {
769 host.callbacks = null;
770 pruneHostLocked(host);
771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
773 }
774
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700775 boolean canAccessAppWidgetId(AppWidgetId id, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 if (id.host.uid == callingUid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700777 // Apps hosting the AppWidget have access to it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 return true;
779 }
780 if (id.provider != null && id.provider.uid == callingUid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700781 // Apps providing the AppWidget have access to it (if the appWidgetId has been bound)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 return true;
783 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700784 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.BIND_APPWIDGET)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 == PackageManager.PERMISSION_GRANTED) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700786 // Apps that can bind have access to all appWidgetIds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 return true;
788 }
789 // Nobody else can access it.
790 return false;
791 }
792
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700793 AppWidgetId lookupAppWidgetIdLocked(int appWidgetId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 int callingUid = getCallingUid();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700795 final int N = mAppWidgetIds.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700797 AppWidgetId id = mAppWidgetIds.get(i);
798 if (id.appWidgetId == appWidgetId && canAccessAppWidgetId(id, callingUid)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 return id;
800 }
801 }
802 return null;
803 }
804
805 Provider lookupProviderLocked(ComponentName provider) {
Romain Guyd2671e12010-03-11 18:06:42 -0800806 final String className = provider.getClassName();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 final int N = mInstalledProviders.size();
808 for (int i=0; i<N; i++) {
809 Provider p = mInstalledProviders.get(i);
Romain Guyd2671e12010-03-11 18:06:42 -0800810 if (p.info.provider.equals(provider) || className.equals(p.info.oldName)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 return p;
812 }
813 }
814 return null;
815 }
816
817 Host lookupHostLocked(int uid, int hostId) {
818 final int N = mHosts.size();
819 for (int i=0; i<N; i++) {
820 Host h = mHosts.get(i);
821 if (h.uid == uid && h.hostId == hostId) {
822 return h;
823 }
824 }
825 return null;
826 }
827
828 Host lookupOrAddHostLocked(int uid, String packageName, int hostId) {
829 final int N = mHosts.size();
830 for (int i=0; i<N; i++) {
831 Host h = mHosts.get(i);
832 if (h.hostId == hostId && h.packageName.equals(packageName)) {
833 return h;
834 }
835 }
836 Host host = new Host();
837 host.packageName = packageName;
838 host.uid = uid;
839 host.hostId = hostId;
840 mHosts.add(host);
841 return host;
842 }
843
844 void pruneHostLocked(Host host) {
845 if (host.instances.size() == 0 && host.callbacks == null) {
846 mHosts.remove(host);
847 }
848 }
849
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700850 void loadAppWidgetList() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 PackageManager pm = mPackageManager;
852
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700853 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 List<ResolveInfo> broadcastReceivers = pm.queryBroadcastReceivers(intent,
855 PackageManager.GET_META_DATA);
856
Bjorn Bringert5f857802010-02-10 23:09:48 +0000857 final int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 for (int i=0; i<N; i++) {
859 ResolveInfo ri = broadcastReceivers.get(i);
860 addProviderLocked(ri);
861 }
862 }
863
864 boolean addProviderLocked(ResolveInfo ri) {
Joe Onoratod070e892011-01-07 20:50:37 -0800865 if ((ri.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
866 return false;
867 }
868 if (!ri.activityInfo.isEnabled()) {
869 return false;
870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 Provider p = parseProviderInfoXml(new ComponentName(ri.activityInfo.packageName,
872 ri.activityInfo.name), ri);
873 if (p != null) {
874 mInstalledProviders.add(p);
875 return true;
876 } else {
877 return false;
878 }
879 }
880
881 void removeProviderLocked(int index, Provider p) {
882 int N = p.instances.size();
883 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700884 AppWidgetId id = p.instances.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 // Call back with empty RemoteViews
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700886 updateAppWidgetInstanceLocked(id, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 // Stop telling the host about updates for this from now on
888 cancelBroadcasts(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700889 // clear out references to this appWidgetId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 id.host.instances.remove(id);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700891 mAppWidgetIds.remove(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 id.provider = null;
893 pruneHostLocked(id.host);
894 id.host = null;
895 }
896 p.instances.clear();
897 mInstalledProviders.remove(index);
898 // no need to send the DISABLE broadcast, since the receiver is gone anyway
899 cancelBroadcasts(p);
900 }
901
902 void sendEnableIntentLocked(Provider p) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700903 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_ENABLED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 intent.setComponent(p.info.provider);
905 mContext.sendBroadcast(intent);
906 }
907
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700908 void sendUpdateIntentLocked(Provider p, int[] appWidgetIds) {
909 if (appWidgetIds != null && appWidgetIds.length > 0) {
910 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
911 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 intent.setComponent(p.info.provider);
913 mContext.sendBroadcast(intent);
914 }
915 }
916
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700917 void registerForBroadcastsLocked(Provider p, int[] appWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 if (p.info.updatePeriodMillis > 0) {
919 // if this is the first instance, set the alarm. otherwise,
920 // rely on the fact that we've already set it and that
921 // PendingIntent.getBroadcast will update the extras.
922 boolean alreadyRegistered = p.broadcast != null;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700923 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
924 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 intent.setComponent(p.info.provider);
926 long token = Binder.clearCallingIdentity();
927 try {
928 p.broadcast = PendingIntent.getBroadcast(mContext, 1, intent,
929 PendingIntent.FLAG_UPDATE_CURRENT);
930 } finally {
931 Binder.restoreCallingIdentity(token);
932 }
933 if (!alreadyRegistered) {
Joe Onoratobe96b3a2009-07-14 19:49:27 -0700934 long period = p.info.updatePeriodMillis;
935 if (period < MIN_UPDATE_PERIOD) {
936 period = MIN_UPDATE_PERIOD;
937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
Joe Onoratobe96b3a2009-07-14 19:49:27 -0700939 SystemClock.elapsedRealtime() + period, period, p.broadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
941 }
942 }
943
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700944 static int[] getAppWidgetIds(Provider p) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 int instancesSize = p.instances.size();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700946 int appWidgetIds[] = new int[instancesSize];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 for (int i=0; i<instancesSize; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700948 appWidgetIds[i] = p.instances.get(i).appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700950 return appWidgetIds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700953 public int[] getAppWidgetIds(ComponentName provider) {
954 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 Provider p = lookupProviderLocked(provider);
956 if (p != null && getCallingUid() == p.uid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700957 return getAppWidgetIds(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 } else {
959 return new int[0];
960 }
961 }
962 }
963
964 private Provider parseProviderInfoXml(ComponentName component, ResolveInfo ri) {
965 Provider p = null;
966
967 ActivityInfo activityInfo = ri.activityInfo;
968 XmlResourceParser parser = null;
969 try {
970 parser = activityInfo.loadXmlMetaData(mPackageManager,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700971 AppWidgetManager.META_DATA_APPWIDGET_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 if (parser == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800973 Slog.w(TAG, "No " + AppWidgetManager.META_DATA_APPWIDGET_PROVIDER + " meta-data for "
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700974 + "AppWidget provider '" + component + '\'');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 return null;
976 }
Adam Cohend2e20de2011-02-25 12:03:37 -0800977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 AttributeSet attrs = Xml.asAttributeSet(parser);
Adam Cohend2e20de2011-02-25 12:03:37 -0800979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 int type;
981 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
982 && type != XmlPullParser.START_TAG) {
983 // drain whitespace, comments, etc.
984 }
Adam Cohend2e20de2011-02-25 12:03:37 -0800985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 String nodeName = parser.getName();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700987 if (!"appwidget-provider".equals(nodeName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800988 Slog.w(TAG, "Meta-data does not start with appwidget-provider tag for"
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700989 + " AppWidget provider '" + component + '\'');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 return null;
991 }
992
993 p = new Provider();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700994 AppWidgetProviderInfo info = p.info = new AppWidgetProviderInfo();
Romain Guyd2671e12010-03-11 18:06:42 -0800995 // If metaData was null, we would have returned earlier when getting
996 // the parser No need to do the check here
997 info.oldName = activityInfo.metaData.getString(
998 AppWidgetManager.META_DATA_APPWIDGET_OLD_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999
1000 info.provider = component;
1001 p.uid = activityInfo.applicationInfo.uid;
1002
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001003 Resources res = mPackageManager.getResourcesForApplication(
1004 activityInfo.applicationInfo);
Adam Cohend2e20de2011-02-25 12:03:37 -08001005
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001006 TypedArray sa = res.obtainAttributes(attrs,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001007 com.android.internal.R.styleable.AppWidgetProviderInfo);
Adam Cohend2e20de2011-02-25 12:03:37 -08001008
Mitsuru Oshima8f25c422009-07-01 00:10:43 -07001009 // These dimensions has to be resolved in the application's context.
1010 // We simply send back the raw complex data, which will be
1011 // converted to dp in {@link AppWidgetManager#getAppWidgetInfo}.
1012 TypedValue value = sa.peekValue(
1013 com.android.internal.R.styleable.AppWidgetProviderInfo_minWidth);
1014 info.minWidth = value != null ? value.data : 0;
1015 value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minHeight);
1016 info.minHeight = value != null ? value.data : 0;
Adam Cohend2e20de2011-02-25 12:03:37 -08001017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 info.updatePeriodMillis = sa.getInt(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001019 com.android.internal.R.styleable.AppWidgetProviderInfo_updatePeriodMillis, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 info.initialLayout = sa.getResourceId(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001021 com.android.internal.R.styleable.AppWidgetProviderInfo_initialLayout, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 String className = sa.getString(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001023 com.android.internal.R.styleable.AppWidgetProviderInfo_configure);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 if (className != null) {
1025 info.configure = new ComponentName(component.getPackageName(), className);
1026 }
1027 info.label = activityInfo.loadLabel(mPackageManager).toString();
1028 info.icon = ri.getIconResource();
Patrick Dubroyd2db2a52010-06-23 14:56:28 -07001029 info.previewImage = sa.getResourceId(
1030 com.android.internal.R.styleable.AppWidgetProviderInfo_previewImage, 0);
Adam Cohena02fdf12010-11-03 13:27:40 -07001031 info.autoAdvanceViewId = sa.getResourceId(
1032 com.android.internal.R.styleable.AppWidgetProviderInfo_autoAdvanceViewId, -1);
Adam Cohen9611f2e2011-02-28 13:39:38 -08001033 info.resizeMode = sa.getInt(
Adam Cohend2e20de2011-02-25 12:03:37 -08001034 com.android.internal.R.styleable.AppWidgetProviderInfo_resizeMode,
1035 AppWidgetProviderInfo.RESIZE_NONE);
Patrick Dubroyd2db2a52010-06-23 14:56:28 -07001036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 sa.recycle();
1038 } catch (Exception e) {
1039 // Ok to catch Exception here, because anything going wrong because
1040 // of what a client process passes to us should not be fatal for the
1041 // system process.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001042 Slog.w(TAG, "XML parsing failed for AppWidget provider '" + component + '\'', e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 return null;
1044 } finally {
1045 if (parser != null) parser.close();
1046 }
1047 return p;
1048 }
1049
1050 int getUidForPackage(String packageName) throws PackageManager.NameNotFoundException {
1051 PackageInfo pkgInfo = mPackageManager.getPackageInfo(packageName, 0);
1052 if (pkgInfo == null || pkgInfo.applicationInfo == null) {
1053 throw new PackageManager.NameNotFoundException();
1054 }
1055 return pkgInfo.applicationInfo.uid;
1056 }
1057
1058 int enforceCallingUid(String packageName) throws IllegalArgumentException {
1059 int callingUid = getCallingUid();
1060 int packageUid;
1061 try {
1062 packageUid = getUidForPackage(packageName);
1063 } catch (PackageManager.NameNotFoundException ex) {
1064 throw new IllegalArgumentException("packageName and uid don't match packageName="
1065 + packageName);
1066 }
Marco Nelissen54796e72009-04-30 15:16:30 -07001067 if (callingUid != packageUid && Process.supportsProcesses()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 throw new IllegalArgumentException("packageName and uid don't match packageName="
1069 + packageName);
1070 }
1071 return callingUid;
1072 }
1073
1074 void sendInitialBroadcasts() {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001075 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 final int N = mInstalledProviders.size();
1077 for (int i=0; i<N; i++) {
1078 Provider p = mInstalledProviders.get(i);
1079 if (p.instances.size() > 0) {
1080 sendEnableIntentLocked(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001081 int[] appWidgetIds = getAppWidgetIds(p);
1082 sendUpdateIntentLocked(p, appWidgetIds);
1083 registerForBroadcastsLocked(p, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
1085 }
1086 }
1087 }
1088
1089 // only call from initialization -- it assumes that the data structures are all empty
1090 void loadStateLocked() {
1091 File temp = savedStateTempFile();
1092 File real = savedStateRealFile();
1093
1094 // prefer the real file. If it doesn't exist, use the temp one, and then copy it to the
1095 // real one. if there is both a real file and a temp one, assume that the temp one isn't
1096 // fully written and delete it.
1097 if (real.exists()) {
1098 readStateFromFileLocked(real);
1099 if (temp.exists()) {
Romain Guya5475592009-07-01 17:20:08 -07001100 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 temp.delete();
1102 }
1103 } else if (temp.exists()) {
1104 readStateFromFileLocked(temp);
Romain Guya5475592009-07-01 17:20:08 -07001105 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 temp.renameTo(real);
1107 }
1108 }
1109
1110 void saveStateLocked() {
1111 File temp = savedStateTempFile();
1112 File real = savedStateRealFile();
1113
1114 if (!real.exists()) {
1115 // If the real one doesn't exist, it's either because this is the first time
1116 // or because something went wrong while copying them. In this case, we can't
1117 // trust anything that's in temp. In order to have the loadState code not
1118 // use the temporary one until it's fully written, create an empty file
1119 // for real, which will we'll shortly delete.
1120 try {
Romain Guya5475592009-07-01 17:20:08 -07001121 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 real.createNewFile();
1123 } catch (IOException e) {
Romain Guya5475592009-07-01 17:20:08 -07001124 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 }
1126 }
1127
1128 if (temp.exists()) {
Romain Guya5475592009-07-01 17:20:08 -07001129 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 temp.delete();
1131 }
1132
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07001133 if (!writeStateToFileLocked(temp)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001134 Slog.w(TAG, "Failed to persist new settings");
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07001135 return;
1136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137
Romain Guya5475592009-07-01 17:20:08 -07001138 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 real.delete();
Romain Guya5475592009-07-01 17:20:08 -07001140 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 temp.renameTo(real);
1142 }
1143
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07001144 boolean writeStateToFileLocked(File file) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 FileOutputStream stream = null;
1146 int N;
1147
1148 try {
1149 stream = new FileOutputStream(file, false);
1150 XmlSerializer out = new FastXmlSerializer();
1151 out.setOutput(stream, "utf-8");
1152 out.startDocument(null, true);
1153
1154
1155 out.startTag(null, "gs");
1156
1157 int providerIndex = 0;
1158 N = mInstalledProviders.size();
1159 for (int i=0; i<N; i++) {
1160 Provider p = mInstalledProviders.get(i);
1161 if (p.instances.size() > 0) {
1162 out.startTag(null, "p");
1163 out.attribute(null, "pkg", p.info.provider.getPackageName());
1164 out.attribute(null, "cl", p.info.provider.getClassName());
Patrick Tsaibd742e432010-05-01 00:30:19 +08001165 out.endTag(null, "p");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 p.tag = providerIndex;
1167 providerIndex++;
1168 }
1169 }
1170
1171 N = mHosts.size();
1172 for (int i=0; i<N; i++) {
1173 Host host = mHosts.get(i);
1174 out.startTag(null, "h");
1175 out.attribute(null, "pkg", host.packageName);
1176 out.attribute(null, "id", Integer.toHexString(host.hostId));
1177 out.endTag(null, "h");
1178 host.tag = i;
1179 }
1180
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001181 N = mAppWidgetIds.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001183 AppWidgetId id = mAppWidgetIds.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 out.startTag(null, "g");
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001185 out.attribute(null, "id", Integer.toHexString(id.appWidgetId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 out.attribute(null, "h", Integer.toHexString(id.host.tag));
1187 if (id.provider != null) {
1188 out.attribute(null, "p", Integer.toHexString(id.provider.tag));
1189 }
1190 out.endTag(null, "g");
1191 }
1192
1193 out.endTag(null, "gs");
1194
1195 out.endDocument();
1196 stream.close();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07001197 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 } catch (IOException e) {
1199 try {
1200 if (stream != null) {
1201 stream.close();
1202 }
1203 } catch (IOException ex) {
Romain Guya5475592009-07-01 17:20:08 -07001204 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
1206 if (file.exists()) {
Romain Guya5475592009-07-01 17:20:08 -07001207 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 file.delete();
1209 }
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07001210 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212 }
1213
1214 void readStateFromFileLocked(File file) {
1215 FileInputStream stream = null;
1216
1217 boolean success = false;
1218
1219 try {
1220 stream = new FileInputStream(file);
1221 XmlPullParser parser = Xml.newPullParser();
1222 parser.setInput(stream, null);
1223
1224 int type;
1225 int providerIndex = 0;
Romain Guya5475592009-07-01 17:20:08 -07001226 HashMap<Integer,Provider> loadedProviders = new HashMap<Integer, Provider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 do {
1228 type = parser.next();
1229 if (type == XmlPullParser.START_TAG) {
1230 String tag = parser.getName();
1231 if ("p".equals(tag)) {
1232 // TODO: do we need to check that this package has the same signature
1233 // as before?
1234 String pkg = parser.getAttributeValue(null, "pkg");
1235 String cl = parser.getAttributeValue(null, "cl");
Romain Guyff3e61c62010-03-11 15:30:02 -08001236
1237 final PackageManager packageManager = mContext.getPackageManager();
1238 try {
1239 packageManager.getReceiverInfo(new ComponentName(pkg, cl), 0);
1240 } catch (PackageManager.NameNotFoundException e) {
1241 String[] pkgs = packageManager.currentToCanonicalPackageNames(
1242 new String[] { pkg });
1243 pkg = pkgs[0];
1244 }
1245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 Provider p = lookupProviderLocked(new ComponentName(pkg, cl));
1247 if (p == null && mSafeMode) {
1248 // if we're in safe mode, make a temporary one
1249 p = new Provider();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001250 p.info = new AppWidgetProviderInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 p.info.provider = new ComponentName(pkg, cl);
1252 p.zombie = true;
1253 mInstalledProviders.add(p);
1254 }
1255 if (p != null) {
1256 // if it wasn't uninstalled or something
1257 loadedProviders.put(providerIndex, p);
1258 }
1259 providerIndex++;
1260 }
1261 else if ("h".equals(tag)) {
1262 Host host = new Host();
1263
1264 // TODO: do we need to check that this package has the same signature
1265 // as before?
1266 host.packageName = parser.getAttributeValue(null, "pkg");
1267 try {
1268 host.uid = getUidForPackage(host.packageName);
1269 } catch (PackageManager.NameNotFoundException ex) {
1270 host.zombie = true;
1271 }
1272 if (!host.zombie || mSafeMode) {
1273 // In safe mode, we don't discard the hosts we don't recognize
1274 // so that they're not pruned from our list. Otherwise, we do.
1275 host.hostId = Integer.parseInt(
1276 parser.getAttributeValue(null, "id"), 16);
1277 mHosts.add(host);
1278 }
1279 }
1280 else if ("g".equals(tag)) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001281 AppWidgetId id = new AppWidgetId();
1282 id.appWidgetId = Integer.parseInt(parser.getAttributeValue(null, "id"), 16);
1283 if (id.appWidgetId >= mNextAppWidgetId) {
1284 mNextAppWidgetId = id.appWidgetId + 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 }
1286
1287 String providerString = parser.getAttributeValue(null, "p");
1288 if (providerString != null) {
1289 // there's no provider if it hasn't been bound yet.
1290 // maybe we don't have to save this, but it brings the system
1291 // to the state it was in.
1292 int pIndex = Integer.parseInt(providerString, 16);
1293 id.provider = loadedProviders.get(pIndex);
1294 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001295 Slog.d(TAG, "bound appWidgetId=" + id.appWidgetId + " to provider "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 + pIndex + " which is " + id.provider);
1297 }
1298 if (id.provider == null) {
1299 // This provider is gone. We just let the host figure out
1300 // that this happened when it fails to load it.
1301 continue;
1302 }
1303 }
1304
1305 int hIndex = Integer.parseInt(parser.getAttributeValue(null, "h"), 16);
1306 id.host = mHosts.get(hIndex);
1307 if (id.host == null) {
1308 // This host is gone.
1309 continue;
1310 }
1311
1312 if (id.provider != null) {
1313 id.provider.instances.add(id);
1314 }
1315 id.host.instances.add(id);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001316 mAppWidgetIds.add(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
1318 }
1319 } while (type != XmlPullParser.END_DOCUMENT);
1320 success = true;
1321 } catch (NullPointerException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001322 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001324 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001326 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001328 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 } catch (IndexOutOfBoundsException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001330 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
1332 try {
1333 if (stream != null) {
1334 stream.close();
1335 }
1336 } catch (IOException e) {
Romain Guya5475592009-07-01 17:20:08 -07001337 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339
1340 if (success) {
1341 // delete any hosts that didn't manage to get connected (should happen)
1342 // if it matters, they'll be reconnected.
Dianne Hackborn002716d2009-08-12 11:13:26 -07001343 for (int i=mHosts.size()-1; i>=0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 pruneHostLocked(mHosts.get(i));
1345 }
1346 } else {
1347 // failed reading, clean up
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001348 mAppWidgetIds.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 mHosts.clear();
1350 final int N = mInstalledProviders.size();
1351 for (int i=0; i<N; i++) {
1352 mInstalledProviders.get(i).instances.clear();
1353 }
1354 }
1355 }
1356
1357 File savedStateTempFile() {
1358 return new File("/data/system/" + SETTINGS_TMP_FILENAME);
1359 //return new File(mContext.getFilesDir(), SETTINGS_FILENAME);
1360 }
1361
1362 File savedStateRealFile() {
1363 return new File("/data/system/" + SETTINGS_FILENAME);
1364 //return new File(mContext.getFilesDir(), SETTINGS_TMP_FILENAME);
1365 }
1366
1367 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1368 public void onReceive(Context context, Intent intent) {
1369 String action = intent.getAction();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001370 //Slog.d(TAG, "received " + action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
1372 sendInitialBroadcasts();
Eric Fischer63c2d9e2009-10-22 15:22:50 -07001373 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1374 Locale revised = Locale.getDefault();
1375 if (revised == null || mLocale == null ||
1376 !(revised.equals(mLocale))) {
1377 mLocale = revised;
1378
1379 synchronized (mAppWidgetIds) {
1380 int N = mInstalledProviders.size();
1381 for (int i=N-1; i>=0; i--) {
1382 Provider p = mInstalledProviders.get(i);
1383 String pkgName = p.info.provider.getPackageName();
1384 updateProvidersForPackageLocked(pkgName);
1385 }
1386 saveStateLocked();
1387 }
1388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 } else {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001390 boolean added = false;
Joe Onoratod070e892011-01-07 20:50:37 -08001391 boolean changed = false;
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001392 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001393 if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001394 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1395 added = true;
Joe Onorato94258cd2010-08-24 16:45:40 -04001396 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001397 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1398 added = false;
1399 } else {
1400 Uri uri = intent.getData();
1401 if (uri == null) {
1402 return;
1403 }
1404 String pkgName = uri.getSchemeSpecificPart();
1405 if (pkgName == null) {
1406 return;
1407 }
1408 pkgList = new String[] { pkgName };
1409 added = Intent.ACTION_PACKAGE_ADDED.equals(action);
Joe Onoratod070e892011-01-07 20:50:37 -08001410 changed = Intent.ACTION_PACKAGE_CHANGED.equals(action);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001411 }
1412 if (pkgList == null || pkgList.length == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 return;
1414 }
Joe Onoratod070e892011-01-07 20:50:37 -08001415 if (added || changed) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001416 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 Bundle extras = intent.getExtras();
Joe Onoratod070e892011-01-07 20:50:37 -08001418 if (changed || (extras != null &&
1419 extras.getBoolean(Intent.EXTRA_REPLACING, false))) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001420 for (String pkgName : pkgList) {
1421 // The package was just upgraded
1422 updateProvidersForPackageLocked(pkgName);
1423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 } else {
1425 // The package was just added
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001426 for (String pkgName : pkgList) {
1427 addProvidersForPackageLocked(pkgName);
1428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430 saveStateLocked();
1431 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001432 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 Bundle extras = intent.getExtras();
1434 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
1435 // The package is being updated. We'll receive a PACKAGE_ADDED shortly.
1436 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001437 synchronized (mAppWidgetIds) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001438 for (String pkgName : pkgList) {
1439 removeProvidersForPackageLocked(pkgName);
1440 saveStateLocked();
1441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 }
1443 }
1444 }
1445 }
1446 }
1447 };
1448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 void addProvidersForPackageLocked(String pkgName) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001450 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
Joe Onoratof6133fe2010-02-01 18:24:46 -05001451 intent.setPackage(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent,
1453 PackageManager.GET_META_DATA);
1454
Bjorn Bringert5f857802010-02-10 23:09:48 +00001455 final int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 for (int i=0; i<N; i++) {
1457 ResolveInfo ri = broadcastReceivers.get(i);
1458 ActivityInfo ai = ri.activityInfo;
Joe Onorato331fbdc72010-08-24 17:02:09 -04001459 if ((ai.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
1460 continue;
1461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 if (pkgName.equals(ai.packageName)) {
1463 addProviderLocked(ri);
1464 }
1465 }
1466 }
1467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 void updateProvidersForPackageLocked(String pkgName) {
Romain Guya5475592009-07-01 17:20:08 -07001469 HashSet<String> keep = new HashSet<String>();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001470 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
Joe Onoratof6133fe2010-02-01 18:24:46 -05001471 intent.setPackage(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent,
1473 PackageManager.GET_META_DATA);
1474
1475 // add the missing ones and collect which ones to keep
Bjorn Bringert5f857802010-02-10 23:09:48 +00001476 int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 for (int i=0; i<N; i++) {
1478 ResolveInfo ri = broadcastReceivers.get(i);
1479 ActivityInfo ai = ri.activityInfo;
Joe Onorato331fbdc72010-08-24 17:02:09 -04001480 if ((ai.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
1481 continue;
1482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 if (pkgName.equals(ai.packageName)) {
1484 ComponentName component = new ComponentName(ai.packageName, ai.name);
1485 Provider p = lookupProviderLocked(component);
1486 if (p == null) {
1487 if (addProviderLocked(ri)) {
1488 keep.add(ai.name);
1489 }
1490 } else {
1491 Provider parsed = parseProviderInfoXml(component, ri);
1492 if (parsed != null) {
1493 keep.add(ai.name);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001494 // Use the new AppWidgetProviderInfo.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 p.info = parsed.info;
1496 // If it's enabled
1497 final int M = p.instances.size();
1498 if (M > 0) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001499 int[] appWidgetIds = getAppWidgetIds(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 // Reschedule for the new updatePeriodMillis (don't worry about handling
1501 // it specially if updatePeriodMillis didn't change because we just sent
1502 // an update, and the next one will be updatePeriodMillis from now).
1503 cancelBroadcasts(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001504 registerForBroadcastsLocked(p, appWidgetIds);
1505 // If it's currently showing, call back with the new AppWidgetProviderInfo.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 for (int j=0; j<M; j++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001507 AppWidgetId id = p.instances.get(j);
Joe Onoratoa8a8a422010-06-16 15:06:16 -04001508 id.views = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 if (id.host != null && id.host.callbacks != null) {
1510 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001511 id.host.callbacks.providerChanged(id.appWidgetId, p.info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 } catch (RemoteException ex) {
1513 // It failed; remove the callback. No need to prune because
1514 // we know that this host is still referenced by this
1515 // instance.
1516 id.host.callbacks = null;
1517 }
1518 }
1519 }
1520 // Now that we've told the host, push out an update.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001521 sendUpdateIntentLocked(p, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
1523 }
1524 }
1525 }
1526 }
1527
1528 // prune the ones we don't want to keep
1529 N = mInstalledProviders.size();
1530 for (int i=N-1; i>=0; i--) {
1531 Provider p = mInstalledProviders.get(i);
1532 if (pkgName.equals(p.info.provider.getPackageName())
1533 && !keep.contains(p.info.provider.getClassName())) {
1534 removeProviderLocked(i, p);
1535 }
1536 }
1537 }
1538
1539 void removeProvidersForPackageLocked(String pkgName) {
1540 int N = mInstalledProviders.size();
1541 for (int i=N-1; i>=0; i--) {
1542 Provider p = mInstalledProviders.get(i);
1543 if (pkgName.equals(p.info.provider.getPackageName())) {
1544 removeProviderLocked(i, p);
1545 }
1546 }
1547
1548 // Delete the hosts for this package too
1549 //
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001550 // By now, we have removed any AppWidgets that were in any hosts here,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 // so we don't need to worry about sending DISABLE broadcasts to them.
1552 N = mHosts.size();
1553 for (int i=N-1; i>=0; i--) {
1554 Host host = mHosts.get(i);
1555 if (pkgName.equals(host.packageName)) {
1556 deleteHostLocked(host);
1557 }
1558 }
1559 }
1560}
1561