blob: 73502137886bbe2381eb164898c64c5105f88828 [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
19import android.app.AlarmManager;
20import android.app.PendingIntent;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070021import android.appwidget.AppWidgetManager;
22import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.BroadcastReceiver;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.pm.ActivityInfo;
29import android.content.pm.PackageManager;
30import android.content.pm.PackageInfo;
31import android.content.pm.ResolveInfo;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080032import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.res.TypedArray;
34import android.content.res.XmlResourceParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.net.Uri;
36import android.os.Binder;
37import android.os.Bundle;
Marco Nelissen54796e72009-04-30 15:16:30 -070038import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.RemoteException;
40import android.os.SystemClock;
41import android.util.AttributeSet;
Joe Onorato8a9b2202010-02-26 18:56:32 -080042import android.util.Slog;
Mitsuru Oshima8f25c422009-07-01 00:10:43 -070043import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.util.Xml;
45import android.widget.RemoteViews;
46
47import java.io.IOException;
48import java.io.File;
49import java.io.FileDescriptor;
50import java.io.FileInputStream;
51import java.io.FileOutputStream;
52import java.io.PrintWriter;
53import java.util.ArrayList;
54import java.util.List;
Eric Fischer63c2d9e2009-10-22 15:22:50 -070055import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import java.util.HashMap;
57import java.util.HashSet;
58
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070059import com.android.internal.appwidget.IAppWidgetService;
60import com.android.internal.appwidget.IAppWidgetHost;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080061import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63import org.xmlpull.v1.XmlPullParser;
64import org.xmlpull.v1.XmlPullParserException;
65import org.xmlpull.v1.XmlSerializer;
66
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070067class AppWidgetService extends IAppWidgetService.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070069 private static final String TAG = "AppWidgetService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070071 private static final String SETTINGS_FILENAME = "appwidgets.xml";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 private static final String SETTINGS_TMP_FILENAME = SETTINGS_FILENAME + ".tmp";
Joe Onoratobe96b3a2009-07-14 19:49:27 -070073 private static final int MIN_UPDATE_PERIOD = 30 * 60 * 1000; // 30 minutes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
75 /*
76 * When identifying a Host or Provider based on the calling process, use the uid field.
77 * When identifying a Host or Provider based on a package manager broadcast, use the
78 * package given.
79 */
80
81 static class Provider {
82 int uid;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070083 AppWidgetProviderInfo info;
Romain Guya5475592009-07-01 17:20:08 -070084 ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 PendingIntent broadcast;
86 boolean zombie; // if we're in safe mode, don't prune this just because nobody references it
87
88 int tag; // for use while saving state (the index)
89 }
90
91 static class Host {
92 int uid;
93 int hostId;
94 String packageName;
Romain Guya5475592009-07-01 17:20:08 -070095 ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070096 IAppWidgetHost callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 boolean zombie; // if we're in safe mode, don't prune this just because nobody references it
98
99 int tag; // for use while saving state (the index)
100 }
101
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700102 static class AppWidgetId {
103 int appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 Provider provider;
105 RemoteViews views;
106 Host host;
107 }
108
109 Context mContext;
Eric Fischer63c2d9e2009-10-22 15:22:50 -0700110 Locale mLocale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 PackageManager mPackageManager;
112 AlarmManager mAlarmManager;
Romain Guya5475592009-07-01 17:20:08 -0700113 ArrayList<Provider> mInstalledProviders = new ArrayList<Provider>();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700114 int mNextAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID + 1;
Romain Guya5475592009-07-01 17:20:08 -0700115 final ArrayList<AppWidgetId> mAppWidgetIds = new ArrayList<AppWidgetId>();
116 ArrayList<Host> mHosts = new ArrayList<Host>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 boolean mSafeMode;
118
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700119 AppWidgetService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 mContext = context;
121 mPackageManager = context.getPackageManager();
122 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
123 }
124
125 public void systemReady(boolean safeMode) {
126 mSafeMode = safeMode;
127
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700128 loadAppWidgetList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 loadStateLocked();
130
131 // Register for the boot completed broadcast, so we can send the
132 // ENABLE broacasts. If we try to send them now, they time out,
133 // because the system isn't ready to handle them yet.
134 mContext.registerReceiver(mBroadcastReceiver,
135 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
136
Eric Fischer63c2d9e2009-10-22 15:22:50 -0700137 // Register for configuration changes so we can update the names
138 // of the widgets when the locale changes.
139 mContext.registerReceiver(mBroadcastReceiver,
140 new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED), null, null);
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 // Register for broadcasts about package install, etc., so we can
143 // update the provider list.
144 IntentFilter filter = new IntentFilter();
145 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
146 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
147 filter.addDataScheme("package");
148 mContext.registerReceiver(mBroadcastReceiver, filter);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800149 // Register for events related to sdcard installation.
150 IntentFilter sdFilter = new IntentFilter();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800151 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
152 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800153 mContext.registerReceiver(mBroadcastReceiver, sdFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 }
155
156 @Override
157 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
158 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
159 != PackageManager.PERMISSION_GRANTED) {
160 pw.println("Permission Denial: can't dump from from pid="
161 + Binder.getCallingPid()
162 + ", uid=" + Binder.getCallingUid());
163 return;
164 }
165
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700166 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 int N = mInstalledProviders.size();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700168 pw.println("Providers:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 for (int i=0; i<N; i++) {
170 Provider p = mInstalledProviders.get(i);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700171 AppWidgetProviderInfo info = p.info;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700172 pw.print(" ["); pw.print(i); pw.print("] provider ");
173 pw.print(info.provider.flattenToShortString());
174 pw.println(':');
175 pw.print(" min=("); pw.print(info.minWidth);
176 pw.print("x"); pw.print(info.minHeight);
177 pw.print(") updatePeriodMillis=");
178 pw.print(info.updatePeriodMillis);
179 pw.print(" initialLayout=#");
180 pw.print(Integer.toHexString(info.initialLayout));
181 pw.print(" zombie="); pw.println(p.zombie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 }
183
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700184 N = mAppWidgetIds.size();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700185 pw.println(" ");
186 pw.println("AppWidgetIds:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700188 AppWidgetId id = mAppWidgetIds.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700189 pw.print(" ["); pw.print(i); pw.print("] id=");
Romain Guya5475592009-07-01 17:20:08 -0700190 pw.println(id.appWidgetId);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700191 pw.print(" hostId=");
192 pw.print(id.host.hostId); pw.print(' ');
193 pw.print(id.host.packageName); pw.print('/');
194 pw.println(id.host.uid);
195 if (id.provider != null) {
196 pw.print(" provider=");
197 pw.println(id.provider.info.provider.flattenToShortString());
198 }
199 if (id.host != null) {
200 pw.print(" host.callbacks="); pw.println(id.host.callbacks);
201 }
202 if (id.views != null) {
203 pw.print(" views="); pw.println(id.views);
204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
206
207 N = mHosts.size();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700208 pw.println(" ");
209 pw.println("Hosts:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 for (int i=0; i<N; i++) {
211 Host host = mHosts.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700212 pw.print(" ["); pw.print(i); pw.print("] hostId=");
213 pw.print(host.hostId); pw.print(' ');
214 pw.print(host.packageName); pw.print('/');
215 pw.print(host.uid); pw.println(':');
216 pw.print(" callbacks="); pw.println(host.callbacks);
217 pw.print(" instances.size="); pw.print(host.instances.size());
218 pw.print(" zombie="); pw.println(host.zombie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 }
220 }
221 }
222
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700223 public int allocateAppWidgetId(String packageName, int hostId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 int callingUid = enforceCallingUid(packageName);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700225 synchronized (mAppWidgetIds) {
226 int appWidgetId = mNextAppWidgetId++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227
228 Host host = lookupOrAddHostLocked(callingUid, packageName, hostId);
229
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700230 AppWidgetId id = new AppWidgetId();
231 id.appWidgetId = appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 id.host = host;
233
234 host.instances.add(id);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700235 mAppWidgetIds.add(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
237 saveStateLocked();
238
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700239 return appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 }
241 }
242
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700243 public void deleteAppWidgetId(int appWidgetId) {
244 synchronized (mAppWidgetIds) {
245 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 if (id != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700247 deleteAppWidgetLocked(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 saveStateLocked();
249 }
250 }
251 }
252
253 public void deleteHost(int hostId) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700254 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 int callingUid = getCallingUid();
256 Host host = lookupHostLocked(callingUid, hostId);
257 if (host != null) {
258 deleteHostLocked(host);
259 saveStateLocked();
260 }
261 }
262 }
263
264 public void deleteAllHosts() {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700265 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 int callingUid = getCallingUid();
267 final int N = mHosts.size();
268 boolean changed = false;
269 for (int i=N-1; i>=0; i--) {
270 Host host = mHosts.get(i);
271 if (host.uid == callingUid) {
272 deleteHostLocked(host);
273 changed = true;
274 }
275 }
276 if (changed) {
277 saveStateLocked();
278 }
279 }
280 }
281
282 void deleteHostLocked(Host host) {
283 final int N = host.instances.size();
284 for (int i=N-1; i>=0; i--) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700285 AppWidgetId id = host.instances.get(i);
286 deleteAppWidgetLocked(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
288 host.instances.clear();
289 mHosts.remove(host);
290 // it's gone or going away, abruptly drop the callback connection
291 host.callbacks = null;
292 }
293
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700294 void deleteAppWidgetLocked(AppWidgetId id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 Host host = id.host;
296 host.instances.remove(id);
297 pruneHostLocked(host);
298
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700299 mAppWidgetIds.remove(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300
301 Provider p = id.provider;
302 if (p != null) {
303 p.instances.remove(id);
304 if (!p.zombie) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700305 // send the broacast saying that this appWidgetId has been deleted
306 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_DELETED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 intent.setComponent(p.info.provider);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700308 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id.appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 mContext.sendBroadcast(intent);
310 if (p.instances.size() == 0) {
311 // cancel the future updates
312 cancelBroadcasts(p);
313
314 // send the broacast saying that the provider is not in use any more
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700315 intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_DISABLED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 intent.setComponent(p.info.provider);
317 mContext.sendBroadcast(intent);
318 }
319 }
320 }
321 }
322
323 void cancelBroadcasts(Provider p) {
324 if (p.broadcast != null) {
325 mAlarmManager.cancel(p.broadcast);
326 long token = Binder.clearCallingIdentity();
327 try {
328 p.broadcast.cancel();
329 } finally {
330 Binder.restoreCallingIdentity(token);
331 }
332 p.broadcast = null;
333 }
334 }
335
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700336 public void bindAppWidgetId(int appWidgetId, ComponentName provider) {
337 mContext.enforceCallingPermission(android.Manifest.permission.BIND_APPWIDGET,
338 "bindGagetId appWidgetId=" + appWidgetId + " provider=" + provider);
339 synchronized (mAppWidgetIds) {
340 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 if (id == null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700342 throw new IllegalArgumentException("bad appWidgetId");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
344 if (id.provider != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700345 throw new IllegalArgumentException("appWidgetId " + appWidgetId + " already bound to "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 + id.provider.info.provider);
347 }
348 Provider p = lookupProviderLocked(provider);
349 if (p == null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700350 throw new IllegalArgumentException("not a appwidget provider: " + provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 }
352 if (p.zombie) {
353 throw new IllegalArgumentException("can't bind to a 3rd party provider in"
354 + " safe mode: " + provider);
355 }
356
357 id.provider = p;
358 p.instances.add(id);
359 int instancesSize = p.instances.size();
360 if (instancesSize == 1) {
361 // tell the provider that it's ready
362 sendEnableIntentLocked(p);
363 }
364
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700365 // send an update now -- We need this update now, and just for this appWidgetId.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 // It's less critical when the next one happens, so when we schdule the next one,
367 // we add updatePeriodMillis to its start time. That time will have some slop,
368 // but that's okay.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700369 sendUpdateIntentLocked(p, new int[] { appWidgetId });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370
371 // schedule the future updates
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700372 registerForBroadcastsLocked(p, getAppWidgetIds(p));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 saveStateLocked();
374 }
375 }
376
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700377 public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId) {
378 synchronized (mAppWidgetIds) {
379 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 if (id != null && id.provider != null && !id.provider.zombie) {
381 return id.provider.info;
382 }
383 return null;
384 }
385 }
386
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700387 public RemoteViews getAppWidgetViews(int appWidgetId) {
388 synchronized (mAppWidgetIds) {
389 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 if (id != null) {
391 return id.views;
392 }
393 return null;
394 }
395 }
396
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700397 public List<AppWidgetProviderInfo> getInstalledProviders() {
398 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 final int N = mInstalledProviders.size();
Romain Guya5475592009-07-01 17:20:08 -0700400 ArrayList<AppWidgetProviderInfo> result = new ArrayList<AppWidgetProviderInfo>(N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 for (int i=0; i<N; i++) {
402 Provider p = mInstalledProviders.get(i);
403 if (!p.zombie) {
404 result.add(p.info);
405 }
406 }
407 return result;
408 }
409 }
410
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700411 public void updateAppWidgetIds(int[] appWidgetIds, RemoteViews views) {
412 if (appWidgetIds == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 return;
414 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700415 if (appWidgetIds.length == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 return;
417 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700418 final int N = appWidgetIds.length;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700420 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700422 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetIds[i]);
423 updateAppWidgetInstanceLocked(id, views);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
425 }
426 }
427
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700428 public void updateAppWidgetProvider(ComponentName provider, RemoteViews views) {
429 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 Provider p = lookupProviderLocked(provider);
431 if (p == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800432 Slog.w(TAG, "updateAppWidgetProvider: provider doesn't exist: " + provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 return;
434 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700435 ArrayList<AppWidgetId> instances = p.instances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 final int N = instances.size();
437 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700438 AppWidgetId id = instances.get(i);
439 updateAppWidgetInstanceLocked(id, views);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 }
441 }
442 }
443
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700444 void updateAppWidgetInstanceLocked(AppWidgetId id, RemoteViews views) {
445 // allow for stale appWidgetIds and other badness
446 // lookup also checks that the calling process can access the appWidgetId
447 // drop unbound appWidgetIds (shouldn't be possible under normal circumstances)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 if (id != null && id.provider != null && !id.provider.zombie && !id.host.zombie) {
449 id.views = views;
450
451 // is anyone listening?
452 if (id.host.callbacks != null) {
453 try {
454 // the lock is held, but this is a oneway call
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700455 id.host.callbacks.updateAppWidget(id.appWidgetId, views);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 } catch (RemoteException e) {
457 // It failed; remove the callback. No need to prune because
458 // we know that this host is still referenced by this instance.
459 id.host.callbacks = null;
460 }
461 }
462 }
463 }
464
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700465 public int[] startListening(IAppWidgetHost callbacks, String packageName, int hostId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 List<RemoteViews> updatedViews) {
467 int callingUid = enforceCallingUid(packageName);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700468 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 Host host = lookupOrAddHostLocked(callingUid, packageName, hostId);
470 host.callbacks = callbacks;
471
472 updatedViews.clear();
473
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700474 ArrayList<AppWidgetId> instances = host.instances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 int N = instances.size();
476 int[] updatedIds = new int[N];
477 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700478 AppWidgetId id = instances.get(i);
479 updatedIds[i] = id.appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 updatedViews.add(id.views);
481 }
482 return updatedIds;
483 }
484 }
485
486 public void stopListening(int hostId) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700487 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 Host host = lookupHostLocked(getCallingUid(), hostId);
Ken Shirriffe21167a2009-09-23 16:42:53 -0700489 if (host != null) {
490 host.callbacks = null;
491 pruneHostLocked(host);
492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
494 }
495
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700496 boolean canAccessAppWidgetId(AppWidgetId id, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 if (id.host.uid == callingUid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700498 // Apps hosting the AppWidget have access to it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 return true;
500 }
501 if (id.provider != null && id.provider.uid == callingUid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700502 // Apps providing the AppWidget have access to it (if the appWidgetId has been bound)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 return true;
504 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700505 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.BIND_APPWIDGET)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 == PackageManager.PERMISSION_GRANTED) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700507 // Apps that can bind have access to all appWidgetIds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 return true;
509 }
510 // Nobody else can access it.
511 return false;
512 }
513
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700514 AppWidgetId lookupAppWidgetIdLocked(int appWidgetId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 int callingUid = getCallingUid();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700516 final int N = mAppWidgetIds.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700518 AppWidgetId id = mAppWidgetIds.get(i);
519 if (id.appWidgetId == appWidgetId && canAccessAppWidgetId(id, callingUid)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 return id;
521 }
522 }
523 return null;
524 }
525
526 Provider lookupProviderLocked(ComponentName provider) {
527 final int N = mInstalledProviders.size();
528 for (int i=0; i<N; i++) {
529 Provider p = mInstalledProviders.get(i);
530 if (p.info.provider.equals(provider)) {
531 return p;
532 }
533 }
534 return null;
535 }
536
537 Host lookupHostLocked(int uid, int hostId) {
538 final int N = mHosts.size();
539 for (int i=0; i<N; i++) {
540 Host h = mHosts.get(i);
541 if (h.uid == uid && h.hostId == hostId) {
542 return h;
543 }
544 }
545 return null;
546 }
547
548 Host lookupOrAddHostLocked(int uid, String packageName, int hostId) {
549 final int N = mHosts.size();
550 for (int i=0; i<N; i++) {
551 Host h = mHosts.get(i);
552 if (h.hostId == hostId && h.packageName.equals(packageName)) {
553 return h;
554 }
555 }
556 Host host = new Host();
557 host.packageName = packageName;
558 host.uid = uid;
559 host.hostId = hostId;
560 mHosts.add(host);
561 return host;
562 }
563
564 void pruneHostLocked(Host host) {
565 if (host.instances.size() == 0 && host.callbacks == null) {
566 mHosts.remove(host);
567 }
568 }
569
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700570 void loadAppWidgetList() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 PackageManager pm = mPackageManager;
572
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700573 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 List<ResolveInfo> broadcastReceivers = pm.queryBroadcastReceivers(intent,
575 PackageManager.GET_META_DATA);
576
Bjorn Bringert5f857802010-02-10 23:09:48 +0000577 final int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 for (int i=0; i<N; i++) {
579 ResolveInfo ri = broadcastReceivers.get(i);
580 addProviderLocked(ri);
581 }
582 }
583
584 boolean addProviderLocked(ResolveInfo ri) {
585 Provider p = parseProviderInfoXml(new ComponentName(ri.activityInfo.packageName,
586 ri.activityInfo.name), ri);
587 if (p != null) {
588 mInstalledProviders.add(p);
589 return true;
590 } else {
591 return false;
592 }
593 }
594
595 void removeProviderLocked(int index, Provider p) {
596 int N = p.instances.size();
597 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700598 AppWidgetId id = p.instances.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 // Call back with empty RemoteViews
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700600 updateAppWidgetInstanceLocked(id, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 // Stop telling the host about updates for this from now on
602 cancelBroadcasts(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700603 // clear out references to this appWidgetId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 id.host.instances.remove(id);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700605 mAppWidgetIds.remove(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 id.provider = null;
607 pruneHostLocked(id.host);
608 id.host = null;
609 }
610 p.instances.clear();
611 mInstalledProviders.remove(index);
612 // no need to send the DISABLE broadcast, since the receiver is gone anyway
613 cancelBroadcasts(p);
614 }
615
616 void sendEnableIntentLocked(Provider p) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700617 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_ENABLED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 intent.setComponent(p.info.provider);
619 mContext.sendBroadcast(intent);
620 }
621
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700622 void sendUpdateIntentLocked(Provider p, int[] appWidgetIds) {
623 if (appWidgetIds != null && appWidgetIds.length > 0) {
624 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
625 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 intent.setComponent(p.info.provider);
627 mContext.sendBroadcast(intent);
628 }
629 }
630
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700631 void registerForBroadcastsLocked(Provider p, int[] appWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 if (p.info.updatePeriodMillis > 0) {
633 // if this is the first instance, set the alarm. otherwise,
634 // rely on the fact that we've already set it and that
635 // PendingIntent.getBroadcast will update the extras.
636 boolean alreadyRegistered = p.broadcast != null;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700637 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
638 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 intent.setComponent(p.info.provider);
640 long token = Binder.clearCallingIdentity();
641 try {
642 p.broadcast = PendingIntent.getBroadcast(mContext, 1, intent,
643 PendingIntent.FLAG_UPDATE_CURRENT);
644 } finally {
645 Binder.restoreCallingIdentity(token);
646 }
647 if (!alreadyRegistered) {
Joe Onoratobe96b3a2009-07-14 19:49:27 -0700648 long period = p.info.updatePeriodMillis;
649 if (period < MIN_UPDATE_PERIOD) {
650 period = MIN_UPDATE_PERIOD;
651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
Joe Onoratobe96b3a2009-07-14 19:49:27 -0700653 SystemClock.elapsedRealtime() + period, period, p.broadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
655 }
656 }
657
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700658 static int[] getAppWidgetIds(Provider p) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 int instancesSize = p.instances.size();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700660 int appWidgetIds[] = new int[instancesSize];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 for (int i=0; i<instancesSize; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700662 appWidgetIds[i] = p.instances.get(i).appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700664 return appWidgetIds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700667 public int[] getAppWidgetIds(ComponentName provider) {
668 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 Provider p = lookupProviderLocked(provider);
670 if (p != null && getCallingUid() == p.uid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700671 return getAppWidgetIds(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 } else {
673 return new int[0];
674 }
675 }
676 }
677
678 private Provider parseProviderInfoXml(ComponentName component, ResolveInfo ri) {
679 Provider p = null;
680
681 ActivityInfo activityInfo = ri.activityInfo;
682 XmlResourceParser parser = null;
683 try {
684 parser = activityInfo.loadXmlMetaData(mPackageManager,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700685 AppWidgetManager.META_DATA_APPWIDGET_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 if (parser == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800687 Slog.w(TAG, "No " + AppWidgetManager.META_DATA_APPWIDGET_PROVIDER + " meta-data for "
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700688 + "AppWidget provider '" + component + '\'');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 return null;
690 }
691
692 AttributeSet attrs = Xml.asAttributeSet(parser);
693
694 int type;
695 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
696 && type != XmlPullParser.START_TAG) {
697 // drain whitespace, comments, etc.
698 }
699
700 String nodeName = parser.getName();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700701 if (!"appwidget-provider".equals(nodeName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800702 Slog.w(TAG, "Meta-data does not start with appwidget-provider tag for"
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700703 + " AppWidget provider '" + component + '\'');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 return null;
705 }
706
707 p = new Provider();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700708 AppWidgetProviderInfo info = p.info = new AppWidgetProviderInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709
710 info.provider = component;
711 p.uid = activityInfo.applicationInfo.uid;
712
Dianne Hackborn20cb56e2010-03-04 00:58:29 -0800713 Resources res = mPackageManager.getResourcesForApplication(
714 activityInfo.applicationInfo);
715
716 TypedArray sa = res.obtainAttributes(attrs,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700717 com.android.internal.R.styleable.AppWidgetProviderInfo);
Mitsuru Oshima8f25c422009-07-01 00:10:43 -0700718
719 // These dimensions has to be resolved in the application's context.
720 // We simply send back the raw complex data, which will be
721 // converted to dp in {@link AppWidgetManager#getAppWidgetInfo}.
722 TypedValue value = sa.peekValue(
723 com.android.internal.R.styleable.AppWidgetProviderInfo_minWidth);
724 info.minWidth = value != null ? value.data : 0;
725 value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minHeight);
726 info.minHeight = value != null ? value.data : 0;
727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 info.updatePeriodMillis = sa.getInt(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700729 com.android.internal.R.styleable.AppWidgetProviderInfo_updatePeriodMillis, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 info.initialLayout = sa.getResourceId(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700731 com.android.internal.R.styleable.AppWidgetProviderInfo_initialLayout, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 String className = sa.getString(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700733 com.android.internal.R.styleable.AppWidgetProviderInfo_configure);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 if (className != null) {
735 info.configure = new ComponentName(component.getPackageName(), className);
736 }
737 info.label = activityInfo.loadLabel(mPackageManager).toString();
738 info.icon = ri.getIconResource();
739 sa.recycle();
740 } catch (Exception e) {
741 // Ok to catch Exception here, because anything going wrong because
742 // of what a client process passes to us should not be fatal for the
743 // system process.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800744 Slog.w(TAG, "XML parsing failed for AppWidget provider '" + component + '\'', e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 return null;
746 } finally {
747 if (parser != null) parser.close();
748 }
749 return p;
750 }
751
752 int getUidForPackage(String packageName) throws PackageManager.NameNotFoundException {
753 PackageInfo pkgInfo = mPackageManager.getPackageInfo(packageName, 0);
754 if (pkgInfo == null || pkgInfo.applicationInfo == null) {
755 throw new PackageManager.NameNotFoundException();
756 }
757 return pkgInfo.applicationInfo.uid;
758 }
759
760 int enforceCallingUid(String packageName) throws IllegalArgumentException {
761 int callingUid = getCallingUid();
762 int packageUid;
763 try {
764 packageUid = getUidForPackage(packageName);
765 } catch (PackageManager.NameNotFoundException ex) {
766 throw new IllegalArgumentException("packageName and uid don't match packageName="
767 + packageName);
768 }
Marco Nelissen54796e72009-04-30 15:16:30 -0700769 if (callingUid != packageUid && Process.supportsProcesses()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 throw new IllegalArgumentException("packageName and uid don't match packageName="
771 + packageName);
772 }
773 return callingUid;
774 }
775
776 void sendInitialBroadcasts() {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700777 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 final int N = mInstalledProviders.size();
779 for (int i=0; i<N; i++) {
780 Provider p = mInstalledProviders.get(i);
781 if (p.instances.size() > 0) {
782 sendEnableIntentLocked(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700783 int[] appWidgetIds = getAppWidgetIds(p);
784 sendUpdateIntentLocked(p, appWidgetIds);
785 registerForBroadcastsLocked(p, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 }
787 }
788 }
789 }
790
791 // only call from initialization -- it assumes that the data structures are all empty
792 void loadStateLocked() {
793 File temp = savedStateTempFile();
794 File real = savedStateRealFile();
795
796 // prefer the real file. If it doesn't exist, use the temp one, and then copy it to the
797 // real one. if there is both a real file and a temp one, assume that the temp one isn't
798 // fully written and delete it.
799 if (real.exists()) {
800 readStateFromFileLocked(real);
801 if (temp.exists()) {
Romain Guya5475592009-07-01 17:20:08 -0700802 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 temp.delete();
804 }
805 } else if (temp.exists()) {
806 readStateFromFileLocked(temp);
Romain Guya5475592009-07-01 17:20:08 -0700807 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 temp.renameTo(real);
809 }
810 }
811
812 void saveStateLocked() {
813 File temp = savedStateTempFile();
814 File real = savedStateRealFile();
815
816 if (!real.exists()) {
817 // If the real one doesn't exist, it's either because this is the first time
818 // or because something went wrong while copying them. In this case, we can't
819 // trust anything that's in temp. In order to have the loadState code not
820 // use the temporary one until it's fully written, create an empty file
821 // for real, which will we'll shortly delete.
822 try {
Romain Guya5475592009-07-01 17:20:08 -0700823 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 real.createNewFile();
825 } catch (IOException e) {
Romain Guya5475592009-07-01 17:20:08 -0700826 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 }
828 }
829
830 if (temp.exists()) {
Romain Guya5475592009-07-01 17:20:08 -0700831 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 temp.delete();
833 }
834
Suchi Amalapurapu8550f252009-09-29 15:20:32 -0700835 if (!writeStateToFileLocked(temp)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800836 Slog.w(TAG, "Failed to persist new settings");
Suchi Amalapurapu8550f252009-09-29 15:20:32 -0700837 return;
838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839
Romain Guya5475592009-07-01 17:20:08 -0700840 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 real.delete();
Romain Guya5475592009-07-01 17:20:08 -0700842 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 temp.renameTo(real);
844 }
845
Suchi Amalapurapu8550f252009-09-29 15:20:32 -0700846 boolean writeStateToFileLocked(File file) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 FileOutputStream stream = null;
848 int N;
849
850 try {
851 stream = new FileOutputStream(file, false);
852 XmlSerializer out = new FastXmlSerializer();
853 out.setOutput(stream, "utf-8");
854 out.startDocument(null, true);
855
856
857 out.startTag(null, "gs");
858
859 int providerIndex = 0;
860 N = mInstalledProviders.size();
861 for (int i=0; i<N; i++) {
862 Provider p = mInstalledProviders.get(i);
863 if (p.instances.size() > 0) {
864 out.startTag(null, "p");
865 out.attribute(null, "pkg", p.info.provider.getPackageName());
866 out.attribute(null, "cl", p.info.provider.getClassName());
867 out.endTag(null, "h");
868 p.tag = providerIndex;
869 providerIndex++;
870 }
871 }
872
873 N = mHosts.size();
874 for (int i=0; i<N; i++) {
875 Host host = mHosts.get(i);
876 out.startTag(null, "h");
877 out.attribute(null, "pkg", host.packageName);
878 out.attribute(null, "id", Integer.toHexString(host.hostId));
879 out.endTag(null, "h");
880 host.tag = i;
881 }
882
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700883 N = mAppWidgetIds.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700885 AppWidgetId id = mAppWidgetIds.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 out.startTag(null, "g");
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700887 out.attribute(null, "id", Integer.toHexString(id.appWidgetId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 out.attribute(null, "h", Integer.toHexString(id.host.tag));
889 if (id.provider != null) {
890 out.attribute(null, "p", Integer.toHexString(id.provider.tag));
891 }
892 out.endTag(null, "g");
893 }
894
895 out.endTag(null, "gs");
896
897 out.endDocument();
898 stream.close();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -0700899 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 } catch (IOException e) {
901 try {
902 if (stream != null) {
903 stream.close();
904 }
905 } catch (IOException ex) {
Romain Guya5475592009-07-01 17:20:08 -0700906 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
908 if (file.exists()) {
Romain Guya5475592009-07-01 17:20:08 -0700909 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 file.delete();
911 }
Suchi Amalapurapu8550f252009-09-29 15:20:32 -0700912 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914 }
915
916 void readStateFromFileLocked(File file) {
917 FileInputStream stream = null;
918
919 boolean success = false;
920
921 try {
922 stream = new FileInputStream(file);
923 XmlPullParser parser = Xml.newPullParser();
924 parser.setInput(stream, null);
925
926 int type;
927 int providerIndex = 0;
Romain Guya5475592009-07-01 17:20:08 -0700928 HashMap<Integer,Provider> loadedProviders = new HashMap<Integer, Provider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 do {
930 type = parser.next();
931 if (type == XmlPullParser.START_TAG) {
932 String tag = parser.getName();
933 if ("p".equals(tag)) {
934 // TODO: do we need to check that this package has the same signature
935 // as before?
936 String pkg = parser.getAttributeValue(null, "pkg");
937 String cl = parser.getAttributeValue(null, "cl");
Romain Guyff3e61c2010-03-11 15:30:02 -0800938
939 final PackageManager packageManager = mContext.getPackageManager();
940 try {
941 packageManager.getReceiverInfo(new ComponentName(pkg, cl), 0);
942 } catch (PackageManager.NameNotFoundException e) {
943 String[] pkgs = packageManager.currentToCanonicalPackageNames(
944 new String[] { pkg });
945 pkg = pkgs[0];
946 }
947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 Provider p = lookupProviderLocked(new ComponentName(pkg, cl));
949 if (p == null && mSafeMode) {
950 // if we're in safe mode, make a temporary one
951 p = new Provider();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700952 p.info = new AppWidgetProviderInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 p.info.provider = new ComponentName(pkg, cl);
954 p.zombie = true;
955 mInstalledProviders.add(p);
956 }
957 if (p != null) {
958 // if it wasn't uninstalled or something
959 loadedProviders.put(providerIndex, p);
960 }
961 providerIndex++;
962 }
963 else if ("h".equals(tag)) {
964 Host host = new Host();
965
966 // TODO: do we need to check that this package has the same signature
967 // as before?
968 host.packageName = parser.getAttributeValue(null, "pkg");
969 try {
970 host.uid = getUidForPackage(host.packageName);
971 } catch (PackageManager.NameNotFoundException ex) {
972 host.zombie = true;
973 }
974 if (!host.zombie || mSafeMode) {
975 // In safe mode, we don't discard the hosts we don't recognize
976 // so that they're not pruned from our list. Otherwise, we do.
977 host.hostId = Integer.parseInt(
978 parser.getAttributeValue(null, "id"), 16);
979 mHosts.add(host);
980 }
981 }
982 else if ("g".equals(tag)) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700983 AppWidgetId id = new AppWidgetId();
984 id.appWidgetId = Integer.parseInt(parser.getAttributeValue(null, "id"), 16);
985 if (id.appWidgetId >= mNextAppWidgetId) {
986 mNextAppWidgetId = id.appWidgetId + 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 }
988
989 String providerString = parser.getAttributeValue(null, "p");
990 if (providerString != null) {
991 // there's no provider if it hasn't been bound yet.
992 // maybe we don't have to save this, but it brings the system
993 // to the state it was in.
994 int pIndex = Integer.parseInt(providerString, 16);
995 id.provider = loadedProviders.get(pIndex);
996 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800997 Slog.d(TAG, "bound appWidgetId=" + id.appWidgetId + " to provider "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 + pIndex + " which is " + id.provider);
999 }
1000 if (id.provider == null) {
1001 // This provider is gone. We just let the host figure out
1002 // that this happened when it fails to load it.
1003 continue;
1004 }
1005 }
1006
1007 int hIndex = Integer.parseInt(parser.getAttributeValue(null, "h"), 16);
1008 id.host = mHosts.get(hIndex);
1009 if (id.host == null) {
1010 // This host is gone.
1011 continue;
1012 }
1013
1014 if (id.provider != null) {
1015 id.provider.instances.add(id);
1016 }
1017 id.host.instances.add(id);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001018 mAppWidgetIds.add(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 }
1020 }
1021 } while (type != XmlPullParser.END_DOCUMENT);
1022 success = true;
1023 } catch (NullPointerException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001024 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001026 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001028 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001030 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 } catch (IndexOutOfBoundsException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001032 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
1034 try {
1035 if (stream != null) {
1036 stream.close();
1037 }
1038 } catch (IOException e) {
Romain Guya5475592009-07-01 17:20:08 -07001039 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041
1042 if (success) {
1043 // delete any hosts that didn't manage to get connected (should happen)
1044 // if it matters, they'll be reconnected.
Dianne Hackborn002716d2009-08-12 11:13:26 -07001045 for (int i=mHosts.size()-1; i>=0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 pruneHostLocked(mHosts.get(i));
1047 }
1048 } else {
1049 // failed reading, clean up
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001050 mAppWidgetIds.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 mHosts.clear();
1052 final int N = mInstalledProviders.size();
1053 for (int i=0; i<N; i++) {
1054 mInstalledProviders.get(i).instances.clear();
1055 }
1056 }
1057 }
1058
1059 File savedStateTempFile() {
1060 return new File("/data/system/" + SETTINGS_TMP_FILENAME);
1061 //return new File(mContext.getFilesDir(), SETTINGS_FILENAME);
1062 }
1063
1064 File savedStateRealFile() {
1065 return new File("/data/system/" + SETTINGS_FILENAME);
1066 //return new File(mContext.getFilesDir(), SETTINGS_TMP_FILENAME);
1067 }
1068
1069 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1070 public void onReceive(Context context, Intent intent) {
1071 String action = intent.getAction();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001072 //Slog.d(TAG, "received " + action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
1074 sendInitialBroadcasts();
Eric Fischer63c2d9e2009-10-22 15:22:50 -07001075 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1076 Locale revised = Locale.getDefault();
1077 if (revised == null || mLocale == null ||
1078 !(revised.equals(mLocale))) {
1079 mLocale = revised;
1080
1081 synchronized (mAppWidgetIds) {
1082 int N = mInstalledProviders.size();
1083 for (int i=N-1; i>=0; i--) {
1084 Provider p = mInstalledProviders.get(i);
1085 String pkgName = p.info.provider.getPackageName();
1086 updateProvidersForPackageLocked(pkgName);
1087 }
1088 saveStateLocked();
1089 }
1090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 } else {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001092 boolean added = false;
1093 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001094 if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001095 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1096 added = true;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001097 } if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001098 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1099 added = false;
1100 } else {
1101 Uri uri = intent.getData();
1102 if (uri == null) {
1103 return;
1104 }
1105 String pkgName = uri.getSchemeSpecificPart();
1106 if (pkgName == null) {
1107 return;
1108 }
1109 pkgList = new String[] { pkgName };
1110 added = Intent.ACTION_PACKAGE_ADDED.equals(action);
1111 }
1112 if (pkgList == null || pkgList.length == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 return;
1114 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001115 if (added) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001116 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 Bundle extras = intent.getExtras();
1118 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001119 for (String pkgName : pkgList) {
1120 // The package was just upgraded
1121 updateProvidersForPackageLocked(pkgName);
1122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 } else {
1124 // The package was just added
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001125 for (String pkgName : pkgList) {
1126 addProvidersForPackageLocked(pkgName);
1127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
1129 saveStateLocked();
1130 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001131 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 Bundle extras = intent.getExtras();
1133 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
1134 // The package is being updated. We'll receive a PACKAGE_ADDED shortly.
1135 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001136 synchronized (mAppWidgetIds) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001137 for (String pkgName : pkgList) {
1138 removeProvidersForPackageLocked(pkgName);
1139 saveStateLocked();
1140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
1142 }
1143 }
1144 }
1145 }
1146 };
1147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 void addProvidersForPackageLocked(String pkgName) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001149 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
Joe Onoratof6133fe2010-02-01 18:24:46 -05001150 intent.setPackage(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent,
1152 PackageManager.GET_META_DATA);
1153
Bjorn Bringert5f857802010-02-10 23:09:48 +00001154 final int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 for (int i=0; i<N; i++) {
1156 ResolveInfo ri = broadcastReceivers.get(i);
1157 ActivityInfo ai = ri.activityInfo;
1158
1159 if (pkgName.equals(ai.packageName)) {
1160 addProviderLocked(ri);
1161 }
1162 }
1163 }
1164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 void updateProvidersForPackageLocked(String pkgName) {
Romain Guya5475592009-07-01 17:20:08 -07001166 HashSet<String> keep = new HashSet<String>();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001167 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
Joe Onoratof6133fe2010-02-01 18:24:46 -05001168 intent.setPackage(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent,
1170 PackageManager.GET_META_DATA);
1171
1172 // add the missing ones and collect which ones to keep
Bjorn Bringert5f857802010-02-10 23:09:48 +00001173 int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 for (int i=0; i<N; i++) {
1175 ResolveInfo ri = broadcastReceivers.get(i);
1176 ActivityInfo ai = ri.activityInfo;
1177 if (pkgName.equals(ai.packageName)) {
1178 ComponentName component = new ComponentName(ai.packageName, ai.name);
1179 Provider p = lookupProviderLocked(component);
1180 if (p == null) {
1181 if (addProviderLocked(ri)) {
1182 keep.add(ai.name);
1183 }
1184 } else {
1185 Provider parsed = parseProviderInfoXml(component, ri);
1186 if (parsed != null) {
1187 keep.add(ai.name);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001188 // Use the new AppWidgetProviderInfo.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 p.info = parsed.info;
1190 // If it's enabled
1191 final int M = p.instances.size();
1192 if (M > 0) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001193 int[] appWidgetIds = getAppWidgetIds(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 // Reschedule for the new updatePeriodMillis (don't worry about handling
1195 // it specially if updatePeriodMillis didn't change because we just sent
1196 // an update, and the next one will be updatePeriodMillis from now).
1197 cancelBroadcasts(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001198 registerForBroadcastsLocked(p, appWidgetIds);
1199 // If it's currently showing, call back with the new AppWidgetProviderInfo.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 for (int j=0; j<M; j++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001201 AppWidgetId id = p.instances.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 if (id.host != null && id.host.callbacks != null) {
1203 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001204 id.host.callbacks.providerChanged(id.appWidgetId, p.info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 } catch (RemoteException ex) {
1206 // It failed; remove the callback. No need to prune because
1207 // we know that this host is still referenced by this
1208 // instance.
1209 id.host.callbacks = null;
1210 }
1211 }
1212 }
1213 // Now that we've told the host, push out an update.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001214 sendUpdateIntentLocked(p, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 }
1216 }
1217 }
1218 }
1219 }
1220
1221 // prune the ones we don't want to keep
1222 N = mInstalledProviders.size();
1223 for (int i=N-1; i>=0; i--) {
1224 Provider p = mInstalledProviders.get(i);
1225 if (pkgName.equals(p.info.provider.getPackageName())
1226 && !keep.contains(p.info.provider.getClassName())) {
1227 removeProviderLocked(i, p);
1228 }
1229 }
1230 }
1231
1232 void removeProvidersForPackageLocked(String pkgName) {
1233 int N = mInstalledProviders.size();
1234 for (int i=N-1; i>=0; i--) {
1235 Provider p = mInstalledProviders.get(i);
1236 if (pkgName.equals(p.info.provider.getPackageName())) {
1237 removeProviderLocked(i, p);
1238 }
1239 }
1240
1241 // Delete the hosts for this package too
1242 //
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001243 // By now, we have removed any AppWidgets that were in any hosts here,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 // so we don't need to worry about sending DISABLE broadcasts to them.
1245 N = mHosts.size();
1246 for (int i=N-1; i>=0; i--) {
1247 Host host = mHosts.get(i);
1248 if (pkgName.equals(host.packageName)) {
1249 deleteHostLocked(host);
1250 }
1251 }
1252 }
1253}
1254