Skip to content

Commit

Permalink
BugFix: Fix potential NPEs with getService()
Browse files Browse the repository at this point in the history
Should fix
"java.lang.NullPointerException: Attempt to invoke interface method
'int cyanogenmod.hardware.ICMHardwareService.getSupportedFeatures()'
on a null object reference" and others

Change-Id: Ic5a02fc953376aa746844fd6c2f93b5f48246516
  • Loading branch information
BadDaemon authored and Gerrit Code Review committed Sep 1, 2015
1 parent 6850732 commit 40a46bd
Showing 1 changed file with 68 additions and 19 deletions.
87 changes: 68 additions & 19 deletions src/java/cyanogenmod/hardware/CMHardwareManager.java
Expand Up @@ -166,7 +166,9 @@ private static ICMHardwareService getService() {
*/
public int getSupportedFeatures() {
try {
return getService().getSupportedFeatures();
if (checkService()) {
return sService.getSupportedFeatures();
}
} catch (RemoteException e) {
}
return 0;
Expand Down Expand Up @@ -198,7 +200,9 @@ public boolean get(int feature) {
}

try {
return getService().get(feature);
if (checkService()) {
return sService.get(feature);
}
} catch (RemoteException e) {
}
return false;
Expand All @@ -220,7 +224,9 @@ public boolean set(int feature, boolean enable) {
}

try {
return getService().set(feature, enable);
if (checkService()) {
return sService.set(feature, enable);
}
} catch (RemoteException e) {
}
return false;
Expand Down Expand Up @@ -257,7 +263,9 @@ private int getArrayValue(int[] arr, int idx, int defaultValue) {

private int[] getVibratorIntensityArray() {
try {
return getService().getVibratorIntensity();
if (checkService()) {
return sService.getVibratorIntensity();
}
} catch (RemoteException e) {
}
return null;
Expand Down Expand Up @@ -308,7 +316,9 @@ public int getVibratorWarningIntensity() {
*/
public boolean setVibratorIntensity(int intensity) {
try {
return getService().setVibratorIntensity(intensity);
if (checkService()) {
return sService.setVibratorIntensity(intensity);
}
} catch (RemoteException e) {
}
return false;
Expand Down Expand Up @@ -341,7 +351,9 @@ public boolean setVibratorIntensity(int intensity) {

private int[] getDisplayColorCalibrationArray() {
try {
return getService().getDisplayColorCalibration();
if (checkService()) {
return sService.getDisplayColorCalibration();
}
} catch (RemoteException e) {
}
return null;
Expand Down Expand Up @@ -390,7 +402,9 @@ public int getDisplayColorCalibrationMax() {
*/
public boolean setDisplayColorCalibration(int[] rgb) {
try {
return getService().setDisplayColorCalibration(rgb);
if (checkService()) {
return sService.setDisplayColorCalibration(rgb);
}
} catch (RemoteException e) {
}
return false;
Expand Down Expand Up @@ -419,7 +433,9 @@ public boolean setDisplayColorCalibration(int[] rgb) {

private int[] getDisplayGammaCalibrationArray(int idx) {
try {
return getService().getDisplayGammaCalibration(idx);
if (checkService()) {
return sService.getDisplayGammaCalibration(idx);
}
} catch (RemoteException e) {
}
return null;
Expand All @@ -431,7 +447,9 @@ private int[] getDisplayGammaCalibrationArray(int idx) {
@Deprecated
public int getNumGammaControls() {
try {
return getService().getNumGammaControls();
if (checkService()) {
return sService.getNumGammaControls();
}
} catch (RemoteException e) {
}
return 0;
Expand Down Expand Up @@ -480,7 +498,9 @@ public int getDisplayGammaCalibrationMax() {
@Deprecated
public boolean setDisplayGammaCalibration(int idx, int[] rgb) {
try {
return getService().setDisplayGammaCalibration(idx, rgb);
if (checkService()) {
return sService.setDisplayGammaCalibration(idx, rgb);
}
} catch (RemoteException e) {
}
return false;
Expand All @@ -491,7 +511,9 @@ public boolean setDisplayGammaCalibration(int idx, int[] rgb) {
*/
public String getLtoSource() {
try {
return getService().getLtoSource();
if (checkService()) {
return sService.getLtoSource();
}
} catch (RemoteException e) {
}
return null;
Expand All @@ -502,7 +524,9 @@ public String getLtoSource() {
*/
public String getLtoDestination() {
try {
return getService().getLtoDestination();
if (checkService()) {
return sService.getLtoDestination();
}
} catch (RemoteException e) {
}
return null;
Expand All @@ -513,7 +537,9 @@ public String getLtoDestination() {
*/
public long getLtoDownloadInterval() {
try {
return getService().getLtoDownloadInterval();
if (checkService()) {
return sService.getLtoDownloadInterval();
}
} catch (RemoteException e) {
}
return 0;
Expand All @@ -524,7 +550,9 @@ public long getLtoDownloadInterval() {
*/
public String getSerialNumber() {
try {
return getService().getSerialNumber();
if (checkService()) {
return sService.getSerialNumber();
}
} catch (RemoteException e) {
}
return null;
Expand All @@ -536,7 +564,9 @@ public String getSerialNumber() {
*/
public boolean requireAdaptiveBacklightForSunlightEnhancement() {
try {
return getService().requireAdaptiveBacklightForSunlightEnhancement();
if (checkService()) {
return sService.requireAdaptiveBacklightForSunlightEnhancement();
}
} catch (RemoteException e) {
}
return false;
Expand All @@ -547,7 +577,9 @@ public boolean requireAdaptiveBacklightForSunlightEnhancement() {
*/
public DisplayMode[] getDisplayModes() {
try {
return getService().getDisplayModes();
if (checkService()) {
return sService.getDisplayModes();
}
} catch (RemoteException e) {
}
return null;
Expand All @@ -558,7 +590,9 @@ public DisplayMode[] getDisplayModes() {
*/
public DisplayMode getCurrentDisplayMode() {
try {
return getService().getCurrentDisplayMode();
if (checkService()) {
return sService.getCurrentDisplayMode();
}
} catch (RemoteException e) {
}
return null;
Expand All @@ -569,7 +603,9 @@ public DisplayMode getCurrentDisplayMode() {
*/
public DisplayMode getDefaultDisplayMode() {
try {
return getService().getDefaultDisplayMode();
if (checkService()) {
return sService.getDefaultDisplayMode();
}
} catch (RemoteException e) {
}
return null;
Expand All @@ -580,9 +616,22 @@ public DisplayMode getDefaultDisplayMode() {
*/
public boolean setDisplayMode(DisplayMode mode, boolean makeDefault) {
try {
return getService().setDisplayMode(mode, makeDefault);
if (checkService()) {
return sService.setDisplayMode(mode, makeDefault);
}
} catch (RemoteException e) {
}
return false;
}

/**
* @return true if service is valid
*/
private boolean checkService() {
if (sService == null) {
Log.w(TAG, "not connected to CMHardwareManagerService");
return false;
}
return true;
}
}

0 comments on commit 40a46bd

Please sign in to comment.