| 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 |
|
| 21 |
import com.google.android.collect.Maps; |
| 22 |
|
| 23 |
import java.util.HashMap; |
| 24 |
import java.util.concurrent.TimeoutException; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class SystemService { |
| 32 |
|
| 33 |
private static HashMap<String, State> sStates = Maps.newHashMap(); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public enum State { |
| 39 |
RUNNING("running"), |
| 40 |
STOPPING("stopping"), |
| 41 |
STOPPED("stopped"), |
| 42 |
RESTARTING("restarting"); |
| 43 |
|
| 44 |
State(String state) { |
| 45 |
sStates.put(state, this); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
private static Object sPropertyLock = new Object(); |
| 50 |
|
| 51 |
static { |
| 52 |
SystemProperties.addChangeCallback(new Runnable() { |
| 53 |
@Override |
| 54 |
public void run() { |
| 55 |
synchronized (sPropertyLock) { |
| 56 |
sPropertyLock.notifyAll(); |
| 57 |
} |
| 58 |
} |
| 59 |
}); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
@UnsupportedAppUsage |
| 64 |
public static void start(String name) { |
| 65 |
SystemProperties.set("ctl.start", name); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 70 |
public static void stop(String name) { |
| 71 |
SystemProperties.set("ctl.stop", name); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
public static void restart(String name) { |
| 76 |
SystemProperties.set("ctl.restart", name); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static State getState(String service) { |
| 83 |
final String rawState = SystemProperties.get("init.svc." + service); |
| 84 |
final State state = sStates.get(rawState); |
| 85 |
if (state != null) { |
| 86 |
return state; |
| 87 |
} else { |
| 88 |
return State.STOPPED; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static boolean isStopped(String service) { |
| 96 |
return State.STOPPED.equals(getState(service)); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public static boolean isRunning(String service) { |
| 103 |
return State.RUNNING.equals(getState(service)); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public static void waitForState(String service, State state, long timeoutMillis) |
| 110 |
throws TimeoutException { |
| 111 |
final long endMillis = SystemClock.elapsedRealtime() + timeoutMillis; |
| 112 |
while (true) { |
| 113 |
synchronized (sPropertyLock) { |
| 114 |
final State currentState = getState(service); |
| 115 |
if (state.equals(currentState)) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
if (SystemClock.elapsedRealtime() >= endMillis) { |
| 120 |
throw new TimeoutException("Service " + service + " currently " + currentState |
| 121 |
+ "; waited " + timeoutMillis + "ms for " + state); |
| 122 |
} |
| 123 |
|
| 124 |
try { |
| 125 |
sPropertyLock.wait(timeoutMillis); |
| 126 |
} catch (InterruptedException e) { |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public static void waitForAnyStopped(String... services) { |
| 136 |
while (true) { |
| 137 |
synchronized (sPropertyLock) { |
| 138 |
for (String service : services) { |
| 139 |
if (State.STOPPED.equals(getState(service))) { |
| 140 |
return; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
try { |
| 145 |
sPropertyLock.wait(); |
| 146 |
} catch (InterruptedException e) { |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |