/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.os;
import java.util.ArrayList;
import java.util.HashMap;
/**
* UEventObserver is an abstract class that receives UEvent's from the kernel.
*
* Subclass UEventObserver, implementing onUEvent(UEvent event), then call
* startObserving() with a match string. The UEvent thread will then call your
* onUEvent() method when a UEvent occurs that contains your match string.
*
* Call stopObserving() to stop receiving UEvent's.
*
* There is only one UEvent thread per process, even if that process has
* multiple UEventObserver subclass instances. The UEvent thread starts when
* the startObserving() is called for the first time in that process. Once
* started the UEvent thread will not stop (although it can stop notifying
* UEventObserver's via stopObserving()).
*
* @hide
*/
public abstract class UEventObserver {
private static final String TAG = UEventObserver.class.getSimpleName();
/**
* Representation of a UEvent.
*/
static public class UEvent {
// collection of key=value pairs parsed from the uevent message
public HashMap mMap = new HashMap();
public UEvent(String message) {
int offset = 0;
int length = message.length();
while (offset < length) {
int equals = message.indexOf('=', offset);
int at = message.indexOf(0, offset);
if (at < 0) break;
if (equals > offset && equals < at) {
// key is before the equals sign, and value is after
mMap.put(message.substring(offset, equals),
message.substring(equals + 1, at));
}
offset = at + 1;
}
}
public String get(String key) {
return mMap.get(key);
}
public String get(String key, String defaultValue) {
String result = mMap.get(key);
return (result == null ? defaultValue : result);
}
public String toString() {
return mMap.toString();
}
}
private static UEventThread sThread;
private static boolean sThreadStarted = false;
private static class UEventThread extends Thread {
/** Many to many mapping of string match to observer.
* Multimap would be better, but not available in android, so use
* an ArrayList where even elements are the String match and odd
* elements the corresponding UEventObserver observer */
private ArrayList