blob: f5fd6bd866a5f1d386ed581a1ec624c38983a71c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Winson Chung81f39eb2011-01-11 18:05:01 -080019import java.io.File;
20import java.io.FileDescriptor;
21import java.io.FileInputStream;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.io.PrintWriter;
25import java.util.ArrayList;
26import java.util.HashMap;
27import java.util.HashSet;
28import java.util.Iterator;
29import java.util.List;
30import java.util.Locale;
31
Dianne Hackborn2f0b1752011-05-31 17:59:49 -070032import org.apache.commons.logging.impl.SimpleLog;
Winson Chung81f39eb2011-01-11 18:05:01 -080033import org.xmlpull.v1.XmlPullParser;
34import org.xmlpull.v1.XmlPullParserException;
35import org.xmlpull.v1.XmlSerializer;
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.app.AlarmManager;
38import android.app.PendingIntent;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070039import android.appwidget.AppWidgetManager;
40import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.BroadcastReceiver;
42import android.content.ComponentName;
43import android.content.Context;
44import android.content.Intent;
Winson Chung81f39eb2011-01-11 18:05:01 -080045import android.content.Intent.FilterComparison;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.IntentFilter;
Winson Chung81f39eb2011-01-11 18:05:01 -080047import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.pm.ActivityInfo;
Joe Onorato331fbdc72010-08-24 17:02:09 -040049import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.pm.PackageInfo;
Winson Chung81f39eb2011-01-11 18:05:01 -080051import android.content.pm.PackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.pm.ResolveInfo;
Winson Chung81f39eb2011-01-11 18:05:01 -080053import android.content.pm.ServiceInfo;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080054import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.res.TypedArray;
56import android.content.res.XmlResourceParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.net.Uri;
58import android.os.Binder;
59import android.os.Bundle;
Winson Chung81f39eb2011-01-11 18:05:01 -080060import android.os.IBinder;
Marco Nelissen54796e72009-04-30 15:16:30 -070061import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.RemoteException;
63import android.os.SystemClock;
64import android.util.AttributeSet;
Winson Chung16c8d8a2011-01-20 16:19:33 -080065import android.util.Log;
Winson Chung81f39eb2011-01-11 18:05:01 -080066import android.util.Pair;
Joe Onorato8a9b2202010-02-26 18:56:32 -080067import android.util.Slog;
Mitsuru Oshima8f25c422009-07-01 00:10:43 -070068import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.Xml;
70import android.widget.RemoteViews;
Winson Chung84bbb022011-02-21 13:57:45 -080071import android.widget.RemoteViewsService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070073import com.android.internal.appwidget.IAppWidgetHost;
Winson Chung81f39eb2011-01-11 18:05:01 -080074import com.android.internal.appwidget.IAppWidgetService;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080075import com.android.internal.util.FastXmlSerializer;
Winson Chung81f39eb2011-01-11 18:05:01 -080076import com.android.internal.widget.IRemoteViewsAdapterConnection;
Winson Chung84bbb022011-02-21 13:57:45 -080077import com.android.internal.widget.IRemoteViewsFactory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070079class AppWidgetService extends IAppWidgetService.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070081 private static final String TAG = "AppWidgetService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070083 private static final String SETTINGS_FILENAME = "appwidgets.xml";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 private static final String SETTINGS_TMP_FILENAME = SETTINGS_FILENAME + ".tmp";
Joe Onoratobe96b3a2009-07-14 19:49:27 -070085 private static final int MIN_UPDATE_PERIOD = 30 * 60 * 1000; // 30 minutes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87 /*
88 * When identifying a Host or Provider based on the calling process, use the uid field.
89 * When identifying a Host or Provider based on a package manager broadcast, use the
90 * package given.
91 */
92
93 static class Provider {
94 int uid;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070095 AppWidgetProviderInfo info;
Romain Guya5475592009-07-01 17:20:08 -070096 ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 PendingIntent broadcast;
98 boolean zombie; // if we're in safe mode, don't prune this just because nobody references it
99
100 int tag; // for use while saving state (the index)
101 }
102
103 static class Host {
104 int uid;
105 int hostId;
106 String packageName;
Romain Guya5475592009-07-01 17:20:08 -0700107 ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700108 IAppWidgetHost callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 boolean zombie; // if we're in safe mode, don't prune this just because nobody references it
110
111 int tag; // for use while saving state (the index)
112 }
113
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700114 static class AppWidgetId {
115 int appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 Provider provider;
117 RemoteViews views;
118 Host host;
119 }
120
Winson Chung81f39eb2011-01-11 18:05:01 -0800121 /**
122 * Acts as a proxy between the ServiceConnection and the RemoteViewsAdapterConnection.
123 * This needs to be a static inner class since a reference to the ServiceConnection is held
124 * globally and may lead us to leak AppWidgetService instances (if there were more than one).
125 */
126 static class ServiceConnectionProxy implements ServiceConnection {
Winson Chung81f39eb2011-01-11 18:05:01 -0800127 private final Pair<Integer, Intent.FilterComparison> mKey;
128 private final IBinder mConnectionCb;
129
Winson Chung16c8d8a2011-01-20 16:19:33 -0800130 ServiceConnectionProxy(Pair<Integer, Intent.FilterComparison> key, IBinder connectionCb) {
Winson Chung81f39eb2011-01-11 18:05:01 -0800131 mKey = key;
132 mConnectionCb = connectionCb;
133 }
134 public void onServiceConnected(ComponentName name, IBinder service) {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800135 final IRemoteViewsAdapterConnection cb =
Winson Chung81f39eb2011-01-11 18:05:01 -0800136 IRemoteViewsAdapterConnection.Stub.asInterface(mConnectionCb);
137 try {
138 cb.onServiceConnected(service);
Adam Cohenc2be22c2011-03-16 16:33:53 -0700139 } catch (Exception e) {
Winson Chung81f39eb2011-01-11 18:05:01 -0800140 e.printStackTrace();
141 }
142 }
143 public void onServiceDisconnected(ComponentName name) {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800144 disconnect();
145 }
146 public void disconnect() {
147 final IRemoteViewsAdapterConnection cb =
Winson Chung81f39eb2011-01-11 18:05:01 -0800148 IRemoteViewsAdapterConnection.Stub.asInterface(mConnectionCb);
149 try {
150 cb.onServiceDisconnected();
Adam Cohenc2be22c2011-03-16 16:33:53 -0700151 } catch (Exception e) {
Winson Chung81f39eb2011-01-11 18:05:01 -0800152 e.printStackTrace();
153 }
154 }
155 }
156
Winson Chung84bbb022011-02-21 13:57:45 -0800157 // Manages active connections to RemoteViewsServices
Winson Chung81f39eb2011-01-11 18:05:01 -0800158 private final HashMap<Pair<Integer, FilterComparison>, ServiceConnection>
159 mBoundRemoteViewsServices = new HashMap<Pair<Integer,FilterComparison>,ServiceConnection>();
Winson Chung84bbb022011-02-21 13:57:45 -0800160 // Manages persistent references to RemoteViewsServices from different App Widgets
161 private final HashMap<FilterComparison, HashSet<Integer>>
162 mRemoteViewsServicesAppWidgets = new HashMap<FilterComparison, HashSet<Integer>>();
Winson Chung81f39eb2011-01-11 18:05:01 -0800163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 Context mContext;
Eric Fischer63c2d9e2009-10-22 15:22:50 -0700165 Locale mLocale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 PackageManager mPackageManager;
167 AlarmManager mAlarmManager;
Romain Guya5475592009-07-01 17:20:08 -0700168 ArrayList<Provider> mInstalledProviders = new ArrayList<Provider>();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700169 int mNextAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID + 1;
Romain Guya5475592009-07-01 17:20:08 -0700170 final ArrayList<AppWidgetId> mAppWidgetIds = new ArrayList<AppWidgetId>();
171 ArrayList<Host> mHosts = new ArrayList<Host>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 boolean mSafeMode;
173
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700174 AppWidgetService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 mContext = context;
176 mPackageManager = context.getPackageManager();
177 mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
178 }
179
180 public void systemReady(boolean safeMode) {
181 mSafeMode = safeMode;
182
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700183 loadAppWidgetList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 loadStateLocked();
185
186 // Register for the boot completed broadcast, so we can send the
187 // ENABLE broacasts. If we try to send them now, they time out,
188 // because the system isn't ready to handle them yet.
189 mContext.registerReceiver(mBroadcastReceiver,
190 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
191
Eric Fischer63c2d9e2009-10-22 15:22:50 -0700192 // Register for configuration changes so we can update the names
193 // of the widgets when the locale changes.
194 mContext.registerReceiver(mBroadcastReceiver,
195 new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED), null, null);
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 // Register for broadcasts about package install, etc., so we can
198 // update the provider list.
199 IntentFilter filter = new IntentFilter();
200 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
Joe Onoratod070e892011-01-07 20:50:37 -0800201 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
203 filter.addDataScheme("package");
204 mContext.registerReceiver(mBroadcastReceiver, filter);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800205 // Register for events related to sdcard installation.
206 IntentFilter sdFilter = new IntentFilter();
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800207 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
208 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800209 mContext.registerReceiver(mBroadcastReceiver, sdFilter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211
212 @Override
213 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
214 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
215 != PackageManager.PERMISSION_GRANTED) {
216 pw.println("Permission Denial: can't dump from from pid="
217 + Binder.getCallingPid()
218 + ", uid=" + Binder.getCallingUid());
219 return;
220 }
221
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700222 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 int N = mInstalledProviders.size();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700224 pw.println("Providers:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 for (int i=0; i<N; i++) {
226 Provider p = mInstalledProviders.get(i);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700227 AppWidgetProviderInfo info = p.info;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700228 pw.print(" ["); pw.print(i); pw.print("] provider ");
229 pw.print(info.provider.flattenToShortString());
230 pw.println(':');
231 pw.print(" min=("); pw.print(info.minWidth);
232 pw.print("x"); pw.print(info.minHeight);
Adam Cohen324afba2011-07-22 11:51:45 -0700233 pw.print(") minResize=("); pw.print(info.minResizeWidth);
234 pw.print("x"); pw.print(info.minResizeHeight);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700235 pw.print(") updatePeriodMillis=");
236 pw.print(info.updatePeriodMillis);
Adam Cohen324afba2011-07-22 11:51:45 -0700237 pw.print(" resizeMode=");
238 pw.print(info.resizeMode);
239 pw.print(" autoAdvanceViewId=");
240 pw.print(info.autoAdvanceViewId);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700241 pw.print(" initialLayout=#");
242 pw.print(Integer.toHexString(info.initialLayout));
243 pw.print(" zombie="); pw.println(p.zombie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 }
245
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700246 N = mAppWidgetIds.size();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700247 pw.println(" ");
248 pw.println("AppWidgetIds:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700250 AppWidgetId id = mAppWidgetIds.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700251 pw.print(" ["); pw.print(i); pw.print("] id=");
Romain Guya5475592009-07-01 17:20:08 -0700252 pw.println(id.appWidgetId);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700253 pw.print(" hostId=");
254 pw.print(id.host.hostId); pw.print(' ');
255 pw.print(id.host.packageName); pw.print('/');
256 pw.println(id.host.uid);
257 if (id.provider != null) {
258 pw.print(" provider=");
259 pw.println(id.provider.info.provider.flattenToShortString());
260 }
261 if (id.host != null) {
262 pw.print(" host.callbacks="); pw.println(id.host.callbacks);
263 }
264 if (id.views != null) {
265 pw.print(" views="); pw.println(id.views);
266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 }
268
269 N = mHosts.size();
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700270 pw.println(" ");
271 pw.println("Hosts:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 for (int i=0; i<N; i++) {
273 Host host = mHosts.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700274 pw.print(" ["); pw.print(i); pw.print("] hostId=");
275 pw.print(host.hostId); pw.print(' ');
276 pw.print(host.packageName); pw.print('/');
277 pw.print(host.uid); pw.println(':');
278 pw.print(" callbacks="); pw.println(host.callbacks);
279 pw.print(" instances.size="); pw.print(host.instances.size());
280 pw.print(" zombie="); pw.println(host.zombie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 }
282 }
283 }
284
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700285 public int allocateAppWidgetId(String packageName, int hostId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 int callingUid = enforceCallingUid(packageName);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700287 synchronized (mAppWidgetIds) {
288 int appWidgetId = mNextAppWidgetId++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289
290 Host host = lookupOrAddHostLocked(callingUid, packageName, hostId);
291
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700292 AppWidgetId id = new AppWidgetId();
293 id.appWidgetId = appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 id.host = host;
295
296 host.instances.add(id);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700297 mAppWidgetIds.add(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
299 saveStateLocked();
300
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700301 return appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303 }
304
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700305 public void deleteAppWidgetId(int appWidgetId) {
306 synchronized (mAppWidgetIds) {
307 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 if (id != null) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700309 deleteAppWidgetLocked(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 saveStateLocked();
311 }
312 }
313 }
314
315 public void deleteHost(int hostId) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700316 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 int callingUid = getCallingUid();
318 Host host = lookupHostLocked(callingUid, hostId);
319 if (host != null) {
320 deleteHostLocked(host);
321 saveStateLocked();
322 }
323 }
324 }
325
326 public void deleteAllHosts() {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700327 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 int callingUid = getCallingUid();
329 final int N = mHosts.size();
330 boolean changed = false;
331 for (int i=N-1; i>=0; i--) {
332 Host host = mHosts.get(i);
333 if (host.uid == callingUid) {
334 deleteHostLocked(host);
335 changed = true;
336 }
337 }
338 if (changed) {
339 saveStateLocked();
340 }
341 }
342 }
343
344 void deleteHostLocked(Host host) {
345 final int N = host.instances.size();
346 for (int i=N-1; i>=0; i--) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700347 AppWidgetId id = host.instances.get(i);
348 deleteAppWidgetLocked(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
350 host.instances.clear();
351 mHosts.remove(host);
352 // it's gone or going away, abruptly drop the callback connection
353 host.callbacks = null;
354 }
355
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700356 void deleteAppWidgetLocked(AppWidgetId id) {
Winson Chung81f39eb2011-01-11 18:05:01 -0800357 // We first unbind all services that are bound to this id
358 unbindAppWidgetRemoteViewsServicesLocked(id);
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 Host host = id.host;
361 host.instances.remove(id);
362 pruneHostLocked(host);
363
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700364 mAppWidgetIds.remove(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365
366 Provider p = id.provider;
367 if (p != null) {
368 p.instances.remove(id);
369 if (!p.zombie) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700370 // send the broacast saying that this appWidgetId has been deleted
371 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_DELETED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 intent.setComponent(p.info.provider);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700373 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id.appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 mContext.sendBroadcast(intent);
375 if (p.instances.size() == 0) {
376 // cancel the future updates
377 cancelBroadcasts(p);
378
379 // send the broacast saying that the provider is not in use any more
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700380 intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_DISABLED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 intent.setComponent(p.info.provider);
382 mContext.sendBroadcast(intent);
383 }
384 }
385 }
386 }
387
388 void cancelBroadcasts(Provider p) {
389 if (p.broadcast != null) {
390 mAlarmManager.cancel(p.broadcast);
391 long token = Binder.clearCallingIdentity();
392 try {
393 p.broadcast.cancel();
394 } finally {
395 Binder.restoreCallingIdentity(token);
396 }
397 p.broadcast = null;
398 }
399 }
400
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700401 public void bindAppWidgetId(int appWidgetId, ComponentName provider) {
402 mContext.enforceCallingPermission(android.Manifest.permission.BIND_APPWIDGET,
403 "bindGagetId appWidgetId=" + appWidgetId + " provider=" + provider);
Dianne Hackborn4912f162011-06-15 17:40:44 -0700404
405 final long ident = Binder.clearCallingIdentity();
406 try {
407 synchronized (mAppWidgetIds) {
408 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
409 if (id == null) {
410 throw new IllegalArgumentException("bad appWidgetId");
411 }
412 if (id.provider != null) {
413 throw new IllegalArgumentException("appWidgetId " + appWidgetId + " already bound to "
414 + id.provider.info.provider);
415 }
416 Provider p = lookupProviderLocked(provider);
417 if (p == null) {
418 throw new IllegalArgumentException("not a appwidget provider: " + provider);
419 }
420 if (p.zombie) {
421 throw new IllegalArgumentException("can't bind to a 3rd party provider in"
422 + " safe mode: " + provider);
423 }
424
425 id.provider = p;
426 p.instances.add(id);
427 int instancesSize = p.instances.size();
428 if (instancesSize == 1) {
429 // tell the provider that it's ready
430 sendEnableIntentLocked(p);
431 }
432
433 // send an update now -- We need this update now, and just for this appWidgetId.
434 // It's less critical when the next one happens, so when we schdule the next one,
435 // we add updatePeriodMillis to its start time. That time will have some slop,
436 // but that's okay.
437 sendUpdateIntentLocked(p, new int[] { appWidgetId });
438
439 // schedule the future updates
440 registerForBroadcastsLocked(p, getAppWidgetIds(p));
441 saveStateLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
Dianne Hackborn4912f162011-06-15 17:40:44 -0700443 } finally {
444 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 }
446 }
447
Winson Chung84bbb022011-02-21 13:57:45 -0800448 // Binds to a specific RemoteViewsService
Winson Chung81f39eb2011-01-11 18:05:01 -0800449 public void bindRemoteViewsService(int appWidgetId, Intent intent, IBinder connection) {
450 synchronized (mAppWidgetIds) {
451 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
452 if (id == null) {
453 throw new IllegalArgumentException("bad appWidgetId");
454 }
455 final ComponentName componentName = intent.getComponent();
456 try {
457 final ServiceInfo si = mContext.getPackageManager().getServiceInfo(componentName,
458 PackageManager.GET_PERMISSIONS);
459 if (!android.Manifest.permission.BIND_REMOTEVIEWS.equals(si.permission)) {
460 throw new SecurityException("Selected service does not require "
461 + android.Manifest.permission.BIND_REMOTEVIEWS
462 + ": " + componentName);
463 }
464 } catch (PackageManager.NameNotFoundException e) {
465 throw new IllegalArgumentException("Unknown component " + componentName);
466 }
467
Winson Chung16c8d8a2011-01-20 16:19:33 -0800468 // If there is already a connection made for this service intent, then disconnect from
469 // that first. (This does not allow multiple connections to the same service under
470 // the same key)
471 ServiceConnectionProxy conn = null;
Winson Chung84bbb022011-02-21 13:57:45 -0800472 FilterComparison fc = new FilterComparison(intent);
473 Pair<Integer, FilterComparison> key = Pair.create(appWidgetId, fc);
Winson Chung16c8d8a2011-01-20 16:19:33 -0800474 if (mBoundRemoteViewsServices.containsKey(key)) {
475 conn = (ServiceConnectionProxy) mBoundRemoteViewsServices.get(key);
476 conn.disconnect();
477 mContext.unbindService(conn);
478 mBoundRemoteViewsServices.remove(key);
479 }
480
481 // Bind to the RemoteViewsService (which will trigger a callback to the
482 // RemoteViewsAdapter.onServiceConnected())
Winson Chung81f39eb2011-01-11 18:05:01 -0800483 final long token = Binder.clearCallingIdentity();
484 try {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800485 conn = new ServiceConnectionProxy(key, connection);
Winson Chung81f39eb2011-01-11 18:05:01 -0800486 mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE);
487 mBoundRemoteViewsServices.put(key, conn);
488 } finally {
489 Binder.restoreCallingIdentity(token);
490 }
Winson Chung84bbb022011-02-21 13:57:45 -0800491
492 // Add it to the mapping of RemoteViewsService to appWidgetIds so that we can determine
493 // when we can call back to the RemoteViewsService later to destroy associated
494 // factories.
Winson Chung22bc69d2011-02-25 15:13:38 -0800495 incrementAppWidgetServiceRefCount(appWidgetId, fc);
Winson Chung81f39eb2011-01-11 18:05:01 -0800496 }
497 }
498
Winson Chung84bbb022011-02-21 13:57:45 -0800499 // Unbinds from a specific RemoteViewsService
Winson Chung81f39eb2011-01-11 18:05:01 -0800500 public void unbindRemoteViewsService(int appWidgetId, Intent intent) {
501 synchronized (mAppWidgetIds) {
Winson Chung81f39eb2011-01-11 18:05:01 -0800502 // Unbind from the RemoteViewsService (which will trigger a callback to the bound
503 // RemoteViewsAdapter)
504 Pair<Integer, FilterComparison> key = Pair.create(appWidgetId,
505 new FilterComparison(intent));
506 if (mBoundRemoteViewsServices.containsKey(key)) {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800507 // We don't need to use the appWidgetId until after we are sure there is something
508 // to unbind. Note that this may mask certain issues with apps calling unbind()
509 // more than necessary.
510 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
511 if (id == null) {
512 throw new IllegalArgumentException("bad appWidgetId");
513 }
514
515 ServiceConnectionProxy conn =
516 (ServiceConnectionProxy) mBoundRemoteViewsServices.get(key);
517 conn.disconnect();
Winson Chung81f39eb2011-01-11 18:05:01 -0800518 mContext.unbindService(conn);
Winson Chung16c8d8a2011-01-20 16:19:33 -0800519 mBoundRemoteViewsServices.remove(key);
520 } else {
521 Log.e("AppWidgetService", "Error (unbindRemoteViewsService): Connection not bound");
Winson Chung81f39eb2011-01-11 18:05:01 -0800522 }
523 }
524 }
525
Winson Chung84bbb022011-02-21 13:57:45 -0800526 // Unbinds from a RemoteViewsService when we delete an app widget
Winson Chung81f39eb2011-01-11 18:05:01 -0800527 private void unbindAppWidgetRemoteViewsServicesLocked(AppWidgetId id) {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800528 int appWidgetId = id.appWidgetId;
529 // Unbind all connections to Services bound to this AppWidgetId
Winson Chung81f39eb2011-01-11 18:05:01 -0800530 Iterator<Pair<Integer, Intent.FilterComparison>> it =
531 mBoundRemoteViewsServices.keySet().iterator();
Winson Chung81f39eb2011-01-11 18:05:01 -0800532 while (it.hasNext()) {
533 final Pair<Integer, Intent.FilterComparison> key = it.next();
534 if (key.first.intValue() == appWidgetId) {
Winson Chung16c8d8a2011-01-20 16:19:33 -0800535 final ServiceConnectionProxy conn = (ServiceConnectionProxy)
536 mBoundRemoteViewsServices.get(key);
537 conn.disconnect();
Winson Chung81f39eb2011-01-11 18:05:01 -0800538 mContext.unbindService(conn);
Winson Chung16c8d8a2011-01-20 16:19:33 -0800539 it.remove();
Winson Chung81f39eb2011-01-11 18:05:01 -0800540 }
541 }
Winson Chung84bbb022011-02-21 13:57:45 -0800542
543 // Check if we need to destroy any services (if no other app widgets are
544 // referencing the same service)
545 decrementAppWidgetServiceRefCount(appWidgetId);
546 }
547
548 // Destroys the cached factory on the RemoteViewsService's side related to the specified intent
549 private void destroyRemoteViewsService(final Intent intent) {
550 final ServiceConnection conn = new ServiceConnection() {
551 @Override
552 public void onServiceConnected(ComponentName name, IBinder service) {
553 final IRemoteViewsFactory cb =
554 IRemoteViewsFactory.Stub.asInterface(service);
555 try {
556 cb.onDestroy(intent);
Adam Cohen2625fea2011-03-23 17:24:30 -0700557 } catch (RemoteException e) {
558 e.printStackTrace();
559 } catch (RuntimeException e) {
Winson Chung84bbb022011-02-21 13:57:45 -0800560 e.printStackTrace();
561 }
562 mContext.unbindService(this);
563 }
564 @Override
565 public void onServiceDisconnected(android.content.ComponentName name) {
566 // Do nothing
567 }
568 };
569
570 // Bind to the service and remove the static intent->factory mapping in the
571 // RemoteViewsService.
572 final long token = Binder.clearCallingIdentity();
573 try {
574 mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE);
575 } finally {
576 Binder.restoreCallingIdentity(token);
577 }
578 }
579
580 // Adds to the ref-count for a given RemoteViewsService intent
581 private void incrementAppWidgetServiceRefCount(int appWidgetId, FilterComparison fc) {
582 HashSet<Integer> appWidgetIds = null;
583 if (mRemoteViewsServicesAppWidgets.containsKey(fc)) {
584 appWidgetIds = mRemoteViewsServicesAppWidgets.get(fc);
585 } else {
586 appWidgetIds = new HashSet<Integer>();
587 mRemoteViewsServicesAppWidgets.put(fc, appWidgetIds);
588 }
589 appWidgetIds.add(appWidgetId);
590 }
591
592 // Subtracts from the ref-count for a given RemoteViewsService intent, prompting a delete if
593 // the ref-count reaches zero.
594 private void decrementAppWidgetServiceRefCount(int appWidgetId) {
595 Iterator<FilterComparison> it =
596 mRemoteViewsServicesAppWidgets.keySet().iterator();
597 while (it.hasNext()) {
598 final FilterComparison key = it.next();
599 final HashSet<Integer> ids = mRemoteViewsServicesAppWidgets.get(key);
600 if (ids.remove(appWidgetId)) {
601 // If we have removed the last app widget referencing this service, then we
602 // should destroy it and remove it from this set
603 if (ids.isEmpty()) {
604 destroyRemoteViewsService(key.getIntent());
605 it.remove();
606 }
607 }
608 }
Winson Chung81f39eb2011-01-11 18:05:01 -0800609 }
610
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700611 public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId) {
612 synchronized (mAppWidgetIds) {
613 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 if (id != null && id.provider != null && !id.provider.zombie) {
615 return id.provider.info;
616 }
617 return null;
618 }
619 }
620
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700621 public RemoteViews getAppWidgetViews(int appWidgetId) {
622 synchronized (mAppWidgetIds) {
623 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 if (id != null) {
625 return id.views;
626 }
627 return null;
628 }
629 }
630
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700631 public List<AppWidgetProviderInfo> getInstalledProviders() {
632 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 final int N = mInstalledProviders.size();
Romain Guya5475592009-07-01 17:20:08 -0700634 ArrayList<AppWidgetProviderInfo> result = new ArrayList<AppWidgetProviderInfo>(N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 for (int i=0; i<N; i++) {
636 Provider p = mInstalledProviders.get(i);
637 if (!p.zombie) {
638 result.add(p.info);
639 }
640 }
641 return result;
642 }
643 }
644
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700645 public void updateAppWidgetIds(int[] appWidgetIds, RemoteViews views) {
646 if (appWidgetIds == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 return;
648 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700649 if (appWidgetIds.length == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 return;
651 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700652 final int N = appWidgetIds.length;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700654 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700656 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetIds[i]);
657 updateAppWidgetInstanceLocked(id, views);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
659 }
660 }
661
Adam Cohen2dd21972010-08-15 18:20:04 -0700662 public void partiallyUpdateAppWidgetIds(int[] appWidgetIds, RemoteViews views) {
663 if (appWidgetIds == null) {
664 return;
665 }
666 if (appWidgetIds.length == 0) {
667 return;
668 }
669 final int N = appWidgetIds.length;
670
671 synchronized (mAppWidgetIds) {
672 for (int i=0; i<N; i++) {
673 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetIds[i]);
674 updateAppWidgetInstanceLocked(id, views, true);
675 }
676 }
677 }
678
Winson Chung6394c0e2010-08-16 10:14:56 -0700679 public void notifyAppWidgetViewDataChanged(int[] appWidgetIds, int viewId) {
Winson Chung499cb9f2010-07-16 11:18:17 -0700680 if (appWidgetIds == null) {
681 return;
682 }
683 if (appWidgetIds.length == 0) {
684 return;
685 }
686 final int N = appWidgetIds.length;
687
688 synchronized (mAppWidgetIds) {
689 for (int i=0; i<N; i++) {
690 AppWidgetId id = lookupAppWidgetIdLocked(appWidgetIds[i]);
Winson Chung6394c0e2010-08-16 10:14:56 -0700691 notifyAppWidgetViewDataChangedInstanceLocked(id, viewId);
Winson Chung499cb9f2010-07-16 11:18:17 -0700692 }
693 }
694 }
695
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700696 public void updateAppWidgetProvider(ComponentName provider, RemoteViews views) {
697 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 Provider p = lookupProviderLocked(provider);
699 if (p == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800700 Slog.w(TAG, "updateAppWidgetProvider: provider doesn't exist: " + provider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 return;
702 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700703 ArrayList<AppWidgetId> instances = p.instances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 final int N = instances.size();
705 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700706 AppWidgetId id = instances.get(i);
707 updateAppWidgetInstanceLocked(id, views);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 }
709 }
710 }
711
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700712 void updateAppWidgetInstanceLocked(AppWidgetId id, RemoteViews views) {
Adam Cohen2dd21972010-08-15 18:20:04 -0700713 updateAppWidgetInstanceLocked(id, views, false);
714 }
715
716 void updateAppWidgetInstanceLocked(AppWidgetId id, RemoteViews views, boolean isPartialUpdate) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700717 // allow for stale appWidgetIds and other badness
718 // lookup also checks that the calling process can access the appWidgetId
719 // drop unbound appWidgetIds (shouldn't be possible under normal circumstances)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 if (id != null && id.provider != null && !id.provider.zombie && !id.host.zombie) {
Adam Cohen2dd21972010-08-15 18:20:04 -0700721
722 // We do not want to save this RemoteViews
723 if (!isPartialUpdate) id.views = views;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724
725 // is anyone listening?
726 if (id.host.callbacks != null) {
727 try {
728 // the lock is held, but this is a oneway call
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700729 id.host.callbacks.updateAppWidget(id.appWidgetId, views);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 } catch (RemoteException e) {
731 // It failed; remove the callback. No need to prune because
732 // we know that this host is still referenced by this instance.
733 id.host.callbacks = null;
734 }
735 }
736 }
737 }
738
Winson Chung6394c0e2010-08-16 10:14:56 -0700739 void notifyAppWidgetViewDataChangedInstanceLocked(AppWidgetId id, int viewId) {
Winson Chung499cb9f2010-07-16 11:18:17 -0700740 // allow for stale appWidgetIds and other badness
741 // lookup also checks that the calling process can access the appWidgetId
742 // drop unbound appWidgetIds (shouldn't be possible under normal circumstances)
743 if (id != null && id.provider != null && !id.provider.zombie && !id.host.zombie) {
Winson Chung499cb9f2010-07-16 11:18:17 -0700744 // is anyone listening?
745 if (id.host.callbacks != null) {
746 try {
747 // the lock is held, but this is a oneway call
Winson Chung6394c0e2010-08-16 10:14:56 -0700748 id.host.callbacks.viewDataChanged(id.appWidgetId, viewId);
Winson Chung499cb9f2010-07-16 11:18:17 -0700749 } catch (RemoteException e) {
750 // It failed; remove the callback. No need to prune because
751 // we know that this host is still referenced by this instance.
752 id.host.callbacks = null;
753 }
754 }
755 }
756 }
757
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700758 public int[] startListening(IAppWidgetHost callbacks, String packageName, int hostId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 List<RemoteViews> updatedViews) {
760 int callingUid = enforceCallingUid(packageName);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700761 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 Host host = lookupOrAddHostLocked(callingUid, packageName, hostId);
763 host.callbacks = callbacks;
764
765 updatedViews.clear();
766
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700767 ArrayList<AppWidgetId> instances = host.instances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 int N = instances.size();
769 int[] updatedIds = new int[N];
770 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700771 AppWidgetId id = instances.get(i);
772 updatedIds[i] = id.appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 updatedViews.add(id.views);
774 }
775 return updatedIds;
776 }
777 }
778
779 public void stopListening(int hostId) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700780 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 Host host = lookupHostLocked(getCallingUid(), hostId);
Ken Shirriffe21167a2009-09-23 16:42:53 -0700782 if (host != null) {
783 host.callbacks = null;
784 pruneHostLocked(host);
785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 }
787 }
788
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700789 boolean canAccessAppWidgetId(AppWidgetId id, int callingUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 if (id.host.uid == callingUid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700791 // Apps hosting the AppWidget have access to it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 return true;
793 }
794 if (id.provider != null && id.provider.uid == callingUid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700795 // Apps providing the AppWidget have access to it (if the appWidgetId has been bound)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 return true;
797 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700798 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.BIND_APPWIDGET)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 == PackageManager.PERMISSION_GRANTED) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700800 // Apps that can bind have access to all appWidgetIds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 return true;
802 }
803 // Nobody else can access it.
804 return false;
805 }
806
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700807 AppWidgetId lookupAppWidgetIdLocked(int appWidgetId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 int callingUid = getCallingUid();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700809 final int N = mAppWidgetIds.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700811 AppWidgetId id = mAppWidgetIds.get(i);
812 if (id.appWidgetId == appWidgetId && canAccessAppWidgetId(id, callingUid)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 return id;
814 }
815 }
816 return null;
817 }
818
819 Provider lookupProviderLocked(ComponentName provider) {
820 final int N = mInstalledProviders.size();
821 for (int i=0; i<N; i++) {
822 Provider p = mInstalledProviders.get(i);
Adam Cohenbac26a122011-08-16 20:42:30 -0700823 if (p.info.provider.equals(provider)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 return p;
825 }
826 }
827 return null;
828 }
829
830 Host lookupHostLocked(int uid, int hostId) {
831 final int N = mHosts.size();
832 for (int i=0; i<N; i++) {
833 Host h = mHosts.get(i);
834 if (h.uid == uid && h.hostId == hostId) {
835 return h;
836 }
837 }
838 return null;
839 }
840
841 Host lookupOrAddHostLocked(int uid, String packageName, int hostId) {
842 final int N = mHosts.size();
843 for (int i=0; i<N; i++) {
844 Host h = mHosts.get(i);
845 if (h.hostId == hostId && h.packageName.equals(packageName)) {
846 return h;
847 }
848 }
849 Host host = new Host();
850 host.packageName = packageName;
851 host.uid = uid;
852 host.hostId = hostId;
853 mHosts.add(host);
854 return host;
855 }
856
857 void pruneHostLocked(Host host) {
858 if (host.instances.size() == 0 && host.callbacks == null) {
859 mHosts.remove(host);
860 }
861 }
862
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700863 void loadAppWidgetList() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 PackageManager pm = mPackageManager;
865
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700866 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 List<ResolveInfo> broadcastReceivers = pm.queryBroadcastReceivers(intent,
868 PackageManager.GET_META_DATA);
869
Bjorn Bringert5f857802010-02-10 23:09:48 +0000870 final int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 for (int i=0; i<N; i++) {
872 ResolveInfo ri = broadcastReceivers.get(i);
873 addProviderLocked(ri);
874 }
875 }
876
877 boolean addProviderLocked(ResolveInfo ri) {
Joe Onoratod070e892011-01-07 20:50:37 -0800878 if ((ri.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
879 return false;
880 }
881 if (!ri.activityInfo.isEnabled()) {
882 return false;
883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 Provider p = parseProviderInfoXml(new ComponentName(ri.activityInfo.packageName,
885 ri.activityInfo.name), ri);
886 if (p != null) {
887 mInstalledProviders.add(p);
888 return true;
889 } else {
890 return false;
891 }
892 }
893
894 void removeProviderLocked(int index, Provider p) {
895 int N = p.instances.size();
896 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700897 AppWidgetId id = p.instances.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 // Call back with empty RemoteViews
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700899 updateAppWidgetInstanceLocked(id, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 // Stop telling the host about updates for this from now on
901 cancelBroadcasts(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700902 // clear out references to this appWidgetId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 id.host.instances.remove(id);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700904 mAppWidgetIds.remove(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 id.provider = null;
906 pruneHostLocked(id.host);
907 id.host = null;
908 }
909 p.instances.clear();
910 mInstalledProviders.remove(index);
911 // no need to send the DISABLE broadcast, since the receiver is gone anyway
912 cancelBroadcasts(p);
913 }
914
915 void sendEnableIntentLocked(Provider p) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700916 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_ENABLED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 intent.setComponent(p.info.provider);
918 mContext.sendBroadcast(intent);
919 }
920
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700921 void sendUpdateIntentLocked(Provider p, int[] appWidgetIds) {
922 if (appWidgetIds != null && appWidgetIds.length > 0) {
923 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
924 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 intent.setComponent(p.info.provider);
926 mContext.sendBroadcast(intent);
927 }
928 }
929
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700930 void registerForBroadcastsLocked(Provider p, int[] appWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 if (p.info.updatePeriodMillis > 0) {
932 // if this is the first instance, set the alarm. otherwise,
933 // rely on the fact that we've already set it and that
934 // PendingIntent.getBroadcast will update the extras.
935 boolean alreadyRegistered = p.broadcast != null;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700936 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
937 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 intent.setComponent(p.info.provider);
939 long token = Binder.clearCallingIdentity();
940 try {
941 p.broadcast = PendingIntent.getBroadcast(mContext, 1, intent,
942 PendingIntent.FLAG_UPDATE_CURRENT);
943 } finally {
944 Binder.restoreCallingIdentity(token);
945 }
946 if (!alreadyRegistered) {
Joe Onoratobe96b3a2009-07-14 19:49:27 -0700947 long period = p.info.updatePeriodMillis;
948 if (period < MIN_UPDATE_PERIOD) {
949 period = MIN_UPDATE_PERIOD;
950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
Joe Onoratobe96b3a2009-07-14 19:49:27 -0700952 SystemClock.elapsedRealtime() + period, period, p.broadcast);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
954 }
955 }
956
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700957 static int[] getAppWidgetIds(Provider p) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 int instancesSize = p.instances.size();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700959 int appWidgetIds[] = new int[instancesSize];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 for (int i=0; i<instancesSize; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700961 appWidgetIds[i] = p.instances.get(i).appWidgetId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700963 return appWidgetIds;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
965
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700966 public int[] getAppWidgetIds(ComponentName provider) {
967 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 Provider p = lookupProviderLocked(provider);
969 if (p != null && getCallingUid() == p.uid) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700970 return getAppWidgetIds(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 } else {
972 return new int[0];
973 }
974 }
975 }
976
977 private Provider parseProviderInfoXml(ComponentName component, ResolveInfo ri) {
978 Provider p = null;
979
980 ActivityInfo activityInfo = ri.activityInfo;
981 XmlResourceParser parser = null;
982 try {
983 parser = activityInfo.loadXmlMetaData(mPackageManager,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700984 AppWidgetManager.META_DATA_APPWIDGET_PROVIDER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 if (parser == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800986 Slog.w(TAG, "No " + AppWidgetManager.META_DATA_APPWIDGET_PROVIDER + " meta-data for "
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700987 + "AppWidget provider '" + component + '\'');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 return null;
989 }
Adam Cohend2e20de2011-02-25 12:03:37 -0800990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 AttributeSet attrs = Xml.asAttributeSet(parser);
Adam Cohend2e20de2011-02-25 12:03:37 -0800992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 int type;
994 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
995 && type != XmlPullParser.START_TAG) {
996 // drain whitespace, comments, etc.
997 }
Adam Cohend2e20de2011-02-25 12:03:37 -0800998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 String nodeName = parser.getName();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001000 if (!"appwidget-provider".equals(nodeName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001001 Slog.w(TAG, "Meta-data does not start with appwidget-provider tag for"
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001002 + " AppWidget provider '" + component + '\'');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 return null;
1004 }
1005
1006 p = new Provider();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001007 AppWidgetProviderInfo info = p.info = new AppWidgetProviderInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 info.provider = component;
1009 p.uid = activityInfo.applicationInfo.uid;
1010
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001011 Resources res = mPackageManager.getResourcesForApplication(
1012 activityInfo.applicationInfo);
Adam Cohend2e20de2011-02-25 12:03:37 -08001013
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001014 TypedArray sa = res.obtainAttributes(attrs,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001015 com.android.internal.R.styleable.AppWidgetProviderInfo);
Adam Cohend2e20de2011-02-25 12:03:37 -08001016
Mitsuru Oshima8f25c422009-07-01 00:10:43 -07001017 // These dimensions has to be resolved in the application's context.
1018 // We simply send back the raw complex data, which will be
1019 // converted to dp in {@link AppWidgetManager#getAppWidgetInfo}.
1020 TypedValue value = sa.peekValue(
1021 com.android.internal.R.styleable.AppWidgetProviderInfo_minWidth);
1022 info.minWidth = value != null ? value.data : 0;
1023 value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minHeight);
1024 info.minHeight = value != null ? value.data : 0;
Adam Cohen1bfaf562011-07-19 18:05:33 -07001025 value = sa.peekValue(
1026 com.android.internal.R.styleable.AppWidgetProviderInfo_minResizeWidth);
1027 info.minResizeWidth = value != null ? value.data : info.minWidth;
1028 value = sa.peekValue(
1029 com.android.internal.R.styleable.AppWidgetProviderInfo_minResizeHeight);
1030 info.minResizeHeight = value != null ? value.data : info.minHeight;
Adam Cohend2e20de2011-02-25 12:03:37 -08001031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 info.updatePeriodMillis = sa.getInt(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001033 com.android.internal.R.styleable.AppWidgetProviderInfo_updatePeriodMillis, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 info.initialLayout = sa.getResourceId(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001035 com.android.internal.R.styleable.AppWidgetProviderInfo_initialLayout, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 String className = sa.getString(
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001037 com.android.internal.R.styleable.AppWidgetProviderInfo_configure);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 if (className != null) {
1039 info.configure = new ComponentName(component.getPackageName(), className);
1040 }
1041 info.label = activityInfo.loadLabel(mPackageManager).toString();
1042 info.icon = ri.getIconResource();
Patrick Dubroyd2db2a52010-06-23 14:56:28 -07001043 info.previewImage = sa.getResourceId(
1044 com.android.internal.R.styleable.AppWidgetProviderInfo_previewImage, 0);
Adam Cohena02fdf12010-11-03 13:27:40 -07001045 info.autoAdvanceViewId = sa.getResourceId(
1046 com.android.internal.R.styleable.AppWidgetProviderInfo_autoAdvanceViewId, -1);
Adam Cohen9611f2e2011-02-28 13:39:38 -08001047 info.resizeMode = sa.getInt(
Adam Cohend2e20de2011-02-25 12:03:37 -08001048 com.android.internal.R.styleable.AppWidgetProviderInfo_resizeMode,
1049 AppWidgetProviderInfo.RESIZE_NONE);
Patrick Dubroyd2db2a52010-06-23 14:56:28 -07001050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 sa.recycle();
1052 } catch (Exception e) {
1053 // Ok to catch Exception here, because anything going wrong because
1054 // of what a client process passes to us should not be fatal for the
1055 // system process.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001056 Slog.w(TAG, "XML parsing failed for AppWidget provider '" + component + '\'', e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 return null;
1058 } finally {
1059 if (parser != null) parser.close();
1060 }
1061 return p;
1062 }
1063
1064 int getUidForPackage(String packageName) throws PackageManager.NameNotFoundException {
1065 PackageInfo pkgInfo = mPackageManager.getPackageInfo(packageName, 0);
1066 if (pkgInfo == null || pkgInfo.applicationInfo == null) {
1067 throw new PackageManager.NameNotFoundException();
1068 }
1069 return pkgInfo.applicationInfo.uid;
1070 }
1071
1072 int enforceCallingUid(String packageName) throws IllegalArgumentException {
1073 int callingUid = getCallingUid();
1074 int packageUid;
1075 try {
1076 packageUid = getUidForPackage(packageName);
1077 } catch (PackageManager.NameNotFoundException ex) {
1078 throw new IllegalArgumentException("packageName and uid don't match packageName="
1079 + packageName);
1080 }
Jeff Brown10e89712011-07-08 18:52:57 -07001081 if (callingUid != packageUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 throw new IllegalArgumentException("packageName and uid don't match packageName="
1083 + packageName);
1084 }
1085 return callingUid;
1086 }
1087
1088 void sendInitialBroadcasts() {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001089 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 final int N = mInstalledProviders.size();
1091 for (int i=0; i<N; i++) {
1092 Provider p = mInstalledProviders.get(i);
1093 if (p.instances.size() > 0) {
1094 sendEnableIntentLocked(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001095 int[] appWidgetIds = getAppWidgetIds(p);
1096 sendUpdateIntentLocked(p, appWidgetIds);
1097 registerForBroadcastsLocked(p, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
1099 }
1100 }
1101 }
1102
1103 // only call from initialization -- it assumes that the data structures are all empty
1104 void loadStateLocked() {
1105 File temp = savedStateTempFile();
1106 File real = savedStateRealFile();
1107
1108 // prefer the real file. If it doesn't exist, use the temp one, and then copy it to the
1109 // real one. if there is both a real file and a temp one, assume that the temp one isn't
1110 // fully written and delete it.
1111 if (real.exists()) {
1112 readStateFromFileLocked(real);
1113 if (temp.exists()) {
Romain Guya5475592009-07-01 17:20:08 -07001114 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 temp.delete();
1116 }
1117 } else if (temp.exists()) {
1118 readStateFromFileLocked(temp);
Romain Guya5475592009-07-01 17:20:08 -07001119 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 temp.renameTo(real);
1121 }
1122 }
1123
1124 void saveStateLocked() {
1125 File temp = savedStateTempFile();
1126 File real = savedStateRealFile();
1127
1128 if (!real.exists()) {
1129 // If the real one doesn't exist, it's either because this is the first time
1130 // or because something went wrong while copying them. In this case, we can't
1131 // trust anything that's in temp. In order to have the loadState code not
1132 // use the temporary one until it's fully written, create an empty file
1133 // for real, which will we'll shortly delete.
1134 try {
Romain Guya5475592009-07-01 17:20:08 -07001135 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 real.createNewFile();
1137 } catch (IOException e) {
Romain Guya5475592009-07-01 17:20:08 -07001138 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 }
1140 }
1141
1142 if (temp.exists()) {
Romain Guya5475592009-07-01 17:20:08 -07001143 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 temp.delete();
1145 }
1146
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07001147 if (!writeStateToFileLocked(temp)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001148 Slog.w(TAG, "Failed to persist new settings");
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07001149 return;
1150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151
Romain Guya5475592009-07-01 17:20:08 -07001152 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 real.delete();
Romain Guya5475592009-07-01 17:20:08 -07001154 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 temp.renameTo(real);
1156 }
1157
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07001158 boolean writeStateToFileLocked(File file) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 FileOutputStream stream = null;
1160 int N;
1161
1162 try {
1163 stream = new FileOutputStream(file, false);
1164 XmlSerializer out = new FastXmlSerializer();
1165 out.setOutput(stream, "utf-8");
1166 out.startDocument(null, true);
1167
1168
1169 out.startTag(null, "gs");
1170
1171 int providerIndex = 0;
1172 N = mInstalledProviders.size();
1173 for (int i=0; i<N; i++) {
1174 Provider p = mInstalledProviders.get(i);
1175 if (p.instances.size() > 0) {
1176 out.startTag(null, "p");
1177 out.attribute(null, "pkg", p.info.provider.getPackageName());
1178 out.attribute(null, "cl", p.info.provider.getClassName());
Patrick Tsaibd742e432010-05-01 00:30:19 +08001179 out.endTag(null, "p");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 p.tag = providerIndex;
1181 providerIndex++;
1182 }
1183 }
1184
1185 N = mHosts.size();
1186 for (int i=0; i<N; i++) {
1187 Host host = mHosts.get(i);
1188 out.startTag(null, "h");
1189 out.attribute(null, "pkg", host.packageName);
1190 out.attribute(null, "id", Integer.toHexString(host.hostId));
1191 out.endTag(null, "h");
1192 host.tag = i;
1193 }
1194
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001195 N = mAppWidgetIds.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 for (int i=0; i<N; i++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001197 AppWidgetId id = mAppWidgetIds.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 out.startTag(null, "g");
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001199 out.attribute(null, "id", Integer.toHexString(id.appWidgetId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 out.attribute(null, "h", Integer.toHexString(id.host.tag));
1201 if (id.provider != null) {
1202 out.attribute(null, "p", Integer.toHexString(id.provider.tag));
1203 }
1204 out.endTag(null, "g");
1205 }
1206
1207 out.endTag(null, "gs");
1208
1209 out.endDocument();
1210 stream.close();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07001211 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 } catch (IOException e) {
1213 try {
1214 if (stream != null) {
1215 stream.close();
1216 }
1217 } catch (IOException ex) {
Romain Guya5475592009-07-01 17:20:08 -07001218 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
1220 if (file.exists()) {
Romain Guya5475592009-07-01 17:20:08 -07001221 //noinspection ResultOfMethodCallIgnored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 file.delete();
1223 }
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07001224 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 }
1226 }
1227
1228 void readStateFromFileLocked(File file) {
1229 FileInputStream stream = null;
1230
1231 boolean success = false;
1232
1233 try {
1234 stream = new FileInputStream(file);
1235 XmlPullParser parser = Xml.newPullParser();
1236 parser.setInput(stream, null);
1237
1238 int type;
1239 int providerIndex = 0;
Romain Guya5475592009-07-01 17:20:08 -07001240 HashMap<Integer,Provider> loadedProviders = new HashMap<Integer, Provider>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 do {
1242 type = parser.next();
1243 if (type == XmlPullParser.START_TAG) {
1244 String tag = parser.getName();
1245 if ("p".equals(tag)) {
1246 // TODO: do we need to check that this package has the same signature
1247 // as before?
1248 String pkg = parser.getAttributeValue(null, "pkg");
1249 String cl = parser.getAttributeValue(null, "cl");
Romain Guyff3e61c62010-03-11 15:30:02 -08001250
1251 final PackageManager packageManager = mContext.getPackageManager();
1252 try {
1253 packageManager.getReceiverInfo(new ComponentName(pkg, cl), 0);
1254 } catch (PackageManager.NameNotFoundException e) {
1255 String[] pkgs = packageManager.currentToCanonicalPackageNames(
1256 new String[] { pkg });
1257 pkg = pkgs[0];
1258 }
1259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 Provider p = lookupProviderLocked(new ComponentName(pkg, cl));
1261 if (p == null && mSafeMode) {
1262 // if we're in safe mode, make a temporary one
1263 p = new Provider();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001264 p.info = new AppWidgetProviderInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 p.info.provider = new ComponentName(pkg, cl);
1266 p.zombie = true;
1267 mInstalledProviders.add(p);
1268 }
1269 if (p != null) {
1270 // if it wasn't uninstalled or something
1271 loadedProviders.put(providerIndex, p);
1272 }
1273 providerIndex++;
1274 }
1275 else if ("h".equals(tag)) {
1276 Host host = new Host();
1277
1278 // TODO: do we need to check that this package has the same signature
1279 // as before?
1280 host.packageName = parser.getAttributeValue(null, "pkg");
1281 try {
1282 host.uid = getUidForPackage(host.packageName);
1283 } catch (PackageManager.NameNotFoundException ex) {
1284 host.zombie = true;
1285 }
1286 if (!host.zombie || mSafeMode) {
1287 // In safe mode, we don't discard the hosts we don't recognize
1288 // so that they're not pruned from our list. Otherwise, we do.
1289 host.hostId = Integer.parseInt(
1290 parser.getAttributeValue(null, "id"), 16);
1291 mHosts.add(host);
1292 }
1293 }
1294 else if ("g".equals(tag)) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001295 AppWidgetId id = new AppWidgetId();
1296 id.appWidgetId = Integer.parseInt(parser.getAttributeValue(null, "id"), 16);
1297 if (id.appWidgetId >= mNextAppWidgetId) {
1298 mNextAppWidgetId = id.appWidgetId + 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 }
1300
1301 String providerString = parser.getAttributeValue(null, "p");
1302 if (providerString != null) {
1303 // there's no provider if it hasn't been bound yet.
1304 // maybe we don't have to save this, but it brings the system
1305 // to the state it was in.
1306 int pIndex = Integer.parseInt(providerString, 16);
1307 id.provider = loadedProviders.get(pIndex);
1308 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001309 Slog.d(TAG, "bound appWidgetId=" + id.appWidgetId + " to provider "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 + pIndex + " which is " + id.provider);
1311 }
1312 if (id.provider == null) {
1313 // This provider is gone. We just let the host figure out
1314 // that this happened when it fails to load it.
1315 continue;
1316 }
1317 }
1318
1319 int hIndex = Integer.parseInt(parser.getAttributeValue(null, "h"), 16);
1320 id.host = mHosts.get(hIndex);
1321 if (id.host == null) {
1322 // This host is gone.
1323 continue;
1324 }
1325
1326 if (id.provider != null) {
1327 id.provider.instances.add(id);
1328 }
1329 id.host.instances.add(id);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001330 mAppWidgetIds.add(id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
1332 }
1333 } while (type != XmlPullParser.END_DOCUMENT);
1334 success = true;
1335 } catch (NullPointerException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001336 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001338 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001340 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001342 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 } catch (IndexOutOfBoundsException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001344 Slog.w(TAG, "failed parsing " + file, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 }
1346 try {
1347 if (stream != null) {
1348 stream.close();
1349 }
1350 } catch (IOException e) {
Romain Guya5475592009-07-01 17:20:08 -07001351 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353
1354 if (success) {
1355 // delete any hosts that didn't manage to get connected (should happen)
1356 // if it matters, they'll be reconnected.
Dianne Hackborn002716d2009-08-12 11:13:26 -07001357 for (int i=mHosts.size()-1; i>=0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 pruneHostLocked(mHosts.get(i));
1359 }
1360 } else {
1361 // failed reading, clean up
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001362 mAppWidgetIds.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 mHosts.clear();
1364 final int N = mInstalledProviders.size();
1365 for (int i=0; i<N; i++) {
1366 mInstalledProviders.get(i).instances.clear();
1367 }
1368 }
1369 }
1370
1371 File savedStateTempFile() {
1372 return new File("/data/system/" + SETTINGS_TMP_FILENAME);
1373 //return new File(mContext.getFilesDir(), SETTINGS_FILENAME);
1374 }
1375
1376 File savedStateRealFile() {
1377 return new File("/data/system/" + SETTINGS_FILENAME);
1378 //return new File(mContext.getFilesDir(), SETTINGS_TMP_FILENAME);
1379 }
1380
1381 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1382 public void onReceive(Context context, Intent intent) {
1383 String action = intent.getAction();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001384 //Slog.d(TAG, "received " + action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
1386 sendInitialBroadcasts();
Eric Fischer63c2d9e2009-10-22 15:22:50 -07001387 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1388 Locale revised = Locale.getDefault();
1389 if (revised == null || mLocale == null ||
1390 !(revised.equals(mLocale))) {
1391 mLocale = revised;
1392
1393 synchronized (mAppWidgetIds) {
1394 int N = mInstalledProviders.size();
1395 for (int i=N-1; i>=0; i--) {
1396 Provider p = mInstalledProviders.get(i);
1397 String pkgName = p.info.provider.getPackageName();
1398 updateProvidersForPackageLocked(pkgName);
1399 }
1400 saveStateLocked();
1401 }
1402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 } else {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001404 boolean added = false;
Joe Onoratod070e892011-01-07 20:50:37 -08001405 boolean changed = false;
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001406 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001407 if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001408 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1409 added = true;
Joe Onorato94258cd2010-08-24 16:45:40 -04001410 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001411 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1412 added = false;
1413 } else {
1414 Uri uri = intent.getData();
1415 if (uri == null) {
1416 return;
1417 }
1418 String pkgName = uri.getSchemeSpecificPart();
1419 if (pkgName == null) {
1420 return;
1421 }
1422 pkgList = new String[] { pkgName };
1423 added = Intent.ACTION_PACKAGE_ADDED.equals(action);
Joe Onoratod070e892011-01-07 20:50:37 -08001424 changed = Intent.ACTION_PACKAGE_CHANGED.equals(action);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001425 }
1426 if (pkgList == null || pkgList.length == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 return;
1428 }
Joe Onoratod070e892011-01-07 20:50:37 -08001429 if (added || changed) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001430 synchronized (mAppWidgetIds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 Bundle extras = intent.getExtras();
Joe Onoratod070e892011-01-07 20:50:37 -08001432 if (changed || (extras != null &&
1433 extras.getBoolean(Intent.EXTRA_REPLACING, false))) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001434 for (String pkgName : pkgList) {
1435 // The package was just upgraded
1436 updateProvidersForPackageLocked(pkgName);
1437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 } else {
1439 // The package was just added
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001440 for (String pkgName : pkgList) {
1441 addProvidersForPackageLocked(pkgName);
1442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 }
1444 saveStateLocked();
1445 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001446 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 Bundle extras = intent.getExtras();
1448 if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) {
1449 // The package is being updated. We'll receive a PACKAGE_ADDED shortly.
1450 } else {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001451 synchronized (mAppWidgetIds) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001452 for (String pkgName : pkgList) {
1453 removeProvidersForPackageLocked(pkgName);
1454 saveStateLocked();
1455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 }
1457 }
1458 }
1459 }
1460 }
1461 };
1462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 void addProvidersForPackageLocked(String pkgName) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001464 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
Joe Onoratof6133fe2010-02-01 18:24:46 -05001465 intent.setPackage(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent,
1467 PackageManager.GET_META_DATA);
1468
Bjorn Bringert5f857802010-02-10 23:09:48 +00001469 final int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 for (int i=0; i<N; i++) {
1471 ResolveInfo ri = broadcastReceivers.get(i);
1472 ActivityInfo ai = ri.activityInfo;
Joe Onorato331fbdc72010-08-24 17:02:09 -04001473 if ((ai.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
1474 continue;
1475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 if (pkgName.equals(ai.packageName)) {
1477 addProviderLocked(ri);
1478 }
1479 }
1480 }
1481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 void updateProvidersForPackageLocked(String pkgName) {
Romain Guya5475592009-07-01 17:20:08 -07001483 HashSet<String> keep = new HashSet<String>();
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001484 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
Joe Onoratof6133fe2010-02-01 18:24:46 -05001485 intent.setPackage(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent,
1487 PackageManager.GET_META_DATA);
1488
1489 // add the missing ones and collect which ones to keep
Bjorn Bringert5f857802010-02-10 23:09:48 +00001490 int N = broadcastReceivers == null ? 0 : broadcastReceivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 for (int i=0; i<N; i++) {
1492 ResolveInfo ri = broadcastReceivers.get(i);
1493 ActivityInfo ai = ri.activityInfo;
Joe Onorato331fbdc72010-08-24 17:02:09 -04001494 if ((ai.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
1495 continue;
1496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 if (pkgName.equals(ai.packageName)) {
1498 ComponentName component = new ComponentName(ai.packageName, ai.name);
1499 Provider p = lookupProviderLocked(component);
1500 if (p == null) {
1501 if (addProviderLocked(ri)) {
1502 keep.add(ai.name);
1503 }
1504 } else {
1505 Provider parsed = parseProviderInfoXml(component, ri);
1506 if (parsed != null) {
1507 keep.add(ai.name);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001508 // Use the new AppWidgetProviderInfo.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 p.info = parsed.info;
1510 // If it's enabled
1511 final int M = p.instances.size();
1512 if (M > 0) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001513 int[] appWidgetIds = getAppWidgetIds(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 // Reschedule for the new updatePeriodMillis (don't worry about handling
1515 // it specially if updatePeriodMillis didn't change because we just sent
1516 // an update, and the next one will be updatePeriodMillis from now).
1517 cancelBroadcasts(p);
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001518 registerForBroadcastsLocked(p, appWidgetIds);
1519 // If it's currently showing, call back with the new AppWidgetProviderInfo.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 for (int j=0; j<M; j++) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001521 AppWidgetId id = p.instances.get(j);
Joe Onoratoa8a8a422010-06-16 15:06:16 -04001522 id.views = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 if (id.host != null && id.host.callbacks != null) {
1524 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001525 id.host.callbacks.providerChanged(id.appWidgetId, p.info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 } catch (RemoteException ex) {
1527 // It failed; remove the callback. No need to prune because
1528 // we know that this host is still referenced by this
1529 // instance.
1530 id.host.callbacks = null;
1531 }
1532 }
1533 }
1534 // Now that we've told the host, push out an update.
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001535 sendUpdateIntentLocked(p, appWidgetIds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 }
1537 }
1538 }
1539 }
1540 }
1541
1542 // prune the ones we don't want to keep
1543 N = mInstalledProviders.size();
1544 for (int i=N-1; i>=0; i--) {
1545 Provider p = mInstalledProviders.get(i);
1546 if (pkgName.equals(p.info.provider.getPackageName())
1547 && !keep.contains(p.info.provider.getClassName())) {
1548 removeProviderLocked(i, p);
1549 }
1550 }
1551 }
1552
1553 void removeProvidersForPackageLocked(String pkgName) {
1554 int N = mInstalledProviders.size();
1555 for (int i=N-1; i>=0; i--) {
1556 Provider p = mInstalledProviders.get(i);
1557 if (pkgName.equals(p.info.provider.getPackageName())) {
1558 removeProviderLocked(i, p);
1559 }
1560 }
1561
1562 // Delete the hosts for this package too
1563 //
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001564 // By now, we have removed any AppWidgets that were in any hosts here,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 // so we don't need to worry about sending DISABLE broadcasts to them.
1566 N = mHosts.size();
1567 for (int i=N-1; i>=0; i--) {
1568 Host host = mHosts.get(i);
1569 if (pkgName.equals(host.packageName)) {
1570 deleteHostLocked(host);
1571 }
1572 }
1573 }
1574}
1575