Skip to content

Commit 9cff772

Browse files
author
Ryan Willoughby
committedOct 16, 2012
[android] Modified zxing client to access resources from main package
1 parent e3505ae commit 9cff772

39 files changed

+504
-447
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.DS_Store
22
bin/
3+
.classpath
4+
.project

‎src/android/LibraryProject/default.properties

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** Automatically generated file. DO NOT MODIFY */
2+
package com.google.zxing.client.android;
3+
4+
public final class BuildConfig {
5+
public final static boolean DEBUG = true;
6+
}

‎src/android/LibraryProject/gen/com/google/zxing/client/android/R.java

+206-206
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file is automatically generated by Android Tools.
2+
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3+
#
4+
# This file must be checked in Version Control Systems.
5+
#
6+
# To customize properties used by the Ant build system edit
7+
# "ant.properties", and override values to adapt the script to your
8+
# project structure.
9+
#
10+
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11+
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12+
13+
android.library=true
14+
# Project target.
15+
target=android-16

‎src/android/LibraryProject/src/com/google/zxing/client/android/BeepManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void onCompletion(MediaPlayer player) {
9393
}
9494
});
9595

96-
AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
96+
AssetFileDescriptor file = activity.getResources().openRawResourceFd(activity.getApplicationContext().getResources().getIdentifier("beep", "raw", activity.getApplicationContext().getPackageName()));
9797
try {
9898
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
9999
file.close();

‎src/android/LibraryProject/src/com/google/zxing/client/android/CaptureActivity.java

+54-44
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import android.app.Activity;
3232
import android.app.AlertDialog;
33+
import android.content.Context;
3334
import android.content.DialogInterface;
3435
import android.content.Intent;
3536
import android.content.SharedPreferences;
@@ -128,11 +129,13 @@ private enum Source {
128129
private HistoryManager historyManager;
129130
private InactivityTimer inactivityTimer;
130131
private BeepManager beepManager;
132+
private Context context;
133+
private String pkgName;
131134

132135
private final DialogInterface.OnClickListener aboutListener =
133136
new DialogInterface.OnClickListener() {
134137
public void onClick(DialogInterface dialogInterface, int i) {
135-
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.zxing_url)));
138+
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(getIdentifier("string", "zxing_url"))));
136139
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
137140
startActivity(intent);
138141
}
@@ -149,15 +152,18 @@ public Handler getHandler() {
149152
@Override
150153
public void onCreate(Bundle icicle) {
151154
super.onCreate(icicle);
152-
155+
156+
context = getApplicationContext();
157+
pkgName = context.getPackageName();
158+
153159
Window window = getWindow();
154160
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
155-
setContentView(R.layout.capture);
161+
setContentView(getIdentifier("layout", "capture"));
156162

157163
CameraManager.init(getApplication());
158-
viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
159-
resultView = findViewById(R.id.result_view);
160-
statusView = (TextView) findViewById(R.id.status_view);
164+
viewfinderView = (ViewfinderView) findViewById(getIdentifier("id", "viewfinder_view"));
165+
resultView = findViewById(getIdentifier("id", "result_view"));
166+
statusView = (TextView) findViewById(getIdentifier("id", "status_view"));
161167
handler = null;
162168
lastResult = null;
163169
hasSurface = false;
@@ -168,13 +174,17 @@ public void onCreate(Bundle icicle) {
168174

169175
// showHelpOnFirstLaunch();
170176
}
177+
178+
private int getIdentifier(String type, String name) {
179+
return context.getResources().getIdentifier(name, type, pkgName);
180+
}
171181

172182
@Override
173183
protected void onResume() {
174184
super.onResume();
175185
resetStatusView();
176186

177-
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
187+
SurfaceView surfaceView = (SurfaceView) findViewById(getIdentifier("id", "preview_view"));
178188
SurfaceHolder surfaceHolder = surfaceView.getHolder();
179189
if (hasSurface) {
180190
// The activity was paused but not stopped, so the surface still exists. Therefore
@@ -263,7 +273,7 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
263273
} else if ((source == Source.NONE || source == Source.ZXING_LINK) && lastResult != null) {
264274
resetStatusView();
265275
if (handler != null) {
266-
handler.sendEmptyMessage(R.id.restart_preview);
276+
handler.sendEmptyMessage(getIdentifier("id", "restart_preview"));
267277
}
268278
return true;
269279
}
@@ -277,16 +287,16 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
277287
@Override
278288
public boolean onCreateOptionsMenu(Menu menu) {
279289
super.onCreateOptionsMenu(menu);
280-
menu.add(0, SHARE_ID, 0, R.string.menu_share)
281-
.setIcon(android.R.drawable.ic_menu_share);
282-
menu.add(0, HISTORY_ID, 0, R.string.menu_history)
283-
.setIcon(android.R.drawable.ic_menu_recent_history);
284-
menu.add(0, SETTINGS_ID, 0, R.string.menu_settings)
285-
.setIcon(android.R.drawable.ic_menu_preferences);
286-
menu.add(0, HELP_ID, 0, R.string.menu_help)
287-
.setIcon(android.R.drawable.ic_menu_help);
288-
menu.add(0, ABOUT_ID, 0, R.string.menu_about)
289-
.setIcon(android.R.drawable.ic_menu_info_details);
290+
menu.add(0, SHARE_ID, 0, getIdentifier("string", "menu_share"))
291+
.setIcon(getIdentifier("drawable", "ic_menu_share"));
292+
menu.add(0, HISTORY_ID, 0, getIdentifier("string", "menu_history"))
293+
.setIcon(getIdentifier("drawable", "ic_menu_recent_history"));
294+
menu.add(0, SETTINGS_ID, 0, getIdentifier("string", "menu_settings"))
295+
.setIcon(getIdentifier("drawable", "ic_menu_preferences"));
296+
menu.add(0, HELP_ID, 0, getIdentifier("string", "menu_help"))
297+
.setIcon(getIdentifier("drawable", "ic_menu_help"));
298+
menu.add(0, ABOUT_ID, 0, getIdentifier("string", "menu_about"))
299+
.setIcon(getIdentifier("drawable", "ic_menu_info_details"));
290300
return true;
291301
}
292302

@@ -329,11 +339,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
329339
}
330340
case ABOUT_ID:
331341
AlertDialog.Builder builder = new AlertDialog.Builder(this);
332-
builder.setTitle(getString(R.string.title_about) + versionName);
333-
builder.setMessage(getString(R.string.msg_about) + "\n\n" + getString(R.string.zxing_url));
334-
builder.setIcon(R.drawable.launcher_icon);
335-
builder.setPositiveButton(R.string.button_open_browser, aboutListener);
336-
builder.setNegativeButton(R.string.button_cancel, null);
342+
builder.setTitle(getString(getIdentifier("string", "title_about")) + versionName);
343+
builder.setMessage(getString(getIdentifier("string", "msg_about")) + "\n\n" + getString(getIdentifier("string", "zxing_url")));
344+
builder.setIcon(getIdentifier("drawable", "launcher_icon"));
345+
builder.setPositiveButton(getIdentifier("string", "button_open_browser"), aboutListener);
346+
builder.setNegativeButton(getIdentifier("string", "button_cancel"), null);
337347
builder.show();
338348
break;
339349
}
@@ -388,10 +398,10 @@ public void handleDecode(Result rawResult, Bitmap barcode) {
388398
case NONE:
389399
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
390400
if (prefs.getBoolean(PreferencesActivity.KEY_BULK_MODE, false)) {
391-
Toast.makeText(this, R.string.msg_bulk_mode_scanned, Toast.LENGTH_SHORT).show();
401+
Toast.makeText(this, getIdentifier("string", "msg_bulk_mode_scanned"), Toast.LENGTH_SHORT).show();
392402
// Wait a moment or else it will scan the same barcode continuously about 3 times
393403
if (handler != null) {
394-
handler.sendEmptyMessageDelayed(R.id.restart_preview, BULK_MODE_SCAN_DELAY_MS);
404+
handler.sendEmptyMessageDelayed(getIdentifier("id", "restart_preview"), BULK_MODE_SCAN_DELAY_MS);
395405
}
396406
resetStatusView();
397407
} else {
@@ -413,13 +423,13 @@ private void drawResultPoints(Bitmap barcode, Result rawResult) {
413423
if (points != null && points.length > 0) {
414424
Canvas canvas = new Canvas(barcode);
415425
Paint paint = new Paint();
416-
paint.setColor(getResources().getColor(R.color.result_image_border));
426+
paint.setColor(getResources().getColor(getIdentifier("color", "result_image_border")));
417427
paint.setStrokeWidth(3.0f);
418428
paint.setStyle(Paint.Style.STROKE);
419429
Rect border = new Rect(2, 2, barcode.getWidth() - 2, barcode.getHeight() - 2);
420430
canvas.drawRect(border, paint);
421431

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

451-
ImageView barcodeImageView = (ImageView) findViewById(R.id.barcode_image_view);
461+
ImageView barcodeImageView = (ImageView) findViewById(getIdentifier("id", "barcode_image_view"));
452462
if (barcode == null) {
453463
barcodeImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),
454-
R.drawable.launcher_icon));
464+
getIdentifier("drawable", "launcher_icon")));
455465
} else {
456466
barcodeImageView.setImageBitmap(barcode);
457467
}
458468

459-
TextView formatTextView = (TextView) findViewById(R.id.format_text_view);
469+
TextView formatTextView = (TextView) findViewById(getIdentifier("id", "format_text_view"));
460470
formatTextView.setText(rawResult.getBarcodeFormat().toString());
461471

462-
TextView typeTextView = (TextView) findViewById(R.id.type_text_view);
472+
TextView typeTextView = (TextView) findViewById(getIdentifier("id", "type_text_view"));
463473
typeTextView.setText(resultHandler.getType().toString());
464474

465475
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
466476
String formattedTime = formatter.format(new Date(rawResult.getTimestamp()));
467-
TextView timeTextView = (TextView) findViewById(R.id.time_text_view);
477+
TextView timeTextView = (TextView) findViewById(getIdentifier("id", "time_text_view"));
468478
timeTextView.setText(formattedTime);
469479

470480

471-
TextView metaTextView = (TextView) findViewById(R.id.meta_text_view);
472-
View metaTextViewLabel = findViewById(R.id.meta_text_view_label);
481+
TextView metaTextView = (TextView) findViewById(getIdentifier("id", "meta_text_view"));
482+
View metaTextViewLabel = findViewById(getIdentifier("id", "meta_text_view_label"));
473483
metaTextView.setVisibility(View.GONE);
474484
metaTextViewLabel.setVisibility(View.GONE);
475485
Map<ResultMetadataType,Object> metadata =
@@ -489,14 +499,14 @@ private void handleDecodeInternally(Result rawResult, ResultHandler resultHandle
489499
}
490500
}
491501

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

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

508518
int buttonCount = resultHandler.getButtonCount();
509-
ViewGroup buttonView = (ViewGroup) findViewById(R.id.result_button_view);
519+
ViewGroup buttonView = (ViewGroup) findViewById(getIdentifier("id", "result_button_view"));
510520
buttonView.requestFocus();
511521
for (int x = 0; x < ResultHandler.MAX_BUTTON_COUNT; x++) {
512522
TextView button = (TextView) buttonView.getChildAt(x);
@@ -550,21 +560,21 @@ private void handleDecodeExternally(Result rawResult, ResultHandler resultHandle
550560
if (rawBytes != null && rawBytes.length > 0) {
551561
intent.putExtra(Intents.Scan.RESULT_BYTES, rawBytes);
552562
}
553-
Message message = Message.obtain(handler, R.id.return_scan_result);
563+
Message message = Message.obtain(handler, getIdentifier("id", "return_scan_result"));
554564
message.obj = intent;
555565
handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
556566
} else if (source == Source.PRODUCT_SEARCH_LINK) {
557567
// Reformulate the URL which triggered us into a query, so that the request goes to the same
558568
// TLD as the scan URL.
559-
Message message = Message.obtain(handler, R.id.launch_product_query);
569+
Message message = Message.obtain(handler, getIdentifier("id", "launch_product_query"));
560570
int end = sourceUrl.lastIndexOf("/scan");
561571
message.obj = sourceUrl.substring(0, end) + "?q=" +
562572
resultHandler.getDisplayContents().toString() + "&source=zxing";
563573
handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
564574
} else if (source == Source.ZXING_LINK) {
565575
// Replace each occurrence of RETURN_CODE_PLACEHOLDER in the returnUrlTemplate
566576
// with the scanned code. This allows both queries and REST-style URLs to work.
567-
Message message = Message.obtain(handler, R.id.launch_product_query);
577+
Message message = Message.obtain(handler, getIdentifier("id", "launch_product_query"));
568578
message.obj = returnUrlTemplate.replace(RETURN_CODE_PLACEHOLDER,
569579
resultHandler.getDisplayContents().toString());
570580
handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
@@ -621,16 +631,16 @@ private void initCamera(SurfaceHolder surfaceHolder) {
621631

622632
private void displayFrameworkBugMessageAndExit() {
623633
AlertDialog.Builder builder = new AlertDialog.Builder(this);
624-
builder.setTitle(getString(R.string.app_name));
625-
builder.setMessage(getString(R.string.msg_camera_framework_bug));
626-
builder.setPositiveButton(R.string.button_ok, new FinishListener(this));
634+
builder.setTitle(getString(getIdentifier("string", "app_name")));
635+
builder.setMessage(getString(getIdentifier("string", "msg_camera_framework_bug")));
636+
builder.setPositiveButton(getIdentifier("string", "button_ok"), new FinishListener(this));
627637
builder.setOnCancelListener(new FinishListener(this));
628638
builder.show();
629639
}
630640

631641
private void resetStatusView() {
632642
resultView.setVisibility(View.GONE);
633-
statusView.setText(R.string.msg_default_status);
643+
statusView.setText(getIdentifier("string", "msg_default_status"));
634644
statusView.setVisibility(View.VISIBLE);
635645
viewfinderView.setVisibility(View.VISIBLE);
636646
lastResult = null;

‎src/android/LibraryProject/src/com/google/zxing/client/android/CaptureActivityHandler.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,32 @@ private enum State {
6565

6666
@Override
6767
public void handleMessage(Message message) {
68-
if (message.what == R.id.auto_focus) {
68+
if (message.what == activity.getApplicationContext().getResources().getIdentifier("auto_focus", "id", activity.getApplicationContext().getPackageName())) {
6969
//Log.d(TAG, "Got auto-focus message");
7070
// When one auto focus pass finishes, start another. This is the closest thing to
7171
// continuous AF. It does seem to hunt a bit, but I'm not sure what else to do.
7272
if (state == State.PREVIEW) {
73-
CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
73+
CameraManager.get().requestAutoFocus(this, activity.getApplicationContext().getResources().getIdentifier("auto_focus", "id", activity.getApplicationContext().getPackageName()));
7474
}
75-
} else if (message.what == R.id.restart_preview) {
75+
} else if (message.what == activity.getApplicationContext().getResources().getIdentifier("restart_preview", "id", activity.getApplicationContext().getPackageName())) {
7676
Log.d(TAG, "Got restart preview message");
7777
restartPreviewAndDecode();
78-
} else if (message.what == R.id.decode_succeeded) {
78+
} else if (message.what == activity.getApplicationContext().getResources().getIdentifier("decode_succeeded", "id", activity.getApplicationContext().getPackageName())) {
7979
Log.d(TAG, "Got decode succeeded message");
8080
state = State.SUCCESS;
8181
Bundle bundle = message.getData();
8282
Bitmap barcode = bundle == null ? null :
8383
(Bitmap) bundle.getParcelable(DecodeThread.BARCODE_BITMAP);
8484
activity.handleDecode((Result) message.obj, barcode);
85-
} else if (message.what == R.id.decode_failed) {
85+
} else if (message.what == activity.getApplicationContext().getResources().getIdentifier("decode_failed", "id", activity.getApplicationContext().getPackageName())) {
8686
// We're decoding as fast as possible, so when one decode fails, start another.
8787
state = State.PREVIEW;
88-
CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
89-
} else if (message.what == R.id.return_scan_result) {
88+
CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), activity.getApplicationContext().getResources().getIdentifier("decode", "id", activity.getApplicationContext().getPackageName()));
89+
} else if (message.what == activity.getApplicationContext().getResources().getIdentifier("return_scan_result", "id", activity.getApplicationContext().getPackageName())) {
9090
Log.d(TAG, "Got return scan result message");
9191
activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
9292
activity.finish();
93-
} else if (message.what == R.id.launch_product_query) {
93+
} else if (message.what == activity.getApplicationContext().getResources().getIdentifier("launch_product_query", "id", activity.getApplicationContext().getPackageName())) {
9494
Log.d(TAG, "Got product query message");
9595
String url = (String) message.obj;
9696
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
@@ -102,7 +102,7 @@ public void handleMessage(Message message) {
102102
public void quitSynchronously() {
103103
state = State.DONE;
104104
CameraManager.get().stopPreview();
105-
Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
105+
Message quit = Message.obtain(decodeThread.getHandler(), activity.getApplicationContext().getResources().getIdentifier("quit", "id", activity.getApplicationContext().getPackageName()));
106106
quit.sendToTarget();
107107
try {
108108
decodeThread.join();
@@ -111,15 +111,15 @@ public void quitSynchronously() {
111111
}
112112

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

118118
private void restartPreviewAndDecode() {
119119
if (state == State.SUCCESS) {
120120
state = State.PREVIEW;
121-
CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
122-
CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
121+
CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), activity.getApplicationContext().getResources().getIdentifier("decode", "id", activity.getApplicationContext().getPackageName()));
122+
CameraManager.get().requestAutoFocus(this, activity.getApplicationContext().getResources().getIdentifier("auto_focus", "id", activity.getApplicationContext().getPackageName()));
123123
activity.drawViewfinder();
124124
}
125125
}

0 commit comments

Comments
 (0)