package com.baidu.location; import android.content.ContentValues; import android.content.Context; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import android.os.Message; import android.telephony.TelephonyManager; import android.text.TextUtils; import com.baidu.location.GeofenceClient; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Locale; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.JSONObject; class ay implements a1, m { private static final int i0 = 2; private static final int i1 = 1; private static final String i3 = "geofence_ids"; private static final String iP = "GeofenceMan"; private static final int iQ = 3; public static final int iR = 10; private static final String iS = "http://loc.map.baidu.com/fence"; private static final String iT = "geofence_id"; private static final String iU = ";"; private static final String iW = "status_code"; private static ay iX = null; private static final int iZ = 5; private static final String o = "GeofenceMan"; private a i2; private Context iO; private Object iV = new Object(); private HandlerThread iY; /* access modifiers changed from: private */ public class a extends Handler { /* renamed from: do reason: not valid java name */ public static final int f103do = 2; /* renamed from: for reason: not valid java name */ public static final int f104for = 3; /* renamed from: if reason: not valid java name */ public static final int f105if = 0; public a(Looper looper) { super(looper); } public void handleMessage(Message message) { int i; String[] strArr; int i2; String str; super.handleMessage(message); int i3 = message.what; Bundle data = message.getData(); switch (i3) { case 0: if (data != null) { i2 = data.getInt(ay.iW, 1); str = data.getString("geofence_id"); } else { i2 = 1; str = null; } ay.this.m248if((ay) i2, (int) str, (String) ((GeofenceClient.OnAddBDGeofencesResultListener) message.obj)); return; case 1: default: return; case 2: if (data != null) { i = data.getInt(ay.iW, 1); strArr = data.getStringArray(ay.i3); } else { i = 1; strArr = null; } ay.this.m249if((ay) i, (int) strArr, (String[]) ((GeofenceClient.OnRemoveBDGeofencesResultListener) message.obj)); return; case 3: ay.this.cH(); return; } } } class b extends s { private static final String dQ = "error"; private static final int dR = -3; private static final String dS = "ext"; private static final String dT = "cl"; private static final String dU = "fence"; private static final String dV = "lac"; private static final String dX = "wf"; private static final String dZ = "radius"; private final au d0; private GeofenceClient.OnAddBDGeofencesResultListener dP; private int dY; public b(au auVar, GeofenceClient.OnAddBDGeofencesResultListener onAddBDGeofencesResultListener) { this.d0 = auVar; this.dP = onAddBDGeofencesResultListener; this.c0 = new ArrayList(); } @Override // com.baidu.location.s public void X() { this.cY = "http://loc.map.baidu.com/fence"; DecimalFormat decimalFormat = new DecimalFormat("0.00000"); this.c0.add(new BasicNameValuePair(dU, Jni.k(String.format("&x=%s&y=%s&r=%s&coord=%s&type=%s&cu=%s&sdk=%s", decimalFormat.format(this.d0.a()), decimalFormat.format(this.d0.m205case()), String.valueOf(this.d0.m215new()), String.valueOf(this.d0.m214int()), Integer.valueOf(ay.m239do(ay.this.iO)), com.baidu.location.b.a.a.m262if(ay.this.iO), Float.valueOf(5.02f))))); this.c0.add(new BasicNameValuePair(dS, Jni.k(String.format(Locale.CHINA, "&ki=%s&sn=%s", v.a(ay.this.iO), v.m388if(ay.this.iO))))); } public void al() { R(); } @Override // com.baidu.location.s /* renamed from: do reason: not valid java name */ public void m260do(boolean z) { if (!z || this.cZ == null) { ay.this.m250if((ay) this.dP, (GeofenceClient.OnAddBDGeofencesResultListener) 1, (int) this.d0.getGeofenceId()); return; } String str = null; try { JSONObject jSONObject = new JSONObject(EntityUtils.toString(this.cZ, "UTF-8")); if (jSONObject != null) { StringBuilder sb = new StringBuilder(); if (jSONObject.has(dV)) { String string = jSONObject.getString(dV); if (!TextUtils.isEmpty(string)) { sb.append(string); this.d0.m208do(true); } } if (jSONObject.has(dT)) { String string2 = jSONObject.getString(dT); if (!TextUtils.isEmpty(string2)) { sb.append(string2); this.d0.a(true); } } if (jSONObject.has(dX)) { String string3 = jSONObject.getString(dX); if (!TextUtils.isEmpty(string3)) { sb.append(string3); this.d0.m212if(true); } } str = sb.toString(); if (jSONObject.has("radius")) { this.d0.a(Float.valueOf(jSONObject.getString("radius")).floatValue()); } if (jSONObject.has(dQ)) { this.dY = Integer.valueOf(jSONObject.getString(dQ)).intValue(); } } if (!TextUtils.isEmpty(str)) { ay.this.i2.post(new c(this.d0, str, this.dP)); } else if (this.dY == dR) { ay.this.m250if((ay) this.dP, (GeofenceClient.OnAddBDGeofencesResultListener) BDLocationStatusCodes.GEOFENCE_SERVICE_NO_ALIVIABLE, (int) this.d0.getGeofenceId()); } else { ay.this.m250if((ay) this.dP, (GeofenceClient.OnAddBDGeofencesResultListener) this.dY, (int) this.d0.getGeofenceId()); } } catch (Exception e) { ay.this.m250if((ay) this.dP, (GeofenceClient.OnAddBDGeofencesResultListener) 1, (int) this.d0.getGeofenceId()); } } } private class c implements Runnable { /* renamed from: do reason: not valid java name */ private final au f106do; /* renamed from: for reason: not valid java name */ private final GeofenceClient.OnAddBDGeofencesResultListener f107for; /* renamed from: if reason: not valid java name */ private final String f108if; public c(au auVar, String str, GeofenceClient.OnAddBDGeofencesResultListener onAddBDGeofencesResultListener) { this.f106do = auVar; this.f108if = str; this.f107for = onAddBDGeofencesResultListener; } public void run() { ay.this.m250if((ay) this.f107for, (GeofenceClient.OnAddBDGeofencesResultListener) ay.this.m244if((ay) this.f106do, (au) this.f108if), (int) this.f106do.getGeofenceId()); if (f.getServiceContext() != null) { a4.dc().m101for(this.f106do); } } } private class d implements Runnable { /* renamed from: do reason: not valid java name */ private final GeofenceClient.OnRemoveBDGeofencesResultListener f109do; /* renamed from: if reason: not valid java name */ private final List f110if; public d(List list, GeofenceClient.OnRemoveBDGeofencesResultListener onRemoveBDGeofencesResultListener) { this.f110if = list; this.f109do = onRemoveBDGeofencesResultListener; } public void run() { int i = ay.this.m240do((ay) this.f110if); Message obtain = Message.obtain(ay.this.i2); obtain.what = 2; obtain.obj = this.f109do; Bundle bundle = new Bundle(); bundle.putInt(ay.iW, i); bundle.putStringArray(ay.i3, (String[]) this.f110if.toArray(new String[this.f110if.size()])); obtain.setData(bundle); ay.this.i2.sendMessage(obtain); } } ay() { } private void cI() { this.iY = new HandlerThread("GeofenceMan", 10); this.iY.start(); this.i2 = new a(this.iY.getLooper()); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private synchronized void cJ() { SQLiteDatabase writableDatabase = l.a(this.iO).getWritableDatabase(); if (writableDatabase != null) { writableDatabase.beginTransaction(); try { long currentTimeMillis = System.currentTimeMillis(); writableDatabase.execSQL(String.format("DELETE FROM %s WHERE EXISTS (SELECT * FROM %s WHERE (%s + %s) < %d)", com.baidu.location.a.b.a, com.baidu.location.a.a.f32goto, com.baidu.location.a.a.f33if, com.baidu.location.a.a.b, Long.valueOf(currentTimeMillis))); writableDatabase.execSQL(String.format("DELETE FROM %s WHERE (%s + %s) < %d", com.baidu.location.a.a.f32goto, com.baidu.location.a.a.f33if, com.baidu.location.a.a.b, Long.valueOf(currentTimeMillis))); writableDatabase.setTransactionSuccessful(); } catch (Exception e) { } finally { writableDatabase.endTransaction(); writableDatabase.close(); } } } private void cK() { this.i2.sendEmptyMessage(3); } private final void cL() { if (!s.m359if(this.iO)) { throw new IllegalStateException("Not net connection"); } } /* JADX WARNING: Exception block dominator not found, dom blocks: [] */ private synchronized long cM() { long j; j = 0; SQLiteDatabase readableDatabase = l.a(this.iO).getReadableDatabase(); if (readableDatabase != null) { j = DatabaseUtils.queryNumEntries(readableDatabase, com.baidu.location.a.a.f32goto); readableDatabase.close(); } return j; } /* renamed from: do reason: not valid java name */ public static int m239do(Context context) { String subscriberId = ((TelephonyManager) context.getSystemService("phone")).getSubscriberId(); if (subscriberId != null) { if (subscriberId.startsWith("46000") || subscriberId.startsWith("46002") || subscriberId.startsWith("46007")) { return 1; } if (subscriberId.startsWith("46001")) { return 2; } if (subscriberId.startsWith("46003")) { return 3; } } return 5; } /* access modifiers changed from: private */ /* access modifiers changed from: public */ /* renamed from: do reason: not valid java name */ private synchronized int m240do(List list) { int i; SQLiteDatabase writableDatabase = l.a(this.iO).getWritableDatabase(); if (writableDatabase != null) { writableDatabase.beginTransaction(); try { Iterator it = list.iterator(); while (it.hasNext()) { String[] strArr = {(String) it.next()}; writableDatabase.delete(com.baidu.location.a.a.f32goto, String.format("%s=?", "geofence_id"), strArr); writableDatabase.delete(com.baidu.location.a.b.a, String.format("%s=?", "geofence_id"), strArr); } writableDatabase.setTransactionSuccessful(); writableDatabase.endTransaction(); i = 0; } catch (Exception e) { writableDatabase.endTransaction(); i = 1; } catch (Throwable th) { writableDatabase.endTransaction(); throw th; } writableDatabase.close(); } else { i = 0; } return i; } /* renamed from: for reason: not valid java name */ public static ay m242for(Context context) { if (iX == null) { iX = new ay(); iX.cI(); iX.iO = context; } return iX; } /* JADX INFO: finally extract failed */ /* access modifiers changed from: private */ /* access modifiers changed from: public */ /* renamed from: if reason: not valid java name */ private synchronized int m244if(au auVar, String str) { int i = 0; synchronized (this) { SQLiteDatabase writableDatabase = l.a(this.iO).getWritableDatabase(); if (writableDatabase != null) { writableDatabase.beginTransaction(); long currentTimeMillis = System.currentTimeMillis(); try { ContentValues contentValues = new ContentValues(); String geofenceId = auVar.getGeofenceId(); contentValues.put("geofence_id", geofenceId); contentValues.put(com.baidu.location.a.a.f28char, Double.valueOf(auVar.a())); contentValues.put(com.baidu.location.a.a.f34int, Double.valueOf(auVar.m205case())); contentValues.put(com.baidu.location.a.a.f30else, Float.valueOf(auVar.m207do())); contentValues.put(com.baidu.location.a.a.f26byte, Integer.valueOf(auVar.m215new())); contentValues.put(com.baidu.location.a.a.f33if, Long.valueOf(currentTimeMillis)); contentValues.put(com.baidu.location.a.a.b, Long.valueOf(auVar.m211goto())); contentValues.put(com.baidu.location.a.a.f36new, auVar.m214int()); contentValues.put(com.baidu.location.a.a.f27case, Integer.valueOf(auVar.m204byte() ? 1 : 0)); contentValues.put(com.baidu.location.a.a.f38void, Integer.valueOf(auVar.m213if() ? 1 : 0)); contentValues.put(com.baidu.location.a.a.f35long, Integer.valueOf(auVar.m210for() ? 1 : 0)); contentValues.put(com.baidu.location.a.a.f37try, (Integer) 0); contentValues.put(com.baidu.location.a.a.a, (Integer) 0); writableDatabase.insert(com.baidu.location.a.a.f32goto, null, contentValues); String[] split = str.split(iU); for (String str2 : split) { ContentValues contentValues2 = new ContentValues(); contentValues2.put("geofence_id", geofenceId); contentValues2.put(com.baidu.location.a.b.f42int, str2); int lastIndexOf = str2.lastIndexOf("|"); if (lastIndexOf != -1) { str2 = str2.substring(0, lastIndexOf); } contentValues2.put(com.baidu.location.a.b.f40for, str2); writableDatabase.insert(com.baidu.location.a.b.a, null, contentValues2); } writableDatabase.setTransactionSuccessful(); writableDatabase.endTransaction(); } catch (Exception e) { writableDatabase.endTransaction(); i = 1; } catch (Throwable th) { writableDatabase.endTransaction(); throw th; } writableDatabase.close(); } } return i; } /* access modifiers changed from: private */ /* access modifiers changed from: public */ /* renamed from: if reason: not valid java name */ private void m248if(int i, String str, GeofenceClient.OnAddBDGeofencesResultListener onAddBDGeofencesResultListener) { if (i == 1) { } onAddBDGeofencesResultListener.onAddBDGeofencesResult(i, str); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ /* renamed from: if reason: not valid java name */ private void m249if(int i, String[] strArr, GeofenceClient.OnRemoveBDGeofencesResultListener onRemoveBDGeofencesResultListener) { onRemoveBDGeofencesResultListener.onRemoveBDGeofencesByRequestIdsResult(i, strArr); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ /* renamed from: if reason: not valid java name */ private void m250if(GeofenceClient.OnAddBDGeofencesResultListener onAddBDGeofencesResultListener, int i, String str) { Message obtain = Message.obtain(this.i2); obtain.what = 0; obtain.obj = onAddBDGeofencesResultListener; Bundle bundle = new Bundle(); bundle.putInt(iW, i); bundle.putString("geofence_id", str); obtain.setData(bundle); this.i2.sendMessage(obtain); } /* renamed from: int reason: not valid java name */ public static void m254int(Context context) { a4.dc().m106try(f.getServiceContext()); } public void cH() { synchronized (this.iV) { this.i2.post(new Runnable() { /* class com.baidu.location.ay.AnonymousClass3 */ public void run() { ay.this.cJ(); } }); } } /* renamed from: do reason: not valid java name */ public void m255do(final au auVar) { this.i2.post(new Runnable() { /* class com.baidu.location.ay.AnonymousClass1 */ public void run() { ay.this.m256do(auVar.getGeofenceId(), true); } }); } /* renamed from: do reason: not valid java name */ public synchronized void m256do(String str, boolean z) { SQLiteDatabase writableDatabase; long j = a4.lh; synchronized (this) { if (!TextUtils.isEmpty(str) && (writableDatabase = l.a(this.iO).getWritableDatabase()) != null) { try { ContentValues contentValues = new ContentValues(); if (z) { long currentTimeMillis = System.currentTimeMillis(); if (!(GeofenceClient.e() == 0 || GeofenceClient.e() == a4.lh)) { j = GeofenceClient.e(); } contentValues.put(com.baidu.location.a.a.f37try, Long.valueOf(j + currentTimeMillis)); } else { long currentTimeMillis2 = System.currentTimeMillis(); if (!(GeofenceClient.e() == 0 || GeofenceClient.e() == a4.lh)) { j = GeofenceClient.e(); } contentValues.put(com.baidu.location.a.a.a, Long.valueOf(j + currentTimeMillis2)); } writableDatabase.update(com.baidu.location.a.a.f32goto, contentValues, "geofence_id= ?", new String[]{str}); } catch (Exception e) { } finally { writableDatabase.close(); } } } } /* renamed from: if reason: not valid java name */ public void m257if(final au auVar) { this.i2.post(new Runnable() { /* class com.baidu.location.ay.AnonymousClass2 */ public void run() { ay.this.m256do(auVar.getGeofenceId(), false); } }); } /* renamed from: if reason: not valid java name */ public void m258if(au auVar, GeofenceClient.OnAddBDGeofencesResultListener onAddBDGeofencesResultListener) { cL(); ar.a(onAddBDGeofencesResultListener, "OnAddBDGeofenceRecesResultListener not provided."); if (cM() >= 10) { onAddBDGeofencesResultListener.onAddBDGeofencesResult(BDLocationStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES, auVar.getGeofenceId()); return; } new b(auVar, onAddBDGeofencesResultListener).al(); cK(); } /* renamed from: if reason: not valid java name */ public void m259if(List list, GeofenceClient.OnRemoveBDGeofencesResultListener onRemoveBDGeofencesResultListener) { ar.m197if(list != null && list.size() > 0, "geofenceRequestIds can't be null nor empty."); ar.a(onRemoveBDGeofencesResultListener, "onRemoveBDGeofencesResultListener not provided."); this.i2.post(new d(list, onRemoveBDGeofencesResultListener)); } }