package com.ifpdos.factorytest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.storage.DiskInfo;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import com.seewo.vcommons.constants.SeewoIntent;
import com.seewo.vcommons.helper.StorageHelper;
import com.seewo.vcommons.utils.ShellCommandUtils;
import com.seewo.vcommons.utils.reflect.StorageManagerInsider;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public class TFCardTest extends TestItem {
    private static final String TAG = "TFCardTest";
    private AskDialog askDialog;
    Disposable dis;
    private OnAskDialogResultListener mAskRetListener;
    private Context mContext;
    private TFCardReceiver mTFCardReceiver;
    private Boolean unregisterReceiver;

    static /* synthetic */ void lambda$scanTFCard$2(Long l) throws Exception {
    }

    public TFCardTest(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.mAskRetListener = null;
        this.unregisterReceiver = false;
        this.mContext = context;
        StorageHelper.getInstance().init(this.mContext);
        registerReceiver();
    }

    private void registerReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.MEDIA_MOUNTED");
        intentFilter.addAction("android.intent.action.MEDIA_UNMOUNTED");
        this.mTFCardReceiver = new TFCardReceiver();
        intentFilter.addDataScheme(SeewoIntent.KEY_FILE_TO_3288);
        this.mContext.registerReceiver(this.mTFCardReceiver, intentFilter);
    }

    @Override // com.ifpdos.factorytest.TestItem
    void onResume() {
        super.onResume();
    }

    @Override // com.ifpdos.factorytest.TestItem
    boolean doTest() {
        if (this.mContext == null) {
            testDone(false, this.mTagDone);
            return false;
        }
        String string = getResources().getString(R.string.test_tf_ask_dialog);
        this.mAskRetListener = new OnAskDialogResultListener() { // from class: com.ifpdos.factorytest.TFCardTest.1
            @Override // com.ifpdos.factorytest.OnAskDialogResultListener
            public void onAskDialogResult(Context context, boolean z) {
                TFCardTest tFCardTest = TFCardTest.this;
                tFCardTest.testDone(z, tFCardTest.mTagDone);
                TFCardTest.this.closeTFCardListener();
            }
        };
        this.askDialog = new AskDialog(this.mContext, string, updateTFCardDevice(), this.mAskRetListener);
        scanTFCard();
        updateTFCardDevice();
        return true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void closeTFCardListener() {
        Disposable disposable = this.dis;
        if (disposable != null && !disposable.isDisposed()) {
            this.dis.dispose();
        }
        this.askDialog = null;
    }

    @Override // com.ifpdos.factorytest.TestItem
    void onStop() {
        Disposable disposable;
        super.onStop();
        if (this.askDialog == null || (disposable = this.dis) == null || disposable.isDisposed()) {
            return;
        }
        this.dis.dispose();
    }

    @Override // com.ifpdos.factorytest.TestItem
    void onDestroy() {
        super.onDestroy();
        if (this.unregisterReceiver.booleanValue()) {
            return;
        }
        Log.d(TAG, "unregisterReceiver");
        this.mContext.unregisterReceiver(this.mTFCardReceiver);
    }

    public List<StorageHelper.StorageItem> getTFCardItems() {
        String str;
        ArrayList arrayList = new ArrayList();
        List<VolumeInfo> volumes = StorageManagerInsider.getVolumes((StorageManager) this.mContext.getSystemService("storage"));
        if (volumes != null && !volumes.isEmpty()) {
            int i = 0;
            for (VolumeInfo volumeInfo : volumes) {
                DiskInfo disk = volumeInfo.getDisk();
                if (volumeInfo.isMountedReadable() && disk != null) {
                    StorageHelper.StorageItem storageItem = new StorageHelper.StorageItem();
                    storageItem.mPath = volumeInfo.path;
                    storageItem.mInternalPath = volumeInfo.internalPath;
                    if (disk.isSd()) {
                        if (TextUtils.isEmpty(volumeInfo.fsLabel)) {
                            i++;
                            str = "SD" + i;
                        } else {
                            str = volumeInfo.fsLabel;
                        }
                        storageItem.mLabel = str;
                        storageItem.mSavePath = volumeInfo.path;
                        storageItem.mAliasPath = str;
                        arrayList.add(storageItem);
                    }
                }
            }
        }
        return arrayList;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public String updateTFCardDevice() {
        StringBuilder sb = new StringBuilder();
        List<StorageHelper.StorageItem> tFCardItems = getTFCardItems();
        int size = tFCardItems.size();
        Log.d(TAG, "TFCardNumber= " + size);
        if (size > 0) {
            for (int i = 1; i <= size; i++) {
                int i2 = i - 1;
                sb.append(this.mContext.getResources().getString(R.string.tf_card_found)).append(tFCardItems.get(i2).mLabel).append(ShellCommandUtils.COMMAND_LINE_END);
                Log.d(TAG, "TFCardPath= " + tFCardItems.get(i2).mPath);
            }
        } else {
            sb.append(this.mContext.getResources().getString(R.string.no_tf_card_found));
        }
        return sb.toString();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void scanTFCard() {
        if (this.askDialog != null) {
            Disposable disposable = this.dis;
            if (disposable != null && !disposable.isDisposed()) {
                this.dis.dispose();
            }
            this.dis = Observable.interval(0L, 1L, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).filter(new Predicate() { // from class: com.ifpdos.factorytest.TFCardTest$$ExternalSyntheticLambda0
                @Override // io.reactivex.functions.Predicate
                public final boolean test(Object obj) {
                    return this.f$0.lambda$scanTFCard$0((Long) obj);
                }
            }).takeUntil(new Predicate() { // from class: com.ifpdos.factorytest.TFCardTest$$ExternalSyntheticLambda1
                @Override // io.reactivex.functions.Predicate
                public final boolean test(Object obj) {
                    return TFCardTest.lambda$scanTFCard$1((Long) obj);
                }
            }).subscribe(new Consumer() { // from class: com.ifpdos.factorytest.TFCardTest$$ExternalSyntheticLambda2
                @Override // io.reactivex.functions.Consumer
                public final void accept(Object obj) throws Exception {
                    TFCardTest.lambda$scanTFCard$2((Long) obj);
                }
            });
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ boolean lambda$scanTFCard$0(Long l) throws Exception {
        this.askDialog.mDialog.setMessage(updateTFCardDevice());
        return l.longValue() == 10;
    }

    static /* synthetic */ boolean lambda$scanTFCard$1(Long l) throws Exception {
        return l.longValue() == 10;
    }

    private class TFCardReceiver extends BroadcastReceiver {
        private TFCardReceiver() {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            action.hashCode();
            if (action.equals("android.intent.action.MEDIA_MOUNTED") || action.equals("android.intent.action.MEDIA_UNMOUNTED")) {
                TFCardTest.this.scanTFCard();
                TFCardTest.this.updateTFCardDevice();
            }
        }
    }
}
