1 /*
2  * wpadebug - wpa_supplicant and Wi-Fi debugging app for Android
3  * Copyright (c) 2018, The Linux Foundation
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 package w1.fi.wpadebug;
10 
11 import android.app.Activity;
12 import android.content.ActivityNotFoundException;
13 import android.content.Intent;
14 import android.os.Bundle;
15 import android.util.Log;
16 import android.widget.Toast;
17 
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStreamWriter;
22 
23 public class QrCodeScannerActivity extends Activity {
24 
25     private static final String TAG = "wpadebug";
26     private static final String RESULT = "SCAN_RESULT";
27     private static final String FILE_NAME = "wpadebug_qrdata.txt";
28     private static final String ACTION = "com.google.zxing.client.android.SCAN";
29 
30     private static final int QRCODE = 1;
31 
32     @Override
onCreate(Bundle savedInstanceState)33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35 
36         Intent intent = new Intent();
37         intent.setAction(ACTION);
38 	intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
39 	intent.putExtra("PROMPT_MESSAGE",
40 			"Place a QR Code inside the viewfinder rectangle to scan it.");
41         try {
42             startActivityForResult(intent, QRCODE);
43         } catch (ActivityNotFoundException e) {
44             Log.e(TAG, "No QR code scanner found with name=" + ACTION);
45             Toast.makeText(QrCodeScannerActivity.this, "QR code scanner not found", Toast.LENGTH_SHORT).show();
46             finish();
47         }
48     }
49 
50     @Override
onActivityResult(int requestCode, int resultCode, Intent data)51     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
52 	Log.d(TAG, "onActivityResult: requestCode=" + requestCode + " resultCode=" + resultCode);
53         if (requestCode == QRCODE && resultCode == RESULT_OK) {
54 	    String contents = data.getStringExtra(RESULT);
55 	    writeToFile(contents);
56 	    Log.d(TAG, "onActivityResult: QRCODE RESULT_OK: " + contents);
57 	    finishActivity(requestCode);
58             finish();
59         }
60     }
61 
writeToFile(String data)62     public void writeToFile(String data)
63     {
64         File file = new File("/sdcard", FILE_NAME);
65         try
66         {
67             file.createNewFile();
68             FileOutputStream fOut = new FileOutputStream(file);
69             OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
70             myOutWriter.append(data);
71 
72             myOutWriter.close();
73 
74             fOut.flush();
75             fOut.close();
76         }
77         catch (IOException e)
78         {
79             Log.e(TAG, "File write failed: " + e.toString());
80         }
81     }
82 }
83