Skip to content

Commit

Permalink
- updated gradle files with latest updated deps
Browse files Browse the repository at this point in the history
- removed jack
- added runtime permission handling in sample app
- updated gradle wrapper to 4.1
  • Loading branch information
nisrulz committed Aug 22, 2017
1 parent cdb0fd1 commit 7e07f7e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 42 deletions.
3 changes: 0 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ android {
applicationId "github.nisrulz.projectqreader"
versionCode 2
versionName "1.0.1"
jackOptions {
enabled false
}
}
buildTypes {
release {
Expand Down
74 changes: 60 additions & 14 deletions app/src/main/java/github/nisrulz/projectqreader/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package github.nisrulz.projectqreader;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceView;
Expand All @@ -28,30 +30,35 @@
import github.nisrulz.qreader.QREader;

public class MainActivity extends AppCompatActivity {

private static final String cameraPerm = Manifest.permission.CAMERA;

// UI
private TextView text;

// QREader
private SurfaceView mySurfaceView;
private QREader qrEader;

boolean hasCameraPermission = false;

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hasCameraPermission = RuntimePermissionUtil.checkPermissonGranted(this, cameraPerm);

text = (TextView) findViewById(R.id.code_info);
text = findViewById(R.id.code_info);

final Button stateBtn = (Button) findViewById(R.id.btn_start_stop);
final Button stateBtn = findViewById(R.id.btn_start_stop);
// change of reader state in dynamic
stateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (qrEader.isCameraRunning()) {
stateBtn.setText("Start QREader");
qrEader.stop();
}
else {
} else {
stateBtn.setText("Stop QREader");
qrEader.start();
}
Expand All @@ -60,19 +67,32 @@ public void onClick(View v) {

stateBtn.setVisibility(View.VISIBLE);

Button restartbtn = (Button) findViewById(R.id.btn_restart_activity);
Button restartbtn = findViewById(R.id.btn_restart_activity);
restartbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
finish();
restartActivity();
}
});

// Setup SurfaceView
// -----------------
mySurfaceView = (SurfaceView) findViewById(R.id.camera_view);
mySurfaceView = findViewById(R.id.camera_view);

if (hasCameraPermission) {
// Setup QREader
setupQREader();
} else {
RuntimePermissionUtil.requestPermission(MainActivity.this, cameraPerm, 100);
}
}

void restartActivity() {
startActivity(new Intent(MainActivity.this, MainActivity.class));
finish();
}

void setupQREader() {
// Init QREader
// ------------
qrEader = new QREader.Builder(this, mySurfaceView, new QRDataListener() {
Expand All @@ -97,17 +117,43 @@ public void run() {
protected void onPause() {
super.onPause();

// Cleanup in onPause()
// --------------------
qrEader.releaseAndCleanup();
if (hasCameraPermission) {

// Cleanup in onPause()
// --------------------
qrEader.releaseAndCleanup();
}
}

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

// Init and Start with SurfaceView
// -------------------------------
qrEader.initAndStart(mySurfaceView);
if (hasCameraPermission) {

// Init and Start with SurfaceView
// -------------------------------
qrEader.initAndStart(mySurfaceView);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull final String[] permissions,
@NonNull final int[] grantResults) {
if (requestCode == 100) {
RuntimePermissionUtil.onRequestPermissionsResult(grantResults, new RPResultListener() {
@Override
public void onPermissionGranted() {
if ( RuntimePermissionUtil.checkPermissonGranted(MainActivity.this, cameraPerm)) {
restartActivity();
}
}

@Override
public void onPermissionDenied() {
// do nothing
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2016 Nishant Srivastava
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package github.nisrulz.projectqreader;

interface RPResultListener {
void onPermissionGranted();

void onPermissionDenied();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2016 Nishant Srivastava
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package github.nisrulz.projectqreader;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;

class RuntimePermissionUtil {

private RuntimePermissionUtil() {

}

public static void onRequestPermissionsResult(int[] grantResults,
RPResultListener rpResultListener) {
if (grantResults.length > 0) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_GRANTED) {
rpResultListener.onPermissionGranted();
} else {
rpResultListener.onPermissionDenied();
}
}
}
}

public static void requestPermission(final Activity activity, final String[] permissions,
final int REQUEST_CODE) {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity, permissions, REQUEST_CODE);
}

public static void requestPermission(final Activity activity, final String permission,
final int REQUEST_CODE) {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity, new String[] { permission }, REQUEST_CODE);
}

public static boolean checkPermissonGranted(Context context, String permission) {
return (ActivityCompat.checkSelfPermission(context, permission)
== PackageManager.PERMISSION_GRANTED);
}
}
14 changes: 6 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:3.0.0-beta2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -35,10 +36,7 @@ buildscript {
allprojects {
repositories {
jcenter()
maven {
url 'https://maven.google.com'
// Alternative URL is 'https://dl.google.com/dl/android/maven2/'
}
google()
}
}

Expand All @@ -52,9 +50,9 @@ ext {
targetSdkVersion = 26
minSdkVersion = 14
compileSdkVersion = targetSdkVersion
buildToolsVersion = '26.0.0'
supportLibVersion = '26.0.0'
playServicesVersion = '11.0.2'
buildToolsVersion = '26.0.1'
supportLibVersion = '26.0.1'
playServicesVersion = '11.2.0'

// Library Info
libVersionCode = 10
Expand Down
19 changes: 2 additions & 17 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
#
# Copyright (C) 2016 Nishant Srivastava
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#Thu Sep 15 18:01:55 IST 2016
#Mon Aug 21 23:15:11 PDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

0 comments on commit 7e07f7e

Please sign in to comment.