55import android .util .Log ;
66import android .content .Intent ;
77import android .Manifest ;
8+ import android .content .ClipData ;
9+ import android .content .ClipDescription ;
10+ import android .content .ClipboardManager ;
11+ import android .content .Context ;
812import android .content .pm .PackageManager ;
13+ import android .view .View ;
14+ import android .view .ViewGroup ;
15+ import android .widget .Button ;
16+ import android .widget .TextView ;
17+ import android .widget .Toast ;
918
1019import androidx .core .app .ActivityCompat ;
1120
1625import com .google .zxing .Result ;
1726import com .google .zxing .BarcodeFormat ;
1827
28+ import org .electrum .electrum .res .R ; // package set in build.gradle
29+
1930public class SimpleScannerActivity extends Activity implements ZXingScannerView .ResultHandler {
2031 private static final int MY_PERMISSIONS_CAMERA = 1002 ;
2132
2233 private ZXingScannerView mScannerView = null ;
2334 final String TAG = "org.electrum.SimpleScannerActivity" ;
2435
36+ private boolean mAlreadyRequestedPermissions = false ;
37+
38+ @ Override
39+ public void onCreate (Bundle savedInstanceState ) {
40+ super .onCreate (savedInstanceState );
41+ setContentView (R .layout .scanner_layout );
42+
43+ // change top text
44+ Intent intent = getIntent ();
45+ String text = intent .getStringExtra (intent .EXTRA_TEXT );
46+ TextView hintTextView = (TextView ) findViewById (R .id .hint );
47+ hintTextView .setText (text );
48+
49+ // bind "paste" button
50+ Button btn = (Button ) findViewById (R .id .paste_btn );
51+ btn .setOnClickListener (new View .OnClickListener () {
52+ @ Override
53+ public void onClick (View v ) {
54+ ClipboardManager clipboard = (ClipboardManager ) getSystemService (Context .CLIPBOARD_SERVICE );
55+ if (clipboard .hasPrimaryClip ()
56+ && (clipboard .getPrimaryClipDescription ().hasMimeType (ClipDescription .MIMETYPE_TEXT_PLAIN )
57+ || clipboard .getPrimaryClipDescription ().hasMimeType (ClipDescription .MIMETYPE_TEXT_HTML ))) {
58+ ClipData .Item item = clipboard .getPrimaryClip ().getItemAt (0 );
59+ String clipboardText = item .getText ().toString ();
60+ SimpleScannerActivity .this .setResultAndClose (clipboardText );
61+ } else {
62+ Toast .makeText (SimpleScannerActivity .this , "Clipboard is empty." , Toast .LENGTH_SHORT ).show ();
63+ }
64+ }
65+ });
66+ }
67+
2568 @ Override
2669 public void onResume () {
2770 super .onResume ();
2871 if (this .hasPermission ()) {
2972 this .startCamera ();
30- } else {
73+ } else if (!mAlreadyRequestedPermissions ) {
74+ mAlreadyRequestedPermissions = true ;
3175 this .requestPermission ();
3276 }
3377 }
@@ -41,18 +85,23 @@ public void onPause() {
4185 }
4286
4387 private void startCamera () {
44- mScannerView = new ZXingScannerView (this ); // Programmatically initialize the scanner view
88+ mScannerView = new ZXingScannerView (this );
4589 mScannerView .setFormats (Arrays .asList (BarcodeFormat .QR_CODE ));
46- setContentView (mScannerView ); // Set the scanner view as the content view
90+ ViewGroup contentFrame = (ViewGroup ) findViewById (R .id .content_frame );
91+ contentFrame .addView (mScannerView );
4792 mScannerView .setResultHandler (this ); // Register ourselves as a handler for scan results.
4893 mScannerView .startCamera (); // Start camera on resume
4994 }
5095
5196 @ Override
5297 public void handleResult (Result rawResult ) {
98+ //resultIntent.putExtra("format", rawResult.getBarcodeFormat().toString());
99+ this .setResultAndClose (rawResult .getText ());
100+ }
101+
102+ private void setResultAndClose (String resultText ) {
53103 Intent resultIntent = new Intent ();
54- resultIntent .putExtra ("text" , rawResult .getText ());
55- resultIntent .putExtra ("format" , rawResult .getBarcodeFormat ().toString ());
104+ resultIntent .putExtra ("text" , resultText );
56105 setResult (Activity .RESULT_OK , resultIntent );
57106 this .finish ();
58107 }
@@ -80,7 +129,7 @@ public void onRequestPermissionsResult(int requestCode,
80129 this .startCamera ();
81130 } else {
82131 // permission denied
83- this .finish ();
132+ // this.finish();
84133 }
85134 return ;
86135 }
0 commit comments