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; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 23 | import android.app.AppOpsManager; |
| 24 | import android.content.BroadcastReceiver; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 25 | import android.content.ComponentName; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 26 | import android.content.ContentResolver; |
| 27 | import android.content.Context; |
| 28 | import android.content.IContentService; |
Benjamin Franz | adea191 | 2015-06-19 16:03:38 +0100 | [diff] [blame] | 29 | import android.content.Intent; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 30 | import android.content.IntentFilter; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 31 | import android.content.ISyncStatusObserver; |
| 32 | import android.content.PeriodicSync; |
| 33 | import android.content.SyncAdapterType; |
| 34 | import android.content.SyncInfo; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 35 | import android.content.SyncRequest; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 36 | import android.content.SyncStatusInfo; |
Benjamin Franz | adea191 | 2015-06-19 16:03:38 +0100 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 38 | import android.content.pm.PackageManagerInternal; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 39 | import android.content.pm.ProviderInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.database.IContentObserver; |
| 41 | import android.database.sqlite.SQLiteException; |
| 42 | import android.net.Uri; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 43 | import android.os.Binder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.os.Bundle; |
| 45 | import android.os.IBinder; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 46 | import android.os.Parcel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.os.RemoteException; |
| 48 | import android.os.ServiceManager; |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 49 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 50 | import android.os.UserHandle; |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 51 | import android.text.TextUtils; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 52 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.util.Log; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 54 | import android.util.Pair; |
Jeff Sharkey | 6009459 | 2013-03-21 18:14:24 -0700 | [diff] [blame] | 55 | import android.util.Slog; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 56 | import android.util.SparseArray; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 57 | import android.util.SparseIntArray; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 58 | |
| 59 | import com.android.internal.annotations.GuardedBy; |
| 60 | import com.android.internal.util.IndentingPrintWriter; |
| 61 | import com.android.internal.util.Preconditions; |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 62 | import com.android.server.LocalServices; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
| 64 | import java.io.FileDescriptor; |
| 65 | import java.io.PrintWriter; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 66 | import java.security.InvalidParameterException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import java.util.ArrayList; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 68 | import java.util.Arrays; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 69 | import java.util.Collections; |
| 70 | import java.util.Comparator; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 71 | import java.util.List; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 72 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * {@hide} |
| 76 | */ |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 77 | public final class ContentService extends IContentService.Stub { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | private static final String TAG = "ContentService"; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 79 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | private Context mContext; |
| 81 | private boolean mFactoryTest; |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | private final ObserverNode mRootNode = new ObserverNode(""); |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | private SyncManager mSyncManager = null; |
| 86 | private final Object mSyncManagerLock = new Object(); |
| 87 | |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Map from userId to providerPackageName to [clientPackageName, uri] to |
| 90 | * value. This structure is carefully optimized to keep invalidation logic |
| 91 | * as cheap as possible. |
| 92 | */ |
| 93 | @GuardedBy("mCache") |
| 94 | private final SparseArray<ArrayMap<String, ArrayMap<Pair<String, Uri>, Bundle>>> |
| 95 | mCache = new SparseArray<>(); |
| 96 | |
| 97 | private BroadcastReceiver mCacheReceiver = new BroadcastReceiver() { |
| 98 | @Override |
| 99 | public void onReceive(Context context, Intent intent) { |
| 100 | final Uri data = intent.getData(); |
| 101 | if (data != null) { |
| 102 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 103 | UserHandle.USER_NULL); |
| 104 | final String packageName = data.getSchemeSpecificPart(); |
| 105 | invalidateCacheLocked(userId, packageName, null); |
| 106 | } |
| 107 | } |
| 108 | }; |
| 109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | private SyncManager getSyncManager() { |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 111 | if (SystemProperties.getBoolean("config.disable_network", false)) { |
| 112 | return null; |
| 113 | } |
| 114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | synchronized(mSyncManagerLock) { |
| 116 | try { |
| 117 | // Try to create the SyncManager, return null if it fails (e.g. the disk is full). |
| 118 | if (mSyncManager == null) mSyncManager = new SyncManager(mContext, mFactoryTest); |
| 119 | } catch (SQLiteException e) { |
| 120 | Log.e(TAG, "Can't create SyncManager", e); |
| 121 | } |
| 122 | return mSyncManager; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 127 | protected synchronized void dump(FileDescriptor fd, PrintWriter pw_, String[] args) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | mContext.enforceCallingOrSelfPermission(Manifest.permission.DUMP, |
| 129 | "caller doesn't have the DUMP permission"); |
| 130 | |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 131 | final IndentingPrintWriter pw = new IndentingPrintWriter(pw_, " "); |
| 132 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | // This makes it so that future permission checks will be in the context of this |
| 134 | // process rather than the caller's process. We will restore this before returning. |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 135 | final long identityToken = clearCallingIdentity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | try { |
| 137 | if (mSyncManager == null) { |
| 138 | pw.println("No SyncManager created! (Disk full?)"); |
| 139 | } else { |
| 140 | mSyncManager.dump(fd, pw); |
| 141 | } |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 142 | pw.println(); |
| 143 | pw.println("Observer tree:"); |
| 144 | synchronized (mRootNode) { |
| 145 | int[] counts = new int[2]; |
| 146 | final SparseIntArray pidCounts = new SparseIntArray(); |
| 147 | mRootNode.dumpLocked(fd, pw, args, "", " ", counts, pidCounts); |
| 148 | pw.println(); |
| 149 | ArrayList<Integer> sorted = new ArrayList<Integer>(); |
| 150 | for (int i=0; i<pidCounts.size(); i++) { |
| 151 | sorted.add(pidCounts.keyAt(i)); |
| 152 | } |
| 153 | Collections.sort(sorted, new Comparator<Integer>() { |
| 154 | @Override |
| 155 | public int compare(Integer lhs, Integer rhs) { |
| 156 | int lc = pidCounts.get(lhs); |
| 157 | int rc = pidCounts.get(rhs); |
| 158 | if (lc < rc) { |
| 159 | return 1; |
| 160 | } else if (lc > rc) { |
| 161 | return -1; |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | }); |
| 167 | for (int i=0; i<sorted.size(); i++) { |
| 168 | int pid = sorted.get(i); |
| 169 | pw.print(" pid "); pw.print(pid); pw.print(": "); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 170 | pw.print(pidCounts.get(pid)); pw.println(" observers"); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 171 | } |
| 172 | pw.println(); |
| 173 | pw.print(" Total number of nodes: "); pw.println(counts[0]); |
| 174 | pw.print(" Total number of observers: "); pw.println(counts[1]); |
| 175 | } |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 176 | |
| 177 | synchronized (mCache) { |
| 178 | pw.println(); |
| 179 | pw.println("Cached content:"); |
| 180 | pw.increaseIndent(); |
| 181 | for (int i = 0; i < mCache.size(); i++) { |
| 182 | pw.println("User " + mCache.keyAt(i) + ":"); |
| 183 | pw.increaseIndent(); |
| 184 | pw.println(mCache.valueAt(i)); |
| 185 | pw.decreaseIndent(); |
| 186 | } |
| 187 | pw.decreaseIndent(); |
| 188 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | } finally { |
| 190 | restoreCallingIdentity(identityToken); |
| 191 | } |
| 192 | } |
| 193 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 194 | @Override |
| 195 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 196 | throws RemoteException { |
| 197 | try { |
| 198 | return super.onTransact(code, data, reply, flags); |
| 199 | } catch (RuntimeException e) { |
| 200 | // The content service only throws security exceptions, so let's |
| 201 | // log all others. |
| 202 | if (!(e instanceof SecurityException)) { |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 203 | Slog.wtf(TAG, "Content Service Crash", e); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 204 | } |
| 205 | throw e; |
| 206 | } |
| 207 | } |
| 208 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | /*package*/ ContentService(Context context, boolean factoryTest) { |
| 210 | mContext = context; |
| 211 | mFactoryTest = factoryTest; |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 212 | |
| 213 | // Let the package manager query for the sync adapters for a given authority |
Svet Ganov | 52153f4 | 2015-08-11 08:59:12 -0700 | [diff] [blame] | 214 | // as we grant default permissions to sync adapters for specific authorities. |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 215 | PackageManagerInternal packageManagerInternal = LocalServices.getService( |
| 216 | PackageManagerInternal.class); |
| 217 | packageManagerInternal.setSyncAdapterPackagesprovider( |
| 218 | new PackageManagerInternal.SyncAdapterPackagesProvider() { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 219 | @Override |
| 220 | public String[] getPackages(String authority, int userId) { |
| 221 | return getSyncAdapterPackagesForAuthorityAsUser(authority, userId); |
| 222 | } |
| 223 | }); |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 224 | |
| 225 | final IntentFilter packageFilter = new IntentFilter(); |
| 226 | packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 227 | packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 228 | packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 229 | packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED); |
| 230 | packageFilter.addDataScheme("package"); |
| 231 | mContext.registerReceiverAsUser(mCacheReceiver, UserHandle.ALL, |
| 232 | packageFilter, null, null); |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | public void systemReady() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | getSyncManager(); |
| 237 | } |
| 238 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 239 | /** |
| 240 | * Register a content observer tied to a specific user's view of the provider. |
| 241 | * @param userHandle the user whose view of the provider is to be observed. May be |
| 242 | * the calling user without requiring any permission, otherwise the caller needs to |
Benjamin Franz | adea191 | 2015-06-19 16:03:38 +0100 | [diff] [blame] | 243 | * hold the INTERACT_ACROSS_USERS_FULL permission or hold a read uri grant to the uri. |
| 244 | * Pseudousers USER_ALL and USER_CURRENT are properly handled; all other pseudousers |
| 245 | * are forbidden. |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 246 | */ |
| 247 | @Override |
| 248 | public void registerContentObserver(Uri uri, boolean notifyForDescendants, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 249 | IContentObserver observer, int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | if (observer == null || uri == null) { |
| 251 | throw new IllegalArgumentException("You must pass a valid uri and observer"); |
| 252 | } |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 253 | |
Benjamin Franz | adea191 | 2015-06-19 16:03:38 +0100 | [diff] [blame] | 254 | final int uid = Binder.getCallingUid(); |
| 255 | final int pid = Binder.getCallingPid(); |
| 256 | final int callingUserHandle = UserHandle.getCallingUserId(); |
| 257 | // Registering an observer for any user other than the calling user requires uri grant or |
| 258 | // cross user permission |
| 259 | if (callingUserHandle != userHandle && |
| 260 | mContext.checkUriPermission(uri, pid, uid, Intent.FLAG_GRANT_READ_URI_PERMISSION) |
| 261 | != PackageManager.PERMISSION_GRANTED) { |
| 262 | enforceCrossUserPermission(userHandle, |
| 263 | "no permission to observe other users' provider view"); |
| 264 | } |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 265 | |
| 266 | if (userHandle < 0) { |
| 267 | if (userHandle == UserHandle.USER_CURRENT) { |
| 268 | userHandle = ActivityManager.getCurrentUser(); |
| 269 | } else if (userHandle != UserHandle.USER_ALL) { |
| 270 | throw new InvalidParameterException("Bad user handle for registerContentObserver: " |
| 271 | + userHandle); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | synchronized (mRootNode) { |
| 276 | mRootNode.addObserverLocked(uri, observer, notifyForDescendants, mRootNode, |
Benjamin Franz | adea191 | 2015-06-19 16:03:38 +0100 | [diff] [blame] | 277 | uid, pid, userHandle); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 278 | if (false) Log.v(TAG, "Registered observer " + observer + " at " + uri + |
| 279 | " with notifyForDescendants " + notifyForDescendants); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | public void registerContentObserver(Uri uri, boolean notifyForDescendants, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 284 | IContentObserver observer) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 285 | registerContentObserver(uri, notifyForDescendants, observer, |
| 286 | UserHandle.getCallingUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 289 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | public void unregisterContentObserver(IContentObserver observer) { |
| 291 | if (observer == null) { |
| 292 | throw new IllegalArgumentException("You must pass a valid observer"); |
| 293 | } |
| 294 | synchronized (mRootNode) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 295 | mRootNode.removeObserverLocked(observer); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 296 | if (false) Log.v(TAG, "Unregistered observer " + observer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 300 | /** |
| 301 | * Notify observers of a particular user's view of the provider. |
| 302 | * @param userHandle the user whose view of the provider is to be notified. May be |
| 303 | * the calling user without requiring any permission, otherwise the caller needs to |
Benjamin Franz | adea191 | 2015-06-19 16:03:38 +0100 | [diff] [blame] | 304 | * hold the INTERACT_ACROSS_USERS_FULL permission or hold a write uri grant to the uri. |
| 305 | * Pseudousers USER_ALL and USER_CURRENT are properly interpreted; no other pseudousers are |
| 306 | * allowed. |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 307 | */ |
| 308 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | public void notifyChange(Uri uri, IContentObserver observer, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 310 | boolean observerWantsSelfNotifications, boolean syncToNetwork, |
| 311 | int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 313 | Log.v(TAG, "Notifying update of " + uri + " for user " + userHandle |
| 314 | + " from observer " + observer + ", syncToNetwork " + syncToNetwork); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 316 | |
Benjamin Franz | adea191 | 2015-06-19 16:03:38 +0100 | [diff] [blame] | 317 | final int uid = Binder.getCallingUid(); |
| 318 | final int pid = Binder.getCallingPid(); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 319 | final int callingUserHandle = UserHandle.getCallingUserId(); |
Benjamin Franz | adea191 | 2015-06-19 16:03:38 +0100 | [diff] [blame] | 320 | // Notify for any user other than the caller requires uri grant or cross user permission |
| 321 | if (callingUserHandle != userHandle && |
| 322 | mContext.checkUriPermission(uri, pid, uid, Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
| 323 | != PackageManager.PERMISSION_GRANTED) { |
| 324 | enforceCrossUserPermission(userHandle, "no permission to notify other users"); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // We passed the permission check; resolve pseudouser targets as appropriate |
| 328 | if (userHandle < 0) { |
| 329 | if (userHandle == UserHandle.USER_CURRENT) { |
| 330 | userHandle = ActivityManager.getCurrentUser(); |
| 331 | } else if (userHandle != UserHandle.USER_ALL) { |
| 332 | throw new InvalidParameterException("Bad user handle for notifyChange: " |
| 333 | + userHandle); |
| 334 | } |
| 335 | } |
| 336 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | // This makes it so that future permission checks will be in the context of this |
| 338 | // process rather than the caller's process. We will restore this before returning. |
| 339 | long identityToken = clearCallingIdentity(); |
| 340 | try { |
| 341 | ArrayList<ObserverCall> calls = new ArrayList<ObserverCall>(); |
| 342 | synchronized (mRootNode) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 343 | mRootNode.collectObserversLocked(uri, 0, observer, observerWantsSelfNotifications, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 344 | userHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | } |
| 346 | final int numCalls = calls.size(); |
| 347 | for (int i=0; i<numCalls; i++) { |
| 348 | ObserverCall oc = calls.get(i); |
| 349 | try { |
Svetoslav | 86b1df2 | 2014-08-21 14:22:53 -0700 | [diff] [blame] | 350 | oc.mObserver.onChange(oc.mSelfChange, uri, userHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 352 | Log.v(TAG, "Notified " + oc.mObserver + " of " + "update at " + uri); |
| 353 | } |
| 354 | } catch (RemoteException ex) { |
| 355 | synchronized (mRootNode) { |
| 356 | Log.w(TAG, "Found dead observer, removing"); |
| 357 | IBinder binder = oc.mObserver.asBinder(); |
| 358 | final ArrayList<ObserverNode.ObserverEntry> list |
| 359 | = oc.mNode.mObservers; |
| 360 | int numList = list.size(); |
| 361 | for (int j=0; j<numList; j++) { |
| 362 | ObserverNode.ObserverEntry oe = list.get(j); |
| 363 | if (oe.observer.asBinder() == binder) { |
| 364 | list.remove(j); |
| 365 | j--; |
| 366 | numList--; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | if (syncToNetwork) { |
| 373 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 374 | if (syncManager != null) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 375 | syncManager.scheduleLocalSync(null /* all accounts */, callingUserHandle, uid, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 376 | uri.getAuthority()); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 377 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | } |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 379 | |
| 380 | synchronized (mCache) { |
| 381 | final String providerPackageName = getProviderPackageName(uri); |
| 382 | invalidateCacheLocked(userHandle, providerPackageName, uri); |
| 383 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | } finally { |
| 385 | restoreCallingIdentity(identityToken); |
| 386 | } |
| 387 | } |
| 388 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 389 | public void notifyChange(Uri uri, IContentObserver observer, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 390 | boolean observerWantsSelfNotifications, boolean syncToNetwork) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 391 | notifyChange(uri, observer, observerWantsSelfNotifications, syncToNetwork, |
| 392 | UserHandle.getCallingUserId()); |
| 393 | } |
| 394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | /** |
| 396 | * Hide this class since it is not part of api, |
| 397 | * but current unittest framework requires it to be public |
| 398 | * @hide |
| 399 | * |
| 400 | */ |
| 401 | public static final class ObserverCall { |
| 402 | final ObserverNode mNode; |
| 403 | final IContentObserver mObserver; |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 404 | final boolean mSelfChange; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 406 | ObserverCall(ObserverNode node, IContentObserver observer, boolean selfChange) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | mNode = node; |
| 408 | mObserver = observer; |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 409 | mSelfChange = selfChange; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 413 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 414 | public void requestSync(Account account, String authority, Bundle extras) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 415 | Bundle.setDefusable(extras, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | ContentResolver.validateSyncExtrasBundle(extras); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 417 | int userId = UserHandle.getCallingUserId(); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 418 | int uId = Binder.getCallingUid(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 419 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | // This makes it so that future permission checks will be in the context of this |
| 421 | // process rather than the caller's process. We will restore this before returning. |
| 422 | long identityToken = clearCallingIdentity(); |
| 423 | try { |
| 424 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 425 | if (syncManager != null) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 426 | syncManager.scheduleSync(account, userId, uId, authority, extras, |
| 427 | 0 /* no delay */, 0 /* no delay */, |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 428 | false /* onlyThoseWithUnkownSyncableState */); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 429 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | } finally { |
| 431 | restoreCallingIdentity(identityToken); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /** |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 436 | * Request a sync with a generic {@link android.content.SyncRequest} object. This will be |
| 437 | * either: |
| 438 | * periodic OR one-off sync. |
| 439 | * and |
| 440 | * anonymous OR provider sync. |
| 441 | * Depending on the request, we enqueue to suit in the SyncManager. |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 442 | * @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] | 443 | */ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 444 | @Override |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 445 | public void sync(SyncRequest request) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 446 | syncAsUser(request, UserHandle.getCallingUserId()); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * If the user id supplied is different to the calling user, the caller must hold the |
| 451 | * INTERACT_ACROSS_USERS_FULL permission. |
| 452 | */ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 453 | @Override |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 454 | public void syncAsUser(SyncRequest request, int userId) { |
| 455 | enforceCrossUserPermission(userId, "no permission to request sync as user: " + userId); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 456 | int callerUid = Binder.getCallingUid(); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 457 | // This makes it so that future permission checks will be in the context of this |
| 458 | // process rather than the caller's process. We will restore this before returning. |
| 459 | long identityToken = clearCallingIdentity(); |
| 460 | try { |
| 461 | SyncManager syncManager = getSyncManager(); |
Matthew Williams | d08d668 | 2013-10-14 10:39:41 -0700 | [diff] [blame] | 462 | if (syncManager == null) { |
| 463 | return; |
| 464 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 465 | |
Matthew Williams | d08d668 | 2013-10-14 10:39:41 -0700 | [diff] [blame] | 466 | Bundle extras = request.getBundle(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 467 | long flextime = request.getSyncFlexTime(); |
| 468 | long runAtTime = request.getSyncRunTime(); |
| 469 | if (request.isPeriodic()) { |
| 470 | mContext.enforceCallingOrSelfPermission( |
| 471 | Manifest.permission.WRITE_SYNC_SETTINGS, |
| 472 | "no permission to write the sync settings"); |
| 473 | SyncStorageEngine.EndPoint info; |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 474 | info = new SyncStorageEngine.EndPoint( |
| 475 | request.getAccount(), request.getProvider(), userId); |
Shreyas Basarge | e96c3b7 | 2016-01-29 19:25:51 +0000 | [diff] [blame] | 476 | if (runAtTime < 3600) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 477 | Slog.w(TAG, "Requested poll frequency of " + runAtTime |
Shreyas Basarge | e96c3b7 | 2016-01-29 19:25:51 +0000 | [diff] [blame] | 478 | + " seconds being rounded up to 1 hour."); |
| 479 | runAtTime = 3600; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 480 | } |
| 481 | // Schedule periodic sync. |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 482 | getSyncManager().updateOrAddPeriodicSync(info, runAtTime, |
| 483 | flextime, extras); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 484 | } else { |
| 485 | long beforeRuntimeMillis = (flextime) * 1000; |
| 486 | long runtimeMillis = runAtTime * 1000; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 487 | syncManager.scheduleSync( |
| 488 | request.getAccount(), userId, callerUid, request.getProvider(), extras, |
| 489 | beforeRuntimeMillis, runtimeMillis, |
| 490 | false /* onlyThoseWithUnknownSyncableState */); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 491 | } |
| 492 | } finally { |
| 493 | restoreCallingIdentity(identityToken); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | * 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] | 499 | * if they match the authority and account, if they are present. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 500 | * |
| 501 | * @param account filter the pending and active syncs to cancel using this account, or null. |
| 502 | * @param authority filter the pending and active syncs to cancel using this authority, or |
| 503 | * null. |
| 504 | * @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] | 505 | */ |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 506 | @Override |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 507 | public void cancelSync(Account account, String authority, ComponentName cname) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 508 | cancelSyncAsUser(account, authority, cname, UserHandle.getCallingUserId()); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Clear all scheduled sync operations that match the uri and cancel the active sync |
| 513 | * if they match the authority and account, if they are present. |
| 514 | * |
| 515 | * <p> If the user id supplied is different to the calling user, the caller must hold the |
| 516 | * INTERACT_ACROSS_USERS_FULL permission. |
| 517 | * |
| 518 | * @param account filter the pending and active syncs to cancel using this account, or null. |
| 519 | * @param authority filter the pending and active syncs to cancel using this authority, or |
| 520 | * null. |
| 521 | * @param userId the user id for which to cancel sync operations. |
| 522 | * @param cname cancel syncs running on this service, or null for provider/account. |
| 523 | */ |
| 524 | @Override |
| 525 | public void cancelSyncAsUser(Account account, String authority, ComponentName cname, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 526 | int userId) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 527 | if (authority != null && authority.length() == 0) { |
| 528 | throw new IllegalArgumentException("Authority must be non-empty"); |
| 529 | } |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 530 | enforceCrossUserPermission(userId, |
| 531 | "no permission to modify the sync settings for user " + userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | // This makes it so that future permission checks will be in the context of this |
| 533 | // process rather than the caller's process. We will restore this before returning. |
| 534 | long identityToken = clearCallingIdentity(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 535 | if (cname != null) { |
| 536 | Slog.e(TAG, "cname not null."); |
| 537 | return; |
| 538 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | try { |
| 540 | SyncManager syncManager = getSyncManager(); |
| 541 | if (syncManager != null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 542 | SyncStorageEngine.EndPoint info; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 543 | info = new SyncStorageEngine.EndPoint(account, authority, userId); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 544 | syncManager.clearScheduledSyncOperations(info); |
| 545 | syncManager.cancelActiveSync(info, null /* all syncs for this adapter */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | } |
| 547 | } finally { |
| 548 | restoreCallingIdentity(identityToken); |
| 549 | } |
| 550 | } |
| 551 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 552 | @Override |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 553 | public void cancelRequest(SyncRequest request) { |
| 554 | SyncManager syncManager = getSyncManager(); |
| 555 | if (syncManager == null) return; |
| 556 | int userId = UserHandle.getCallingUserId(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 557 | |
| 558 | long identityToken = clearCallingIdentity(); |
| 559 | try { |
| 560 | SyncStorageEngine.EndPoint info; |
| 561 | Bundle extras = new Bundle(request.getBundle()); |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 562 | Account account = request.getAccount(); |
| 563 | String provider = request.getProvider(); |
| 564 | info = new SyncStorageEngine.EndPoint(account, provider, userId); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 565 | if (request.isPeriodic()) { |
| 566 | // Remove periodic sync. |
| 567 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 568 | "no permission to write the sync settings"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 569 | getSyncManager().removePeriodicSync(info, extras); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 570 | } |
| 571 | // Cancel active syncs and clear pending syncs from the queue. |
| 572 | syncManager.cancelScheduledSyncOperation(info, extras); |
| 573 | syncManager.cancelActiveSync(info, extras); |
| 574 | } finally { |
| 575 | restoreCallingIdentity(identityToken); |
| 576 | } |
| 577 | } |
| 578 | |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 579 | /** |
| 580 | * Get information about the SyncAdapters that are known to the system. |
| 581 | * @return an array of SyncAdapters that have registered with the system |
| 582 | */ |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 583 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 584 | public SyncAdapterType[] getSyncAdapterTypes() { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 585 | return getSyncAdapterTypesAsUser(UserHandle.getCallingUserId()); |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Get information about the SyncAdapters that are known to the system for a particular user. |
| 590 | * |
| 591 | * <p> If the user id supplied is different to the calling user, the caller must hold the |
| 592 | * INTERACT_ACROSS_USERS_FULL permission. |
| 593 | * |
| 594 | * @return an array of SyncAdapters that have registered with the system |
| 595 | */ |
| 596 | @Override |
| 597 | public SyncAdapterType[] getSyncAdapterTypesAsUser(int userId) { |
| 598 | enforceCrossUserPermission(userId, |
| 599 | "no permission to read sync settings for user " + userId); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 600 | // This makes it so that future permission checks will be in the context of this |
| 601 | // 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] | 602 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 603 | try { |
| 604 | SyncManager syncManager = getSyncManager(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 605 | return syncManager.getSyncAdapterTypes(userId); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 606 | } finally { |
| 607 | restoreCallingIdentity(identityToken); |
| 608 | } |
| 609 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 610 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 611 | @Override |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 612 | public String[] getSyncAdapterPackagesForAuthorityAsUser(String authority, int userId) { |
| 613 | enforceCrossUserPermission(userId, |
| 614 | "no permission to read sync settings for user " + userId); |
| 615 | // This makes it so that future permission checks will be in the context of this |
| 616 | // process rather than the caller's process. We will restore this before returning. |
| 617 | final long identityToken = clearCallingIdentity(); |
| 618 | try { |
| 619 | SyncManager syncManager = getSyncManager(); |
| 620 | return syncManager.getSyncAdapterPackagesForAuthorityAsUser(authority, userId); |
| 621 | } finally { |
| 622 | restoreCallingIdentity(identityToken); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 627 | public boolean getSyncAutomatically(Account account, String providerName) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 628 | return getSyncAutomaticallyAsUser(account, providerName, UserHandle.getCallingUserId()); |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * If the user id supplied is different to the calling user, the caller must hold the |
| 633 | * INTERACT_ACROSS_USERS_FULL permission. |
| 634 | */ |
| 635 | @Override |
| 636 | public boolean getSyncAutomaticallyAsUser(Account account, String providerName, int userId) { |
| 637 | enforceCrossUserPermission(userId, |
| 638 | "no permission to read the sync settings for user " + userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 639 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 640 | "no permission to read the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 641 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 642 | long identityToken = clearCallingIdentity(); |
| 643 | try { |
| 644 | SyncManager syncManager = getSyncManager(); |
| 645 | if (syncManager != null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 646 | return syncManager.getSyncStorageEngine() |
| 647 | .getSyncAutomatically(account, userId, providerName); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 648 | } |
| 649 | } finally { |
| 650 | restoreCallingIdentity(identityToken); |
| 651 | } |
| 652 | return false; |
| 653 | } |
| 654 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 655 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 656 | public void setSyncAutomatically(Account account, String providerName, boolean sync) { |
Alexandra Gherghina | cb22807 | 2014-07-01 15:14:11 +0100 | [diff] [blame] | 657 | setSyncAutomaticallyAsUser(account, providerName, sync, UserHandle.getCallingUserId()); |
| 658 | } |
| 659 | |
| 660 | @Override |
| 661 | public void setSyncAutomaticallyAsUser(Account account, String providerName, boolean sync, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 662 | int userId) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 663 | if (TextUtils.isEmpty(providerName)) { |
| 664 | throw new IllegalArgumentException("Authority must be non-empty"); |
| 665 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 666 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 667 | "no permission to write the sync settings"); |
Alexandra Gherghina | cb22807 | 2014-07-01 15:14:11 +0100 | [diff] [blame] | 668 | enforceCrossUserPermission(userId, |
| 669 | "no permission to modify the sync settings for user " + userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 670 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 671 | long identityToken = clearCallingIdentity(); |
| 672 | try { |
| 673 | SyncManager syncManager = getSyncManager(); |
| 674 | if (syncManager != null) { |
Alexandra Gherghina | cb22807 | 2014-07-01 15:14:11 +0100 | [diff] [blame] | 675 | syncManager.getSyncStorageEngine().setSyncAutomatically(account, userId, |
| 676 | providerName, sync); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 677 | } |
| 678 | } finally { |
| 679 | restoreCallingIdentity(identityToken); |
| 680 | } |
| 681 | } |
| 682 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 683 | /** Old API. Schedule periodic sync with default flexMillis time. */ |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 684 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 685 | public void addPeriodicSync(Account account, String authority, Bundle extras, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 686 | long pollFrequency) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 687 | Bundle.setDefusable(extras, true); |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 688 | if (account == null) { |
| 689 | throw new IllegalArgumentException("Account must not be null"); |
| 690 | } |
| 691 | if (TextUtils.isEmpty(authority)) { |
| 692 | throw new IllegalArgumentException("Authority must not be empty."); |
| 693 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 694 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 695 | "no permission to write the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 696 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 697 | int userId = UserHandle.getCallingUserId(); |
Shreyas Basarge | e96c3b7 | 2016-01-29 19:25:51 +0000 | [diff] [blame] | 698 | if (pollFrequency < 3600) { |
Jeff Sharkey | 6009459 | 2013-03-21 18:14:24 -0700 | [diff] [blame] | 699 | Slog.w(TAG, "Requested poll frequency of " + pollFrequency |
Shreyas Basarge | e96c3b7 | 2016-01-29 19:25:51 +0000 | [diff] [blame] | 700 | + " seconds being rounded up to 1 hour."); |
| 701 | pollFrequency = 3600; |
Jeff Sharkey | 6009459 | 2013-03-21 18:14:24 -0700 | [diff] [blame] | 702 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 703 | long defaultFlex = SyncStorageEngine.calculateDefaultFlexTime(pollFrequency); |
Jeff Sharkey | 6009459 | 2013-03-21 18:14:24 -0700 | [diff] [blame] | 704 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 705 | long identityToken = clearCallingIdentity(); |
| 706 | try { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 707 | SyncStorageEngine.EndPoint info = |
| 708 | new SyncStorageEngine.EndPoint(account, authority, userId); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 709 | getSyncManager().updateOrAddPeriodicSync(info, pollFrequency, |
| 710 | defaultFlex, extras); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 711 | } finally { |
| 712 | restoreCallingIdentity(identityToken); |
| 713 | } |
| 714 | } |
| 715 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 716 | @Override |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 717 | public void removePeriodicSync(Account account, String authority, Bundle extras) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 718 | Bundle.setDefusable(extras, true); |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 719 | if (account == null) { |
| 720 | throw new IllegalArgumentException("Account must not be null"); |
| 721 | } |
| 722 | if (TextUtils.isEmpty(authority)) { |
| 723 | throw new IllegalArgumentException("Authority must not be empty"); |
| 724 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 725 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 726 | "no permission to write the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 727 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 728 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 729 | long identityToken = clearCallingIdentity(); |
| 730 | try { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 731 | getSyncManager() |
| 732 | .removePeriodicSync( |
| 733 | new SyncStorageEngine.EndPoint(account, authority, userId), |
| 734 | extras); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 735 | } finally { |
| 736 | restoreCallingIdentity(identityToken); |
| 737 | } |
| 738 | } |
| 739 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 740 | @Override |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 741 | public List<PeriodicSync> getPeriodicSyncs(Account account, String providerName, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 742 | ComponentName cname) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 743 | if (account == null) { |
| 744 | throw new IllegalArgumentException("Account must not be null"); |
| 745 | } |
| 746 | if (TextUtils.isEmpty(providerName)) { |
| 747 | throw new IllegalArgumentException("Authority must not be empty"); |
| 748 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 749 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 750 | "no permission to read the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 751 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 752 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 753 | long identityToken = clearCallingIdentity(); |
| 754 | try { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 755 | return getSyncManager().getPeriodicSyncs( |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 756 | new SyncStorageEngine.EndPoint(account, providerName, userId)); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 757 | } finally { |
| 758 | restoreCallingIdentity(identityToken); |
| 759 | } |
| 760 | } |
| 761 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 762 | @Override |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 763 | public int getIsSyncable(Account account, String providerName) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 764 | return getIsSyncableAsUser(account, providerName, UserHandle.getCallingUserId()); |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * If the user id supplied is different to the calling user, the caller must hold the |
| 769 | * INTERACT_ACROSS_USERS_FULL permission. |
| 770 | */ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 771 | @Override |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 772 | public int getIsSyncableAsUser(Account account, String providerName, int userId) { |
| 773 | enforceCrossUserPermission(userId, |
| 774 | "no permission to read the sync settings for user " + userId); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 775 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 776 | "no permission to read the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 777 | |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 778 | long identityToken = clearCallingIdentity(); |
| 779 | try { |
| 780 | SyncManager syncManager = getSyncManager(); |
| 781 | if (syncManager != null) { |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 782 | return syncManager.getIsSyncable( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 783 | account, userId, providerName); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 784 | } |
| 785 | } finally { |
| 786 | restoreCallingIdentity(identityToken); |
| 787 | } |
| 788 | return -1; |
| 789 | } |
| 790 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 791 | @Override |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 792 | public void setIsSyncable(Account account, String providerName, int syncable) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 793 | if (TextUtils.isEmpty(providerName)) { |
| 794 | throw new IllegalArgumentException("Authority must not be empty"); |
| 795 | } |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 796 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 797 | "no permission to write the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 798 | |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 799 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 800 | long identityToken = clearCallingIdentity(); |
| 801 | try { |
| 802 | SyncManager syncManager = getSyncManager(); |
| 803 | if (syncManager != null) { |
| 804 | syncManager.getSyncStorageEngine().setIsSyncable( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 805 | account, userId, providerName, syncable); |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 806 | } |
| 807 | } finally { |
| 808 | restoreCallingIdentity(identityToken); |
| 809 | } |
| 810 | } |
| 811 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 812 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 813 | public boolean getMasterSyncAutomatically() { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 814 | return getMasterSyncAutomaticallyAsUser(UserHandle.getCallingUserId()); |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * If the user id supplied is different to the calling user, the caller must hold the |
| 819 | * INTERACT_ACROSS_USERS_FULL permission. |
| 820 | */ |
| 821 | @Override |
| 822 | public boolean getMasterSyncAutomaticallyAsUser(int userId) { |
| 823 | enforceCrossUserPermission(userId, |
| 824 | "no permission to read the sync settings for user " + userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 825 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_SETTINGS, |
| 826 | "no permission to read the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 827 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 828 | long identityToken = clearCallingIdentity(); |
| 829 | try { |
| 830 | SyncManager syncManager = getSyncManager(); |
| 831 | if (syncManager != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 832 | return syncManager.getSyncStorageEngine().getMasterSyncAutomatically(userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 833 | } |
| 834 | } finally { |
| 835 | restoreCallingIdentity(identityToken); |
| 836 | } |
| 837 | return false; |
| 838 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 839 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 840 | @Override |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 841 | public void setMasterSyncAutomatically(boolean flag) { |
Alexandra Gherghina | 0e9ac20 | 2014-07-15 23:11:48 +0100 | [diff] [blame] | 842 | setMasterSyncAutomaticallyAsUser(flag, UserHandle.getCallingUserId()); |
| 843 | } |
| 844 | |
| 845 | @Override |
| 846 | public void setMasterSyncAutomaticallyAsUser(boolean flag, int userId) { |
| 847 | enforceCrossUserPermission(userId, |
| 848 | "no permission to set the sync status for user " + userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 849 | mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS, |
| 850 | "no permission to write the sync settings"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 851 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 852 | long identityToken = clearCallingIdentity(); |
| 853 | try { |
| 854 | SyncManager syncManager = getSyncManager(); |
| 855 | if (syncManager != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 856 | syncManager.getSyncStorageEngine().setMasterSyncAutomatically(flag, userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 857 | } |
| 858 | } finally { |
| 859 | restoreCallingIdentity(identityToken); |
| 860 | } |
| 861 | } |
| 862 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 863 | @Override |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 864 | public boolean isSyncActive(Account account, String authority, ComponentName cname) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 865 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 866 | "no permission to read the sync stats"); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 867 | int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 868 | long identityToken = clearCallingIdentity(); |
| 869 | try { |
| 870 | SyncManager syncManager = getSyncManager(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 871 | if (syncManager == null) { |
| 872 | return false; |
| 873 | } |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 874 | return syncManager.getSyncStorageEngine().isSyncActive( |
| 875 | new SyncStorageEngine.EndPoint(account, authority, userId)); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 876 | } finally { |
| 877 | restoreCallingIdentity(identityToken); |
| 878 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 879 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 880 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 881 | @Override |
Fred Quintana | c6a6955 | 2010-09-27 17:05:04 -0700 | [diff] [blame] | 882 | public List<SyncInfo> getCurrentSyncs() { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 883 | return getCurrentSyncsAsUser(UserHandle.getCallingUserId()); |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * If the user id supplied is different to the calling user, the caller must hold the |
| 888 | * INTERACT_ACROSS_USERS_FULL permission. |
| 889 | */ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 890 | @Override |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 891 | public List<SyncInfo> getCurrentSyncsAsUser(int userId) { |
| 892 | enforceCrossUserPermission(userId, |
| 893 | "no permission to read the sync settings for user " + userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 894 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 895 | "no permission to read the sync stats"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 896 | |
Matthew Williams | f39549e | 2016-01-19 23:04:04 +0000 | [diff] [blame] | 897 | final boolean canAccessAccounts = |
| 898 | mContext.checkCallingOrSelfPermission(Manifest.permission.GET_ACCOUNTS) |
| 899 | == PackageManager.PERMISSION_GRANTED; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 900 | long identityToken = clearCallingIdentity(); |
| 901 | try { |
Matthew Williams | f39549e | 2016-01-19 23:04:04 +0000 | [diff] [blame] | 902 | return getSyncManager().getSyncStorageEngine() |
| 903 | .getCurrentSyncsCopy(userId, canAccessAccounts); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 904 | } finally { |
| 905 | restoreCallingIdentity(identityToken); |
| 906 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 907 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 908 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 909 | @Override |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 910 | public SyncStatusInfo getSyncStatus(Account account, String authority, ComponentName cname) { |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 911 | return getSyncStatusAsUser(account, authority, cname, UserHandle.getCallingUserId()); |
| 912 | } |
| 913 | |
| 914 | /** |
| 915 | * If the user id supplied is different to the calling user, the caller must hold the |
| 916 | * INTERACT_ACROSS_USERS_FULL permission. |
| 917 | */ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 918 | @Override |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 919 | public SyncStatusInfo getSyncStatusAsUser(Account account, String authority, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 920 | ComponentName cname, int userId) { |
Matthew Williams | 632515b | 2013-10-10 15:51:00 -0700 | [diff] [blame] | 921 | if (TextUtils.isEmpty(authority)) { |
| 922 | throw new IllegalArgumentException("Authority must not be empty"); |
| 923 | } |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 924 | |
| 925 | enforceCrossUserPermission(userId, |
| 926 | "no permission to read the sync stats for user " + userId); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 927 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 928 | "no permission to read the sync stats"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 929 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 930 | long identityToken = clearCallingIdentity(); |
| 931 | try { |
| 932 | SyncManager syncManager = getSyncManager(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 933 | if (syncManager == null) { |
| 934 | return null; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 935 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 936 | SyncStorageEngine.EndPoint info; |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 937 | if (!(account == null || authority == null)) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 938 | info = new SyncStorageEngine.EndPoint(account, authority, userId); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 939 | } else { |
| 940 | throw new IllegalArgumentException("Must call sync status with valid authority"); |
| 941 | } |
| 942 | return syncManager.getSyncStorageEngine().getStatusByAuthority(info); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 943 | } finally { |
| 944 | restoreCallingIdentity(identityToken); |
| 945 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 946 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 947 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 948 | @Override |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 949 | public boolean isSyncPending(Account account, String authority, ComponentName cname) { |
Alexandra Gherghina | cb22807 | 2014-07-01 15:14:11 +0100 | [diff] [blame] | 950 | return isSyncPendingAsUser(account, authority, cname, UserHandle.getCallingUserId()); |
| 951 | } |
| 952 | |
| 953 | @Override |
| 954 | public boolean isSyncPendingAsUser(Account account, String authority, ComponentName cname, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 955 | int userId) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 956 | mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_SYNC_STATS, |
| 957 | "no permission to read the sync stats"); |
Alexandra Gherghina | cb22807 | 2014-07-01 15:14:11 +0100 | [diff] [blame] | 958 | enforceCrossUserPermission(userId, |
| 959 | "no permission to retrieve the sync settings for user " + userId); |
Torne (Richard Coles) | 4890b08 | 2013-08-12 10:26:57 +0000 | [diff] [blame] | 960 | long identityToken = clearCallingIdentity(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 961 | SyncManager syncManager = getSyncManager(); |
| 962 | if (syncManager == null) return false; |
| 963 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 964 | try { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 965 | SyncStorageEngine.EndPoint info; |
Matthew Williams | 5a9decd | 2014-06-04 09:25:11 -0700 | [diff] [blame] | 966 | if (!(account == null || authority == null)) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 967 | info = new SyncStorageEngine.EndPoint(account, authority, userId); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 968 | } else { |
| 969 | throw new IllegalArgumentException("Invalid authority specified"); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 970 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 971 | return syncManager.getSyncStorageEngine().isSyncPending(info); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 972 | } finally { |
| 973 | restoreCallingIdentity(identityToken); |
| 974 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 975 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 976 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 977 | @Override |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 978 | public void addStatusChangeListener(int mask, ISyncStatusObserver callback) { |
| 979 | long identityToken = clearCallingIdentity(); |
| 980 | try { |
| 981 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 982 | if (syncManager != null && callback != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 983 | syncManager.getSyncStorageEngine().addStatusChangeListener(mask, callback); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 984 | } |
| 985 | } finally { |
| 986 | restoreCallingIdentity(identityToken); |
| 987 | } |
| 988 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 989 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 990 | @Override |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 991 | public void removeStatusChangeListener(ISyncStatusObserver callback) { |
| 992 | long identityToken = clearCallingIdentity(); |
| 993 | try { |
| 994 | SyncManager syncManager = getSyncManager(); |
Fred Quintana | 1b487ec | 2010-02-26 10:57:55 -0800 | [diff] [blame] | 995 | if (syncManager != null && callback != null) { |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 996 | syncManager.getSyncStorageEngine().removeStatusChangeListener(callback); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 997 | } |
| 998 | } finally { |
| 999 | restoreCallingIdentity(identityToken); |
| 1000 | } |
| 1001 | } |
Costin Manolache | b752098 | 2009-09-02 18:03:05 -0700 | [diff] [blame] | 1002 | |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 1003 | private @Nullable String getProviderPackageName(Uri uri) { |
| 1004 | final ProviderInfo pi = mContext.getPackageManager() |
| 1005 | .resolveContentProvider(uri.getAuthority(), 0); |
| 1006 | return (pi != null) ? pi.packageName : null; |
| 1007 | } |
| 1008 | |
| 1009 | private ArrayMap<Pair<String, Uri>, Bundle> findOrCreateCacheLocked(int userId, |
| 1010 | String providerPackageName) { |
| 1011 | ArrayMap<String, ArrayMap<Pair<String, Uri>, Bundle>> userCache = mCache.get(userId); |
| 1012 | if (userCache == null) { |
| 1013 | userCache = new ArrayMap<>(); |
| 1014 | mCache.put(userId, userCache); |
| 1015 | } |
| 1016 | ArrayMap<Pair<String, Uri>, Bundle> packageCache = userCache.get(providerPackageName); |
| 1017 | if (packageCache == null) { |
| 1018 | packageCache = new ArrayMap<>(); |
| 1019 | userCache.put(providerPackageName, packageCache); |
| 1020 | } |
| 1021 | return packageCache; |
| 1022 | } |
| 1023 | |
| 1024 | private void invalidateCacheLocked(int userId, String providerPackageName, Uri uri) { |
| 1025 | ArrayMap<String, ArrayMap<Pair<String, Uri>, Bundle>> userCache = mCache.get(userId); |
| 1026 | if (userCache == null) return; |
| 1027 | |
| 1028 | ArrayMap<Pair<String, Uri>, Bundle> packageCache = userCache.get(providerPackageName); |
| 1029 | if (packageCache == null) return; |
| 1030 | |
| 1031 | if (uri != null) { |
| 1032 | for (int i = 0; i < packageCache.size();) { |
| 1033 | final Uri key = packageCache.keyAt(i).second; |
| 1034 | if (Objects.equals(key, uri)) { |
| 1035 | packageCache.removeAt(i); |
| 1036 | } else { |
| 1037 | i++; |
| 1038 | } |
| 1039 | } |
| 1040 | } else { |
| 1041 | packageCache.clear(); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | @Override |
| 1046 | public void putCache(String packageName, Uri key, Bundle value, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1047 | Bundle.setDefusable(value, true); |
Jeff Sharkey | 8731408 | 2016-03-11 17:25:11 -0700 | [diff] [blame] | 1048 | enforceCrossUserPermission(userId, TAG); |
| 1049 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CACHE_CONTENT, TAG); |
| 1050 | mContext.getSystemService(AppOpsManager.class).checkPackage(Binder.getCallingUid(), |
| 1051 | packageName); |
| 1052 | |
| 1053 | final String providerPackageName = getProviderPackageName(key); |
| 1054 | final Pair<String, Uri> fullKey = Pair.create(packageName, key); |
| 1055 | |
| 1056 | synchronized (mCache) { |
| 1057 | final ArrayMap<Pair<String, Uri>, Bundle> cache = findOrCreateCacheLocked(userId, |
| 1058 | providerPackageName); |
| 1059 | if (value != null) { |
| 1060 | cache.put(fullKey, value); |
| 1061 | } else { |
| 1062 | cache.remove(fullKey); |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | @Override |
| 1068 | public Bundle getCache(String packageName, Uri key, int userId) { |
| 1069 | enforceCrossUserPermission(userId, TAG); |
| 1070 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.CACHE_CONTENT, TAG); |
| 1071 | mContext.getSystemService(AppOpsManager.class).checkPackage(Binder.getCallingUid(), |
| 1072 | packageName); |
| 1073 | |
| 1074 | final String providerPackageName = getProviderPackageName(key); |
| 1075 | final Pair<String, Uri> fullKey = Pair.create(packageName, key); |
| 1076 | |
| 1077 | synchronized (mCache) { |
| 1078 | final ArrayMap<Pair<String, Uri>, Bundle> cache = findOrCreateCacheLocked(userId, |
| 1079 | providerPackageName); |
| 1080 | return cache.get(fullKey); |
| 1081 | } |
| 1082 | } |
| 1083 | |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 1084 | public static ContentService main(Context context, boolean factoryTest) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | ContentService service = new ContentService(context, factoryTest); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1086 | ServiceManager.addService(ContentResolver.CONTENT_SERVICE_NAME, service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1087 | return service; |
| 1088 | } |
| 1089 | |
| 1090 | /** |
Alexandra Gherghina | 0363c3e | 2014-06-23 13:34:59 +0100 | [diff] [blame] | 1091 | * Checks if the request is from the system or an app that has INTERACT_ACROSS_USERS_FULL |
| 1092 | * permission, if the userHandle is not for the caller. |
| 1093 | * |
| 1094 | * @param userHandle the user handle of the user we want to act on behalf of. |
| 1095 | * @param message the message to log on security exception. |
| 1096 | */ |
| 1097 | private void enforceCrossUserPermission(int userHandle, String message) { |
| 1098 | final int callingUser = UserHandle.getCallingUserId(); |
| 1099 | if (callingUser != userHandle) { |
| 1100 | mContext.enforceCallingOrSelfPermission( |
| 1101 | Manifest.permission.INTERACT_ACROSS_USERS_FULL, message); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | * Hide this class since it is not part of api, |
| 1107 | * but current unittest framework requires it to be public |
| 1108 | * @hide |
| 1109 | */ |
| 1110 | public static final class ObserverNode { |
| 1111 | private class ObserverEntry implements IBinder.DeathRecipient { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1112 | public final IContentObserver observer; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 1113 | public final int uid; |
| 1114 | public final int pid; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1115 | public final boolean notifyForDescendants; |
| 1116 | private final int userHandle; |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1117 | private final Object observersLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 1119 | public ObserverEntry(IContentObserver o, boolean n, Object observersLock, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1120 | int _uid, int _pid, int _userHandle) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1121 | this.observersLock = observersLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | observer = o; |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 1123 | uid = _uid; |
| 1124 | pid = _pid; |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1125 | userHandle = _userHandle; |
| 1126 | notifyForDescendants = n; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | try { |
| 1128 | observer.asBinder().linkToDeath(this, 0); |
| 1129 | } catch (RemoteException e) { |
| 1130 | binderDied(); |
| 1131 | } |
| 1132 | } |
| 1133 | |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1134 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | public void binderDied() { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1136 | synchronized (observersLock) { |
| 1137 | removeObserverLocked(observer); |
| 1138 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1139 | } |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 1140 | |
| 1141 | public void dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1142 | String name, String prefix, SparseIntArray pidCounts) { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 1143 | pidCounts.put(pid, pidCounts.get(pid)+1); |
| 1144 | pw.print(prefix); pw.print(name); pw.print(": pid="); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1145 | pw.print(pid); pw.print(" uid="); |
| 1146 | pw.print(uid); pw.print(" user="); |
| 1147 | pw.print(userHandle); pw.print(" target="); |
| 1148 | pw.println(Integer.toHexString(System.identityHashCode( |
| 1149 | observer != null ? observer.asBinder() : null))); |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 1150 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | public static final int INSERT_TYPE = 0; |
| 1154 | public static final int UPDATE_TYPE = 1; |
| 1155 | public static final int DELETE_TYPE = 2; |
| 1156 | |
| 1157 | private String mName; |
| 1158 | private ArrayList<ObserverNode> mChildren = new ArrayList<ObserverNode>(); |
| 1159 | private ArrayList<ObserverEntry> mObservers = new ArrayList<ObserverEntry>(); |
| 1160 | |
| 1161 | public ObserverNode(String name) { |
| 1162 | mName = name; |
| 1163 | } |
| 1164 | |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 1165 | public void dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1166 | String name, String prefix, int[] counts, SparseIntArray pidCounts) { |
Dianne Hackborn | 1b64e0d | 2011-07-17 15:23:59 -0700 | [diff] [blame] | 1167 | String innerName = null; |
| 1168 | if (mObservers.size() > 0) { |
| 1169 | if ("".equals(name)) { |
| 1170 | innerName = mName; |
| 1171 | } else { |
| 1172 | innerName = name + "/" + mName; |
| 1173 | } |
| 1174 | for (int i=0; i<mObservers.size(); i++) { |
| 1175 | counts[1]++; |
| 1176 | mObservers.get(i).dumpLocked(fd, pw, args, innerName, prefix, |
| 1177 | pidCounts); |
| 1178 | } |
| 1179 | } |
| 1180 | if (mChildren.size() > 0) { |
| 1181 | if (innerName == null) { |
| 1182 | if ("".equals(name)) { |
| 1183 | innerName = mName; |
| 1184 | } else { |
| 1185 | innerName = name + "/" + mName; |
| 1186 | } |
| 1187 | } |
| 1188 | for (int i=0; i<mChildren.size(); i++) { |
| 1189 | counts[0]++; |
| 1190 | mChildren.get(i).dumpLocked(fd, pw, args, innerName, prefix, |
| 1191 | counts, pidCounts); |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | private String getUriSegment(Uri uri, int index) { |
| 1197 | if (uri != null) { |
| 1198 | if (index == 0) { |
| 1199 | return uri.getAuthority(); |
| 1200 | } else { |
| 1201 | return uri.getPathSegments().get(index - 1); |
| 1202 | } |
| 1203 | } else { |
| 1204 | return null; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | private int countUriSegments(Uri uri) { |
| 1209 | if (uri == null) { |
| 1210 | return 0; |
| 1211 | } |
| 1212 | return uri.getPathSegments().size() + 1; |
| 1213 | } |
| 1214 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1215 | // Invariant: userHandle is either a hard user number or is USER_ALL |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1216 | public void addObserverLocked(Uri uri, IContentObserver observer, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1217 | boolean notifyForDescendants, Object observersLock, |
| 1218 | int uid, int pid, int userHandle) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1219 | addObserverLocked(uri, 0, observer, notifyForDescendants, observersLock, |
| 1220 | uid, pid, userHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1223 | private void addObserverLocked(Uri uri, int index, IContentObserver observer, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1224 | boolean notifyForDescendants, Object observersLock, |
| 1225 | int uid, int pid, int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1226 | // If this is the leaf node add the observer |
| 1227 | if (index == countUriSegments(uri)) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1228 | mObservers.add(new ObserverEntry(observer, notifyForDescendants, observersLock, |
| 1229 | uid, pid, userHandle)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | // Look to see if the proper child already exists |
| 1234 | String segment = getUriSegment(uri, index); |
Anthony Newnam | f512664 | 2010-03-22 17:29:06 -0500 | [diff] [blame] | 1235 | if (segment == null) { |
| 1236 | throw new IllegalArgumentException("Invalid Uri (" + uri + ") used for observer"); |
| 1237 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | int N = mChildren.size(); |
| 1239 | for (int i = 0; i < N; i++) { |
| 1240 | ObserverNode node = mChildren.get(i); |
| 1241 | if (node.mName.equals(segment)) { |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1242 | node.addObserverLocked(uri, index + 1, observer, notifyForDescendants, |
| 1243 | observersLock, uid, pid, userHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1244 | return; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | // No child found, create one |
| 1249 | ObserverNode node = new ObserverNode(segment); |
| 1250 | mChildren.add(node); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1251 | node.addObserverLocked(uri, index + 1, observer, notifyForDescendants, |
| 1252 | observersLock, uid, pid, userHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1255 | public boolean removeObserverLocked(IContentObserver observer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1256 | int size = mChildren.size(); |
| 1257 | for (int i = 0; i < size; i++) { |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1258 | boolean empty = mChildren.get(i).removeObserverLocked(observer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 | if (empty) { |
| 1260 | mChildren.remove(i); |
| 1261 | i--; |
| 1262 | size--; |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | IBinder observerBinder = observer.asBinder(); |
| 1267 | size = mObservers.size(); |
| 1268 | for (int i = 0; i < size; i++) { |
| 1269 | ObserverEntry entry = mObservers.get(i); |
| 1270 | if (entry.observer.asBinder() == observerBinder) { |
| 1271 | mObservers.remove(i); |
| 1272 | // We no longer need to listen for death notifications. Remove it. |
| 1273 | observerBinder.unlinkToDeath(entry, 0); |
| 1274 | break; |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | if (mChildren.size() == 0 && mObservers.size() == 0) { |
| 1279 | return true; |
| 1280 | } |
| 1281 | return false; |
| 1282 | } |
| 1283 | |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1284 | private void collectMyObserversLocked(boolean leaf, IContentObserver observer, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1285 | boolean observerWantsSelfNotifications, int targetUserHandle, |
| 1286 | ArrayList<ObserverCall> calls) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | int N = mObservers.size(); |
| 1288 | IBinder observerBinder = observer == null ? null : observer.asBinder(); |
| 1289 | for (int i = 0; i < N; i++) { |
| 1290 | ObserverEntry entry = mObservers.get(i); |
| 1291 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1292 | // 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] | 1293 | // in self notifications |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 1294 | boolean selfChange = (entry.observer.asBinder() == observerBinder); |
| 1295 | if (selfChange && !observerWantsSelfNotifications) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | continue; |
| 1297 | } |
| 1298 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1299 | // Does this observer match the target user? |
| 1300 | if (targetUserHandle == UserHandle.USER_ALL |
| 1301 | || entry.userHandle == UserHandle.USER_ALL |
| 1302 | || targetUserHandle == entry.userHandle) { |
| 1303 | // Make sure the observer is interested in the notification |
| 1304 | if (leaf || (!leaf && entry.notifyForDescendants)) { |
| 1305 | calls.add(new ObserverCall(this, entry.observer, selfChange)); |
| 1306 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1311 | /** |
| 1312 | * targetUserHandle is either a hard user handle or is USER_ALL |
| 1313 | */ |
Fred Quintana | 002ffad5 | 2010-03-02 11:18:16 -0800 | [diff] [blame] | 1314 | public void collectObserversLocked(Uri uri, int index, IContentObserver observer, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1315 | boolean observerWantsSelfNotifications, int targetUserHandle, |
| 1316 | ArrayList<ObserverCall> calls) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | String segment = null; |
| 1318 | int segmentCount = countUriSegments(uri); |
| 1319 | if (index >= segmentCount) { |
| 1320 | // This is the leaf node, notify all observers |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1321 | collectMyObserversLocked(true, observer, observerWantsSelfNotifications, |
| 1322 | targetUserHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | } else if (index < segmentCount){ |
| 1324 | segment = getUriSegment(uri, index); |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1325 | // Notify any observers at this level who are interested in descendants |
| 1326 | collectMyObserversLocked(false, observer, observerWantsSelfNotifications, |
| 1327 | targetUserHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | int N = mChildren.size(); |
| 1331 | for (int i = 0; i < N; i++) { |
| 1332 | ObserverNode node = mChildren.get(i); |
| 1333 | if (segment == null || node.mName.equals(segment)) { |
| 1334 | // We found the child, |
Jeff Brown | 86de059 | 2012-01-23 13:01:18 -0800 | [diff] [blame] | 1335 | node.collectObserversLocked(uri, index + 1, |
Christopher Tate | 16aa973 | 2012-09-17 16:23:44 -0700 | [diff] [blame] | 1336 | observer, observerWantsSelfNotifications, targetUserHandle, calls); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1337 | if (segment != null) { |
| 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | } |