package com.baidu.location.indoor.mapversion.c; import android.content.Context; import android.location.Location; import com.baidu.location.BDLocation; import com.baidu.mapsdkplatform.comapi.location.CoordinateType; import com.king.zxing.util.LogUtils; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Serializable; import java.lang.reflect.Array; import java.math.BigInteger; import java.net.HttpURLConnection; import java.net.URL; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.security.MessageDigest; import java.util.ArrayList; import java.util.HashMap; public class a { private static a a; private c b; private String c; private boolean d = false; private String e = "rn"; private b f; private String g = CoordinateType.GCJ02; private HashMap<String, d> h = new HashMap<>(); private HashMap<String, double[][]> i = new HashMap<>(); private d j = null; /* renamed from: com.baidu.location.indoor.mapversion.c.a$a reason: collision with other inner class name */ public static class C0010a { public double a; public double b; public double c; public double d; public double e; public double f; public double g; public double h; public C0010a(String str) { a(str); } public void a(String str) { String[] split = str.trim().split("\\|"); this.a = Double.valueOf(split[0]).doubleValue(); this.b = Double.valueOf(split[1]).doubleValue(); this.c = Double.valueOf(split[2]).doubleValue(); this.d = Double.valueOf(split[3]).doubleValue(); this.e = Double.valueOf(split[4]).doubleValue(); this.f = Double.valueOf(split[5]).doubleValue(); this.g = Double.valueOf(split[6]).doubleValue(); this.h = Double.valueOf(split[7]).doubleValue(); } } /* access modifiers changed from: private */ public class b extends Thread { private String b; private String c; public b(String str, String str2) { this.b = str; this.c = str2; } /* JADX WARNING: Removed duplicated region for block: B:30:0x011d A[Catch:{ Exception -> 0x0127 }] */ public void run() { boolean z; try { File file = new File(a.this.e); if (!file.exists() || !file.isDirectory()) { file.mkdirs(); } HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://loc.map.baidu.com/cfgs/indoorloc/indoorroadnet").openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.addRequestProperty("Accept-encoding", "gzip"); OutputStream outputStream = httpURLConnection.getOutputStream(); StringBuilder sb = new StringBuilder(); sb.append("bldg="); sb.append(this.b); sb.append("&md5="); sb.append(this.c == null ? "null" : this.c); outputStream.write(sb.toString().getBytes()); int responseCode = httpURLConnection.getResponseCode(); String str = null; if (responseCode == 200) { String headerField = httpURLConnection.getHeaderField("Hash"); InputStream inputStream = httpURLConnection.getInputStream(); File file2 = new File(file, a.this.a((a) this.b, headerField)); FileOutputStream fileOutputStream = new FileOutputStream(file2); byte[] bArr = new byte[1024]; while (true) { int read = inputStream.read(bArr); if (read < 0) { break; } fileOutputStream.write(bArr, 0, read); } fileOutputStream.flush(); fileOutputStream.close(); if (a.a(file2).equalsIgnoreCase(headerField)) { a.this.b((a) this.b, this.c); a.this.c = this.b; z = a.this.d(); str = "OK"; if (a.this.b != null) { a.this.b.a(z, str); } a.this.d = false; } file2.delete(); str = "Download failed"; } else if (responseCode == 304) { a.this.c = this.b; z = a.this.d(); str = "No roadnet update for " + this.b + "," + this.c; if (a.this.b != null) { } a.this.d = false; } else if (responseCode == 204) { str = "Not found bldg " + this.b; } z = false; if (a.this.b != null) { } } catch (Exception e) { e.printStackTrace(); } a.this.d = false; } } public interface c { void a(boolean z, String str); } public static class d implements Serializable { public String a; public String b; public C0010a c; public C0010a d; public C0010a e; public C0010a f = this.d; public short[][] g; private String h = CoordinateType.GCJ02; public d(String str) { this.a = str; } public double a(double d2) { return (d2 + this.f.d) * this.f.c; } public C0010a a() { return this.f; } public void a(String str) { C0010a aVar; if (str != null) { String lowerCase = str.toLowerCase(); this.h = lowerCase; if (lowerCase.startsWith(CoordinateType.WGS84)) { aVar = this.c; } else if (this.h.startsWith(BDLocation.BDLOCATION_GCJ02_TO_BD09)) { aVar = this.e; } else if (this.h.startsWith(CoordinateType.GCJ02)) { aVar = this.d; } else { return; } this.f = aVar; } } public double b(double d2) { return (d2 + this.f.f) * this.f.e; } public void b(String str) { String[] split = str.split("\\t"); this.b = split[1]; this.c = new C0010a(split[2]); this.e = new C0010a(split[3]); C0010a aVar = new C0010a(split[4]); this.d = aVar; this.f = aVar; int i = (int) aVar.g; int[] iArr = new int[2]; iArr[1] = (int) this.f.h; iArr[0] = i; this.g = (short[][]) Array.newInstance(short.class, iArr); for (int i2 = 0; ((double) i2) < this.f.g; i2++) { for (int i3 = 0; ((double) i3) < this.f.h; i3++) { this.g[i2][i3] = (short) (split[5].charAt((((int) this.f.h) * i2) + i3) - '0'); } } } public double c(double d2) { return (d2 / this.f.c) - this.f.d; } public double d(double d2) { return (d2 / this.f.e) - this.f.f; } } private a(Context context) { this.e = new File(context.getCacheDir(), this.e).getAbsolutePath(); } public static a a() { return a; } public static a a(Context context) { if (a == null) { a = new a(context); } return a; } public static String a(File file) { try { FileInputStream fileInputStream = new FileInputStream(file); MappedByteBuffer map = fileInputStream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length()); MessageDigest instance = MessageDigest.getInstance("MD5"); instance.update(map); String bigInteger = new BigInteger(1, instance.digest()).toString(16); fileInputStream.close(); for (int length = 32 - bigInteger.length(); length > 0; length--) { bigInteger = "0" + bigInteger; } return bigInteger; } catch (Exception e2) { e2.printStackTrace(); return null; } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private String a(String str, String str2) { return d(str) + "_" + str2; } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void b(String str, String str2) { try { File file = new File(this.e + "/" + a(str, str2)); if (file.exists()) { file.delete(); } } catch (Exception e2) { e2.printStackTrace(); } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private String d(String str) { return str; } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private boolean d() { String str = this.c; if (str == null) { return false; } File f2 = f(str); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); if (!d.a(f2, byteArrayOutputStream)) { return false; } this.h.clear(); this.i.clear(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))); while (true) { try { String readLine = bufferedReader.readLine(); if (readLine == null) { break; } else if (readLine.split("\\t")[1].split("_")[0].equals("geo")) { j(readLine); } else { d dVar = new d(this.c); dVar.b(readLine); dVar.a(this.g); this.h.put(dVar.b.toLowerCase(), dVar); } } catch (IOException e2) { e2.printStackTrace(); } } bufferedReader.close(); return true; } private String e(String str) { File file = new File(this.e); if (file.exists() && file.isDirectory()) { File[] listFiles = file.listFiles(new b(this, str)); int i2 = 0; if (listFiles == null || listFiles.length != 1) { while (listFiles != null && i2 < listFiles.length) { listFiles[i2].delete(); i2++; } } else { String[] split = listFiles[0].getName().split("_"); if (split.length < 2) { return null; } return split[1]; } } return null; } private File f(String str) { String e2 = e(str); return new File(this.e + "/" + a(str, e2)); } private boolean g(String str) { File f2 = f(str); return f2.exists() && f2.length() > 0; } private boolean h(String str) { return System.currentTimeMillis() - f(str).lastModified() > 1296000000; } private ArrayList<Double> i(String str) { double d2; ArrayList<Double> arrayList = new ArrayList<>(); int i2 = 0; while (i2 < str.length()) { if (str.charAt(i2) == ',') { int i3 = i2 + 1; i2 += 2; d2 = (double) Integer.valueOf(str.substring(i3, i2)).intValue(); } else if (str.charAt(i2) == '.') { int i4 = i2 + 1; i2 += 4; d2 = Double.valueOf(str.substring(i4, i2)).doubleValue(); } else { int i5 = i2 + 2; double intValue = (double) Integer.valueOf(str.substring(i2, i5)).intValue(); i2 = i5; d2 = intValue; } arrayList.add(Double.valueOf(d2)); } return arrayList; } private void j(String str) { String[] split = str.split("\\t"); String lowerCase = split[1].split("_")[1].toLowerCase(); try { if (this.h.containsKey(lowerCase)) { ArrayList<Double> i2 = i(split[5]); int length = this.h.get(lowerCase).g.length; int length2 = this.h.get(lowerCase).g[0].length; int[] iArr = new int[2]; iArr[1] = length2; iArr[0] = length; double[][] dArr = (double[][]) Array.newInstance(double.class, iArr); int i3 = 0; for (int i4 = 0; i4 < length; i4++) { for (int i5 = 0; i5 < length2; i5++) { if (this.h.get(lowerCase).g[i4][i5] <= 0 || this.h.get(lowerCase).g[i4][i5] == 9) { dArr[i4][i5] = 0.0d; } else { dArr[i4][i5] = i2.get(i3).doubleValue(); i3++; } } } this.i.put(lowerCase.toLowerCase(), dArr); } } catch (Exception e2) { e2.printStackTrace(); } } private void k(String str) { if (!this.d) { this.d = true; b bVar = new b(str, e(str)); this.f = bVar; bVar.start(); } } public void a(double d2, double d3) { if (this.j == null) { float[] fArr = new float[2]; float[] fArr2 = new float[2]; Location.distanceBetween(d3, d2, d3, d2 + 0.01d, fArr); double d4 = (double) fArr[0]; Double.isNaN(d4); double d5 = d4 / 0.01d; Location.distanceBetween(d3, d2, d3 + 0.01d, d2, fArr2); double d6 = (double) fArr2[0]; Double.isNaN(d6); d dVar = new d("outdoor"); this.j = dVar; dVar.b = "out"; d dVar2 = this.j; dVar2.f = new C0010a("0|1.0|" + d5 + LogUtils.VERTICAL + (-d2) + LogUtils.VERTICAL + (d6 / 0.01d) + LogUtils.VERTICAL + (-d3) + "|0|0"); } } public void a(String str) { this.g = str; } public void a(String str, c cVar) { String str2 = this.c; if (str2 == null || !str.equals(str2)) { this.b = cVar; if (!g(str) || h(str)) { k(str); return; } this.c = str; d(); c cVar2 = this.b; if (cVar2 != null) { cVar2.a(true, "OK"); } } } public d b(String str) { return this.h.get(str.toLowerCase()); } public void b() { this.h.clear(); this.i.clear(); this.c = null; this.d = false; } public d c() { return this.j; } public double[][] c(String str) { return this.i.get(str.toLowerCase()); } }