Search in sources :

Example 1 with SpaceAllocation

use of com.cloudrail.si.types.SpaceAllocation in project AmazeFileManager by TeamAmaze.

the class HybridFile method getUsableSpace.

/**
 * Gets usable i.e. free space of a device
 * @return
 */
public long getUsableSpace() {
    long size = 0L;
    switch(mode) {
        case SMB:
            try {
                size = (new SmbFile(path).getDiskFreeSpace());
            } catch (MalformedURLException e) {
                size = 0L;
                e.printStackTrace();
            } catch (SmbException e) {
                size = 0L;
                e.printStackTrace();
            }
            break;
        case FILE:
        case ROOT:
            size = new File(path).getUsableSpace();
            break;
        case DROPBOX:
        case BOX:
        case GDRIVE:
        case ONEDRIVE:
            SpaceAllocation spaceAllocation = dataUtils.getAccount(mode).getAllocation();
            size = spaceAllocation.getTotal() - spaceAllocation.getUsed();
            break;
        case SFTP:
            size = SshClientUtils.execute(new SFtpClientTemplate(path) {

                @Override
                public Long execute(@NonNull SFTPClient client) throws IOException {
                    try {
                        Statvfs.Response response = new Statvfs.Response(path, client.getSFTPEngine().request(Statvfs.request(client, SshClientUtils.extractRemotePathFrom(path))).retrieve());
                        return response.diskFreeSpace();
                    } catch (SFTPException e) {
                        Log.e(TAG, "Error querying server", e);
                        return 0L;
                    } catch (Buffer.BufferException e) {
                        Log.e(TAG, "Error parsing reply", e);
                        return 0L;
                    }
                }
            });
            break;
        case OTG:
            // TODO: Get free space from OTG when {@link DocumentFile} API adds support
            break;
    }
    return size;
}
Also used : Buffer(net.schmizz.sshj.common.Buffer) MalformedURLException(java.net.MalformedURLException) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) Statvfs(com.amaze.filemanager.filesystem.ssh.Statvfs) SFTPException(net.schmizz.sshj.sftp.SFTPException) SmbFile(jcifs.smb.SmbFile) SmbException(jcifs.smb.SmbException) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) SpaceAllocation(com.cloudrail.si.types.SpaceAllocation) NonNull(android.support.annotation.NonNull) RemoteFile(net.schmizz.sshj.sftp.RemoteFile) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(android.support.v4.provider.DocumentFile)

Example 2 with SpaceAllocation

use of com.cloudrail.si.types.SpaceAllocation in project cloudrail-si-android-sdk by CloudRail.

the class Files method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_files, container, false);
    TextView tv = (TextView) v.findViewById(R.id.service_name);
    switch(this.currentService) {
        case 1:
            {
                tv.setText("My Dropbox:");
                break;
            }
        case 2:
            {
                tv.setText("My Box:");
                break;
            }
        case 3:
            {
                tv.setText("My GoogleDrive:");
                break;
            }
        case 4:
            {
                tv.setText("My OneDrive:");
                break;
            }
        case 5:
            {
                tv.setText("My Egnyte:");
                break;
            }
        case 6:
            {
                tv.setText("My OneDrive Business:");
                break;
            }
    }
    final TextView tv2 = (TextView) v.findViewById(R.id.allocation);
    new Thread(new Runnable() {

        @Override
        public void run() {
            final SpaceAllocation alloc = getService().getAllocation();
            getOwnActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    String used = getSizeString(alloc.getUsed());
                    String total = getSizeString(alloc.getTotal());
                    tv2.setText(used + " used of " + total);
                }
            });
        }
    }).start();
    this.list = (ListView) v.findViewById(R.id.listView);
    this.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startSpinner();
            LinearLayout ll = (LinearLayout) view;
            TextView tv = (TextView) ll.findViewById(R.id.list_item);
            final String name = (String) tv.getText();
            new Thread(new Runnable() {

                @Override
                public void run() {
                    String next = currentPath;
                    if (!currentPath.equals("/")) {
                        next += "/";
                    }
                    next += name;
                    CloudMetaData info = getService().getMetadata(next);
                    if (info.getFolder()) {
                        setNewPath(next);
                    } else {
                        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                        InputStream data = getService().download(next);
                        File file = new File(path, name);
                        try {
                            FileOutputStream stream = new FileOutputStream(file, true);
                            pipe(data, stream);
                            data.close();
                            stream.close();
                        } catch (Exception e) {
                            stopSpinner();
                            e.printStackTrace();
                        }
                        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                        intent.setData(Uri.fromFile(file));
                        getOwnActivity().sendBroadcast(intent);
                        String ext = getMimeType(name);
                        String mime = null;
                        stopSpinner();
                        if (ext != null && isDisplayable(ext)) {
                            mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
                            intent = new Intent(Intent.ACTION_VIEW);
                            intent.setDataAndType(Uri.fromFile(file), mime);
                            startActivity(intent);
                        } else {
                            getOwnActivity().runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    Toast.makeText(context, "Download complete! Stored to download folder.", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }
            }).start();
        }
    });
    this.list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            selectedItem = view;
            PopupMenu popupMenu = new PopupMenu(getOwnActivity(), view);
            MenuInflater menuInflater = getOwnActivity().getMenuInflater();
            menuInflater.inflate(R.menu.selected_item_bar, popupMenu.getMenu());
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch(item.getItemId()) {
                        case R.id.action_delete:
                            {
                                removeItem();
                                return true;
                            }
                        case R.id.action_copy:
                            {
                                copyItem();
                                return true;
                            }
                        case R.id.action_create_share_link:
                            {
                                createShareLink();
                                return true;
                            }
                        case R.id.action_cut:
                            {
                                cutItem();
                                return true;
                            }
                        default:
                            return false;
                    }
                }
            });
            popupMenu.show();
            return true;
        }
    });
    this.spinner = (ProgressBar) v.findViewById(R.id.spinner);
    this.spinner.setVisibility(View.GONE);
    this.currentPath = "/";
    this.updateList();
    return v;
}
Also used : MenuInflater(android.view.MenuInflater) InputStream(java.io.InputStream) Intent(android.content.Intent) MenuItem(android.view.MenuItem) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) IOException(java.io.IOException) CloudMetaData(com.cloudrail.si.types.CloudMetaData) SpaceAllocation(com.cloudrail.si.types.SpaceAllocation) FileOutputStream(java.io.FileOutputStream) TextView(android.widget.TextView) AdapterView(android.widget.AdapterView) File(java.io.File) LinearLayout(android.widget.LinearLayout) PopupMenu(android.widget.PopupMenu)

Example 3 with SpaceAllocation

use of com.cloudrail.si.types.SpaceAllocation in project AmazeFileManager by TeamAmaze.

the class HybridFile method getTotal.

/**
 * Gets total size of the disk
 * @param context
 * @return
 */
public long getTotal(Context context) {
    long size = 0l;
    switch(mode) {
        case SMB:
            // TODO: Find total storage space of SMB when JCIFS adds support
            try {
                size = new SmbFile(path).getDiskFreeSpace();
            } catch (SmbException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            break;
        case FILE:
        case ROOT:
            size = new File(path).getTotalSpace();
            break;
        case DROPBOX:
        case BOX:
        case ONEDRIVE:
        case GDRIVE:
            SpaceAllocation spaceAllocation = dataUtils.getAccount(mode).getAllocation();
            size = spaceAllocation.getTotal();
            break;
        case SFTP:
            size = SshClientUtils.execute(new SFtpClientTemplate(path) {

                @Override
                public Long execute(@NonNull SFTPClient client) throws IOException {
                    try {
                        Statvfs.Response response = new Statvfs.Response(path, client.getSFTPEngine().request(Statvfs.request(client, SshClientUtils.extractRemotePathFrom(path))).retrieve());
                        return response.diskSize();
                    } catch (SFTPException e) {
                        Log.e(TAG, "Error querying server", e);
                        return 0L;
                    } catch (Buffer.BufferException e) {
                        Log.e(TAG, "Error parsing reply", e);
                        return 0L;
                    }
                }
            });
            break;
        case OTG:
            // TODO: Find total storage space of OTG when {@link DocumentFile} API adds support
            DocumentFile documentFile = OTGUtil.getDocumentFile(path, context, false);
            documentFile.length();
            break;
    }
    return size;
}
Also used : Buffer(net.schmizz.sshj.common.Buffer) MalformedURLException(java.net.MalformedURLException) DocumentFile(android.support.v4.provider.DocumentFile) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) Statvfs(com.amaze.filemanager.filesystem.ssh.Statvfs) SFTPException(net.schmizz.sshj.sftp.SFTPException) SmbFile(jcifs.smb.SmbFile) SmbException(jcifs.smb.SmbException) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) SpaceAllocation(com.cloudrail.si.types.SpaceAllocation) NonNull(android.support.annotation.NonNull) RemoteFile(net.schmizz.sshj.sftp.RemoteFile) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(android.support.v4.provider.DocumentFile)

Aggregations

SpaceAllocation (com.cloudrail.si.types.SpaceAllocation)3 File (java.io.File)3 NonNull (android.support.annotation.NonNull)2 DocumentFile (android.support.v4.provider.DocumentFile)2 SFtpClientTemplate (com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate)2 Statvfs (com.amaze.filemanager.filesystem.ssh.Statvfs)2 MalformedURLException (java.net.MalformedURLException)2 SmbException (jcifs.smb.SmbException)2 SmbFile (jcifs.smb.SmbFile)2 Buffer (net.schmizz.sshj.common.Buffer)2 RemoteFile (net.schmizz.sshj.sftp.RemoteFile)2 SFTPClient (net.schmizz.sshj.sftp.SFTPClient)2 SFTPException (net.schmizz.sshj.sftp.SFTPException)2 Intent (android.content.Intent)1 MenuInflater (android.view.MenuInflater)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 LinearLayout (android.widget.LinearLayout)1 ListView (android.widget.ListView)1