中通快递.apk(点击下载) / AddressManager.java


package cn.beekee.zhongtong.util;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import cn.beekee.zhongtong.database.DatabaseAdrressHelper;
import cn.beekee.zhongtong.util.net.JsonOperate;
import cn.beekee.zhongtong.util.net.NetOperate;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.lang.reflect.Array;

public class AddressManager {
    private static final String ADDRESS_FILE = "address.txt";
    private static final String CITY_URL = "http://usercenter.zt-express.com/UserCenterV42/Area.ashx?Function=GetListByParent&parentId=";
    private static final String DISTRICT_URL = "http://usercenter.zt-express.com/UserCenterV42/Area.ashx?Function=GetListByParent&parentId=";
    public static final int FLAG_SEARCH_CITY = 4;
    public static final int FLAG_SEARCH_CITY_CODE = 7;
    public static final int FLAG_SEARCH_DISTRUCT = 5;
    public static final int FLAG_SEARCH_DIS_CODE = 8;
    public static final int FLAG_SEARCH_PROVINCE = 3;
    public static final int FLAG_SEARCH__PROVINCE_CODE = 6;
    public static final int FLAG_UPADATE_PRE = 1;
    public static final int FLAG_UPDATE_NET = 2;
    private static final String PROVINCE_URL = "http://usercenter.zt-express.com/UserCenterV42/Area.ashx?Function=GetProvinceList";
    private DatabaseAdrressHelper mAddressDatabase;
    Context mContext;
    private IAddressOpeListener mListener;

    public interface IAddressOpeListener {
        void onAddressOver(int i, Object obj);
    }

    public AddressManager(Context context, IAddressOpeListener listener) {
        this.mListener = listener;
        this.mContext = context;
        this.mAddressDatabase = DatabaseAdrressHelper.getInstance(context);
    }

    public void updateDataPre() {
        new AddressTask(1).execute(DatabaseAdrressHelper.TYPE_GET_PROVINCE);
    }

    public void updateDataNet() {
        new AddressTask(2).execute(DatabaseAdrressHelper.TYPE_GET_PROVINCE);
    }

    public void getProvinceArray(String country) {
        new AddressTask(3).execute(country);
    }

    public void getCityArray(String province) {
        new AddressTask(4).execute(province);
    }

    public void getDistrictArray(String city) {
        new AddressTask(5).execute(city);
    }

    public void getProvinceCode(String name) {
        new AddressTask(6).execute(DatabaseAdrressHelper.TYPE_GET_PROVINCE, name);
    }

    public void getCityCode(String parentName, String name) {
        new AddressTask(7).execute(parentName, name);
    }

    public void getDistrictCode(String parentName, String name) {
        new AddressTask(8).execute(parentName, name);
    }

    /* access modifiers changed from: private */
    public class AddressTask extends AsyncTask<String, Void, Object> {
        ProgressDialog mDialog;
        private final int mFlag;

        /* access modifiers changed from: protected */
        public void onPreExecute() {
            super.onPreExecute();
            if (this.mFlag == 1 || this.mFlag == 2) {
                this.mDialog = new ProgressDialog(AddressManager.this.mContext, 0);
                this.mDialog.setTitle("请稍等");
                this.mDialog.setMessage("数据初始化中...");
                this.mDialog.setCancelable(false);
                this.mDialog.show();
            }
        }

        public AddressTask(int flag) {
            this.mFlag = flag;
        }

        /* access modifiers changed from: protected */
        public Object doInBackground(String... params) {
            boolean initResult = false;
            if (!MySharePreference.getHasAddress(AddressManager.this.mContext)) {
                AddressManager.this.mAddressDatabase.clearTable();
                initResult = new FileNetAddress().writeDataBase(AddressManager.this.mContext);
                if (initResult) {
                    MySharePreference.setHasAddress(AddressManager.this.mContext, true);
                }
            }
            switch (this.mFlag) {
                case 1:
                    if (!initResult) {
                        AddressManager.this.mAddressDatabase.clearTable();
                        initResult = new FileNetAddress().writeDataBase(AddressManager.this.mContext);
                    }
                    return Boolean.valueOf(initResult);
                case 2:
                    AddressManager.this.mAddressDatabase.clearTable();
                    return Boolean.valueOf(new NetAddress().createProvinceList(params[0]));
                case 3:
                case 4:
                case 5:
                    return adrStringTrans(AddressManager.this.mAddressDatabase.getValue(params[0]));
                case 6:
                case 7:
                case 8:
                    return adrTranCode(AddressManager.this.mAddressDatabase.getValue(params[0]), params[1]);
                default:
                    return null;
            }
        }

        private String[][] adrStringTrans(String stringArr) {
            Log.e("", "查到:" + stringArr);
            if (TextUtils.isEmpty(stringArr)) {
                return null;
            }
            String[] obj = stringArr.split(";");
            String[][] result = (String[][]) Array.newInstance(String.class, 2, obj.length);
            for (int i = 0; i < obj.length; i++) {
                String[] dataStrings = obj[i].split(",");
                result[0][i] = dataStrings[0];
                result[1][i] = dataStrings[1];
            }
            return result;
        }

        private String adrTranCode(String districtString, String districtName) {
            int index;
            Log.e("", "内容:" + districtString + "\n需要查的:" + districtName);
            if (districtString == null || districtName == null || (index = districtString.indexOf(districtName)) <= -1) {
                return null;
            }
            return districtString.substring(districtName.length() + index + 1, districtName.length() + index + 1 + 6);
        }

        /* access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        public void onPostExecute(Object result) {
            super.onPostExecute(result);
            if (this.mDialog != null && this.mDialog.isShowing()) {
                this.mDialog.dismiss();
            }
            AddressManager.this.mListener.onAddressOver(this.mFlag, result);
        }
    }

    /* access modifiers changed from: private */
    public class NetAddress {
        private NetAddress() {
        }

        public boolean createProvinceList(String parent) {
            String data = NetOperate.getUrlDataApach(AddressManager.PROVINCE_URL);
            if (data == null) {
                return false;
            }
            String provicesString = JsonOperate.decodeAdrPri(data);
            System.out.println("中国:" + provicesString);
            AddressManager.this.mAddressDatabase.insertValue(parent, provicesString);
            for (String string : provicesString.split(";")) {
                createCityList(string.split(","), DatabaseAdrressHelper.TYPE_GET_PROVINCE);
            }
            return true;
        }

        private void createCityList(String[] province, String parent) {
            String citysString = JsonOperate.decodeAdrPri(NetOperate.getUrlDataApach("http://usercenter.zt-express.com/UserCenterV42/Area.ashx?Function=GetListByParent&parentId=" + province[1]));
            System.out.println(parent + province[0] + ":" + citysString);
            AddressManager.this.mAddressDatabase.insertValue(parent + province[0], citysString);
            String[] citysArray = citysString.split(";");
            int len$ = citysArray.length;
            for (int i$ = 0; i$ < len$; i$++) {
                createDistrict(citysArray[i$].split(","), parent + province[0]);
            }
        }

        private void createDistrict(String[] city, String parent) {
            String districtsString = JsonOperate.decodeAdrPri(NetOperate.getUrlDataApach("http://usercenter.zt-express.com/UserCenterV42/Area.ashx?Function=GetListByParent&parentId=" + city[1]));
            System.out.println(parent + city[0] + ":" + districtsString);
            AddressManager.this.mAddressDatabase.insertValue(parent + city[0], districtsString);
        }
    }

    public class FileNetAddress {
        private static final String FILE_NAME = "address.txt";

        public FileNetAddress() {
        }

        public boolean writeChina() {
            return writeProvince(DatabaseAdrressHelper.TYPE_GET_PROVINCE, AddressManager.PROVINCE_URL);
        }

        private boolean writeProvince(String provinceKey, String provinceUrl) {
            String provinsString;
            String provinsJson = NetOperate.getUrlDataApach(provinceUrl);
            if (provinsJson == null || (provinsString = JsonOperate.decodeAdrPri(provinsJson)) == null || !writeFile(provinceKey) || !writeFile(provinsString)) {
                return false;
            }
            for (String province : provinsString.split(";")) {
                String[] provinceContent = province.split(",");
                if (!writeCity(provinceKey + provinceContent[0], "http://usercenter.zt-express.com/UserCenterV42/Area.ashx?Function=GetListByParent&parentId=" + provinceContent[1])) {
                    return false;
                }
            }
            return true;
        }

        private boolean writeCity(String cityKey, String cityUrl) {
            String citysString;
            String citysJson = NetOperate.getUrlDataApach(cityUrl);
            if (citysJson == null || (citysString = JsonOperate.decodeAdrPri(citysJson)) == null || !writeFile(cityKey) || !writeFile(citysString)) {
                return false;
            }
            for (String district : citysString.split(";")) {
                String[] districtContent = district.split(",");
                if (!writeDistrict(cityKey + districtContent[0], "http://usercenter.zt-express.com/UserCenterV42/Area.ashx?Function=GetListByParent&parentId=" + districtContent[1])) {
                    return false;
                }
            }
            return true;
        }

        private boolean writeDistrict(String districtKey, String districtUrl) {
            String districtString;
            String districtsJson = NetOperate.getUrlDataApach(districtUrl);
            if (districtsJson != null && (districtString = JsonOperate.decodeAdrPri(districtsJson)) != null && writeFile(districtKey) && writeFile(districtString)) {
                return true;
            }
            return false;
        }

        /* JADX WARNING: Removed duplicated region for block: B:17:0x002f A[SYNTHETIC, Splitter:B:17:0x002f] */
        /* JADX WARNING: Removed duplicated region for block: B:23:0x003b A[SYNTHETIC, Splitter:B:23:0x003b] */
        /* JADX WARNING: Removed duplicated region for block: B:33:? A[RETURN, SYNTHETIC] */
        private boolean writeFile(String data) {
            Throwable th;
            IOException e;
            if (data == null) {
                return false;
            }
            RandomAccessFile accessFile = null;
            try {
                System.out.println(data);
                RandomAccessFile accessFile2 = new RandomAccessFile(FILE_NAME, "rw");
                try {
                    accessFile2.seek(accessFile2.length());
                    accessFile2.writeUTF(data);
                    if (accessFile2 != null) {
                        try {
                            accessFile2.close();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                    }
                    return true;
                } catch (IOException e3) {
                    e = e3;
                    accessFile = accessFile2;
                    try {
                        e.printStackTrace();
                        if (accessFile != null) {
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        if (accessFile != null) {
                            try {
                                accessFile.close();
                            } catch (IOException e4) {
                                e4.printStackTrace();
                            }
                        }
                        throw th;
                    }
                } catch (Throwable th3) {
                    th = th3;
                    accessFile = accessFile2;
                    if (accessFile != null) {
                    }
                    throw th;
                }
            } catch (IOException e5) {
                e = e5;
                e.printStackTrace();
                if (accessFile != null) {
                    return false;
                }
                try {
                    accessFile.close();
                    return false;
                } catch (IOException e6) {
                    e6.printStackTrace();
                    return false;
                }
            }
        }

        /* JADX WARNING: Removed duplicated region for block: B:14:0x003a A[SYNTHETIC, Splitter:B:14:0x003a] */
        /* JADX WARNING: Removed duplicated region for block: B:29:0x005a A[SYNTHETIC, Splitter:B:29:0x005a] */
        /* JADX WARNING: Removed duplicated region for block: B:35:0x0066 A[SYNTHETIC, Splitter:B:35:0x0066] */
        public String readFile() {
            Throwable th;
            IOException e;
            RandomAccessFile mAccessFile = null;
            StringBuilder result = new StringBuilder();
            try {
                RandomAccessFile mAccessFile2 = new RandomAccessFile(FILE_NAME, "r");
                while (true) {
                    try {
                        String readContent = mAccessFile2.readUTF();
                        if ("".equals(readContent)) {
                            break;
                        }
                        result.append(readContent + "\n");
                    } catch (FileNotFoundException e2) {
                        e = e2;
                        mAccessFile = mAccessFile2;
                        try {
                            e.printStackTrace();
                            if (mAccessFile != null) {
                            }
                            return null;
                        } catch (Throwable th2) {
                            th = th2;
                            if (mAccessFile != null) {
                                try {
                                    mAccessFile.close();
                                } catch (IOException e3) {
                                    e3.printStackTrace();
                                }
                            }
                            throw th;
                        }
                    } catch (IOException e4) {
                        e = e4;
                        mAccessFile = mAccessFile2;
                        e.printStackTrace();
                        if (mAccessFile != null) {
                        }
                        return null;
                    } catch (Throwable th3) {
                        th = th3;
                        mAccessFile = mAccessFile2;
                        if (mAccessFile != null) {
                        }
                        throw th;
                    }
                }
                String sb = result.toString();
                if (mAccessFile2 != null) {
                    try {
                        mAccessFile2.close();
                    } catch (IOException e5) {
                        e5.printStackTrace();
                    }
                }
                return sb;
            } catch (FileNotFoundException e6) {
                e = e6;
                e.printStackTrace();
                if (mAccessFile != null) {
                    try {
                        mAccessFile.close();
                    } catch (IOException e7) {
                        e7.printStackTrace();
                    }
                }
                return null;
            } catch (IOException e8) {
                e = e8;
                e.printStackTrace();
                if (mAccessFile != null) {
                    try {
                        mAccessFile.close();
                    } catch (IOException e9) {
                        e9.printStackTrace();
                    }
                }
                return null;
            }
        }

        /* JADX WARNING: Removed duplicated region for block: B:22:0x0045 A[SYNTHETIC, Splitter:B:22:0x0045] */
        /* JADX WARNING: Removed duplicated region for block: B:43:0x0078 A[SYNTHETIC, Splitter:B:43:0x0078] */
        /* JADX WARNING: Removed duplicated region for block: B:52:0x0087 A[SYNTHETIC, Splitter:B:52:0x0087] */
        /* JADX WARNING: Removed duplicated region for block: B:59:0x0098 A[SYNTHETIC, Splitter:B:59:0x0098] */
        /* JADX WARNING: Removed duplicated region for block: B:65:0x00a6 A[SYNTHETIC, Splitter:B:65:0x00a6] */
        public boolean writeDataBase(Context context) {
            Throwable th;
            InputStream inputStream;
            OutputStream outputStream;
            Throwable th2;
            IOException e;
            FileNotFoundException e2;
            boolean z = false;
            if (context != null) {
                OutputStream outputStream2 = null;
                try {
                    File file = new File(context.getCacheDir(), FILE_NAME);
                    try {
                        if (file.exists()) {
                            file.delete();
                        }
                        file.createNewFile();
                        inputStream = context.getAssets().open(FILE_NAME);
                        outputStream = new FileOutputStream(file);
                    } catch (IOException e3) {
                        e = e3;
                        try {
                            e.printStackTrace();
                            if (outputStream2 != null) {
                                try {
                                    outputStream2.close();
                                } catch (IOException e4) {
                                    e4.printStackTrace();
                                }
                            }
                            return z;
                        } catch (Throwable th3) {
                            th = th3;
                            if (outputStream2 != null) {
                            }
                            throw th;
                        }
                    } catch (Throwable th4) {
                        th = th4;
                        if (outputStream2 != null) {
                            try {
                                outputStream2.close();
                            } catch (IOException e5) {
                                e5.printStackTrace();
                            }
                        }
                        throw th;
                    }
                    try {
                        byte[] bytes = new byte[1024];
                        while (true) {
                            int length = inputStream.read(bytes);
                            if (length <= 0) {
                                break;
                            }
                            outputStream.write(bytes, 0, length);
                        }
                        if (outputStream != null) {
                            try {
                                outputStream.close();
                            } catch (IOException e6) {
                                e6.printStackTrace();
                            }
                        }
                        RandomAccessFile accessFile = null;
                        try {
                            RandomAccessFile accessFile2 = new RandomAccessFile(file, "r");
                            try {
                                z = AddressManager.this.mAddressDatabase.initInsertAddress(accessFile2);
                                if (accessFile2 != null) {
                                    try {
                                        accessFile2.close();
                                    } catch (IOException e7) {
                                        e7.printStackTrace();
                                    }
                                }
                            } catch (FileNotFoundException e8) {
                                e2 = e8;
                                accessFile = accessFile2;
                                try {
                                    e2.printStackTrace();
                                    if (accessFile != null) {
                                    }
                                    return z;
                                } catch (Throwable th5) {
                                    th2 = th5;
                                    if (accessFile != null) {
                                        try {
                                            accessFile.close();
                                        } catch (IOException e9) {
                                            e9.printStackTrace();
                                        }
                                    }
                                    throw th2;
                                }
                            } catch (IOException e10) {
                                e = e10;
                                accessFile = accessFile2;
                                e.printStackTrace();
                                if (accessFile != null) {
                                }
                                return z;
                            } catch (Throwable th6) {
                                th2 = th6;
                                accessFile = accessFile2;
                                if (accessFile != null) {
                                }
                                throw th2;
                            }
                        } catch (FileNotFoundException e11) {
                            e2 = e11;
                            e2.printStackTrace();
                            if (accessFile != null) {
                                try {
                                    accessFile.close();
                                } catch (IOException e12) {
                                    e12.printStackTrace();
                                }
                            }
                            return z;
                        } catch (IOException e13) {
                            e = e13;
                            e.printStackTrace();
                            if (accessFile != null) {
                                try {
                                    accessFile.close();
                                } catch (IOException e14) {
                                    e14.printStackTrace();
                                }
                            }
                            return z;
                        }
                    } catch (IOException e15) {
                        e = e15;
                        outputStream2 = outputStream;
                        e.printStackTrace();
                        if (outputStream2 != null) {
                        }
                        return z;
                    } catch (Throwable th7) {
                        th = th7;
                        outputStream2 = outputStream;
                        if (outputStream2 != null) {
                        }
                        throw th;
                    }
                } catch (IOException e16) {
                    e = e16;
                    e.printStackTrace();
                    if (outputStream2 != null) {
                    }
                    return z;
                }
            }
            return z;
        }
    }
}