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