blob: ab72fe6edfd7d0d92f776f035594253499037d43 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 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
Dianne Hackbornc1309d72012-05-08 18:54:22 -070017#define LOG_TAG "misc"
18
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080019#include <utils/misc.h>
Steven Moreland8da96132017-04-13 15:17:59 -070020
21#include <pthread.h>
22
Dianne Hackbornc1309d72012-05-08 18:54:22 -070023#include <utils/Log.h>
Dianne Hackbornc1309d72012-05-08 18:54:22 -070024#include <utils/Vector.h>
25
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026using namespace android;
27
28namespace android {
29
Dianne Hackbornc1309d72012-05-08 18:54:22 -070030struct sysprop_change_callback_info {
31 sysprop_change_callback callback;
32 int priority;
33};
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080034
Yabin Cui4a6e5a32015-01-26 19:48:54 -080035#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070036static pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER;
37static Vector<sysprop_change_callback_info>* gSyspropList = NULL;
38#endif
39
40void add_sysprop_change_callback(sysprop_change_callback cb, int priority) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -080041#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070042 pthread_mutex_lock(&gSyspropMutex);
43 if (gSyspropList == NULL) {
44 gSyspropList = new Vector<sysprop_change_callback_info>();
45 }
46 sysprop_change_callback_info info;
47 info.callback = cb;
48 info.priority = priority;
49 bool added = false;
50 for (size_t i=0; i<gSyspropList->size(); i++) {
51 if (priority >= gSyspropList->itemAt(i).priority) {
52 gSyspropList->insertAt(info, i);
53 added = true;
54 break;
55 }
56 }
57 if (!added) {
58 gSyspropList->add(info);
59 }
60 pthread_mutex_unlock(&gSyspropMutex);
61#endif
62}
63
64void report_sysprop_change() {
Yabin Cui4a6e5a32015-01-26 19:48:54 -080065#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070066 pthread_mutex_lock(&gSyspropMutex);
67 Vector<sysprop_change_callback_info> listeners;
68 if (gSyspropList != NULL) {
69 listeners = *gSyspropList;
70 }
71 pthread_mutex_unlock(&gSyspropMutex);
72
73 //ALOGI("Reporting sysprop change to %d listeners", listeners.size());
74 for (size_t i=0; i<listeners.size(); i++) {
75 listeners[i].callback();
76 }
77#endif
78}
79
80}; // namespace android