package com.seewo.remote.g;

import android.content.Context;
import android.os.Build;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.util.Log;
import com.cvte.vman.instance.InvokeCmd;
import h.l;
import java.util.ArrayList;
import java.util.List;
import kotlinx.coroutines.b1;
import kotlinx.coroutines.n0;
import kotlinx.coroutines.o0;
import kotlinx.coroutines.s1;
import kotlinx.coroutines.x0;

/* JADX INFO: compiled from: UsbManager.kt */
/* JADX INFO: loaded from: classes.dex */
public final class v {
    public static final v a = new v();
    private static final n0 b = o0.a(b1.b());

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static s1 f291c;

    /* JADX INFO: compiled from: UsbManager.kt */
    @h.u.j.a.f(c = "com.seewo.remote.utils.UsbManager$mountUsb$1", f = "UsbManager.kt", l = {61}, m = "invokeSuspend")
    static final class a extends h.u.j.a.k implements h.x.c.p<n0, h.u.d<? super h.r>, Object> {

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        int f292h;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        private /* synthetic */ Object f293i;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        final /* synthetic */ List<VolumeInfo> f294j;

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        final /* synthetic */ StorageManager f295k;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        a(List<VolumeInfo> list, StorageManager storageManager, h.u.d<? super a> dVar) {
            super(2, dVar);
            this.f294j = list;
            this.f295k = storageManager;
        }

        @Override // h.u.j.a.a
        public final h.u.d<h.r> b(Object obj, h.u.d<?> dVar) {
            a aVar = new a(this.f294j, this.f295k, dVar);
            aVar.f293i = obj;
            return aVar;
        }

        @Override // h.u.j.a.a
        public final Object i(Object obj) throws Throwable {
            n0 n0Var;
            Object objA;
            Object objD = h.u.i.d.d();
            int i2 = this.f292h;
            if (i2 == 0) {
                h.m.b(obj);
                n0 n0Var2 = (n0) this.f293i;
                this.f293i = n0Var2;
                this.f292h = 1;
                if (x0.a(300L, this) == objD) {
                    return objD;
                }
                n0Var = n0Var2;
            } else {
                if (i2 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                n0Var = (n0) this.f293i;
                h.m.b(obj);
            }
            for (VolumeInfo volumeInfo : this.f294j) {
                if (!o0.d(n0Var)) {
                    return h.r.a;
                }
                StorageManager storageManager = this.f295k;
                try {
                    l.a aVar = h.l.b;
                    e.c.e.f.e.a("UsbManager", "mountedUsbTask execute, mount " + volumeInfo.getId());
                    objA = e.c.e.f.g.c.l(storageManager).c("mount", volumeInfo.getId());
                    h.l.a(objA);
                } catch (Throwable th) {
                    l.a aVar2 = h.l.b;
                    objA = h.m.a(th);
                    h.l.a(objA);
                }
                if (h.l.b(objA) != null) {
                    Log.w("UsbManager", "mountedUsb: No volume found for ID  " + volumeInfo.getId());
                }
            }
            return h.r.a;
        }

        @Override // h.x.c.p
        /* JADX INFO: renamed from: m, reason: merged with bridge method [inline-methods] */
        public final Object a(n0 n0Var, h.u.d<? super h.r> dVar) {
            return ((a) b(n0Var, dVar)).i(h.r.a);
        }
    }

    /* JADX INFO: compiled from: UsbManager.kt */
    @h.u.j.a.f(c = "com.seewo.remote.utils.UsbManager$unmountUsb$1", f = "UsbManager.kt", l = {InvokeCmd.CMD_SYSTEMCTRL_CLEAR_SYSTEM_CACHE}, m = "invokeSuspend")
    static final class b extends h.u.j.a.k implements h.x.c.p<n0, h.u.d<? super h.r>, Object> {

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        int f296h;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        private /* synthetic */ Object f297i;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        final /* synthetic */ List<VolumeInfo> f298j;

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        final /* synthetic */ StorageManager f299k;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        b(List<VolumeInfo> list, StorageManager storageManager, h.u.d<? super b> dVar) {
            super(2, dVar);
            this.f298j = list;
            this.f299k = storageManager;
        }

        @Override // h.u.j.a.a
        public final h.u.d<h.r> b(Object obj, h.u.d<?> dVar) {
            b bVar = new b(this.f298j, this.f299k, dVar);
            bVar.f297i = obj;
            return bVar;
        }

        @Override // h.u.j.a.a
        public final Object i(Object obj) throws Throwable {
            n0 n0Var;
            Object objA;
            Object objD = h.u.i.d.d();
            int i2 = this.f296h;
            if (i2 == 0) {
                h.m.b(obj);
                n0 n0Var2 = (n0) this.f297i;
                this.f297i = n0Var2;
                this.f296h = 1;
                if (x0.a(300L, this) == objD) {
                    return objD;
                }
                n0Var = n0Var2;
            } else {
                if (i2 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                n0Var = (n0) this.f297i;
                h.m.b(obj);
            }
            for (VolumeInfo volumeInfo : this.f298j) {
                if (!o0.d(n0Var)) {
                    return h.r.a;
                }
                StorageManager storageManager = this.f299k;
                try {
                    l.a aVar = h.l.b;
                    e.c.e.f.e.a("UsbManager", "unmountedUsbTask execute, unmount " + volumeInfo.getId());
                    objA = e.c.e.f.g.c.l(storageManager).c("unmount", volumeInfo.getId());
                    h.l.a(objA);
                } catch (Throwable th) {
                    l.a aVar2 = h.l.b;
                    objA = h.m.a(th);
                    h.l.a(objA);
                }
                if (h.l.b(objA) != null) {
                    Log.w("UsbManager", "unmountedUsb: No volume found for ID  " + volumeInfo.getId());
                }
            }
            return h.r.a;
        }

        @Override // h.x.c.p
        /* JADX INFO: renamed from: m, reason: merged with bridge method [inline-methods] */
        public final Object a(n0 n0Var, h.u.d<? super h.r> dVar) {
            return ((b) b(n0Var, dVar)).i(h.r.a);
        }
    }

    private v() {
    }

    private final void c(Context context) {
        if (Build.VERSION.SDK_INT < 23) {
            e.c.e.f.e.a("UsbManager", "unmountedUsbTask execute error, SDK version below M, skip unmounted");
            return;
        }
        StorageManager storageManager = (StorageManager) context.getSystemService(StorageManager.class);
        ArrayList arrayList = new ArrayList();
        Object objG = e.c.e.f.g.c.l(storageManager).b("getVolumes").g();
        h.x.d.k.c(objG, "on(storageManager).call(\"getVolumes\").get()");
        for (VolumeInfo volumeInfo : (List) objG) {
            if (volumeInfo.getType() == 0 && (volumeInfo.getState() == 0 || volumeInfo.getState() == 5)) {
                arrayList.add(volumeInfo);
            }
        }
        s1 s1Var = f291c;
        if (s1Var != null) {
            s1.a.a(s1Var, null, 1, null);
        }
        f291c = kotlinx.coroutines.k.b(b, b1.b(), null, new a(arrayList, storageManager, null), 2, null);
    }

    private final void d(Context context) {
        if (Build.VERSION.SDK_INT < 23) {
            e.c.e.f.e.a("UsbManager", "unmountedUsbTask execute error, SDK version below M, skip unmounted");
            return;
        }
        StorageManager storageManager = (StorageManager) context.getSystemService(StorageManager.class);
        ArrayList arrayList = new ArrayList();
        Object objG = e.c.e.f.g.c.l(storageManager).b("getVolumes").g();
        h.x.d.k.c(objG, "on(storageManager).call(\"getVolumes\").get()");
        for (VolumeInfo volumeInfo : (List) objG) {
            if (volumeInfo.getType() == 0 && volumeInfo.getState() == 2) {
                arrayList.add(volumeInfo);
            }
        }
        s1 s1Var = f291c;
        if (s1Var != null) {
            s1.a.a(s1Var, null, 1, null);
        }
        f291c = kotlinx.coroutines.k.b(b, b1.b(), null, new b(arrayList, storageManager, null), 2, null);
    }

    public final void a(Context context, boolean z) {
        h.x.d.k.d(context, "context");
        com.seewo.vcommons.data.d.E(z);
        if (z) {
            c(context);
        } else {
            d(context);
        }
    }

    public final boolean b() {
        return com.seewo.vcommons.data.d.y();
    }
}
