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.graphics.Bitmap;
13 import android.os.Bundle;
14 import android.text.TextUtils;
15 import android.util.Log;
16 import android.widget.ImageView;
17 
18 import com.google.zxing.BarcodeFormat;
19 import com.google.zxing.MultiFormatWriter;
20 import com.google.zxing.WriterException;
21 import com.google.zxing.common.BitMatrix;
22 
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 
30 public class QrCodeDisplayActivity extends Activity {
31 
32     private static final String TAG = "wpadebug";
33     private static final String FILE_NAME = "wpadebug_qrdata.txt";
34     private ImageView imageView;
35 
36     // Below set of configs are used for QR code display window
37     private final static int WHITE = 0xFFFFFFFF;
38     private final static int BLACK = 0xFF000000;
39     private final static int WIDTH = 400;
40     private final static int HEIGHT = 400;
41 
42     @Override
onCreate(Bundle savedInstanceState)43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         // create imageview for this and attach to this activity.
46         setContentView(R.layout.qrcode);
47         imageView = (ImageView) findViewById(R.id.qrCode);
48         String str = readFromFile(FILE_NAME);
49 
50         //Encode and launch qrcode now
51         try {
52             Bitmap bitmap = (TextUtils.isEmpty(str)) ? null : encodeAsBitmap(str);
53             if (bitmap != null) {
54                 imageView.setImageBitmap(bitmap);
55             } else {
56                 Log.e(TAG, "Failed to generate bitmap for uri=" + str);
57                 finish();
58             }
59         } catch (WriterException e) {
60             e.printStackTrace();
61             finish();
62         }
63     }
64 
encodeAsBitmap(String str)65     private Bitmap encodeAsBitmap(String str) throws WriterException {
66         BitMatrix result;
67         try {
68             result = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null);
69         } catch (IllegalArgumentException iae) {
70             // Unsupported format
71             return null;
72         }
73 
74         int width = result.getWidth();
75         int height = result.getHeight();
76         int[] pixels = new int[width * height];
77         for (int y = 0; y < height; y++) {
78             int offset = y * width;
79             for (int x = 0; x < width; x++) {
80                 pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
81             }
82         }
83 
84         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
85         bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
86         return bitmap;
87     }
88 
readFromFile(String filePath)89     private String readFromFile(String filePath) {
90         try {
91             FileInputStream fis = new FileInputStream(new File("/sdcard", filePath));
92             BufferedReader br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
93             StringBuilder sb = new StringBuilder();
94             String line;
95             while(( line = br.readLine()) != null ) {
96                 sb.append( line );
97                 sb.append( '\n' );
98             }
99             return sb.toString();
100         }
101         catch (FileNotFoundException e) {
102             Log.e(TAG, "File not found: " + e.toString());
103         } catch (IOException e) {
104             Log.e(TAG, "Can not read file: " + e.toString());
105         }
106 
107         return null;
108     }
109 }
110