package com.mstar.android.samba;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import com.mstar.android.storage.MStorageManager;
import e1.AbstractC0453a;
import i1.g;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import k1.C0599q;
import k1.V;
import k1.W;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes.dex */
public class SmbDevice implements Parcelable {
    public static final Parcelable.Creator<SmbDevice> CREATOR = new Parcelable.Creator<SmbDevice>() { // from class: com.mstar.android.samba.SmbDevice.1
        /* JADX WARN: Can't rename method to resolve collision */
        @Override // android.os.Parcelable.Creator
        public SmbDevice createFromParcel(Parcel parcel) {
            return new SmbDevice(parcel);
        }

        /* JADX WARN: Can't rename method to resolve collision */
        @Override // android.os.Parcelable.Creator
        public SmbDevice[] newArray(int i2) {
            return new SmbDevice[i2];
        }
    };
    private static final String DEF_SMB_DEV_NAME = "Default Smb Device";
    public static final int FLAG_WRITEABLE = 1;
    private static final String TAG = "SmbDevice";
    private static final String localpath = "/mnt/samba/";
    private String CurrentPath;
    private boolean DEBUG;
    private String ParentPath;
    private String Path;
    private String SmbURL;
    private SmbAuthentication auth;
    ArrayList<SmbShareFolder> folderList;
    private String ip;
    private boolean mHasPassWord;
    private boolean mMountedFlag;
    private String mName;
    private String mPassWord;
    private String mUser;
    private boolean mountPointByIp;
    private OnRecvMsg onRecvMsg;
    W root;
    private MStorageManager stm;

    public SmbDevice() {
        this.folderList = new ArrayList<>();
        this.onRecvMsg = null;
        this.SmbURL = null;
        this.Path = null;
        this.CurrentPath = null;
        this.ParentPath = null;
        this.auth = null;
        this.mountPointByIp = false;
        this.mMountedFlag = false;
        this.mHasPassWord = false;
        this.DEBUG = true;
        this.ip = HttpUrl.FRAGMENT_ENCODE_SET;
        this.mUser = HttpUrl.FRAGMENT_ENCODE_SET;
        this.mPassWord = HttpUrl.FRAGMENT_ENCODE_SET;
        this.mName = DEF_SMB_DEV_NAME;
        AbstractC0453a.k("jcifs.encoding", "GBK");
    }

    static void convertToSambaFolder(SmbShareFolder smbShareFolder, W w2) throws V {
        smbShareFolder.file = w2;
    }

    private String getServerName() {
        try {
            g gVarJ = g.j(this.ip);
            if (gVarJ == null || !gVarJ.u()) {
                return null;
            }
            for (g gVar : g.i(gVarJ)) {
                if (!gVar.v() && gVar.s() == 0 && gVar.o() != null) {
                    return gVar.o();
                }
            }
            return null;
        } catch (UnknownHostException e2) {
            e2.printStackTrace();
            return null;
        }
    }

    private boolean isChinese(String str) {
        return str.length() < str.getBytes().length;
    }

    public String GetCurrentPath() {
        String str = this.Path;
        if (str != null) {
            return str;
        }
        return null;
    }

    public String GetParentPath() {
        String str = this.ParentPath;
        if (str != null) {
            return str;
        }
        return null;
    }

    public void SetParent(String str) {
        this.ParentPath = str;
    }

    public void SetPath(String str) {
        this.Path = str;
    }

    @Override // android.os.Parcelable
    public int describeContents() {
        return 0;
    }

    public ArrayList<SmbShareFolder> enterDirectory(String str) {
        C0599q c0599q;
        String strSubstring;
        if (str == null) {
            return null;
        }
        ArrayList<SmbShareFolder> arrayList = new ArrayList<>();
        this.CurrentPath = GetCurrentPath();
        PrintStream printStream = System.out;
        printStream.println("enterDirectory CurrentPath " + this.CurrentPath);
        String str2 = this.CurrentPath + str + "/";
        this.SmbURL = str2;
        SetPath(str2);
        SmbAuthentication smbAuthentication = this.auth;
        if (smbAuthentication != null) {
            String name = smbAuthentication.getName();
            String password = this.auth.getPassword();
            int iIndexOf = name.indexOf("/");
            if (iIndexOf > 0) {
                name = this.auth.getName().substring(iIndexOf + 1);
                strSubstring = this.auth.getName().substring(0, iIndexOf);
                c0599q = new C0599q(strSubstring + ";" + name + ":" + password);
            } else {
                c0599q = new C0599q(name + ":" + password);
                strSubstring = null;
            }
            printStream.println(" NtlmPasswordAuthentication = " + strSubstring + " " + name);
        } else {
            c0599q = null;
        }
        try {
            try {
                printStream.println("enterDirectory SmbURL " + this.SmbURL);
            } catch (MalformedURLException e2) {
                e2.printStackTrace();
            }
        } catch (V e3) {
            int iC = e3.c();
            System.out.println(iC);
            OnRecvMsg onRecvMsg = this.onRecvMsg;
            if (onRecvMsg != null) {
                onRecvMsg.onRecvMsg(iC);
            }
            e3.printStackTrace();
        }
        if (this.SmbURL == null) {
            printStream.println(" SmbURL == null");
            return null;
        }
        if (this.auth == null) {
            this.root = new W(this.SmbURL);
        } else {
            this.root = new W(this.SmbURL, c0599q);
        }
        SetParent(this.root.u());
        W[] wArrL = this.root.L();
        printStream.println("enterDirectory list1 number: " + wArrL.length);
        for (int i2 = 0; i2 < wArrL.length; i2++) {
            if (isHost(wArrL[i2])) {
                SmbShareFolder smbShareFolder = new SmbShareFolder(this);
                convertToSambaFolder(smbShareFolder, wArrL[i2]);
                arrayList.add(smbShareFolder);
            }
            PrintStream printStream2 = System.out;
            printStream2.println("enterDirectory get type :" + wArrL[i2].y());
            if (wArrL[i2].y() == 2) {
                try {
                    W[] wArrL2 = wArrL[i2].L();
                    printStream2.println("enterDirectory list2 number: " + wArrL2.length);
                    for (int i3 = 0; i3 < wArrL2.length; i3++) {
                        if (isHost(wArrL2[i3])) {
                            SmbShareFolder smbShareFolder2 = new SmbShareFolder(this);
                            convertToSambaFolder(smbShareFolder2, wArrL2[i3]);
                            arrayList.add(smbShareFolder2);
                        }
                    }
                } catch (V e4) {
                    int iC2 = e4.c();
                    OnRecvMsg onRecvMsg2 = this.onRecvMsg;
                    if (onRecvMsg2 != null) {
                        onRecvMsg2.onRecvMsg(iC2);
                    }
                    System.out.println("workgroup :" + iC2);
                }
            }
        }
        if (arrayList.isEmpty()) {
            System.out.println("enterParent rootList --------------------NULL : ");
            return null;
        }
        System.out.println("enterDirectory rootList : " + arrayList.size());
        return arrayList;
    }

    public ArrayList<SmbShareFolder> enterParent() {
        C0599q c0599q;
        String strSubstring;
        ArrayList<SmbShareFolder> arrayList = new ArrayList<>();
        this.SmbURL = GetParentPath();
        PrintStream printStream = System.out;
        printStream.println("enterParent = " + this.SmbURL);
        SetPath(this.SmbURL);
        SmbAuthentication smbAuthentication = this.auth;
        if (smbAuthentication != null) {
            String name = smbAuthentication.getName();
            String password = this.auth.getPassword();
            int iIndexOf = name.indexOf("/");
            if (iIndexOf > 0) {
                name = this.auth.getName().substring(iIndexOf + 1);
                strSubstring = this.auth.getName().substring(0, iIndexOf);
                c0599q = new C0599q(strSubstring + ";" + name + ":" + password);
            } else {
                c0599q = new C0599q(name + ":" + password);
                strSubstring = null;
            }
            printStream.println("enterParent NtlmPasswordAuthentication = " + strSubstring + " " + name);
        } else {
            c0599q = null;
        }
        try {
            try {
                printStream.println("enterParent SmbURL " + this.SmbURL);
            } catch (V e2) {
                int iC = e2.c();
                System.out.println(iC);
                OnRecvMsg onRecvMsg = this.onRecvMsg;
                if (onRecvMsg != null) {
                    onRecvMsg.onRecvMsg(iC);
                }
                e2.printStackTrace();
            }
        } catch (MalformedURLException e3) {
            e3.printStackTrace();
        }
        if (this.SmbURL == null) {
            printStream.println(" SmbURL == null");
            return null;
        }
        if (this.auth == null) {
            this.root = new W(this.SmbURL);
        } else {
            this.root = new W(this.SmbURL, c0599q);
        }
        SetParent(this.root.u());
        W[] wArrL = this.root.L();
        printStream.println("enterParent list1 number: " + wArrL.length);
        for (int i2 = 0; i2 < wArrL.length; i2++) {
            if (isHost(wArrL[i2])) {
                SmbShareFolder smbShareFolder = new SmbShareFolder(this);
                convertToSambaFolder(smbShareFolder, wArrL[i2]);
                arrayList.add(smbShareFolder);
            }
            PrintStream printStream2 = System.out;
            printStream2.println("enterParent list2 URL: " + wArrL[i2].getURL());
            if (wArrL[i2].y() == 2) {
                try {
                    W[] wArrL2 = wArrL[i2].L();
                    printStream2.println("list2 number: " + wArrL2.length);
                    for (int i3 = 0; i3 < wArrL2.length; i3++) {
                        if (isHost(wArrL2[i3])) {
                            SmbShareFolder smbShareFolder2 = new SmbShareFolder(this);
                            convertToSambaFolder(smbShareFolder2, wArrL2[i3]);
                            arrayList.add(smbShareFolder2);
                        }
                    }
                } catch (V e4) {
                    int iC2 = e4.c();
                    OnRecvMsg onRecvMsg2 = this.onRecvMsg;
                    if (onRecvMsg2 != null) {
                        onRecvMsg2.onRecvMsg(iC2);
                    }
                    System.out.println("workgroup :" + iC2);
                }
            }
        }
        if (arrayList.isEmpty()) {
            System.out.println("enterParent parentList --------------------NULL : ");
            return null;
        }
        System.out.println("enterParent parentList : " + arrayList.size());
        return arrayList;
    }

    public String getAddress() {
        return this.ip;
    }

    public SmbAuthentication getAuth() {
        return !this.mHasPassWord ? SmbAuthentication.ANONYMOUS : this.auth;
    }

    public String getHostName() throws UnknownHostException {
        String serverName = getServerName();
        this.mName = serverName;
        if (serverName == null) {
            this.mName = InetAddress.getByName(this.ip).getHostName();
        }
        System.out.println(" hostName : " + this.mName);
        return this.mName;
    }

    public ArrayList<SmbShareFolder> getSharefolderList() {
        C0599q c0599q;
        PrintStream printStream;
        String strSubstring;
        String str = "smb://" + this.ip + "/";
        this.SmbURL = str;
        SetPath(str);
        SmbAuthentication smbAuthentication = this.auth;
        if (smbAuthentication != null) {
            String name = smbAuthentication.getName();
            String password = this.auth.getPassword();
            int iIndexOf = name.indexOf("/");
            if (iIndexOf > 0) {
                name = this.auth.getName().substring(iIndexOf + 1);
                strSubstring = this.auth.getName().substring(0, iIndexOf);
                c0599q = new C0599q(strSubstring + ";" + name + ":" + password);
            } else {
                c0599q = new C0599q(name + ":" + password);
                strSubstring = null;
            }
            System.out.println("  getSharefolderList NtlmPasswordAuthentication = " + strSubstring + " " + name);
        } else {
            c0599q = null;
        }
        try {
            try {
                printStream = System.out;
                printStream.println(" getSharefolderList SmbURL " + this.SmbURL);
            } catch (V e2) {
                int iC = e2.c();
                System.out.println(iC);
                OnRecvMsg onRecvMsg = this.onRecvMsg;
                if (onRecvMsg != null) {
                    onRecvMsg.onRecvMsg(iC);
                }
                e2.printStackTrace();
            }
        } catch (MalformedURLException e3) {
            e3.printStackTrace();
        }
        if (this.SmbURL == null) {
            printStream.println(" SmbURL == null");
            return null;
        }
        if (this.auth == null) {
            this.root = new W(this.SmbURL);
        } else {
            this.root = new W(this.SmbURL, c0599q);
        }
        W[] wArrL = this.root.L();
        printStream.println("getSharefolderList list1 number: " + wArrL.length);
        this.folderList.clear();
        for (int i2 = 0; i2 < wArrL.length; i2++) {
            if (isHost(wArrL[i2])) {
                SmbShareFolder smbShareFolder = new SmbShareFolder(this);
                convertToSambaFolder(smbShareFolder, wArrL[i2]);
                this.folderList.add(smbShareFolder);
            }
            PrintStream printStream2 = System.out;
            printStream2.println("getSharefolderList get type :" + wArrL[i2].y());
            if (wArrL[i2].y() == 2) {
                try {
                    W[] wArrL2 = wArrL[i2].L();
                    printStream2.println("getSharefolderList list2 number: " + wArrL2.length);
                    for (int i3 = 0; i3 < wArrL2.length; i3++) {
                        if (isHost(wArrL2[i3])) {
                            SmbShareFolder smbShareFolder2 = new SmbShareFolder(this);
                            convertToSambaFolder(smbShareFolder2, wArrL2[i3]);
                            this.folderList.add(smbShareFolder2);
                        }
                    }
                } catch (V e4) {
                    int iC2 = e4.c();
                    OnRecvMsg onRecvMsg2 = this.onRecvMsg;
                    if (onRecvMsg2 != null) {
                        onRecvMsg2.onRecvMsg(iC2);
                    }
                    System.out.println("workgroup :" + iC2);
                }
            }
        }
        if (this.folderList.isEmpty()) {
            OnRecvMsg onRecvMsg3 = this.onRecvMsg;
            if (onRecvMsg3 != null) {
                onRecvMsg3.onRecvMsg(OnRecvMsg.NT_STATUS_NOT_FOUND);
            }
        } else {
            System.out.println("getSharefolderList folderList : " + this.folderList.size());
            OnRecvMsg onRecvMsg4 = this.onRecvMsg;
            if (onRecvMsg4 != null) {
                onRecvMsg4.onRecvMsg(0);
            }
        }
        return this.folderList;
    }

    public boolean hasPassword() {
        try {
            W w2 = new W("smb://" + this.ip + "/", new C0599q(null));
            W[] wArrL = w2.L();
            if (!w2.p()) {
                return true;
            }
            Log.d(TAG, "hasPassword : smb server has no password!");
            for (W w3 : wArrL) {
                if (!w3.v().endsWith("$/")) {
                    Log.d(TAG, "hasPassword : Test shared folder: " + w3.v());
                    new W(w3.v(), new C0599q(null)).J();
                }
            }
            return false;
        } catch (Exception unused) {
            Log.d(TAG, this.mName + ": hasPassword : yes !");
            return true;
        }
    }

    public boolean isActive() {
        g gVarJ;
        try {
            String str = this.ip;
            if (str == null || (gVarJ = g.j(str)) == null) {
                return false;
            }
            return gVarJ.u();
        } catch (UnknownHostException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    public boolean isHost(W w2) throws V {
        int iY = w2.y();
        return !w2.E() && (iY == 1 || iY == 8 || iY == 4);
    }

    public boolean isMounted() {
        return this.mMountedFlag;
    }

    public String localPath() {
        if (!this.mountPointByIp) {
            return localpath;
        }
        return localpath + this.ip + "/";
    }

    public void mount(SmbAuthentication smbAuthentication) {
        boolean zMountSamba;
        Iterator<SmbShareFolder> it = this.folderList.iterator();
        while (it.hasNext()) {
            String fileName = it.next().getFileName();
            PrintStream printStream = System.out;
            printStream.println(" mount " + this.ip + " " + fileName);
            if (isChinese(fileName)) {
                char[] charArray = fileName.toCharArray();
                printStream.println(" datas.length " + charArray.length);
                int i2 = 0;
                while (true) {
                    if (i2 >= charArray.length) {
                        break;
                    }
                    if (charArray[i2] == 0) {
                        fileName = String.copyValueOf(charArray, 0, i2);
                        System.out.println(" folderName " + fileName);
                        break;
                    }
                    i2++;
                }
            }
            String str = fileName;
            SmbAuthentication smbAuthentication2 = this.auth;
            if (smbAuthentication2 == null) {
                zMountSamba = this.stm.mountSamba(this.ip, str, str, " ", HttpUrl.FRAGMENT_ENCODE_SET);
            } else {
                zMountSamba = this.stm.mountSamba(this.ip, str, str, smbAuthentication2.getName(), smbAuthentication.getPassword());
            }
            if (zMountSamba) {
                this.mMountedFlag = true;
                this.mountPointByIp = false;
            } else {
                System.out.println(" mount jcifs fail! ");
            }
        }
    }

    public String remotePath() {
        return "//" + this.ip;
    }

    public void setAddress(String str) {
        this.ip = str;
    }

    public void setAuth(SmbAuthentication smbAuthentication) {
        this.auth = smbAuthentication;
        if (smbAuthentication != null) {
            this.mUser = smbAuthentication.getName();
            String password = smbAuthentication.getPassword();
            this.mPassWord = password;
            if (password != null) {
                this.mHasPassWord = true;
            }
        }
    }

    public void setHostName(String str) {
        this.mName = str;
    }

    public void setOnRecvMsg(OnRecvMsg onRecvMsg) {
        this.onRecvMsg = onRecvMsg;
    }

    public void setStorageManager(MStorageManager mStorageManager) {
        this.stm = mStorageManager;
    }

    public boolean testPassword(String str, String str2) {
        try {
            if (this.ip == null) {
                if (this.DEBUG) {
                    Log.d(TAG, "testPassword --> Ip is null!");
                }
                return false;
            }
            W w2 = new W("smb://" + this.ip + "/", new C0599q(str + ":" + str2));
            W[] wArrL = w2.L();
            if (!w2.p()) {
                return false;
            }
            for (W w3 : wArrL) {
                if (!w3.v().endsWith("$/")) {
                    Log.d(TAG, "hasPassword : Test shared folder: " + w3.v());
                    new W(w3.v(), new C0599q(str + ":" + str2)).J();
                }
            }
            Log.d(TAG, "testPassword : password is correct!");
            return true;
        } catch (Exception e2) {
            Log.d(TAG, "testPassword : password is wrong!");
            e2.printStackTrace();
            return false;
        }
    }

    public int unmount() {
        boolean zUnmountSamba = false;
        for (SmbShareFolder smbShareFolder : this.folderList) {
            PrintStream printStream = System.out;
            printStream.println(" umount " + smbShareFolder.getFileName());
            String fileName = smbShareFolder.getFileName();
            if (isChinese(fileName)) {
                char[] charArray = fileName.toCharArray();
                printStream.println(" datas.length " + charArray.length);
                int i2 = 0;
                while (true) {
                    if (i2 >= charArray.length) {
                        break;
                    }
                    if (charArray[i2] == 0) {
                        fileName = String.copyValueOf(charArray, 0, i2);
                        System.out.println(" folderName " + fileName);
                        break;
                    }
                    i2++;
                }
            }
            zUnmountSamba = this.mountPointByIp ? this.stm.unmountSamba(this.ip + "/" + fileName, true) : this.stm.unmountSamba(fileName, true);
            if (zUnmountSamba) {
                this.mMountedFlag = false;
            } else {
                System.out.println(" umount jcifs fail! ");
            }
        }
        return zUnmountSamba ? 18 : 19;
    }

    @Override // android.os.Parcelable
    public void writeToParcel(Parcel parcel, int i2) {
        parcel.writeString(this.ip);
        parcel.writeString(this.mUser);
        parcel.writeString(this.mPassWord);
        parcel.writeString(this.mName);
    }

    public SmbDevice(String str) {
        this.folderList = new ArrayList<>();
        this.onRecvMsg = null;
        this.SmbURL = null;
        this.Path = null;
        this.CurrentPath = null;
        this.ParentPath = null;
        this.auth = null;
        this.mountPointByIp = false;
        this.mMountedFlag = false;
        this.mHasPassWord = false;
        this.DEBUG = true;
        this.ip = str;
        this.mUser = HttpUrl.FRAGMENT_ENCODE_SET;
        this.mPassWord = HttpUrl.FRAGMENT_ENCODE_SET;
        this.mName = DEF_SMB_DEV_NAME;
        AbstractC0453a.k("jcifs.encoding", "GBK");
    }

    public int mount(SmbAuthentication smbAuthentication, int i2) {
        boolean zMountSamba;
        Iterator<SmbShareFolder> it = this.folderList.iterator();
        while (it.hasNext()) {
            String fileName = it.next().getFileName();
            PrintStream printStream = System.out;
            printStream.println(" mount " + this.ip + " " + fileName);
            if (isChinese(fileName)) {
                char[] charArray = fileName.toCharArray();
                printStream.println(" datas.length " + charArray.length);
                int i3 = 0;
                while (true) {
                    if (i3 >= charArray.length) {
                        break;
                    }
                    if (charArray[i3] == 0) {
                        fileName = String.copyValueOf(charArray, 0, i3);
                        System.out.println(" folderName " + fileName);
                        break;
                    }
                    i3++;
                }
            }
            String str = fileName;
            SmbAuthentication smbAuthentication2 = this.auth;
            if (smbAuthentication2 == null) {
                zMountSamba = this.stm.mountSamba(this.ip, str, this.ip + "/" + str, " ", HttpUrl.FRAGMENT_ENCODE_SET);
            } else {
                String name = smbAuthentication2.getName();
                int iIndexOf = name.indexOf("\\");
                if (iIndexOf > 0) {
                    name = this.auth.getName().substring(iIndexOf + 1);
                }
                MStorageManager mStorageManager = this.stm;
                String str2 = this.ip;
                zMountSamba = mStorageManager.mountSamba(str2, str, this.ip + "/" + str, name, smbAuthentication.getPassword());
            }
            if (!zMountSamba) {
                System.out.println(" mount jcifs fail! ");
            } else {
                this.mMountedFlag = true;
                this.mountPointByIp = true;
            }
        }
        return this.mMountedFlag ? 16 : 17;
    }

    public SmbDevice(Parcel parcel) {
        this.folderList = new ArrayList<>();
        this.ip = null;
        this.mUser = HttpUrl.FRAGMENT_ENCODE_SET;
        this.mPassWord = HttpUrl.FRAGMENT_ENCODE_SET;
        this.mName = DEF_SMB_DEV_NAME;
        this.onRecvMsg = null;
        this.SmbURL = null;
        this.Path = null;
        this.CurrentPath = null;
        this.ParentPath = null;
        this.auth = null;
        this.mountPointByIp = false;
        this.mMountedFlag = false;
        this.mHasPassWord = false;
        this.DEBUG = true;
        this.ip = parcel.readString();
        this.mUser = parcel.readString();
        this.mPassWord = parcel.readString();
        this.mName = parcel.readString();
        this.auth = new SmbAuthentication(this.mUser, this.mPassWord);
        AbstractC0453a.k("jcifs.encoding", "GBK");
    }
}
