Aqara Home v2.4.1版本的 MD5 值为:529779ac2bc0131db52a1fc3098d2b35

以下内容为反编译后的 LocationUpdateService.java 源代码,内容仅作参考


package com.lumiunited.aqara.location;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Pair;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import androidx.work.impl.background.systemalarm.CommandHandler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.facebook.react.views.text.FontMetricsUtil;
import com.lumiunited.aqara.device.lock.fragment.OnceTimePasswordFragment;
import com.lumiunited.aqara.location.LocationUpdateService;
import com.lumiunited.aqarahome.R;
import com.umeng.socialize.common.SocializeConstants;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import k.u.c.d.d.l0;
import k.u.c.d.j.n;
import p.a.k0;
import p.a.x0.o;
public class LocationUpdateService extends IntentService {
    public static double f12267n;
    public static double f12268o;
    public static Address f12269p;
    public final int a;
    public final int b;
    public final int c;
    public final int d;
    public final int f12270e;
    public ArrayList<String> f12271f;
    public boolean f12272g;
    public String f12273h;
    public LocationManager f12274i;
    public int f12275j;
    public LocationListener f12276k;
    public LocationListener f12277l;
    public LocationListener f12278m;

    public class a implements LocationListener {
        public a() {
        }

        @Override
        public void onLocationChanged(Location location) {
        }

        @Override
        public void onProviderDisabled(String str) {
            LocationUpdateService.this.b();
        }

        @Override
        public void onProviderEnabled(String str) {
            LocationUpdateService.this.b();
        }

        @Override
        public void onStatusChanged(String str, int i2, Bundle bundle) {
        }
    }

    public class b implements LocationListener {
        public b() {
        }

        @Override
        public void onLocationChanged(Location location) {
        }

        @Override
        public void onProviderDisabled(String str) {
            LocationUpdateService.this.b();
        }

        @Override
        public void onProviderEnabled(String str) {
            LocationUpdateService.this.b();
        }

        @Override
        public void onStatusChanged(String str, int i2, Bundle bundle) {
        }
    }

    public class c implements LocationListener {
        public c() {
        }

        @Override
        public void onLocationChanged(Location location) {
        }

        @Override
        public void onProviderDisabled(String str) {
            LocationUpdateService.this.b();
        }

        @Override
        public void onProviderEnabled(String str) {
            LocationUpdateService.this.b();
        }

        @Override
        public void onStatusChanged(String str, int i2, Bundle bundle) {
        }
    }

    public class d implements n<Pair<String, String>> {
        public d() {
        }

        @Override
        public void onSuccess(Pair<String, String> pair) {
            try {
                LocationUpdateService.f12269p = LocationUpdateService.this.a(Double.parseDouble((String) pair.first), Double.parseDouble((String) pair.second));
                x.b.a.c.f().c(new k.u.c.i.h3.p.d(LocationUpdateService.f12269p));
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

        @Override
        public void onFailed(int i2, String str) {
        }
    }

    public static class e implements n<String> {
        public final n a;

        public e(n nVar) {
            this.a = nVar;
        }

        @Override
        public void onSuccess(String str) {
            try {
                JSONObject jSONObject = JSON.parseObject(str).getJSONObject("content").getJSONObject("point");
                this.a.onSuccess(new Pair(jSONObject.getString(FontMetricsUtil.X_HEIGHT_MEASUREMENT_TEXT), jSONObject.getString("y")));
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

        @Override
        public void onFailed(int i2, String str) {
        }
    }

    public LocationUpdateService() {
        super("haha");
        this.a = OnceTimePasswordFragment.M;
        this.b = 15000;
        this.c = 2000;
        this.d = 2;
        this.f12270e = 4;
        this.f12275j = 0;
        this.f12276k = new a();
        this.f12277l = new b();
        this.f12278m = new c();
        ArrayList<String> arrayList = new ArrayList<>();
        this.f12271f = arrayList;
        arrayList.add("gps");
        this.f12271f.add("network");
        this.f12271f.add("passive");
        this.f12272g = false;
    }

    public static void b(Context context) {
        context.stopService(new Intent(context, LocationUpdateService.class));
    }

    public static String c() {
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://pv.sohu.com/cityjson?ie=utf-8").openConnection();
            if (httpURLConnection.getResponseCode() == 200) {
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                StringBuilder sb = new StringBuilder();
                while (true) {
                    String readLine = bufferedReader.readLine();
                    if (readLine == null) {
                        break;
                    }
                    sb.append(readLine + "\n");
                }
                inputStream.close();
                String substring = sb.substring(sb.indexOf("{"), sb.indexOf("}") + 1);
                if (substring != null) {
                    try {
                        return JSON.parseObject(substring).getString("cip");
                    } catch (JSONException e2) {
                        e2.printStackTrace();
                        return null;
                    }
                }
                return null;
            }
            return null;
        } catch (MalformedURLException e3) {
            e3.printStackTrace();
            return null;
        } catch (IOException e4) {
            e4.printStackTrace();
            return null;
        }
    }

    private boolean d() {
        if (f12269p == null && this.f12275j < 4) {
            a(new d());
            return true;
        }
        return false;
    }

    private void e() {
        Location lastKnownLocation;
        String str = this.f12273h;
        if (str == null || str.equals("") || !this.f12271f.contains(this.f12273h)) {
            return;
        }
        try {
            if ((ContextCompat.checkSelfPermission(this, "android.permission.ACCESS_FINE_LOCATION") == 0 || ContextCompat.checkSelfPermission(this, "android.permission.ACCESS_COARSE_LOCATION") == 0) && (lastKnownLocation = this.f12274i.getLastKnownLocation(this.f12273h)) != null) {
                double latitude = lastKnownLocation.getLatitude();
                double longitude = lastKnownLocation.getLongitude();
                f12267n = latitude;
                f12268o = longitude;
                f12269p = a(longitude, latitude);
                x.b.a.c.f().c(new k.u.c.i.h3.p.d(f12269p));
            }
        } catch (Exception unused) {
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= 26) {
            a();
        }
    }

    @Override
    public void onDestroy() {
        LocationListener locationListener;
        LocationListener locationListener2;
        LocationListener locationListener3;
        super.onDestroy();
        this.f12272g = true;
        LocationManager locationManager = this.f12274i;
        if (locationManager != null && (locationListener3 = this.f12276k) != null) {
            locationManager.removeUpdates(locationListener3);
        }
        LocationManager locationManager2 = this.f12274i;
        if (locationManager2 != null && (locationListener2 = this.f12277l) != null) {
            locationManager2.removeUpdates(locationListener2);
        }
        LocationManager locationManager3 = this.f12274i;
        if (locationManager3 != null && (locationListener = this.f12278m) != null) {
            locationManager3.removeUpdates(locationListener);
        }
        stopForeground(true);
    }

    @Override
    public void onHandleIntent(Intent intent) {
        this.f12273h = null;
        this.f12274i = null;
        this.f12275j = 0;
        LocationManager locationManager = (LocationManager) getSystemService(SocializeConstants.KEY_LOCATION);
        this.f12274i = locationManager;
        if (locationManager == null) {
            return;
        }
        if (ContextCompat.checkSelfPermission(this, "android.permission.ACCESS_FINE_LOCATION") == 0 || ContextCompat.checkSelfPermission(this, "android.permission.ACCESS_COARSE_LOCATION") == 0) {
            List<String> allProviders = this.f12274i.getAllProviders();
            if (allProviders != null) {
                for (String str : allProviders) {
                    if (str != null && this.f12271f.contains(str)) {
                        if ("gps".equals(str)) {
                            this.f12274i.requestLocationUpdates("gps", 10000L, 0.0f, this.f12276k);
                        } else if ("network".equals(str)) {
                            this.f12274i.requestLocationUpdates("network", 10000L, 0.0f, this.f12277l);
                        } else if ("passive".equals(str)) {
                            this.f12274i.requestLocationUpdates("passive", 10000L, 0.0f, this.f12278m);
                        }
                    }
                }
            }
            while (!this.f12272g) {
                b();
                e();
                String str2 = this.f12273h;
                if (str2 != null && this.f12271f.contains(str2)) {
                    try {
                        if (b(f12267n, 0.0d) == 0 && b(f12268o, 0.0d) == 0) {
                            Thread.sleep(15000L);
                        } else {
                            Thread.sleep(CommandHandler.WORK_PROCESSING_TIME_IN_MS);
                        }
                    } catch (InterruptedException | Exception unused) {
                    }
                } else {
                    int i2 = this.f12275j + 1;
                    this.f12275j = i2;
                    if (f12269p != null) {
                        x.b.a.c.f().c(new k.u.c.i.h3.p.d(f12269p));
                        return;
                    } else if (2 <= i2 && !d()) {
                        return;
                    } else {
                        Thread.sleep(2000L);
                    }
                }
            }
        }
    }

    public static void a(Context context) {
        Intent intent = new Intent(context, LocationUpdateService.class);
        if (Build.VERSION.SDK_INT >= 26) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }

    public synchronized void b() {
        String str = null;
        if (this.f12274i == null) {
            this.f12273h = null;
            return;
        }
        List<String> allProviders = this.f12274i.getAllProviders();
        for (String str2 : allProviders) {
        }
        if (allProviders != null && allProviders.size() > 0) {
            Location location = null;
            for (String str3 : allProviders) {
                if (str3 != null && this.f12271f.contains(str3)) {
                    if (ContextCompat.checkSelfPermission(this, "android.permission.ACCESS_FINE_LOCATION") != 0 && ContextCompat.checkSelfPermission(this, "android.permission.ACCESS_COARSE_LOCATION") != 0) {
                        return;
                    }
                    Location lastKnownLocation = this.f12274i.getLastKnownLocation(str3);
                    if (lastKnownLocation != null) {
                        if (location != null && Float.valueOf(lastKnownLocation.getAccuracy()).compareTo(Float.valueOf(location.getAccuracy())) < 0) {
                        }
                        str = str3;
                        location = lastKnownLocation;
                    }
                }
            }
            this.f12273h = str;
            return;
        }
        this.f12273h = null;
    }

    public Address a(double d2, double d3) {
        Geocoder geocoder = new Geocoder(getApplicationContext());
        Geocoder.isPresent();
        StringBuilder sb = new StringBuilder();
        try {
            List<Address> fromLocation = geocoder.getFromLocation(d3, d2, 1);
            if (fromLocation.size() > 0) {
                int i2 = 0;
                Address address = fromLocation.get(0);
                while (true) {
                    if (i2 >= address.getMaxAddressLineIndex()) {
                        break;
                    }
                    if (i2 == 0) {
                        sb.append(address.getAddressLine(i2));
                        sb.append("-");
                    }
                    if (i2 == 1) {
                        break;
                    }
                    i2++;
                }
                return address;
            }
            return null;
        } catch (IOException e2) {
            e2.printStackTrace();
            return null;
        }
    }

    public static p.a.u0.c a(final n<Pair<String, String>> nVar) {
        return k0.c(1).j().b(p.a.e1.b.e()).i(new o() {
            @Override
            public final Object apply(Object obj) {
                String c2;
                Integer num = (Integer) obj;
                c2 = LocationUpdateService.c();
                return c2;
            }
        }).a(p.a.e1.b.e()).i(new o() {
            @Override
            public final Object apply(Object obj) {
                return LocationUpdateService.a(n.this, (String) obj);
            }
        }).subscribe();
    }

    public static int b(double d2, double d3) {
        if (Double.isInfinite(d2) && Double.isInfinite(d3)) {
            return 0;
        }
        if (Double.isInfinite(d2)) {
            return 1;
        }
        if (Double.isInfinite(d3)) {
            return -1;
        }
        if (Double.isNaN(d2) && Double.isNaN(d3)) {
            return 0;
        }
        if (Double.isNaN(d2)) {
            return -1;
        }
        if (Double.isNaN(d3)) {
            return 1;
        }
        BigDecimal bigDecimal = new BigDecimal(d2);
        BigDecimal bigDecimal2 = new BigDecimal(d3);
        if (bigDecimal.compareTo(bigDecimal2) < 0) {
            return -1;
        }
        if (bigDecimal.compareTo(bigDecimal2) == 0) {
            return 0;
        }
        return bigDecimal.compareTo(bigDecimal2) > 0 ? 1 : -2;
    }

    public static Boolean a(n nVar, String str) throws Exception {
        l0.i().b("http://api.map.baidu.com/location/ip?ip=" + str + "&ak=nwzHnxiXsIbGhh88Vi3oPnwsx03wunmI&coor=gcj02", "GET", (String) null, (HashMap<String, String>) null, new e(nVar));
        return true;
    }

    @RequiresApi(api = 26)
    private void a() {
        NotificationChannel notificationChannel = new NotificationChannel(getString(R.string.aqara_home_name), getString(R.string.aqara_home_name), 0);
        notificationChannel.setName(getString(R.string.location_notify));
        ((NotificationManager) getApplicationContext().getSystemService("notification")).createNotificationChannel(notificationChannel);
        startForeground(1, new Notification.Builder(getApplicationContext(), getString(R.string.aqara_home_name)).build());
    }
}