CamScanner.apk(点击下载) / DriveAPI.java


package com.intsig.webstorage.drive;

import android.content.Context;
import android.text.TextUtils;
import androidx.collection.ArrayMap;
import com.intsig.utils.IOUtil;
import com.intsig.utils.net.OkHttpUtil;
import com.intsig.webstorage.RemoteFile;
import com.intsig.webstorage.UploadProgressListener;
import com.intsig.webstorage.WebStorageApi;
import com.intsig.webstorage.WebstorageException;
import com.intsig.webstorage.exception.LoginException;
import com.intsig.webstorage.googleaccount.GoogleAuthUtil;
import com.intsig.webstorage.util.CloudServiceUtils;
import com.intsig.webstorage.util.ParamCheckUtil;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSource;
import okio.Okio;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.scribe.model.OAuthConstants;

public class DriveAPI extends WebStorageApi {

    /* renamed from: c  reason: collision with root package name */
    private String f41040c = GoogleAuthUtil.i(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email");

    public DriveAPI(Context context) {
        super(context, 0);
        GoogleAuthUtil.l(context.getApplicationContext());
    }

    private JSONObject n(RemoteFile remoteFile, RemoteFile remoteFile2) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("title", remoteFile.f40978b);
            jSONObject.put("mimeType", "application/vnd.google-apps.folder");
        } catch (JSONException e10) {
            CloudServiceUtils.c("DriveApi", "createFolder4Drive JSONException", e10);
        }
        if (remoteFile2 != null && !TextUtils.isEmpty(remoteFile2.f40977a)) {
            JSONObject jSONObject2 = new JSONObject();
            try {
                jSONObject2.put("id", remoteFile2.f40977a);
                JSONArray jSONArray = new JSONArray();
                jSONArray.put(jSONObject2);
                jSONObject.put("parents", jSONArray);
            } catch (Exception e11) {
                CloudServiceUtils.c("DriveApi", "createFolder4Drive JSONException", e11);
            }
            return jSONObject;
        }
        return jSONObject;
    }

    private int o(int i10, String str, InputStream inputStream) {
        CloudServiceUtils.e("DriveApi", "checkResponse code " + i10 + " , message = " + str + " , errorStr = " + r(inputStream));
        if (i10 == 200) {
            return 0;
        }
        CloudServiceUtils.b("DriveApi", "Error code = " + i10 + ", message = " + str);
        if (i10 == 401) {
            try {
                if (GoogleAuthUtil.n(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email") == 0) {
                    this.f41040c = GoogleAuthUtil.i(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email");
                }
            } catch (Exception e10) {
                CloudServiceUtils.c("DriveApi", "checkReponse LoginException ", e10);
            }
            return -8;
        } else if (i10 == 412) {
            return -9;
        } else {
            if (i10 == 403) {
                return -6;
            }
            if (i10 == 404) {
                return -7;
            }
        }
        return -4;
    }

    private int p(Response response) {
        return o(response.p(), response.A(), null);
    }

    private int q(RemoteFile remoteFile, RemoteFile remoteFile2) {
        JSONObject n10 = n(remoteFile, remoteFile2);
        try {
            MultipartUtility multipartUtility = new MultipartUtility(new URL("https://www.googleapis.com/upload/drive/v2/files"), this.f41040c);
            try {
                String jSONObject = n10.toString();
                CloudServiceUtils.e("DriveApi", "createFolder4Drive json " + jSONObject);
                multipartUtility.b("meta", jSONObject, "application/json");
                int o10 = o(multipartUtility.e(), multipartUtility.g(), multipartUtility.f());
                CloudServiceUtils.e("DriveApi", "createFolder4Drive result " + o10);
                return o10;
            } finally {
                multipartUtility.c();
            }
        } catch (UnsupportedEncodingException e10) {
            CloudServiceUtils.d("DriveApi", e10);
            return -4;
        } catch (IOException e11) {
            CloudServiceUtils.d("DriveApi", e11);
            return -3;
        } catch (Exception e12) {
            CloudServiceUtils.c("DriveApi", "uploadFile2Drive other exception", e12);
            return -4;
        }
    }

    private String r(InputStream inputStream) {
        if (inputStream != null) {
            try {
                BufferedSource buffer = Okio.buffer(Okio.source(inputStream));
                return buffer.readString(IOUtil.a(buffer, IOUtil.f40071f));
            } catch (Exception e10) {
                CloudServiceUtils.d("DriveApi", e10);
            }
        }
        return "";
    }

    private JSONObject s(File file, String str) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("title", file.getName());
        } catch (JSONException e10) {
            CloudServiceUtils.c("DriveApi", "uploadFile2Drive JSONException", e10);
        }
        if (str != null && str.length() > 0) {
            JSONObject jSONObject2 = new JSONObject();
            try {
                jSONObject2.put("id", str);
                jSONObject2.put("kind", "drive#fileLink");
                JSONArray jSONArray = new JSONArray();
                jSONArray.put(jSONObject2);
                jSONObject.put("parents", jSONArray);
            } catch (Exception e11) {
                CloudServiceUtils.c("DriveApi", "uploadFile2Drive JSONException", e11);
            }
            return jSONObject;
        }
        return jSONObject;
    }

    /* JADX DEBUG: Multi-variable search result rejected for r7v22, resolved type: com.intsig.utils.net.OkHttpUtil */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v0, types: [java.util.List<com.intsig.webstorage.RemoteFile>, java.util.Map] */
    /* JADX WARNING: Removed duplicated region for block: B:24:0x0091  */
    /* JADX WARNING: Removed duplicated region for block: B:40:0x00ba  */
    /* JADX WARNING: Removed duplicated region for block: B:48:0x00cb  */
    /* JADX WARNING: Removed duplicated region for block: B:54:0x00d3  */
    /* JADX WARNING: Unknown variable types count: 1 */
    private List<RemoteFile> t(RemoteFile remoteFile, int i10) throws WebstorageException {
        Throwable th;
        InputStream inputStream;
        IllegalStateException e10;
        InputStream inputStream2;
        IOException e11;
        List<RemoteFile> list;
        InputStream inputStream3;
        ?? r02 = 0;
        try {
            ArrayMap arrayMap = new ArrayMap();
            arrayMap.put("maxResults", "1000");
            arrayMap.put(OAuthConstants.TOKEN, this.f41040c);
            if (i10 == 1) {
                arrayMap.put("q", "trashed=false and mimeType = 'application/vnd.google-apps.folder'");
            } else {
                arrayMap.put("q", "trashed=false");
            }
            Response e12 = OkHttpUtil.t().e("https://www.googleapis.com/drive/v2/files", arrayMap, r02, true);
            int p10 = p(e12);
            CloudServiceUtils.e("DriveApi", "listDriveFile result " + p10);
            if (p10 == 0) {
                ResponseBody e13 = e12.e();
                if (e13 != null) {
                    inputStream2 = e13.byteStream();
                    try {
                        list = v(inputStream2, i10);
                        inputStream3 = inputStream2;
                        if (inputStream3 != null) {
                            try {
                                inputStream3.close();
                            } catch (IOException unused) {
                            }
                        }
                        return list;
                    } catch (IllegalStateException e14) {
                        e10 = e14;
                        CloudServiceUtils.c("DriveApi", "uploadFile2Drive IllegalStateException", e10);
                        if (inputStream2 != null) {
                            try {
                                inputStream2.close();
                                return r02;
                            } catch (IOException unused2) {
                            }
                        }
                        return r02;
                    } catch (IOException e15) {
                        e11 = e15;
                        try {
                            CloudServiceUtils.c("DriveApi", "uploadFile2Drive other exception", e11);
                            if (inputStream2 != null) {
                                inputStream2.close();
                                return r02;
                            }
                            return r02;
                        } catch (Throwable th2) {
                            th = th2;
                            inputStream = inputStream2;
                            if (inputStream != null) {
                            }
                            throw th;
                        }
                    }
                }
            } else if (p10 == -8) {
                throw new WebstorageException(-8, "listDriveFile");
            } else if (p10 == -6) {
                throw new WebstorageException(-6, "listDriveFile");
            }
            list = r02;
            inputStream3 = r02;
            if (inputStream3 != null) {
            }
            return list;
        } catch (IllegalStateException e16) {
            e10 = e16;
            inputStream2 = r02;
            CloudServiceUtils.c("DriveApi", "uploadFile2Drive IllegalStateException", e10);
            if (inputStream2 != null) {
            }
            return r02;
        } catch (IOException e17) {
            e11 = e17;
            inputStream2 = r02;
            CloudServiceUtils.c("DriveApi", "uploadFile2Drive other exception", e11);
            if (inputStream2 != null) {
            }
            return r02;
        } catch (Throwable th3) {
            th = th3;
            inputStream = r02;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException unused3) {
                }
            }
            throw th;
        }
    }

    private List<RemoteFile> u(RemoteFile remoteFile, int i10) throws LoginException, WebstorageException {
        List<RemoteFile> list;
        if ((GoogleAuthUtil.m(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email") ? GoogleAuthUtil.n(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email") : 0) == 0) {
            list = t(remoteFile, i10);
            if (list == null) {
                CloudServiceUtils.b("DriveApi", "listDriveFile get null, retry");
                return t(remoteFile, i10);
            }
        } else {
            list = null;
        }
        return list;
    }

    private List<RemoteFile> v(InputStream inputStream, int i10) {
        ArrayList arrayList = new ArrayList();
        if (inputStream != null) {
            try {
                JSONArray jSONArray = new JSONObject(w(inputStream)).getJSONArray("items");
                if (jSONArray != null && jSONArray.length() >= 1) {
                    int length = jSONArray.length();
                    CloudServiceUtils.b("DriveApi", "parseListJson size " + length);
                    for (int i11 = 0; i11 < length; i11++) {
                        DriveFile driveFile = new DriveFile(jSONArray.getJSONObject(i11));
                        if (!driveFile.j()) {
                            if (i10 != 2) {
                                if (i10 == 0) {
                                    if (!driveFile.i()) {
                                    }
                                }
                                if (i10 == 1 && driveFile.h()) {
                                }
                            }
                            arrayList.add(driveFile);
                        }
                    }
                }
            } catch (Exception e10) {
                CloudServiceUtils.c("DriveApi", "Exception", e10);
            }
        }
        return arrayList;
    }

    private String w(InputStream inputStream) {
        StringBuilder sb2 = new StringBuilder();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    break;
                }
                sb2.append(readLine);
            }
            bufferedReader.close();
        } catch (Exception e10) {
            CloudServiceUtils.d("DriveApi", e10);
        }
        return sb2.toString();
    }

    private int x(RemoteFile remoteFile, RemoteFile remoteFile2, UploadProgressListener uploadProgressListener) {
        JSONObject s10 = s(remoteFile.f40979c, remoteFile2 != null ? remoteFile2.f40977a : null);
        try {
            MultipartUtility multipartUtility = new MultipartUtility(new URL("https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart"), this.f41040c);
            try {
                String jSONObject = s10.toString();
                CloudServiceUtils.e("DriveApi", "uploadFile2Drive json " + jSONObject);
                multipartUtility.b("meta", jSONObject, "application/json");
                multipartUtility.a("content", remoteFile.f40979c, uploadProgressListener);
                int o10 = o(multipartUtility.e(), multipartUtility.g(), multipartUtility.f());
                CloudServiceUtils.e("DriveApi", "uploadFile2Drive responseCode = " + o10);
                return o10;
            } finally {
                multipartUtility.c();
            }
        } catch (UnsupportedEncodingException e10) {
            CloudServiceUtils.c("DriveApi", "uploadFile2Drive UnsupportedEncodingException", e10);
            return -4;
        } catch (IOException e11) {
            CloudServiceUtils.c("DriveApi", "uploadFile2Drive IOException", e11);
            return -3;
        } catch (Exception e12) {
            CloudServiceUtils.c("DriveApi", "uploadFile2Drive other exception", e12);
            return -4;
        }
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public boolean b(int i10) {
        GoogleAuthUtil.a(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email");
        return false;
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public int c(RemoteFile remoteFile, RemoteFile remoteFile2) {
        String str = this.f41040c;
        int i10 = -8;
        if (str != null) {
            if (str.length() == 0) {
                return i10;
            }
            if (GoogleAuthUtil.m(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email")) {
                try {
                    i10 = GoogleAuthUtil.n(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email");
                } catch (LoginException unused) {
                }
            } else {
                i10 = 0;
            }
            if (i10 == 0) {
                i10 = q(remoteFile, remoteFile2);
            }
        }
        return i10;
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public String g() {
        return GoogleAuthUtil.k(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email");
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public boolean h() {
        String i10 = GoogleAuthUtil.i(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email");
        this.f41040c = i10;
        return !TextUtils.isEmpty(i10);
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public List<RemoteFile> i(RemoteFile remoteFile) throws WebstorageException {
        try {
            return u(remoteFile, 2);
        } catch (LoginException unused) {
            throw new WebstorageException(-8, "LoginException");
        }
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public List<RemoteFile> j(RemoteFile remoteFile) throws WebstorageException {
        try {
            return u(remoteFile, 1);
        } catch (LoginException unused) {
            throw new WebstorageException(-8, "LoginException");
        }
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public void k() {
        GoogleAuthUtil.b(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email");
        this.f41040c = null;
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public boolean l() {
        return true;
    }

    @Override // com.intsig.webstorage.WebStorageApi
    public int m(RemoteFile remoteFile, RemoteFile remoteFile2, UploadProgressListener uploadProgressListener) {
        int i10;
        if (ParamCheckUtil.b(remoteFile)) {
            return -7;
        }
        this.f41040c = GoogleAuthUtil.i(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email");
        if (GoogleAuthUtil.m(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email")) {
            CloudServiceUtils.e("DriveApi", "upload Token is Expired");
            try {
                i10 = GoogleAuthUtil.n(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email");
            } catch (LoginException e10) {
                CloudServiceUtils.c("DriveApi", "LoginException upload ", e10);
                i10 = -8;
            }
            this.f41040c = GoogleAuthUtil.i(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email");
            CloudServiceUtils.e("DriveApi", "new token " + this.f41040c);
        } else {
            CloudServiceUtils.e("DriveApi", "upload Token is not Expired ");
            i10 = 0;
        }
        if (i10 != 0) {
            return i10;
        }
        CloudServiceUtils.e("DriveApi", "authType " + GoogleAuthUtil.j(this.f40999a, "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.email"));
        String str = this.f41040c;
        if (str != null) {
            if (str.length() != 0) {
                return x(remoteFile, remoteFile2, uploadProgressListener);
            }
        }
        return -8;
    }
}