Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
[android] Modified zxing client to access resources from main package
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Willoughby committed Oct 16, 2012
1 parent e3505ae commit 9cff772
Show file tree
Hide file tree
Showing 39 changed files with 504 additions and 447 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
bin/
.classpath
.project
12 changes: 0 additions & 12 deletions src/android/LibraryProject/default.properties

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** Automatically generated file. DO NOT MODIFY */
package com.google.zxing.client.android;

public final class BuildConfig {
public final static boolean DEBUG = true;
}
412 changes: 206 additions & 206 deletions src/android/LibraryProject/gen/com/google/zxing/client/android/R.java

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions src/android/LibraryProject/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

android.library=true
# Project target.
target=android-16
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void onCompletion(MediaPlayer player) {
}
});

AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
AssetFileDescriptor file = activity.getResources().openRawResourceFd(activity.getApplicationContext().getResources().getIdentifier("beep", "raw", activity.getApplicationContext().getPackageName()));
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
file.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -128,11 +129,13 @@ private enum Source {
private HistoryManager historyManager;
private InactivityTimer inactivityTimer;
private BeepManager beepManager;
private Context context;
private String pkgName;

private final DialogInterface.OnClickListener aboutListener =
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.zxing_url)));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(getIdentifier("string", "zxing_url"))));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(intent);
}
Expand All @@ -149,15 +152,18 @@ public Handler getHandler() {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);


context = getApplicationContext();
pkgName = context.getPackageName();

Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.capture);
setContentView(getIdentifier("layout", "capture"));

CameraManager.init(getApplication());
viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
resultView = findViewById(R.id.result_view);
statusView = (TextView) findViewById(R.id.status_view);
viewfinderView = (ViewfinderView) findViewById(getIdentifier("id", "viewfinder_view"));
resultView = findViewById(getIdentifier("id", "result_view"));
statusView = (TextView) findViewById(getIdentifier("id", "status_view"));
handler = null;
lastResult = null;
hasSurface = false;
Expand All @@ -168,13 +174,17 @@ public void onCreate(Bundle icicle) {

// showHelpOnFirstLaunch();
}

private int getIdentifier(String type, String name) {
return context.getResources().getIdentifier(name, type, pkgName);
}

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

SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
SurfaceView surfaceView = (SurfaceView) findViewById(getIdentifier("id", "preview_view"));
SurfaceHolder surfaceHolder = surfaceView.getHolder();
if (hasSurface) {
// The activity was paused but not stopped, so the surface still exists. Therefore
Expand Down Expand Up @@ -263,7 +273,7 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
} else if ((source == Source.NONE || source == Source.ZXING_LINK) && lastResult != null) {
resetStatusView();
if (handler != null) {
handler.sendEmptyMessage(R.id.restart_preview);
handler.sendEmptyMessage(getIdentifier("id", "restart_preview"));
}
return true;
}
Expand All @@ -277,16 +287,16 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, SHARE_ID, 0, R.string.menu_share)
.setIcon(android.R.drawable.ic_menu_share);
menu.add(0, HISTORY_ID, 0, R.string.menu_history)
.setIcon(android.R.drawable.ic_menu_recent_history);
menu.add(0, SETTINGS_ID, 0, R.string.menu_settings)
.setIcon(android.R.drawable.ic_menu_preferences);
menu.add(0, HELP_ID, 0, R.string.menu_help)
.setIcon(android.R.drawable.ic_menu_help);
menu.add(0, ABOUT_ID, 0, R.string.menu_about)
.setIcon(android.R.drawable.ic_menu_info_details);
menu.add(0, SHARE_ID, 0, getIdentifier("string", "menu_share"))
.setIcon(getIdentifier("drawable", "ic_menu_share"));
menu.add(0, HISTORY_ID, 0, getIdentifier("string", "menu_history"))
.setIcon(getIdentifier("drawable", "ic_menu_recent_history"));
menu.add(0, SETTINGS_ID, 0, getIdentifier("string", "menu_settings"))
.setIcon(getIdentifier("drawable", "ic_menu_preferences"));
menu.add(0, HELP_ID, 0, getIdentifier("string", "menu_help"))
.setIcon(getIdentifier("drawable", "ic_menu_help"));
menu.add(0, ABOUT_ID, 0, getIdentifier("string", "menu_about"))
.setIcon(getIdentifier("drawable", "ic_menu_info_details"));
return true;
}

Expand Down Expand Up @@ -329,11 +339,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
case ABOUT_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.title_about) + versionName);
builder.setMessage(getString(R.string.msg_about) + "\n\n" + getString(R.string.zxing_url));
builder.setIcon(R.drawable.launcher_icon);
builder.setPositiveButton(R.string.button_open_browser, aboutListener);
builder.setNegativeButton(R.string.button_cancel, null);
builder.setTitle(getString(getIdentifier("string", "title_about")) + versionName);
builder.setMessage(getString(getIdentifier("string", "msg_about")) + "\n\n" + getString(getIdentifier("string", "zxing_url")));
builder.setIcon(getIdentifier("drawable", "launcher_icon"));
builder.setPositiveButton(getIdentifier("string", "button_open_browser"), aboutListener);
builder.setNegativeButton(getIdentifier("string", "button_cancel"), null);
builder.show();
break;
}
Expand Down Expand Up @@ -388,10 +398,10 @@ public void handleDecode(Result rawResult, Bitmap barcode) {
case NONE:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getBoolean(PreferencesActivity.KEY_BULK_MODE, false)) {
Toast.makeText(this, R.string.msg_bulk_mode_scanned, Toast.LENGTH_SHORT).show();
Toast.makeText(this, getIdentifier("string", "msg_bulk_mode_scanned"), Toast.LENGTH_SHORT).show();
// Wait a moment or else it will scan the same barcode continuously about 3 times
if (handler != null) {
handler.sendEmptyMessageDelayed(R.id.restart_preview, BULK_MODE_SCAN_DELAY_MS);
handler.sendEmptyMessageDelayed(getIdentifier("id", "restart_preview"), BULK_MODE_SCAN_DELAY_MS);
}
resetStatusView();
} else {
Expand All @@ -413,13 +423,13 @@ private void drawResultPoints(Bitmap barcode, Result rawResult) {
if (points != null && points.length > 0) {
Canvas canvas = new Canvas(barcode);
Paint paint = new Paint();
paint.setColor(getResources().getColor(R.color.result_image_border));
paint.setColor(getResources().getColor(getIdentifier("color", "result_image_border")));
paint.setStrokeWidth(3.0f);
paint.setStyle(Paint.Style.STROKE);
Rect border = new Rect(2, 2, barcode.getWidth() - 2, barcode.getHeight() - 2);
canvas.drawRect(border, paint);

paint.setColor(getResources().getColor(R.color.result_points));
paint.setColor(getResources().getColor(getIdentifier("color", "result_points")));
if (points.length == 2) {
paint.setStrokeWidth(4.0f);
drawLine(canvas, paint, points[0], points[1]);
Expand Down Expand Up @@ -448,28 +458,28 @@ private void handleDecodeInternally(Result rawResult, ResultHandler resultHandle
viewfinderView.setVisibility(View.GONE);
resultView.setVisibility(View.VISIBLE);

ImageView barcodeImageView = (ImageView) findViewById(R.id.barcode_image_view);
ImageView barcodeImageView = (ImageView) findViewById(getIdentifier("id", "barcode_image_view"));
if (barcode == null) {
barcodeImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.launcher_icon));
getIdentifier("drawable", "launcher_icon")));
} else {
barcodeImageView.setImageBitmap(barcode);
}

TextView formatTextView = (TextView) findViewById(R.id.format_text_view);
TextView formatTextView = (TextView) findViewById(getIdentifier("id", "format_text_view"));
formatTextView.setText(rawResult.getBarcodeFormat().toString());

TextView typeTextView = (TextView) findViewById(R.id.type_text_view);
TextView typeTextView = (TextView) findViewById(getIdentifier("id", "type_text_view"));
typeTextView.setText(resultHandler.getType().toString());

DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
String formattedTime = formatter.format(new Date(rawResult.getTimestamp()));
TextView timeTextView = (TextView) findViewById(R.id.time_text_view);
TextView timeTextView = (TextView) findViewById(getIdentifier("id", "time_text_view"));
timeTextView.setText(formattedTime);


TextView metaTextView = (TextView) findViewById(R.id.meta_text_view);
View metaTextViewLabel = findViewById(R.id.meta_text_view_label);
TextView metaTextView = (TextView) findViewById(getIdentifier("id", "meta_text_view"));
View metaTextViewLabel = findViewById(getIdentifier("id", "meta_text_view_label"));
metaTextView.setVisibility(View.GONE);
metaTextViewLabel.setVisibility(View.GONE);
Map<ResultMetadataType,Object> metadata =
Expand All @@ -489,14 +499,14 @@ private void handleDecodeInternally(Result rawResult, ResultHandler resultHandle
}
}

TextView contentsTextView = (TextView) findViewById(R.id.contents_text_view);
TextView contentsTextView = (TextView) findViewById(getIdentifier("id", "contents_text_view"));
CharSequence displayContents = resultHandler.getDisplayContents();
contentsTextView.setText(displayContents);
// Crudely scale betweeen 22 and 32 -- bigger font for shorter text
int scaledSize = Math.max(22, 32 - displayContents.length() / 4);
contentsTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, scaledSize);

TextView supplementTextView = (TextView) findViewById(R.id.contents_supplement_text_view);
TextView supplementTextView = (TextView) findViewById(getIdentifier("id", "contents_supplement_text_view"));
supplementTextView.setText("");
supplementTextView.setOnClickListener(null);
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
Expand All @@ -506,7 +516,7 @@ private void handleDecodeInternally(Result rawResult, ResultHandler resultHandle
}

int buttonCount = resultHandler.getButtonCount();
ViewGroup buttonView = (ViewGroup) findViewById(R.id.result_button_view);
ViewGroup buttonView = (ViewGroup) findViewById(getIdentifier("id", "result_button_view"));
buttonView.requestFocus();
for (int x = 0; x < ResultHandler.MAX_BUTTON_COUNT; x++) {
TextView button = (TextView) buttonView.getChildAt(x);
Expand Down Expand Up @@ -550,21 +560,21 @@ private void handleDecodeExternally(Result rawResult, ResultHandler resultHandle
if (rawBytes != null && rawBytes.length > 0) {
intent.putExtra(Intents.Scan.RESULT_BYTES, rawBytes);
}
Message message = Message.obtain(handler, R.id.return_scan_result);
Message message = Message.obtain(handler, getIdentifier("id", "return_scan_result"));
message.obj = intent;
handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
} else if (source == Source.PRODUCT_SEARCH_LINK) {
// Reformulate the URL which triggered us into a query, so that the request goes to the same
// TLD as the scan URL.
Message message = Message.obtain(handler, R.id.launch_product_query);
Message message = Message.obtain(handler, getIdentifier("id", "launch_product_query"));
int end = sourceUrl.lastIndexOf("/scan");
message.obj = sourceUrl.substring(0, end) + "?q=" +
resultHandler.getDisplayContents().toString() + "&source=zxing";
handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
} else if (source == Source.ZXING_LINK) {
// Replace each occurrence of RETURN_CODE_PLACEHOLDER in the returnUrlTemplate
// with the scanned code. This allows both queries and REST-style URLs to work.
Message message = Message.obtain(handler, R.id.launch_product_query);
Message message = Message.obtain(handler, getIdentifier("id", "launch_product_query"));
message.obj = returnUrlTemplate.replace(RETURN_CODE_PLACEHOLDER,
resultHandler.getDisplayContents().toString());
handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
Expand Down Expand Up @@ -621,16 +631,16 @@ private void initCamera(SurfaceHolder surfaceHolder) {

private void displayFrameworkBugMessageAndExit() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
builder.setMessage(getString(R.string.msg_camera_framework_bug));
builder.setPositiveButton(R.string.button_ok, new FinishListener(this));
builder.setTitle(getString(getIdentifier("string", "app_name")));
builder.setMessage(getString(getIdentifier("string", "msg_camera_framework_bug")));
builder.setPositiveButton(getIdentifier("string", "button_ok"), new FinishListener(this));
builder.setOnCancelListener(new FinishListener(this));
builder.show();
}

private void resetStatusView() {
resultView.setVisibility(View.GONE);
statusView.setText(R.string.msg_default_status);
statusView.setText(getIdentifier("string", "msg_default_status"));
statusView.setVisibility(View.VISIBLE);
viewfinderView.setVisibility(View.VISIBLE);
lastResult = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,32 @@ private enum State {

@Override
public void handleMessage(Message message) {
if (message.what == R.id.auto_focus) {
if (message.what == activity.getApplicationContext().getResources().getIdentifier("auto_focus", "id", activity.getApplicationContext().getPackageName())) {
//Log.d(TAG, "Got auto-focus message");
// When one auto focus pass finishes, start another. This is the closest thing to
// continuous AF. It does seem to hunt a bit, but I'm not sure what else to do.
if (state == State.PREVIEW) {
CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
CameraManager.get().requestAutoFocus(this, activity.getApplicationContext().getResources().getIdentifier("auto_focus", "id", activity.getApplicationContext().getPackageName()));
}
} else if (message.what == R.id.restart_preview) {
} else if (message.what == activity.getApplicationContext().getResources().getIdentifier("restart_preview", "id", activity.getApplicationContext().getPackageName())) {
Log.d(TAG, "Got restart preview message");
restartPreviewAndDecode();
} else if (message.what == R.id.decode_succeeded) {
} else if (message.what == activity.getApplicationContext().getResources().getIdentifier("decode_succeeded", "id", activity.getApplicationContext().getPackageName())) {
Log.d(TAG, "Got decode succeeded message");
state = State.SUCCESS;
Bundle bundle = message.getData();
Bitmap barcode = bundle == null ? null :
(Bitmap) bundle.getParcelable(DecodeThread.BARCODE_BITMAP);
activity.handleDecode((Result) message.obj, barcode);
} else if (message.what == R.id.decode_failed) {
} else if (message.what == activity.getApplicationContext().getResources().getIdentifier("decode_failed", "id", activity.getApplicationContext().getPackageName())) {
// We're decoding as fast as possible, so when one decode fails, start another.
state = State.PREVIEW;
CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
} else if (message.what == R.id.return_scan_result) {
CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), activity.getApplicationContext().getResources().getIdentifier("decode", "id", activity.getApplicationContext().getPackageName()));
} else if (message.what == activity.getApplicationContext().getResources().getIdentifier("return_scan_result", "id", activity.getApplicationContext().getPackageName())) {
Log.d(TAG, "Got return scan result message");
activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
activity.finish();
} else if (message.what == R.id.launch_product_query) {
} else if (message.what == activity.getApplicationContext().getResources().getIdentifier("launch_product_query", "id", activity.getApplicationContext().getPackageName())) {
Log.d(TAG, "Got product query message");
String url = (String) message.obj;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Expand All @@ -102,7 +102,7 @@ public void handleMessage(Message message) {
public void quitSynchronously() {
state = State.DONE;
CameraManager.get().stopPreview();
Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
Message quit = Message.obtain(decodeThread.getHandler(), activity.getApplicationContext().getResources().getIdentifier("quit", "id", activity.getApplicationContext().getPackageName()));
quit.sendToTarget();
try {
decodeThread.join();
Expand All @@ -111,15 +111,15 @@ public void quitSynchronously() {
}

// Be absolutely sure we don't send any queued up messages
removeMessages(R.id.decode_succeeded);
removeMessages(R.id.decode_failed);
removeMessages(activity.getApplicationContext().getResources().getIdentifier("decode_succeeded", "id", activity.getApplicationContext().getPackageName()));
removeMessages(activity.getApplicationContext().getResources().getIdentifier("decode_failed", "id", activity.getApplicationContext().getPackageName()));
}

private void restartPreviewAndDecode() {
if (state == State.SUCCESS) {
state = State.PREVIEW;
CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), activity.getApplicationContext().getResources().getIdentifier("decode", "id", activity.getApplicationContext().getPackageName()));
CameraManager.get().requestAutoFocus(this, activity.getApplicationContext().getResources().getIdentifier("auto_focus", "id", activity.getApplicationContext().getPackageName()));
activity.drawViewfinder();
}
}
Expand Down
Loading

0 comments on commit 9cff772

Please sign in to comment.