package com.mstar.android.storage;

import android.app.ActivityThread;
import android.content.Context;
import android.os.FileUtils;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.storage.IISOActionListener;
import android.os.storage.IStorageManager;
import android.os.storage.StorageVolume;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes.dex */
public class MStorageManager {
    private static final String TAG = "MStorageManager";
    static MStorageManager mInstance;
    static final Object mInstanceSync = new Object();
    private static MStorageManager mMStorageManager;
    Context mContext;
    private ISOActionListener mISOActionListener = new ISOActionListener();
    private final AtomicInteger mNextNonce = new AtomicInteger(0);
    IStorageManager mService;
    Looper mTgtLooper;

    private class ISOListenerDelegate {
        private final Handler mHandler;
        private final OnISOEvnetListener mISOEvnetListener;
        private final int nonce;

        public ISOListenerDelegate(OnISOEvnetListener onISOEvnetListener) {
            this.nonce = MStorageManager.this.getNextNonce();
            this.mISOEvnetListener = onISOEvnetListener;
            this.mHandler = new Handler(MStorageManager.this.mTgtLooper) { // from class: com.mstar.android.storage.MStorageManager.ISOListenerDelegate.1
                @Override // android.os.Handler
                public void handleMessage(Message message) {
                    OnISOEvnetListener listener = ISOListenerDelegate.this.getListener();
                    if (listener == null) {
                        return;
                    }
                    StorageEvent storageEvent = (StorageEvent) message.obj;
                    if (message.what == 4) {
                        ISOStateChangedStorageEvent iSOStateChangedStorageEvent = (ISOStateChangedStorageEvent) storageEvent;
                        listener.onISOStateChange(iSOStateChangedStorageEvent.path, iSOStateChangedStorageEvent.state);
                    } else {
                        Log.e(MStorageManager.TAG, "Unsupported event " + message.what);
                    }
                }
            };
        }

        public OnISOEvnetListener getListener() {
            return this.mISOEvnetListener;
        }

        public void sendISOStateChanged(String str, int i2) {
            this.mHandler.sendMessage(MStorageManager.this.new ISOStateChangedStorageEvent(str, i2).getMessage());
        }
    }

    private class ISOStateChangedStorageEvent extends StorageEvent {
        public final String path;
        public final int state;

        public ISOStateChangedStorageEvent(String str, int i2) {
            super(4);
            this.path = str;
            this.state = i2;
        }
    }

    private class StorageEvent {
        static final int EVENT_ISO_STATE_CHANGED = 4;
        private Message mMessage;

        public StorageEvent(int i2) {
            Message messageObtain = Message.obtain();
            this.mMessage = messageObtain;
            messageObtain.what = i2;
            messageObtain.obj = this;
        }

        public Message getMessage() {
            return this.mMessage;
        }
    }

    private MStorageManager(IStorageManager iStorageManager, Context context) {
        this.mService = null;
        this.mContext = null;
        this.mService = iStorageManager;
        this.mTgtLooper = context.getMainLooper();
        this.mContext = context;
    }

    public static MStorageManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (mInstanceSync) {
                try {
                    if (mInstance == null) {
                        mInstance = new MStorageManager(IStorageManager.Stub.asInterface(ServiceManager.getService("mount")), context);
                    }
                } finally {
                }
            }
        }
        return mInstance;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public int getNextNonce() {
        return this.mNextNonce.getAndIncrement();
    }

    public boolean formatVolume(String str) {
        try {
            return this.mService.formatVolume(str) == 0;
        } catch (RemoteException e2) {
            Log.e(TAG, "Failed to format volume", e2);
            return false;
        }
    }

    public String getISOFileMountPath(String str) {
        if (str == null) {
            throw new IllegalArgumentException("filename cannot be null");
        }
        try {
            return this.mService.getISOFileMountPath(str);
        } catch (RemoteException e2) {
            Log.e(TAG, "Failed to get the mount path of the ISO file", e2);
            return null;
        }
    }

    public String[] getMountedISOFileList() {
        try {
            return this.mService.getMountedISOFileList();
        } catch (RemoteException e2) {
            Log.e(TAG, "Failed to get the mounted ISO files", e2);
            return null;
        }
    }

    public StorageVolume getStorageVolume(File file) {
        return getStorageVolume(getVolumeList(), file);
    }

    public String getVolumeLabel(String str) {
        StorageVolume storageVolume = getStorageVolume(new File(str));
        if (storageVolume == null || HttpUrl.FRAGMENT_ENCODE_SET.equals(storageVolume.getFsLabel())) {
            return null;
        }
        return storageVolume.getFsLabel();
    }

    public StorageVolume[] getVolumeList() {
        return getVolumeList(this.mContext.getUserId(), 0);
    }

    public String[] getVolumePaths() {
        StorageVolume[] volumeList = getVolumeList();
        int length = volumeList.length;
        String[] strArr = new String[length];
        for (int i2 = 0; i2 < length; i2++) {
            strArr[i2] = volumeList[i2].getPath();
        }
        return strArr;
    }

    public String getVolumeState(String str) {
        StorageVolume storageVolume = getStorageVolume(new File(str));
        return storageVolume != null ? storageVolume.getState() : "removed";
    }

    public String getVolumeUUID(String str) {
        StorageVolume storageVolume = getStorageVolume(new File(str));
        if (storageVolume == null || HttpUrl.FRAGMENT_ENCODE_SET.equals(storageVolume.getUuid())) {
            return null;
        }
        return storageVolume.getUuid();
    }

    public boolean isISOFileMounted(String str) {
        return getISOFileMountPath(str) != null;
    }

    public boolean mountISO(String str, OnISOEvnetListener onISOEvnetListener) {
        if (str == null) {
            throw new IllegalArgumentException("filename cannot be null");
        }
        try {
            Integer numAddListener = this.mISOActionListener.addListener(onISOEvnetListener);
            boolean zMountISO = this.mService.mountISO(str, this.mISOActionListener, numAddListener.intValue());
            if (!zMountISO) {
                this.mISOActionListener.removeListener(onISOEvnetListener, numAddListener);
            }
            return zMountISO;
        } catch (RemoteException e2) {
            Log.e(TAG, "Failed to mount ISO", e2);
            return false;
        }
    }

    public boolean mountSamba(String str, String str2, String str3, String str4, String str5) {
        try {
            return this.mService.mountSamba(str, str2, str3, str4, str5, false, true);
        } catch (RemoteException e2) {
            Log.e(TAG, "Failed to mount Samba", e2);
            return false;
        }
    }

    public boolean mountVolume(String str) {
        try {
            return this.mService.mountVolume(str) == 0;
        } catch (RemoteException e2) {
            Log.e(TAG, "Failed to mount volume", e2);
            return false;
        }
    }

    public void removeOnISOEvnetListener(OnISOEvnetListener onISOEvnetListener) {
        this.mISOActionListener.removeListener(onISOEvnetListener);
    }

    public boolean unmountISO(String str, boolean z2) {
        if (str == null) {
            throw new IllegalArgumentException("filename cannot be null");
        }
        try {
            return this.mService.unmountISO(str, z2);
        } catch (RemoteException e2) {
            Log.e(TAG, "Failed to unmount ISO", e2);
            return false;
        }
    }

    public boolean unmountSamba(String str, boolean z2) {
        try {
            return this.mService.unmountSamba(str, z2);
        } catch (RemoteException e2) {
            Log.e(TAG, "Failed to unmount Samba", e2);
            return false;
        }
    }

    public void unmountVolume(String str, boolean z2, boolean z3) {
        try {
            this.mService.unmountVolume(str, z2, z3);
        } catch (RemoteException e2) {
            Log.e(TAG, "Failed to unmount volume", e2);
        }
    }

    public static StorageVolume getStorageVolume(File file, int i2) {
        return getStorageVolume(getVolumeList(i2, 0), file);
    }

    public static StorageVolume[] getVolumeList(int i2, int i3) {
        IStorageManager iStorageManagerAsInterface = IStorageManager.Stub.asInterface(ServiceManager.getService("mount"));
        try {
            String strCurrentOpPackageName = ActivityThread.currentOpPackageName();
            if (strCurrentOpPackageName == null) {
                String[] packagesForUid = ActivityThread.getPackageManager().getPackagesForUid(Process.myUid());
                if (packagesForUid != null && packagesForUid.length > 0) {
                    strCurrentOpPackageName = packagesForUid[0];
                }
                return new StorageVolume[0];
            }
            int packageUid = ActivityThread.getPackageManager().getPackageUid(strCurrentOpPackageName, 268435456, i2);
            return packageUid <= 0 ? new StorageVolume[0] : iStorageManagerAsInterface.getVolumeList(packageUid, strCurrentOpPackageName, i3);
        } catch (RemoteException e2) {
            throw e2.rethrowAsRuntimeException();
        }
    }

    private static StorageVolume getStorageVolume(StorageVolume[] storageVolumeArr, File file) {
        try {
            File canonicalFile = file.getCanonicalFile();
            for (StorageVolume storageVolume : storageVolumeArr) {
                if (FileUtils.contains(storageVolume.getPathFile().getCanonicalFile(), canonicalFile)) {
                    return storageVolume;
                }
            }
        } catch (IOException unused) {
        }
        return null;
    }

    private class ISOActionListener extends IISOActionListener.Stub {
        private HashMap<Integer, ISOListenerDelegate> mListeners;
        private HashMap<OnISOEvnetListener, ArrayList<Integer>> nonces;

        private ISOActionListener() {
            this.mListeners = new HashMap<>();
            this.nonces = new HashMap<>();
        }

        public Integer addListener(OnISOEvnetListener onISOEvnetListener) {
            ISOListenerDelegate iSOListenerDelegate = MStorageManager.this.new ISOListenerDelegate(onISOEvnetListener);
            Integer num = new Integer(iSOListenerDelegate.nonce);
            synchronized (this.mListeners) {
                synchronized (this.nonces) {
                    try {
                        this.mListeners.put(num, iSOListenerDelegate);
                        ArrayList<Integer> arrayList = this.nonces.get(onISOEvnetListener);
                        if (arrayList == null) {
                            arrayList = new ArrayList<>(1);
                            this.nonces.put(onISOEvnetListener, arrayList);
                        }
                        arrayList.add(num);
                    } catch (Throwable th) {
                        throw th;
                    }
                }
            }
            return num;
        }

        public void onISOEvent(String str, int i2, int i3) {
            synchronized (this.mListeners) {
                try {
                    ISOListenerDelegate iSOListenerDelegate = this.mListeners.get(Integer.valueOf(i2));
                    if (iSOListenerDelegate != null) {
                        if (iSOListenerDelegate.getListener() == null) {
                            removeListener(null, Integer.valueOf(i2));
                            return;
                        }
                        iSOListenerDelegate.sendISOStateChanged(str, i3);
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        public void removeListener(OnISOEvnetListener onISOEvnetListener) {
            synchronized (this.mListeners) {
                synchronized (this.nonces) {
                    try {
                        ArrayList<Integer> arrayListRemove = this.nonces.remove(onISOEvnetListener);
                        if (arrayListRemove != null) {
                            for (int i2 = 0; i2 < arrayListRemove.size(); i2++) {
                                this.mListeners.remove(arrayListRemove.get(i2));
                            }
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
            }
        }

        public void removeListener(OnISOEvnetListener onISOEvnetListener, Integer num) {
            if (num == null) {
                return;
            }
            synchronized (this.mListeners) {
                synchronized (this.nonces) {
                    try {
                        ArrayList<Integer> arrayList = this.nonces.get(onISOEvnetListener);
                        if (arrayList != null) {
                            arrayList.remove(num);
                            if (arrayList.size() == 0) {
                                this.nonces.remove(onISOEvnetListener);
                            }
                            this.mListeners.remove(num);
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
            }
        }
    }
}
