The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.app.AlarmManager; |
| 20 | import android.app.PendingIntent; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 21 | import android.appwidget.AppWidgetManager; |
| 22 | import android.appwidget.AppWidgetProviderInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.content.pm.ActivityInfo; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.PackageInfo; |
| 31 | import android.content.pm.ResolveInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.content.res.TypedArray; |
| 33 | import android.content.res.XmlResourceParser; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.net.Uri; |
| 35 | import android.os.Binder; |
| 36 | import android.os.Bundle; |
Marco Nelissen | 54796e7 | 2009-04-30 15:16:30 -0700 | [diff] [blame] | 37 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.os.RemoteException; |
| 39 | import android.os.SystemClock; |
| 40 | import android.util.AttributeSet; |
| 41 | import android.util.Log; |
Mitsuru Oshima | 8f25c42 | 2009-07-01 00:10:43 -0700 | [diff] [blame] | 42 | import android.util.TypedValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.util.Xml; |
| 44 | import android.widget.RemoteViews; |
| 45 | |
| 46 | import java.io.IOException; |
| 47 | import java.io.File; |
| 48 | import java.io.FileDescriptor; |
| 49 | import java.io.FileInputStream; |
| 50 | import java.io.FileOutputStream; |
| 51 | import java.io.PrintWriter; |
| 52 | import java.util.ArrayList; |
| 53 | import java.util.List; |
| 54 | import java.util.HashMap; |
| 55 | import java.util.HashSet; |
| 56 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 57 | import com.android.internal.appwidget.IAppWidgetService; |
| 58 | import com.android.internal.appwidget.IAppWidgetHost; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import com.android.internal.util.FastXmlSerializer; |
| 60 | |
| 61 | import org.xmlpull.v1.XmlPullParser; |
| 62 | import org.xmlpull.v1.XmlPullParserException; |
| 63 | import org.xmlpull.v1.XmlSerializer; |
| 64 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 65 | class AppWidgetService extends IAppWidgetService.Stub |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 67 | private static final String TAG = "AppWidgetService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 69 | private static final String SETTINGS_FILENAME = "appwidgets.xml"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | private static final String SETTINGS_TMP_FILENAME = SETTINGS_FILENAME + ".tmp"; |
Joe Onorato | be96b3a | 2009-07-14 19:49:27 -0700 | [diff] [blame] | 71 | private static final int MIN_UPDATE_PERIOD = 30 * 60 * 1000; // 30 minutes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * When identifying a Host or Provider based on the calling process, use the uid field. |
| 75 | * When identifying a Host or Provider based on a package manager broadcast, use the |
| 76 | * package given. |
| 77 | */ |
| 78 | |
| 79 | static class Provider { |
| 80 | int uid; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 81 | AppWidgetProviderInfo info; |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 82 | ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | PendingIntent broadcast; |
| 84 | boolean zombie; // if we're in safe mode, don't prune this just because nobody references it |
| 85 | |
| 86 | int tag; // for use while saving state (the index) |
| 87 | } |
| 88 | |
| 89 | static class Host { |
| 90 | int uid; |
| 91 | int hostId; |
| 92 | String packageName; |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 93 | ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 94 | IAppWidgetHost callbacks; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | boolean zombie; // if we're in safe mode, don't prune this just because nobody references it |
| 96 | |
| 97 | int tag; // for use while saving state (the index) |
| 98 | } |
| 99 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 100 | static class AppWidgetId { |
| 101 | int appWidgetId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | Provider provider; |
| 103 | RemoteViews views; |
| 104 | Host host; |
| 105 | } |
| 106 | |
| 107 | Context mContext; |
| 108 | PackageManager mPackageManager; |
| 109 | AlarmManager mAlarmManager; |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 110 | ArrayList<Provider> mInstalledProviders = new ArrayList<Provider>(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 111 | int mNextAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID + 1; |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 112 | final ArrayList<AppWidgetId> mAppWidgetIds = new ArrayList<AppWidgetId>(); |
| 113 | ArrayList<Host> mHosts = new ArrayList<Host>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | boolean mSafeMode; |
| 115 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 116 | AppWidgetService(Context context) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | mContext = context; |
| 118 | mPackageManager = context.getPackageManager(); |
| 119 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 120 | } |
| 121 | |
| 122 | public void systemReady(boolean safeMode) { |
| 123 | mSafeMode = safeMode; |
| 124 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 125 | loadAppWidgetList(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | loadStateLocked(); |
| 127 | |
| 128 | // Register for the boot completed broadcast, so we can send the |
| 129 | // ENABLE broacasts. If we try to send them now, they time out, |
| 130 | // because the system isn't ready to handle them yet. |
| 131 | mContext.registerReceiver(mBroadcastReceiver, |
| 132 | new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null); |
| 133 | |
| 134 | // Register for broadcasts about package install, etc., so we can |
| 135 | // update the provider list. |
| 136 | IntentFilter filter = new IntentFilter(); |
| 137 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 138 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 139 | filter.addDataScheme("package"); |
| 140 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 145 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 146 | != PackageManager.PERMISSION_GRANTED) { |
| 147 | pw.println("Permission Denial: can't dump from from pid=" |
| 148 | + Binder.getCallingPid() |
| 149 | + ", uid=" + Binder.getCallingUid()); |
| 150 | return; |
| 151 | } |
| 152 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 153 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | int N = mInstalledProviders.size(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 155 | pw.println("Providers:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | for (int i=0; i<N; i++) { |
| 157 | Provider p = mInstalledProviders.get(i); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 158 | AppWidgetProviderInfo info = p.info; |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 159 | pw.print(" ["); pw.print(i); pw.print("] provider "); |
| 160 | pw.print(info.provider.flattenToShortString()); |
| 161 | pw.println(':'); |
| 162 | pw.print(" min=("); pw.print(info.minWidth); |
| 163 | pw.print("x"); pw.print(info.minHeight); |
| 164 | pw.print(") updatePeriodMillis="); |
| 165 | pw.print(info.updatePeriodMillis); |
| 166 | pw.print(" initialLayout=#"); |
| 167 | pw.print(Integer.toHexString(info.initialLayout)); |
| 168 | pw.print(" zombie="); pw.println(p.zombie); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | } |
| 170 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 171 | N = mAppWidgetIds.size(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 172 | pw.println(" "); |
| 173 | pw.println("AppWidgetIds:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | for (int i=0; i<N; i++) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 175 | AppWidgetId id = mAppWidgetIds.get(i); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 176 | pw.print(" ["); pw.print(i); pw.print("] id="); |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 177 | pw.println(id.appWidgetId); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 178 | pw.print(" hostId="); |
| 179 | pw.print(id.host.hostId); pw.print(' '); |
| 180 | pw.print(id.host.packageName); pw.print('/'); |
| 181 | pw.println(id.host.uid); |
| 182 | if (id.provider != null) { |
| 183 | pw.print(" provider="); |
| 184 | pw.println(id.provider.info.provider.flattenToShortString()); |
| 185 | } |
| 186 | if (id.host != null) { |
| 187 | pw.print(" host.callbacks="); pw.println(id.host.callbacks); |
| 188 | } |
| 189 | if (id.views != null) { |
| 190 | pw.print(" views="); pw.println(id.views); |
| 191 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | N = mHosts.size(); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 195 | pw.println(" "); |
| 196 | pw.println("Hosts:"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | for (int i=0; i<N; i++) { |
| 198 | Host host = mHosts.get(i); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 199 | pw.print(" ["); pw.print(i); pw.print("] hostId="); |
| 200 | pw.print(host.hostId); pw.print(' '); |
| 201 | pw.print(host.packageName); pw.print('/'); |
| 202 | pw.print(host.uid); pw.println(':'); |
| 203 | pw.print(" callbacks="); pw.println(host.callbacks); |
| 204 | pw.print(" instances.size="); pw.print(host.instances.size()); |
| 205 | pw.print(" zombie="); pw.println(host.zombie); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 210 | public int allocateAppWidgetId(String packageName, int hostId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | int callingUid = enforceCallingUid(packageName); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 212 | synchronized (mAppWidgetIds) { |
| 213 | int appWidgetId = mNextAppWidgetId++; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | |
| 215 | Host host = lookupOrAddHostLocked(callingUid, packageName, hostId); |
| 216 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 217 | AppWidgetId id = new AppWidgetId(); |
| 218 | id.appWidgetId = appWidgetId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | id.host = host; |
| 220 | |
| 221 | host.instances.add(id); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 222 | mAppWidgetIds.add(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | |
| 224 | saveStateLocked(); |
| 225 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 226 | return appWidgetId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 230 | public void deleteAppWidgetId(int appWidgetId) { |
| 231 | synchronized (mAppWidgetIds) { |
| 232 | AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | if (id != null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 234 | deleteAppWidgetLocked(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | saveStateLocked(); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | public void deleteHost(int hostId) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 241 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | int callingUid = getCallingUid(); |
| 243 | Host host = lookupHostLocked(callingUid, hostId); |
| 244 | if (host != null) { |
| 245 | deleteHostLocked(host); |
| 246 | saveStateLocked(); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | public void deleteAllHosts() { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 252 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | int callingUid = getCallingUid(); |
| 254 | final int N = mHosts.size(); |
| 255 | boolean changed = false; |
| 256 | for (int i=N-1; i>=0; i--) { |
| 257 | Host host = mHosts.get(i); |
| 258 | if (host.uid == callingUid) { |
| 259 | deleteHostLocked(host); |
| 260 | changed = true; |
| 261 | } |
| 262 | } |
| 263 | if (changed) { |
| 264 | saveStateLocked(); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void deleteHostLocked(Host host) { |
| 270 | final int N = host.instances.size(); |
| 271 | for (int i=N-1; i>=0; i--) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 272 | AppWidgetId id = host.instances.get(i); |
| 273 | deleteAppWidgetLocked(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | } |
| 275 | host.instances.clear(); |
| 276 | mHosts.remove(host); |
| 277 | // it's gone or going away, abruptly drop the callback connection |
| 278 | host.callbacks = null; |
| 279 | } |
| 280 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 281 | void deleteAppWidgetLocked(AppWidgetId id) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | Host host = id.host; |
| 283 | host.instances.remove(id); |
| 284 | pruneHostLocked(host); |
| 285 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 286 | mAppWidgetIds.remove(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | |
| 288 | Provider p = id.provider; |
| 289 | if (p != null) { |
| 290 | p.instances.remove(id); |
| 291 | if (!p.zombie) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 292 | // send the broacast saying that this appWidgetId has been deleted |
| 293 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_DELETED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | intent.setComponent(p.info.provider); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 295 | intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id.appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | mContext.sendBroadcast(intent); |
| 297 | if (p.instances.size() == 0) { |
| 298 | // cancel the future updates |
| 299 | cancelBroadcasts(p); |
| 300 | |
| 301 | // send the broacast saying that the provider is not in use any more |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 302 | intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_DISABLED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | intent.setComponent(p.info.provider); |
| 304 | mContext.sendBroadcast(intent); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void cancelBroadcasts(Provider p) { |
| 311 | if (p.broadcast != null) { |
| 312 | mAlarmManager.cancel(p.broadcast); |
| 313 | long token = Binder.clearCallingIdentity(); |
| 314 | try { |
| 315 | p.broadcast.cancel(); |
| 316 | } finally { |
| 317 | Binder.restoreCallingIdentity(token); |
| 318 | } |
| 319 | p.broadcast = null; |
| 320 | } |
| 321 | } |
| 322 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 323 | public void bindAppWidgetId(int appWidgetId, ComponentName provider) { |
| 324 | mContext.enforceCallingPermission(android.Manifest.permission.BIND_APPWIDGET, |
| 325 | "bindGagetId appWidgetId=" + appWidgetId + " provider=" + provider); |
| 326 | synchronized (mAppWidgetIds) { |
| 327 | AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | if (id == null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 329 | throw new IllegalArgumentException("bad appWidgetId"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | } |
| 331 | if (id.provider != null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 332 | throw new IllegalArgumentException("appWidgetId " + appWidgetId + " already bound to " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | + id.provider.info.provider); |
| 334 | } |
| 335 | Provider p = lookupProviderLocked(provider); |
| 336 | if (p == null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 337 | throw new IllegalArgumentException("not a appwidget provider: " + provider); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | } |
| 339 | if (p.zombie) { |
| 340 | throw new IllegalArgumentException("can't bind to a 3rd party provider in" |
| 341 | + " safe mode: " + provider); |
| 342 | } |
| 343 | |
| 344 | id.provider = p; |
| 345 | p.instances.add(id); |
| 346 | int instancesSize = p.instances.size(); |
| 347 | if (instancesSize == 1) { |
| 348 | // tell the provider that it's ready |
| 349 | sendEnableIntentLocked(p); |
| 350 | } |
| 351 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 352 | // send an update now -- We need this update now, and just for this appWidgetId. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | // It's less critical when the next one happens, so when we schdule the next one, |
| 354 | // we add updatePeriodMillis to its start time. That time will have some slop, |
| 355 | // but that's okay. |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 356 | sendUpdateIntentLocked(p, new int[] { appWidgetId }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | |
| 358 | // schedule the future updates |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 359 | registerForBroadcastsLocked(p, getAppWidgetIds(p)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | saveStateLocked(); |
| 361 | } |
| 362 | } |
| 363 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 364 | public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId) { |
| 365 | synchronized (mAppWidgetIds) { |
| 366 | AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | if (id != null && id.provider != null && !id.provider.zombie) { |
| 368 | return id.provider.info; |
| 369 | } |
| 370 | return null; |
| 371 | } |
| 372 | } |
| 373 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 374 | public RemoteViews getAppWidgetViews(int appWidgetId) { |
| 375 | synchronized (mAppWidgetIds) { |
| 376 | AppWidgetId id = lookupAppWidgetIdLocked(appWidgetId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | if (id != null) { |
| 378 | return id.views; |
| 379 | } |
| 380 | return null; |
| 381 | } |
| 382 | } |
| 383 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 384 | public List<AppWidgetProviderInfo> getInstalledProviders() { |
| 385 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | final int N = mInstalledProviders.size(); |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 387 | ArrayList<AppWidgetProviderInfo> result = new ArrayList<AppWidgetProviderInfo>(N); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | for (int i=0; i<N; i++) { |
| 389 | Provider p = mInstalledProviders.get(i); |
| 390 | if (!p.zombie) { |
| 391 | result.add(p.info); |
| 392 | } |
| 393 | } |
| 394 | return result; |
| 395 | } |
| 396 | } |
| 397 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 398 | public void updateAppWidgetIds(int[] appWidgetIds, RemoteViews views) { |
| 399 | if (appWidgetIds == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | return; |
| 401 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 402 | if (appWidgetIds.length == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | return; |
| 404 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 405 | final int N = appWidgetIds.length; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 407 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | for (int i=0; i<N; i++) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 409 | AppWidgetId id = lookupAppWidgetIdLocked(appWidgetIds[i]); |
| 410 | updateAppWidgetInstanceLocked(id, views); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 415 | public void updateAppWidgetProvider(ComponentName provider, RemoteViews views) { |
| 416 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | Provider p = lookupProviderLocked(provider); |
| 418 | if (p == null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 419 | Log.w(TAG, "updateAppWidgetProvider: provider doesn't exist: " + provider); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | return; |
| 421 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 422 | ArrayList<AppWidgetId> instances = p.instances; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | final int N = instances.size(); |
| 424 | for (int i=0; i<N; i++) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 425 | AppWidgetId id = instances.get(i); |
| 426 | updateAppWidgetInstanceLocked(id, views); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 431 | void updateAppWidgetInstanceLocked(AppWidgetId id, RemoteViews views) { |
| 432 | // allow for stale appWidgetIds and other badness |
| 433 | // lookup also checks that the calling process can access the appWidgetId |
| 434 | // drop unbound appWidgetIds (shouldn't be possible under normal circumstances) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | if (id != null && id.provider != null && !id.provider.zombie && !id.host.zombie) { |
| 436 | id.views = views; |
| 437 | |
| 438 | // is anyone listening? |
| 439 | if (id.host.callbacks != null) { |
| 440 | try { |
| 441 | // the lock is held, but this is a oneway call |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 442 | id.host.callbacks.updateAppWidget(id.appWidgetId, views); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | } catch (RemoteException e) { |
| 444 | // It failed; remove the callback. No need to prune because |
| 445 | // we know that this host is still referenced by this instance. |
| 446 | id.host.callbacks = null; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 452 | public int[] startListening(IAppWidgetHost callbacks, String packageName, int hostId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | List<RemoteViews> updatedViews) { |
| 454 | int callingUid = enforceCallingUid(packageName); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 455 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | Host host = lookupOrAddHostLocked(callingUid, packageName, hostId); |
| 457 | host.callbacks = callbacks; |
| 458 | |
| 459 | updatedViews.clear(); |
| 460 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 461 | ArrayList<AppWidgetId> instances = host.instances; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | int N = instances.size(); |
| 463 | int[] updatedIds = new int[N]; |
| 464 | for (int i=0; i<N; i++) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 465 | AppWidgetId id = instances.get(i); |
| 466 | updatedIds[i] = id.appWidgetId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | updatedViews.add(id.views); |
| 468 | } |
| 469 | return updatedIds; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | public void stopListening(int hostId) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 474 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | Host host = lookupHostLocked(getCallingUid(), hostId); |
Ken Shirriff | e21167a | 2009-09-23 16:42:53 -0700 | [diff] [blame^] | 476 | if (host != null) { |
| 477 | host.callbacks = null; |
| 478 | pruneHostLocked(host); |
| 479 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 483 | boolean canAccessAppWidgetId(AppWidgetId id, int callingUid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | if (id.host.uid == callingUid) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 485 | // Apps hosting the AppWidget have access to it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | return true; |
| 487 | } |
| 488 | if (id.provider != null && id.provider.uid == callingUid) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 489 | // Apps providing the AppWidget have access to it (if the appWidgetId has been bound) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | return true; |
| 491 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 492 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.BIND_APPWIDGET) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | == PackageManager.PERMISSION_GRANTED) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 494 | // Apps that can bind have access to all appWidgetIds. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | return true; |
| 496 | } |
| 497 | // Nobody else can access it. |
| 498 | return false; |
| 499 | } |
| 500 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 501 | AppWidgetId lookupAppWidgetIdLocked(int appWidgetId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | int callingUid = getCallingUid(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 503 | final int N = mAppWidgetIds.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | for (int i=0; i<N; i++) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 505 | AppWidgetId id = mAppWidgetIds.get(i); |
| 506 | if (id.appWidgetId == appWidgetId && canAccessAppWidgetId(id, callingUid)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | return id; |
| 508 | } |
| 509 | } |
| 510 | return null; |
| 511 | } |
| 512 | |
| 513 | Provider lookupProviderLocked(ComponentName provider) { |
| 514 | final int N = mInstalledProviders.size(); |
| 515 | for (int i=0; i<N; i++) { |
| 516 | Provider p = mInstalledProviders.get(i); |
| 517 | if (p.info.provider.equals(provider)) { |
| 518 | return p; |
| 519 | } |
| 520 | } |
| 521 | return null; |
| 522 | } |
| 523 | |
| 524 | Host lookupHostLocked(int uid, int hostId) { |
| 525 | final int N = mHosts.size(); |
| 526 | for (int i=0; i<N; i++) { |
| 527 | Host h = mHosts.get(i); |
| 528 | if (h.uid == uid && h.hostId == hostId) { |
| 529 | return h; |
| 530 | } |
| 531 | } |
| 532 | return null; |
| 533 | } |
| 534 | |
| 535 | Host lookupOrAddHostLocked(int uid, String packageName, int hostId) { |
| 536 | final int N = mHosts.size(); |
| 537 | for (int i=0; i<N; i++) { |
| 538 | Host h = mHosts.get(i); |
| 539 | if (h.hostId == hostId && h.packageName.equals(packageName)) { |
| 540 | return h; |
| 541 | } |
| 542 | } |
| 543 | Host host = new Host(); |
| 544 | host.packageName = packageName; |
| 545 | host.uid = uid; |
| 546 | host.hostId = hostId; |
| 547 | mHosts.add(host); |
| 548 | return host; |
| 549 | } |
| 550 | |
| 551 | void pruneHostLocked(Host host) { |
| 552 | if (host.instances.size() == 0 && host.callbacks == null) { |
| 553 | mHosts.remove(host); |
| 554 | } |
| 555 | } |
| 556 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 557 | void loadAppWidgetList() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | PackageManager pm = mPackageManager; |
| 559 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 560 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | List<ResolveInfo> broadcastReceivers = pm.queryBroadcastReceivers(intent, |
| 562 | PackageManager.GET_META_DATA); |
| 563 | |
| 564 | final int N = broadcastReceivers.size(); |
| 565 | for (int i=0; i<N; i++) { |
| 566 | ResolveInfo ri = broadcastReceivers.get(i); |
| 567 | addProviderLocked(ri); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | boolean addProviderLocked(ResolveInfo ri) { |
| 572 | Provider p = parseProviderInfoXml(new ComponentName(ri.activityInfo.packageName, |
| 573 | ri.activityInfo.name), ri); |
| 574 | if (p != null) { |
| 575 | mInstalledProviders.add(p); |
| 576 | return true; |
| 577 | } else { |
| 578 | return false; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | void removeProviderLocked(int index, Provider p) { |
| 583 | int N = p.instances.size(); |
| 584 | for (int i=0; i<N; i++) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 585 | AppWidgetId id = p.instances.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | // Call back with empty RemoteViews |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 587 | updateAppWidgetInstanceLocked(id, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | // Stop telling the host about updates for this from now on |
| 589 | cancelBroadcasts(p); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 590 | // clear out references to this appWidgetId |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | id.host.instances.remove(id); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 592 | mAppWidgetIds.remove(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | id.provider = null; |
| 594 | pruneHostLocked(id.host); |
| 595 | id.host = null; |
| 596 | } |
| 597 | p.instances.clear(); |
| 598 | mInstalledProviders.remove(index); |
| 599 | // no need to send the DISABLE broadcast, since the receiver is gone anyway |
| 600 | cancelBroadcasts(p); |
| 601 | } |
| 602 | |
| 603 | void sendEnableIntentLocked(Provider p) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 604 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_ENABLED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | intent.setComponent(p.info.provider); |
| 606 | mContext.sendBroadcast(intent); |
| 607 | } |
| 608 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 609 | void sendUpdateIntentLocked(Provider p, int[] appWidgetIds) { |
| 610 | if (appWidgetIds != null && appWidgetIds.length > 0) { |
| 611 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); |
| 612 | intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | intent.setComponent(p.info.provider); |
| 614 | mContext.sendBroadcast(intent); |
| 615 | } |
| 616 | } |
| 617 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 618 | void registerForBroadcastsLocked(Provider p, int[] appWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | if (p.info.updatePeriodMillis > 0) { |
| 620 | // if this is the first instance, set the alarm. otherwise, |
| 621 | // rely on the fact that we've already set it and that |
| 622 | // PendingIntent.getBroadcast will update the extras. |
| 623 | boolean alreadyRegistered = p.broadcast != null; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 624 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); |
| 625 | intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | intent.setComponent(p.info.provider); |
| 627 | long token = Binder.clearCallingIdentity(); |
| 628 | try { |
| 629 | p.broadcast = PendingIntent.getBroadcast(mContext, 1, intent, |
| 630 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 631 | } finally { |
| 632 | Binder.restoreCallingIdentity(token); |
| 633 | } |
| 634 | if (!alreadyRegistered) { |
Joe Onorato | be96b3a | 2009-07-14 19:49:27 -0700 | [diff] [blame] | 635 | long period = p.info.updatePeriodMillis; |
| 636 | if (period < MIN_UPDATE_PERIOD) { |
| 637 | period = MIN_UPDATE_PERIOD; |
| 638 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Joe Onorato | be96b3a | 2009-07-14 19:49:27 -0700 | [diff] [blame] | 640 | SystemClock.elapsedRealtime() + period, period, p.broadcast); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 645 | static int[] getAppWidgetIds(Provider p) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | int instancesSize = p.instances.size(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 647 | int appWidgetIds[] = new int[instancesSize]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | for (int i=0; i<instancesSize; i++) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 649 | appWidgetIds[i] = p.instances.get(i).appWidgetId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | } |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 651 | return appWidgetIds; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | } |
| 653 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 654 | public int[] getAppWidgetIds(ComponentName provider) { |
| 655 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | Provider p = lookupProviderLocked(provider); |
| 657 | if (p != null && getCallingUid() == p.uid) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 658 | return getAppWidgetIds(p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | } else { |
| 660 | return new int[0]; |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | private Provider parseProviderInfoXml(ComponentName component, ResolveInfo ri) { |
| 666 | Provider p = null; |
| 667 | |
| 668 | ActivityInfo activityInfo = ri.activityInfo; |
| 669 | XmlResourceParser parser = null; |
| 670 | try { |
| 671 | parser = activityInfo.loadXmlMetaData(mPackageManager, |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 672 | AppWidgetManager.META_DATA_APPWIDGET_PROVIDER); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | if (parser == null) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 674 | Log.w(TAG, "No " + AppWidgetManager.META_DATA_APPWIDGET_PROVIDER + " meta-data for " |
| 675 | + "AppWidget provider '" + component + '\''); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | return null; |
| 677 | } |
| 678 | |
| 679 | AttributeSet attrs = Xml.asAttributeSet(parser); |
| 680 | |
| 681 | int type; |
| 682 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 683 | && type != XmlPullParser.START_TAG) { |
| 684 | // drain whitespace, comments, etc. |
| 685 | } |
| 686 | |
| 687 | String nodeName = parser.getName(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 688 | if (!"appwidget-provider".equals(nodeName)) { |
| 689 | Log.w(TAG, "Meta-data does not start with appwidget-provider tag for" |
| 690 | + " AppWidget provider '" + component + '\''); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | return null; |
| 692 | } |
| 693 | |
| 694 | p = new Provider(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 695 | AppWidgetProviderInfo info = p.info = new AppWidgetProviderInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | |
| 697 | info.provider = component; |
| 698 | p.uid = activityInfo.applicationInfo.uid; |
| 699 | |
| 700 | TypedArray sa = mContext.getResources().obtainAttributes(attrs, |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 701 | com.android.internal.R.styleable.AppWidgetProviderInfo); |
Mitsuru Oshima | 8f25c42 | 2009-07-01 00:10:43 -0700 | [diff] [blame] | 702 | |
| 703 | // These dimensions has to be resolved in the application's context. |
| 704 | // We simply send back the raw complex data, which will be |
| 705 | // converted to dp in {@link AppWidgetManager#getAppWidgetInfo}. |
| 706 | TypedValue value = sa.peekValue( |
| 707 | com.android.internal.R.styleable.AppWidgetProviderInfo_minWidth); |
| 708 | info.minWidth = value != null ? value.data : 0; |
| 709 | value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minHeight); |
| 710 | info.minHeight = value != null ? value.data : 0; |
| 711 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | info.updatePeriodMillis = sa.getInt( |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 713 | com.android.internal.R.styleable.AppWidgetProviderInfo_updatePeriodMillis, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | info.initialLayout = sa.getResourceId( |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 715 | com.android.internal.R.styleable.AppWidgetProviderInfo_initialLayout, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | String className = sa.getString( |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 717 | com.android.internal.R.styleable.AppWidgetProviderInfo_configure); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | if (className != null) { |
| 719 | info.configure = new ComponentName(component.getPackageName(), className); |
| 720 | } |
| 721 | info.label = activityInfo.loadLabel(mPackageManager).toString(); |
| 722 | info.icon = ri.getIconResource(); |
| 723 | sa.recycle(); |
| 724 | } catch (Exception e) { |
| 725 | // Ok to catch Exception here, because anything going wrong because |
| 726 | // of what a client process passes to us should not be fatal for the |
| 727 | // system process. |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 728 | Log.w(TAG, "XML parsing failed for AppWidget provider '" + component + '\'', e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | return null; |
| 730 | } finally { |
| 731 | if (parser != null) parser.close(); |
| 732 | } |
| 733 | return p; |
| 734 | } |
| 735 | |
| 736 | int getUidForPackage(String packageName) throws PackageManager.NameNotFoundException { |
| 737 | PackageInfo pkgInfo = mPackageManager.getPackageInfo(packageName, 0); |
| 738 | if (pkgInfo == null || pkgInfo.applicationInfo == null) { |
| 739 | throw new PackageManager.NameNotFoundException(); |
| 740 | } |
| 741 | return pkgInfo.applicationInfo.uid; |
| 742 | } |
| 743 | |
| 744 | int enforceCallingUid(String packageName) throws IllegalArgumentException { |
| 745 | int callingUid = getCallingUid(); |
| 746 | int packageUid; |
| 747 | try { |
| 748 | packageUid = getUidForPackage(packageName); |
| 749 | } catch (PackageManager.NameNotFoundException ex) { |
| 750 | throw new IllegalArgumentException("packageName and uid don't match packageName=" |
| 751 | + packageName); |
| 752 | } |
Marco Nelissen | 54796e7 | 2009-04-30 15:16:30 -0700 | [diff] [blame] | 753 | if (callingUid != packageUid && Process.supportsProcesses()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | throw new IllegalArgumentException("packageName and uid don't match packageName=" |
| 755 | + packageName); |
| 756 | } |
| 757 | return callingUid; |
| 758 | } |
| 759 | |
| 760 | void sendInitialBroadcasts() { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 761 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | final int N = mInstalledProviders.size(); |
| 763 | for (int i=0; i<N; i++) { |
| 764 | Provider p = mInstalledProviders.get(i); |
| 765 | if (p.instances.size() > 0) { |
| 766 | sendEnableIntentLocked(p); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 767 | int[] appWidgetIds = getAppWidgetIds(p); |
| 768 | sendUpdateIntentLocked(p, appWidgetIds); |
| 769 | registerForBroadcastsLocked(p, appWidgetIds); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | // only call from initialization -- it assumes that the data structures are all empty |
| 776 | void loadStateLocked() { |
| 777 | File temp = savedStateTempFile(); |
| 778 | File real = savedStateRealFile(); |
| 779 | |
| 780 | // prefer the real file. If it doesn't exist, use the temp one, and then copy it to the |
| 781 | // real one. if there is both a real file and a temp one, assume that the temp one isn't |
| 782 | // fully written and delete it. |
| 783 | if (real.exists()) { |
| 784 | readStateFromFileLocked(real); |
| 785 | if (temp.exists()) { |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 786 | //noinspection ResultOfMethodCallIgnored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | temp.delete(); |
| 788 | } |
| 789 | } else if (temp.exists()) { |
| 790 | readStateFromFileLocked(temp); |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 791 | //noinspection ResultOfMethodCallIgnored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | temp.renameTo(real); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | void saveStateLocked() { |
| 797 | File temp = savedStateTempFile(); |
| 798 | File real = savedStateRealFile(); |
| 799 | |
| 800 | if (!real.exists()) { |
| 801 | // If the real one doesn't exist, it's either because this is the first time |
| 802 | // or because something went wrong while copying them. In this case, we can't |
| 803 | // trust anything that's in temp. In order to have the loadState code not |
| 804 | // use the temporary one until it's fully written, create an empty file |
| 805 | // for real, which will we'll shortly delete. |
| 806 | try { |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 807 | //noinspection ResultOfMethodCallIgnored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | real.createNewFile(); |
| 809 | } catch (IOException e) { |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 810 | // Ignore |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
| 814 | if (temp.exists()) { |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 815 | //noinspection ResultOfMethodCallIgnored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | temp.delete(); |
| 817 | } |
| 818 | |
| 819 | writeStateToFileLocked(temp); |
| 820 | |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 821 | //noinspection ResultOfMethodCallIgnored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | real.delete(); |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 823 | //noinspection ResultOfMethodCallIgnored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | temp.renameTo(real); |
| 825 | } |
| 826 | |
| 827 | void writeStateToFileLocked(File file) { |
| 828 | FileOutputStream stream = null; |
| 829 | int N; |
| 830 | |
| 831 | try { |
| 832 | stream = new FileOutputStream(file, false); |
| 833 | XmlSerializer out = new FastXmlSerializer(); |
| 834 | out.setOutput(stream, "utf-8"); |
| 835 | out.startDocument(null, true); |
| 836 | |
| 837 | |
| 838 | out.startTag(null, "gs"); |
| 839 | |
| 840 | int providerIndex = 0; |
| 841 | N = mInstalledProviders.size(); |
| 842 | for (int i=0; i<N; i++) { |
| 843 | Provider p = mInstalledProviders.get(i); |
| 844 | if (p.instances.size() > 0) { |
| 845 | out.startTag(null, "p"); |
| 846 | out.attribute(null, "pkg", p.info.provider.getPackageName()); |
| 847 | out.attribute(null, "cl", p.info.provider.getClassName()); |
| 848 | out.endTag(null, "h"); |
| 849 | p.tag = providerIndex; |
| 850 | providerIndex++; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | N = mHosts.size(); |
| 855 | for (int i=0; i<N; i++) { |
| 856 | Host host = mHosts.get(i); |
| 857 | out.startTag(null, "h"); |
| 858 | out.attribute(null, "pkg", host.packageName); |
| 859 | out.attribute(null, "id", Integer.toHexString(host.hostId)); |
| 860 | out.endTag(null, "h"); |
| 861 | host.tag = i; |
| 862 | } |
| 863 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 864 | N = mAppWidgetIds.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | for (int i=0; i<N; i++) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 866 | AppWidgetId id = mAppWidgetIds.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | out.startTag(null, "g"); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 868 | out.attribute(null, "id", Integer.toHexString(id.appWidgetId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | out.attribute(null, "h", Integer.toHexString(id.host.tag)); |
| 870 | if (id.provider != null) { |
| 871 | out.attribute(null, "p", Integer.toHexString(id.provider.tag)); |
| 872 | } |
| 873 | out.endTag(null, "g"); |
| 874 | } |
| 875 | |
| 876 | out.endTag(null, "gs"); |
| 877 | |
| 878 | out.endDocument(); |
| 879 | stream.close(); |
| 880 | } catch (IOException e) { |
| 881 | try { |
| 882 | if (stream != null) { |
| 883 | stream.close(); |
| 884 | } |
| 885 | } catch (IOException ex) { |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 886 | // Ignore |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | } |
| 888 | if (file.exists()) { |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 889 | //noinspection ResultOfMethodCallIgnored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | file.delete(); |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | void readStateFromFileLocked(File file) { |
| 896 | FileInputStream stream = null; |
| 897 | |
| 898 | boolean success = false; |
| 899 | |
| 900 | try { |
| 901 | stream = new FileInputStream(file); |
| 902 | XmlPullParser parser = Xml.newPullParser(); |
| 903 | parser.setInput(stream, null); |
| 904 | |
| 905 | int type; |
| 906 | int providerIndex = 0; |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 907 | HashMap<Integer,Provider> loadedProviders = new HashMap<Integer, Provider>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | do { |
| 909 | type = parser.next(); |
| 910 | if (type == XmlPullParser.START_TAG) { |
| 911 | String tag = parser.getName(); |
| 912 | if ("p".equals(tag)) { |
| 913 | // TODO: do we need to check that this package has the same signature |
| 914 | // as before? |
| 915 | String pkg = parser.getAttributeValue(null, "pkg"); |
| 916 | String cl = parser.getAttributeValue(null, "cl"); |
| 917 | Provider p = lookupProviderLocked(new ComponentName(pkg, cl)); |
| 918 | if (p == null && mSafeMode) { |
| 919 | // if we're in safe mode, make a temporary one |
| 920 | p = new Provider(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 921 | p.info = new AppWidgetProviderInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | p.info.provider = new ComponentName(pkg, cl); |
| 923 | p.zombie = true; |
| 924 | mInstalledProviders.add(p); |
| 925 | } |
| 926 | if (p != null) { |
| 927 | // if it wasn't uninstalled or something |
| 928 | loadedProviders.put(providerIndex, p); |
| 929 | } |
| 930 | providerIndex++; |
| 931 | } |
| 932 | else if ("h".equals(tag)) { |
| 933 | Host host = new Host(); |
| 934 | |
| 935 | // TODO: do we need to check that this package has the same signature |
| 936 | // as before? |
| 937 | host.packageName = parser.getAttributeValue(null, "pkg"); |
| 938 | try { |
| 939 | host.uid = getUidForPackage(host.packageName); |
| 940 | } catch (PackageManager.NameNotFoundException ex) { |
| 941 | host.zombie = true; |
| 942 | } |
| 943 | if (!host.zombie || mSafeMode) { |
| 944 | // In safe mode, we don't discard the hosts we don't recognize |
| 945 | // so that they're not pruned from our list. Otherwise, we do. |
| 946 | host.hostId = Integer.parseInt( |
| 947 | parser.getAttributeValue(null, "id"), 16); |
| 948 | mHosts.add(host); |
| 949 | } |
| 950 | } |
| 951 | else if ("g".equals(tag)) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 952 | AppWidgetId id = new AppWidgetId(); |
| 953 | id.appWidgetId = Integer.parseInt(parser.getAttributeValue(null, "id"), 16); |
| 954 | if (id.appWidgetId >= mNextAppWidgetId) { |
| 955 | mNextAppWidgetId = id.appWidgetId + 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | String providerString = parser.getAttributeValue(null, "p"); |
| 959 | if (providerString != null) { |
| 960 | // there's no provider if it hasn't been bound yet. |
| 961 | // maybe we don't have to save this, but it brings the system |
| 962 | // to the state it was in. |
| 963 | int pIndex = Integer.parseInt(providerString, 16); |
| 964 | id.provider = loadedProviders.get(pIndex); |
| 965 | if (false) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 966 | Log.d(TAG, "bound appWidgetId=" + id.appWidgetId + " to provider " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | + pIndex + " which is " + id.provider); |
| 968 | } |
| 969 | if (id.provider == null) { |
| 970 | // This provider is gone. We just let the host figure out |
| 971 | // that this happened when it fails to load it. |
| 972 | continue; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | int hIndex = Integer.parseInt(parser.getAttributeValue(null, "h"), 16); |
| 977 | id.host = mHosts.get(hIndex); |
| 978 | if (id.host == null) { |
| 979 | // This host is gone. |
| 980 | continue; |
| 981 | } |
| 982 | |
| 983 | if (id.provider != null) { |
| 984 | id.provider.instances.add(id); |
| 985 | } |
| 986 | id.host.instances.add(id); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 987 | mAppWidgetIds.add(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | } while (type != XmlPullParser.END_DOCUMENT); |
| 991 | success = true; |
| 992 | } catch (NullPointerException e) { |
| 993 | Log.w(TAG, "failed parsing " + file, e); |
| 994 | } catch (NumberFormatException e) { |
| 995 | Log.w(TAG, "failed parsing " + file, e); |
| 996 | } catch (XmlPullParserException e) { |
| 997 | Log.w(TAG, "failed parsing " + file, e); |
| 998 | } catch (IOException e) { |
| 999 | Log.w(TAG, "failed parsing " + file, e); |
| 1000 | } catch (IndexOutOfBoundsException e) { |
| 1001 | Log.w(TAG, "failed parsing " + file, e); |
| 1002 | } |
| 1003 | try { |
| 1004 | if (stream != null) { |
| 1005 | stream.close(); |
| 1006 | } |
| 1007 | } catch (IOException e) { |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 1008 | // Ignore |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | if (success) { |
| 1012 | // delete any hosts that didn't manage to get connected (should happen) |
| 1013 | // if it matters, they'll be reconnected. |
Dianne Hackborn | 002716d | 2009-08-12 11:13:26 -0700 | [diff] [blame] | 1014 | for (int i=mHosts.size()-1; i>=0; i--) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | pruneHostLocked(mHosts.get(i)); |
| 1016 | } |
| 1017 | } else { |
| 1018 | // failed reading, clean up |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1019 | mAppWidgetIds.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | mHosts.clear(); |
| 1021 | final int N = mInstalledProviders.size(); |
| 1022 | for (int i=0; i<N; i++) { |
| 1023 | mInstalledProviders.get(i).instances.clear(); |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | File savedStateTempFile() { |
| 1029 | return new File("/data/system/" + SETTINGS_TMP_FILENAME); |
| 1030 | //return new File(mContext.getFilesDir(), SETTINGS_FILENAME); |
| 1031 | } |
| 1032 | |
| 1033 | File savedStateRealFile() { |
| 1034 | return new File("/data/system/" + SETTINGS_FILENAME); |
| 1035 | //return new File(mContext.getFilesDir(), SETTINGS_TMP_FILENAME); |
| 1036 | } |
| 1037 | |
| 1038 | BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 1039 | public void onReceive(Context context, Intent intent) { |
| 1040 | String action = intent.getAction(); |
| 1041 | //Log.d(TAG, "received " + action); |
| 1042 | if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { |
| 1043 | sendInitialBroadcasts(); |
| 1044 | } else { |
| 1045 | Uri uri = intent.getData(); |
| 1046 | if (uri == null) { |
| 1047 | return; |
| 1048 | } |
| 1049 | String pkgName = uri.getSchemeSpecificPart(); |
| 1050 | if (pkgName == null) { |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1055 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | Bundle extras = intent.getExtras(); |
| 1057 | if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) { |
| 1058 | // The package was just upgraded |
| 1059 | updateProvidersForPackageLocked(pkgName); |
| 1060 | } else { |
| 1061 | // The package was just added |
| 1062 | addProvidersForPackageLocked(pkgName); |
| 1063 | } |
| 1064 | saveStateLocked(); |
| 1065 | } |
| 1066 | } |
| 1067 | else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 1068 | Bundle extras = intent.getExtras(); |
| 1069 | if (extras != null && extras.getBoolean(Intent.EXTRA_REPLACING, false)) { |
| 1070 | // The package is being updated. We'll receive a PACKAGE_ADDED shortly. |
| 1071 | } else { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1072 | synchronized (mAppWidgetIds) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | removeProvidersForPackageLocked(pkgName); |
| 1074 | saveStateLocked(); |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | }; |
| 1081 | |
| 1082 | // TODO: If there's a better way of matching an intent filter against the |
| 1083 | // packages for a given package, use that. |
| 1084 | void addProvidersForPackageLocked(String pkgName) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1085 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent, |
| 1087 | PackageManager.GET_META_DATA); |
| 1088 | |
| 1089 | final int N = broadcastReceivers.size(); |
| 1090 | for (int i=0; i<N; i++) { |
| 1091 | ResolveInfo ri = broadcastReceivers.get(i); |
| 1092 | ActivityInfo ai = ri.activityInfo; |
| 1093 | |
| 1094 | if (pkgName.equals(ai.packageName)) { |
| 1095 | addProviderLocked(ri); |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | // TODO: If there's a better way of matching an intent filter against the |
| 1101 | // packages for a given package, use that. |
| 1102 | void updateProvidersForPackageLocked(String pkgName) { |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 1103 | HashSet<String> keep = new HashSet<String>(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1104 | Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | List<ResolveInfo> broadcastReceivers = mPackageManager.queryBroadcastReceivers(intent, |
| 1106 | PackageManager.GET_META_DATA); |
| 1107 | |
| 1108 | // add the missing ones and collect which ones to keep |
| 1109 | int N = broadcastReceivers.size(); |
| 1110 | for (int i=0; i<N; i++) { |
| 1111 | ResolveInfo ri = broadcastReceivers.get(i); |
| 1112 | ActivityInfo ai = ri.activityInfo; |
| 1113 | if (pkgName.equals(ai.packageName)) { |
| 1114 | ComponentName component = new ComponentName(ai.packageName, ai.name); |
| 1115 | Provider p = lookupProviderLocked(component); |
| 1116 | if (p == null) { |
| 1117 | if (addProviderLocked(ri)) { |
| 1118 | keep.add(ai.name); |
| 1119 | } |
| 1120 | } else { |
| 1121 | Provider parsed = parseProviderInfoXml(component, ri); |
| 1122 | if (parsed != null) { |
| 1123 | keep.add(ai.name); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1124 | // Use the new AppWidgetProviderInfo. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1125 | p.info = parsed.info; |
| 1126 | // If it's enabled |
| 1127 | final int M = p.instances.size(); |
| 1128 | if (M > 0) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1129 | int[] appWidgetIds = getAppWidgetIds(p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1130 | // Reschedule for the new updatePeriodMillis (don't worry about handling |
| 1131 | // it specially if updatePeriodMillis didn't change because we just sent |
| 1132 | // an update, and the next one will be updatePeriodMillis from now). |
| 1133 | cancelBroadcasts(p); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1134 | registerForBroadcastsLocked(p, appWidgetIds); |
| 1135 | // If it's currently showing, call back with the new AppWidgetProviderInfo. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1136 | for (int j=0; j<M; j++) { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1137 | AppWidgetId id = p.instances.get(j); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | if (id.host != null && id.host.callbacks != null) { |
| 1139 | try { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1140 | id.host.callbacks.providerChanged(id.appWidgetId, p.info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1141 | } catch (RemoteException ex) { |
| 1142 | // It failed; remove the callback. No need to prune because |
| 1143 | // we know that this host is still referenced by this |
| 1144 | // instance. |
| 1145 | id.host.callbacks = null; |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | // Now that we've told the host, push out an update. |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1150 | sendUpdateIntentLocked(p, appWidgetIds); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | // prune the ones we don't want to keep |
| 1158 | N = mInstalledProviders.size(); |
| 1159 | for (int i=N-1; i>=0; i--) { |
| 1160 | Provider p = mInstalledProviders.get(i); |
| 1161 | if (pkgName.equals(p.info.provider.getPackageName()) |
| 1162 | && !keep.contains(p.info.provider.getClassName())) { |
| 1163 | removeProviderLocked(i, p); |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | void removeProvidersForPackageLocked(String pkgName) { |
| 1169 | int N = mInstalledProviders.size(); |
| 1170 | for (int i=N-1; i>=0; i--) { |
| 1171 | Provider p = mInstalledProviders.get(i); |
| 1172 | if (pkgName.equals(p.info.provider.getPackageName())) { |
| 1173 | removeProviderLocked(i, p); |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | // Delete the hosts for this package too |
| 1178 | // |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1179 | // By now, we have removed any AppWidgets that were in any hosts here, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | // so we don't need to worry about sending DISABLE broadcasts to them. |
| 1181 | N = mHosts.size(); |
| 1182 | for (int i=N-1; i>=0; i--) { |
| 1183 | Host host = mHosts.get(i); |
| 1184 | if (pkgName.equals(host.packageName)) { |
| 1185 | deleteHostLocked(host); |
| 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |