Skip to content

Commit

Permalink
Fix NPE when releasing camera
Browse files Browse the repository at this point in the history
When tapping rapidly on the telegraph key, the camera is
released, either directly or delayed via a posted Runnable.
When tapping rapidly, the delayed runnable would run after
a synchronous release of the camera, in a state where the
camera would be null.

Extract open and release of the camera to two methods and
protect the release with a null check.

Also update the Gradle wrapper to 8.0 to be compatible
with the latest stable version of Android Studio (Giraffe).
  • Loading branch information
fejd committed Oct 1, 2023
1 parent 09e40ca commit a37c30e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
31 changes: 18 additions & 13 deletions app/src/main/java/rocks/poopjournal/morse/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ protected void onCreate(Bundle savedInstanceState) {


if (!Build.MANUFACTURER.equals("HUAWEI")) {
camera = Camera.open();
openCamera();
}


Expand All @@ -528,10 +528,7 @@ protected void onCreate(Bundle savedInstanceState) {
turnOff();
}
}

camera.release();
camera = null;

releaseCamera();
}
} else {
if (!TextUtils.isEmpty(input.getText().toString())) {
Expand Down Expand Up @@ -561,8 +558,7 @@ protected void onCreate(Bundle savedInstanceState) {
turnOff();
}
}
camera.release();
camera = null;
releaseCamera();
}
}
});
Expand Down Expand Up @@ -997,7 +993,7 @@ public void onAutoFocus(boolean b, Camera camera) {
public void turnOn() {
try {
if (android.os.Build.MANUFACTURER.equals("HUAWEI")) {
camera = Camera.open();
openCamera();
}

Log.d("cameraMorseCheck","turning camera on at " + System.currentTimeMillis());
Expand Down Expand Up @@ -1234,7 +1230,7 @@ private boolean setKeySelectedForTelegraph(MotionEvent event) {
telegraphPlayer.start();
} else {
time = System.currentTimeMillis();
camera = Camera.open();
openCamera();
turnOn();
}
return true;
Expand All @@ -1255,19 +1251,28 @@ private boolean setKeySelectedForTelegraph(MotionEvent event) {

if (System.currentTimeMillis() - time >= 200) {
turnOff();
camera.release();
camera = null;
releaseCamera();
} else {
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(() -> {
turnOff();
camera.release();
camera = null;
releaseCamera();
}, 100);
}
}
return true;
}
return false;
}

private void openCamera() {
camera = Camera.open();
}

private void releaseCamera() {
if (camera != null) {
camera.release();
camera = null;
}
}
}
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.1.1'
classpath 'com.android.tools.build:gradle:8.1.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -16,7 +16,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Jun 09 15:32:18 PKT 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit a37c30e

Please sign in to comment.