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 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.content; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 19 | import android.Manifest; |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 20 | import android.accounts.Account; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.content.IContentService; |
| 25 | import android.content.ISyncStatusObserver; |
| 26 | import android.content.PeriodicSync; |
| 27 | import android.content.SyncAdapterType; |
| 28 | import android.content.SyncInfo; |
| 29 | import android.content.SyncStatusInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.database.IContentObserver; |
| 31 | import android.database.sqlite.SQLiteException; |
| 32 | import android.net.Uri; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 33 | import android.os.Binder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.Bundle; |
| 35 | import android.os.IBinder; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 36 | import android.os.Parcel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.os.RemoteException; |
| 38 | import android.os.ServiceManager; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 39 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.util.Log; |
Jeff Sharkey | 6009459 | 2013-03-21 18:14:24 -0700 | [diff] [blame] | 41 | import android.util.Slog; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 42 | import android.util.SparseIntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| 44 | import java.io.FileDescriptor; |
| 45 | import java.io.PrintWriter; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 46 | import java.security.InvalidParameterException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import java.util.ArrayList; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 48 | import java.util.Collections; |
| 49 | import java.util.Comparator; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 50 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * {@hide} |
| 54 | */ |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 55 | public final class ContentService extends IContentService.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | private static final String TAG = "ContentService"; |
| 57 | private Context mContext; |
| 58 | private boolean mFactoryTest; |
| 59 | private final ObserverNode mRootNode = new ObserverNode(""); |
| 60 | private SyncManager mSyncManager = null; |
| 61 | private final Object mSyncManagerLock = new Object(); |
| 62 | |
| 63 | private SyncManager getSyncManager() { |
| 64 | synchronized(mSyncManagerLock) { |
| 65 | try { |
| 66 | // Try to create the SyncManager, return null if it fails (e.g. the disk is full). |
| 67 | if (mSyncManager == null) mSyncManager = new SyncManager(mContext, mFactoryTest); |
| 68 | } catch (SQLiteException e) { |
| 69 | Log.e(TAG, "Can't create SyncManager", e); |
| 70 | } |
| 71 | return mSyncManager; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 77 | mContext.enforceCallingOrSelfPermission(Manifest.permission.DUMP, |
| 78 | "caller doesn't have the DUMP permission"); |
| 79 | |
| 80 | // This makes it so that future permission checks will be in the context of this |
| 81 | // process rather than the caller's process. We will restore this before returning. |
| 82 | long identityToken = clearCallingIdentity(); |
| 83 | try { |
| 84 | if (mSyncManager == null) { |
| 85 | pw.println("No SyncManager created! (Disk full?)"); |
| 86 | } else { |
| 87 | mSyncManager.dump(fd, pw); |
| 88 | } |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 89 | pw.println(); |
| 90 | pw.println("Observer tree:"); |
| 91 | synchronized (mRootNode) { |
| 92 | int[] counts = new int[2]; |
| 93 | final SparseIntArray pidCounts = new SparseIntArray(); |
| 94 | mRootNode.dumpLocked(fd, pw, args, "", " ", counts, pidCounts); |
| 95 | pw.println(); |
| 96 | ArrayList<Integer> sorted = new ArrayList<Integer>(); |
| 97 | for (int i=0; i<pidCounts.size(); i++) { |
| 98 | sorted.add(pidCounts.keyAt(i)); |
| 99 | } |
| 100 | Collections.sort(sorted, new Comparator<Integer>() { |
| 101 | @Override |
| 102 | public int compare(Integer lhs, Integer rhs) { |
| 103 | int lc = pidCounts.get(lhs); |
| 104 | int rc = pidCounts.get(rhs); |
| 105 | if (lc < rc) { |
| 106 | return 1; |
| 107 | } else if (lc > rc) { |
| 108 | return -1; |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | }); |
| 114 | for (int i=0; i<sorted.size(); i++) { |
| 115 | int pid = sorted.get(i); |
| 116 | pw.print(" pid "); pw.print(pid); pw.print(": "); |
| 117 | pw.print(pidCounts.get(pid)); pw.println(" observers"); |
| 118 | } |
| 119 | pw.println(); |
| 120 | pw.print(" Total number of nodes: "); pw.println(counts[0]); |
| 121 | pw.print(" Total number of observers: "); pw.println(counts[1]); |
| 122 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | } finally { |
| 124 | restoreCallingIdentity(identityToken); |
| 125 | } |
| 126 | } |
| 127 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 128 | @Override |
| 129 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 130 | throws RemoteException { |
| 131 | try { |
| 132 | return super.onTransact(code, data, reply, flags); |
| 133 | } catch (RuntimeException e) { |
| 134 | // The content service only throws security exceptions, so let's |
| 135 | // log all others. |
| 136 | if (!(e instanceof SecurityException)) { |
| 137 | Log.e(TAG, "Content Service Crash", e); |
| 138 | } |
| 139 | throw e; |
| 140 | } |
| 141 | } |
| 142 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | /*package*/ ContentService(Context context, boolean factoryTest) { |
| 144 | mContext = context; |
| 145 | mFactoryTest = factoryTest; |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | public void systemReady() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | getSyncManager(); |
| 150 | } |
| 151 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Register a content observer tied to a specific user's view of the provider. |
| 154 | * @param userHandle the user whose view of the provider is to be observed. May be |
| 155 | * the calling user without requiring any permission, otherwise the caller needs to |
| 156 | * hold the INTERACT_ACROSS_USERS_FULL permission. Pseudousers USER_ALL and |
| 157 | * USER_CURRENT are properly handled; all other pseudousers are forbidden. |
| 158 | */ |
| 159 | @Override |
| 160 | public void registerContentObserver(Uri uri, boolean notifyForDescendants, |
| 161 | IContentObserver observer, int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | if (observer == null || uri == null) { |
| 163 | throw new IllegalArgumentException("You must pass a valid uri and observer"); |
| 164 | } |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 165 | |
| 166 | final int callingUser = UserHandle.getCallingUserId(); |
| 167 | if (callingUser != userHandle) { |
| 168 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 169 | "no permission to observe other users' provider view"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | } |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 171 | |
| 172 | if (userHandle < 0) { |
| 173 | if (userHandle == UserHandle.USER_CURRENT) { |
| 174 | userHandle = ActivityManager.getCurrentUser(); |
| 175 | } else if (userHandle != UserHandle.USER_ALL) { |
| 176 | throw new InvalidParameterException("Bad user handle for registerContentObserver: " |
| 177 | + userHandle); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | synchronized (mRootNode) { |
| 182 | mRootNode.addObserverLocked(uri, observer, notifyForDescendants, mRootNode, |
| 183 | Binder.getCallingUid(), Binder.getCallingPid(), userHandle); |
| 184 | if (false) Log.v(TAG, "Registered observer " + observer + " at " + uri + |
| 185 | " with notifyForDescendants " + notifyForDescendants); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | public void registerContentObserver(Uri uri, boolean notifyForDescendants, |
| 190 | IContentObserver observer) { |
| 191 | registerContentObserver(uri, notifyForDescendants, observer, |
| 192 | UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | public void unregisterContentObserver(IContentObserver observer) { |
| 196 | if (observer == null) { |
| 197 | throw new IllegalArgumentException("You must pass a valid observer"); |
| 198 | } |
| 199 | synchronized (mRootNode) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 200 | mRootNode.removeObserverLocked(observer); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 201 | if (false) Log.v(TAG, "Unregistered observer " + observer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 205 | /** |
| 206 | * Notify observers of a particular user's view of the provider. |
| 207 | * @param userHandle the user whose view of the provider is to be notified. May be |
| 208 | * the calling user without requiring any permission, otherwise the caller needs to |
| 209 | * hold the INTERACT_ACROSS_USERS_FULL permission. Pseudousers USER_ALL and |
| 210 | * USER_CURRENT are properly interpreted; no other pseudousers are allowed. |
| 211 | */ |
| 212 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | public void notifyChange(Uri uri, IContentObserver observer, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 214 | boolean observerWantsSelfNotifications, boolean syncToNetwork, |
| 215 | int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 217 | Log.v(TAG, "Notifying update of " + uri + " for user " + userHandle |
| 218 | + " from observer " + observer + ", syncToNetwork " + syncToNetwork); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 220 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 221 | // Notify for any user other than the caller's own requires permission. |
| 222 | final int callingUserHandle = UserHandle.getCallingUserId(); |
| 223 | if (userHandle != callingUserHandle) { |
| 224 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 225 | "no permission to notify other users"); |
| 226 | } |
| 227 | |
| 228 | // We passed the permission check; resolve pseudouser targets as appropriate |
| 229 | if (userHandle < 0) { |
| 230 | if (userHandle == UserHandle.USER_CURRENT) { |
| 231 | userHandle = ActivityManager.getCurrentUser(); |
| 232 | } else if (userHandle != UserHandle.USER_ALL) { |
| 233 | throw new InvalidParameterException("Bad user handle for notifyChange: " |
| 234 | + userHandle); |
| 235 | } |
| 236 | } |
| 237 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 238 | final int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | // This makes it so that future permission checks will be in the context of this |
| 240 | // process rather than the caller's process. We will restore this before returning. |
| 241 | long identityToken = clearCallingIdentity(); |
| 242 | try { |
| 243 | ArrayList<ObserverCall> calls = new ArrayList<ObserverCall>(); |
| 244 | synchronized (mRootNode) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 245 | mRootNode.collectObserversLocked(uri, 0, observer, observerWantsSelfNotifications, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 246 | userHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | } |
| 248 | final int numCalls = calls.size(); |
| 249 | for (int i=0; i<numCalls; i++) { |
| 250 | ObserverCall oc = calls.get(i); |
| 251 | try { |
Jeff Brown | 655e66b | 2012-01-23 15:51:41 -0800 | [diff] [blame] | 252 | oc.mObserver.onChange(oc.mSelfChange, uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 254 | Log.v(TAG, "Notified " + oc.mObserver + " of " + "update at " + uri); |
| 255 | } |
| 256 | } catch (RemoteException ex) { |
| 257 | synchronized (mRootNode) { |
| 258 | Log.w(TAG, "Found dead observer, removing"); |
| 259 | IBinder binder = oc.mObserver.asBinder(); |
| 260 | final ArrayList<ObserverNode.ObserverEntry> list |
| 261 | = oc.mNode.mObservers; |
| 262 | int numList = list.size(); |
| 263 | for (int j=0; j<numList; j++) { |
| 264 | ObserverNode.ObserverEntry oe = list.get(j); |
| 265 | if (oe.observer.asBinder() == binder) { |
| 266 | list.remove(j); |
| 267 | j--; |
| 268 | numList--; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | if (syncToNetwork) { |
| 275 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 276 | if (syncManager != null) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 277 | syncManager.scheduleLocalSync(null /* all accounts */, callingUserHandle, uid, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 278 | uri.getAuthority()); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 279 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | } |
| 281 | } finally { |
| 282 | restoreCallingIdentity(identityToken); |
| 283 | } |
| 284 | } |
| 285 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 286 | public void notifyChange(Uri uri, IContentObserver observer, |
| 287 | boolean observerWantsSelfNotifications, boolean syncToNetwork) { |
| 288 | notifyChange(uri, observer, observerWantsSelfNotifications, syncToNetwork, |
| 289 | UserHandle.getCallingUserId()); |
| 290 | } |
| 291 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | /** |
| 293 | * Hide this class since it is not part of api, |
| 294 | * but current unittest framework requires it to be public |
| 295 | * @hide |
| 296 | * |
| 297 | */ |
| 298 | public static final class ObserverCall { |
| 299 | final ObserverNode mNode; |
| 300 | final IContentObserver mObserver; |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 301 | final boolean mSelfChange; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 303 | ObserverCall(ObserverNode node, IContentObserver observer, boolean selfChange) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | mNode = node; |
| 305 | mObserver = observer; |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 306 | mSelfChange = selfChange; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 310 | public void requestSync(Account account, String authority, Bundle extras) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | ContentResolver.validateSyncExtrasBundle(extras); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 312 | int userId = UserHandle.getCallingUserId(); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 313 | int uId = Binder.getCallingUid(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 314 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | // This makes it so that future permission checks will be in the context of this |
| 316 | // process rather than the caller's process. We will restore this before returning. |
| 317 | long identityToken = clearCallingIdentity(); |
| 318 | try { |
| 319 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 320 | if (syncManager != null) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 321 | syncManager.scheduleSync(account, userId, uId, authority, extras, 0 /* no delay */, |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 322 | false /* onlyThoseWithUnkownSyncableState */); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 323 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | } finally { |
| 325 | restoreCallingIdentity(identityToken); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * 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] | 331 | * if they match the authority and account, if they are present. |
| 332 | * @param account filter the pending and active syncs to cancel using this account |
| 333 | * @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] | 334 | */ |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 335 | public void cancelSync(Account account, String authority) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 336 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 337 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | // This makes it so that future permission checks will be in the context of this |
| 339 | // process rather than the caller's process. We will restore this before returning. |
| 340 | long identityToken = clearCallingIdentity(); |
| 341 | try { |
| 342 | SyncManager syncManager = getSyncManager(); |
| 343 | if (syncManager != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 344 | syncManager.clearScheduledSyncOperations(account, userId, authority); |
| 345 | syncManager.cancelActiveSync(account, userId, authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | } |
| 347 | } finally { |
| 348 | restoreCallingIdentity(identityToken); |
| 349 | } |
| 350 | } |
| 351 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 352 | /** |
| 353 | * Get information about the SyncAdapters that are known to the system. |
| 354 | * @return an array of SyncAdapters that have registered with the system |
| 355 | */ |
| 356 | public SyncAdapterType[] getSyncAdapterTypes() { |
| 357 | // This makes it so that future permission checks will be in the context of this |
| 358 | // process rather than the caller's process. We will restore this before returning. |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 359 | final int userId = UserHandle.getCallingUserId(); |
| 360 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 361 | try { |
| 362 | SyncManager syncManager = getSyncManager(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 363 | return syncManager.getSyncAdapterTypes(userId); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 364 | } finally { |
| 365 | restoreCallingIdentity(identityToken); |
| 366 | } |
| 367 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 368 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 369 | public boolean getSyncAutomatically(Account account, String providerName) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 370 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 371 | "no permission to read the sync settings"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 372 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 373 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 374 | long identityToken = clearCallingIdentity(); |
| 375 | try { |
| 376 | SyncManager syncManager = getSyncManager(); |
| 377 | if (syncManager != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 378 | return syncManager.getSyncStorageEngine().getSyncAutomatically( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 379 | account, userId, providerName); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 380 | } |
| 381 | } finally { |
| 382 | restoreCallingIdentity(identityToken); |
| 383 | } |
| 384 | return false; |
| 385 | } |
| 386 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 387 | public void setSyncAutomatically(Account account, String providerName, boolean sync) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 388 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 389 | "no permission to write the sync settings"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 390 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 391 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 392 | long identityToken = clearCallingIdentity(); |
| 393 | try { |
| 394 | SyncManager syncManager = getSyncManager(); |
| 395 | if (syncManager != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 396 | syncManager.getSyncStorageEngine().setSyncAutomatically( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 397 | account, userId, providerName, sync); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 398 | } |
| 399 | } finally { |
| 400 | restoreCallingIdentity(identityToken); |
| 401 | } |
| 402 | } |
| 403 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 404 | public void addPeriodicSync(Account account, String authority, Bundle extras, |
| 405 | long pollFrequency) { |
| 406 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 407 | "no permission to write the sync settings"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 408 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 409 | |
Jeff Sharkey | 51366be | 2013-03-27 14:46:55 -0700 | [diff] [blame] | 410 | if (pollFrequency < 60) { |
Jeff Sharkey | 6009459 | 2013-03-21 18:14:24 -0700 | [diff] [blame] | 411 | Slog.w(TAG, "Requested poll frequency of " + pollFrequency |
Jeff Sharkey | 51366be | 2013-03-27 14:46:55 -0700 | [diff] [blame] | 412 | + " seconds being rounded up to 60 seconds."); |
| 413 | pollFrequency = 60; |
Jeff Sharkey | 6009459 | 2013-03-21 18:14:24 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 416 | long identityToken = clearCallingIdentity(); |
| 417 | try { |
| 418 | getSyncManager().getSyncStorageEngine().addPeriodicSync( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 419 | account, userId, authority, extras, pollFrequency); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 420 | } finally { |
| 421 | restoreCallingIdentity(identityToken); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | public void removePeriodicSync(Account account, String authority, Bundle extras) { |
| 426 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 427 | "no permission to write the sync settings"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 428 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 429 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 430 | long identityToken = clearCallingIdentity(); |
| 431 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 432 | getSyncManager().getSyncStorageEngine().removePeriodicSync(account, userId, authority, |
| 433 | extras); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 434 | } finally { |
| 435 | restoreCallingIdentity(identityToken); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | public List<PeriodicSync> getPeriodicSyncs(Account account, String providerName) { |
| 440 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 441 | "no permission to read the sync settings"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 442 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 443 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 444 | long identityToken = clearCallingIdentity(); |
| 445 | try { |
| 446 | return getSyncManager().getSyncStorageEngine().getPeriodicSyncs( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 447 | account, userId, providerName); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 448 | } finally { |
| 449 | restoreCallingIdentity(identityToken); |
| 450 | } |
| 451 | } |
| 452 | |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 453 | public int getIsSyncable(Account account, String providerName) { |
| 454 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 455 | "no permission to read the sync settings"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 456 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 457 | |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 458 | long identityToken = clearCallingIdentity(); |
| 459 | try { |
| 460 | SyncManager syncManager = getSyncManager(); |
| 461 | if (syncManager != null) { |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 462 | return syncManager.getIsSyncable( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 463 | account, userId, providerName); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 464 | } |
| 465 | } finally { |
| 466 | restoreCallingIdentity(identityToken); |
| 467 | } |
| 468 | return -1; |
| 469 | } |
| 470 | |
| 471 | public void setIsSyncable(Account account, String providerName, int syncable) { |
| 472 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 473 | "no permission to write the sync settings"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 474 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 475 | |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 476 | long identityToken = clearCallingIdentity(); |
| 477 | try { |
| 478 | SyncManager syncManager = getSyncManager(); |
| 479 | if (syncManager != null) { |
| 480 | syncManager.getSyncStorageEngine().setIsSyncable( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 481 | account, userId, providerName, syncable); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 482 | } |
| 483 | } finally { |
| 484 | restoreCallingIdentity(identityToken); |
| 485 | } |
| 486 | } |
| 487 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 488 | public boolean getMasterSyncAutomatically() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 489 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 490 | "no permission to read the sync settings"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 491 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 492 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 493 | long identityToken = clearCallingIdentity(); |
| 494 | try { |
| 495 | SyncManager syncManager = getSyncManager(); |
| 496 | if (syncManager != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 497 | return syncManager.getSyncStorageEngine().getMasterSyncAutomatically(userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 498 | } |
| 499 | } finally { |
| 500 | restoreCallingIdentity(identityToken); |
| 501 | } |
| 502 | return false; |
| 503 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 504 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 505 | public void setMasterSyncAutomatically(boolean flag) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 506 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 507 | "no permission to write the sync settings"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 508 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 509 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 510 | long identityToken = clearCallingIdentity(); |
| 511 | try { |
| 512 | SyncManager syncManager = getSyncManager(); |
| 513 | if (syncManager != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 514 | syncManager.getSyncStorageEngine().setMasterSyncAutomatically(flag, userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 515 | } |
| 516 | } finally { |
| 517 | restoreCallingIdentity(identityToken); |
| 518 | } |
| 519 | } |
| 520 | |
Dianne Hackborn | 7a13559 | 2009-05-06 00:28:37 -0700 | [diff] [blame] | 521 | public boolean isSyncActive(Account account, String authority) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 522 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 523 | "no permission to read the sync stats"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 524 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 525 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 526 | long identityToken = clearCallingIdentity(); |
| 527 | try { |
| 528 | SyncManager syncManager = getSyncManager(); |
| 529 | if (syncManager != null) { |
| 530 | return syncManager.getSyncStorageEngine().isSyncActive( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 531 | account, userId, authority); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 532 | } |
| 533 | } finally { |
| 534 | restoreCallingIdentity(identityToken); |
| 535 | } |
| 536 | return false; |
| 537 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 538 | |
Fred Quintana | c6a6955 | 2010-09-27 17:05:04 -0700 | [diff] [blame] | 539 | public List<SyncInfo> getCurrentSyncs() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 540 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 541 | "no permission to read the sync stats"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 542 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 543 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 544 | long identityToken = clearCallingIdentity(); |
| 545 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 546 | return getSyncManager().getSyncStorageEngine().getCurrentSyncs(userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 547 | } finally { |
| 548 | restoreCallingIdentity(identityToken); |
| 549 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 550 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 551 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 552 | public SyncStatusInfo getSyncStatus(Account account, String authority) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 553 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 554 | "no permission to read the sync stats"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 555 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 556 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 557 | long identityToken = clearCallingIdentity(); |
| 558 | try { |
| 559 | SyncManager syncManager = getSyncManager(); |
| 560 | if (syncManager != null) { |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 561 | return syncManager.getSyncStorageEngine().getStatusByAccountAndAuthority( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 562 | account, userId, authority); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 563 | } |
| 564 | } finally { |
| 565 | restoreCallingIdentity(identityToken); |
| 566 | } |
| 567 | return null; |
| 568 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 569 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 570 | public boolean isSyncPending(Account account, String authority) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 571 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 572 | "no permission to read the sync stats"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 573 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 574 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 575 | long identityToken = clearCallingIdentity(); |
| 576 | try { |
| 577 | SyncManager syncManager = getSyncManager(); |
| 578 | if (syncManager != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 579 | return syncManager.getSyncStorageEngine().isSyncPending(account, userId, authority); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 580 | } |
| 581 | } finally { |
| 582 | restoreCallingIdentity(identityToken); |
| 583 | } |
| 584 | return false; |
| 585 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 586 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 587 | public void addStatusChangeListener(int mask, ISyncStatusObserver callback) { |
| 588 | long identityToken = clearCallingIdentity(); |
| 589 | try { |
| 590 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 591 | if (syncManager != null && callback != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 592 | syncManager.getSyncStorageEngine().addStatusChangeListener(mask, callback); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 593 | } |
| 594 | } finally { |
| 595 | restoreCallingIdentity(identityToken); |
| 596 | } |
| 597 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 598 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 599 | public void removeStatusChangeListener(ISyncStatusObserver callback) { |
| 600 | long identityToken = clearCallingIdentity(); |
| 601 | try { |
| 602 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 603 | if (syncManager != null && callback != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 604 | syncManager.getSyncStorageEngine().removeStatusChangeListener(callback); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 605 | } |
| 606 | } finally { |
| 607 | restoreCallingIdentity(identityToken); |
| 608 | } |
| 609 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 610 | |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 611 | public static ContentService main(Context context, boolean factoryTest) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | ContentService service = new ContentService(context, factoryTest); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 613 | ServiceManager.addService(ContentResolver.CONTENT_SERVICE_NAME, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | return service; |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Hide this class since it is not part of api, |
| 619 | * but current unittest framework requires it to be public |
| 620 | * @hide |
| 621 | */ |
| 622 | public static final class ObserverNode { |
| 623 | private class ObserverEntry implements IBinder.DeathRecipient { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 624 | public final IContentObserver observer; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 625 | public final int uid; |
| 626 | public final int pid; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 627 | public final boolean notifyForDescendants; |
| 628 | private final int userHandle; |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 629 | private final Object observersLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 631 | public ObserverEntry(IContentObserver o, boolean n, Object observersLock, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 632 | int _uid, int _pid, int _userHandle) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 633 | this.observersLock = observersLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | observer = o; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 635 | uid = _uid; |
| 636 | pid = _pid; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 637 | userHandle = _userHandle; |
| 638 | notifyForDescendants = n; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | try { |
| 640 | observer.asBinder().linkToDeath(this, 0); |
| 641 | } catch (RemoteException e) { |
| 642 | binderDied(); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | public void binderDied() { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 647 | synchronized (observersLock) { |
| 648 | removeObserverLocked(observer); |
| 649 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | } |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 651 | |
| 652 | public void dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 653 | String name, String prefix, SparseIntArray pidCounts) { |
| 654 | pidCounts.put(pid, pidCounts.get(pid)+1); |
| 655 | pw.print(prefix); pw.print(name); pw.print(": pid="); |
| 656 | pw.print(pid); pw.print(" uid="); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 657 | pw.print(uid); pw.print(" user="); |
| 658 | pw.print(userHandle); pw.print(" target="); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 659 | pw.println(Integer.toHexString(System.identityHashCode( |
| 660 | observer != null ? observer.asBinder() : null))); |
| 661 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | public static final int INSERT_TYPE = 0; |
| 665 | public static final int UPDATE_TYPE = 1; |
| 666 | public static final int DELETE_TYPE = 2; |
| 667 | |
| 668 | private String mName; |
| 669 | private ArrayList<ObserverNode> mChildren = new ArrayList<ObserverNode>(); |
| 670 | private ArrayList<ObserverEntry> mObservers = new ArrayList<ObserverEntry>(); |
| 671 | |
| 672 | public ObserverNode(String name) { |
| 673 | mName = name; |
| 674 | } |
| 675 | |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 676 | public void dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 677 | String name, String prefix, int[] counts, SparseIntArray pidCounts) { |
| 678 | String innerName = null; |
| 679 | if (mObservers.size() > 0) { |
| 680 | if ("".equals(name)) { |
| 681 | innerName = mName; |
| 682 | } else { |
| 683 | innerName = name + "/" + mName; |
| 684 | } |
| 685 | for (int i=0; i<mObservers.size(); i++) { |
| 686 | counts[1]++; |
| 687 | mObservers.get(i).dumpLocked(fd, pw, args, innerName, prefix, |
| 688 | pidCounts); |
| 689 | } |
| 690 | } |
| 691 | if (mChildren.size() > 0) { |
| 692 | if (innerName == null) { |
| 693 | if ("".equals(name)) { |
| 694 | innerName = mName; |
| 695 | } else { |
| 696 | innerName = name + "/" + mName; |
| 697 | } |
| 698 | } |
| 699 | for (int i=0; i<mChildren.size(); i++) { |
| 700 | counts[0]++; |
| 701 | mChildren.get(i).dumpLocked(fd, pw, args, innerName, prefix, |
| 702 | counts, pidCounts); |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | private String getUriSegment(Uri uri, int index) { |
| 708 | if (uri != null) { |
| 709 | if (index == 0) { |
| 710 | return uri.getAuthority(); |
| 711 | } else { |
| 712 | return uri.getPathSegments().get(index - 1); |
| 713 | } |
| 714 | } else { |
| 715 | return null; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | private int countUriSegments(Uri uri) { |
| 720 | if (uri == null) { |
| 721 | return 0; |
| 722 | } |
| 723 | return uri.getPathSegments().size() + 1; |
| 724 | } |
| 725 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 726 | // Invariant: userHandle is either a hard user number or is USER_ALL |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 727 | public void addObserverLocked(Uri uri, IContentObserver observer, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 728 | boolean notifyForDescendants, Object observersLock, |
| 729 | int uid, int pid, int userHandle) { |
| 730 | addObserverLocked(uri, 0, observer, notifyForDescendants, observersLock, |
| 731 | uid, pid, userHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | } |
| 733 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 734 | private void addObserverLocked(Uri uri, int index, IContentObserver observer, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 735 | boolean notifyForDescendants, Object observersLock, |
| 736 | int uid, int pid, int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | // If this is the leaf node add the observer |
| 738 | if (index == countUriSegments(uri)) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 739 | mObservers.add(new ObserverEntry(observer, notifyForDescendants, observersLock, |
| 740 | uid, pid, userHandle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | return; |
| 742 | } |
| 743 | |
| 744 | // Look to see if the proper child already exists |
| 745 | String segment = getUriSegment(uri, index); |
Anthony Newnam | f512664 | 2010-03-22 17:29:06 -0500 | [diff] [blame] | 746 | if (segment == null) { |
| 747 | throw new IllegalArgumentException("Invalid Uri (" + uri + ") used for observer"); |
| 748 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | int N = mChildren.size(); |
| 750 | for (int i = 0; i < N; i++) { |
| 751 | ObserverNode node = mChildren.get(i); |
| 752 | if (node.mName.equals(segment)) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 753 | node.addObserverLocked(uri, index + 1, observer, notifyForDescendants, |
| 754 | observersLock, uid, pid, userHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | return; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | // No child found, create one |
| 760 | ObserverNode node = new ObserverNode(segment); |
| 761 | mChildren.add(node); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 762 | node.addObserverLocked(uri, index + 1, observer, notifyForDescendants, |
| 763 | observersLock, uid, pid, userHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | } |
| 765 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 766 | public boolean removeObserverLocked(IContentObserver observer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | int size = mChildren.size(); |
| 768 | for (int i = 0; i < size; i++) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 769 | boolean empty = mChildren.get(i).removeObserverLocked(observer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | if (empty) { |
| 771 | mChildren.remove(i); |
| 772 | i--; |
| 773 | size--; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | IBinder observerBinder = observer.asBinder(); |
| 778 | size = mObservers.size(); |
| 779 | for (int i = 0; i < size; i++) { |
| 780 | ObserverEntry entry = mObservers.get(i); |
| 781 | if (entry.observer.asBinder() == observerBinder) { |
| 782 | mObservers.remove(i); |
| 783 | // We no longer need to listen for death notifications. Remove it. |
| 784 | observerBinder.unlinkToDeath(entry, 0); |
| 785 | break; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | if (mChildren.size() == 0 && mObservers.size() == 0) { |
| 790 | return true; |
| 791 | } |
| 792 | return false; |
| 793 | } |
| 794 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 795 | private void collectMyObserversLocked(boolean leaf, IContentObserver observer, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 796 | boolean observerWantsSelfNotifications, int targetUserHandle, |
| 797 | ArrayList<ObserverCall> calls) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | int N = mObservers.size(); |
| 799 | IBinder observerBinder = observer == null ? null : observer.asBinder(); |
| 800 | for (int i = 0; i < N; i++) { |
| 801 | ObserverEntry entry = mObservers.get(i); |
| 802 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 803 | // Don't notify the observer if it sent the notification and isn't interested |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | // in self notifications |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 805 | boolean selfChange = (entry.observer.asBinder() == observerBinder); |
| 806 | if (selfChange && !observerWantsSelfNotifications) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | continue; |
| 808 | } |
| 809 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 810 | // Does this observer match the target user? |
| 811 | if (targetUserHandle == UserHandle.USER_ALL |
| 812 | || entry.userHandle == UserHandle.USER_ALL |
| 813 | || targetUserHandle == entry.userHandle) { |
| 814 | // Make sure the observer is interested in the notification |
| 815 | if (leaf || (!leaf && entry.notifyForDescendants)) { |
| 816 | calls.add(new ObserverCall(this, entry.observer, selfChange)); |
| 817 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 822 | /** |
| 823 | * targetUserHandle is either a hard user handle or is USER_ALL |
| 824 | */ |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 825 | public void collectObserversLocked(Uri uri, int index, IContentObserver observer, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 826 | boolean observerWantsSelfNotifications, int targetUserHandle, |
| 827 | ArrayList<ObserverCall> calls) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | String segment = null; |
| 829 | int segmentCount = countUriSegments(uri); |
| 830 | if (index >= segmentCount) { |
| 831 | // This is the leaf node, notify all observers |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 832 | collectMyObserversLocked(true, observer, observerWantsSelfNotifications, |
| 833 | targetUserHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | } else if (index < segmentCount){ |
| 835 | segment = getUriSegment(uri, index); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 836 | // Notify any observers at this level who are interested in descendants |
| 837 | collectMyObserversLocked(false, observer, observerWantsSelfNotifications, |
| 838 | targetUserHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | int N = mChildren.size(); |
| 842 | for (int i = 0; i < N; i++) { |
| 843 | ObserverNode node = mChildren.get(i); |
| 844 | if (segment == null || node.mName.equals(segment)) { |
| 845 | // We found the child, |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 846 | node.collectObserversLocked(uri, index + 1, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 847 | observer, observerWantsSelfNotifications, targetUserHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | if (segment != null) { |
| 849 | break; |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | } |