blob: 30597320f9a0761e70b37c5bb7ba0f31271a1d6e [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
Winson Chung499cb9f2010-07-16 11:18:17 -0700428 public void notifyAppWidgetViewDataChanged(int[] appWidgetIds, RemoteViews views, int viewId) {
429 if (appWidgetIds == null) {
430 return;
431 }
432 if (appWidgetIds.length == 0) {
433 return;
434 }
435 final int N = appWidgetIds.length;
436
437 synchronized (mAppWidgetIds) {
438 for (int i=0; i<N; i++) {
439 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetIds[i]);
440 notifyAppWidgetViewDataChangedInstanceLocked(id, views, viewId);
441 }
442 }
443 }
444
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700445 public void updateAppWidgetProvider(ComponentName provider, RemoteViews views) {
446 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 Provider p = lookupProviderLocked(provider);
448 if (p == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800449 Slog.w(TAG, "updateAppWidgetProvider: provider doesn't exist: " + provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 return;
451 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700452 ArrayList<AppWidgetId> instances = p.instances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 final int N = instances.size();
454 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700455 AppWidgetId id = instances.get(i);
456 updateAppWidgetInstanceLocked(id, views);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 }
458 }
459 }
460
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700461 void updateAppWidgetInstanceLocked(AppWidgetId id, RemoteViews views) {
462 // allow for stale appWidgetIds and other badness
463 // lookup also checks that the calling process can access the appWidgetId
464 // drop unbound appWidgetIds (shouldn't be possible under normal circumstances)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 if (id != null && id.provider != null && !id.provider.zombie && !id.host.zombie) {
466 id.views = views;
467
468 // is anyone listening?
469 if (id.host.callbacks != null) {
470 try {
471 // the lock is held, but this is a oneway call
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700472 id.host.callbacks.updateAppWidget(id.appWidgetId, views);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 } catch (RemoteException e) {
474 // It failed; remove the callback. No need to prune because
475 // we know that this host is still referenced by this instance.
476 id.host.callbacks = null;
477 }
478 }
479 }
480 }
481
Winson Chung499cb9f2010-07-16 11:18:17 -0700482 void notifyAppWidgetViewDataChangedInstanceLocked(AppWidgetId id, RemoteViews views, int viewId) {
483 // allow for stale appWidgetIds and other badness
484 // lookup also checks that the calling process can access the appWidgetId
485 // drop unbound appWidgetIds (shouldn't be possible under normal circumstances)
486 if (id != null && id.provider != null && !id.provider.zombie && !id.host.zombie) {
487 id.views = views;
488
489 // is anyone listening?
490 if (id.host.callbacks != null) {
491 try {
492 // the lock is held, but this is a oneway call
493 id.host.callbacks.viewDataChanged(id.appWidgetId, views, viewId);
494 } catch (RemoteException e) {
495 // It failed; remove the callback. No need to prune because
496 // we know that this host is still referenced by this instance.
497 id.host.callbacks = null;
498 }
499 }
500 }
501 }
502
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700503 public int[] startListening(IAppWidgetHost callbacks, String packageName, int hostId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 List<RemoteViews> updatedViews) {
505 int callingUid = enforceCallingUid(packageName);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700506 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 Host host = lookupOrAddHostLocked(callingUid, packageName, hostId);
508 host.callbacks = callbacks;
509
510 updatedViews.clear();
511
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700512 ArrayList<AppWidgetId> instances = host.instances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 int N = instances.size();
514 int[] updatedIds = new int[N];
515 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700516 AppWidgetId id = instances.get(i);
517 updatedIds[i] = id.appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 updatedViews.add(id.views);
519 }
520 return updatedIds;
521 }
522 }
523
524 public void stopListening(int hostId) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700525 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 Host host = lookupHostLocked(getCallingUid(), hostId);
Ken Shirriffe21167a2009-09-23 16:42:53 -0700527 if (host != null) {
528 host.callbacks = null;
529 pruneHostLocked(host);
530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
532 }
533
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700534 boolean canAccessAppWidgetId(AppWidgetId id, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 if (id.host.uid == callingUid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700536 // Apps hosting the AppWidget have access to it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 return true;
538 }
539 if (id.provider != null && id.provider.uid == callingUid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700540 // Apps providing the AppWidget have access to it (if the appWidgetId has been bound)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 return true;
542 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700543 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.BIND_APPWIDGET)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 == PackageManager.PERMISSION_GRANTED) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700545 // Apps that can bind have access to all appWidgetIds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 return true;
547 }
548 // Nobody else can access it.
549 return false;
550 }
551
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700552 AppWidgetId lookupAppWidgetIdLocked(int appWidgetId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 int callingUid = getCallingUid();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700554 final int N = mAppWidgetIds.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700556 AppWidgetId id = mAppWidgetIds.get(i);
557 if (id.appWidgetId == appWidgetId && canAccessAppWidgetId(id, callingUid)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 return id;
559 }
560 }
561 return null;
562 }
563
564 Provider lookupProviderLocked(ComponentName provider) {
Romain Guyd2671e12010-03-11 18:06:42 -0800565 final String className = provider.getClassName();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 final int N = mInstalledProviders.size();
567 for (int i=0; i<N; i++) {
568 Provider p = mInstalledProviders.get(i);
Romain Guyd2671e12010-03-11 18:06:42 -0800569 if (p.info.provider.equals(provider) || className.equals(p.info.oldName)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 return p;
571 }
572 }
573 return null;
574 }
575
576 Host lookupHostLocked(int uid, int hostId) {
577 final int N = mHosts.size();
578 for (int i=0; i<N; i++) {
579 Host h = mHosts.get(i);
580 if (h.uid == uid && h.hostId == hostId) {
581 return h;
582 }
583 }
584 return null;
585 }
586
587 Host lookupOrAddHostLocked(int uid, String packageName, int hostId) {
588 final int N = mHosts.size();
589 for (int i=0; i<N; i++) {
590 Host h = mHosts.get(i);
591 if (h.hostId == hostId && h.packageName.equals(packageName)) {
592 return h;
593 }
594 }
595 Host host = new Host();
596 host.packageName = packageName;
597 host.uid = uid;
598 host.hostId = hostId;
599 mHosts.add(host);
600 return host;
601 }
602
603 void pruneHostLocked(Host host) {
604 if (host.instances.size() == 0 && host.callbacks == null) {
605 mHosts.remove(host);
606 }
607 }
608
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700609 void loadAppWidgetList() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 PackageManager pm = mPackageManager;
611
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700612 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 List<ResolveInfo> broadcastReceivers = pm.queryBroadcastReceivers(intent,
614 PackageManager.GET_META_DATA);
615
Bjorn Bringert5f857802010-02-10 23:09:48 +0000616 final int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 for (int i=0; i<N; i++) {
618 ResolveInfo ri = broadcastReceivers.get(i);
619 addProviderLocked(ri);
620 }
621 }
622
623 boolean addProviderLocked(ResolveInfo ri) {
624 Provider p = parseProviderInfoXml(new ComponentName(ri.activityInfo.packageName,
625 ri.activityInfo.name), ri);
626 if (p != null) {
627 mInstalledProviders.add(p);
628 return true;
629 } else {
630 return false;
631 }
632 }
633
634 void removeProviderLocked(int index, Provider p) {
635 int N = p.instances.size();
636 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700637 AppWidgetId id = p.instances.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 // Call back with empty RemoteViews
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700639 updateAppWidgetInstanceLocked(id, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 // Stop telling the host about updates for this from now on
641 cancelBroadcasts(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700642 // clear out references to this appWidgetId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 id.host.instances.remove(id);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700644 mAppWidgetIds.remove(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 id.provider = null;
646 pruneHostLocked(id.host);
647 id.host = null;
648 }
649 p.instances.clear();
650 mInstalledProviders.remove(index);
651 // no need to send the DISABLE broadcast, since the receiver is gone anyway
652 cancelBroadcasts(p);
653 }
654
655 void sendEnableIntentLocked(Provider p) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700656 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_ENABLED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 intent.setComponent(p.info.provider);
658 mContext.sendBroadcast(intent);
659 }
660
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700661 void sendUpdateIntentLocked(Provider p, int[] appWidgetIds) {
662 if (appWidgetIds != null && appWidgetIds.length > 0) {
663 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
664 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 intent.setComponent(p.info.provider);
666 mContext.sendBroadcast(intent);
667 }
668 }
669
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700670 void registerForBroadcastsLocked(Provider p, int[] appWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 if (p.info.updatePeriodMillis > 0) {
672 // if this is the first instance, set the alarm. otherwise,
673 // rely on the fact that we've already set it and that
674 // PendingIntent.getBroadcast will update the extras.
675 boolean alreadyRegistered = p.broadcast != null;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700676 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
677 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 intent.setComponent(p.info.provider);
679 long token = Binder.clearCallingIdentity();
680 try {
681 p.broadcast = PendingIntent.getBroadcast(mContext, 1, intent,
682 PendingIntent.FLAG_UPDATE_CURRENT);
683 } finally {
684 Binder.restoreCallingIdentity(token);
685 }
686 if (!alreadyRegistered) {
Joe Onoratobe96b3a2009-07-14 19:49:27 -0700687 long period = p.info.updatePeriodMillis;
688 if (period < MIN_UPDATE_PERIOD) {
689 period = MIN_UPDATE_PERIOD;
690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
Joe Onoratobe96b3a2009-07-14 19:49:27 -0700692 SystemClock.elapsedRealtime() + period, period, p.broadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
694 }
695 }
696
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700697 static int[] getAppWidgetIds(Provider p) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 int instancesSize = p.instances.size();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700699 int appWidgetIds[] = new int[instancesSize];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 for (int i=0; i<instancesSize; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700701 appWidgetIds[i] = p.instances.get(i).appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700703 return appWidgetIds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700706 public int[] getAppWidgetIds(ComponentName provider) {
707 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 Provider p = lookupProviderLocked(provider);
709 if (p != null && getCallingUid() == p.uid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700710 return getAppWidgetIds(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 } else {
712 return new int[0];
713 }
714 }
715 }
716
717 private Provider parseProviderInfoXml(ComponentName component, ResolveInfo ri) {
718 Provider p = null;
719
720 ActivityInfo activityInfo = ri.activityInfo;
721 XmlResourceParser parser = null;
722 try {
723 parser = activityInfo.loadXmlMetaData(mPackageManager,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700724 AppWidgetManager.META_DATA_APPWIDGET_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 if (parser == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800726 Slog.w(TAG, "No " + AppWidgetManager.META_DATA_APPWIDGET_PROVIDER + " meta-data for "
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700727 + "AppWidget provider '" + component + '\'');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 return null;
729 }
730
731 AttributeSet attrs = Xml.asAttributeSet(parser);
732
733 int type;
734 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
735 && type != XmlPullParser.START_TAG) {
736 // drain whitespace, comments, etc.
737 }
738
739 String nodeName = parser.getName();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700740 if (!"appwidget-provider".equals(nodeName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800741 Slog.w(TAG, "Meta-data does not start with appwidget-provider tag for"
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700742 + " AppWidget provider '" + component + '\'');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 return null;
744 }
745
746 p = new Provider();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700747 AppWidgetProviderInfo info = p.info = new AppWidgetProviderInfo();
Romain Guyd2671e12010-03-11 18:06:42 -0800748 // If metaData was null, we would have returned earlier when getting
749 // the parser No need to do the check here
750 info.oldName = activityInfo.metaData.getString(
751 AppWidgetManager.META_DATA_APPWIDGET_OLD_NAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752
753 info.provider = component;
754 p.uid = activityInfo.applicationInfo.uid;
755
Dianne Hackborn20cb56e2010-03-04 00:58:29 -0800756 Resources res = mPackageManager.getResourcesForApplication(
757 activityInfo.applicationInfo);
758
759 TypedArray sa = res.obtainAttributes(attrs,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700760 com.android.internal.R.styleable.AppWidgetProviderInfo);
Mitsuru Oshima8f25c422009-07-01 00:10:43 -0700761
762 // These dimensions has to be resolved in the application's context.
763 // We simply send back the raw complex data, which will be
764 // converted to dp in {@link AppWidgetManager#getAppWidgetInfo}.
765 TypedValue value = sa.peekValue(
766 com.android.internal.R.styleable.AppWidgetProviderInfo_minWidth);
767 info.minWidth = value != null ? value.data : 0;
768 value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minHeight);
769 info.minHeight = value != null ? value.data : 0;
770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 info.updatePeriodMillis = sa.getInt(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700772 com.android.internal.R.styleable.AppWidgetProviderInfo_updatePeriodMillis, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 info.initialLayout = sa.getResourceId(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700774 com.android.internal.R.styleable.AppWidgetProviderInfo_initialLayout, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 String className = sa.getString(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700776 com.android.internal.R.styleable.AppWidgetProviderInfo_configure);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 if (className != null) {
778 info.configure = new ComponentName(component.getPackageName(), className);
779 }
780 info.label = activityInfo.loadLabel(mPackageManager).toString();
781 info.icon = ri.getIconResource();
Patrick Dubroyd2db2a52010-06-23 14:56:28 -0700782 info.previewImage = sa.getResourceId(
783 com.android.internal.R.styleable.AppWidgetProviderInfo_previewImage, 0);
784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 sa.recycle();
786 } catch (Exception e) {
787 // Ok to catch Exception here, because anything going wrong because
788 // of what a client process passes to us should not be fatal for the
789 // system process.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800790 Slog.w(TAG, "XML parsing failed for AppWidget provider '" + component + '\'', e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 return null;
792 } finally {
793 if (parser != null) parser.close();
794 }
795 return p;
796 }
797
798 int getUidForPackage(String packageName) throws PackageManager.NameNotFoundException {
799 PackageInfo pkgInfo = mPackageManager.getPackageInfo(packageName, 0);
800 if (pkgInfo == null || pkgInfo.applicationInfo == null) {
801 throw new PackageManager.NameNotFoundException();
802 }
803 return pkgInfo.applicationInfo.uid;
804 }
805
806 int enforceCallingUid(String packageName) throws IllegalArgumentException {
807 int callingUid = getCallingUid();
808 int packageUid;
809 try {
810 packageUid = getUidForPackage(packageName);
811 } catch (PackageManager.NameNotFoundException ex) {
812 throw new IllegalArgumentException("packageName and uid don't match packageName="
813 + packageName);
814 }
Marco Nelissen54796e72009-04-30 15:16:30 -0700815 if (callingUid != packageUid && Process.supportsProcesses()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 throw new IllegalArgumentException("packageName and uid don't match packageName="
817 + packageName);
818 }
819 return callingUid;
820 }
821
822 void sendInitialBroadcasts() {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700823 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 final int N = mInstalledProviders.size();
825 for (int i=0; i<N; i++) {
826 Provider p = mInstalledProviders.get(i);
827 if (p.instances.size() > 0) {
828 sendEnableIntentLocked(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700829 int[] appWidgetIds = getAppWidgetIds(p);
830 sendUpdateIntentLocked(p, appWidgetIds);
831 registerForBroadcastsLocked(p, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 }
833 }
834 }
835 }
836
837 // only call from initialization -- it assumes that the data structures are all empty
838 void loadStateLocked() {
839 File temp = savedStateTempFile();
840 File real = savedStateRealFile();
841
842 // prefer the real file. If it doesn't exist, use the temp one, and then copy it to the
843 // real one. if there is both a real file and a temp one, assume that the temp one isn't
844 // fully written and delete it.
845 if (real.exists()) {
846 readStateFromFileLocked(real);
847 if (temp.exists()) {
Romain Guya5475592009-07-01 17:20:08 -0700848 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 temp.delete();
850 }
851 } else if (temp.exists()) {
852 readStateFromFileLocked(temp);
Romain Guya5475592009-07-01 17:20:08 -0700853 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 temp.renameTo(real);
855 }
856 }
857
858 void saveStateLocked() {
859 File temp = savedStateTempFile();
860 File real = savedStateRealFile();
861
862 if (!real.exists()) {
863 // If the real one doesn't exist, it's either because this is the first time
864 // or because something went wrong while copying them. In this case, we can't
865 // trust anything that's in temp. In order to have the loadState code not
866 // use the temporary one until it's fully written, create an empty file
867 // for real, which will we'll shortly delete.
868 try {
Romain Guya5475592009-07-01 17:20:08 -0700869 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 real.createNewFile();
871 } catch (IOException e) {
Romain Guya5475592009-07-01 17:20:08 -0700872 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 }
874 }
875
876 if (temp.exists()) {
Romain Guya5475592009-07-01 17:20:08 -0700877 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 temp.delete();
879 }
880
Suchi Amalapurapu8550f252009-09-29 15:20:32 -0700881 if (!writeStateToFileLocked(temp)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800882 Slog.w(TAG, "Failed to persist new settings");
Suchi Amalapurapu8550f252009-09-29 15:20:32 -0700883 return;
884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885
Romain Guya5475592009-07-01 17:20:08 -0700886 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 real.delete();
Romain Guya5475592009-07-01 17:20:08 -0700888 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 temp.renameTo(real);
890 }
891
Suchi Amalapurapu8550f252009-09-29 15:20:32 -0700892 boolean writeStateToFileLocked(File file) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 FileOutputStream stream = null;
894 int N;
895
896 try {
897 stream = new FileOutputStream(file, false);
898 XmlSerializer out = new FastXmlSerializer();
899 out.setOutput(stream, "utf-8");
900 out.startDocument(null, true);
901
902
903 out.startTag(null, "gs");
904
905 int providerIndex = 0;
906 N = mInstalledProviders.size();
907 for (int i=0; i<N; i++) {
908 Provider p = mInstalledProviders.get(i);
909 if (p.instances.size() > 0) {
910 out.startTag(null, "p");
911 out.attribute(null, "pkg", p.info.provider.getPackageName());
912 out.attribute(null, "cl", p.info.provider.getClassName());
Patrick Tsaibd742e432010-05-01 00:30:19 +0800913 out.endTag(null, "p");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 p.tag = providerIndex;
915 providerIndex++;
916 }
917 }
918
919 N = mHosts.size();
920 for (int i=0; i<N; i++) {
921 Host host = mHosts.get(i);
922 out.startTag(null, "h");
923 out.attribute(null, "pkg", host.packageName);
924 out.attribute(null, "id", Integer.toHexString(host.hostId));
925 out.endTag(null, "h");
926 host.tag = i;
927 }
928
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700929 N = mAppWidgetIds.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700931 AppWidgetId id = mAppWidgetIds.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 out.startTag(null, "g");
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700933 out.attribute(null, "id", Integer.toHexString(id.appWidgetId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 out.attribute(null, "h", Integer.toHexString(id.host.tag));
935 if (id.provider != null) {
936 out.attribute(null, "p", Integer.toHexString(id.provider.tag));
937 }
938 out.endTag(null, "g");
939 }
940
941 out.endTag(null, "gs");
942
943 out.endDocument();
944 stream.close();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -0700945 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 } catch (IOException e) {
947 try {
948 if (stream != null) {
949 stream.close();
950 }
951 } catch (IOException ex) {
Romain Guya5475592009-07-01 17:20:08 -0700952 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
954 if (file.exists()) {
Romain Guya5475592009-07-01 17:20:08 -0700955 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 file.delete();
957 }
Suchi Amalapurapu8550f252009-09-29 15:20:32 -0700958 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
960 }
961
962 void readStateFromFileLocked(File file) {
963 FileInputStream stream = null;
964
965 boolean success = false;
966
967 try {
968 stream = new FileInputStream(file);
969 XmlPullParser parser = Xml.newPullParser();
970 parser.setInput(stream, null);
971
972 int type;
973 int providerIndex = 0;
Romain Guya5475592009-07-01 17:20:08 -0700974 HashMap<Integer,Provider> loadedProviders = new HashMap<Integer, Provider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 do {
976 type = parser.next();
977 if (type == XmlPullParser.START_TAG) {
978 String tag = parser.getName();
979 if ("p".equals(tag)) {
980 // TODO: do we need to check that this package has the same signature
981 // as before?
982 String pkg = parser.getAttributeValue(null, "pkg");
983 String cl = parser.getAttributeValue(null, "cl");
Romain Guyff3e61c2010-03-11 15:30:02 -0800984
985 final PackageManager packageManager = mContext.getPackageManager();
986 try {
987 packageManager.getReceiverInfo(new ComponentName(pkg, cl), 0);
988 } catch (PackageManager.NameNotFoundException e) {
989 String[] pkgs = packageManager.currentToCanonicalPackageNames(
990 new String[] { pkg });
991 pkg = pkgs[0];
992 }
993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 Provider p = lookupProviderLocked(new ComponentName(pkg, cl));
995 if (p == null && mSafeMode) {
996 // if we're in safe mode, make a temporary one
997 p = new Provider();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700998 p.info = new AppWidgetProviderInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 p.info.provider = new ComponentName(pkg, cl);
1000 p.zombie = true;
1001 mInstalledProviders.add(p);
1002 }
1003 if (p != null) {
1004 // if it wasn't uninstalled or something
1005 loadedProviders.put(providerIndex, p);
1006 }
1007 providerIndex++;
1008 }
1009 else if ("h".equals(tag)) {
1010 Host host = new Host();
1011
1012 // TODO: do we need to check that this package has the same signature
1013 // as before?
1014 host.packageName = parser.getAttributeValue(null, "pkg");
1015 try {
1016 host.uid = getUidForPackage(host.packageName);
1017 } catch (PackageManager.NameNotFoundException ex) {
1018 host.zombie = true;
1019 }
1020 if (!host.zombie || mSafeMode) {
1021 // In safe mode, we don't discard the hosts we don't recognize
1022 // so that they're not pruned from our list. Otherwise, we do.
1023 host.hostId = Integer.parseInt(
1024 parser.getAttributeValue(null, "id"), 16);
1025 mHosts.add(host);
1026 }
1027 }
1028 else if ("g".equals(tag)) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001029 AppWidgetId id = new AppWidgetId();
1030 id.appWidgetId = Integer.parseInt(parser.getAttributeValue(null, "id"), 16);
1031 if (id.appWidgetId >= mNextAppWidgetId) {
1032 mNextAppWidgetId = id.appWidgetId + 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
1034
1035 String providerString = parser.getAttributeValue(null, "p");
1036 if (providerString != null) {
1037 // there's no provider if it hasn't been bound yet.
1038 // maybe we don't have to save this, but it brings the system
1039 // to the state it was in.
1040 int pIndex = Integer.parseInt(providerString, 16);
1041 id.provider = loadedProviders.get(pIndex);
1042 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001043 Slog.d(TAG, "bound appWidgetId=" + id.appWidgetId + " to provider "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 + pIndex + " which is " + id.provider);
1045 }
1046 if (id.provider == null) {
1047 // This provider is gone. We just let the host figure out
1048 // that this happened when it fails to load it.
1049 continue;
1050 }
1051 }
1052
1053 int hIndex = Integer.parseInt(parser.getAttributeValue(null, "h"), 16);
1054 id.host = mHosts.get(hIndex);
1055 if (id.host == null) {
1056 // This host is gone.
1057 continue;
1058 }
1059
1060 if (id.provider != null) {
1061 id.provider.instances.add(id);
1062 }
1063 id.host.instances.add(id);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001064 mAppWidgetIds.add(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 }
1066 }
1067 } while (type != XmlPullParser.END_DOCUMENT);
1068 success = true;
1069 } catch (NullPointerException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001070 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001072 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001074 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001076 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 } catch (IndexOutOfBoundsException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001078 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
1080 try {
1081 if (stream != null) {
1082 stream.close();
1083 }
1084 } catch (IOException e) {
Romain Guya5475592009-07-01 17:20:08 -07001085 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
1087
1088 if (success) {
1089 // delete any hosts that didn't manage to get connected (should happen)
1090 // if it matters, they'll be reconnected.
Dianne Hackborn002716d2009-08-12 11:13:26 -07001091 for (int i=mHosts.size()-1; i>=0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 pruneHostLocked(mHosts.get(i));
1093 }
1094 } else {
1095 // failed reading, clean up
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001096 mAppWidgetIds.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 mHosts.clear();
1098 final int N = mInstalledProviders.size();
1099 for (int i=0; i<N; i++) {
1100 mInstalledProviders.get(i).instances.clear();
1101 }
1102 }
1103 }
1104
1105 File savedStateTempFile() {
1106 return new File("/data/system/" + SETTINGS_TMP_FILENAME);
1107 //return new File(mContext.getFilesDir(), SETTINGS_FILENAME);
1108 }
1109
1110 File savedStateRealFile() {
1111 return new File("/data/system/" + SETTINGS_FILENAME);
1112 //return new File(mContext.getFilesDir(), SETTINGS_TMP_FILENAME);
1113 }
1114
1115 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1116 public void onReceive(Context context, Intent intent) {
1117 String action = intent.getAction();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001118 //Slog.d(TAG, "received " + action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
1120 sendInitialBroadcasts();
Eric Fischer63c2d9e2009-10-22 15:22:50 -07001121 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1122 Locale revised = Locale.getDefault();
1123 if (revised == null || mLocale == null ||
1124 !(revised.equals(mLocale))) {
1125 mLocale = revised;
1126
1127 synchronized (mAppWidgetIds) {
1128 int N = mInstalledProviders.size();
1129 for (int i=N-1; i>=0; i--) {
1130 Provider p = mInstalledProviders.get(i);
1131 String pkgName = p.info.provider.getPackageName();
1132 updateProvidersForPackageLocked(pkgName);
1133 }
1134 saveStateLocked();
1135 }
1136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 } else {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001138 boolean added = false;
1139 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001140 if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001141 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1142 added = true;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001143 } if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001144 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1145 added = false;
1146 } else {
1147 Uri uri = intent.getData();
1148 if (uri == null) {
1149 return;
1150 }
1151 String pkgName = uri.getSchemeSpecificPart();
1152 if (pkgName == null) {
1153 return;
1154 }
1155 pkgList = new String[] { pkgName };
1156 added = Intent.ACTION_PACKAGE_ADDED.equals(action);
1157 }
1158 if (pkgList == null || pkgList.length == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 return;
1160 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001161 if (added) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001162 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 Bundle extras = intent.getExtras();
1164 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001165 for (String pkgName : pkgList) {
1166 // The package was just upgraded
1167 updateProvidersForPackageLocked(pkgName);
1168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 } else {
1170 // The package was just added
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001171 for (String pkgName : pkgList) {
1172 addProvidersForPackageLocked(pkgName);
1173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
1175 saveStateLocked();
1176 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001177 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 Bundle extras = intent.getExtras();
1179 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
1180 // The package is being updated. We'll receive a PACKAGE_ADDED shortly.
1181 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001182 synchronized (mAppWidgetIds) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001183 for (String pkgName : pkgList) {
1184 removeProvidersForPackageLocked(pkgName);
1185 saveStateLocked();
1186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188 }
1189 }
1190 }
1191 }
1192 };
1193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 void addProvidersForPackageLocked(String pkgName) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001195 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
Joe Onoratof6133fe2010-02-01 18:24:46 -05001196 intent.setPackage(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent,
1198 PackageManager.GET_META_DATA);
1199
Bjorn Bringert5f857802010-02-10 23:09:48 +00001200 final int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 for (int i=0; i<N; i++) {
1202 ResolveInfo ri = broadcastReceivers.get(i);
1203 ActivityInfo ai = ri.activityInfo;
1204
1205 if (pkgName.equals(ai.packageName)) {
1206 addProviderLocked(ri);
1207 }
1208 }
1209 }
1210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 void updateProvidersForPackageLocked(String pkgName) {
Romain Guya5475592009-07-01 17:20:08 -07001212 HashSet<String> keep = new HashSet<String>();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001213 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
Joe Onoratof6133fe2010-02-01 18:24:46 -05001214 intent.setPackage(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent,
1216 PackageManager.GET_META_DATA);
1217
1218 // add the missing ones and collect which ones to keep
Bjorn Bringert5f857802010-02-10 23:09:48 +00001219 int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 for (int i=0; i<N; i++) {
1221 ResolveInfo ri = broadcastReceivers.get(i);
1222 ActivityInfo ai = ri.activityInfo;
1223 if (pkgName.equals(ai.packageName)) {
1224 ComponentName component = new ComponentName(ai.packageName, ai.name);
1225 Provider p = lookupProviderLocked(component);
1226 if (p == null) {
1227 if (addProviderLocked(ri)) {
1228 keep.add(ai.name);
1229 }
1230 } else {
1231 Provider parsed = parseProviderInfoXml(component, ri);
1232 if (parsed != null) {
1233 keep.add(ai.name);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001234 // Use the new AppWidgetProviderInfo.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 p.info = parsed.info;
1236 // If it's enabled
1237 final int M = p.instances.size();
1238 if (M > 0) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001239 int[] appWidgetIds = getAppWidgetIds(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 // Reschedule for the new updatePeriodMillis (don't worry about handling
1241 // it specially if updatePeriodMillis didn't change because we just sent
1242 // an update, and the next one will be updatePeriodMillis from now).
1243 cancelBroadcasts(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001244 registerForBroadcastsLocked(p, appWidgetIds);
1245 // If it's currently showing, call back with the new AppWidgetProviderInfo.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 for (int j=0; j<M; j++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001247 AppWidgetId id = p.instances.get(j);
Joe Onoratoa8a8a422010-06-16 15:06:16 -04001248 id.views = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 if (id.host != null && id.host.callbacks != null) {
1250 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001251 id.host.callbacks.providerChanged(id.appWidgetId, p.info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 } catch (RemoteException ex) {
1253 // It failed; remove the callback. No need to prune because
1254 // we know that this host is still referenced by this
1255 // instance.
1256 id.host.callbacks = null;
1257 }
1258 }
1259 }
1260 // Now that we've told the host, push out an update.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001261 sendUpdateIntentLocked(p, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263 }
1264 }
1265 }
1266 }
1267
1268 // prune the ones we don't want to keep
1269 N = mInstalledProviders.size();
1270 for (int i=N-1; i>=0; i--) {
1271 Provider p = mInstalledProviders.get(i);
1272 if (pkgName.equals(p.info.provider.getPackageName())
1273 && !keep.contains(p.info.provider.getClassName())) {
1274 removeProviderLocked(i, p);
1275 }
1276 }
1277 }
1278
1279 void removeProvidersForPackageLocked(String pkgName) {
1280 int N = mInstalledProviders.size();
1281 for (int i=N-1; i>=0; i--) {
1282 Provider p = mInstalledProviders.get(i);
1283 if (pkgName.equals(p.info.provider.getPackageName())) {
1284 removeProviderLocked(i, p);
1285 }
1286 }
1287
1288 // Delete the hosts for this package too
1289 //
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001290 // By now, we have removed any AppWidgets that were in any hosts here,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 // so we don't need to worry about sending DISABLE broadcasts to them.
1292 N = mHosts.size();
1293 for (int i=N-1; i>=0; i--) {
1294 Host host = mHosts.get(i);
1295 if (pkgName.equals(host.packageName)) {
1296 deleteHostLocked(host);
1297 }
1298 }
1299 }
1300}
1301