Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
| 17 | package com.android.server.print; |
| 18 | |
| 19 | import android.Manifest; |
| 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 26 | import android.database.ContentObserver; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Binder; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 29 | import android.os.Process; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 30 | import android.os.UserHandle; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 31 | import android.print.IPrintClient; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 32 | import android.print.IPrintDocumentAdapter; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 33 | import android.print.IPrintManager; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 34 | import android.print.PrintAttributes; |
| 35 | import android.print.PrintJobInfo; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 36 | import android.provider.Settings; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 37 | import android.util.SparseArray; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 38 | |
| 39 | import com.android.internal.content.PackageMonitor; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 40 | import com.android.internal.os.BackgroundThread; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 41 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 42 | import java.util.Iterator; |
| 43 | import java.util.List; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 44 | import java.util.Set; |
| 45 | |
| 46 | public final class PrintManagerService extends IPrintManager.Stub { |
| 47 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 48 | private static final char COMPONENT_NAME_SEPARATOR = ':'; |
| 49 | |
| 50 | private final Object mLock = new Object(); |
| 51 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 52 | private final Context mContext; |
| 53 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 54 | private final SparseArray<UserState> mUserStates = new SparseArray<UserState>(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 55 | |
| 56 | private int mCurrentUserId = UserHandle.USER_OWNER; |
| 57 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 58 | public PrintManagerService(Context context) { |
| 59 | mContext = context; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 60 | registerContentObservers(); |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 61 | registerBoradcastReceivers(); |
| 62 | } |
| 63 | |
| 64 | public void systemRuning() { |
| 65 | BackgroundThread.getHandler().post(new Runnable() { |
| 66 | @Override |
| 67 | public void run() { |
| 68 | synchronized (mLock) { |
| 69 | UserState userState = getCurrentUserStateLocked(); |
| 70 | userState.updateIfNeededLocked(); |
| 71 | userState.getSpoolerLocked().notifyClientForActivteJobs(); |
| 72 | } |
| 73 | } |
| 74 | }); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 78 | public PrintJobInfo print(String printJobName, IPrintClient client, |
| 79 | IPrintDocumentAdapter documentAdapter, PrintAttributes attributes, int appId, |
| 80 | int userId) { |
| 81 | final int resolvedAppId = resolveCallingAppEnforcingPermissions(appId); |
| 82 | final int resolvedUserId = resolveCallingUserEnforcingPermissions(userId); |
| 83 | final UserState userState; |
| 84 | final RemotePrintSpooler spooler; |
| 85 | synchronized (mLock) { |
| 86 | userState = getOrCreateUserStateLocked(resolvedUserId); |
| 87 | spooler = userState.getSpoolerLocked(); |
| 88 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 89 | final long identity = Binder.clearCallingIdentity(); |
| 90 | try { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 91 | return spooler.createPrintJob(printJobName, client, documentAdapter, |
| 92 | attributes, resolvedAppId); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 93 | } finally { |
| 94 | Binder.restoreCallingIdentity(identity); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | @Override |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 99 | public List<PrintJobInfo> getPrintJobInfos(int appId, int userId) { |
| 100 | final int resolvedAppId = resolveCallingAppEnforcingPermissions(appId); |
| 101 | final int resolvedUserId = resolveCallingUserEnforcingPermissions(userId); |
| 102 | final UserState userState; |
| 103 | final RemotePrintSpooler spooler; |
| 104 | synchronized (mLock) { |
| 105 | userState = getOrCreateUserStateLocked(resolvedUserId); |
| 106 | spooler = userState.getSpoolerLocked(); |
| 107 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 108 | final long identity = Binder.clearCallingIdentity(); |
| 109 | try { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 110 | return spooler.getPrintJobInfos(null, PrintJobInfo.STATE_ANY, |
| 111 | resolvedAppId); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 112 | } finally { |
| 113 | Binder.restoreCallingIdentity(identity); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 118 | public PrintJobInfo getPrintJobInfo(int printJobId, int appId, int userId) { |
| 119 | final int resolvedAppId = resolveCallingAppEnforcingPermissions(appId); |
| 120 | final int resolvedUserId = resolveCallingUserEnforcingPermissions(userId); |
| 121 | final UserState userState; |
| 122 | final RemotePrintSpooler spooler; |
| 123 | synchronized (mLock) { |
| 124 | userState = getOrCreateUserStateLocked(resolvedUserId); |
| 125 | spooler = userState.getSpoolerLocked(); |
| 126 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 127 | final long identity = Binder.clearCallingIdentity(); |
| 128 | try { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 129 | return spooler.getPrintJobInfo(printJobId, resolvedAppId); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 130 | } finally { |
| 131 | Binder.restoreCallingIdentity(identity); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void cancelPrintJob(int printJobId, int appId, int userId) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 137 | final int resolvedAppId = resolveCallingAppEnforcingPermissions(appId); |
| 138 | final int resolvedUserId = resolveCallingUserEnforcingPermissions(userId); |
| 139 | final UserState userState; |
| 140 | final RemotePrintSpooler spooler; |
| 141 | synchronized (mLock) { |
| 142 | userState = getOrCreateUserStateLocked(resolvedUserId); |
| 143 | spooler = userState.getSpoolerLocked(); |
| 144 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 145 | final long identity = Binder.clearCallingIdentity(); |
| 146 | try { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 147 | if (spooler.cancelPrintJob(printJobId, resolvedAppId)) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 148 | return; |
| 149 | } |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 150 | PrintJobInfo printJobInfo = getPrintJobInfo(printJobId, resolvedAppId, resolvedUserId); |
| 151 | if (printJobInfo == null) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 152 | return; |
| 153 | } |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 154 | ComponentName printServiceName = printJobInfo.getPrinterId().getService(); |
| 155 | RemotePrintService printService = null; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 156 | synchronized (mLock) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 157 | printService = userState.getActiveServices().get(printServiceName); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 158 | } |
| 159 | if (printService == null) { |
| 160 | return; |
| 161 | } |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 162 | printService.onRequestCancelPrintJob(printJobInfo); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 163 | } finally { |
| 164 | Binder.restoreCallingIdentity(identity); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private void registerContentObservers() { |
| 169 | final Uri enabledPrintServicesUri = Settings.Secure.getUriFor( |
| 170 | Settings.Secure.ENABLED_PRINT_SERVICES); |
| 171 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 172 | ContentObserver observer = new ContentObserver(BackgroundThread.getHandler()) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 173 | @Override |
| 174 | public void onChange(boolean selfChange, Uri uri) { |
| 175 | if (enabledPrintServicesUri.equals(uri)) { |
| 176 | synchronized (mLock) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 177 | UserState userState = getCurrentUserStateLocked(); |
| 178 | userState.updateIfNeededLocked(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | mContext.getContentResolver().registerContentObserver(enabledPrintServicesUri, |
| 185 | false, observer, UserHandle.USER_ALL); |
| 186 | } |
| 187 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 188 | private void registerBoradcastReceivers() { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 189 | PackageMonitor monitor = new PackageMonitor() { |
| 190 | @Override |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 191 | public boolean onPackageChanged(String packageName, int uid, String[] components) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 192 | synchronized (mLock) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 193 | UserState userState = getOrCreateUserStateLocked(getChangingUserId()); |
| 194 | Iterator<ComponentName> iterator = userState.getEnabledServices().iterator(); |
| 195 | while (iterator.hasNext()) { |
| 196 | ComponentName componentName = iterator.next(); |
| 197 | if (packageName.equals(componentName.getPackageName())) { |
| 198 | userState.updateIfNeededLocked(); |
| 199 | return true; |
| 200 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 203 | return false; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void onPackageRemoved(String packageName, int uid) { |
| 208 | synchronized (mLock) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 209 | UserState userState = getOrCreateUserStateLocked(getChangingUserId()); |
| 210 | Iterator<ComponentName> iterator = userState.getEnabledServices().iterator(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 211 | while (iterator.hasNext()) { |
| 212 | ComponentName componentName = iterator.next(); |
| 213 | if (packageName.equals(componentName.getPackageName())) { |
| 214 | iterator.remove(); |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 215 | persistComponentNamesToSettingLocked( |
| 216 | Settings.Secure.ENABLED_PRINT_SERVICES, |
| 217 | userState.getEnabledServices(), getChangingUserId()); |
| 218 | userState.updateIfNeededLocked(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public boolean onHandleForceStop(Intent intent, String[] stoppedPackages, |
| 227 | int uid, boolean doit) { |
| 228 | synchronized (mLock) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 229 | UserState userState = getOrCreateUserStateLocked(getChangingUserId()); |
| 230 | boolean stoppedSomePackages = false; |
| 231 | Iterator<ComponentName> iterator = userState.getEnabledServices().iterator(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 232 | while (iterator.hasNext()) { |
| 233 | ComponentName componentName = iterator.next(); |
| 234 | String componentPackage = componentName.getPackageName(); |
| 235 | for (String stoppedPackage : stoppedPackages) { |
| 236 | if (componentPackage.equals(stoppedPackage)) { |
| 237 | if (!doit) { |
| 238 | return true; |
| 239 | } |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 240 | stoppedSomePackages = true; |
| 241 | break; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 245 | if (stoppedSomePackages) { |
| 246 | userState.updateIfNeededLocked(); |
| 247 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 252 | private void persistComponentNamesToSettingLocked(String settingName, |
| 253 | Set<ComponentName> componentNames, int userId) { |
| 254 | StringBuilder builder = new StringBuilder(); |
| 255 | for (ComponentName componentName : componentNames) { |
| 256 | if (builder.length() > 0) { |
| 257 | builder.append(COMPONENT_NAME_SEPARATOR); |
| 258 | } |
| 259 | builder.append(componentName.flattenToShortString()); |
| 260 | } |
| 261 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
| 262 | settingName, builder.toString(), userId); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 263 | } |
| 264 | }; |
| 265 | |
| 266 | // package changes |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 267 | monitor.register(mContext, BackgroundThread.getHandler().getLooper(), |
| 268 | UserHandle.ALL, true); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 269 | |
| 270 | // user changes |
| 271 | IntentFilter intentFilter = new IntentFilter(); |
| 272 | intentFilter.addAction(Intent.ACTION_USER_SWITCHED); |
| 273 | |
| 274 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 275 | @Override |
| 276 | public void onReceive(Context context, Intent intent) { |
| 277 | String action = intent.getAction(); |
| 278 | if (Intent.ACTION_USER_SWITCHED.equals(action)) { |
| 279 | switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 280 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 281 | removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 284 | }, UserHandle.ALL, intentFilter, null, BackgroundThread.getHandler()); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 287 | private UserState getCurrentUserStateLocked() { |
| 288 | return getOrCreateUserStateLocked(mCurrentUserId); |
| 289 | } |
| 290 | |
| 291 | private UserState getOrCreateUserStateLocked(int userId) { |
| 292 | UserState userState = mUserStates.get(userId); |
| 293 | if (userState == null) { |
| 294 | userState = new UserState(mContext, userId, mLock); |
| 295 | mUserStates.put(userId, userState); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 296 | } |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 297 | return userState; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | private void switchUser(int newUserId) { |
| 301 | synchronized (mLock) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 302 | if (newUserId == mCurrentUserId) { |
| 303 | return; |
| 304 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 305 | mCurrentUserId = newUserId; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 306 | UserState userState = getCurrentUserStateLocked(); |
| 307 | userState.updateIfNeededLocked(); |
| 308 | userState.getSpoolerLocked().notifyClientForActivteJobs(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 312 | private void removeUser(int removedUserId) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 313 | synchronized (mLock) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 314 | UserState userState = mUserStates.get(removedUserId); |
| 315 | if (userState != null) { |
| 316 | userState.destroyLocked(); |
| 317 | mUserStates.remove(removedUserId); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 322 | private int resolveCallingAppEnforcingPermissions(int appId) { |
| 323 | final int callingUid = Binder.getCallingUid(); |
| 324 | if (callingUid == 0 || callingUid == Process.SYSTEM_UID |
| 325 | || callingUid == Process.SHELL_UID) { |
| 326 | return appId; |
| 327 | } |
| 328 | final int callingAppId = UserHandle.getAppId(callingUid); |
| 329 | if (appId == callingAppId) { |
| 330 | return appId; |
| 331 | } |
| 332 | if (mContext.checkCallingPermission(Manifest.permission.ACCESS_ALL_PRINT_JOBS) |
| 333 | != PackageManager.PERMISSION_GRANTED) { |
| 334 | throw new SecurityException("Call from app " + callingAppId + " as app " |
| 335 | + appId + " without permission ACCESS_ALL_PRINT_JOBS"); |
| 336 | } |
| 337 | return appId; |
| 338 | } |
| 339 | |
| 340 | private int resolveCallingUserEnforcingPermissions(int userId) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 341 | final int callingUid = Binder.getCallingUid(); |
| 342 | if (callingUid == 0 || callingUid == Process.SYSTEM_UID |
| 343 | || callingUid == Process.SHELL_UID) { |
| 344 | return userId; |
| 345 | } |
| 346 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 347 | if (callingUserId == userId) { |
| 348 | return userId; |
| 349 | } |
| 350 | if (mContext.checkCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 351 | != PackageManager.PERMISSION_GRANTED |
| 352 | || mContext.checkCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS) |
| 353 | != PackageManager.PERMISSION_GRANTED) { |
| 354 | if (userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 355 | return callingUserId; |
| 356 | } |
| 357 | throw new SecurityException("Call from user " + callingUserId + " as user " |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 358 | + userId + " without permission INTERACT_ACROSS_USERS or " |
| 359 | + "INTERACT_ACROSS_USERS_FULL not allowed."); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 360 | } |
| 361 | if (userId == UserHandle.USER_CURRENT || userId == UserHandle.USER_CURRENT_OR_SELF) { |
| 362 | return mCurrentUserId; |
| 363 | } |
| 364 | throw new IllegalArgumentException("Calling user can be changed to only " |
| 365 | + "UserHandle.USER_CURRENT or UserHandle.USER_CURRENT_OR_SELF."); |
| 366 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 367 | } |