开发专用 v00.00.26版本的 MD5 值为:27d23588b440d1fe24ff30607dd0eda9

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


package com.uzmap.pkg.uzmodules.uzxml;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import com.uzmap.pkg.uzcore.UZWebView;
import com.uzmap.pkg.uzcore.annotation.UzJavascriptMethod;
import com.uzmap.pkg.uzcore.uzmodule.UZModule;
import com.uzmap.pkg.uzcore.uzmodule.UZModuleContext;
import com.uzmap.pkg.uzkit.UZOpenApi;
import com.uzmap.pkg.uzkit.UZUtility;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import org.json.JSONException;
import org.json.JSONObject;

public class UzXml extends UZModule {
    private static final int DEFAULT_BUFFER_SIZE = 4096;
    private JSONObject err;
    private JSONObject ret;

    public UzXml(UZWebView uZWebView) {
        super(uZWebView);
        this.ret = new JSONObject();
        this.err = new JSONObject();
    }

    @UzJavascriptMethod
    public void jsmethod_parse(UZModuleContext uZModuleContext) {
        String optString = uZModuleContext.optString(UZOpenApi.DATA);
        String optString2 = uZModuleContext.optString("path");
        String optString3 = uZModuleContext.optString("customKey");
        if (TextUtils.isEmpty(optString3)) {
            optString3 = "plainText";
        }
        if (!TextUtils.isEmpty(optString) && !TextUtils.isEmpty(optString2)) {
            uZModuleContext.success(UZUtility.xmlToJsonObject("<author email = '123@app3c.com'>app3c</author>", optString3), false);
            return;
        }
        if (!TextUtils.isEmpty(optString2)) {
            try {
                uZModuleContext.success(UZUtility.xmlToJsonObject(toString(generatePath(optString2), "UTF-8"), optString3), false);
                return;
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        }
        if (TextUtils.isEmpty(optString)) {
            return;
        }
        uZModuleContext.success(UZUtility.xmlToJsonObject(optString, optString3), false);
    }

    public void jsmethod_saveImage(final UZModuleContext uZModuleContext) {
        new Thread() {
            @Override
            public void run() {
                String optString = uZModuleContext.optString("base64Str");
                if (TextUtils.isEmpty(optString)) {
                    return;
                }
                String optString2 = uZModuleContext.optString("imgName", "apicloud.png");
                String optString3 = uZModuleContext.optString("imgPath");
                String lowerCase = optString2.substring(optString2.lastIndexOf(46) + 1, optString2.length()).toLowerCase();
                try {
                    if (!uZModuleContext.isNull("album")) {
                        boolean optBoolean = uZModuleContext.optBoolean("album", false);
                        if (optBoolean) {
                            UzXml.this.base64ToBitmap(optString, UzXml.this.getOutputMediaFile(optString2), lowerCase, optBoolean);
                        }
                        if (TextUtils.isEmpty(optString3) || TextUtils.isEmpty(optString2)) {
                            return;
                        }
                        UzXml.this.base64ToBitmap(optString, UzXml.this.getOutputMediaFile(optString3, optString2), lowerCase, optBoolean);
                        UzXml.this.success(uZModuleContext);
                        return;
                    }
                    if (TextUtils.isEmpty(optString3) || TextUtils.isEmpty(optString2)) {
                        return;
                    }
                    UzXml.this.base64ToBitmap(optString, UzXml.this.getOutputMediaFile(optString3, optString2), lowerCase, true);
                    UzXml.this.success(uZModuleContext);
                } catch (Exception e) {
                    UzXml.this.failed(uZModuleContext, e.getMessage());
                }
            }
        }.start();
    }

    void failed(UZModuleContext uZModuleContext, String str) {
        JSONObject jSONObject = new JSONObject();
        JSONObject jSONObject2 = new JSONObject();
        try {
            jSONObject.put(NotificationCompat.CATEGORY_STATUS, false);
            jSONObject2.put(NotificationCompat.CATEGORY_MESSAGE, str);
            uZModuleContext.error(jSONObject, jSONObject2, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private String toString(InputStream inputStream, String str) throws IOException {
        StringWriter stringWriter = new StringWriter();
        copy(inputStream, stringWriter, str);
        return stringWriter.toString();
    }

    private void copy(InputStream inputStream, Writer writer) throws IOException {
        copy(new InputStreamReader(inputStream), writer);
    }

    private void copy(InputStream inputStream, Writer writer, String str) throws IOException {
        if (str == null) {
            copy(inputStream, writer);
        } else {
            copy(new InputStreamReader(inputStream, str), writer);
        }
    }

    private int copy(Reader reader, Writer writer) throws IOException {
        long copyLarge = copyLarge(reader, writer);
        if (copyLarge > 2147483647L) {
            return -1;
        }
        return (int) copyLarge;
    }

    private long copyLarge(Reader reader, Writer writer) throws IOException {
        char[] cArr = new char[4096];
        long j = 0;
        while (true) {
            int read = reader.read(cArr);
            if (-1 == read) {
                return j;
            }
            writer.write(cArr, 0, read);
            j += read;
        }
    }

    @Override
    public void onClean() {
        super.onClean();
    }

    private InputStream generatePath(String str) {
        try {
            return UZUtility.guessInputStream(makeRealPath(str));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private boolean isEmpty(CharSequence charSequence) {
        return charSequence == null || charSequence.length() == 0;
    }

    private String substringAfter(String str, String str2) {
        int indexOf;
        return isEmpty(str) ? str : (str2 == null || (indexOf = str.indexOf(str2)) == -1) ? "" : str.substring(indexOf + str2.length());
    }

    public void success(UZModuleContext uZModuleContext) {
        try {
            try {
                this.ret.put(NotificationCompat.CATEGORY_STATUS, true);
                uZModuleContext.success(this.ret, false);
                if (!this.ret.has(NotificationCompat.CATEGORY_STATUS)) {
                    return;
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (!this.ret.has(NotificationCompat.CATEGORY_STATUS)) {
                    return;
                }
            }
            this.ret.remove(NotificationCompat.CATEGORY_STATUS);
        } catch (Throwable th) {
            if (this.ret.has(NotificationCompat.CATEGORY_STATUS)) {
                this.ret.remove(NotificationCompat.CATEGORY_STATUS);
            }
            throw th;
        }
    }

    public boolean checkStorage() {
        return Environment.getExternalStorageState().equals("mounted");
    }

    public File getOutputMediaFile(String str) {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera");
        if (!file.exists()) {
            file.mkdirs();
        }
        return new File(file.getPath() + File.separator + str);
    }

    public File getOutputMediaFile(String str, String str2) {
        File file = new File(generateRealPath(str));
        if (!file.exists()) {
            file.mkdirs();
        }
        File file2 = new File(file.getPath() + File.separator + str2);
        Log.i("MediaFile", file2.toString());
        return file2;
    }

    public Bitmap generateBitmap(String str) {
        Bitmap bitmap = null;
        try {
            InputStream guessInputStream = UZUtility.guessInputStream(makeRealPath(str));
            bitmap = BitmapFactory.decodeStream(guessInputStream);
            guessInputStream.close();
            return bitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return bitmap;
        }
    }

    public byte[] getDataFromFile(UZModuleContext uZModuleContext, String str) {
        try {
            InputStream guessInputStream = UZUtility.guessInputStream(uZModuleContext.makeRealPath(str));
            byte[] bArr = new byte[guessInputStream.available()];
            guessInputStream.read(bArr, 0, guessInputStream.available());
            guessInputStream.close();
            return bArr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e2) {
            e2.printStackTrace();
            return null;
        }
    }

    public void jsmethod_decodeImgToBase64(final UZModuleContext uZModuleContext) {
        new Thread() {
            @Override
            public void run() {
                String optString = uZModuleContext.optString("imgPath");
                if (TextUtils.isEmpty(optString)) {
                    return;
                }
                byte[] dataFromFile = UzXml.this.getDataFromFile(uZModuleContext, optString);
                if (dataFromFile != null) {
                    String encodeToString = Base64.encodeToString(dataFromFile, 0);
                    try {
                        JSONObject jSONObject = new JSONObject();
                        jSONObject.put(NotificationCompat.CATEGORY_STATUS, true);
                        jSONObject.put("base64Str", encodeToString);
                        uZModuleContext.success(jSONObject, false);
                        return;
                    } catch (JSONException e) {
                        e.printStackTrace();
                        return;
                    }
                }
                try {
                    JSONObject jSONObject2 = new JSONObject();
                    jSONObject2.put(NotificationCompat.CATEGORY_STATUS, false);
                    jSONObject2.put("base64Str", "");
                    uZModuleContext.success(jSONObject2, false);
                } catch (JSONException e2) {
                    e2.printStackTrace();
                }
            }
        }.start();
    }

    private Bitmap readBitmap(String str) {
        try {
            return BitmapFactory.decodeFile(str);
        } catch (Exception unused) {
            return null;
        }
    }

    public String imgToBase64(Bitmap bitmap, boolean z, String str, UZModuleContext uZModuleContext) {
        ByteArrayOutputStream byteArrayOutputStream;
        try {
            if (bitmap == null) {
                try {
                    this.ret.put(NotificationCompat.CATEGORY_STATUS, false);
                    this.err.put(NotificationCompat.CATEGORY_MESSAGE, "图片不存在或图片路径错误");
                    uZModuleContext.error(this.ret, this.err, false);
                    if (this.ret.has(NotificationCompat.CATEGORY_STATUS)) {
                        this.ret.remove(NotificationCompat.CATEGORY_STATUS);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    if (this.ret.has(NotificationCompat.CATEGORY_STATUS)) {
                        this.ret.remove(NotificationCompat.CATEGORY_STATUS);
                    }
                }
            }
            ByteArrayOutputStream byteArrayOutputStream2 = null;
            try {
                byteArrayOutputStream = new ByteArrayOutputStream();
                try {
                    if (str.equals("jpg")) {
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
                    } else if (str.equals("png")) {
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                    } else {
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                    }
                    if (z) {
                        bitmap.recycle();
                    }
                    byteArrayOutputStream.flush();
                    byteArrayOutputStream.close();
                    String encodeToString = Base64.encodeToString(byteArrayOutputStream.toByteArray(), 0);
                    try {
                        byteArrayOutputStream.flush();
                        byteArrayOutputStream.close();
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                    return encodeToString;
                } catch (Exception unused) {
                    try {
                        byteArrayOutputStream.flush();
                        byteArrayOutputStream.close();
                    } catch (IOException e3) {
                        e3.printStackTrace();
                    }
                    return null;
                } catch (Throwable th) {
                    th = th;
                    byteArrayOutputStream2 = byteArrayOutputStream;
                    try {
                        byteArrayOutputStream2.flush();
                        byteArrayOutputStream2.close();
                    } catch (IOException e4) {
                        e4.printStackTrace();
                    }
                    throw th;
                }
            } catch (Exception unused2) {
                byteArrayOutputStream = null;
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Throwable th3) {
            if (this.ret.has(NotificationCompat.CATEGORY_STATUS)) {
                this.ret.remove(NotificationCompat.CATEGORY_STATUS);
            }
            if (this.err.has(NotificationCompat.CATEGORY_MESSAGE)) {
                this.err.remove(NotificationCompat.CATEGORY_MESSAGE);
            }
            throw th3;
        }
    }

    private String generateRealPath(String str) {
        String makeRealPath = UZUtility.makeRealPath(str, getWidgetInfo());
        if (TextUtils.isEmpty(makeRealPath)) {
            return null;
        }
        if (makeRealPath.contains("file://")) {
            return substringAfter(makeRealPath, "file://");
        }
        makeRealPath.contains("android_asset");
        return makeRealPath;
    }

    public void base64ToBitmap(String str, File file, String str2, boolean z) throws Exception {
        boolean compress;
        byte[] decode = Base64.decode(str, 0);
        Bitmap decodeByteArray = BitmapFactory.decodeByteArray(decode, 0, decode.length);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        if (str2.equals("jpg")) {
            compress = decodeByteArray.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        } else if (str2.equals("png")) {
            compress = decodeByteArray.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
        } else {
            compress = decodeByteArray.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
        }
        if (compress) {
            fileOutputStream.flush();
            fileOutputStream.close();
            if (z) {
                Uri fromFile = Uri.fromFile(file);
                if (context() != null) {
                    context().sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", fromFile));
                    return;
                }
                return;
            }
            return;
        }
        fileOutputStream.close();
    }

    public void jsmethod_saveFile(final UZModuleContext uZModuleContext) {
        new Thread() {
            @Override
            public void run() {
                String optString = uZModuleContext.optString("base64Str");
                if (TextUtils.isEmpty(optString)) {
                    return;
                }
                String optString2 = uZModuleContext.optString("fileName");
                UZModuleContext uZModuleContext2 = uZModuleContext;
                try {
                    UzXml.this.base64ToFile(optString, UzXml.this.getOutputMediaFile(uZModuleContext2.makeRealPath(uZModuleContext2.optString("filePath")), optString2));
                    UzXml.this.success(uZModuleContext);
                } catch (Exception e) {
                    UzXml.this.failed(uZModuleContext, e.getMessage());
                }
            }
        }.start();
    }

    public void base64ToFile(String str, File file) throws FileNotFoundException, IOException {
        byte[] decode = Base64.decode(str, 0);
        new FileOutputStream(file).write(decode, 0, decode.length);
    }

    public void jsmethod_decodeFileToBase64(final UZModuleContext uZModuleContext) {
        new Thread() {
            @Override
            public void run() {
                String optString = uZModuleContext.optString("filePath");
                if (TextUtils.isEmpty(optString)) {
                    return;
                }
                byte[] dataFromFile = UzXml.this.getDataFromFile(uZModuleContext, optString);
                if (dataFromFile != null) {
                    String encodeToString = Base64.encodeToString(dataFromFile, 0);
                    try {
                        JSONObject jSONObject = new JSONObject();
                        jSONObject.put(NotificationCompat.CATEGORY_STATUS, true);
                        jSONObject.put("base64Str", encodeToString);
                        uZModuleContext.success(jSONObject, false);
                        return;
                    } catch (JSONException e) {
                        e.printStackTrace();
                        return;
                    }
                }
                try {
                    JSONObject jSONObject2 = new JSONObject();
                    jSONObject2.put(NotificationCompat.CATEGORY_STATUS, false);
                    jSONObject2.put("base64Str", "");
                    uZModuleContext.success(jSONObject2, false);
                } catch (JSONException e2) {
                    e2.printStackTrace();
                }
            }
        }.start();
    }

    public void jsmethod_saveBase64Image(UZModuleContext uZModuleContext) {
        FileOutputStream fileOutputStream;
        String optString = uZModuleContext.optString("name");
        String optString2 = uZModuleContext.optString("base64str");
        FileOutputStream fileOutputStream2 = null;
        try {
            try {
                try {
                    fileOutputStream = new FileOutputStream(new File(Constants.makeDir(context()), optString));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (Exception e2) {
                e = e2;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            fileOutputStream.write(optString2.getBytes());
            fileOutputStream.close();
            callbackSuccess(uZModuleContext, optString2);
            fileOutputStream.close();
        } catch (Exception e3) {
            e = e3;
            fileOutputStream2 = fileOutputStream;
            e.printStackTrace();
            callbackFailed(uZModuleContext, e.getMessage());
            if (fileOutputStream2 != null) {
                fileOutputStream2.close();
            }
        } catch (Throwable th2) {
            th = th2;
            fileOutputStream2 = fileOutputStream;
            if (fileOutputStream2 != null) {
                try {
                    fileOutputStream2.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
            throw th;
        }
    }

    public void callbackSuccess(UZModuleContext uZModuleContext, String str) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put(NotificationCompat.CATEGORY_STATUS, true);
            if (!TextUtils.isEmpty(str)) {
                jSONObject.put("imageData", str);
            }
            uZModuleContext.success(jSONObject, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void callbackFailed(UZModuleContext uZModuleContext, String str) {
        JSONObject jSONObject = new JSONObject();
        JSONObject jSONObject2 = new JSONObject();
        try {
            jSONObject.put(NotificationCompat.CATEGORY_STATUS, false);
            jSONObject2.put(NotificationCompat.CATEGORY_MESSAGE, str);
            uZModuleContext.error(jSONObject, jSONObject2, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void jsmethod_getBase64Image(UZModuleContext uZModuleContext) {
        try {
            FileInputStream fileInputStream = new FileInputStream(new File(Constants.makeDir(context()), uZModuleContext.optString("name")));
            int available = fileInputStream.available();
            byte[] bArr = new byte[available];
            fileInputStream.read(bArr, 0, available);
            callbackSuccess(uZModuleContext, new String(bArr));
        } catch (Exception e) {
            e.printStackTrace();
            callbackFailed(uZModuleContext, e.getMessage());
        }
    }

    public void jsmethod_deleteBase64Image(UZModuleContext uZModuleContext) {
        if (new File(Constants.makeDir(context()), uZModuleContext.optString("name")).delete()) {
            callbackSuccess(uZModuleContext, null);
        } else {
            callbackFailed(uZModuleContext, "delete failed");
        }
    }
}