package com.baidu.location.b; import android.app.ActivityManager; import android.content.Context; import android.location.Location; import android.os.Handler; import androidx.core.view.MotionEventCompat; import androidx.core.view.ViewCompat; import asmack.org.jivesoftware.smackx.packet.IBBExtensions; import com.baidu.location.Jni; import com.baidu.location.e.b; import com.baidu.location.e.e; import com.baidu.location.e.j; import com.baidu.location.e.k; import com.baidu.location.f; import com.netease.nimlib.sdk.avchat.constant.AVChatControlCommand; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.RandomAccessFile; import java.net.MalformedURLException; import java.net.URL; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.UUID; import javax.net.ssl.HttpsURLConnection; import org.json.JSONException; import org.json.JSONObject; public class d { /* renamed from: f */ public static String f1222f; private static d j; private a A; private boolean B; private boolean C; private int D; private float E; private float F; private long G; private int H; private Handler I; private byte[] J; private byte[] K; private int L; private List<Byte> M; private boolean N; /* renamed from: a */ long f1223a; /* renamed from: b */ Location f1224b; /* renamed from: c */ Location f1225c; /* renamed from: d */ StringBuilder f1226d; /* renamed from: e */ long f1227e; /* renamed from: g */ int f1228g; /* renamed from: h */ double f1229h; double i; private int k; private double l; private String m; private int n; private int o; private int p; private int q; private double r; private double s; private double t; private int u; private int v; private int w; private int x; private int y; private long z; public class a extends e { /* renamed from: a */ String f1230a = null; public a() { d.this = r1; this.k = new HashMap(); } @Override // com.baidu.location.e.e public void a() { this.f1450h = "http://loc.map.baidu.com/cc.php"; String encode = Jni.encode(this.f1230a); this.f1230a = null; this.k.put("q", encode); } public void a(String str) { this.f1230a = str; b(v.a().c()); } @Override // com.baidu.location.e.e public void a(boolean z) { String str; if (z && (str = this.j) != null) { try { JSONObject jSONObject = new JSONObject(str); jSONObject.put("prod", b.f1421e); jSONObject.put("uptime", System.currentTimeMillis()); d.this.e((d) jSONObject.toString()); } catch (Exception unused) { } } Map<String, Object> map = this.k; if (map != null) { map.clear(); } } } private d() { this.k = 1; this.l = 0.699999988079071d; this.m = "3G|4G"; this.n = 1; this.o = 307200; this.p = 15; this.q = 1; this.r = 3.5d; this.s = 3.0d; this.t = 0.5d; this.u = 300; this.v = 60; this.w = 0; this.x = 60; this.y = 0; this.z = 0; this.A = null; this.B = false; this.C = false; this.D = 0; this.E = 0.0f; this.F = 0.0f; this.G = 0; this.H = 500; this.f1223a = 0; this.f1224b = null; this.f1225c = null; this.f1226d = null; this.f1227e = 0; this.I = null; this.J = new byte[4]; this.K = null; this.L = 0; this.M = null; this.N = false; this.f1228g = 0; this.f1229h = 116.22345545d; this.i = 40.245667323d; this.I = new Handler(); } public static d a() { if (j == null) { j = new d(); } return j; } /* access modifiers changed from: public */ private String a(File file, String str) { String uuid = UUID.randomUUID().toString(); try { HttpsURLConnection httpsURLConnection = (HttpsURLConnection) new URL(str).openConnection(); httpsURLConnection.setReadTimeout(10000); httpsURLConnection.setConnectTimeout(10000); httpsURLConnection.setDoInput(true); httpsURLConnection.setDoOutput(true); httpsURLConnection.setUseCaches(false); httpsURLConnection.setRequestMethod("POST"); httpsURLConnection.setRequestProperty("Charset", "utf-8"); httpsURLConnection.setRequestProperty("connection", IBBExtensions.Close.ELEMENT_NAME); httpsURLConnection.setRequestProperty("Content-Type", "multipart/form-data" + ";boundary=" + uuid); if (file == null || !file.exists()) { return "0"; } OutputStream outputStream = httpsURLConnection.getOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("--"); stringBuffer.append(uuid); stringBuffer.append("\r\n"); stringBuffer.append("Content-Disposition: form-data; name=\"location_dat\"; filename=\"" + file.getName() + "\"" + "\r\n"); StringBuilder sb = new StringBuilder(); sb.append("Content-Type: application/octet-stream; charset=utf-8"); sb.append("\r\n"); stringBuffer.append(sb.toString()); stringBuffer.append("\r\n"); dataOutputStream.write(stringBuffer.toString().getBytes()); FileInputStream fileInputStream = new FileInputStream(file); byte[] bArr = new byte[1024]; while (true) { int read = fileInputStream.read(bArr); if (read == -1) { break; } dataOutputStream.write(bArr, 0, read); } fileInputStream.close(); dataOutputStream.write("\r\n".getBytes()); dataOutputStream.write(("--" + uuid + "--" + "\r\n").getBytes()); dataOutputStream.flush(); dataOutputStream.close(); int responseCode = httpsURLConnection.getResponseCode(); outputStream.close(); httpsURLConnection.disconnect(); this.y += 400; c(this.y); return responseCode == 200 ? "1" : "0"; } catch (IOException | MalformedURLException unused) { return "0"; } } private boolean a(String str, Context context) { boolean z2 = false; try { List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = ((ActivityManager) context.getSystemService("activity")).getRunningAppProcesses(); if (runningAppProcesses != null) { for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { if (runningAppProcessInfo.processName.equals(str)) { int i2 = runningAppProcessInfo.importance; if (i2 == 200 || i2 == 100) { z2 = true; } } } } } catch (Exception unused) { } return z2; } private byte[] a(int i2) { return new byte[]{(byte) (i2 & 255), (byte) ((65280 & i2) >> 8), (byte) ((16711680 & i2) >> 16), (byte) ((i2 & ViewCompat.MEASURED_STATE_MASK) >> 24)}; } private byte[] a(String str) { if (str == null) { return null; } byte[] bytes = str.getBytes(); byte nextInt = (byte) new Random().nextInt(255); byte nextInt2 = (byte) new Random().nextInt(255); byte[] bArr = new byte[(bytes.length + 2)]; int length = bytes.length; int i2 = 0; int i3 = 0; while (i2 < length) { bArr[i3] = (byte) (bytes[i2] ^ nextInt); i2++; i3++; } bArr[i3] = nextInt; bArr[i3 + 1] = nextInt2; return bArr; } private String b(String str) { Calendar instance = Calendar.getInstance(); return String.format(str, Integer.valueOf(instance.get(1)), Integer.valueOf(instance.get(2) + 1), Integer.valueOf(instance.get(5))); } private void b(int i2) { byte[] a2 = a(i2); for (int i3 = 0; i3 < 4; i3++) { this.M.add(Byte.valueOf(a2[i3])); } } /* access modifiers changed from: public */ private void b(Location location) { c(location); h(); } private void c() { if (!this.N) { this.N = true; d(b.f1421e); j(); d(); } } private void c(int i2) { if (i2 != 0) { try { File file = new File(j.f1459a + "/grtcf.dat"); if (!file.exists()) { File file2 = new File(j.f1459a); if (!file2.exists()) { file2.mkdirs(); } if (file.createNewFile()) { RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); randomAccessFile.seek(2); randomAccessFile.writeInt(0); randomAccessFile.seek(8); byte[] bytes = "1980_01_01:0".getBytes(); randomAccessFile.writeInt(bytes.length); randomAccessFile.write(bytes); randomAccessFile.seek(200); randomAccessFile.writeBoolean(false); randomAccessFile.seek(800); randomAccessFile.writeBoolean(false); randomAccessFile.close(); } else { return; } } RandomAccessFile randomAccessFile2 = new RandomAccessFile(file, "rw"); randomAccessFile2.seek(8); byte[] bytes2 = (b("%d_%02d_%02d") + ":" + i2).getBytes(); randomAccessFile2.writeInt(bytes2.length); randomAccessFile2.write(bytes2); randomAccessFile2.close(); } catch (Exception unused) { } } } private void c(Location location) { if (System.currentTimeMillis() - this.f1223a >= ((long) this.H) && location != null) { if (location != null && location.hasSpeed() && location.getSpeed() > this.E) { this.E = location.getSpeed(); } try { if (this.M == null) { this.M = new ArrayList(); i(); d(location); } else { e(location); } } catch (Exception unused) { } this.L++; } } private void c(String str) { if (str != null) { try { JSONObject jSONObject = new JSONObject(str); if (jSONObject.has("on")) { this.k = jSONObject.getInt("on"); } if (jSONObject.has("bash")) { this.l = jSONObject.getDouble("bash"); } if (jSONObject.has("net")) { this.m = jSONObject.getString("net"); } if (jSONObject.has("tcon")) { this.n = jSONObject.getInt("tcon"); } if (jSONObject.has("tcsh")) { this.o = jSONObject.getInt("tcsh"); } if (jSONObject.has("per")) { this.p = jSONObject.getInt("per"); } if (jSONObject.has("chdron")) { this.q = jSONObject.getInt("chdron"); } if (jSONObject.has("spsh")) { this.r = jSONObject.getDouble("spsh"); } if (jSONObject.has("acsh")) { this.s = jSONObject.getDouble("acsh"); } if (jSONObject.has("stspsh")) { this.t = jSONObject.getDouble("stspsh"); } if (jSONObject.has("drstsh")) { this.u = jSONObject.getInt("drstsh"); } if (jSONObject.has("stper")) { this.v = jSONObject.getInt("stper"); } if (jSONObject.has("nondron")) { this.w = jSONObject.getInt("nondron"); } if (jSONObject.has("nondrper")) { this.x = jSONObject.getInt("nondrper"); } if (jSONObject.has("uptime")) { this.z = jSONObject.getLong("uptime"); } k(); } catch (JSONException unused) { } } } private void d() { String[] split = "8.2.1".split("\\."); int length = split.length; byte[] bArr = this.J; bArr[0] = 0; bArr[1] = 0; bArr[2] = 0; bArr[3] = 0; if (length >= 4) { length = 4; } for (int i2 = 0; i2 < length; i2++) { try { this.J[i2] = (byte) (Integer.valueOf(split[i2]).intValue() & 255); } catch (Exception unused) { } } this.K = a(b.f1421e + ":" + b.a().f1427c); } private void d(Location location) { this.f1227e = System.currentTimeMillis(); b((int) (location.getTime() / 1000)); b((int) (location.getLongitude() * 1000000.0d)); b((int) (location.getLatitude() * 1000000.0d)); int i2 = !location.hasBearing(); int i3 = !location.hasSpeed(); this.M.add(Byte.valueOf(i2 > 0 ? 32 : (byte) (((byte) (((int) (location.getBearing() / 15.0f)) & 255)) & -33))); this.M.add(Byte.valueOf(i3 > 0 ? Byte.MIN_VALUE : (byte) (((byte) (((int) ((((double) location.getSpeed()) * 3.6d) / 4.0d)) & 255)) & Byte.MAX_VALUE))); this.f1224b = location; } private void d(String str) { try { File file = new File(j.f1459a + "/grtcf.dat"); if (file.exists()) { RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); randomAccessFile.seek(2); int readInt = randomAccessFile.readInt(); randomAccessFile.seek(8); int readInt2 = randomAccessFile.readInt(); byte[] bArr = new byte[readInt2]; randomAccessFile.read(bArr, 0, readInt2); String str2 = new String(bArr); int i2 = 1; if (str2.contains(b("%d_%02d_%02d")) && str2.contains(":")) { try { String[] split = str2.split(":"); if (split.length > 1) { this.y = Integer.valueOf(split[1]).intValue(); } } catch (Exception unused) { } } while (true) { if (i2 > readInt) { break; } randomAccessFile.seek((long) (i2 * 2048)); int readInt3 = randomAccessFile.readInt(); byte[] bArr2 = new byte[readInt3]; randomAccessFile.read(bArr2, 0, readInt3); String str3 = new String(bArr2); if (str != null && str3.contains(str)) { c(str3); break; } i2++; } randomAccessFile.close(); } } catch (Exception unused2) { } } /* JADX WARNING: Code restructure failed: missing block: B:30:0x0106, code lost: if (r8 > 0) goto L_0x011e; */ /* JADX WARNING: Code restructure failed: missing block: B:34:0x011c, code lost: if (r8 > 0) goto L_0x011e; */ private void e(Location location) { byte b2; if (location != null) { int longitude = (int) ((location.getLongitude() - this.f1224b.getLongitude()) * 1000000.0d); int latitude = (int) ((location.getLatitude() - this.f1224b.getLatitude()) * 1000000.0d); int i2 = !location.hasBearing(); int i3 = !location.hasSpeed(); char c2 = longitude > 0 ? (char) 0 : 1; int abs = Math.abs(longitude); char c3 = latitude > 0 ? (char) 0 : 1; int abs2 = Math.abs(latitude); if (this.L > 1) { this.f1225c = null; this.f1225c = this.f1224b; } this.f1224b = location; Location location2 = this.f1224b; if (location2 != null && this.f1225c != null && location2.getTime() > this.f1225c.getTime() && this.f1224b.getTime() - this.f1225c.getTime() < 5000) { long time = this.f1224b.getTime() - this.f1225c.getTime(); float[] fArr = new float[2]; Location.distanceBetween(this.f1224b.getAltitude(), this.f1224b.getLongitude(), this.f1225c.getLatitude(), this.f1225c.getLongitude(), fArr); double speed = (double) (((fArr[0] - (this.f1225c.getSpeed() * ((float) time))) * 2.0f) / ((float) (time * time))); if (speed > ((double) this.F)) { this.F = (float) speed; } } this.M.add(Byte.valueOf((byte) (abs & 255))); this.M.add(Byte.valueOf((byte) ((abs & MotionEventCompat.ACTION_POINTER_INDEX_MASK) >> 8))); this.M.add(Byte.valueOf((byte) (abs2 & 255))); this.M.add(Byte.valueOf((byte) ((abs2 & MotionEventCompat.ACTION_POINTER_INDEX_MASK) >> 8))); if (i2 > 0) { b2 = 32; if (c3 > 0) { b2 = (byte) 96; } } else { b2 = (byte) (((byte) (((int) (location.getBearing() / 15.0f)) & 255)) & 31); if (c3 > 0) { b2 = (byte) (b2 | AVChatControlCommand.NOTIFY_CUSTOM_BASE); } } b2 = (byte) (b2 | Byte.MIN_VALUE); this.M.add(Byte.valueOf(b2)); if (i3 > 0) { this.M.add(Byte.MIN_VALUE); } else { this.M.add(Byte.valueOf((byte) (((byte) (((int) ((((double) location.getSpeed()) * 3.6d) / 4.0d)) & 255)) & Byte.MAX_VALUE))); } } } private void e(String str) { try { File file = new File(j.f1459a + "/grtcf.dat"); if (!file.exists()) { File file2 = new File(j.f1459a); if (!file2.exists()) { file2.mkdirs(); } if (file.createNewFile()) { RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); randomAccessFile.seek(2); randomAccessFile.writeInt(0); randomAccessFile.seek(8); byte[] bytes = "1980_01_01:0".getBytes(); randomAccessFile.writeInt(bytes.length); randomAccessFile.write(bytes); randomAccessFile.seek(200); randomAccessFile.writeBoolean(false); randomAccessFile.seek(800); randomAccessFile.writeBoolean(false); randomAccessFile.close(); } else { return; } } RandomAccessFile randomAccessFile2 = new RandomAccessFile(file, "rw"); randomAccessFile2.seek(2); int readInt = randomAccessFile2.readInt(); int i2 = 1; while (i2 <= readInt) { randomAccessFile2.seek((long) (i2 * 2048)); int readInt2 = randomAccessFile2.readInt(); byte[] bArr = new byte[readInt2]; randomAccessFile2.read(bArr, 0, readInt2); if (new String(bArr).contains(b.f1421e)) { break; } i2++; } if (i2 >= readInt) { randomAccessFile2.seek(2); randomAccessFile2.writeInt(i2); } randomAccessFile2.seek((long) (i2 * 2048)); byte[] bytes2 = str.getBytes(); randomAccessFile2.writeInt(bytes2.length); randomAccessFile2.write(bytes2); randomAccessFile2.close(); } catch (Exception unused) { } } /* JADX WARNING: Removed duplicated region for block: B:27:0x0056 A[SYNTHETIC, Splitter:B:27:0x0056] */ /* JADX WARNING: Removed duplicated region for block: B:30:0x005b A[Catch:{ Exception -> 0x005e }] */ /* JADX WARNING: Removed duplicated region for block: B:35:0x0061 */ private boolean e() { RandomAccessFile randomAccessFile; Throwable th; RandomAccessFile randomAccessFile2 = null; FileLock fileLock = null; r0 = null; FileChannel fileChannel = null; boolean z2 = false; try { File file = new File(k.i() + File.separator + "gflk.dat"); if (!file.exists()) { file.createNewFile(); } randomAccessFile = new RandomAccessFile(file, "rw"); try { FileChannel channel = randomAccessFile.getChannel(); try { fileLock = channel.tryLock(); } catch (Exception unused) { z2 = true; } catch (Throwable th2) { th = th2; fileChannel = channel; if (fileChannel != null) { try { fileChannel.close(); } catch (Exception unused2) { throw th; } } if (randomAccessFile != null) { randomAccessFile.close(); } throw th; } if (fileLock != null) { try { fileLock.release(); } catch (Exception unused3) { } } if (channel != null) { channel.close(); } if (randomAccessFile != null) { randomAccessFile.close(); } } catch (Exception unused4) { randomAccessFile2 = randomAccessFile; if (randomAccessFile2 != null) { randomAccessFile2.close(); } return z2; } catch (Throwable th3) { th = th3; if (fileChannel != null) { } if (randomAccessFile != null) { } throw th; } } catch (Exception unused5) { if (randomAccessFile2 != null) { } return z2; } catch (Throwable th4) { th = th4; randomAccessFile = null; if (fileChannel != null) { } if (randomAccessFile != null) { } throw th; } return z2; } private boolean f() { if (this.B) { if (this.C) { if (((double) this.E) < this.t) { this.D += this.p; if (this.D <= this.u || System.currentTimeMillis() - this.G > ((long) (this.v * 1000))) { return true; } } else { this.D = 0; this.C = false; return true; } } else if (((double) this.E) >= this.t) { return true; } else { this.C = true; this.D = 0; this.D += this.p; return true; } } else if (((double) this.E) < this.r && ((double) this.F) < this.s) { return this.w == 1 && System.currentTimeMillis() - this.G > ((long) (this.x * 1000)); } else { this.B = true; return true; } } private void g() { this.M = null; this.f1227e = 0; this.L = 0; this.f1224b = null; this.f1225c = null; this.E = 0.0f; this.F = 0.0f; } private void h() { if (this.f1227e != 0 && System.currentTimeMillis() - this.f1227e >= ((long) (this.p * 1000))) { if (f.getServiceContext().getSharedPreferences("loc_navi_mode", 4).getBoolean("is_navi_on", false)) { g(); } else if (this.n != 1 || f()) { if (!b.f1421e.equals("com.ubercab.driver")) { if (!a(b.f1421e, f.getServiceContext())) { g(); return; } } else if (e()) { g(); return; } List<Byte> list = this.M; if (list != null) { int size = list.size(); this.M.set(0, Byte.valueOf((byte) (size & 255))); this.M.set(1, Byte.valueOf((byte) ((65280 & size) >> 8))); this.M.set(3, Byte.valueOf((byte) (this.L & 255))); byte[] bArr = new byte[size]; for (int i2 = 0; i2 < size; i2++) { bArr[i2] = this.M.get(i2).byteValue(); } File file = new File(k.k(), "baidu/tempdata"); if (!file.exists()) { file.mkdirs(); } if (file.exists()) { File file2 = new File(file, "intime.dat"); if (file2.exists()) { file2.delete(); } try { BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file2)); bufferedOutputStream.write(bArr); bufferedOutputStream.flush(); bufferedOutputStream.close(); new f(this).start(); } catch (Exception unused) { } } g(); this.G = System.currentTimeMillis(); } } else { g(); } } } private void i() { byte b2; List<Byte> list; this.M.add((byte) 0); this.M.add((byte) 0); if (f1222f.equals("0")) { list = this.M; b2 = -82; } else { list = this.M; b2 = -66; } list.add(Byte.valueOf(b2)); this.M.add((byte) 0); this.M.add(Byte.valueOf(this.J[0])); this.M.add(Byte.valueOf(this.J[1])); this.M.add(Byte.valueOf(this.J[2])); this.M.add(Byte.valueOf(this.J[3])); int length = this.K.length; this.M.add(Byte.valueOf((byte) ((length + 1) & 255))); for (int i2 = 0; i2 < length; i2++) { this.M.add(Byte.valueOf(this.K[i2])); } } private void j() { if (System.currentTimeMillis() - this.z > 86400000) { if (this.A == null) { this.A = new a(); } StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(b.a().a(false)); stringBuffer.append(a.a().c()); this.A.a(stringBuffer.toString()); } k(); } private void k() { } public void a(Location location) { if (!this.N) { c(); } if (this.k == 1 && this.m.contains(com.baidu.location.c.e.a(com.baidu.location.c.b.a().e()))) { if (this.n != 1 || this.y <= this.o) { this.I.post(new e(this, location)); } } } public void b() { if (this.N) { this.N = false; g(); } } }