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 static com.android.car.ui.core.CarUi.requireToolbar; 21 import static com.android.car.ui.toolbar.Toolbar.State.SUBPAGE; 22 23 import android.app.Activity; 24 import android.content.Intent; 25 import android.graphics.Bitmap; 26 import android.graphics.Color; 27 import android.graphics.drawable.BitmapDrawable; 28 import android.os.Bundle; 29 import android.util.Log; 30 import android.widget.ImageView; 31 import android.widget.TextView; 32 33 import com.android.car.ui.toolbar.ToolbarController; 34 35 import com.google.zxing.WriterException; 36 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 37 import com.google.zxing.qrcode.encoder.ByteMatrix; 38 import com.google.zxing.qrcode.encoder.Encoder; 39 import com.google.zxing.qrcode.encoder.QRCode; 40 41 /** 42 * LinkViewerActivity responds to intents to display an HTTP or HTTPS URL by showing the link and 43 * also a QR code. 44 * 45 */ 46 public class LinkViewerActivity extends Activity { 47 private static final String TAG = LinkViewerActivity.class.getSimpleName(); 48 49 private TextView mUrlText; 50 private ImageView mUrlImage; 51 52 @Override onCreate(Bundle savedInstanceState)53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 56 ToolbarController toolbar = requireToolbar(this); 57 toolbar.setTitle(R.string.app_name); 58 toolbar.setState(SUBPAGE); 59 60 String url = getUrlFromIntent(getIntent()); 61 if (url == null) { 62 finish(); 63 return; 64 } 65 66 setContentView(R.layout.link_viewer_activity); 67 mUrlText = findViewById(R.id.url_text); 68 mUrlImage = findViewById(R.id.url_image); 69 70 showUrl(url); 71 } 72 getUrlFromIntent(Intent intent)73 private static String getUrlFromIntent(Intent intent) { 74 return intent != null ? intent.getDataString() : null; 75 } 76 showUrl(String url)77 private void showUrl(String url) { 78 mUrlText.setText(url); 79 80 QRCode qrCode; 81 try { 82 qrCode = Encoder.encode(url, ErrorCorrectionLevel.H); 83 } catch (WriterException e) { 84 Log.e(TAG, "Error encoding URL: " + url, e); 85 return; 86 } 87 88 BitmapDrawable drawable = new BitmapDrawable(getResources(), qrToBitmap(qrCode)); 89 drawable.setAntiAlias(false); 90 drawable.setFilterBitmap(false); 91 mUrlImage.setImageDrawable(drawable); 92 } 93 qrToBitmap(QRCode qrCode)94 private Bitmap qrToBitmap(QRCode qrCode) { 95 ByteMatrix matrix = qrCode.getMatrix(); 96 int width = matrix.getWidth(); 97 int height = matrix.getHeight(); 98 int[] colors = new int[width * height]; 99 for (int y = 0; y < height; y++) { 100 for (int x = 0; x < width; x++) { 101 colors[y * width + x] = (matrix.get(x, y) != 0) 102 ? Color.WHITE 103 : Color.TRANSPARENT; 104 } 105 } 106 107 return Bitmap.createBitmap(colors, 0, width, width, height, Bitmap.Config.ALPHA_8); 108 } 109 } 110 111