30
30
31
31
import android .app .Activity ;
32
32
import android .app .AlertDialog ;
33
+ import android .content .Context ;
33
34
import android .content .DialogInterface ;
34
35
import android .content .Intent ;
35
36
import android .content .SharedPreferences ;
@@ -128,11 +129,13 @@ private enum Source {
128
129
private HistoryManager historyManager ;
129
130
private InactivityTimer inactivityTimer ;
130
131
private BeepManager beepManager ;
132
+ private Context context ;
133
+ private String pkgName ;
131
134
132
135
private final DialogInterface .OnClickListener aboutListener =
133
136
new DialogInterface .OnClickListener () {
134
137
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" ) )));
136
139
intent .addFlags (Intent .FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET );
137
140
startActivity (intent );
138
141
}
@@ -149,15 +152,18 @@ public Handler getHandler() {
149
152
@ Override
150
153
public void onCreate (Bundle icicle ) {
151
154
super .onCreate (icicle );
152
-
155
+
156
+ context = getApplicationContext ();
157
+ pkgName = context .getPackageName ();
158
+
153
159
Window window = getWindow ();
154
160
window .addFlags (WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON );
155
- setContentView (R . layout . capture );
161
+ setContentView (getIdentifier ( " layout" , " capture" ) );
156
162
157
163
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" ) );
161
167
handler = null ;
162
168
lastResult = null ;
163
169
hasSurface = false ;
@@ -168,13 +174,17 @@ public void onCreate(Bundle icicle) {
168
174
169
175
// showHelpOnFirstLaunch();
170
176
}
177
+
178
+ private int getIdentifier (String type , String name ) {
179
+ return context .getResources ().getIdentifier (name , type , pkgName );
180
+ }
171
181
172
182
@ Override
173
183
protected void onResume () {
174
184
super .onResume ();
175
185
resetStatusView ();
176
186
177
- SurfaceView surfaceView = (SurfaceView ) findViewById (R . id . preview_view );
187
+ SurfaceView surfaceView = (SurfaceView ) findViewById (getIdentifier ( "id" , " preview_view" ) );
178
188
SurfaceHolder surfaceHolder = surfaceView .getHolder ();
179
189
if (hasSurface ) {
180
190
// The activity was paused but not stopped, so the surface still exists. Therefore
@@ -263,7 +273,7 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
263
273
} else if ((source == Source .NONE || source == Source .ZXING_LINK ) && lastResult != null ) {
264
274
resetStatusView ();
265
275
if (handler != null ) {
266
- handler .sendEmptyMessage (R . id . restart_preview );
276
+ handler .sendEmptyMessage (getIdentifier ( "id" , " restart_preview" ) );
267
277
}
268
278
return true ;
269
279
}
@@ -277,16 +287,16 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
277
287
@ Override
278
288
public boolean onCreateOptionsMenu (Menu menu ) {
279
289
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" ) );
290
300
return true ;
291
301
}
292
302
@@ -329,11 +339,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
329
339
}
330
340
case ABOUT_ID :
331
341
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 );
337
347
builder .show ();
338
348
break ;
339
349
}
@@ -388,10 +398,10 @@ public void handleDecode(Result rawResult, Bitmap barcode) {
388
398
case NONE :
389
399
SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (this );
390
400
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 ();
392
402
// Wait a moment or else it will scan the same barcode continuously about 3 times
393
403
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 );
395
405
}
396
406
resetStatusView ();
397
407
} else {
@@ -413,13 +423,13 @@ private void drawResultPoints(Bitmap barcode, Result rawResult) {
413
423
if (points != null && points .length > 0 ) {
414
424
Canvas canvas = new Canvas (barcode );
415
425
Paint paint = new Paint ();
416
- paint .setColor (getResources ().getColor (R . color . result_image_border ));
426
+ paint .setColor (getResources ().getColor (getIdentifier ( " color" , " result_image_border" ) ));
417
427
paint .setStrokeWidth (3.0f );
418
428
paint .setStyle (Paint .Style .STROKE );
419
429
Rect border = new Rect (2 , 2 , barcode .getWidth () - 2 , barcode .getHeight () - 2 );
420
430
canvas .drawRect (border , paint );
421
431
422
- paint .setColor (getResources ().getColor (R . color . result_points ));
432
+ paint .setColor (getResources ().getColor (getIdentifier ( " color" , " result_points" ) ));
423
433
if (points .length == 2 ) {
424
434
paint .setStrokeWidth (4.0f );
425
435
drawLine (canvas , paint , points [0 ], points [1 ]);
@@ -448,28 +458,28 @@ private void handleDecodeInternally(Result rawResult, ResultHandler resultHandle
448
458
viewfinderView .setVisibility (View .GONE );
449
459
resultView .setVisibility (View .VISIBLE );
450
460
451
- ImageView barcodeImageView = (ImageView ) findViewById (R . id . barcode_image_view );
461
+ ImageView barcodeImageView = (ImageView ) findViewById (getIdentifier ( "id" , " barcode_image_view" ) );
452
462
if (barcode == null ) {
453
463
barcodeImageView .setImageBitmap (BitmapFactory .decodeResource (getResources (),
454
- R . drawable . launcher_icon ));
464
+ getIdentifier ( " drawable" , " launcher_icon" ) ));
455
465
} else {
456
466
barcodeImageView .setImageBitmap (barcode );
457
467
}
458
468
459
- TextView formatTextView = (TextView ) findViewById (R . id . format_text_view );
469
+ TextView formatTextView = (TextView ) findViewById (getIdentifier ( "id" , " format_text_view" ) );
460
470
formatTextView .setText (rawResult .getBarcodeFormat ().toString ());
461
471
462
- TextView typeTextView = (TextView ) findViewById (R . id . type_text_view );
472
+ TextView typeTextView = (TextView ) findViewById (getIdentifier ( "id" , " type_text_view" ) );
463
473
typeTextView .setText (resultHandler .getType ().toString ());
464
474
465
475
DateFormat formatter = DateFormat .getDateTimeInstance (DateFormat .SHORT , DateFormat .SHORT );
466
476
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" ) );
468
478
timeTextView .setText (formattedTime );
469
479
470
480
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" ) );
473
483
metaTextView .setVisibility (View .GONE );
474
484
metaTextViewLabel .setVisibility (View .GONE );
475
485
Map <ResultMetadataType ,Object > metadata =
@@ -489,14 +499,14 @@ private void handleDecodeInternally(Result rawResult, ResultHandler resultHandle
489
499
}
490
500
}
491
501
492
- TextView contentsTextView = (TextView ) findViewById (R . id . contents_text_view );
502
+ TextView contentsTextView = (TextView ) findViewById (getIdentifier ( "id" , " contents_text_view" ) );
493
503
CharSequence displayContents = resultHandler .getDisplayContents ();
494
504
contentsTextView .setText (displayContents );
495
505
// Crudely scale betweeen 22 and 32 -- bigger font for shorter text
496
506
int scaledSize = Math .max (22 , 32 - displayContents .length () / 4 );
497
507
contentsTextView .setTextSize (TypedValue .COMPLEX_UNIT_SP , scaledSize );
498
508
499
- TextView supplementTextView = (TextView ) findViewById (R . id . contents_supplement_text_view );
509
+ TextView supplementTextView = (TextView ) findViewById (getIdentifier ( "id" , " contents_supplement_text_view" ) );
500
510
supplementTextView .setText ("" );
501
511
supplementTextView .setOnClickListener (null );
502
512
if (PreferenceManager .getDefaultSharedPreferences (this ).getBoolean (
@@ -506,7 +516,7 @@ private void handleDecodeInternally(Result rawResult, ResultHandler resultHandle
506
516
}
507
517
508
518
int buttonCount = resultHandler .getButtonCount ();
509
- ViewGroup buttonView = (ViewGroup ) findViewById (R . id . result_button_view );
519
+ ViewGroup buttonView = (ViewGroup ) findViewById (getIdentifier ( "id" , " result_button_view" ) );
510
520
buttonView .requestFocus ();
511
521
for (int x = 0 ; x < ResultHandler .MAX_BUTTON_COUNT ; x ++) {
512
522
TextView button = (TextView ) buttonView .getChildAt (x );
@@ -550,21 +560,21 @@ private void handleDecodeExternally(Result rawResult, ResultHandler resultHandle
550
560
if (rawBytes != null && rawBytes .length > 0 ) {
551
561
intent .putExtra (Intents .Scan .RESULT_BYTES , rawBytes );
552
562
}
553
- Message message = Message .obtain (handler , R . id . return_scan_result );
563
+ Message message = Message .obtain (handler , getIdentifier ( "id" , " return_scan_result" ) );
554
564
message .obj = intent ;
555
565
handler .sendMessageDelayed (message , INTENT_RESULT_DURATION );
556
566
} else if (source == Source .PRODUCT_SEARCH_LINK ) {
557
567
// Reformulate the URL which triggered us into a query, so that the request goes to the same
558
568
// 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" ) );
560
570
int end = sourceUrl .lastIndexOf ("/scan" );
561
571
message .obj = sourceUrl .substring (0 , end ) + "?q=" +
562
572
resultHandler .getDisplayContents ().toString () + "&source=zxing" ;
563
573
handler .sendMessageDelayed (message , INTENT_RESULT_DURATION );
564
574
} else if (source == Source .ZXING_LINK ) {
565
575
// Replace each occurrence of RETURN_CODE_PLACEHOLDER in the returnUrlTemplate
566
576
// 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" ) );
568
578
message .obj = returnUrlTemplate .replace (RETURN_CODE_PLACEHOLDER ,
569
579
resultHandler .getDisplayContents ().toString ());
570
580
handler .sendMessageDelayed (message , INTENT_RESULT_DURATION );
@@ -621,16 +631,16 @@ private void initCamera(SurfaceHolder surfaceHolder) {
621
631
622
632
private void displayFrameworkBugMessageAndExit () {
623
633
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 ));
627
637
builder .setOnCancelListener (new FinishListener (this ));
628
638
builder .show ();
629
639
}
630
640
631
641
private void resetStatusView () {
632
642
resultView .setVisibility (View .GONE );
633
- statusView .setText (R . string . msg_default_status );
643
+ statusView .setText (getIdentifier ( " string" , " msg_default_status" ) );
634
644
statusView .setVisibility (View .VISIBLE );
635
645
viewfinderView .setVisibility (View .VISIBLE );
636
646
lastResult = null ;
0 commit comments