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