Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [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 | #include <binder/AppOpsManager.h> |
| 18 | #include <binder/IServiceManager.h> |
| 19 | |
| 20 | #include <utils/SystemClock.h> |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | static String16 _appops("appops"); |
| 25 | |
| 26 | AppOpsManager::AppOpsManager() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | sp<IAppOpsService> AppOpsManager::getService() |
| 31 | { |
| 32 | int64_t startTime = 0; |
| 33 | mLock.lock(); |
| 34 | sp<IAppOpsService> service = mService; |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame^] | 35 | while (service == NULL || !service->asBinder()->isBinderAlive()) { |
| 36 | sp<IBinder> binder = defaultServiceManager()->checkService(_appops); |
| 37 | if (binder == NULL) { |
| 38 | // Wait for the app ops service to come back... |
| 39 | if (startTime == 0) { |
| 40 | startTime = uptimeMillis(); |
| 41 | ALOGI("Waiting for app ops service"); |
| 42 | } else if ((uptimeMillis()-startTime) > 10000) { |
| 43 | ALOGW("Waiting too long for app ops service, giving up"); |
| 44 | return NULL; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 45 | } |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame^] | 46 | sleep(1); |
| 47 | } else { |
| 48 | service = interface_cast<IAppOpsService>(binder); |
| 49 | mService = service; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | mLock.unlock(); |
| 53 | return service; |
| 54 | } |
| 55 | |
| 56 | int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage) |
| 57 | { |
| 58 | sp<IAppOpsService> service = getService(); |
| 59 | return service != NULL ? service->checkOperation(op, uid, callingPackage) : MODE_IGNORED; |
| 60 | } |
| 61 | |
| 62 | int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) { |
| 63 | sp<IAppOpsService> service = getService(); |
| 64 | return service != NULL ? service->noteOperation(op, uid, callingPackage) : MODE_IGNORED; |
| 65 | } |
| 66 | |
| 67 | int32_t AppOpsManager::startOp(int32_t op, int32_t uid, const String16& callingPackage) { |
| 68 | sp<IAppOpsService> service = getService(); |
| 69 | return service != NULL ? service->startOperation(op, uid, callingPackage) : MODE_IGNORED; |
| 70 | } |
| 71 | |
| 72 | void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) { |
| 73 | sp<IAppOpsService> service = getService(); |
| 74 | if (service != NULL) { |
| 75 | service->finishOperation(op, uid, callingPackage); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName, |
| 80 | const sp<IAppOpsCallback>& callback) { |
| 81 | sp<IAppOpsService> service = getService(); |
| 82 | if (service != NULL) { |
| 83 | service->startWatchingMode(op, packageName, callback); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) { |
| 88 | sp<IAppOpsService> service = getService(); |
| 89 | if (service != NULL) { |
| 90 | service->stopWatchingMode(callback); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | }; // namespace android |