| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.lights; |
| 18 |
|
| 19 |
import android.Manifest; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.RequiresPermission; |
| 22 |
import android.content.Context; |
| 23 |
import android.hardware.lights.LightsManager.LightsSession; |
| 24 |
import android.os.RemoteException; |
| 25 |
import android.os.ServiceManager; |
| 26 |
import android.os.ServiceManager.ServiceNotFoundException; |
| 27 |
import android.util.CloseGuard; |
| 28 |
|
| 29 |
import com.android.internal.annotations.VisibleForTesting; |
| 30 |
import com.android.internal.util.Preconditions; |
| 31 |
|
| 32 |
import java.lang.ref.Reference; |
| 33 |
import java.util.List; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public final class SystemLightsManager extends LightsManager { |
| 41 |
private static final String TAG = "LightsManager"; |
| 42 |
|
| 43 |
@NonNull private final ILightsManager mService; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public SystemLightsManager(@NonNull Context context) throws ServiceNotFoundException { |
| 51 |
this(context, ILightsManager.Stub.asInterface( |
| 52 |
ServiceManager.getServiceOrThrow(Context.LIGHTS_SERVICE))); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
@VisibleForTesting |
| 61 |
public SystemLightsManager(@NonNull Context context, @NonNull ILightsManager service) { |
| 62 |
super(context); |
| 63 |
mService = Preconditions.checkNotNull(service); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
@RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS) |
| 72 |
@Override |
| 73 |
public @NonNull List<Light> getLights() { |
| 74 |
try { |
| 75 |
return mService.getLights(); |
| 76 |
} catch (RemoteException e) { |
| 77 |
throw e.rethrowFromSystemServer(); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
@RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS) |
| 87 |
@Override |
| 88 |
public @NonNull LightState getLightState(@NonNull Light light) { |
| 89 |
Preconditions.checkNotNull(light); |
| 90 |
try { |
| 91 |
return mService.getLightState(light.getId()); |
| 92 |
} catch (RemoteException e) { |
| 93 |
throw e.rethrowFromSystemServer(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
@RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS) |
| 101 |
@Override |
| 102 |
public @NonNull LightsSession openSession() { |
| 103 |
try { |
| 104 |
final LightsSession session = new SystemLightsSession(); |
| 105 |
mService.openSession(session.getToken(), 0); |
| 106 |
return session; |
| 107 |
} catch (RemoteException e) { |
| 108 |
throw e.rethrowFromSystemServer(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
@RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS) |
| 122 |
@Override |
| 123 |
public @NonNull LightsSession openSession(int priority) { |
| 124 |
try { |
| 125 |
final LightsSession session = new SystemLightsSession(); |
| 126 |
mService.openSession(session.getToken(), priority); |
| 127 |
return session; |
| 128 |
} catch (RemoteException e) { |
| 129 |
throw e.rethrowFromSystemServer(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public final class SystemLightsSession extends LightsManager.LightsSession |
| 138 |
implements AutoCloseable { |
| 139 |
|
| 140 |
private final CloseGuard mCloseGuard = new CloseGuard(); |
| 141 |
private boolean mClosed = false; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
@RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS) |
| 147 |
private SystemLightsSession() { |
| 148 |
mCloseGuard.open("SystemLightsSession.close"); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
@RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS) |
| 161 |
@Override |
| 162 |
public void requestLights(@NonNull LightsRequest request) { |
| 163 |
Preconditions.checkNotNull(request); |
| 164 |
if (!mClosed) { |
| 165 |
try { |
| 166 |
List<Integer> idList = request.getLights(); |
| 167 |
List<LightState> stateList = request.getLightStates(); |
| 168 |
int[] ids = new int[idList.size()]; |
| 169 |
for (int i = 0; i < idList.size(); i++) { |
| 170 |
ids[i] = idList.get(i); |
| 171 |
} |
| 172 |
LightState[] states = new LightState[stateList.size()]; |
| 173 |
for (int i = 0; i < stateList.size(); i++) { |
| 174 |
states[i] = stateList.get(i); |
| 175 |
} |
| 176 |
mService.setLightStates(getToken(), ids, states); |
| 177 |
} catch (RemoteException e) { |
| 178 |
throw e.rethrowFromSystemServer(); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
@RequiresPermission(Manifest.permission.CONTROL_DEVICE_LIGHTS) |
| 187 |
@Override |
| 188 |
public void close() { |
| 189 |
if (!mClosed) { |
| 190 |
try { |
| 191 |
mService.closeSession(getToken()); |
| 192 |
mClosed = true; |
| 193 |
mCloseGuard.close(); |
| 194 |
} catch (RemoteException e) { |
| 195 |
throw e.rethrowFromSystemServer(); |
| 196 |
} |
| 197 |
} |
| 198 |
Reference.reachabilityFence(this); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
@Override |
| 203 |
protected void finalize() throws Throwable { |
| 204 |
try { |
| 205 |
mCloseGuard.warnIfOpen(); |
| 206 |
close(); |
| 207 |
} finally { |
| 208 |
super.finalize(); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |