Skip to content

Commit

Permalink
Merge pull request #4 from BlinkID/fix/resourcesAndroid
Browse files Browse the repository at this point in the history
Fix/resources android
  • Loading branch information
i1E authored Sep 28, 2017
2 parents 04b2a8f + 3db2077 commit 00e7ef1
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 118 deletions.
17 changes: 8 additions & 9 deletions BlinkMoto/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,21 @@

<source-file src="src/android/java/com/phonegap/plugins/blinkid/BlinkIdScanner.java" target-dir="src/com/phonegap/plugins/blinkid" />
<source-file src="src/android/java/com/phonegap/plugins/blinkid/ScanActivity.java" target-dir="src/com/phonegap/plugins/blinkid" />
<!--source-file src="src/android/java/com/phonegap/plugins/blinkid/FakeR.java" target-dir="src/com/phonegap/plugins/blinkid" /-->
<source-file src="src/android/java/com/phonegap/plugins/blinkid/FakeR.java" target-dir="src/com/phonegap/plugins/blinkid" />

<resource-file src="src/android/res/drawable/button_accent_text_color.xml" target="res/drawable/button_accent_text_color.xml" />
<resource-file src="src/android/res/drawable/button_accent.xml" target="res/drawable/button_accent.xml" />
<resource-file src="src/android/res/drawable/button_normal.xml" target="res/drawable/button_normal.xml" />
<resource-file src="src/android/res/drawable/scan_frame.xml" target="res/drawable/scan_frame.xml" />
<resource-file src="src/android/res/drawable/blinkid_button_accent_text_color.xml" target="res/drawable/blinkid_button_accent_text_color.xml" />
<resource-file src="src/android/res/drawable/blinkid_button_accent.xml" target="res/drawable/blinkid_button_accent.xml" />
<resource-file src="src/android/res/drawable/blinkid_button_normal.xml" target="res/drawable/blinkid_button_normal.xml" />
<resource-file src="src/android/res/drawable/blinkid_scan_frame.xml" target="res/drawable/blinkid_scan_frame.xml" />

<resource-file src="src/android/res/layout/custom_scan_layout.xml" target="res/layout/custom_scan_layout.xml" />
<resource-file src="src/android/res/layout/custom_scan_overlay.xml" target="res/layout/custom_scan_overlay.xml" />

<resource-file src="src/android/res/values/colors.xml" target="res/values/colors.xml" />
<resource-file src="src/android/res/values/dimens.xml" target="res/values/dimens.xml" />
<resource-file src="src/android/res/values/strings.xml" target="res/values/strings.xml" />
<resource-file src="src/android/res/values/colors.xml" target="res/values/blinkid-colors.xml" />
<resource-file src="src/android/res/values/dimens.xml" target="res/values/blinkid-dimens.xml" />
<resource-file src="src/android/res/values/strings.xml" target="res/values/blinkid-strings.xml" />

<framework src="src/android/blinkid-android/libBlinkID.gradle" custom="true" type="gradleReference" />
<resource-file src="src/android/blinkid-android/LibBlinkID.aar" target="aar/LibBlinkID.aar" />

<hook type="after_prepare" src="scripts/fix-largeheap.js" />
</platform>
Expand Down
Binary file removed BlinkMoto/src/android/blinkid-android/LibBlinkID.aar
Binary file not shown.
15 changes: 8 additions & 7 deletions BlinkMoto/src/android/blinkid-android/libBlinkID.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
repositories{
jcenter()
flatDir{
dirs 'aar'
}
repositories {
jcenter()
mavenCentral()
maven { url 'http://maven.microblink.com' }
}

dependencies {
compile(name:'LibBlinkID', ext:'aar')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:appcompat-v7:25.0.0'
compile('com.microblink:blinkid:3.11.0@aar') {
transitive = false
}
}

android {
Expand Down
54 changes: 54 additions & 0 deletions BlinkMoto/src/android/java/com/phonegap/plugins/blinkid/FakeR.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.phonegap.plugins.blinkid;

import android.app.Activity;
import android.content.Context;

/**
* Replacement for android resources <b>R</b> in PhoneGap plugins.<br/>
* Used for resources added with a plugin. Since the application has its own <b>R</b> the plugin must fetch its resources through the activity's context.
*/
public class FakeR {
private Context context;
private String packageName;

public FakeR(Activity activity) {
context = activity.getApplicationContext();
packageName = context.getPackageName();
}

public FakeR(Context context) {
this.context = context;
packageName = context.getPackageName();
}

/**
* Get resource identifier from given group.
*
* @param group id, string, layout, dimen, bool, etc.
* @param key Resource key
* @return Resource identifier
*/
public int getIdFrom(String group, String key) {
return context.getResources().getIdentifier(key, group, packageName);
}

/**
* Get resource identifier from group <b>id</b>.
*
* @param key Resource key
* @return Resource value
*/
public int getId(String key) {
return getIdFrom("id", key);
}

/**
* Get resource identifier from group <b>string</b>.
*
* @param key Resource key
* @return Resource value
*/
public String getString(String key) {
return context.getResources().getString(getIdFrom("string", key));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import android.widget.ImageView;
import android.widget.TextView;

import com.microblink.blinkmoto.R;
import com.microblink.detectors.DetectorResult;
import com.microblink.detectors.points.PointsDetectorResult;
import com.microblink.geometry.Rectangle;
Expand Down Expand Up @@ -82,6 +81,8 @@ public enum RecognizerType {

private final String OCR_PARSER_NAME = "parser";

private FakeR mFakeR;

private CameraPermissionManager mCameraPermManager;
private RecognizerView mRecognizerView;
private PointSetView mPointSetView;
Expand Down Expand Up @@ -109,9 +110,10 @@ private enum ActivityState {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
mFakeR = new FakeR(this);
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.custom_scan_layout);
setContentView(mFakeR.getIdFrom("layout", "custom_scan_layout"));
} catch (InflateException ie) {
Throwable cause = ie.getCause();
while (cause.getCause() != null) {
Expand All @@ -131,7 +133,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
// Set internationalized strings.
Bundle extras = getIntent().getExtras();

mRecognizerView = (RecognizerView) findViewById(R.id.recognizerView);
mRecognizerView = (RecognizerView) findViewById(mFakeR.getId("recognizerView"));

// Set license key.
String licenseKey = extras.getString(EXTRAS_LICENSE_KEY);
Expand All @@ -143,7 +145,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

// Add the camera permissions overlay.
mCameraPermManager = new CameraPermissionManager(this);
mRecognizerViewRoot = (FrameLayout) findViewById(R.id.recognozerViewRoot);
mRecognizerViewRoot = (FrameLayout) findViewById(mFakeR.getId("recognizerViewRoot"));
View cameraPermissionView = mCameraPermManager.getAskPermissionOverlay();
if (cameraPermissionView != null) {
mRecognizerViewRoot.addView(cameraPermissionView);
Expand Down Expand Up @@ -211,20 +213,20 @@ public void onOrientationChange(Orientation orientation) {
mRecognizerView.addChildView(mOcrResultView.getView(), false);

// Inflate the overlay view.
final ViewGroup overlay = (ViewGroup) getLayoutInflater().inflate(R.layout.custom_scan_overlay, null);
final ViewGroup overlay = (ViewGroup) getLayoutInflater().inflate(mFakeR.getIdFrom("layout", "custom_scan_overlay"), null);
// Bind view elements.
mScanViewfinder = (FrameLayout) overlay.findViewById(R.id.fl_scan_frame);
mScanTitleView = (TextView) overlay.findViewById(R.id.tv_scan_title);
mScanResultStringView = (TextView) overlay.findViewById(R.id.tv_scan_result);
mScanResultImageView = (ImageView) overlay.findViewById(R.id.iv_scan_result);
mAcceptButton = (Button) overlay.findViewById(R.id.btn_accept);
mCancelButton = (Button) overlay.findViewById(R.id.btn_cancel);
mRepeatButton = (Button) overlay.findViewById(R.id.btn_repeat);
mScanViewfinder = (FrameLayout) overlay.findViewById(mFakeR.getId("fl_scan_frame"));
mScanTitleView = (TextView) overlay.findViewById(mFakeR.getId("tv_scan_title"));
mScanResultStringView = (TextView) overlay.findViewById(mFakeR.getId("tv_scan_result"));
mScanResultImageView = (ImageView) overlay.findViewById(mFakeR.getId("iv_scan_result"));
mAcceptButton = (Button) overlay.findViewById(mFakeR.getId("btn_accept"));
mCancelButton = (Button) overlay.findViewById(mFakeR.getId("btn_cancel"));
mRepeatButton = (Button) overlay.findViewById(mFakeR.getId("btn_repeat"));
// Set user defined titles.
mScanTitleView.setText(extras.getString(EXTRAS_TITLE_STRING, getString(R.string.scanning_title)));
mAcceptButton.setText(extras.getString(EXTRAS_ACCEPT_STRING, getString(R.string.accept)));
mCancelButton.setText(extras.getString(EXTRAS_CANCEL_STRING, getString(R.string.cancel)));
mRepeatButton.setText(extras.getString(EXTRAS_REPEAT_STRING, getString(R.string.repeat)));
mScanTitleView.setText(extras.getString(EXTRAS_TITLE_STRING, mFakeR.getString("blinkid_scanning_title")));
mAcceptButton.setText(extras.getString(EXTRAS_ACCEPT_STRING, mFakeR.getString("blinkid_accept")));
mCancelButton.setText(extras.getString(EXTRAS_CANCEL_STRING, mFakeR.getString("blinkid_cancel")));
mRepeatButton.setText(extras.getString(EXTRAS_REPEAT_STRING, mFakeR.getString("blinkid_repeat")));
// Result image invisible at start.
mScanResultImageView.setVisibility(View.GONE);
// Cannot accept or retry when scanning is in progress.
Expand Down Expand Up @@ -267,7 +269,7 @@ public void run() {
}

// Subtract the relative horizontal padding
int horizontalPadding = (int) getResources().getDimension(R.dimen.scan_region_padding);
int horizontalPadding = (int) getResources().getDimension(mFakeR.getIdFrom("dimen", "blinkid_scan_region_padding"));
width -= horizontalPadding * 2;

// Calculate the height based on given aspect ratio
Expand Down Expand Up @@ -426,30 +428,28 @@ public void onActivityFlip() {
}

public void onButtonClicked(View view) {
switch (view.getId()) {
case R.id.btn_accept:
Intent intent = new Intent();
intent.putExtra(EXTRAS_RESULT_STRING, mScanResultStringView.getText().toString());
setResult(RESULT_OK, intent);
finish();
break;

case R.id.btn_cancel:
setResult(RESULT_CANCELED);
finish();
break;

case R.id.btn_repeat:
mScanResultImageView.setBackground(null);
mScanResultImageView.setVisibility(View.GONE);
mScanResultStringView.setVisibility(View.INVISIBLE);
mAcceptButton.setEnabled(false);
mRepeatButton.setEnabled(false);
mRecognizerView.resumeScanning(true);
break;
int id = view.getId();
if (id == mFakeR.getId("btn_accept")) {
Intent intent = new Intent();
intent.putExtra(EXTRAS_RESULT_STRING, mScanResultStringView.getText().toString());
setResult(RESULT_OK, intent);
finish();

} else if (id == mFakeR.getId("btn_cancel")) {
setResult(RESULT_CANCELED);
finish();

} else if (id == mFakeR.getId("btn_repeat")) {
mScanResultImageView.setBackground(null);
mScanResultImageView.setVisibility(View.GONE);
mScanResultStringView.setVisibility(View.INVISIBLE);
mAcceptButton.setEnabled(false);
mRepeatButton.setEnabled(false);
mRecognizerView.resumeScanning(true);
}
}


@Override
public void onScanningDone(@Nullable final RecognitionResults recognitionResults) {
if (mRecognizerView != null) {
Expand Down Expand Up @@ -496,7 +496,7 @@ public void run() {
mScanResultImageView.setImageBitmap(bitmap);
}

String resultString = getString(R.string.unknown_result);
String resultString = mFakeR.getString("blinkid_unknown_result");

if (result instanceof VinScanResult) {
resultString = ((VinScanResult) result).getVin();
Expand All @@ -509,7 +509,7 @@ public void run() {
resultString = parsedAmount;
}
} else {
resultString = getString(R.string.invalid_result_message);
resultString = mFakeR.getString("blinkid_invalid_result_message");
}
}

Expand All @@ -536,9 +536,9 @@ public void onError(Throwable throwable) {
if (mActivityState == ActivityState.RESUMED || mActivityState == ActivityState.STARTED) {
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setCancelable(false)
.setTitle(R.string.error_dialog_title)
.setTitle(mFakeR.getString("blinkid_error_dialog_title"))
.setMessage(throwable.getClass().getSimpleName() + ": " + throwable.getMessage())
.setNeutralButton(R.string.error_dialog_ok, new DialogInterface.OnClickListener() {
.setNeutralButton(mFakeR.getString("blinkid_error_dialog_ok"), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null) dialog.dismiss();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<item android:state_enabled="false">
<shape>
<corners android:radius="0dp" />
<solid android:color="@color/btn_background" />
<solid android:color="@color/blinkid_btn_background" />
</shape>
</item>

<item>
<shape>
<corners android:radius="0dp" />
<solid android:color="@color/btn_background_accent" />
<solid android:color="@color/blinkid_btn_background_accent" />
</shape>
</item>
</selector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@color/blinkid_btn_accent_text_color_disabled" />
<item android:color="@color/blinkid_btn_accent_text_color" />
</selector>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="0dp" />
<solid android:color="@color/btn_background" />
<solid android:color="@color/blinkid_btn_background" />
</shape>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@color/scan_frame_border_color" />
android:color="@color/blinkid_scan_frame_border_color" />
<solid android:color="#00000000" />
</shape>

This file was deleted.

2 changes: 1 addition & 1 deletion BlinkMoto/src/android/res/layout/custom_scan_layout.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/recognozerViewRoot"
android:id="@+id/recognizerViewRoot"
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand Down
Loading

0 comments on commit 00e7ef1

Please sign in to comment.