The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 android.content; |
| 18 | |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 19 | import android.accounts.Account; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.database.IContentObserver; |
| 21 | import android.database.sqlite.SQLiteException; |
| 22 | import android.net.Uri; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.IBinder; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 25 | import android.os.Parcel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.os.RemoteException; |
| 27 | import android.os.ServiceManager; |
| 28 | import android.util.Config; |
| 29 | import android.util.Log; |
| 30 | import android.Manifest; |
| 31 | |
| 32 | import java.io.FileDescriptor; |
| 33 | import java.io.PrintWriter; |
| 34 | import java.util.ArrayList; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 35 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * {@hide} |
| 39 | */ |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 40 | public final class ContentService extends IContentService.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | private static final String TAG = "ContentService"; |
| 42 | private Context mContext; |
| 43 | private boolean mFactoryTest; |
| 44 | private final ObserverNode mRootNode = new ObserverNode(""); |
| 45 | private SyncManager mSyncManager = null; |
| 46 | private final Object mSyncManagerLock = new Object(); |
| 47 | |
| 48 | private SyncManager getSyncManager() { |
| 49 | synchronized(mSyncManagerLock) { |
| 50 | try { |
| 51 | // Try to create the SyncManager, return null if it fails (e.g. the disk is full). |
| 52 | if (mSyncManager == null) mSyncManager = new SyncManager(mContext, mFactoryTest); |
| 53 | } catch (SQLiteException e) { |
| 54 | Log.e(TAG, "Can't create SyncManager", e); |
| 55 | } |
| 56 | return mSyncManager; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 62 | mContext.enforceCallingOrSelfPermission(Manifest.permission.DUMP, |
| 63 | "caller doesn't have the DUMP permission"); |
| 64 | |
| 65 | // This makes it so that future permission checks will be in the context of this |
| 66 | // process rather than the caller's process. We will restore this before returning. |
| 67 | long identityToken = clearCallingIdentity(); |
| 68 | try { |
| 69 | if (mSyncManager == null) { |
| 70 | pw.println("No SyncManager created! (Disk full?)"); |
| 71 | } else { |
| 72 | mSyncManager.dump(fd, pw); |
| 73 | } |
| 74 | } finally { |
| 75 | restoreCallingIdentity(identityToken); |
| 76 | } |
| 77 | } |
| 78 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 79 | @Override |
| 80 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 81 | throws RemoteException { |
| 82 | try { |
| 83 | return super.onTransact(code, data, reply, flags); |
| 84 | } catch (RuntimeException e) { |
| 85 | // The content service only throws security exceptions, so let's |
| 86 | // log all others. |
| 87 | if (!(e instanceof SecurityException)) { |
| 88 | Log.e(TAG, "Content Service Crash", e); |
| 89 | } |
| 90 | throw e; |
| 91 | } |
| 92 | } |
| 93 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | /*package*/ ContentService(Context context, boolean factoryTest) { |
| 95 | mContext = context; |
| 96 | mFactoryTest = factoryTest; |
| 97 | getSyncManager(); |
| 98 | } |
| 99 | |
| 100 | public void registerContentObserver(Uri uri, boolean notifyForDescendents, |
| 101 | IContentObserver observer) { |
| 102 | if (observer == null || uri == null) { |
| 103 | throw new IllegalArgumentException("You must pass a valid uri and observer"); |
| 104 | } |
| 105 | synchronized (mRootNode) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 106 | mRootNode.addObserverLocked(uri, observer, notifyForDescendents, mRootNode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | if (Config.LOGV) Log.v(TAG, "Registered observer " + observer + " at " + uri + |
| 108 | " with notifyForDescendents " + notifyForDescendents); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | public void unregisterContentObserver(IContentObserver observer) { |
| 113 | if (observer == null) { |
| 114 | throw new IllegalArgumentException("You must pass a valid observer"); |
| 115 | } |
| 116 | synchronized (mRootNode) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 117 | mRootNode.removeObserverLocked(observer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | if (Config.LOGV) Log.v(TAG, "Unregistered observer " + observer); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | public void notifyChange(Uri uri, IContentObserver observer, |
| 123 | boolean observerWantsSelfNotifications, boolean syncToNetwork) { |
| 124 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 125 | Log.v(TAG, "Notifying update of " + uri + " from observer " + observer |
| 126 | + ", syncToNetwork " + syncToNetwork); |
| 127 | } |
| 128 | // This makes it so that future permission checks will be in the context of this |
| 129 | // process rather than the caller's process. We will restore this before returning. |
| 130 | long identityToken = clearCallingIdentity(); |
| 131 | try { |
| 132 | ArrayList<ObserverCall> calls = new ArrayList<ObserverCall>(); |
| 133 | synchronized (mRootNode) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 134 | mRootNode.collectObserversLocked(uri, 0, observer, observerWantsSelfNotifications, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | calls); |
| 136 | } |
| 137 | final int numCalls = calls.size(); |
| 138 | for (int i=0; i<numCalls; i++) { |
| 139 | ObserverCall oc = calls.get(i); |
| 140 | try { |
| 141 | oc.mObserver.onChange(oc.mSelfNotify); |
| 142 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 143 | Log.v(TAG, "Notified " + oc.mObserver + " of " + "update at " + uri); |
| 144 | } |
| 145 | } catch (RemoteException ex) { |
| 146 | synchronized (mRootNode) { |
| 147 | Log.w(TAG, "Found dead observer, removing"); |
| 148 | IBinder binder = oc.mObserver.asBinder(); |
| 149 | final ArrayList<ObserverNode.ObserverEntry> list |
| 150 | = oc.mNode.mObservers; |
| 151 | int numList = list.size(); |
| 152 | for (int j=0; j<numList; j++) { |
| 153 | ObserverNode.ObserverEntry oe = list.get(j); |
| 154 | if (oe.observer.asBinder() == binder) { |
| 155 | list.remove(j); |
| 156 | j--; |
| 157 | numList--; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | if (syncToNetwork) { |
| 164 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 165 | if (syncManager != null) { |
| 166 | syncManager.scheduleLocalSync(null /* all accounts */, uri.getAuthority()); |
| 167 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | } |
| 169 | } finally { |
| 170 | restoreCallingIdentity(identityToken); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Hide this class since it is not part of api, |
| 176 | * but current unittest framework requires it to be public |
| 177 | * @hide |
| 178 | * |
| 179 | */ |
| 180 | public static final class ObserverCall { |
| 181 | final ObserverNode mNode; |
| 182 | final IContentObserver mObserver; |
| 183 | final boolean mSelfNotify; |
| 184 | |
| 185 | ObserverCall(ObserverNode node, IContentObserver observer, |
| 186 | boolean selfNotify) { |
| 187 | mNode = node; |
| 188 | mObserver = observer; |
| 189 | mSelfNotify = selfNotify; |
| 190 | } |
| 191 | } |
| 192 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 193 | public void requestSync(Account account, String authority, Bundle extras) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | ContentResolver.validateSyncExtrasBundle(extras); |
| 195 | // This makes it so that future permission checks will be in the context of this |
| 196 | // process rather than the caller's process. We will restore this before returning. |
| 197 | long identityToken = clearCallingIdentity(); |
| 198 | try { |
| 199 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 200 | if (syncManager != null) { |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 201 | syncManager.scheduleSync(account, authority, extras, 0 /* no delay */, |
| 202 | false /* onlyThoseWithUnkownSyncableState */); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 203 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } finally { |
| 205 | restoreCallingIdentity(identityToken); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Clear all scheduled sync operations that match the uri and cancel the active sync |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 211 | * if they match the authority and account, if they are present. |
| 212 | * @param account filter the pending and active syncs to cancel using this account |
| 213 | * @param authority filter the pending and active syncs to cancel using this authority |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | */ |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 215 | public void cancelSync(Account account, String authority) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | // This makes it so that future permission checks will be in the context of this |
| 217 | // process rather than the caller's process. We will restore this before returning. |
| 218 | long identityToken = clearCallingIdentity(); |
| 219 | try { |
| 220 | SyncManager syncManager = getSyncManager(); |
| 221 | if (syncManager != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 222 | syncManager.clearScheduledSyncOperations(account, authority); |
| 223 | syncManager.cancelActiveSync(account, authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | } |
| 225 | } finally { |
| 226 | restoreCallingIdentity(identityToken); |
| 227 | } |
| 228 | } |
| 229 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 230 | /** |
| 231 | * Get information about the SyncAdapters that are known to the system. |
| 232 | * @return an array of SyncAdapters that have registered with the system |
| 233 | */ |
| 234 | public SyncAdapterType[] getSyncAdapterTypes() { |
| 235 | // This makes it so that future permission checks will be in the context of this |
| 236 | // process rather than the caller's process. We will restore this before returning. |
| 237 | long identityToken = clearCallingIdentity(); |
| 238 | try { |
| 239 | SyncManager syncManager = getSyncManager(); |
| 240 | return syncManager.getSyncAdapterTypes(); |
| 241 | } finally { |
| 242 | restoreCallingIdentity(identityToken); |
| 243 | } |
| 244 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 245 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 246 | public boolean getSyncAutomatically(Account account, String providerName) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 247 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 248 | "no permission to read the sync settings"); |
| 249 | long identityToken = clearCallingIdentity(); |
| 250 | try { |
| 251 | SyncManager syncManager = getSyncManager(); |
| 252 | if (syncManager != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 253 | return syncManager.getSyncStorageEngine().getSyncAutomatically( |
| 254 | account, providerName); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 255 | } |
| 256 | } finally { |
| 257 | restoreCallingIdentity(identityToken); |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 262 | public void setSyncAutomatically(Account account, String providerName, boolean sync) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 263 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 264 | "no permission to write the sync settings"); |
| 265 | long identityToken = clearCallingIdentity(); |
| 266 | try { |
| 267 | SyncManager syncManager = getSyncManager(); |
| 268 | if (syncManager != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 269 | syncManager.getSyncStorageEngine().setSyncAutomatically( |
| 270 | account, providerName, sync); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 271 | } |
| 272 | } finally { |
| 273 | restoreCallingIdentity(identityToken); |
| 274 | } |
| 275 | } |
| 276 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 277 | public void addPeriodicSync(Account account, String authority, Bundle extras, |
| 278 | long pollFrequency) { |
| 279 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 280 | "no permission to write the sync settings"); |
| 281 | long identityToken = clearCallingIdentity(); |
| 282 | try { |
| 283 | getSyncManager().getSyncStorageEngine().addPeriodicSync( |
| 284 | account, authority, extras, pollFrequency); |
| 285 | } finally { |
| 286 | restoreCallingIdentity(identityToken); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | public void removePeriodicSync(Account account, String authority, Bundle extras) { |
| 291 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 292 | "no permission to write the sync settings"); |
| 293 | long identityToken = clearCallingIdentity(); |
| 294 | try { |
| 295 | getSyncManager().getSyncStorageEngine().removePeriodicSync(account, authority, extras); |
| 296 | } finally { |
| 297 | restoreCallingIdentity(identityToken); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | public List<PeriodicSync> getPeriodicSyncs(Account account, String providerName) { |
| 302 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 303 | "no permission to read the sync settings"); |
| 304 | long identityToken = clearCallingIdentity(); |
| 305 | try { |
| 306 | return getSyncManager().getSyncStorageEngine().getPeriodicSyncs( |
| 307 | account, providerName); |
| 308 | } finally { |
| 309 | restoreCallingIdentity(identityToken); |
| 310 | } |
| 311 | } |
| 312 | |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 313 | public int getIsSyncable(Account account, String providerName) { |
| 314 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 315 | "no permission to read the sync settings"); |
| 316 | long identityToken = clearCallingIdentity(); |
| 317 | try { |
| 318 | SyncManager syncManager = getSyncManager(); |
| 319 | if (syncManager != null) { |
| 320 | return syncManager.getSyncStorageEngine().getIsSyncable( |
| 321 | account, providerName); |
| 322 | } |
| 323 | } finally { |
| 324 | restoreCallingIdentity(identityToken); |
| 325 | } |
| 326 | return -1; |
| 327 | } |
| 328 | |
| 329 | public void setIsSyncable(Account account, String providerName, int syncable) { |
| 330 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 331 | "no permission to write the sync settings"); |
| 332 | long identityToken = clearCallingIdentity(); |
| 333 | try { |
| 334 | SyncManager syncManager = getSyncManager(); |
| 335 | if (syncManager != null) { |
| 336 | syncManager.getSyncStorageEngine().setIsSyncable( |
| 337 | account, providerName, syncable); |
| 338 | } |
| 339 | } finally { |
| 340 | restoreCallingIdentity(identityToken); |
| 341 | } |
| 342 | } |
| 343 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 344 | public boolean getMasterSyncAutomatically() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 345 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 346 | "no permission to read the sync settings"); |
| 347 | long identityToken = clearCallingIdentity(); |
| 348 | try { |
| 349 | SyncManager syncManager = getSyncManager(); |
| 350 | if (syncManager != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 351 | return syncManager.getSyncStorageEngine().getMasterSyncAutomatically(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 352 | } |
| 353 | } finally { |
| 354 | restoreCallingIdentity(identityToken); |
| 355 | } |
| 356 | return false; |
| 357 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 358 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 359 | public void setMasterSyncAutomatically(boolean flag) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 360 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 361 | "no permission to write the sync settings"); |
| 362 | long identityToken = clearCallingIdentity(); |
| 363 | try { |
| 364 | SyncManager syncManager = getSyncManager(); |
| 365 | if (syncManager != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 366 | syncManager.getSyncStorageEngine().setMasterSyncAutomatically(flag); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 367 | } |
| 368 | } finally { |
| 369 | restoreCallingIdentity(identityToken); |
| 370 | } |
| 371 | } |
| 372 | |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 373 | public boolean isSyncActive(Account account, String authority) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 374 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 375 | "no permission to read the sync stats"); |
| 376 | long identityToken = clearCallingIdentity(); |
| 377 | try { |
| 378 | SyncManager syncManager = getSyncManager(); |
| 379 | if (syncManager != null) { |
| 380 | return syncManager.getSyncStorageEngine().isSyncActive( |
| 381 | account, authority); |
| 382 | } |
| 383 | } finally { |
| 384 | restoreCallingIdentity(identityToken); |
| 385 | } |
| 386 | return false; |
| 387 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 388 | |
Fred Quintana | c6a6955 | 2010-09-27 17:05:04 -0700 | [diff] [blame] | 389 | public List<SyncInfo> getCurrentSyncs() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 390 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 391 | "no permission to read the sync stats"); |
| 392 | long identityToken = clearCallingIdentity(); |
| 393 | try { |
Fred Quintana | c6a6955 | 2010-09-27 17:05:04 -0700 | [diff] [blame] | 394 | return getSyncManager().getSyncStorageEngine().getCurrentSyncs(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 395 | } finally { |
| 396 | restoreCallingIdentity(identityToken); |
| 397 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 398 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 399 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 400 | public SyncStatusInfo getSyncStatus(Account account, String authority) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 401 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 402 | "no permission to read the sync stats"); |
| 403 | long identityToken = clearCallingIdentity(); |
| 404 | try { |
| 405 | SyncManager syncManager = getSyncManager(); |
| 406 | if (syncManager != null) { |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 407 | return syncManager.getSyncStorageEngine().getStatusByAccountAndAuthority( |
| 408 | account, authority); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 409 | } |
| 410 | } finally { |
| 411 | restoreCallingIdentity(identityToken); |
| 412 | } |
| 413 | return null; |
| 414 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 415 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 416 | public boolean isSyncPending(Account account, String authority) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 417 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 418 | "no permission to read the sync stats"); |
| 419 | long identityToken = clearCallingIdentity(); |
| 420 | try { |
| 421 | SyncManager syncManager = getSyncManager(); |
| 422 | if (syncManager != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 423 | return syncManager.getSyncStorageEngine().isSyncPending(account, authority); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 424 | } |
| 425 | } finally { |
| 426 | restoreCallingIdentity(identityToken); |
| 427 | } |
| 428 | return false; |
| 429 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 430 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 431 | public void addStatusChangeListener(int mask, ISyncStatusObserver callback) { |
| 432 | long identityToken = clearCallingIdentity(); |
| 433 | try { |
| 434 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 435 | if (syncManager != null && callback != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 436 | syncManager.getSyncStorageEngine().addStatusChangeListener(mask, callback); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 437 | } |
| 438 | } finally { |
| 439 | restoreCallingIdentity(identityToken); |
| 440 | } |
| 441 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 442 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 443 | public void removeStatusChangeListener(ISyncStatusObserver callback) { |
| 444 | long identityToken = clearCallingIdentity(); |
| 445 | try { |
| 446 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 447 | if (syncManager != null && callback != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 448 | syncManager.getSyncStorageEngine().removeStatusChangeListener(callback); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 449 | } |
| 450 | } finally { |
| 451 | restoreCallingIdentity(identityToken); |
| 452 | } |
| 453 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 454 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | public static IContentService main(Context context, boolean factoryTest) { |
| 456 | ContentService service = new ContentService(context, factoryTest); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 457 | ServiceManager.addService(ContentResolver.CONTENT_SERVICE_NAME, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | return service; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Hide this class since it is not part of api, |
| 463 | * but current unittest framework requires it to be public |
| 464 | * @hide |
| 465 | */ |
| 466 | public static final class ObserverNode { |
| 467 | private class ObserverEntry implements IBinder.DeathRecipient { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 468 | public final IContentObserver observer; |
| 469 | public final boolean notifyForDescendents; |
| 470 | private final Object observersLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 472 | public ObserverEntry(IContentObserver o, boolean n, Object observersLock) { |
| 473 | this.observersLock = observersLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | observer = o; |
| 475 | notifyForDescendents = n; |
| 476 | try { |
| 477 | observer.asBinder().linkToDeath(this, 0); |
| 478 | } catch (RemoteException e) { |
| 479 | binderDied(); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | public void binderDied() { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 484 | synchronized (observersLock) { |
| 485 | removeObserverLocked(observer); |
| 486 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| 490 | public static final int INSERT_TYPE = 0; |
| 491 | public static final int UPDATE_TYPE = 1; |
| 492 | public static final int DELETE_TYPE = 2; |
| 493 | |
| 494 | private String mName; |
| 495 | private ArrayList<ObserverNode> mChildren = new ArrayList<ObserverNode>(); |
| 496 | private ArrayList<ObserverEntry> mObservers = new ArrayList<ObserverEntry>(); |
| 497 | |
| 498 | public ObserverNode(String name) { |
| 499 | mName = name; |
| 500 | } |
| 501 | |
| 502 | private String getUriSegment(Uri uri, int index) { |
| 503 | if (uri != null) { |
| 504 | if (index == 0) { |
| 505 | return uri.getAuthority(); |
| 506 | } else { |
| 507 | return uri.getPathSegments().get(index - 1); |
| 508 | } |
| 509 | } else { |
| 510 | return null; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private int countUriSegments(Uri uri) { |
| 515 | if (uri == null) { |
| 516 | return 0; |
| 517 | } |
| 518 | return uri.getPathSegments().size() + 1; |
| 519 | } |
| 520 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 521 | public void addObserverLocked(Uri uri, IContentObserver observer, |
| 522 | boolean notifyForDescendents, Object observersLock) { |
| 523 | addObserverLocked(uri, 0, observer, notifyForDescendents, observersLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 526 | private void addObserverLocked(Uri uri, int index, IContentObserver observer, |
| 527 | boolean notifyForDescendents, Object observersLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | // If this is the leaf node add the observer |
| 529 | if (index == countUriSegments(uri)) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 530 | mObservers.add(new ObserverEntry(observer, notifyForDescendents, observersLock)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | return; |
| 532 | } |
| 533 | |
| 534 | // Look to see if the proper child already exists |
| 535 | String segment = getUriSegment(uri, index); |
Anthony Newnam | f512664 | 2010-03-22 17:29:06 -0500 | [diff] [blame] | 536 | if (segment == null) { |
| 537 | throw new IllegalArgumentException("Invalid Uri (" + uri + ") used for observer"); |
| 538 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | int N = mChildren.size(); |
| 540 | for (int i = 0; i < N; i++) { |
| 541 | ObserverNode node = mChildren.get(i); |
| 542 | if (node.mName.equals(segment)) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 543 | node.addObserverLocked(uri, index + 1, observer, notifyForDescendents, observersLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // No child found, create one |
| 549 | ObserverNode node = new ObserverNode(segment); |
| 550 | mChildren.add(node); |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 551 | node.addObserverLocked(uri, index + 1, observer, notifyForDescendents, observersLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | } |
| 553 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 554 | public boolean removeObserverLocked(IContentObserver observer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | int size = mChildren.size(); |
| 556 | for (int i = 0; i < size; i++) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 557 | boolean empty = mChildren.get(i).removeObserverLocked(observer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | if (empty) { |
| 559 | mChildren.remove(i); |
| 560 | i--; |
| 561 | size--; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | IBinder observerBinder = observer.asBinder(); |
| 566 | size = mObservers.size(); |
| 567 | for (int i = 0; i < size; i++) { |
| 568 | ObserverEntry entry = mObservers.get(i); |
| 569 | if (entry.observer.asBinder() == observerBinder) { |
| 570 | mObservers.remove(i); |
| 571 | // We no longer need to listen for death notifications. Remove it. |
| 572 | observerBinder.unlinkToDeath(entry, 0); |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | if (mChildren.size() == 0 && mObservers.size() == 0) { |
| 578 | return true; |
| 579 | } |
| 580 | return false; |
| 581 | } |
| 582 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 583 | private void collectMyObserversLocked(boolean leaf, IContentObserver observer, |
| 584 | boolean selfNotify, ArrayList<ObserverCall> calls) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | int N = mObservers.size(); |
| 586 | IBinder observerBinder = observer == null ? null : observer.asBinder(); |
| 587 | for (int i = 0; i < N; i++) { |
| 588 | ObserverEntry entry = mObservers.get(i); |
| 589 | |
| 590 | // Don't notify the observer if it sent the notification and isn't interesed |
| 591 | // in self notifications |
| 592 | if (entry.observer.asBinder() == observerBinder && !selfNotify) { |
| 593 | continue; |
| 594 | } |
| 595 | |
| 596 | // Make sure the observer is interested in the notification |
| 597 | if (leaf || (!leaf && entry.notifyForDescendents)) { |
| 598 | calls.add(new ObserverCall(this, entry.observer, selfNotify)); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 603 | public void collectObserversLocked(Uri uri, int index, IContentObserver observer, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | boolean selfNotify, ArrayList<ObserverCall> calls) { |
| 605 | String segment = null; |
| 606 | int segmentCount = countUriSegments(uri); |
| 607 | if (index >= segmentCount) { |
| 608 | // This is the leaf node, notify all observers |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 609 | collectMyObserversLocked(true, observer, selfNotify, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | } else if (index < segmentCount){ |
| 611 | segment = getUriSegment(uri, index); |
| 612 | // Notify any observers at this level who are interested in descendents |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 613 | collectMyObserversLocked(false, observer, selfNotify, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | int N = mChildren.size(); |
| 617 | for (int i = 0; i < N; i++) { |
| 618 | ObserverNode node = mChildren.get(i); |
| 619 | if (segment == null || node.mName.equals(segment)) { |
| 620 | // We found the child, |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 621 | node.collectObserversLocked(uri, index + 1, observer, selfNotify, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | if (segment != null) { |
| 623 | break; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | } |