The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 17 | #define LOG_TAG "misc" |
| 18 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | // |
| 20 | // Miscellaneous utility functions. |
| 21 | // |
| 22 | #include <utils/misc.h> |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | #include <sys/stat.h> |
| 26 | #include <string.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 29 | #if !defined(_WIN32) |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 30 | # include <pthread.h> |
| 31 | #endif |
| 32 | |
| 33 | #include <utils/Vector.h> |
| 34 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | using namespace android; |
| 36 | |
| 37 | namespace android { |
| 38 | |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 39 | struct sysprop_change_callback_info { |
| 40 | sysprop_change_callback callback; |
| 41 | int priority; |
| 42 | }; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 44 | #if !defined(_WIN32) |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 45 | static pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER; |
| 46 | static Vector<sysprop_change_callback_info>* gSyspropList = NULL; |
| 47 | #endif |
| 48 | |
| 49 | void add_sysprop_change_callback(sysprop_change_callback cb, int priority) { |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 50 | #if !defined(_WIN32) |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 51 | pthread_mutex_lock(&gSyspropMutex); |
| 52 | if (gSyspropList == NULL) { |
| 53 | gSyspropList = new Vector<sysprop_change_callback_info>(); |
| 54 | } |
| 55 | sysprop_change_callback_info info; |
| 56 | info.callback = cb; |
| 57 | info.priority = priority; |
| 58 | bool added = false; |
| 59 | for (size_t i=0; i<gSyspropList->size(); i++) { |
| 60 | if (priority >= gSyspropList->itemAt(i).priority) { |
| 61 | gSyspropList->insertAt(info, i); |
| 62 | added = true; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | if (!added) { |
| 67 | gSyspropList->add(info); |
| 68 | } |
| 69 | pthread_mutex_unlock(&gSyspropMutex); |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | void report_sysprop_change() { |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 74 | #if !defined(_WIN32) |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 75 | pthread_mutex_lock(&gSyspropMutex); |
| 76 | Vector<sysprop_change_callback_info> listeners; |
| 77 | if (gSyspropList != NULL) { |
| 78 | listeners = *gSyspropList; |
| 79 | } |
| 80 | pthread_mutex_unlock(&gSyspropMutex); |
| 81 | |
| 82 | //ALOGI("Reporting sysprop change to %d listeners", listeners.size()); |
| 83 | for (size_t i=0; i<listeners.size(); i++) { |
| 84 | listeners[i].callback(); |
| 85 | } |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | }; // namespace android |