Skip to content

Commit

Permalink
Merge pull request #338 from reger24/preferences
Browse files Browse the repository at this point in the history
Harmonize use of preference CAMERA_SWITCH by bottom sheets
  • Loading branch information
thias15 authored Feb 23, 2023
2 parents 496a86f + e42f01b commit 062116a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public abstract class CameraFragment extends ControlsFragment {
private ExecutorService cameraExecutor;
private PreviewView previewView;
private Preview preview;
protected static int lensFacing = CameraSelector.LENS_FACING_BACK;
protected int lensFacing;
private ProcessCameraProvider cameraProvider;
private Size analyserResolution = Enums.Preview.HD.getValue();
private YuvToRgbConverter converter;
Expand All @@ -57,7 +57,11 @@ protected View inflateFragment(
private View addCamera(View view, LayoutInflater inflater, ViewGroup container) {
View cameraView = inflater.inflate(R.layout.fragment_camera, container, false);
ViewGroup rootView = (ViewGroup) cameraView.getRootView();

// set lensFacing from user preferences (last used setting)
lensFacing =
preferencesManager.getCameraSwitch()
? CameraSelector.LENS_FACING_FRONT
: CameraSelector.LENS_FACING_BACK;
previewView = cameraView.findViewById(R.id.viewFinder);
rootView.addView(view);

Expand Down Expand Up @@ -175,6 +179,7 @@ public void toggleCamera() {
CameraSelector.LENS_FACING_FRONT == lensFacing
? CameraSelector.LENS_FACING_BACK
: CameraSelector.LENS_FACING_FRONT;
preferencesManager.setCameraSwitch(!preferencesManager.getCameraSwitch());
bindCameraUseCases();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public abstract class ControlsFragment extends Fragment implements ServerListene
private Spinner modelSpinner;
private Spinner serverSpinner;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create before inflateFragment() to prevent npe when calling addCamera()
preferencesManager = new SharedPreferencesManager(requireContext());
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Expand All @@ -75,7 +82,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat

phoneController = PhoneController.getInstance(requireContext());

preferencesManager = new SharedPreferencesManager(requireContext());
audioPlayer = new AudioPlayer(requireContext());
masterList = FileUtils.loadConfigJSONFromAsset(requireActivity());
serverCommunication = new ServerCommunication(requireContext(), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public int getNumThreads() {
return preferences.getInt(NUM_THREAD, DEFAULT_NUM_THREAD);
}

/**
* Get selected camera lens facing
*
* @return true for LENS_FACING_FRONT, false for LENS_FACING_BACK
*/
public boolean getCameraSwitch() {
return preferences.getBoolean(CAMERA_SWITCH, false);
}
Expand Down
14 changes: 10 additions & 4 deletions android/app/src/main/java/org/openbot/original/CameraActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ protected void onCreate(final Bundle savedInstanceState) {
vehicle = OpenBotApplication.vehicle;

phoneController = PhoneController.getInstance(this);
preferencesManager = new SharedPreferencesManager(this);

setContentView(R.layout.activity_camera);
Toolbar toolbar = findViewById(R.id.toolbar);
Expand All @@ -212,8 +213,6 @@ protected void onCreate(final Bundle savedInstanceState) {
PermissionUtils.requestCameraPermission(this);
}

preferencesManager = new SharedPreferencesManager(this);

connectionSwitchCompat = findViewById(R.id.connection_switch);
threadsTextView = findViewById(R.id.threads);
plusImageView = findViewById(R.id.plus);
Expand Down Expand Up @@ -851,9 +850,16 @@ protected void readyForNextImage() {
protected int getCameraUserSelection() {
// during initialisation there is no cameraToggle so we assume default
if (this.cameraSwitchCompat == null) {
this.cameraSelection = CameraCharacteristics.LENS_FACING_BACK;
// set from user preferences (last used selection)
this.cameraSelection =
preferencesManager.getCameraSwitch()
? CameraCharacteristics.LENS_FACING_FRONT
: CameraCharacteristics.LENS_FACING_BACK;
} else {
this.cameraSelection = this.cameraSwitchCompat.isChecked() ? 0 : 1;
this.cameraSelection =
this.cameraSwitchCompat.isChecked()
? CameraCharacteristics.LENS_FACING_FRONT
: CameraCharacteristics.LENS_FACING_BACK;
}
return this.cameraSelection;
}
Expand Down

0 comments on commit 062116a

Please sign in to comment.