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