| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.util.Log; |
| 21 |
|
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.HashMap; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public abstract class UEventObserver { |
| 43 |
private static final String TAG = "UEventObserver"; |
| 44 |
private static final boolean DEBUG = false; |
| 45 |
|
| 46 |
private static UEventThread sThread; |
| 47 |
|
| 48 |
private static native void nativeSetup(); |
| 49 |
private static native String nativeWaitForNextEvent(); |
| 50 |
private static native void nativeAddMatch(String match); |
| 51 |
private static native void nativeRemoveMatch(String match); |
| 52 |
|
| 53 |
@UnsupportedAppUsage |
| 54 |
public UEventObserver() { |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
protected void finalize() throws Throwable { |
| 59 |
try { |
| 60 |
stopObserving(); |
| 61 |
} finally { |
| 62 |
super.finalize(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
private static UEventThread getThread() { |
| 67 |
synchronized (UEventObserver.class) { |
| 68 |
if (sThread == null) { |
| 69 |
sThread = new UEventThread(); |
| 70 |
sThread.start(); |
| 71 |
} |
| 72 |
return sThread; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
private static UEventThread peekThread() { |
| 77 |
synchronized (UEventObserver.class) { |
| 78 |
return sThread; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
@UnsupportedAppUsage |
| 99 |
public final void startObserving(String match) { |
| 100 |
if (match == null || match.isEmpty()) { |
| 101 |
throw new IllegalArgumentException("match substring must be non-empty"); |
| 102 |
} |
| 103 |
|
| 104 |
final UEventThread t = getThread(); |
| 105 |
t.addObserver(match, this); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
@UnsupportedAppUsage |
| 114 |
public final void stopObserving() { |
| 115 |
final UEventThread t = peekThread(); |
| 116 |
if (t != null) { |
| 117 |
t.removeObserver(this); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@UnsupportedAppUsage |
| 126 |
public abstract void onUEvent(UEvent event); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public static final class UEvent { |
| 132 |
|
| 133 |
private final HashMap<String,String> mMap = new HashMap<String,String>(); |
| 134 |
|
| 135 |
public UEvent(String message) { |
| 136 |
int offset = 0; |
| 137 |
int length = message.length(); |
| 138 |
|
| 139 |
while (offset < length) { |
| 140 |
int equals = message.indexOf('=', offset); |
| 141 |
int at = message.indexOf('\0', offset); |
| 142 |
if (at < 0) break; |
| 143 |
|
| 144 |
if (equals > offset && equals < at) { |
| 145 |
|
| 146 |
mMap.put(message.substring(offset, equals), |
| 147 |
message.substring(equals + 1, at)); |
| 148 |
} |
| 149 |
|
| 150 |
offset = at + 1; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
@UnsupportedAppUsage |
| 155 |
public String get(String key) { |
| 156 |
return mMap.get(key); |
| 157 |
} |
| 158 |
|
| 159 |
@UnsupportedAppUsage |
| 160 |
public String get(String key, String defaultValue) { |
| 161 |
String result = mMap.get(key); |
| 162 |
return (result == null ? defaultValue : result); |
| 163 |
} |
| 164 |
|
| 165 |
public String toString() { |
| 166 |
return mMap.toString(); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
private static final class UEventThread extends Thread { |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
private final ArrayList<Object> mKeysAndObservers = new ArrayList<Object>(); |
| 176 |
|
| 177 |
private final ArrayList<UEventObserver> mTempObserversToSignal = |
| 178 |
new ArrayList<UEventObserver>(); |
| 179 |
|
| 180 |
public UEventThread() { |
| 181 |
super("UEventObserver"); |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public void run() { |
| 186 |
nativeSetup(); |
| 187 |
|
| 188 |
while (true) { |
| 189 |
String message = nativeWaitForNextEvent(); |
| 190 |
if (message != null) { |
| 191 |
if (DEBUG) { |
| 192 |
Log.d(TAG, message); |
| 193 |
} |
| 194 |
sendEvent(message); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
private void sendEvent(String message) { |
| 200 |
synchronized (mKeysAndObservers) { |
| 201 |
final int N = mKeysAndObservers.size(); |
| 202 |
for (int i = 0; i < N; i += 2) { |
| 203 |
final String key = (String)mKeysAndObservers.get(i); |
| 204 |
if (message.contains(key)) { |
| 205 |
final UEventObserver observer = |
| 206 |
(UEventObserver)mKeysAndObservers.get(i + 1); |
| 207 |
mTempObserversToSignal.add(observer); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if (!mTempObserversToSignal.isEmpty()) { |
| 213 |
final UEvent event = new UEvent(message); |
| 214 |
final int N = mTempObserversToSignal.size(); |
| 215 |
for (int i = 0; i < N; i++) { |
| 216 |
final UEventObserver observer = mTempObserversToSignal.get(i); |
| 217 |
observer.onUEvent(event); |
| 218 |
} |
| 219 |
mTempObserversToSignal.clear(); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
public void addObserver(String match, UEventObserver observer) { |
| 224 |
synchronized (mKeysAndObservers) { |
| 225 |
mKeysAndObservers.add(match); |
| 226 |
mKeysAndObservers.add(observer); |
| 227 |
nativeAddMatch(match); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
public void removeObserver(UEventObserver observer) { |
| 233 |
synchronized (mKeysAndObservers) { |
| 234 |
for (int i = 0; i < mKeysAndObservers.size(); ) { |
| 235 |
if (mKeysAndObservers.get(i + 1) == observer) { |
| 236 |
mKeysAndObservers.remove(i + 1); |
| 237 |
final String match = (String)mKeysAndObservers.remove(i); |
| 238 |
nativeRemoveMatch(match); |
| 239 |
} else { |
| 240 |
i += 2; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |