Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5992f11f authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

Merge branch '7350-master-camera_failure' into 'master'

camera: Use openLegacy() for older HAL1 devices

See merge request !70
parents 9f3a3f57 8961edf2
Loading
Loading
Loading
Loading
Loading
+0 −53
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022  E FOUNDATION
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package net.sourceforge.opencamera;

import android.annotation.SuppressLint;
import android.util.Log;

import androidx.annotation.Nullable;

import java.lang.reflect.Method;

public class PropertyUtility {

    private static final String TAG = PropertyUtility.class.getName();

    /**
     * Retrieve the property value from systemProperties
     * @param key the property key
     * @return value of the property, null if the value is not present
     */
    @Nullable
    public static String getProperty(String key) {
        String value = null;
        try {
            @SuppressLint("PrivateApi") Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);

            value = (String) get.invoke(c, key);

        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }

        return value;
    }

    private PropertyUtility() {
    }
}
+20 −6
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import android.view.TextureView;
import net.sourceforge.opencamera.MyDebug;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@@ -58,15 +59,28 @@ public class CameraController1 extends CameraController {
        if( MyDebug.LOG )
            Log.d(TAG, "create new CameraController1: " + cameraId);
        this.camera_error_cb = camera_error_cb;

        try {
            camera = Camera.open(cameraId);
            Method openMethod = Class.forName("android.hardware.Camera")
                    .getMethod("openLegacy", int.class, int.class);
            camera = (Camera) openMethod.invoke(null,
                    cameraId, 0x100);
        } catch (Exception e) {
            /* Retry with open if openLegacy doesn't exist/fails */
            camera = null;
        }
        catch(RuntimeException e) {

        if (camera == null) {
            try {
                camera = Camera.open(cameraId);
            } catch (RuntimeException e) {
                if (MyDebug.LOG)
                    Log.e(TAG, "failed to open camera");
                e.printStackTrace();
                throw new CameraControllerException();
            }
        }

        if( camera == null ) {
            // Although the documentation says Camera.open() should throw a RuntimeException, it seems that it some cases it can return null
            // I've seen this in some crashes reported in Google Play; also see:
+1 −6
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@ import android.util.Log;
import android.util.Pair;
import android.view.Display;
import android.view.GestureDetector;
//import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.OrientationEventListener;
@@ -63,7 +62,6 @@ import androidx.core.content.ContextCompat;

import net.sourceforge.opencamera.MainActivity;
import net.sourceforge.opencamera.MyDebug;
import net.sourceforge.opencamera.PropertyUtility;
import net.sourceforge.opencamera.ScriptC_histogram_compute;
import net.sourceforge.opencamera.TakePhoto;
import net.sourceforge.opencamera.ToastBoxer;
@@ -1700,10 +1698,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu

        //final boolean use_background_thread = false;
        //final boolean use_background_thread = true;
        boolean use_background_thread = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
        if ("true".equalsIgnoreCase(PropertyUtility.getProperty("camera.load.sync"))) {
            use_background_thread = true;
        }
        final boolean use_background_thread = true;
		/* Opening camera on background thread is important so that we don't block the UI thread:
		 *   - For old Camera API, this is recommended behaviour by Google for Camera.open().
		     - For Camera2, the manager.openCamera() call is asynchronous, but CameraController2