cmhw: Don't hold a static reference to the service

* If getService fails for whatever reason, there is no chance
   to try again. Fix it.

Change-Id: Ibabd73dfbff0b99e0b75fda96eadbde5e8685c74
This commit is contained in:
Steve Kondik 2016-03-13 05:18:59 -07:00
parent 5e2d5a6850
commit 96cfc7392c
1 changed files with 44 additions and 41 deletions

View File

@ -40,7 +40,7 @@ import java.util.List;
public final class CMHardwareManager { public final class CMHardwareManager {
private static final String TAG = "CMHardwareManager"; private static final String TAG = "CMHardwareManager";
private static ICMHardwareService sService; private ICMHardwareService mService;
private Context mContext; private Context mContext;
/** /**
@ -153,7 +153,7 @@ public final class CMHardwareManager {
} else { } else {
mContext = context; mContext = context;
} }
sService = getService(); checkService();
} }
/** /**
@ -169,14 +169,14 @@ public final class CMHardwareManager {
} }
/** @hide */ /** @hide */
public static ICMHardwareService getService() { private ICMHardwareService getService() {
if (sService != null) { if (mService != null) {
return sService; return mService;
} }
IBinder b = ServiceManager.getService(CMContextConstants.CM_HARDWARE_SERVICE); IBinder b = ServiceManager.getService(CMContextConstants.CM_HARDWARE_SERVICE);
if (b != null) { if (b != null) {
sService = ICMHardwareService.Stub.asInterface(b); mService = ICMHardwareService.Stub.asInterface(b);
return sService; return mService;
} }
return null; return null;
} }
@ -187,7 +187,7 @@ public final class CMHardwareManager {
public int getSupportedFeatures() { public int getSupportedFeatures() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getSupportedFeatures(); return mService.getSupportedFeatures();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -221,7 +221,7 @@ public final class CMHardwareManager {
try { try {
if (checkService()) { if (checkService()) {
return sService.get(feature); return mService.get(feature);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -245,7 +245,7 @@ public final class CMHardwareManager {
try { try {
if (checkService()) { if (checkService()) {
return sService.set(feature, enable); return mService.set(feature, enable);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -284,7 +284,7 @@ public final class CMHardwareManager {
private int[] getVibratorIntensityArray() { private int[] getVibratorIntensityArray() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getVibratorIntensity(); return mService.getVibratorIntensity();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -337,7 +337,7 @@ public final class CMHardwareManager {
public boolean setVibratorIntensity(int intensity) { public boolean setVibratorIntensity(int intensity) {
try { try {
if (checkService()) { if (checkService()) {
return sService.setVibratorIntensity(intensity); return mService.setVibratorIntensity(intensity);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -372,7 +372,7 @@ public final class CMHardwareManager {
private int[] getDisplayColorCalibrationArray() { private int[] getDisplayColorCalibrationArray() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getDisplayColorCalibration(); return mService.getDisplayColorCalibration();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -423,7 +423,7 @@ public final class CMHardwareManager {
public boolean setDisplayColorCalibration(int[] rgb) { public boolean setDisplayColorCalibration(int[] rgb) {
try { try {
if (checkService()) { if (checkService()) {
return sService.setDisplayColorCalibration(rgb); return mService.setDisplayColorCalibration(rgb);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -440,7 +440,7 @@ public final class CMHardwareManager {
public boolean writePersistentString(String key, String value) { public boolean writePersistentString(String key, String value) {
try { try {
if (checkService()) { if (checkService()) {
return getService().writePersistentBytes(key, return mService.writePersistentBytes(key,
value == null ? null : value.getBytes("UTF-8")); value == null ? null : value.getBytes("UTF-8"));
} }
} catch (RemoteException e) { } catch (RemoteException e) {
@ -460,7 +460,7 @@ public final class CMHardwareManager {
public boolean writePersistentInt(String key, int value) { public boolean writePersistentInt(String key, int value) {
try { try {
if (checkService()) { if (checkService()) {
return getService().writePersistentBytes(key, return mService.writePersistentBytes(key,
ByteBuffer.allocate(4).putInt(value).array()); ByteBuffer.allocate(4).putInt(value).array());
} }
} catch (RemoteException e) { } catch (RemoteException e) {
@ -478,7 +478,7 @@ public final class CMHardwareManager {
public boolean writePersistentBytes(String key, byte[] value) { public boolean writePersistentBytes(String key, byte[] value) {
try { try {
if (checkService()) { if (checkService()) {
return getService().writePersistentBytes(key, value); return mService.writePersistentBytes(key, value);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -494,7 +494,7 @@ public final class CMHardwareManager {
public String readPersistentString(String key) { public String readPersistentString(String key) {
try { try {
if (checkService()) { if (checkService()) {
byte[] bytes = getService().readPersistentBytes(key); byte[] bytes = mService.readPersistentBytes(key);
if (bytes != null) { if (bytes != null) {
return new String(bytes, "UTF-8"); return new String(bytes, "UTF-8");
} }
@ -515,7 +515,7 @@ public final class CMHardwareManager {
public int readPersistentInt(String key) { public int readPersistentInt(String key) {
try { try {
if (checkService()) { if (checkService()) {
byte[] bytes = getService().readPersistentBytes(key); byte[] bytes = mService.readPersistentBytes(key);
if (bytes != null) { if (bytes != null) {
return ByteBuffer.wrap(bytes).getInt(); return ByteBuffer.wrap(bytes).getInt();
} }
@ -534,7 +534,7 @@ public final class CMHardwareManager {
public byte[] readPersistentBytes(String key) { public byte[] readPersistentBytes(String key) {
try { try {
if (checkService()) { if (checkService()) {
return getService().readPersistentBytes(key); return mService.readPersistentBytes(key);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -549,7 +549,7 @@ public final class CMHardwareManager {
public boolean deletePersistentObject(String key) { public boolean deletePersistentObject(String key) {
try { try {
if (checkService()) { if (checkService()) {
return getService().writePersistentBytes(key, null); return mService.writePersistentBytes(key, null);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -580,7 +580,7 @@ public final class CMHardwareManager {
private int[] getDisplayGammaCalibrationArray(int idx) { private int[] getDisplayGammaCalibrationArray(int idx) {
try { try {
if (checkService()) { if (checkService()) {
return sService.getDisplayGammaCalibration(idx); return mService.getDisplayGammaCalibration(idx);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -594,7 +594,7 @@ public final class CMHardwareManager {
public int getNumGammaControls() { public int getNumGammaControls() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getNumGammaControls(); return mService.getNumGammaControls();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -645,7 +645,7 @@ public final class CMHardwareManager {
public boolean setDisplayGammaCalibration(int idx, int[] rgb) { public boolean setDisplayGammaCalibration(int idx, int[] rgb) {
try { try {
if (checkService()) { if (checkService()) {
return sService.setDisplayGammaCalibration(idx, rgb); return mService.setDisplayGammaCalibration(idx, rgb);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -658,7 +658,7 @@ public final class CMHardwareManager {
public String getLtoSource() { public String getLtoSource() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getLtoSource(); return mService.getLtoSource();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -671,7 +671,7 @@ public final class CMHardwareManager {
public String getLtoDestination() { public String getLtoDestination() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getLtoDestination(); return mService.getLtoDestination();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -684,7 +684,7 @@ public final class CMHardwareManager {
public long getLtoDownloadInterval() { public long getLtoDownloadInterval() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getLtoDownloadInterval(); return mService.getLtoDownloadInterval();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -697,7 +697,7 @@ public final class CMHardwareManager {
public String getSerialNumber() { public String getSerialNumber() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getSerialNumber(); return mService.getSerialNumber();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -710,7 +710,7 @@ public final class CMHardwareManager {
public String getUniqueDeviceId() { public String getUniqueDeviceId() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getUniqueDeviceId(); return mService.getUniqueDeviceId();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -724,7 +724,7 @@ public final class CMHardwareManager {
public boolean requireAdaptiveBacklightForSunlightEnhancement() { public boolean requireAdaptiveBacklightForSunlightEnhancement() {
try { try {
if (checkService()) { if (checkService()) {
return sService.requireAdaptiveBacklightForSunlightEnhancement(); return mService.requireAdaptiveBacklightForSunlightEnhancement();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -737,7 +737,7 @@ public final class CMHardwareManager {
public boolean isSunlightEnhancementSelfManaged() { public boolean isSunlightEnhancementSelfManaged() {
try { try {
if (checkService()) { if (checkService()) {
return sService.isSunlightEnhancementSelfManaged(); return mService.isSunlightEnhancementSelfManaged();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -750,7 +750,7 @@ public final class CMHardwareManager {
public DisplayMode[] getDisplayModes() { public DisplayMode[] getDisplayModes() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getDisplayModes(); return mService.getDisplayModes();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -763,7 +763,7 @@ public final class CMHardwareManager {
public DisplayMode getCurrentDisplayMode() { public DisplayMode getCurrentDisplayMode() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getCurrentDisplayMode(); return mService.getCurrentDisplayMode();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -776,7 +776,7 @@ public final class CMHardwareManager {
public DisplayMode getDefaultDisplayMode() { public DisplayMode getDefaultDisplayMode() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getDefaultDisplayMode(); return mService.getDefaultDisplayMode();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -789,7 +789,7 @@ public final class CMHardwareManager {
public boolean setDisplayMode(DisplayMode mode, boolean makeDefault) { public boolean setDisplayMode(DisplayMode mode, boolean makeDefault) {
try { try {
if (checkService()) { if (checkService()) {
return sService.setDisplayMode(mode, makeDefault); return mService.setDisplayMode(mode, makeDefault);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -800,9 +800,12 @@ public final class CMHardwareManager {
* @return true if service is valid * @return true if service is valid
*/ */
private boolean checkService() { private boolean checkService() {
if (sService == null) { if (mService == null) {
Log.w(TAG, "not connected to CMHardwareManagerService"); mService = getService();
return false; if (mService == null) {
Log.w(TAG, "not connected to CMHardwareManagerService");
return false;
}
} }
return true; return true;
} }
@ -813,7 +816,7 @@ public final class CMHardwareManager {
public int getThermalState() { public int getThermalState() {
try { try {
if (checkService()) { if (checkService()) {
return sService.getThermalState(); return mService.getThermalState();
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -827,7 +830,7 @@ public final class CMHardwareManager {
public boolean registerThermalListener(ThermalListenerCallback thermalCallback) { public boolean registerThermalListener(ThermalListenerCallback thermalCallback) {
try { try {
if (checkService()) { if (checkService()) {
return sService.registerThermalListener(thermalCallback); return mService.registerThermalListener(thermalCallback);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }
@ -841,7 +844,7 @@ public final class CMHardwareManager {
public boolean unRegisterThermalListener(ThermalListenerCallback thermalCallback) { public boolean unRegisterThermalListener(ThermalListenerCallback thermalCallback) {
try { try {
if (checkService()) { if (checkService()) {
return sService.unRegisterThermalListener(thermalCallback); return mService.unRegisterThermalListener(thermalCallback);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
} }