得物.apk(点击下载) / FetchService.java


package com.shizhuang.duapp.libs.robustplus.core;

import android.os.Build;
import androidx.core.view.accessibility.AccessibilityEventCompat;
import com.meituan.robust.ChangeQuickRedirect;
import com.meituan.robust.PatchProxy;
import com.meituan.robust.PatchProxyResult;
import com.meizu.cloud.pushsdk.constants.PushConstants;
import com.shizhuang.duapp.libs.duapm2.aop.NetOKAspect;
import com.shizhuang.duapp.libs.robustplus.core.track.TrackService;
import com.shizhuang.duapp.libs.robustplus.model.Attribute;
import com.shizhuang.duapp.libs.robustplus.model.Version;
import hq.a;
import hq.e;
import iq.b;
import iq.c;
import iq.d;
import iq.f;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.aspectj.lang.JoinPoint;
import org.aspectj.runtime.internal.AroundClosure;
import org.aspectj.runtime.reflect.Factory;
import org.json.JSONException;
import org.json.JSONObject;

public class FetchService {
    private static /* synthetic */ JoinPoint.StaticPart ajc$tjp_0;
    public static ChangeQuickRedirect changeQuickRedirect;
    public static boolean isDev = false;
    private static String patchApiDev = "http://t1-app.dewu.com/api/v1/app/wireless-platform/client/cold";
    private static String patchApiPublic = "https://app.dewu.com/api/v1/app/wireless-platform/client/cold";
    public OkHttpClient client;
    private long delta;
    private long targetInterval;

    public class AjcClosure1 extends AroundClosure {
        public static ChangeQuickRedirect changeQuickRedirect;

        public AjcClosure1(Object[] objArr) {
            super(objArr);
        }

        @Override // org.aspectj.runtime.internal.AroundClosure
        public Object run(Object[] objArr) {
            PatchProxyResult proxy = PatchProxy.proxy(new Object[]{objArr}, this, changeQuickRedirect, false, 35933, new Class[]{Object[].class}, Object.class);
            if (proxy.isSupported) {
                return proxy.result;
            }
            Object[] objArr2 = this.state;
            FetchService fetchService = (FetchService) objArr2[0];
            JoinPoint joinPoint = (JoinPoint) objArr2[2];
            return ((OkHttpClient.Builder) objArr2[1]).build();
        }
    }

    static {
        ajc$preClinit();
    }

    private static /* synthetic */ void ajc$preClinit() {
        if (!PatchProxy.proxy(new Object[0], null, changeQuickRedirect, true, 35922, new Class[0], Void.TYPE).isSupported) {
            Factory factory = new Factory("FetchService.java", FetchService.class);
            ajc$tjp_0 = factory.makeSJP("method-call", factory.makeMethodSig("1", "build", "okhttp3.OkHttpClient$Builder", "", "", "", "okhttp3.OkHttpClient"), 201);
        }
    }

    private boolean canAccess(boolean z) {
        Object[] objArr = {new Byte(z ? (byte) 1 : 0)};
        ChangeQuickRedirect changeQuickRedirect2 = changeQuickRedirect;
        Class cls = Boolean.TYPE;
        PatchProxyResult proxy = PatchProxy.proxy(objArr, this, changeQuickRedirect2, false, 35914, new Class[]{cls}, cls);
        if (proxy.isSupported) {
            return ((Boolean) proxy.result).booleanValue();
        }
        try {
            String lastTime = getLastTime(z);
            if (!f.c(lastTime)) {
                lastTime = "0";
            }
            long parseLong = Long.parseLong(lastTime);
            this.targetInterval = z ? ConfigService.getSafeFetchInterval() : ConfigService.getFetchInterval();
            this.delta = (System.currentTimeMillis() - parseLong) - this.targetInterval;
            b.a("delta = " + this.delta);
            if (this.delta >= 0) {
                return true;
            }
            return false;
        } catch (Exception e) {
            b.c(e.getLocalizedMessage());
            e.printStackTrace();
            return false;
        }
    }

    private String getLastTime(boolean z) {
        PatchProxyResult proxy = PatchProxy.proxy(new Object[]{new Byte(z ? (byte) 1 : 0)}, this, changeQuickRedirect, false, 35920, new Class[]{Boolean.TYPE}, String.class);
        return proxy.isSupported ? (String) proxy.result : c.a(getTimeFile(z));
    }

    private String getTimeFile(boolean z) {
        PatchProxyResult proxy = PatchProxy.proxy(new Object[]{new Byte(z ? (byte) 1 : 0)}, this, changeQuickRedirect, false, 35921, new Class[]{Boolean.TYPE}, String.class);
        if (proxy.isSupported) {
            return (String) proxy.result;
        }
        StringBuilder sb2 = new StringBuilder();
        sb2.append(a.e());
        sb2.append(File.separator);
        return a.f.g(sb2, z ? "safe_fetch_time" : "fetch_time", ".time");
    }

    public static Attribute parse(String str) {
        PatchProxyResult proxy = PatchProxy.proxy(new Object[]{str}, null, changeQuickRedirect, true, 35918, new Class[]{String.class}, Attribute.class);
        if (proxy.isSupported) {
            return (Attribute) proxy.result;
        }
        Attribute stringToObject = Attribute.stringToObject(str);
        if (stringToObject == null || !f.c(stringToObject.getBuildVersion()) || !f.c(stringToObject.getTargetVersion()) || !f.c(stringToObject.getMd5()) || !f.c(stringToObject.getUrl())) {
            return null;
        }
        return stringToObject;
    }

    private void post(String str, boolean z, final FetchCallback fetchCallback) {
        if (!PatchProxy.proxy(new Object[]{str, new Byte(z ? (byte) 1 : 0), fetchCallback}, this, changeQuickRedirect, false, 35915, new Class[]{String.class, Boolean.TYPE, FetchCallback.class}, Void.TYPE).isSupported) {
            if (this.client == null) {
                OkHttpClient.Builder hostnameVerifier = new OkHttpClient.Builder().sslSocketFactory(d.a()).hostnameVerifier(new d.a());
                TimeUnit timeUnit = TimeUnit.SECONDS;
                OkHttpClient.Builder hostnameVerifier2 = hostnameVerifier.connectTimeout(4, timeUnit).readTimeout(5, timeUnit).hostnameVerifier(new HostnameVerifier() {
                    /* class com.shizhuang.duapp.libs.robustplus.core.FetchService.AnonymousClass2 */
                    public static ChangeQuickRedirect changeQuickRedirect;

                    public boolean verify(String str, SSLSession sSLSession) {
                        PatchProxyResult proxy = PatchProxy.proxy(new Object[]{str, sSLSession}, this, changeQuickRedirect, false, 35928, new Class[]{String.class, SSLSession.class}, Boolean.TYPE);
                        if (proxy.isSupported) {
                            return ((Boolean) proxy.result).booleanValue();
                        }
                        return true;
                    }
                });
                this.client = (OkHttpClient) NetOKAspect.aspectOf().aroundBuild(new AjcClosure1(new Object[]{this, hostnameVerifier2, Factory.makeJP(ajc$tjp_0, this, hostnameVerifier2)}).linkClosureAndJoinPoint(4112));
            }
            setLastTime(String.valueOf(System.currentTimeMillis()), z);
            StringBuilder sb2 = new StringBuilder();
            sb2.append(isDev ? patchApiDev : patchApiPublic);
            sb2.append("?platform=android&buildVersion=");
            sb2.append(Version.buildVersion());
            sb2.append("&patchVersion=");
            sb2.append(InstallService.REAL_PATCH_VERSION());
            sb2.append("&appVersion=");
            sb2.append(Version.buildVersion());
            androidx.appcompat.widget.a.n(sb2, "&appKey=", str, "&deviceModel=");
            sb2.append(Build.BRAND);
            sb2.append("&deviceOsVersion=");
            sb2.append(Build.VERSION.SDK_INT);
            sb2.append("&deviceEncode=");
            sb2.append(f.f34062a);
            String sb3 = sb2.toString();
            b.a("request url = " + sb3);
            fetchCallback.startFetch(sb3);
            this.client.newCall(new Request.Builder().url(sb3).get().build()).enqueue(new Callback() {
                /* class com.shizhuang.duapp.libs.robustplus.core.FetchService.AnonymousClass3 */
                public static ChangeQuickRedirect changeQuickRedirect;

                @Override // okhttp3.Callback
                public void onFailure(Call call, IOException iOException) {
                    if (!PatchProxy.proxy(new Object[]{call, iOException}, this, changeQuickRedirect, false, 35929, new Class[]{Call.class, IOException.class}, Void.TYPE).isSupported) {
                        b.c(iOException.getLocalizedMessage());
                        fetchCallback.fetchError(iOException.getLocalizedMessage());
                    }
                }

                @Override // okhttp3.Callback
                public void onResponse(Call call, Response response) throws IOException {
                    if (!PatchProxy.proxy(new Object[]{call, response}, this, changeQuickRedirect, false, 35930, new Class[]{Call.class, Response.class}, Void.TYPE).isSupported) {
                        ResponseBody body = response.body();
                        if (body == null) {
                            b.c("body is null");
                            fetchCallback.fetchError("response body is null");
                            return;
                        }
                        String string = body.string();
                        b.a("res = " + string);
                        fetchCallback.fetchSuccess(string);
                    }
                }
            });
        }
    }

    private void setLastTime(String str, boolean z) {
        if (!PatchProxy.proxy(new Object[]{str, new Byte(z ? (byte) 1 : 0)}, this, changeQuickRedirect, false, 35919, new Class[]{String.class, Boolean.TYPE}, Void.TYPE).isSupported) {
            c.b(str, getTimeFile(z));
        }
    }

    public void copy(File file, File file2) {
        if (!PatchProxy.proxy(new Object[]{file, file2}, this, changeQuickRedirect, false, 35917, new Class[]{File.class, File.class}, Void.TYPE).isSupported) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                try {
                    FileChannel channel = fileInputStream.getChannel();
                    try {
                        FileOutputStream fileOutputStream = new FileOutputStream(file2);
                        try {
                            FileChannel channel2 = fileOutputStream.getChannel();
                            try {
                                FileLock lock = channel2.lock();
                                try {
                                    channel2.transferFrom(channel, 0, channel.size());
                                    if (lock != null) {
                                        lock.close();
                                    }
                                    channel2.close();
                                    fileOutputStream.close();
                                    channel.close();
                                    fileInputStream.close();
                                    return;
                                } catch (Throwable th2) {
                                    th.addSuppressed(th2);
                                }
                            } catch (Throwable th3) {
                                th.addSuppressed(th3);
                            }
                        } catch (Throwable th4) {
                            th.addSuppressed(th4);
                        }
                    } catch (Throwable th5) {
                        th.addSuppressed(th5);
                    }
                } catch (Throwable th6) {
                    th.addSuppressed(th6);
                }
            } catch (Exception e) {
                b.c(e.getLocalizedMessage());
                e.printStackTrace();
                return;
            }
        } else {
            return;
        }
        throw th;
        throw th;
        throw th;
        throw th;
        throw th;
    }

    public void download(String str, final String str2, final boolean z, final FetchCallback fetchCallback) {
        if (!PatchProxy.proxy(new Object[]{str, str2, new Byte(z ? (byte) 1 : 0), fetchCallback}, this, changeQuickRedirect, false, 35916, new Class[]{String.class, String.class, Boolean.TYPE, FetchCallback.class}, Void.TYPE).isSupported) {
            b.a("download url = " + str);
            this.client.newCall(new Request.Builder().url(str).build()).enqueue(new Callback() {
                /* class com.shizhuang.duapp.libs.robustplus.core.FetchService.AnonymousClass4 */
                public static ChangeQuickRedirect changeQuickRedirect;

                @Override // okhttp3.Callback
                public void onFailure(Call call, IOException iOException) {
                    if (!PatchProxy.proxy(new Object[]{call, iOException}, this, changeQuickRedirect, false, 35931, new Class[]{Call.class, IOException.class}, Void.TYPE).isSupported) {
                        b.c(iOException.getLocalizedMessage());
                        fetchCallback.downloadError(iOException.getLocalizedMessage());
                    }
                }

                @Override // okhttp3.Callback
                public void onResponse(Call call, Response response) throws IOException {
                    if (!PatchProxy.proxy(new Object[]{call, response}, this, changeQuickRedirect, false, 35932, new Class[]{Call.class, Response.class}, Void.TYPE).isSupported) {
                        b.a("download onresponse");
                        if (response.body() == null) {
                            b.c("body is null");
                            fetchCallback.downloadError("response body is null");
                            return;
                        }
                        StringBuilder sb2 = new StringBuilder();
                        sb2.append(a.a());
                        String str = File.separator;
                        sb2.append(str);
                        sb2.append(z ? "safe_temp" : "temp");
                        sb2.append(str);
                        File file = new File(a.f.g(sb2, str2, ".jar"));
                        if (file.exists()) {
                            file.delete();
                        } else {
                            file.getParentFile().mkdirs();
                        }
                        try {
                            InputStream byteStream = response.body().byteStream();
                            try {
                                FileOutputStream fileOutputStream = new FileOutputStream(file);
                                try {
                                    byte[] bArr = new byte[AccessibilityEventCompat.TYPE_TOUCH_EXPLORATION_GESTURE_END];
                                    while (true) {
                                        int read = byteStream.read(bArr);
                                        if (read == -1) {
                                            break;
                                        }
                                        fileOutputStream.write(bArr, 0, read);
                                    }
                                    fileOutputStream.close();
                                    byteStream.close();
                                    b.a("copy patch");
                                    StringBuilder sb3 = new StringBuilder();
                                    sb3.append(a.a());
                                    sb3.append(File.separator);
                                    File file2 = new File(a.f.g(sb3, str2, ".jar"));
                                    if (file2.exists()) {
                                        file2.delete();
                                    } else {
                                        file2.getParentFile().mkdirs();
                                    }
                                    FetchService.this.copy(file, file2);
                                    if (file2.exists()) {
                                        fetchCallback.downloadSuccess();
                                        return;
                                    } else {
                                        fetchCallback.downloadError("file not exist");
                                        return;
                                    }
                                } catch (Throwable th2) {
                                    th.addSuppressed(th2);
                                }
                            } catch (Throwable th3) {
                                th.addSuppressed(th3);
                            }
                        } catch (Exception e) {
                            b.c(e.getLocalizedMessage());
                            e.printStackTrace();
                        }
                    } else {
                        return;
                    }
                    throw th;
                    throw th;
                }
            });
        }
    }

    public void start(String str, final boolean z, final FetchCallback fetchCallback) {
        if (!PatchProxy.proxy(new Object[]{str, new Byte(z ? (byte) 1 : 0), fetchCallback}, this, changeQuickRedirect, false, 35913, new Class[]{String.class, Boolean.TYPE, FetchCallback.class}, Void.TYPE).isSupported) {
            if (canAccess(z)) {
                post(str, z, new FetchCallback() {
                    /* class com.shizhuang.duapp.libs.robustplus.core.FetchService.AnonymousClass1 */
                    public static ChangeQuickRedirect changeQuickRedirect;

                    @Override // com.shizhuang.duapp.libs.robustplus.core.FetchCallback
                    public void fetchError(String str) {
                        if (!PatchProxy.proxy(new Object[]{str}, this, changeQuickRedirect, false, 35925, new Class[]{String.class}, Void.TYPE).isSupported) {
                            fetchCallback.fetchError(str);
                        }
                    }

                    @Override // com.shizhuang.duapp.libs.robustplus.core.FetchCallback
                    public void fetchSuccess(String str) {
                        if (!PatchProxy.proxy(new Object[]{str}, this, changeQuickRedirect, false, 35924, new Class[]{String.class}, Void.TYPE).isSupported) {
                            fetchCallback.fetchSuccess(str);
                            try {
                                JSONObject jSONObject = new JSONObject(str);
                                int i = jSONObject.getInt("status");
                                if (i == 200) {
                                    JSONObject jSONObject2 = jSONObject.getJSONObject("data");
                                    if (jSONObject2 != null) {
                                        if (f.c(jSONObject2.has(PushConstants.WEB_URL) ? jSONObject2.getString(PushConstants.WEB_URL) : null)) {
                                            final Attribute parse = FetchService.parse(jSONObject2.toString());
                                            if (parse != null) {
                                                boolean write = parse.write();
                                                StringBuilder sb2 = new StringBuilder();
                                                sb2.append("write attribute ");
                                                sb2.append(write ? "success" : "fail");
                                                b.a(sb2.toString());
                                                fetchCallback.containsPatch(parse.getTargetVersion(), parse.getUrl(), parse.getMd5());
                                                fetchCallback.startDownload(parse.getUrl());
                                                File file = new File(a.a() + File.separator + parse.getTargetVersion() + ".jar");
                                                if (!file.exists() || !file.isFile() || !parse.getMd5().equals(f.a(file))) {
                                                    FetchService.this.download(parse.getUrl(), parse.getTargetVersion(), z, new FetchCallback() {
                                                        /* class com.shizhuang.duapp.libs.robustplus.core.FetchService.AnonymousClass1.AnonymousClass1 */
                                                        public static ChangeQuickRedirect changeQuickRedirect;

                                                        @Override // com.shizhuang.duapp.libs.robustplus.core.FetchCallback
                                                        public void downloadError(String str) {
                                                            if (!PatchProxy.proxy(new Object[]{str}, this, changeQuickRedirect, false, 35927, new Class[]{String.class}, Void.TYPE).isSupported) {
                                                                fetchCallback.downloadError(str);
                                                            }
                                                        }

                                                        @Override // com.shizhuang.duapp.libs.robustplus.core.FetchCallback
                                                        public void downloadSuccess() {
                                                            if (!PatchProxy.proxy(new Object[0], this, changeQuickRedirect, false, 35926, new Class[0], Void.TYPE).isSupported) {
                                                                fetchCallback.downloadSuccess();
                                                                TrackService.reportDownloadSuccess(parse.getPatchId());
                                                                if (e.d(parse)) {
                                                                    fetchCallback.hotInstall();
                                                                }
                                                            }
                                                        }
                                                    });
                                                    return;
                                                }
                                                b.a("md5 matches, skip download");
                                                fetchCallback.downloadSuccess();
                                                if (e.d(parse)) {
                                                    fetchCallback.hotInstall();
                                                    return;
                                                }
                                                return;
                                            }
                                            fetchCallback.noPatch();
                                            return;
                                        }
                                        b.a("url is null");
                                        fetchCallback.noPatch();
                                        return;
                                    }
                                    fetchCallback.fetchError("response.data null");
                                    return;
                                }
                                FetchCallback fetchCallback = fetchCallback;
                                fetchCallback.fetchError("status = " + i);
                            } catch (JSONException e) {
                                b.c(e.getLocalizedMessage());
                                e.printStackTrace();
                                fetchCallback.fetchError(e.getLocalizedMessage());
                            }
                        }
                    }

                    @Override // com.shizhuang.duapp.libs.robustplus.core.FetchCallback
                    public void startFetch(String str) {
                        if (!PatchProxy.proxy(new Object[]{str}, this, changeQuickRedirect, false, 35923, new Class[]{String.class}, Void.TYPE).isSupported) {
                            fetchCallback.startFetch(str);
                        }
                    }
                });
                return;
            }
            b.a("cd...");
            fetchCallback.cd(this.delta, this.targetInterval);
        }
    }
}