智能工厂.apk(点击下载) / a.java


package cn.jpush.android.b;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.text.TextUtils;
import cn.jpush.android.helper.Logger;
import cn.jpush.android.local.JPushConstants;
import cn.jpush.android.u.h;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class a extends c {
    private long d;
    private LocationManager e;
    private Handler f;
    private long g = 900000;
    private long h = 20000;
    private String i;
    private boolean j = false;
    @SuppressLint({"MissingPermission"})
    private LocationListener k = new LocationListener() {
        /* class cn.jpush.android.b.a.AnonymousClass1 */

        public void onLocationChanged(Location location) {
            a.this.e.removeUpdates(this);
            a.this.l.set(false);
            a.this.a((a) location);
        }

        public void onProviderDisabled(String str) {
        }

        public void onProviderEnabled(String str) {
        }

        public void onStatusChanged(String str, int i, Bundle bundle) {
        }
    };
    private final AtomicBoolean l = new AtomicBoolean();

    public a(Context context) {
        super(context);
        this.e = (LocationManager) context.getSystemService("location");
        this.g = cn.jpush.android.cache.a.c(context, 900000L);
        this.d = cn.jpush.android.cache.a.c(context, -1L);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(Location location) {
        Logger.dd("CustomGeofenAction", "current location:" + location);
        Handler handler = this.f;
        if (handler != null && handler.hasMessages(1001)) {
            this.f.removeMessages(1001);
        }
        if (location != null) {
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            boolean z = false;
            Iterator it = this.b.entrySet().iterator();
            while (it.hasNext()) {
                b bVar = (b) ((Map.Entry) it.next()).getValue();
                if (bVar.e * 1000 <= System.currentTimeMillis()) {
                    Logger.dd("CustomGeofenAction", "Out of date geofence " + bVar.a);
                    it.remove();
                    b(bVar);
                } else {
                    double a = h.a(longitude, latitude, bVar.f, bVar.g);
                    Logger.dd("CustomGeofenAction", bVar.a + " distance to center:" + a);
                    String str = a <= ((double) bVar.b) ? "in" : "out";
                    double d2 = (double) bVar.b;
                    Double.isNaN(d2);
                    if (Math.abs(a - d2) < 1000.0d) {
                        z = true;
                    }
                    Logger.d("CustomGeofenAction", "lastStatus:" + bVar.h + ",currentStatus:" + str);
                    if (!str.equals(bVar.h)) {
                        if (bVar.h != null && str.equals(bVar.c) && f(bVar)) {
                            e(bVar);
                            d(bVar);
                            if (!bVar.d) {
                                Logger.dd("CustomGeofenAction", "No repeat geofence " + bVar.a);
                                this.b.remove(bVar.a);
                                c();
                                b(bVar);
                            }
                        }
                        bVar.h = str;
                    }
                }
            }
            if (this.d == -1) {
                this.g = z ? 180000 : 900000;
            }
            c();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void b(long j2) {
        Logger.dd("CustomGeofenAction", "Scan geofence after " + j2 + "ms");
        Handler handler = this.f;
        if (handler != null) {
            if (handler.hasMessages(1000)) {
                this.f.removeMessages(1000);
            }
            this.f.sendEmptyMessageDelayed(1000, j2);
        }
    }

    private void d() {
        try {
            AnonymousClass2 r0 = new HandlerThread("jg_cgf_thread") {
                /* class cn.jpush.android.b.a.AnonymousClass2 */

                public void run() {
                    try {
                        super.run();
                    } catch (RuntimeException e) {
                        Logger.e("CustomGeofenAction", "handler thread run e:" + e + "  t=" + Thread.currentThread().getName() + "_" + Thread.currentThread().getId());
                    }
                }
            };
            r0.start();
            this.f = new Handler(r0.getLooper()) {
                /* class cn.jpush.android.b.a.AnonymousClass3 */

                /* JADX INFO: Can't fix incorrect switch cases order, some code will duplicate */
                @SuppressLint({"MissingPermission"})
                public void handleMessage(Message message) {
                    String str;
                    String str2;
                    switch (message.what) {
                        case 1000:
                            Logger.d("CustomGeofenAction", "period task tid " + Thread.currentThread().getId());
                            a aVar = a.this;
                            aVar.b((a) aVar.g);
                            break;
                        case 1001:
                            a.this.e();
                            try {
                                if (a.this.i == null || !a.this.i.equals("gps")) {
                                    str = "CustomGeofenAction";
                                    str2 = "Network provider time out!";
                                } else {
                                    Logger.dd("CustomGeofenAction", "GPS provider time out!");
                                    a.this.i = "network";
                                    if (a.this.e.isProviderEnabled(a.this.i)) {
                                        a.this.e.requestLocationUpdates(a.this.i, 2000, 0.0f, a.this.k);
                                        a.this.f.sendEmptyMessageDelayed(1001, a.this.h / 2);
                                        return;
                                    }
                                    str = "CustomGeofenAction";
                                    str2 = "Network provider is disabled";
                                }
                                Logger.dd(str, str2);
                            } catch (Throwable th) {
                                Logger.dd("CustomGeofenAction", "request location error#" + th);
                            }
                            a.this.l.set(false);
                            return;
                        case 1002:
                            break;
                        default:
                            return;
                    }
                    a.this.f();
                }
            };
        } catch (Throwable th) {
            Logger.ww("CustomGeofenAction", "init geofence handler failed:" + th);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    @SuppressLint({"MissingPermission"})
    private void e() {
        String str;
        String str2;
        try {
            if (this.k == null) {
                str = "CustomGeofenAction";
                str2 = "Location listener is null , do nothing!";
            } else if (this.e != null) {
                this.e.removeUpdates(this.k);
                return;
            } else {
                str = "CustomGeofenAction";
                str2 = "locationManager is null , do nothing!";
            }
            Logger.w(str, str2);
        } catch (Throwable th) {
            Logger.w("CustomGeofenAction", "remove location listener failed  e:" + th.getMessage());
        }
    }

    private void e(b bVar) {
        long currentTimeMillis = System.currentTimeMillis();
        bVar.o = currentTimeMillis;
        Date date = new Date(currentTimeMillis);
        String format = h.a("yyyy-DDD").format(date);
        if (format.equals(bVar.r)) {
            bVar.s++;
        } else {
            bVar.r = format;
            bVar.s = 1;
        }
        String format2 = h.a("yyyy-ww").format(date);
        if (format2.equals(bVar.p)) {
            bVar.q++;
            return;
        }
        bVar.p = format2;
        bVar.q = 1;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void f() {
        Logger.d("CustomGeofenAction", "try locate...");
        if (!cn.jpush.android.u.a.a(this.a, "android.permission.ACCESS_FINE_LOCATION")) {
            Logger.w("CustomGeofenAction", "Require the permissionandroid.permission.ACCESS_FINE_LOCATION");
            Logger.dd("CustomGeofenAction", "No location permission!");
            return;
        }
        Logger.d("CustomGeofenAction", "sdk int:" + Build.VERSION.SDK_INT + ",targetSdkVersion:" + this.a.getApplicationInfo().targetSdkVersion);
        if (JPushConstants.canGetLbsInBackGround(this.a)) {
            if (!h()) {
                Logger.dd("CustomGeofenAction", "No enabled provider");
            } else if (this.b.size() <= 0) {
                a();
            } else if (this.l.get()) {
                Logger.d("CustomGeofenAction", "isLocating...");
            } else {
                g();
            }
        }
    }

    private boolean f(b bVar) {
        if (bVar.o <= 0) {
            return true;
        }
        if (!bVar.d) {
            Logger.d("CustomGeofenAction", "can't repeat geofence");
            return false;
        }
        long currentTimeMillis = System.currentTimeMillis();
        if (bVar.n <= 0 || bVar.o <= 0 || currentTimeMillis >= bVar.o + ((long) (bVar.n * 1000))) {
            if (bVar.m > 0 && !TextUtils.isEmpty(bVar.r)) {
                if (bVar.r.equals(h.a("yyyy-DDD").format(new Date(currentTimeMillis))) && bVar.s >= bVar.m) {
                    Logger.d("CustomGeofenAction", "today already repeat enough:" + bVar.s + "/" + bVar.m);
                    return false;
                }
            }
            if (bVar.l > 0 && !TextUtils.isEmpty(bVar.p)) {
                if (bVar.p.equals(h.a("yyyy-ww").format(new Date(currentTimeMillis))) && bVar.q >= bVar.l) {
                    Logger.d("CustomGeofenAction", "this week already repeat enough:" + bVar.q + "/" + bVar.l);
                    return false;
                }
            }
            return true;
        }
        Logger.d("CustomGeofenAction", "in " + bVar.n + "s, can't repeat geofence");
        return false;
    }

    /* JADX WARNING: Removed duplicated region for block: B:24:0x008e A[Catch:{ SecurityException -> 0x0143, Throwable -> 0x012b }] */
    /* JADX WARNING: Removed duplicated region for block: B:25:0x0093 A[Catch:{ SecurityException -> 0x0143, Throwable -> 0x012b }] */
    /* JADX WARNING: Removed duplicated region for block: B:28:0x009b A[Catch:{ SecurityException -> 0x0143, Throwable -> 0x012b }] */
    /* JADX WARNING: Removed duplicated region for block: B:33:0x00c0 A[Catch:{ SecurityException -> 0x0143, Throwable -> 0x012b }] */
    /* JADX WARNING: Removed duplicated region for block: B:42:0x00ff A[Catch:{ SecurityException -> 0x0143, Throwable -> 0x012b }] */
    /* JADX WARNING: Removed duplicated region for block: B:44:0x0107 A[Catch:{ SecurityException -> 0x0143, Throwable -> 0x012b }] */
    @SuppressLint({"MissingPermission"})
    private void g() {
        String str;
        try {
            if (this.e != null) {
                Location lastKnownLocation = this.e.getLastKnownLocation("gps");
                Location lastKnownLocation2 = this.e.getLastKnownLocation("network");
                Location lastKnownLocation3 = this.e.getLastKnownLocation("passive");
                Logger.v("CustomGeofenAction", "gpsLocation:" + lastKnownLocation);
                Logger.v("CustomGeofenAction", "netLocation:" + lastKnownLocation2);
                Logger.v("CustomGeofenAction", "passLocation:" + lastKnownLocation3);
                long j2 = 0;
                long time = lastKnownLocation == null ? 0 : lastKnownLocation.getTime();
                long time2 = lastKnownLocation2 == null ? 0 : lastKnownLocation2.getTime();
                long time3 = lastKnownLocation3 == null ? 0 : lastKnownLocation3.getTime();
                if (time > time2) {
                    if (time > time3) {
                        if (System.currentTimeMillis() - (lastKnownLocation != null ? lastKnownLocation.getTime() : 0) < 30000) {
                            StringBuilder sb = new StringBuilder();
                            sb.append("need not restart gpslocation,the time with last:");
                            long currentTimeMillis = System.currentTimeMillis();
                            if (lastKnownLocation != null) {
                                j2 = lastKnownLocation.getTime();
                            }
                            sb.append(currentTimeMillis - j2);
                            Logger.dd("CustomGeofenAction", sb.toString());
                            a(lastKnownLocation);
                            return;
                        }
                        this.i = null;
                        if (this.e.isProviderEnabled("gps")) {
                            str = "gps";
                        } else {
                            if (this.e.isProviderEnabled("network")) {
                                str = "network";
                            }
                            Logger.d("CustomGeofenAction", "provider " + this.i);
                            if (!TextUtils.isEmpty(this.i)) {
                                Logger.dd("CustomGeofenAction", "not gps nor network provider,stop scan geofence");
                                return;
                            }
                            this.l.set(true);
                            this.e.requestLocationUpdates(this.i, 2000, 0.0f, this.k);
                            this.f.sendEmptyMessageDelayed(1001, this.h);
                            return;
                        }
                        this.i = str;
                        Logger.d("CustomGeofenAction", "provider " + this.i);
                        if (!TextUtils.isEmpty(this.i)) {
                        }
                    }
                } else if (time2 > time3) {
                    lastKnownLocation = lastKnownLocation2;
                    if (System.currentTimeMillis() - (lastKnownLocation != null ? lastKnownLocation.getTime() : 0) < 30000) {
                    }
                }
                lastKnownLocation = lastKnownLocation3;
                if (System.currentTimeMillis() - (lastKnownLocation != null ? lastKnownLocation.getTime() : 0) < 30000) {
                }
            } else {
                Logger.ww("CustomGeofenAction", "locationManager is null");
            }
        } catch (SecurityException unused) {
            Logger.ww("CustomGeofenAction", "No suitable permission when get last known location!");
        } catch (Throwable th) {
            Logger.ww("CustomGeofenAction", "The provider is illegal argument!" + th);
        }
    }

    private boolean h() {
        try {
            if (this.e != null) {
                return this.e.isProviderEnabled("gps") || this.e.isProviderEnabled("network") || this.e.isProviderEnabled("passive");
            }
            return false;
        } catch (SecurityException unused) {
            Logger.ww("CustomGeofenAction", "No suitable permission is present when get GPS_PROVIDER!");
            return false;
        } catch (IllegalArgumentException unused2) {
            Logger.ww("CustomGeofenAction", "The provider [gps] is illegal argument!");
            return false;
        } catch (Throwable th) {
            Logger.ww("CustomGeofenAction", "Check GPS enable failed:" + th);
            return false;
        }
    }

    /* access modifiers changed from: protected */
    @Override // cn.jpush.android.b.c
    public synchronized void a() {
        Logger.dd("CustomGeofenAction", "geofence size:" + this.b.size());
        Logger.dd("CustomGeofenAction", "stop listen geofence");
        if (this.j) {
            if (this.f != null) {
                this.f.removeMessages(1000);
            }
            this.j = false;
        }
    }

    @Override // cn.jpush.android.b.c
    public void a(long j2) {
        Logger.dd("CustomGeofenAction", "Set geofence interval " + j2);
        this.g = j2;
        this.d = j2;
        cn.jpush.android.cache.a.b(this.a, j2);
    }

    /* access modifiers changed from: protected */
    @Override // cn.jpush.android.b.c
    public void a(b bVar) {
        Handler handler;
        Logger.dd("CustomGeofenAction", "Geofence create success, id=" + bVar.a);
        if (this.j && (handler = this.f) != null) {
            handler.sendEmptyMessage(1002);
        }
    }

    /* access modifiers changed from: protected */
    @Override // cn.jpush.android.b.c
    public void a(b bVar, b bVar2) {
        Handler handler;
        Logger.dd("CustomGeofenAction", "Geofence update success, id=" + bVar.a);
        if (this.j && (handler = this.f) != null) {
            handler.sendEmptyMessage(1002);
        }
    }

    @Override // cn.jpush.android.b.c
    public synchronized void b() {
        Logger.dd("CustomGeofenAction", "start listen geofence");
        if (!cn.jpush.android.cache.a.k(this.a)) {
            Logger.ww("CustomGeofenAction", "lbs is disable!");
        } else if (this.j) {
            Logger.ii("CustomGeofenAction", "geofence is running!");
        } else if (this.b.size() == 0) {
            Logger.dd("CustomGeofenAction", "No geofence,not need listen");
        } else {
            if (this.f == null) {
                d();
            }
            b(0L);
            this.j = true;
        }
    }

    /* access modifiers changed from: protected */
    @Override // cn.jpush.android.b.c
    public void b(b bVar) {
        Logger.dd("CustomGeofenAction", "Geofence delete success, id=" + bVar.a);
    }
}