Skip to content
This repository was archived by the owner on Apr 9, 2021. It is now read-only.

Feat : added flash light while scanning QRcode #240

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.useAndroidX=true
android.enableJetifier=true
android.enableJetifier=true
android.enableUnitTestBinaryResources=true
2 changes: 1 addition & 1 deletion skunkworks_crow/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<service android:name=".services.HotspotService" />

<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:name=".views.ui.receive.ScannerActivity"
android:screenOrientation="portrait"
android:stateNotNeeded="true"
tools:replace="android:screenOrientation" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.journeyapps.barcodescanner.CaptureActivity;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -49,6 +48,7 @@
import org.odk.share.views.listeners.OnItemClickListener;
import org.odk.share.views.ui.bluetooth.BtReceiverActivity;
import org.odk.share.views.ui.common.injectable.InjectableActivity;
import org.odk.share.views.ui.receive.ScannerActivity;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -356,7 +356,7 @@ public void scanQRCode() {
.setDesiredBarcodeFormats(QR_CODE_TYPES)
.setCameraId(0)
.setOrientationLocked(false)
.setCaptureActivity(CaptureActivity.class)
.setCaptureActivity(ScannerActivity.class)
.setBeepEnabled(true)
.initiateScan();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.odk.share.views.ui.receive;

/*
* created by Chromicle
*/

// I got resource from one of stackoverflow project https://stackoverflow.com/questions/32731869/turn-on-off-flashlight-in-zxing-fragment-lib/43886809l

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

import org.odk.share.R;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class ScannerActivity extends AppCompatActivity implements
DecoratedBarcodeView.TorchListener {

@BindView(R.id.switch_flashlight)
Button toggleFlashlightButton;
@BindView(R.id.zxing_barcode_scanner)
DecoratedBarcodeView decoratedScannerView;
private CaptureManager capture;
private boolean isFlashLightOn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

ButterKnife.bind(this);
decoratedScannerView.setTorchListener(this);


if (!isFlashlightSupported()) {
toggleFlashlightButton.setVisibility(View.GONE);
}

capture = new CaptureManager(this, decoratedScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
}

private boolean isFlashlightSupported() {
return getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}

public void toggleFlashlight() {
if (isFlashLightOn) {
decoratedScannerView.setTorchOff();
isFlashLightOn = false;
} else {
decoratedScannerView.setTorchOn();
isFlashLightOn = true;
}
}

@Override
public void onTorchOn() {
toggleFlashlightButton.setBackgroundResource(R.drawable.ic_flash_white_on);
}

@Override
public void onTorchOff() {
toggleFlashlightButton.setBackgroundResource(R.drawable.ic_flash_white_off);
}

@Override
protected void onResume() {
super.onResume();
capture.onResume();
}

@Override
protected void onPause() {
super.onPause();
capture.onPause();
}

@Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}

@OnClick(R.id.switch_flashlight)
public void toggleButton() {
toggleFlashlight();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return decoratedScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}

}
9 changes: 9 additions & 0 deletions skunkworks_crow/src/main/res/drawable/ic_flash_white_off.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="18dp"
android:height="24dp"
android:viewportWidth="45.0"
android:viewportHeight="20.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M3.27,3L2,4.27l5,5V13h3v9l3.58,-6.14L17.73,20 19,18.73 3.27,3zM17,10h-4l4,-8H7v2.18l8.46,8.46L17,10z" />
</vector>
9 changes: 9 additions & 0 deletions skunkworks_crow/src/main/res/drawable/ic_flash_white_on.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="18dp"
android:height="24dp"
android:viewportWidth="45.0"
android:viewportHeight="20.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M7,2v11h3v9l7,-12h-4l4,-8z" />
</vector>
26 changes: 26 additions & 0 deletions skunkworks_crow/src/main/res/layout/activity_scanner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_scanner">

<!-- set custom layout for scanner-->
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/zxing_barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<!-- FlashLight Button-->
<Button
android:id="@+id/switch_flashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="20dp"
android:background="@drawable/ic_flash_white_off" />


</RelativeLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.odk.share.activities;

import android.widget.Button;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.odk.share.R;
import org.odk.share.views.ui.receive.ScannerActivity;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowLog;

import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;

/**
* Created by Chromicle on 21/8/19.
*/
@RunWith(RobolectricTestRunner.class)
public class ScannerActivtyTest {

// for debugging unit tests.
static {
ShadowLog.stream = System.out;
}

private ScannerActivity scannerActivity;

@Before
public void setUp() throws Exception {
scannerActivity = Robolectric.setupActivity(ScannerActivity.class);
}


/**
* {@link Test} to assert {@link ScannerActivity} for not null.
*/
@Test
public void shouldNotBeNull() {
assertNotNull(scannerActivity);
}


/**
* {@link Test} to assert flashButton's functioning.
*/
@Test
public void flashLightButtonTest() throws Exception {
Button flashButton = scannerActivity.findViewById(R.id.switch_flashlight);
int flashOff = Shadows.shadowOf(flashButton.getBackground()).getCreatedFromResId();
assertThat("flash light button", R.drawable.ic_flash_white_off, is(equalTo(flashOff)));
assertTrue(flashButton.performClick());
int flashOn = Shadows.shadowOf(flashButton.getBackground()).getCreatedFromResId();
assertThat("flash button on", R.drawable.ic_flash_white_on, is(equalTo(flashOn)));
}


}