1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 package com.android.car.linkviewer;
19 
20 import android.app.Activity;
21 import android.content.Intent;
22 import android.graphics.Bitmap;
23 import android.graphics.Color;
24 import android.graphics.drawable.BitmapDrawable;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.widget.ImageView;
28 import android.widget.TextView;
29 
30 import com.google.zxing.WriterException;
31 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
32 import com.google.zxing.qrcode.encoder.ByteMatrix;
33 import com.google.zxing.qrcode.encoder.Encoder;
34 import com.google.zxing.qrcode.encoder.QRCode;
35 
36 /**
37  * LinkViewerActivity responds to intents to display an HTTP or HTTPS URL by showing the link and
38  * also a QR code.
39  *
40  */
41 public class LinkViewerActivity extends Activity {
42     private static final String TAG = LinkViewerActivity.class.getSimpleName();
43 
44     private TextView mUrlText;
45     private ImageView mUrlImage;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50 
51         String url = getUrlFromIntent(getIntent());
52         if (url == null) {
53             finish();
54             return;
55         }
56 
57         setContentView(R.layout.link_viewer_activity);
58         mUrlText = findViewById(R.id.url_text);
59         mUrlImage = findViewById(R.id.url_image);
60 
61         showUrl(url);
62     }
63 
getUrlFromIntent(Intent intent)64     private static String getUrlFromIntent(Intent intent) {
65         return intent != null ? intent.getDataString() : null;
66     }
67 
showUrl(String url)68     private void showUrl(String url) {
69         mUrlText.setText(url);
70 
71         QRCode qrCode;
72         try {
73             qrCode = Encoder.encode(url, ErrorCorrectionLevel.H);
74         } catch (WriterException e) {
75             Log.e(TAG, "Error encoding URL: " + url, e);
76             return;
77         }
78 
79         BitmapDrawable drawable = new BitmapDrawable(getResources(), qrToBitmap(qrCode));
80         drawable.setAntiAlias(false);
81         drawable.setFilterBitmap(false);
82         mUrlImage.setImageDrawable(drawable);
83     }
84 
qrToBitmap(QRCode qrCode)85     private Bitmap qrToBitmap(QRCode qrCode) {
86         ByteMatrix matrix = qrCode.getMatrix();
87         int width = matrix.getWidth();
88         int height = matrix.getHeight();
89         int[] colors = new int[width * height];
90         for (int y = 0; y < height; y++) {
91             for (int x = 0; x < width; x++) {
92                 colors[y * width + x] = (matrix.get(x, y) != 0)
93                     ? Color.WHITE
94                     : Color.TRANSPARENT;
95             }
96         }
97 
98         return Bitmap.createBitmap(colors, 0, width, width, height, Bitmap.Config.ALPHA_8);
99     }
100 }
101 
102