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; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
| 25 | import android.content.IContentService; |
| 26 | import android.content.ISyncStatusObserver; |
| 27 | import android.content.PeriodicSync; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 29 | import android.content.SyncAdapterType; |
| 30 | import android.content.SyncInfo; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 31 | import android.content.SyncRequest; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 32 | import android.content.SyncStatusInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.database.IContentObserver; |
| 34 | import android.database.sqlite.SQLiteException; |
| 35 | import android.net.Uri; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 36 | import android.os.Binder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.os.Bundle; |
| 38 | import android.os.IBinder; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 39 | import android.os.Parcel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.os.RemoteException; |
| 41 | import android.os.ServiceManager; |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 42 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 43 | import android.os.UserHandle; |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 44 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.util.Log; |
Jeff Sharkey | 6009459 | 2013-03-21 18:14:24 -0700 | [diff] [blame] | 46 | import android.util.Slog; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 47 | import android.util.SparseIntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
| 49 | import java.io.FileDescriptor; |
| 50 | import java.io.PrintWriter; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 51 | import java.security.InvalidParameterException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import java.util.ArrayList; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 53 | import java.util.Collections; |
| 54 | import java.util.Comparator; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 55 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * {@hide} |
| 59 | */ |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 60 | public final class ContentService extends IContentService.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | private static final String TAG = "ContentService"; |
| 62 | private Context mContext; |
| 63 | private boolean mFactoryTest; |
| 64 | private final ObserverNode mRootNode = new ObserverNode(""); |
| 65 | private SyncManager mSyncManager = null; |
| 66 | private final Object mSyncManagerLock = new Object(); |
| 67 | |
| 68 | private SyncManager getSyncManager() { |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 69 | if (SystemProperties.getBoolean("config.disable_network", false)) { |
| 70 | return null; |
| 71 | } |
| 72 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | synchronized(mSyncManagerLock) { |
| 74 | try { |
| 75 | // Try to create the SyncManager, return null if it fails (e.g. the disk is full). |
| 76 | if (mSyncManager == null) mSyncManager = new SyncManager(mContext, mFactoryTest); |
| 77 | } catch (SQLiteException e) { |
| 78 | Log.e(TAG, "Can't create SyncManager", e); |
| 79 | } |
| 80 | return mSyncManager; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 86 | mContext.enforceCallingOrSelfPermission(Manifest.permission.DUMP, |
| 87 | "caller doesn't have the DUMP permission"); |
| 88 | |
| 89 | // This makes it so that future permission checks will be in the context of this |
| 90 | // process rather than the caller's process. We will restore this before returning. |
| 91 | long identityToken = clearCallingIdentity(); |
| 92 | try { |
| 93 | if (mSyncManager == null) { |
| 94 | pw.println("No SyncManager created! (Disk full?)"); |
| 95 | } else { |
| 96 | mSyncManager.dump(fd, pw); |
| 97 | } |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 98 | pw.println(); |
| 99 | pw.println("Observer tree:"); |
| 100 | synchronized (mRootNode) { |
| 101 | int[] counts = new int[2]; |
| 102 | final SparseIntArray pidCounts = new SparseIntArray(); |
| 103 | mRootNode.dumpLocked(fd, pw, args, "", " ", counts, pidCounts); |
| 104 | pw.println(); |
| 105 | ArrayList<Integer> sorted = new ArrayList<Integer>(); |
| 106 | for (int i=0; i<pidCounts.size(); i++) { |
| 107 | sorted.add(pidCounts.keyAt(i)); |
| 108 | } |
| 109 | Collections.sort(sorted, new Comparator<Integer>() { |
| 110 | @Override |
| 111 | public int compare(Integer lhs, Integer rhs) { |
| 112 | int lc = pidCounts.get(lhs); |
| 113 | int rc = pidCounts.get(rhs); |
| 114 | if (lc < rc) { |
| 115 | return 1; |
| 116 | } else if (lc > rc) { |
| 117 | return -1; |
| 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | }); |
| 123 | for (int i=0; i<sorted.size(); i++) { |
| 124 | int pid = sorted.get(i); |
| 125 | pw.print(" pid "); pw.print(pid); pw.print(": "); |
| 126 | pw.print(pidCounts.get(pid)); pw.println(" observers"); |
| 127 | } |
| 128 | pw.println(); |
| 129 | pw.print(" Total number of nodes: "); pw.println(counts[0]); |
| 130 | pw.print(" Total number of observers: "); pw.println(counts[1]); |
| 131 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | } finally { |
| 133 | restoreCallingIdentity(identityToken); |
| 134 | } |
| 135 | } |
| 136 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 139 | throws RemoteException { |
| 140 | try { |
| 141 | return super.onTransact(code, data, reply, flags); |
| 142 | } catch (RuntimeException e) { |
| 143 | // The content service only throws security exceptions, so let's |
| 144 | // log all others. |
| 145 | if (!(e instanceof SecurityException)) { |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 146 | Slog.wtf(TAG, "Content Service Crash", e); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 147 | } |
| 148 | throw e; |
| 149 | } |
| 150 | } |
| 151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | /*package*/ ContentService(Context context, boolean factoryTest) { |
| 153 | mContext = context; |
| 154 | mFactoryTest = factoryTest; |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | public void systemReady() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | getSyncManager(); |
| 159 | } |
| 160 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 161 | /** |
| 162 | * Register a content observer tied to a specific user's view of the provider. |
| 163 | * @param userHandle the user whose view of the provider is to be observed. May be |
| 164 | * the calling user without requiring any permission, otherwise the caller needs to |
| 165 | * hold the INTERACT_ACROSS_USERS_FULL permission. Pseudousers USER_ALL and |
| 166 | * USER_CURRENT are properly handled; all other pseudousers are forbidden. |
| 167 | */ |
| 168 | @Override |
| 169 | public void registerContentObserver(Uri uri, boolean notifyForDescendants, |
| 170 | IContentObserver observer, int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | if (observer == null || uri == null) { |
| 172 | throw new IllegalArgumentException("You must pass a valid uri and observer"); |
| 173 | } |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 174 | |
| 175 | final int callingUser = UserHandle.getCallingUserId(); |
| 176 | if (callingUser != userHandle) { |
| 177 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 178 | "no permission to observe other users' provider view"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 180 | |
| 181 | if (userHandle < 0) { |
| 182 | if (userHandle == UserHandle.USER_CURRENT) { |
| 183 | userHandle = ActivityManager.getCurrentUser(); |
| 184 | } else if (userHandle != UserHandle.USER_ALL) { |
| 185 | throw new InvalidParameterException("Bad user handle for registerContentObserver: " |
| 186 | + userHandle); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | synchronized (mRootNode) { |
| 191 | mRootNode.addObserverLocked(uri, observer, notifyForDescendants, mRootNode, |
| 192 | Binder.getCallingUid(), Binder.getCallingPid(), userHandle); |
| 193 | if (false) Log.v(TAG, "Registered observer " + observer + " at " + uri + |
| 194 | " with notifyForDescendants " + notifyForDescendants); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | public void registerContentObserver(Uri uri, boolean notifyForDescendants, |
| 199 | IContentObserver observer) { |
| 200 | registerContentObserver(uri, notifyForDescendants, observer, |
| 201 | UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | public void unregisterContentObserver(IContentObserver observer) { |
| 205 | if (observer == null) { |
| 206 | throw new IllegalArgumentException("You must pass a valid observer"); |
| 207 | } |
| 208 | synchronized (mRootNode) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 209 | mRootNode.removeObserverLocked(observer); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 210 | if (false) Log.v(TAG, "Unregistered observer " + observer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Notify observers of a particular user's view of the provider. |
| 216 | * @param userHandle the user whose view of the provider is to be notified. May be |
| 217 | * the calling user without requiring any permission, otherwise the caller needs to |
| 218 | * hold the INTERACT_ACROSS_USERS_FULL permission. Pseudousers USER_ALL and |
| 219 | * USER_CURRENT are properly interpreted; no other pseudousers are allowed. |
| 220 | */ |
| 221 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | public void notifyChange(Uri uri, IContentObserver observer, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 223 | boolean observerWantsSelfNotifications, boolean syncToNetwork, |
| 224 | int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 226 | Log.v(TAG, "Notifying update of " + uri + " for user " + userHandle |
| 227 | + " from observer " + observer + ", syncToNetwork " + syncToNetwork); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 229 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 230 | // Notify for any user other than the caller's own requires permission. |
| 231 | final int callingUserHandle = UserHandle.getCallingUserId(); |
| 232 | if (userHandle != callingUserHandle) { |
| 233 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 234 | "no permission to notify other users"); |
| 235 | } |
| 236 | |
| 237 | // We passed the permission check; resolve pseudouser targets as appropriate |
| 238 | if (userHandle < 0) { |
| 239 | if (userHandle == UserHandle.USER_CURRENT) { |
| 240 | userHandle = ActivityManager.getCurrentUser(); |
| 241 | } else if (userHandle != UserHandle.USER_ALL) { |
| 242 | throw new InvalidParameterException("Bad user handle for notifyChange: " |
| 243 | + userHandle); |
| 244 | } |
| 245 | } |
| 246 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 247 | final int uid = Binder.getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | // This makes it so that future permission checks will be in the context of this |
| 249 | // process rather than the caller's process. We will restore this before returning. |
| 250 | long identityToken = clearCallingIdentity(); |
| 251 | try { |
| 252 | ArrayList<ObserverCall> calls = new ArrayList<ObserverCall>(); |
| 253 | synchronized (mRootNode) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 254 | mRootNode.collectObserversLocked(uri, 0, observer, observerWantsSelfNotifications, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 255 | userHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | } |
| 257 | final int numCalls = calls.size(); |
| 258 | for (int i=0; i<numCalls; i++) { |
| 259 | ObserverCall oc = calls.get(i); |
| 260 | try { |
Jeff Brown | 655e66b | 2012-01-23 15:51:41 -0800 | [diff] [blame] | 261 | oc.mObserver.onChange(oc.mSelfChange, uri); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 263 | Log.v(TAG, "Notified " + oc.mObserver + " of " + "update at " + uri); |
| 264 | } |
| 265 | } catch (RemoteException ex) { |
| 266 | synchronized (mRootNode) { |
| 267 | Log.w(TAG, "Found dead observer, removing"); |
| 268 | IBinder binder = oc.mObserver.asBinder(); |
| 269 | final ArrayList<ObserverNode.ObserverEntry> list |
| 270 | = oc.mNode.mObservers; |
| 271 | int numList = list.size(); |
| 272 | for (int j=0; j<numList; j++) { |
| 273 | ObserverNode.ObserverEntry oe = list.get(j); |
| 274 | if (oe.observer.asBinder() == binder) { |
| 275 | list.remove(j); |
| 276 | j--; |
| 277 | numList--; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | if (syncToNetwork) { |
| 284 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 285 | if (syncManager != null) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 286 | syncManager.scheduleLocalSync(null /* all accounts */, callingUserHandle, uid, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 287 | uri.getAuthority()); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 288 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | } |
| 290 | } finally { |
| 291 | restoreCallingIdentity(identityToken); |
| 292 | } |
| 293 | } |
| 294 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 295 | public void notifyChange(Uri uri, IContentObserver observer, |
| 296 | boolean observerWantsSelfNotifications, boolean syncToNetwork) { |
| 297 | notifyChange(uri, observer, observerWantsSelfNotifications, syncToNetwork, |
| 298 | UserHandle.getCallingUserId()); |
| 299 | } |
| 300 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | /** |
| 302 | * Hide this class since it is not part of api, |
| 303 | * but current unittest framework requires it to be public |
| 304 | * @hide |
| 305 | * |
| 306 | */ |
| 307 | public static final class ObserverCall { |
| 308 | final ObserverNode mNode; |
| 309 | final IContentObserver mObserver; |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 310 | final boolean mSelfChange; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 312 | ObserverCall(ObserverNode node, IContentObserver observer, boolean selfChange) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | mNode = node; |
| 314 | mObserver = observer; |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 315 | mSelfChange = selfChange; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 319 | public void requestSync(Account account, String authority, Bundle extras) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | ContentResolver.validateSyncExtrasBundle(extras); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 321 | int userId = UserHandle.getCallingUserId(); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 322 | int uId = Binder.getCallingUid(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | // This makes it so that future permission checks will be in the context of this |
| 325 | // process rather than the caller's process. We will restore this before returning. |
| 326 | long identityToken = clearCallingIdentity(); |
| 327 | try { |
| 328 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 329 | if (syncManager != null) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 330 | syncManager.scheduleSync(account, userId, uId, authority, extras, |
| 331 | 0 /* no delay */, 0 /* no delay */, |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 332 | false /* onlyThoseWithUnkownSyncableState */); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 333 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | } finally { |
| 335 | restoreCallingIdentity(identityToken); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 340 | * Request a sync with a generic {@link android.content.SyncRequest} object. This will be |
| 341 | * either: |
| 342 | * periodic OR one-off sync. |
| 343 | * and |
| 344 | * anonymous OR provider sync. |
| 345 | * Depending on the request, we enqueue to suit in the SyncManager. |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 346 | * @param request The request object. Validation of this object is done by its builder. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 347 | */ |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 348 | public void sync(SyncRequest request) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 349 | int userId = UserHandle.getCallingUserId(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 350 | int callerUid = Binder.getCallingUid(); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 351 | // This makes it so that future permission checks will be in the context of this |
| 352 | // process rather than the caller's process. We will restore this before returning. |
| 353 | long identityToken = clearCallingIdentity(); |
| 354 | try { |
| 355 | SyncManager syncManager = getSyncManager(); |
Matthew Williams | d08d668 | 2013-10-14 10:39:41 -0700 | [diff] [blame] | 356 | if (syncManager == null) { |
| 357 | return; |
| 358 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 359 | |
Matthew Williams | d08d668 | 2013-10-14 10:39:41 -0700 | [diff] [blame] | 360 | Bundle extras = request.getBundle(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 361 | long flextime = request.getSyncFlexTime(); |
| 362 | long runAtTime = request.getSyncRunTime(); |
| 363 | if (request.isPeriodic()) { |
| 364 | mContext.enforceCallingOrSelfPermission( |
| 365 | Manifest.permission.WRITE_SYNC_SETTINGS, |
| 366 | "no permission to write the sync settings"); |
| 367 | SyncStorageEngine.EndPoint info; |
| 368 | if (!request.hasAuthority()) { |
| 369 | // Extra permissions checking for sync service. |
| 370 | verifySignatureForPackage(callerUid, |
| 371 | request.getService().getPackageName(), "sync"); |
| 372 | info = new SyncStorageEngine.EndPoint(request.getService(), userId); |
Matthew Williams | 96ca46c | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 373 | } else { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 374 | info = new SyncStorageEngine.EndPoint( |
| 375 | request.getAccount(), request.getProvider(), userId); |
| 376 | } |
| 377 | if (runAtTime < 60) { |
| 378 | Slog.w(TAG, "Requested poll frequency of " + runAtTime |
| 379 | + " seconds being rounded up to 60 seconds."); |
| 380 | runAtTime = 60; |
| 381 | } |
| 382 | // Schedule periodic sync. |
| 383 | getSyncManager().getSyncStorageEngine() |
| 384 | .updateOrAddPeriodicSync(info, runAtTime, flextime, extras); |
| 385 | } else { |
| 386 | long beforeRuntimeMillis = (flextime) * 1000; |
| 387 | long runtimeMillis = runAtTime * 1000; |
| 388 | if (request.hasAuthority()) { |
| 389 | syncManager.scheduleSync( |
| 390 | request.getAccount(), userId, callerUid, request.getProvider(), extras, |
| 391 | beforeRuntimeMillis, runtimeMillis, |
| 392 | false /* onlyThoseWithUnknownSyncableState */); |
| 393 | } else { |
| 394 | syncManager.scheduleSync( |
| 395 | request.getService(), userId, callerUid, extras, |
| 396 | beforeRuntimeMillis, |
| 397 | runtimeMillis); // Empty function. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | } finally { |
| 401 | restoreCallingIdentity(identityToken); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | * 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] | 407 | * if they match the authority and account, if they are present. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 408 | * |
| 409 | * @param account filter the pending and active syncs to cancel using this account, or null. |
| 410 | * @param authority filter the pending and active syncs to cancel using this authority, or |
| 411 | * null. |
| 412 | * @param cname cancel syncs running on this service, or null for provider/account. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | */ |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 414 | @Override |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 415 | public void cancelSync(Account account, String authority, ComponentName cname) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 416 | if (authority != null && authority.length() == 0) { |
| 417 | throw new IllegalArgumentException("Authority must be non-empty"); |
| 418 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 419 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 420 | int userId = UserHandle.getCallingUserId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | // This makes it so that future permission checks will be in the context of this |
| 422 | // process rather than the caller's process. We will restore this before returning. |
| 423 | long identityToken = clearCallingIdentity(); |
| 424 | try { |
| 425 | SyncManager syncManager = getSyncManager(); |
| 426 | if (syncManager != null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 427 | SyncStorageEngine.EndPoint info; |
| 428 | if (cname == null) { |
| 429 | info = new SyncStorageEngine.EndPoint(account, authority, userId); |
| 430 | } else { |
| 431 | info = new SyncStorageEngine.EndPoint(cname, userId); |
| 432 | } |
| 433 | syncManager.clearScheduledSyncOperations(info); |
| 434 | syncManager.cancelActiveSync(info, null /* all syncs for this adapter */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
| 436 | } finally { |
| 437 | restoreCallingIdentity(identityToken); |
| 438 | } |
| 439 | } |
| 440 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 441 | public void cancelRequest(SyncRequest request) { |
| 442 | SyncManager syncManager = getSyncManager(); |
| 443 | if (syncManager == null) return; |
| 444 | int userId = UserHandle.getCallingUserId(); |
| 445 | int callerUid = Binder.getCallingUid(); |
| 446 | |
| 447 | long identityToken = clearCallingIdentity(); |
| 448 | try { |
| 449 | SyncStorageEngine.EndPoint info; |
| 450 | Bundle extras = new Bundle(request.getBundle()); |
| 451 | if (request.hasAuthority()) { |
| 452 | Account account = request.getAccount(); |
| 453 | String provider = request.getProvider(); |
| 454 | info = new SyncStorageEngine.EndPoint(account, provider, userId); |
| 455 | } else { |
| 456 | // Only allowed to manipulate syncs for a service which you own. |
| 457 | ComponentName service = request.getService(); |
| 458 | verifySignatureForPackage(callerUid, service.getPackageName(), "cancel"); |
| 459 | info = new SyncStorageEngine.EndPoint(service, userId); |
| 460 | } |
| 461 | if (request.isPeriodic()) { |
| 462 | // Remove periodic sync. |
| 463 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 464 | "no permission to write the sync settings"); |
| 465 | getSyncManager().getSyncStorageEngine().removePeriodicSync(info, extras); |
| 466 | } |
| 467 | // Cancel active syncs and clear pending syncs from the queue. |
| 468 | syncManager.cancelScheduledSyncOperation(info, extras); |
| 469 | syncManager.cancelActiveSync(info, extras); |
| 470 | } finally { |
| 471 | restoreCallingIdentity(identityToken); |
| 472 | } |
| 473 | } |
| 474 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 475 | /** |
| 476 | * Get information about the SyncAdapters that are known to the system. |
| 477 | * @return an array of SyncAdapters that have registered with the system |
| 478 | */ |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 479 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 480 | public SyncAdapterType[] getSyncAdapterTypes() { |
| 481 | // This makes it so that future permission checks will be in the context of this |
| 482 | // 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] | 483 | final int userId = UserHandle.getCallingUserId(); |
| 484 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 485 | try { |
| 486 | SyncManager syncManager = getSyncManager(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 487 | return syncManager.getSyncAdapterTypes(userId); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 488 | } finally { |
| 489 | restoreCallingIdentity(identityToken); |
| 490 | } |
| 491 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 492 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 493 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 494 | public boolean getSyncAutomatically(Account account, String providerName) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 495 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 496 | "no permission to read the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 497 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 498 | int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 499 | long identityToken = clearCallingIdentity(); |
| 500 | try { |
| 501 | SyncManager syncManager = getSyncManager(); |
| 502 | if (syncManager != null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 503 | return syncManager.getSyncStorageEngine() |
| 504 | .getSyncAutomatically(account, userId, providerName); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 505 | } |
| 506 | } finally { |
| 507 | restoreCallingIdentity(identityToken); |
| 508 | } |
| 509 | return false; |
| 510 | } |
| 511 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 512 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 513 | public void setSyncAutomatically(Account account, String providerName, boolean sync) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 514 | if (TextUtils.isEmpty(providerName)) { |
| 515 | throw new IllegalArgumentException("Authority must be non-empty"); |
| 516 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 517 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 518 | "no permission to write the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 519 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 520 | int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 521 | long identityToken = clearCallingIdentity(); |
| 522 | try { |
| 523 | SyncManager syncManager = getSyncManager(); |
| 524 | if (syncManager != null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 525 | syncManager.getSyncStorageEngine() |
| 526 | .setSyncAutomatically(account, userId, providerName, sync); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 527 | } |
| 528 | } finally { |
| 529 | restoreCallingIdentity(identityToken); |
| 530 | } |
| 531 | } |
| 532 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 533 | /** Old API. Schedule periodic sync with default flex time. */ |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 534 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 535 | public void addPeriodicSync(Account account, String authority, Bundle extras, |
| 536 | long pollFrequency) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 537 | if (account == null) { |
| 538 | throw new IllegalArgumentException("Account must not be null"); |
| 539 | } |
| 540 | if (TextUtils.isEmpty(authority)) { |
| 541 | throw new IllegalArgumentException("Authority must not be empty."); |
| 542 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 543 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 544 | "no permission to write the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 545 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 546 | int userId = UserHandle.getCallingUserId(); |
Jeff Sharkey | 51366be | 2013-03-27 14:46:55 -0700 | [diff] [blame] | 547 | if (pollFrequency < 60) { |
Jeff Sharkey | 6009459 | 2013-03-21 18:14:24 -0700 | [diff] [blame] | 548 | Slog.w(TAG, "Requested poll frequency of " + pollFrequency |
Jeff Sharkey | 51366be | 2013-03-27 14:46:55 -0700 | [diff] [blame] | 549 | + " seconds being rounded up to 60 seconds."); |
| 550 | pollFrequency = 60; |
Jeff Sharkey | 6009459 | 2013-03-21 18:14:24 -0700 | [diff] [blame] | 551 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 552 | long defaultFlex = SyncStorageEngine.calculateDefaultFlexTime(pollFrequency); |
Jeff Sharkey | 6009459 | 2013-03-21 18:14:24 -0700 | [diff] [blame] | 553 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 554 | long identityToken = clearCallingIdentity(); |
| 555 | try { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 556 | SyncStorageEngine.EndPoint info = |
| 557 | new SyncStorageEngine.EndPoint(account, authority, userId); |
| 558 | getSyncManager().getSyncStorageEngine() |
| 559 | .updateOrAddPeriodicSync(info, |
| 560 | pollFrequency, |
| 561 | defaultFlex, |
| 562 | extras); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 563 | } finally { |
| 564 | restoreCallingIdentity(identityToken); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | public void removePeriodicSync(Account account, String authority, Bundle extras) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 569 | if (account == null) { |
| 570 | throw new IllegalArgumentException("Account must not be null"); |
| 571 | } |
| 572 | if (TextUtils.isEmpty(authority)) { |
| 573 | throw new IllegalArgumentException("Authority must not be empty"); |
| 574 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 575 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 576 | "no permission to write the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 577 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 578 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 579 | long identityToken = clearCallingIdentity(); |
| 580 | try { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 581 | getSyncManager().getSyncStorageEngine() |
| 582 | .removePeriodicSync( |
| 583 | new SyncStorageEngine.EndPoint(account, authority, userId), |
| 584 | extras); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 585 | } finally { |
| 586 | restoreCallingIdentity(identityToken); |
| 587 | } |
| 588 | } |
| 589 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 590 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 591 | public List<PeriodicSync> getPeriodicSyncs(Account account, String providerName, |
| 592 | ComponentName cname) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 593 | if (account == null) { |
| 594 | throw new IllegalArgumentException("Account must not be null"); |
| 595 | } |
| 596 | if (TextUtils.isEmpty(providerName)) { |
| 597 | throw new IllegalArgumentException("Authority must not be empty"); |
| 598 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 599 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 600 | "no permission to read the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 601 | |
Matthew Williams | d08d668 | 2013-10-14 10:39:41 -0700 | [diff] [blame] | 602 | int callerUid = Binder.getCallingUid(); |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 603 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 604 | long identityToken = clearCallingIdentity(); |
| 605 | try { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 606 | if (cname == null) { |
| 607 | return getSyncManager().getSyncStorageEngine().getPeriodicSyncs( |
| 608 | new SyncStorageEngine.EndPoint(account, providerName, userId)); |
| 609 | } else if (account == null && providerName == null) { |
| 610 | verifySignatureForPackage(callerUid, cname.getPackageName(), "getPeriodicSyncs"); |
| 611 | return getSyncManager().getSyncStorageEngine().getPeriodicSyncs( |
| 612 | new SyncStorageEngine.EndPoint(cname, userId)); |
| 613 | } else { |
| 614 | throw new IllegalArgumentException("Invalid authority specified"); |
| 615 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 616 | } finally { |
| 617 | restoreCallingIdentity(identityToken); |
| 618 | } |
| 619 | } |
| 620 | |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 621 | public int getIsSyncable(Account account, String providerName) { |
| 622 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 623 | "no permission to read the sync settings"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 624 | int userId = UserHandle.getCallingUserId(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 625 | |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 626 | long identityToken = clearCallingIdentity(); |
| 627 | try { |
| 628 | SyncManager syncManager = getSyncManager(); |
| 629 | if (syncManager != null) { |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 630 | return syncManager.getIsSyncable( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 631 | account, userId, providerName); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 632 | } |
| 633 | } finally { |
| 634 | restoreCallingIdentity(identityToken); |
| 635 | } |
| 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | public void setIsSyncable(Account account, String providerName, int syncable) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 640 | if (TextUtils.isEmpty(providerName)) { |
| 641 | throw new IllegalArgumentException("Authority must not be empty"); |
| 642 | } |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 643 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 644 | "no permission to write the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 645 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 646 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 647 | long identityToken = clearCallingIdentity(); |
| 648 | try { |
| 649 | SyncManager syncManager = getSyncManager(); |
| 650 | if (syncManager != null) { |
| 651 | syncManager.getSyncStorageEngine().setIsSyncable( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 652 | account, userId, providerName, syncable); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 653 | } |
| 654 | } finally { |
| 655 | restoreCallingIdentity(identityToken); |
| 656 | } |
| 657 | } |
| 658 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 659 | public void setServiceActive(ComponentName cname, boolean active) { |
| 660 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 661 | "no permission to write the sync settings"); |
Matthew Williams | 7a2ab3a | 2013-09-11 14:25:51 -0700 | [diff] [blame] | 662 | verifySignatureForPackage(Binder.getCallingUid(), cname.getPackageName(), |
| 663 | "setServiceActive"); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 664 | |
| 665 | int userId = UserHandle.getCallingUserId(); |
| 666 | long identityToken = clearCallingIdentity(); |
| 667 | try { |
| 668 | SyncManager syncManager = getSyncManager(); |
| 669 | if (syncManager != null) { |
Matthew Williams | 7a2ab3a | 2013-09-11 14:25:51 -0700 | [diff] [blame] | 670 | syncManager.getSyncStorageEngine().setIsTargetServiceActive( |
| 671 | cname, userId, active); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 672 | } |
| 673 | } finally { |
| 674 | restoreCallingIdentity(identityToken); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | public boolean isServiceActive(ComponentName cname) { |
| 679 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 680 | "no permission to read the sync settings"); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 681 | verifySignatureForPackage(Binder.getCallingUid(), cname.getPackageName(), |
| 682 | "isServiceActive"); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 683 | |
| 684 | int userId = UserHandle.getCallingUserId(); |
| 685 | long identityToken = clearCallingIdentity(); |
| 686 | try { |
| 687 | SyncManager syncManager = getSyncManager(); |
| 688 | if (syncManager != null) { |
Matthew Williams | 7a2ab3a | 2013-09-11 14:25:51 -0700 | [diff] [blame] | 689 | return syncManager.getSyncStorageEngine() |
| 690 | .getIsTargetServiceActive(cname, userId); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 691 | } |
| 692 | } finally { |
| 693 | restoreCallingIdentity(identityToken); |
| 694 | } |
| 695 | return false; |
| 696 | } |
| 697 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 698 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 699 | public boolean getMasterSyncAutomatically() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 700 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 701 | "no permission to read the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 702 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 703 | int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 704 | long identityToken = clearCallingIdentity(); |
| 705 | try { |
| 706 | SyncManager syncManager = getSyncManager(); |
| 707 | if (syncManager != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 708 | return syncManager.getSyncStorageEngine().getMasterSyncAutomatically(userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 709 | } |
| 710 | } finally { |
| 711 | restoreCallingIdentity(identityToken); |
| 712 | } |
| 713 | return false; |
| 714 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 715 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 716 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 717 | public void setMasterSyncAutomatically(boolean flag) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 718 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 719 | "no permission to write the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 720 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 721 | int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 722 | long identityToken = clearCallingIdentity(); |
| 723 | try { |
| 724 | SyncManager syncManager = getSyncManager(); |
| 725 | if (syncManager != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 726 | syncManager.getSyncStorageEngine().setMasterSyncAutomatically(flag, userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 727 | } |
| 728 | } finally { |
| 729 | restoreCallingIdentity(identityToken); |
| 730 | } |
| 731 | } |
| 732 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 733 | public boolean isSyncActive(Account account, String authority, ComponentName cname) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 734 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 735 | "no permission to read the sync stats"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 736 | int userId = UserHandle.getCallingUserId(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 737 | int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 738 | long identityToken = clearCallingIdentity(); |
| 739 | try { |
| 740 | SyncManager syncManager = getSyncManager(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 741 | if (syncManager == null) { |
| 742 | return false; |
| 743 | } |
| 744 | if (cname == null) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 745 | return syncManager.getSyncStorageEngine().isSyncActive( |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 746 | new SyncStorageEngine.EndPoint(account, authority, userId)); |
| 747 | } else if (account == null && authority == null) { |
| 748 | verifySignatureForPackage(callingUid, cname.getPackageName(), "isSyncActive"); |
| 749 | return syncManager.getSyncStorageEngine().isSyncActive( |
| 750 | new SyncStorageEngine.EndPoint(cname, userId)); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 751 | } |
| 752 | } finally { |
| 753 | restoreCallingIdentity(identityToken); |
| 754 | } |
| 755 | return false; |
| 756 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 757 | |
Fred Quintana | c6a6955 | 2010-09-27 17:05:04 -0700 | [diff] [blame] | 758 | public List<SyncInfo> getCurrentSyncs() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 759 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 760 | "no permission to read the sync stats"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 761 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 762 | int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 763 | long identityToken = clearCallingIdentity(); |
| 764 | try { |
Matthew Williams | a7456e4 | 2013-11-12 14:41:02 -0800 | [diff] [blame] | 765 | return getSyncManager().getSyncStorageEngine().getCurrentSyncsCopy(userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 766 | } finally { |
| 767 | restoreCallingIdentity(identityToken); |
| 768 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 769 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 770 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 771 | public SyncStatusInfo getSyncStatus(Account account, String authority, ComponentName cname) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 772 | if (TextUtils.isEmpty(authority)) { |
| 773 | throw new IllegalArgumentException("Authority must not be empty"); |
| 774 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 775 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 776 | "no permission to read the sync stats"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 777 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 778 | int userId = UserHandle.getCallingUserId(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 779 | int callerUid = Binder.getCallingUid(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 780 | long identityToken = clearCallingIdentity(); |
| 781 | try { |
| 782 | SyncManager syncManager = getSyncManager(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 783 | if (syncManager == null) { |
| 784 | return null; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 785 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 786 | SyncStorageEngine.EndPoint info; |
| 787 | if (cname == null) { |
| 788 | info = new SyncStorageEngine.EndPoint(account, authority, userId); |
| 789 | } else if (account == null && authority == null) { |
| 790 | verifySignatureForPackage(callerUid, cname.getPackageName(), "getSyncStatus"); |
| 791 | info = new SyncStorageEngine.EndPoint(cname, userId); |
| 792 | } else { |
| 793 | throw new IllegalArgumentException("Must call sync status with valid authority"); |
| 794 | } |
| 795 | return syncManager.getSyncStorageEngine().getStatusByAuthority(info); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 796 | } finally { |
| 797 | restoreCallingIdentity(identityToken); |
| 798 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 799 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 800 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 801 | public boolean isSyncPending(Account account, String authority, ComponentName cname) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 802 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 803 | "no permission to read the sync stats"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 804 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 805 | int callerUid = Binder.getCallingUid(); |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 806 | int userId = UserHandle.getCallingUserId(); |
Torne (Richard Coles) | 4890b08 | 2013-08-12 10:26:57 +0000 | [diff] [blame] | 807 | long identityToken = clearCallingIdentity(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 808 | SyncManager syncManager = getSyncManager(); |
| 809 | if (syncManager == null) return false; |
| 810 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 811 | try { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 812 | SyncStorageEngine.EndPoint info; |
| 813 | if (cname == null) { |
| 814 | info = new SyncStorageEngine.EndPoint(account, authority, userId); |
| 815 | } else if (account == null && authority == null) { |
| 816 | verifySignatureForPackage(callerUid, cname.getPackageName(), "isSyncPending"); |
| 817 | info = new SyncStorageEngine.EndPoint(cname, userId); |
| 818 | } else { |
| 819 | throw new IllegalArgumentException("Invalid authority specified"); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 820 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 821 | return syncManager.getSyncStorageEngine().isSyncPending(info); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 822 | } finally { |
| 823 | restoreCallingIdentity(identityToken); |
| 824 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 825 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 826 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 827 | public void addStatusChangeListener(int mask, ISyncStatusObserver callback) { |
| 828 | long identityToken = clearCallingIdentity(); |
| 829 | try { |
| 830 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 831 | if (syncManager != null && callback != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 832 | syncManager.getSyncStorageEngine().addStatusChangeListener(mask, callback); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 833 | } |
| 834 | } finally { |
| 835 | restoreCallingIdentity(identityToken); |
| 836 | } |
| 837 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 838 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 839 | public void removeStatusChangeListener(ISyncStatusObserver callback) { |
| 840 | long identityToken = clearCallingIdentity(); |
| 841 | try { |
| 842 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 843 | if (syncManager != null && callback != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 844 | syncManager.getSyncStorageEngine().removeStatusChangeListener(callback); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 845 | } |
| 846 | } finally { |
| 847 | restoreCallingIdentity(identityToken); |
| 848 | } |
| 849 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 850 | |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 851 | public static ContentService main(Context context, boolean factoryTest) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | ContentService service = new ContentService(context, factoryTest); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 853 | ServiceManager.addService(ContentResolver.CONTENT_SERVICE_NAME, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | return service; |
| 855 | } |
| 856 | |
| 857 | /** |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 858 | * Helper to verify that the provided package name shares the same cert as the caller. |
| 859 | * @param callerUid uid of the calling process. |
| 860 | * @param packageName package to verify against package of calling application. |
| 861 | * @param tag a tag to use when throwing an exception if the signatures don't |
| 862 | * match. Cannot be null. |
| 863 | * @return true if the calling application and the provided package are signed with the same |
| 864 | * certificate. |
| 865 | */ |
| 866 | private boolean verifySignatureForPackage(int callerUid, String packageName, String tag) { |
| 867 | PackageManager pm = mContext.getPackageManager(); |
| 868 | try { |
| 869 | int serviceUid = pm.getApplicationInfo(packageName, 0).uid; |
| 870 | if (pm.checkSignatures(callerUid, serviceUid) == PackageManager.SIGNATURE_MATCH) { |
| 871 | return true; |
| 872 | } else { |
| 873 | throw new SecurityException(tag + ": Caller certificate does not match that for - " |
| 874 | + packageName); |
| 875 | } |
| 876 | } catch (PackageManager.NameNotFoundException e) { |
| 877 | throw new IllegalArgumentException(tag + ": " + packageName + " package not found."); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | * Hide this class since it is not part of api, |
| 883 | * but current unittest framework requires it to be public |
| 884 | * @hide |
| 885 | */ |
| 886 | public static final class ObserverNode { |
| 887 | private class ObserverEntry implements IBinder.DeathRecipient { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 888 | public final IContentObserver observer; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 889 | public final int uid; |
| 890 | public final int pid; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 891 | public final boolean notifyForDescendants; |
| 892 | private final int userHandle; |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 893 | private final Object observersLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 895 | public ObserverEntry(IContentObserver o, boolean n, Object observersLock, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 896 | int _uid, int _pid, int _userHandle) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 897 | this.observersLock = observersLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | observer = o; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 899 | uid = _uid; |
| 900 | pid = _pid; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 901 | userHandle = _userHandle; |
| 902 | notifyForDescendants = n; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | try { |
| 904 | observer.asBinder().linkToDeath(this, 0); |
| 905 | } catch (RemoteException e) { |
| 906 | binderDied(); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | public void binderDied() { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 911 | synchronized (observersLock) { |
| 912 | removeObserverLocked(observer); |
| 913 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | } |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 915 | |
| 916 | public void dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 917 | String name, String prefix, SparseIntArray pidCounts) { |
| 918 | pidCounts.put(pid, pidCounts.get(pid)+1); |
| 919 | pw.print(prefix); pw.print(name); pw.print(": pid="); |
| 920 | pw.print(pid); pw.print(" uid="); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 921 | pw.print(uid); pw.print(" user="); |
| 922 | pw.print(userHandle); pw.print(" target="); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 923 | pw.println(Integer.toHexString(System.identityHashCode( |
| 924 | observer != null ? observer.asBinder() : null))); |
| 925 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | public static final int INSERT_TYPE = 0; |
| 929 | public static final int UPDATE_TYPE = 1; |
| 930 | public static final int DELETE_TYPE = 2; |
| 931 | |
| 932 | private String mName; |
| 933 | private ArrayList<ObserverNode> mChildren = new ArrayList<ObserverNode>(); |
| 934 | private ArrayList<ObserverEntry> mObservers = new ArrayList<ObserverEntry>(); |
| 935 | |
| 936 | public ObserverNode(String name) { |
| 937 | mName = name; |
| 938 | } |
| 939 | |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 940 | public void dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 941 | String name, String prefix, int[] counts, SparseIntArray pidCounts) { |
| 942 | String innerName = null; |
| 943 | if (mObservers.size() > 0) { |
| 944 | if ("".equals(name)) { |
| 945 | innerName = mName; |
| 946 | } else { |
| 947 | innerName = name + "/" + mName; |
| 948 | } |
| 949 | for (int i=0; i<mObservers.size(); i++) { |
| 950 | counts[1]++; |
| 951 | mObservers.get(i).dumpLocked(fd, pw, args, innerName, prefix, |
| 952 | pidCounts); |
| 953 | } |
| 954 | } |
| 955 | if (mChildren.size() > 0) { |
| 956 | if (innerName == null) { |
| 957 | if ("".equals(name)) { |
| 958 | innerName = mName; |
| 959 | } else { |
| 960 | innerName = name + "/" + mName; |
| 961 | } |
| 962 | } |
| 963 | for (int i=0; i<mChildren.size(); i++) { |
| 964 | counts[0]++; |
| 965 | mChildren.get(i).dumpLocked(fd, pw, args, innerName, prefix, |
| 966 | counts, pidCounts); |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | private String getUriSegment(Uri uri, int index) { |
| 972 | if (uri != null) { |
| 973 | if (index == 0) { |
| 974 | return uri.getAuthority(); |
| 975 | } else { |
| 976 | return uri.getPathSegments().get(index - 1); |
| 977 | } |
| 978 | } else { |
| 979 | return null; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | private int countUriSegments(Uri uri) { |
| 984 | if (uri == null) { |
| 985 | return 0; |
| 986 | } |
| 987 | return uri.getPathSegments().size() + 1; |
| 988 | } |
| 989 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 990 | // Invariant: userHandle is either a hard user number or is USER_ALL |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 991 | public void addObserverLocked(Uri uri, IContentObserver observer, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 992 | boolean notifyForDescendants, Object observersLock, |
| 993 | int uid, int pid, int userHandle) { |
| 994 | addObserverLocked(uri, 0, observer, notifyForDescendants, observersLock, |
| 995 | uid, pid, userHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | } |
| 997 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 998 | private void addObserverLocked(Uri uri, int index, IContentObserver observer, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 999 | boolean notifyForDescendants, Object observersLock, |
| 1000 | int uid, int pid, int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | // If this is the leaf node add the observer |
| 1002 | if (index == countUriSegments(uri)) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1003 | mObservers.add(new ObserverEntry(observer, notifyForDescendants, observersLock, |
| 1004 | uid, pid, userHandle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | return; |
| 1006 | } |
| 1007 | |
| 1008 | // Look to see if the proper child already exists |
| 1009 | String segment = getUriSegment(uri, index); |
Anthony Newnam | f512664 | 2010-03-22 17:29:06 -0500 | [diff] [blame] | 1010 | if (segment == null) { |
| 1011 | throw new IllegalArgumentException("Invalid Uri (" + uri + ") used for observer"); |
| 1012 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | int N = mChildren.size(); |
| 1014 | for (int i = 0; i < N; i++) { |
| 1015 | ObserverNode node = mChildren.get(i); |
| 1016 | if (node.mName.equals(segment)) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1017 | node.addObserverLocked(uri, index + 1, observer, notifyForDescendants, |
| 1018 | observersLock, uid, pid, userHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 | return; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | // No child found, create one |
| 1024 | ObserverNode node = new ObserverNode(segment); |
| 1025 | mChildren.add(node); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1026 | node.addObserverLocked(uri, index + 1, observer, notifyForDescendants, |
| 1027 | observersLock, uid, pid, userHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1030 | public boolean removeObserverLocked(IContentObserver observer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1031 | int size = mChildren.size(); |
| 1032 | for (int i = 0; i < size; i++) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1033 | boolean empty = mChildren.get(i).removeObserverLocked(observer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | if (empty) { |
| 1035 | mChildren.remove(i); |
| 1036 | i--; |
| 1037 | size--; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | IBinder observerBinder = observer.asBinder(); |
| 1042 | size = mObservers.size(); |
| 1043 | for (int i = 0; i < size; i++) { |
| 1044 | ObserverEntry entry = mObservers.get(i); |
| 1045 | if (entry.observer.asBinder() == observerBinder) { |
| 1046 | mObservers.remove(i); |
| 1047 | // We no longer need to listen for death notifications. Remove it. |
| 1048 | observerBinder.unlinkToDeath(entry, 0); |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | if (mChildren.size() == 0 && mObservers.size() == 0) { |
| 1054 | return true; |
| 1055 | } |
| 1056 | return false; |
| 1057 | } |
| 1058 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1059 | private void collectMyObserversLocked(boolean leaf, IContentObserver observer, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1060 | boolean observerWantsSelfNotifications, int targetUserHandle, |
| 1061 | ArrayList<ObserverCall> calls) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1062 | int N = mObservers.size(); |
| 1063 | IBinder observerBinder = observer == null ? null : observer.asBinder(); |
| 1064 | for (int i = 0; i < N; i++) { |
| 1065 | ObserverEntry entry = mObservers.get(i); |
| 1066 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1067 | // 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] | 1068 | // in self notifications |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 1069 | boolean selfChange = (entry.observer.asBinder() == observerBinder); |
| 1070 | if (selfChange && !observerWantsSelfNotifications) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | continue; |
| 1072 | } |
| 1073 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1074 | // Does this observer match the target user? |
| 1075 | if (targetUserHandle == UserHandle.USER_ALL |
| 1076 | || entry.userHandle == UserHandle.USER_ALL |
| 1077 | || targetUserHandle == entry.userHandle) { |
| 1078 | // Make sure the observer is interested in the notification |
| 1079 | if (leaf || (!leaf && entry.notifyForDescendants)) { |
| 1080 | calls.add(new ObserverCall(this, entry.observer, selfChange)); |
| 1081 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1086 | /** |
| 1087 | * targetUserHandle is either a hard user handle or is USER_ALL |
| 1088 | */ |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1089 | public void collectObserversLocked(Uri uri, int index, IContentObserver observer, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1090 | boolean observerWantsSelfNotifications, int targetUserHandle, |
| 1091 | ArrayList<ObserverCall> calls) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | String segment = null; |
| 1093 | int segmentCount = countUriSegments(uri); |
| 1094 | if (index >= segmentCount) { |
| 1095 | // This is the leaf node, notify all observers |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1096 | collectMyObserversLocked(true, observer, observerWantsSelfNotifications, |
| 1097 | targetUserHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1098 | } else if (index < segmentCount){ |
| 1099 | segment = getUriSegment(uri, index); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1100 | // Notify any observers at this level who are interested in descendants |
| 1101 | collectMyObserversLocked(false, observer, observerWantsSelfNotifications, |
| 1102 | targetUserHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | int N = mChildren.size(); |
| 1106 | for (int i = 0; i < N; i++) { |
| 1107 | ObserverNode node = mChildren.get(i); |
| 1108 | if (segment == null || node.mName.equals(segment)) { |
| 1109 | // We found the child, |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 1110 | node.collectObserversLocked(uri, index + 1, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1111 | observer, observerWantsSelfNotifications, targetUserHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | if (segment != null) { |
| 1113 | break; |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | } |