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