Skip to content

Commit

Permalink
refactor: Create viewSwitcher class to manage screen orientation chan…
Browse files Browse the repository at this point in the history
…ges, inside laser and camera activity
  • Loading branch information
VincentKobz committed May 24, 2024
1 parent 742f3ef commit 58ea72f
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

public class ManualLogDialog {
void launchDialog(Context context, String title, String logs, String textButton) {
static void launchDialog(Context context, String title, String logs, String textButton) {
Drawable icon = ContextCompat.getDrawable(context, R.drawable.info);

if (icon != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;

import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

Expand Down Expand Up @@ -358,12 +355,15 @@ private void setViewContent() {
}

setContentView(cameraLayoutId);
switchCameraOrientation(orientation == Configuration.ORIENTATION_PORTRAIT);

View view = findViewById(flashlightViewId).getRootView();
ViewSwitcher.switchCameraOrientation(this, view, cameraResources, orientation == Configuration.ORIENTATION_PORTRAIT);
} else {
setContentView(layoutIdLaser);

if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
switchLaserOrientation(false);
View view = findViewById(flashlightViewId).getRootView();
ViewSwitcher.switchLaserOrientation(this, view, flashlightViewId, false);
}
scannerStatusCard = findViewById(R.id.scanner_card_last_scan);
}
Expand Down Expand Up @@ -816,7 +816,7 @@ private void displayManualProviderLogButton() {

String providerLogTitle = getResources().getString(R.string.provider_log_dialog_title);
String textButton = getResources().getString(R.string.provider_log_dialog_close);
bt.setOnClickListener(view -> new ManualLogDialog().launchDialog(this, providerLogTitle, providerLogs, textButton));
bt.setOnClickListener(view -> ManualLogDialog.launchDialog(this, providerLogTitle, providerLogs, textButton));
}

/**
Expand Down Expand Up @@ -1008,123 +1008,12 @@ public void onConfigurationChanged(@NonNull Configuration newConfig) {

boolean switchCamera = enableScan && goToCamera && hasPermissionSet(this, PERMISSIONS_CAMERA) && hasCameraScannerSdk;
int orientation = getResources().getConfiguration().orientation;
View view = findViewById(flashlightViewId).getRootView();

if (switchCamera) {
switchCameraOrientation(orientation == Configuration.ORIENTATION_PORTRAIT);
} else {
switchLaserOrientation(orientation == Configuration.ORIENTATION_PORTRAIT);
}
}

private void switchCameraOrientation(boolean portrait) {
Integer constraintLayoutId = cameraResources.get("constraint_layout_id");
Integer scannerFlashlightId = cameraResources.get("scanner_flashlight_id");
Integer scannerBtKeyboardId = cameraResources.get("scanner_bt_provider_logs");

if (constraintLayoutId == null || scannerFlashlightId == null || scannerBtKeyboardId == null) {
Log.w(LOG_TAG, "Cannot switch to landscape mode: missing resources");
return;
}

ConstraintLayout constraintLayout = findViewById(constraintLayoutId);

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
int margin = getResources().getDimensionPixelSize(R.dimen.layout_margin_border);

// Set left constraints
if (portrait) {
constraintSet.connect(scannerFlashlightId, ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT, margin);
constraintSet.connect(scannerBtKeyboardId, ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT, margin);
} else {
constraintSet.connect(scannerFlashlightId, ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT, margin);
constraintSet.connect(scannerBtKeyboardId, ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT, margin);

}

if (portrait) {
// Delete left constraints
constraintSet.clear(scannerFlashlightId, ConstraintSet.LEFT);
constraintSet.clear(scannerBtKeyboardId, ConstraintSet.LEFT);

} else {
// Delete right constraints
constraintSet.clear(scannerFlashlightId, ConstraintSet.RIGHT);
constraintSet.clear(scannerBtKeyboardId, ConstraintSet.RIGHT);
}

// Apply constraints
constraintSet.applyTo(constraintLayout);
}

public void switchLaserOrientation(boolean portrait) {
ConstraintLayout mainConstraintLayout = findViewById(R.id.constraint_layout);
LinearLayout linearLayout = findViewById(R.id.bottom_layout);
View scannerFlashlight = findViewById(flashlightViewId);
View scannerBell = findViewById(R.id.scanner_bell);
View scannerRedLed = findViewById(R.id.scanner_red_led);

if (portrait) {
// Move elements to main constraint layout
linearLayout.removeView(scannerFlashlight);
linearLayout.removeView(scannerBell);
linearLayout.removeView(scannerRedLed);

LinearLayout.LayoutParams oldParamsFlashLight = (LinearLayout.LayoutParams) scannerFlashlight.getLayoutParams();
LinearLayout.LayoutParams oldParamsBell = (LinearLayout.LayoutParams) scannerBell.getLayoutParams();
LinearLayout.LayoutParams oldParamsRedLed = (LinearLayout.LayoutParams) scannerRedLed.getLayoutParams();

ConstraintLayout.LayoutParams newParamsFlashLight = new ConstraintLayout.LayoutParams(oldParamsFlashLight.width, oldParamsFlashLight.height);
newParamsFlashLight.setMargins(oldParamsFlashLight.leftMargin, oldParamsFlashLight.topMargin, oldParamsFlashLight.rightMargin, oldParamsFlashLight.bottomMargin);

ConstraintLayout.LayoutParams newParamsBell = new ConstraintLayout.LayoutParams(oldParamsBell.width, oldParamsBell.height);
newParamsBell.setMargins(oldParamsBell.leftMargin, oldParamsBell.topMargin, oldParamsBell.rightMargin, oldParamsBell.bottomMargin);

ConstraintLayout.LayoutParams newParamsRedLed = new ConstraintLayout.LayoutParams(oldParamsRedLed.width, oldParamsRedLed.height);
newParamsRedLed.setMargins(oldParamsRedLed.leftMargin, oldParamsRedLed.topMargin, oldParamsRedLed.rightMargin, oldParamsRedLed.bottomMargin);

mainConstraintLayout.addView(scannerFlashlight, newParamsFlashLight);
mainConstraintLayout.addView(scannerBell, newParamsBell);
mainConstraintLayout.addView(scannerRedLed, newParamsRedLed);

// Apply constraints
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(mainConstraintLayout);

int margin = getResources().getDimensionPixelSize(R.dimen.layout_margin_border);

constraintSet.connect(scannerFlashlight.getId(), ConstraintSet.TOP, R.id.scanner_enable_text, ConstraintSet.BOTTOM, margin);
constraintSet.connect(scannerFlashlight.getId(), ConstraintSet.END, mainConstraintLayout.getId(), ConstraintSet.END, margin);

constraintSet.connect(scannerBell.getId(), ConstraintSet.TOP, scannerFlashlight.getId(), ConstraintSet.BOTTOM, margin);
constraintSet.connect(scannerBell.getId(), ConstraintSet.END, mainConstraintLayout.getId(), ConstraintSet.END, margin);

constraintSet.connect(scannerRedLed.getId(), ConstraintSet.TOP, scannerBell.getId(), ConstraintSet.BOTTOM, margin);
constraintSet.connect(scannerRedLed.getId(), ConstraintSet.END, mainConstraintLayout.getId(), ConstraintSet.END, margin);

constraintSet.applyTo(mainConstraintLayout);
ViewSwitcher.switchCameraOrientation(this, view, cameraResources, orientation == Configuration.ORIENTATION_PORTRAIT);
} else {
// Move elements to linear layout
mainConstraintLayout.removeView(scannerFlashlight);
mainConstraintLayout.removeView(scannerBell);
mainConstraintLayout.removeView(scannerRedLed);

ConstraintLayout.LayoutParams oldParamsFlashLight = (ConstraintLayout.LayoutParams) scannerFlashlight.getLayoutParams();
ConstraintLayout.LayoutParams oldParamsBell = (ConstraintLayout.LayoutParams) scannerBell.getLayoutParams();
ConstraintLayout.LayoutParams oldParamsRedLed = (ConstraintLayout.LayoutParams) scannerRedLed.getLayoutParams();

LinearLayout.LayoutParams newParamsFlashLight = new LinearLayout.LayoutParams(oldParamsFlashLight.width, oldParamsFlashLight.height);
newParamsFlashLight.setMargins(0, oldParamsFlashLight.topMargin, oldParamsFlashLight.rightMargin, oldParamsFlashLight.bottomMargin);

LinearLayout.LayoutParams newParamsBell = new LinearLayout.LayoutParams(oldParamsBell.width, oldParamsBell.height);
newParamsBell.setMargins(0, oldParamsBell.topMargin, oldParamsBell.rightMargin, oldParamsBell.bottomMargin);

LinearLayout.LayoutParams newParamsRedLed = new LinearLayout.LayoutParams(oldParamsRedLed.width, oldParamsRedLed.height);
newParamsRedLed.setMargins(0, oldParamsRedLed.topMargin, oldParamsRedLed.rightMargin, oldParamsRedLed.bottomMargin);

linearLayout.addView(scannerFlashlight, newParamsFlashLight);
linearLayout.addView(scannerBell, newParamsBell);
linearLayout.addView(scannerRedLed, newParamsRedLed);
ViewSwitcher.switchLaserOrientation(this, view, flashlightViewId, orientation == Configuration.ORIENTATION_PORTRAIT);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.enioka.scanner.activities;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;

import com.enioka.scanner.R;

import java.util.HashMap;

/**
* An helper class to manage the dynamic switch of UI between portrait and landscape mode, for the
* camera and the laser activity.
*/
public class ViewSwitcher {
protected final static String LOG_TAG = "ScannerActivity";
public static void switchCameraOrientation(Context context, View view, HashMap<String, Integer> cameraResources, boolean portrait) {
Integer constraintLayoutId = cameraResources.get("constraint_layout_id");
Integer scannerFlashlightId = cameraResources.get("scanner_flashlight_id");
Integer scannerBtKeyboardId = cameraResources.get("scanner_bt_provider_logs");

if (constraintLayoutId == null || scannerFlashlightId == null || scannerBtKeyboardId == null) {
Log.w(LOG_TAG, "Cannot switch to landscape mode: missing resources");
return;
}

ConstraintLayout constraintLayout = view.findViewById(constraintLayoutId);

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
int margin = context.getResources().getDimensionPixelSize(R.dimen.layout_margin_border);

// Set left constraints
if (portrait) {
constraintSet.connect(scannerFlashlightId, ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT, margin);
constraintSet.connect(scannerBtKeyboardId, ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT, margin);
} else {
constraintSet.connect(scannerFlashlightId, ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT, margin);
constraintSet.connect(scannerBtKeyboardId, ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT, margin);

}

if (portrait) {
// Delete left constraints
constraintSet.clear(scannerFlashlightId, ConstraintSet.LEFT);
constraintSet.clear(scannerBtKeyboardId, ConstraintSet.LEFT);

} else {
// Delete right constraints
constraintSet.clear(scannerFlashlightId, ConstraintSet.RIGHT);
constraintSet.clear(scannerBtKeyboardId, ConstraintSet.RIGHT);
}

// Apply constraints
constraintSet.applyTo(constraintLayout);
}

public static void switchLaserOrientation(Context context, View view, int flashlightViewId, boolean portrait) {
ConstraintLayout mainConstraintLayout = view.findViewById(R.id.constraint_layout);
LinearLayout linearLayout = view.findViewById(R.id.bottom_layout);
View scannerFlashlight = view.findViewById(flashlightViewId);
View scannerBell = view.findViewById(R.id.scanner_bell);
View scannerRedLed = view.findViewById(R.id.scanner_red_led);

if (portrait) {
// Move elements to main constraint layout
linearLayout.removeView(scannerFlashlight);
linearLayout.removeView(scannerBell);
linearLayout.removeView(scannerRedLed);

LinearLayout.LayoutParams oldParamsFlashLight = (LinearLayout.LayoutParams) scannerFlashlight.getLayoutParams();
LinearLayout.LayoutParams oldParamsBell = (LinearLayout.LayoutParams) scannerBell.getLayoutParams();
LinearLayout.LayoutParams oldParamsRedLed = (LinearLayout.LayoutParams) scannerRedLed.getLayoutParams();

ConstraintLayout.LayoutParams newParamsFlashLight = new ConstraintLayout.LayoutParams(oldParamsFlashLight.width, oldParamsFlashLight.height);
newParamsFlashLight.setMargins(oldParamsFlashLight.leftMargin, oldParamsFlashLight.topMargin, oldParamsFlashLight.rightMargin, oldParamsFlashLight.bottomMargin);

ConstraintLayout.LayoutParams newParamsBell = new ConstraintLayout.LayoutParams(oldParamsBell.width, oldParamsBell.height);
newParamsBell.setMargins(oldParamsBell.leftMargin, oldParamsBell.topMargin, oldParamsBell.rightMargin, oldParamsBell.bottomMargin);

ConstraintLayout.LayoutParams newParamsRedLed = new ConstraintLayout.LayoutParams(oldParamsRedLed.width, oldParamsRedLed.height);
newParamsRedLed.setMargins(oldParamsRedLed.leftMargin, oldParamsRedLed.topMargin, oldParamsRedLed.rightMargin, oldParamsRedLed.bottomMargin);

mainConstraintLayout.addView(scannerFlashlight, newParamsFlashLight);
mainConstraintLayout.addView(scannerBell, newParamsBell);
mainConstraintLayout.addView(scannerRedLed, newParamsRedLed);

// Apply constraints
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(mainConstraintLayout);

int margin = context.getResources().getDimensionPixelSize(R.dimen.layout_margin_border);

constraintSet.connect(scannerFlashlight.getId(), ConstraintSet.TOP, R.id.scanner_enable_text, ConstraintSet.BOTTOM, margin);
constraintSet.connect(scannerFlashlight.getId(), ConstraintSet.END, mainConstraintLayout.getId(), ConstraintSet.END, margin);

constraintSet.connect(scannerBell.getId(), ConstraintSet.TOP, scannerFlashlight.getId(), ConstraintSet.BOTTOM, margin);
constraintSet.connect(scannerBell.getId(), ConstraintSet.END, mainConstraintLayout.getId(), ConstraintSet.END, margin);

constraintSet.connect(scannerRedLed.getId(), ConstraintSet.TOP, scannerBell.getId(), ConstraintSet.BOTTOM, margin);
constraintSet.connect(scannerRedLed.getId(), ConstraintSet.END, mainConstraintLayout.getId(), ConstraintSet.END, margin);

constraintSet.applyTo(mainConstraintLayout);
} else {
// Move elements to linear layout
mainConstraintLayout.removeView(scannerFlashlight);
mainConstraintLayout.removeView(scannerBell);
mainConstraintLayout.removeView(scannerRedLed);

ConstraintLayout.LayoutParams oldParamsFlashLight = (ConstraintLayout.LayoutParams) scannerFlashlight.getLayoutParams();
ConstraintLayout.LayoutParams oldParamsBell = (ConstraintLayout.LayoutParams) scannerBell.getLayoutParams();
ConstraintLayout.LayoutParams oldParamsRedLed = (ConstraintLayout.LayoutParams) scannerRedLed.getLayoutParams();

LinearLayout.LayoutParams newParamsFlashLight = new LinearLayout.LayoutParams(oldParamsFlashLight.width, oldParamsFlashLight.height);
newParamsFlashLight.setMargins(0, oldParamsFlashLight.topMargin, oldParamsFlashLight.rightMargin, oldParamsFlashLight.bottomMargin);

LinearLayout.LayoutParams newParamsBell = new LinearLayout.LayoutParams(oldParamsBell.width, oldParamsBell.height);
newParamsBell.setMargins(0, oldParamsBell.topMargin, oldParamsBell.rightMargin, oldParamsBell.bottomMargin);

LinearLayout.LayoutParams newParamsRedLed = new LinearLayout.LayoutParams(oldParamsRedLed.width, oldParamsRedLed.height);
newParamsRedLed.setMargins(0, oldParamsRedLed.topMargin, oldParamsRedLed.rightMargin, oldParamsRedLed.bottomMargin);

linearLayout.addView(scannerFlashlight, newParamsFlashLight);
linearLayout.addView(scannerBell, newParamsBell);
linearLayout.addView(scannerRedLed, newParamsRedLed);
}
}
}

0 comments on commit 58ea72f

Please sign in to comment.