Search in sources :

Example 1 with UsbFile

use of com.github.mjdev.libaums.fs.UsbFile in project samourai-wallet-android by Samourai-Wallet.

the class OpenDimeActivity method readOpenDimeUSB.

private void readOpenDimeUSB() {
    strAddress = null;
    strPrivKey = null;
    balance = 0L;
    OpenDimeActivity.this.runOnUiThread(new Runnable() {

        private Handler handler = new Handler();

        public void run() {
            UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
            massStorageDevices = UsbMassStorageDevice.getMassStorageDevices(OpenDimeActivity.this);
            if (massStorageDevices.length == 0) {
                Log.w(TAG, "no device found!");
                return;
            }
            currentDevice = 0;
            UsbDevice usbDevice = (UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (usbDevice == null) {
                HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
                Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
                while (deviceIterator.hasNext()) {
                    UsbDevice device = deviceIterator.next();
                    if (device != null) {
                        usbDevice = device;
                        break;
                    }
                }
            }
            if (usbDevice != null && usbManager.hasPermission(usbDevice)) {
                Log.d(TAG, "received usb device via intent");
                // requesting permission is not needed in this case
                try {
                    massStorageDevices[currentDevice].init();
                    // we always use the first partition of the device
                    currentFs = massStorageDevices[currentDevice].getPartitions().get(0).getFileSystem();
                    Log.d(TAG, "Capacity: " + currentFs.getCapacity());
                    Log.d(TAG, "Occupied Space: " + currentFs.getOccupiedSpace());
                    Log.d(TAG, "Free Space: " + currentFs.getFreeSpace());
                    Log.d(TAG, "Chunk size: " + currentFs.getChunkSize());
                    currentDir = currentFs.getRootDirectory();
                    files.clear();
                    UsbFile[] ufiles = currentDir.listFiles();
                    for (int i = 0; i < ufiles.length; i++) {
                        Log.d("UsbFileListAdapter", "found root level file:" + ufiles[i].getName());
                        if (ufiles[i].getName().equals("address.txt") || ufiles[i].getName().equals("private-key.txt")) {
                            files.add(ufiles[i]);
                        }
                        if (ufiles[i].isDirectory()) {
                            Log.d("UsbFileListAdapter", "is directory:" + ufiles[i].getName());
                            UsbFile advancedDir = ufiles[i];
                            UsbFile[] afiles = advancedDir.listFiles();
                            for (int j = 0; j < afiles.length; j++) {
                                Log.d("UsbFileListAdapter", "found inner dir file:" + afiles[j].getName());
                                if (afiles[j].getName().equals("verify2.txt")) {
                                    files.add(afiles[j]);
                                }
                            }
                        }
                    }
                    if (hasPrivkey() && hasExposedPrivkey() && hasPublicAddress()) {
                        handler.post(new Runnable() {

                            public void run() {
                                // Toast.makeText(OpenDimeActivity.this, "spendable|consultable", Toast.LENGTH_LONG).show();
                                tvAddress.setText(strAddress);
                                btSweep.setVisibility(View.VISIBLE);
                                // btTopUp.setVisibility(View.VISIBLE);
                                btView.setVisibility(View.VISIBLE);
                            }
                        });
                    } else if (hasPrivkey() && !hasExposedPrivkey() && hasPublicAddress()) {
                        handler.post(new Runnable() {

                            public void run() {
                                // Toast.makeText(OpenDimeActivity.this, "not spendable|consultable", Toast.LENGTH_LONG).show();
                                tvAddress.setText(strAddress);
                                btSweep.setVisibility(View.GONE);
                                // btTopUp.setVisibility(View.VISIBLE);
                                btView.setVisibility(View.VISIBLE);
                            }
                        });
                    } else if (hasPublicAddress()) {
                        handler.post(new Runnable() {

                            public void run() {
                                // Toast.makeText(OpenDimeActivity.this, "consultable", Toast.LENGTH_LONG).show();
                                tvAddress.setText(strAddress);
                                btSweep.setVisibility(View.GONE);
                                // btTopUp.setVisibility(View.VISIBLE);
                                btView.setVisibility(View.VISIBLE);
                            }
                        });
                    } else {
                        handler.post(new Runnable() {

                            public void run() {
                                Toast.makeText(OpenDimeActivity.this, "not initialised", Toast.LENGTH_LONG).show();
                                btSweep.setVisibility(View.GONE);
                                // btTopUp.setVisibility(View.GONE);
                                btView.setVisibility(View.GONE);
                            }
                        });
                    }
                    if (hasValidatedSignedMessage() && hasPublicAddress()) {
                        handler.post(new Runnable() {

                            public void run() {
                                // Toast.makeText(OpenDimeActivity.this, "validated", Toast.LENGTH_LONG).show();
                                Spannable spannable = new SpannableString(OpenDimeActivity.this.getText(R.string.opendime_verified) + " - " + OpenDimeActivity.this.getText(R.string.opendime_valid_key));
                                spannable.setSpan(new ForegroundColorSpan(Color.GREEN), 0, OpenDimeActivity.this.getText(R.string.opendime_verified).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                                tvKey.setText(spannable);
                            }
                        });
                    } else if (!hasValidatedSignedMessage() && hasPublicAddress()) {
                        handler.post(new Runnable() {

                            public void run() {
                                // Toast.makeText(OpenDimeActivity.this, "invalidated", Toast.LENGTH_LONG).show();
                                Spannable spannable = new SpannableString(OpenDimeActivity.this.getText(R.string.opendime_not_verified) + " - " + OpenDimeActivity.this.getText(R.string.opendime_no_valid_key));
                                spannable.setSpan(new ForegroundColorSpan(Color.RED), 0, OpenDimeActivity.this.getText(R.string.opendime_not_verified).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                                tvKey.setText(spannable);
                                btSweep.setVisibility(View.GONE);
                                // btTopUp.setVisibility(View.GONE);
                                btView.setVisibility(View.VISIBLE);
                            }
                        });
                    } else {
                        ;
                    }
                    if (strAddress != null) {
                        ivQR.setImageBitmap(generateQRCode(strAddress));
                        new Thread(new Runnable() {

                            @Override
                            public void run() {
                                Looper.prepare();
                                final JSONObject obj = APIFactory.getInstance(OpenDimeActivity.this).getAddressInfo(strAddress);
                                JSONObject walletObj = null;
                                try {
                                    if (obj != null && obj.has("wallet")) {
                                        walletObj = obj.getJSONObject("wallet");
                                        if (walletObj.has("final_balance")) {
                                            balance = walletObj.getLong("final_balance");
                                        }
                                    }
                                } catch (JSONException je) {
                                    ;
                                }
                                if (walletObj != null && walletObj.has("final_balance")) {
                                    handler.post(new Runnable() {

                                        public void run() {
                                            String strBalance = "" + Coin.valueOf(balance).toPlainString() + " BTC";
                                            if (balance > 0L && strPrivKey != null && strPrivKey.length() > 0) {
                                                btSweep.setVisibility(View.VISIBLE);
                                            } else {
                                                btSweep.setVisibility(View.GONE);
                                            }
                                            tvBalance.setText(strBalance);
                                        }
                                    });
                                }
                                Looper.loop();
                            }
                        }).start();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "error setting up device", e);
                }
            } else {
                // first request permission from user to communicate with the
                // underlying
                // UsbDevice
                PendingIntent permissionIntent = PendingIntent.getBroadcast(OpenDimeActivity.this, 0, new Intent(ACTION_USB_PERMISSION), 0);
                usbManager.requestPermission(massStorageDevices[currentDevice].getUsbDevice(), permissionIntent);
            }
        }
    });
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) HashMap(java.util.HashMap) UsbDevice(android.hardware.usb.UsbDevice) Handler(android.os.Handler) JSONException(org.json.JSONException) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) SpannableString(android.text.SpannableString) IOException(java.io.IOException) SpannableString(android.text.SpannableString) JSONObject(org.json.JSONObject) UsbFile(com.github.mjdev.libaums.fs.UsbFile) Iterator(java.util.Iterator) PendingIntent(android.app.PendingIntent) UsbManager(android.hardware.usb.UsbManager) Spannable(android.text.Spannable)

Example 2 with UsbFile

use of com.github.mjdev.libaums.fs.UsbFile in project AnExplorer by 1hakr.

the class UsbStorageProvider method move.

private String move(String sourceDocumentId, String targetParentDocumentId) throws IOException {
    final String afterDocId;
    final UsbFile before = getFile(sourceDocumentId);
    final UsbFile after = getFile(targetParentDocumentId);
    boolean isSourceUSB = sourceDocumentId.startsWith(ROOT_ID_USB);
    boolean isTargetUSB = targetParentDocumentId.startsWith(ROOT_ID_USB);
    if (!(isSourceUSB && isTargetUSB)) {
        DocumentFile sourceDirectory = getDocumentFile(sourceDocumentId);
        DocumentFile targetDirectory = getDocumentFile(targetParentDocumentId);
        if (!FileUtils.moveDocument(getContext(), sourceDirectory, targetDirectory)) {
            throw new IllegalStateException("Failed to move ");
        } else {
            if (!sourceDirectory.delete()) {
                throw new IllegalStateException("Failed to move ");
            }
        }
        afterDocId = targetParentDocumentId;
    } else {
        before.moveTo(after);
        afterDocId = getDocIdForFile(after);
    }
    return afterDocId;
}
Also used : DocumentFile(android.support.provider.DocumentFile) UsbFile(com.github.mjdev.libaums.fs.UsbFile)

Example 3 with UsbFile

use of com.github.mjdev.libaums.fs.UsbFile in project AnExplorer by 1hakr.

the class UsbStorageProvider method getFileForDocId.

public UsbFile getFileForDocId(String documentId) throws IOException {
    UsbFile file = mFileCache.get(documentId);
    if (null != file)
        return file;
    final int splitIndex = documentId.lastIndexOf(DIRECTORY_SEPERATOR);
    if (splitIndex < 0) {
        String rootId = documentId.substring(0, documentId.length() - 1);
        UsbPartition usbPartition = mRoots.get(rootId);
        if (null == usbPartition) {
            throw new FileNotFoundException("Missing root for " + rootId);
        }
        file = usbPartition.fileSystem.getRootDirectory();
        mFileCache.put(documentId, file);
        return file;
    }
    UsbFile parent = getFileForDocId(documentId.substring(0, splitIndex));
    if (null == parent)
        throw new FileNotFoundException("Missing parent for " + documentId);
    String name = documentId.substring(splitIndex + 1);
    for (UsbFile child : parent.listFiles()) {
        if (name.equals(child.getName())) {
            mFileCache.put(documentId, child);
            return child;
        }
    }
    throw new FileNotFoundException("File not found " + documentId);
}
Also used : UsbFile(com.github.mjdev.libaums.fs.UsbFile) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with UsbFile

use of com.github.mjdev.libaums.fs.UsbFile in project AnExplorer by 1hakr.

the class UsbStorageProvider method renameDocument.

@Override
public String renameDocument(String documentId, String displayName) throws FileNotFoundException {
    try {
        UsbFile file = getFileForDocId(documentId);
        file.setName(getFileName(getMimeType(file), displayName));
        mFileCache.remove(documentId);
        final String afterDocId = getDocIdForFile(file);
        notifyDocumentsChanged(afterDocId);
        return afterDocId;
    } catch (IOException e) {
        throw new FileNotFoundException(e.getMessage());
    }
}
Also used : UsbFile(com.github.mjdev.libaums.fs.UsbFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 5 with UsbFile

use of com.github.mjdev.libaums.fs.UsbFile in project AnExplorer by 1hakr.

the class UsbStorageProvider method queryChildDocuments.

@Override
public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) throws FileNotFoundException {
    try {
        updateSettings();
        final MatrixCursor result = new DocumentCursor(resolveDocumentProjection(projection), parentDocumentId);
        UsbFile parent;
        try {
            parent = getFileForDocId(parentDocumentId);
        } catch (Exception e) {
            return result;
        }
        for (UsbFile child : parent.listFiles()) {
            includeFile(result, child);
        }
        return result;
    } catch (IOException e) {
        throw new FileNotFoundException(e.getMessage());
    }
}
Also used : UsbFile(com.github.mjdev.libaums.fs.UsbFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) MatrixCursor(dev.dworks.apps.anexplorer.cursor.MatrixCursor) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

UsbFile (com.github.mjdev.libaums.fs.UsbFile)13 FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6 MatrixCursor (dev.dworks.apps.anexplorer.cursor.MatrixCursor)3 UsbDevice (android.hardware.usb.UsbDevice)2 DocumentFile (android.support.provider.DocumentFile)2 FileSystem (com.github.mjdev.libaums.fs.FileSystem)2 UsbFileInputStream (com.github.mjdev.libaums.fs.UsbFileInputStream)2 UsbFileOutputStream (com.github.mjdev.libaums.fs.UsbFileOutputStream)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 UsbManager (android.hardware.usb.UsbManager)1 Handler (android.os.Handler)1 ArrayMap (android.support.v4.util.ArrayMap)1 Spannable (android.text.Spannable)1 SpannableString (android.text.SpannableString)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 AbstractUsbFile (com.github.mjdev.libaums.fs.AbstractUsbFile)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1