407 lines
15 KiB
Java
407 lines
15 KiB
Java
package com.google.zxing.client.android.encode;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.graphics.Bitmap;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.EncodeHintType;
|
|
import com.google.zxing.MultiFormatWriter;
|
|
import com.google.zxing.Result;
|
|
import com.google.zxing.WriterException;
|
|
import com.google.zxing.client.android.Contents;
|
|
import com.google.zxing.client.android.R$string;
|
|
import com.google.zxing.client.result.AddressBookParsedResult;
|
|
import com.google.zxing.client.result.ParsedResult;
|
|
import com.google.zxing.client.result.ResultParser;
|
|
import com.google.zxing.common.BitMatrix;
|
|
import com.unity3d.ads.metadata.MediationMetaData;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.EnumMap;
|
|
import java.util.List;
|
|
|
|
/* loaded from: classes.dex */
|
|
final class QRCodeEncoder {
|
|
private static final String h = "QRCodeEncoder";
|
|
private final Context a;
|
|
private String b;
|
|
private String c;
|
|
private String d;
|
|
private BarcodeFormat e;
|
|
private final int f;
|
|
private final boolean g;
|
|
|
|
QRCodeEncoder(Context context, Intent intent, int i, boolean z) throws WriterException {
|
|
this.a = context;
|
|
this.f = i;
|
|
this.g = z;
|
|
String action = intent.getAction();
|
|
if ("com.google.zxing.client.android.ENCODE".equals(action)) {
|
|
b(intent);
|
|
} else if ("android.intent.action.SEND".equals(action)) {
|
|
a(intent);
|
|
}
|
|
}
|
|
|
|
private void a(Intent intent) throws WriterException {
|
|
if (intent.hasExtra("android.intent.extra.STREAM")) {
|
|
c(intent);
|
|
} else {
|
|
d(intent);
|
|
}
|
|
}
|
|
|
|
String b() {
|
|
return this.b;
|
|
}
|
|
|
|
String c() {
|
|
return this.c;
|
|
}
|
|
|
|
String d() {
|
|
return this.d;
|
|
}
|
|
|
|
boolean e() {
|
|
return this.g;
|
|
}
|
|
|
|
private void b(Intent intent) {
|
|
String stringExtra = intent.getStringExtra("ENCODE_FORMAT");
|
|
this.e = null;
|
|
if (stringExtra != null) {
|
|
try {
|
|
this.e = BarcodeFormat.valueOf(stringExtra);
|
|
} catch (IllegalArgumentException unused) {
|
|
}
|
|
}
|
|
BarcodeFormat barcodeFormat = this.e;
|
|
if (barcodeFormat == null || barcodeFormat == BarcodeFormat.QR_CODE) {
|
|
String stringExtra2 = intent.getStringExtra("ENCODE_TYPE");
|
|
if (stringExtra2 == null || stringExtra2.isEmpty()) {
|
|
return;
|
|
}
|
|
this.e = BarcodeFormat.QR_CODE;
|
|
a(intent, stringExtra2);
|
|
return;
|
|
}
|
|
String stringExtra3 = intent.getStringExtra("ENCODE_DATA");
|
|
if (stringExtra3 == null || stringExtra3.isEmpty()) {
|
|
return;
|
|
}
|
|
this.b = stringExtra3;
|
|
this.c = stringExtra3;
|
|
this.d = this.a.getString(R$string.contents_text);
|
|
}
|
|
|
|
private void c(Intent intent) throws WriterException {
|
|
this.e = BarcodeFormat.QR_CODE;
|
|
Bundle extras = intent.getExtras();
|
|
if (extras == null) {
|
|
throw new WriterException("No extras");
|
|
}
|
|
Uri uri = (Uri) extras.getParcelable("android.intent.extra.STREAM");
|
|
if (uri == null) {
|
|
throw new WriterException("No EXTRA_STREAM");
|
|
}
|
|
ByteArrayOutputStream byteArrayOutputStream = null;
|
|
try {
|
|
try {
|
|
InputStream openInputStream = this.a.getContentResolver().openInputStream(uri);
|
|
try {
|
|
if (openInputStream == null) {
|
|
throw new WriterException("Can't open stream for " + uri);
|
|
}
|
|
ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
|
|
try {
|
|
byte[] bArr = new byte[2048];
|
|
while (true) {
|
|
int read = openInputStream.read(bArr);
|
|
if (read <= 0) {
|
|
break;
|
|
} else {
|
|
byteArrayOutputStream2.write(bArr, 0, read);
|
|
}
|
|
}
|
|
byte[] byteArray = byteArrayOutputStream2.toByteArray();
|
|
String str = new String(byteArray, 0, byteArray.length, StandardCharsets.UTF_8);
|
|
if (openInputStream != null) {
|
|
try {
|
|
openInputStream.close();
|
|
} catch (IOException e) {
|
|
e = e;
|
|
throw new WriterException(e);
|
|
} catch (Throwable th) {
|
|
byteArrayOutputStream = byteArrayOutputStream2;
|
|
th = th;
|
|
if (byteArrayOutputStream != null) {
|
|
try {
|
|
byteArrayOutputStream.close();
|
|
} catch (IOException e2) {
|
|
e2.printStackTrace();
|
|
}
|
|
}
|
|
throw th;
|
|
}
|
|
}
|
|
try {
|
|
byteArrayOutputStream2.close();
|
|
} catch (IOException e3) {
|
|
e3.printStackTrace();
|
|
}
|
|
Log.d(h, "Encoding share intent content:");
|
|
Log.d(h, str);
|
|
ParsedResult c = ResultParser.c(new Result(str, byteArray, null, BarcodeFormat.QR_CODE));
|
|
if (!(c instanceof AddressBookParsedResult)) {
|
|
throw new WriterException("Result was not an address");
|
|
}
|
|
a((AddressBookParsedResult) c);
|
|
String str2 = this.b;
|
|
if (str2 == null || str2.isEmpty()) {
|
|
throw new WriterException("No content to encode");
|
|
}
|
|
} catch (Throwable th2) {
|
|
th = th2;
|
|
try {
|
|
throw th;
|
|
} finally {
|
|
}
|
|
}
|
|
} catch (Throwable th3) {
|
|
th = th3;
|
|
}
|
|
} catch (IOException e4) {
|
|
e = e4;
|
|
}
|
|
} catch (Throwable th4) {
|
|
th = th4;
|
|
}
|
|
}
|
|
|
|
private void d(Intent intent) throws WriterException {
|
|
String b = ContactEncoder.b(intent.getStringExtra("android.intent.extra.TEXT"));
|
|
if (b == null && (b = ContactEncoder.b(intent.getStringExtra("android.intent.extra.HTML_TEXT"))) == null && (b = ContactEncoder.b(intent.getStringExtra("android.intent.extra.SUBJECT"))) == null) {
|
|
String[] stringArrayExtra = intent.getStringArrayExtra("android.intent.extra.EMAIL");
|
|
b = stringArrayExtra != null ? ContactEncoder.b(stringArrayExtra[0]) : "?";
|
|
}
|
|
if (b == null || b.isEmpty()) {
|
|
throw new WriterException("Empty EXTRA_TEXT");
|
|
}
|
|
this.b = b;
|
|
this.e = BarcodeFormat.QR_CODE;
|
|
if (intent.hasExtra("android.intent.extra.SUBJECT")) {
|
|
this.c = intent.getStringExtra("android.intent.extra.SUBJECT");
|
|
} else if (intent.hasExtra("android.intent.extra.TITLE")) {
|
|
this.c = intent.getStringExtra("android.intent.extra.TITLE");
|
|
} else {
|
|
this.c = this.b;
|
|
}
|
|
this.d = this.a.getString(R$string.contents_text);
|
|
}
|
|
|
|
/* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
|
|
private void a(Intent intent, String str) {
|
|
char c;
|
|
Bundle bundleExtra;
|
|
switch (str.hashCode()) {
|
|
case -1309271157:
|
|
if (str.equals("PHONE_TYPE")) {
|
|
c = 2;
|
|
break;
|
|
}
|
|
c = 65535;
|
|
break;
|
|
case -670199783:
|
|
if (str.equals("CONTACT_TYPE")) {
|
|
c = 4;
|
|
break;
|
|
}
|
|
c = 65535;
|
|
break;
|
|
case 709220992:
|
|
if (str.equals("SMS_TYPE")) {
|
|
c = 3;
|
|
break;
|
|
}
|
|
c = 65535;
|
|
break;
|
|
case 1349204356:
|
|
if (str.equals("LOCATION_TYPE")) {
|
|
c = 5;
|
|
break;
|
|
}
|
|
c = 65535;
|
|
break;
|
|
case 1778595596:
|
|
if (str.equals("TEXT_TYPE")) {
|
|
c = 0;
|
|
break;
|
|
}
|
|
c = 65535;
|
|
break;
|
|
case 1833351709:
|
|
if (str.equals("EMAIL_TYPE")) {
|
|
c = 1;
|
|
break;
|
|
}
|
|
c = 65535;
|
|
break;
|
|
default:
|
|
c = 65535;
|
|
break;
|
|
}
|
|
if (c == 0) {
|
|
String stringExtra = intent.getStringExtra("ENCODE_DATA");
|
|
if (stringExtra == null || stringExtra.isEmpty()) {
|
|
return;
|
|
}
|
|
this.b = stringExtra;
|
|
this.c = stringExtra;
|
|
this.d = this.a.getString(R$string.contents_text);
|
|
return;
|
|
}
|
|
if (c == 1) {
|
|
String b = ContactEncoder.b(intent.getStringExtra("ENCODE_DATA"));
|
|
if (b != null) {
|
|
this.b = "mailto:" + b;
|
|
this.c = b;
|
|
this.d = this.a.getString(R$string.contents_email);
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
if (c == 2) {
|
|
String b2 = ContactEncoder.b(intent.getStringExtra("ENCODE_DATA"));
|
|
if (b2 != null) {
|
|
this.b = "tel:" + b2;
|
|
this.c = ContactEncoder.a(b2);
|
|
this.d = this.a.getString(R$string.contents_phone);
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
if (c == 3) {
|
|
String b3 = ContactEncoder.b(intent.getStringExtra("ENCODE_DATA"));
|
|
if (b3 != null) {
|
|
this.b = "sms:" + b3;
|
|
this.c = ContactEncoder.a(b3);
|
|
this.d = this.a.getString(R$string.contents_sms);
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
if (c == 4) {
|
|
Bundle bundleExtra2 = intent.getBundleExtra("ENCODE_DATA");
|
|
if (bundleExtra2 != null) {
|
|
String string = bundleExtra2.getString(MediationMetaData.KEY_NAME);
|
|
String string2 = bundleExtra2.getString("company");
|
|
String string3 = bundleExtra2.getString("postal");
|
|
List<String> a = a(bundleExtra2, Contents.a);
|
|
List<String> a2 = a(bundleExtra2, Contents.b);
|
|
List<String> a3 = a(bundleExtra2, Contents.c);
|
|
String string4 = bundleExtra2.getString("URL_KEY");
|
|
String[] a4 = (this.g ? new VCardContactEncoder() : new MECARDContactEncoder()).a(Collections.singletonList(string), string2, Collections.singletonList(string3), a, a2, a3, string4 == null ? null : Collections.singletonList(string4), bundleExtra2.getString("NOTE_KEY"));
|
|
if (a4[1].isEmpty()) {
|
|
return;
|
|
}
|
|
this.b = a4[0];
|
|
this.c = a4[1];
|
|
this.d = this.a.getString(R$string.contents_contact);
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
if (c == 5 && (bundleExtra = intent.getBundleExtra("ENCODE_DATA")) != null) {
|
|
float f = bundleExtra.getFloat("LAT", Float.MAX_VALUE);
|
|
float f2 = bundleExtra.getFloat("LONG", Float.MAX_VALUE);
|
|
if (f == Float.MAX_VALUE || f2 == Float.MAX_VALUE) {
|
|
return;
|
|
}
|
|
this.b = "geo:" + f + ',' + f2;
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(f);
|
|
sb.append(",");
|
|
sb.append(f2);
|
|
this.c = sb.toString();
|
|
this.d = this.a.getString(R$string.contents_location);
|
|
}
|
|
}
|
|
|
|
private static List<String> a(Bundle bundle, String[] strArr) {
|
|
ArrayList arrayList = new ArrayList(strArr.length);
|
|
for (String str : strArr) {
|
|
Object obj = bundle.get(str);
|
|
arrayList.add(obj == null ? null : obj.toString());
|
|
}
|
|
return arrayList;
|
|
}
|
|
|
|
private void a(AddressBookParsedResult addressBookParsedResult) {
|
|
String[] a = (this.g ? new VCardContactEncoder() : new MECARDContactEncoder()).a(a(addressBookParsedResult.j()), addressBookParsedResult.m(), a(addressBookParsedResult.d()), a(addressBookParsedResult.n()), (List<String>) null, a(addressBookParsedResult.g()), a(addressBookParsedResult.r()), (String) null);
|
|
if (a[1].isEmpty()) {
|
|
return;
|
|
}
|
|
this.b = a[0];
|
|
this.c = a[1];
|
|
this.d = this.a.getString(R$string.contents_contact);
|
|
}
|
|
|
|
private static List<String> a(String[] strArr) {
|
|
if (strArr == null) {
|
|
return null;
|
|
}
|
|
return Arrays.asList(strArr);
|
|
}
|
|
|
|
Bitmap a() throws WriterException {
|
|
EnumMap enumMap;
|
|
String str = this.b;
|
|
if (str == null) {
|
|
return null;
|
|
}
|
|
String a = a(str);
|
|
if (a != null) {
|
|
EnumMap enumMap2 = new EnumMap(EncodeHintType.class);
|
|
enumMap2.put((EnumMap) EncodeHintType.CHARACTER_SET, (EncodeHintType) a);
|
|
enumMap = enumMap2;
|
|
} else {
|
|
enumMap = null;
|
|
}
|
|
try {
|
|
BitMatrix a2 = new MultiFormatWriter().a(str, this.e, this.f, this.f, enumMap);
|
|
int k = a2.k();
|
|
int i = a2.i();
|
|
int[] iArr = new int[k * i];
|
|
for (int i2 = 0; i2 < i; i2++) {
|
|
int i3 = i2 * k;
|
|
for (int i4 = 0; i4 < k; i4++) {
|
|
iArr[i3 + i4] = a2.b(i4, i2) ? -16777216 : -1;
|
|
}
|
|
}
|
|
Bitmap createBitmap = Bitmap.createBitmap(k, i, Bitmap.Config.ARGB_8888);
|
|
createBitmap.setPixels(iArr, 0, k, 0, 0, k, i);
|
|
return createBitmap;
|
|
} catch (IllegalArgumentException unused) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static String a(CharSequence charSequence) {
|
|
for (int i = 0; i < charSequence.length(); i++) {
|
|
if (charSequence.charAt(i) > 255) {
|
|
return "UTF-8";
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|