AppWidgetService.java revision e8724c82ab1479f13c85a2c6219841e1fd95f2d2
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 17package com.android.server; 18 19import android.app.AlarmManager; 20import android.app.PendingIntent; 21import android.appwidget.AppWidgetManager; 22import android.appwidget.AppWidgetProviderInfo; 23import android.content.BroadcastReceiver; 24import android.content.ComponentName; 25import android.content.Context; 26import android.content.Intent; 27import android.content.IntentFilter; 28import android.content.ServiceConnection; 29import android.content.pm.PackageManager; 30import android.os.Binder; 31import android.os.Bundle; 32import android.os.IBinder; 33import android.os.RemoteException; 34import android.util.Pair; 35import android.util.Slog; 36import android.util.SparseArray; 37import android.widget.RemoteViews; 38 39import com.android.internal.appwidget.IAppWidgetHost; 40import com.android.internal.appwidget.IAppWidgetService; 41import com.android.internal.widget.IRemoteViewsAdapterConnection; 42 43import java.io.FileDescriptor; 44import java.io.PrintWriter; 45import java.util.ArrayList; 46import java.util.List; 47import java.util.Locale; 48 49 50/** 51 * Redirects calls to this service to the instance of the service for the appropriate user. 52 */ 53class AppWidgetService extends IAppWidgetService.Stub 54{ 55 private static final String TAG = "AppWidgetService"; 56 57 /* 58 * When identifying a Host or Provider based on the calling process, use the uid field. 59 * When identifying a Host or Provider based on a package manager broadcast, use the 60 * package given. 61 */ 62 63 static class Provider { 64 int uid; 65 AppWidgetProviderInfo info; 66 ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>(); 67 PendingIntent broadcast; 68 boolean zombie; // if we're in safe mode, don't prune this just because nobody references it 69 70 int tag; // for use while saving state (the index) 71 } 72 73 static class Host { 74 int uid; 75 int hostId; 76 String packageName; 77 ArrayList<AppWidgetId> instances = new ArrayList<AppWidgetId>(); 78 IAppWidgetHost callbacks; 79 boolean zombie; // if we're in safe mode, don't prune this just because nobody references it 80 81 int tag; // for use while saving state (the index) 82 } 83 84 static class AppWidgetId { 85 int appWidgetId; 86 Provider provider; 87 RemoteViews views; 88 Host host; 89 } 90 91 /** 92 * Acts as a proxy between the ServiceConnection and the RemoteViewsAdapterConnection. 93 * This needs to be a static inner class since a reference to the ServiceConnection is held 94 * globally and may lead us to leak AppWidgetService instances (if there were more than one). 95 */ 96 static class ServiceConnectionProxy implements ServiceConnection { 97 private final IBinder mConnectionCb; 98 99 ServiceConnectionProxy(Pair<Integer, Intent.FilterComparison> key, IBinder connectionCb) { 100 mConnectionCb = connectionCb; 101 } 102 public void onServiceConnected(ComponentName name, IBinder service) { 103 final IRemoteViewsAdapterConnection cb = 104 IRemoteViewsAdapterConnection.Stub.asInterface(mConnectionCb); 105 try { 106 cb.onServiceConnected(service); 107 } catch (Exception e) { 108 e.printStackTrace(); 109 } 110 } 111 public void onServiceDisconnected(ComponentName name) { 112 disconnect(); 113 } 114 public void disconnect() { 115 final IRemoteViewsAdapterConnection cb = 116 IRemoteViewsAdapterConnection.Stub.asInterface(mConnectionCb); 117 try { 118 cb.onServiceDisconnected(); 119 } catch (Exception e) { 120 e.printStackTrace(); 121 } 122 } 123 } 124 125 Context mContext; 126 Locale mLocale; 127 PackageManager mPackageManager; 128 AlarmManager mAlarmManager; 129 ArrayList<Provider> mInstalledProviders = new ArrayList<Provider>(); 130 int mNextAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID + 1; 131 final ArrayList<AppWidgetId> mAppWidgetIds = new ArrayList<AppWidgetId>(); 132 ArrayList<Host> mHosts = new ArrayList<Host>(); 133 boolean mSafeMode; 134 135 136 private final SparseArray<AppWidgetServiceImpl> mAppWidgetServices; 137 138 AppWidgetService(Context context) { 139 mContext = context; 140 mAppWidgetServices = new SparseArray<AppWidgetServiceImpl>(5); 141 AppWidgetServiceImpl primary = new AppWidgetServiceImpl(context, 0); 142 mAppWidgetServices.append(0, primary); 143 } 144 145 public void systemReady(boolean safeMode) { 146 mSafeMode = safeMode; 147 148 mAppWidgetServices.get(0).systemReady(safeMode); 149 150 // Register for the boot completed broadcast, so we can send the 151 // ENABLE broacasts. If we try to send them now, they time out, 152 // because the system isn't ready to handle them yet. 153 mContext.registerReceiver(mBroadcastReceiver, 154 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null); 155 156 // Register for configuration changes so we can update the names 157 // of the widgets when the locale changes. 158 mContext.registerReceiver(mBroadcastReceiver, new IntentFilter( 159 Intent.ACTION_CONFIGURATION_CHANGED), null, null); 160 161 // Register for broadcasts about package install, etc., so we can 162 // update the provider list. 163 IntentFilter filter = new IntentFilter(); 164 filter.addAction(Intent.ACTION_PACKAGE_ADDED); 165 filter.addAction(Intent.ACTION_PACKAGE_CHANGED); 166 filter.addAction(Intent.ACTION_PACKAGE_REMOVED); 167 filter.addDataScheme("package"); 168 mContext.registerReceiver(mBroadcastReceiver, filter); 169 // Register for events related to sdcard installation. 170 IntentFilter sdFilter = new IntentFilter(); 171 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); 172 sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); 173 mContext.registerReceiver(mBroadcastReceiver, sdFilter); 174 175 IntentFilter userFilter = new IntentFilter(); 176 userFilter.addAction(Intent.ACTION_USER_REMOVED); 177 mContext.registerReceiver(new BroadcastReceiver() { 178 @Override 179 public void onReceive(Context context, Intent intent) { 180 onUserRemoved(intent.getIntExtra(Intent.EXTRA_USERID, -1)); 181 } 182 }, userFilter); 183 } 184 185 @Override 186 public int allocateAppWidgetId(String packageName, int hostId) throws RemoteException { 187 return getImplForUser().allocateAppWidgetId(packageName, hostId); 188 } 189 190 @Override 191 public void deleteAppWidgetId(int appWidgetId) throws RemoteException { 192 getImplForUser().deleteAppWidgetId(appWidgetId); 193 } 194 195 @Override 196 public void deleteHost(int hostId) throws RemoteException { 197 getImplForUser().deleteHost(hostId); 198 } 199 200 @Override 201 public void deleteAllHosts() throws RemoteException { 202 getImplForUser().deleteAllHosts(); 203 } 204 205 @Override 206 public void bindAppWidgetId(int appWidgetId, ComponentName provider) throws RemoteException { 207 getImplForUser().bindAppWidgetId(appWidgetId, provider); 208 } 209 210 @Override 211 public void bindRemoteViewsService(int appWidgetId, Intent intent, IBinder connection) 212 throws RemoteException { 213 getImplForUser().bindRemoteViewsService(appWidgetId, intent, connection); 214 } 215 216 @Override 217 public int[] startListening(IAppWidgetHost host, String packageName, int hostId, 218 List<RemoteViews> updatedViews) throws RemoteException { 219 return getImplForUser().startListening(host, packageName, hostId, updatedViews); 220 } 221 222 public void onUserRemoved(int userId) { 223 AppWidgetServiceImpl impl = mAppWidgetServices.get(userId); 224 if (userId < 1) return; 225 226 if (impl == null) { 227 AppWidgetServiceImpl.getSettingsFile(userId).delete(); 228 } else { 229 impl.onUserRemoved(); 230 } 231 } 232 233 private AppWidgetServiceImpl getImplForUser() { 234 final int userId = Binder.getOrigCallingUser(); 235 AppWidgetServiceImpl service = mAppWidgetServices.get(userId); 236 if (service == null) { 237 Slog.e(TAG, "Unable to find AppWidgetServiceImpl for the current user"); 238 // TODO: Verify that it's a valid user 239 service = new AppWidgetServiceImpl(mContext, userId); 240 service.systemReady(mSafeMode); 241 // Assume that BOOT_COMPLETED was received, as this is a non-primary user. 242 service.sendInitialBroadcasts(); 243 mAppWidgetServices.append(userId, service); 244 } 245 246 return service; 247 } 248 249 @Override 250 public int[] getAppWidgetIds(ComponentName provider) throws RemoteException { 251 return getImplForUser().getAppWidgetIds(provider); 252 } 253 254 @Override 255 public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId) throws RemoteException { 256 return getImplForUser().getAppWidgetInfo(appWidgetId); 257 } 258 259 @Override 260 public RemoteViews getAppWidgetViews(int appWidgetId) throws RemoteException { 261 return getImplForUser().getAppWidgetViews(appWidgetId); 262 } 263 264 @Override 265 public void updateAppWidgetExtras(int appWidgetId, Bundle extras) { 266 getImplForUser().updateAppWidgetExtras(appWidgetId, extras); 267 } 268 269 @Override 270 public Bundle getAppWidgetExtras(int appWidgetId) { 271 return getImplForUser().getAppWidgetExtras(appWidgetId); 272 } 273 274 static int[] getAppWidgetIds(Provider p) { 275 int instancesSize = p.instances.size(); 276 int appWidgetIds[] = new int[instancesSize]; 277 for (int i=0; i<instancesSize; i++) { 278 appWidgetIds[i] = p.instances.get(i).appWidgetId; 279 } 280 return appWidgetIds; 281 } 282 283 @Override 284 public List<AppWidgetProviderInfo> getInstalledProviders() throws RemoteException { 285 return getImplForUser().getInstalledProviders(); 286 } 287 288 @Override 289 public void notifyAppWidgetViewDataChanged(int[] appWidgetIds, int viewId) 290 throws RemoteException { 291 getImplForUser().notifyAppWidgetViewDataChanged(appWidgetIds, viewId); 292 } 293 294 @Override 295 public void partiallyUpdateAppWidgetIds(int[] appWidgetIds, RemoteViews views) 296 throws RemoteException { 297 getImplForUser().partiallyUpdateAppWidgetIds(appWidgetIds, views); 298 } 299 300 @Override 301 public void stopListening(int hostId) throws RemoteException { 302 getImplForUser().stopListening(hostId); 303 } 304 305 @Override 306 public void unbindRemoteViewsService(int appWidgetId, Intent intent) throws RemoteException { 307 getImplForUser().unbindRemoteViewsService(appWidgetId, intent); 308 } 309 310 @Override 311 public void updateAppWidgetIds(int[] appWidgetIds, RemoteViews views) throws RemoteException { 312 getImplForUser().updateAppWidgetIds(appWidgetIds, views); 313 } 314 315 @Override 316 public void updateAppWidgetProvider(ComponentName provider, RemoteViews views) 317 throws RemoteException { 318 getImplForUser().updateAppWidgetProvider(provider, views); 319 } 320 321 @Override 322 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 323 // Dump the state of all the app widget providers 324 for (int i = 0; i < mAppWidgetServices.size(); i++) { 325 AppWidgetServiceImpl service = mAppWidgetServices.valueAt(i); 326 service.dump(fd, pw, args); 327 } 328 } 329 330 BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 331 public void onReceive(Context context, Intent intent) { 332 String action = intent.getAction(); 333 // Slog.d(TAG, "received " + action); 334 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { 335 getImplForUser().sendInitialBroadcasts(); 336 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) { 337 for (int i = 0; i < mAppWidgetServices.size(); i++) { 338 AppWidgetServiceImpl service = mAppWidgetServices.valueAt(i); 339 service.onConfigurationChanged(); 340 } 341 } else { 342 for (int i = 0; i < mAppWidgetServices.size(); i++) { 343 AppWidgetServiceImpl service = mAppWidgetServices.valueAt(i); 344 service.onBroadcastReceived(intent); 345 } 346 } 347 } 348 }; 349} 350