Java Code Examples for com.hippo.unifile.UniFile#getName()

The following examples show how to use com.hippo.unifile.UniFile#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CleanRedundancyPreference.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
private boolean clearFile(UniFile file) {
    String name = file.getName();
    if (name == null) {
        return false;
    }
    int index = name.indexOf('-');
    if (index >= 0) {
        name = name.substring(0, index);
    }
    String gid = name;
    if (mManager.containDownloadInfo(gid)) {
        return false;
    }
    file.delete();
    return true;
}
 
Example 2
Source File: CleanRedundancyPreference.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
private boolean clearFile(UniFile file) {
    String name = file.getName();
    if (name == null) {
        return false;
    }
    int index = name.indexOf('-');
    if (index >= 0) {
        name = name.substring(0, index);
    }
    long gid = NumberUtils.parseLongSafely(name, -1L);
    if (-1L == gid) {
        return false;
    }
    if (mManager.containDownloadInfo(gid)) {
        return false;
    }
    file.delete();
    return true;
}
 
Example 3
Source File: GalleryActivity.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
private void saveImageTo(int page) {
    if (null == mGalleryProvider) {
        return;
    }
    File dir = getCacheDir();
    UniFile file;
    if (null == (file = mGalleryProvider.save(page, UniFile.fromFile(dir), mGalleryProvider.getImageFilename(page)))) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    String filename = file.getName();
    if (filename == null) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    mCacheFileName = filename;
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_TITLE, filename);
    try {
        startActivityForResult(intent, WRITE_REQUEST_CODE);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        Toast.makeText(this, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
    }
}
 
Example 4
Source File: RestoreDownloadPreference.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
private RestoreItem getRestoreItem(UniFile file) {
    if (null == file || !file.isDirectory()) {
        return null;
    }
    UniFile siFile = file.findFile(SpiderQueen.SPIDER_INFO_FILENAME);
    if (null == siFile) {
        return null;
    }

    InputStream is = null;
    try {
        is = siFile.openInputStream();
        SpiderInfo spiderInfo = SpiderInfo.read(is);
        if (spiderInfo == null) {
            return null;
        }
        String gid = spiderInfo.gid;
        if (mManager.containDownloadInfo(gid)) {
            return null;
        }
        String token = spiderInfo.token;
        RestoreItem restoreItem = new RestoreItem();
        restoreItem.gid = gid;
        restoreItem.token = token;
        restoreItem.dirname = file.getName();
        return restoreItem;
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example 5
Source File: GalleryActivity.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private void saveImageTo(int page) {
    if (null == mGalleryProvider) {
        return;
    }
    File dir = getCacheDir();
    UniFile file;
    if (null == (file = mGalleryProvider.save(page, UniFile.fromFile(dir), mGalleryProvider.getImageFilename(page)))) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    String filename = file.getName();
    if (filename == null) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    mCacheFileName = filename;
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_TITLE, filename);
    try {
        startActivityForResult(intent, WRITE_REQUEST_CODE);
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        Toast.makeText(this, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
    }
}
 
Example 6
Source File: RestoreDownloadPreference.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private RestoreItem getRestoreItem(UniFile file) {
    if (null == file || !file.isDirectory()) {
        return null;
    }
    UniFile siFile = file.findFile(SpiderQueen.SPIDER_INFO_FILENAME);
    if (null == siFile) {
        return null;
    }

    InputStream is = null;
    try {
        is = siFile.openInputStream();
        SpiderInfo spiderInfo = SpiderInfo.read(is);
        if (spiderInfo == null) {
            return null;
        }
        long gid = spiderInfo.gid;
        if (mManager.containDownloadInfo(gid)) {
            return null;
        }
        String token = spiderInfo.token;
        RestoreItem restoreItem = new RestoreItem();
        restoreItem.gid = gid;
        restoreItem.token = token;
        restoreItem.dirname = file.getName();
        return restoreItem;
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example 7
Source File: SpiderDen.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
public static UniFile getGalleryDownloadDir(GalleryInfo galleryInfo) {
    UniFile dir = Settings.getDownloadLocation();
    if (dir != null) {
        // Read from DB
        String dirname = EhDB.getDownloadDirname(galleryInfo.getCid());
        if (null != dirname) {
            // Some dirname may be invalid in some version
            dirname = FileUtils.sanitizeFilename(dirname);
            EhDB.putDownloadDirname(galleryInfo.getCid(), dirname);
        }

        // Find it
        if (null == dirname) {
            UniFile[] files = dir.listFiles(new StartWithFilenameFilter(galleryInfo.getCid() + "-"));
            if (null != files) {
                // Get max-length-name dir
                int maxLength = -1;
                for (UniFile file : files) {
                    if (file.isDirectory()) {
                        String name = file.getName();
                        int length = name.length();
                        if (length > maxLength) {
                            maxLength = length;
                            dirname = name;
                        }
                    }
                }
                if (null != dirname) {
                    EhDB.putDownloadDirname(galleryInfo.getCid(), dirname);
                }
            }
        }

        // Create it
        if (null == dirname) {
            dirname = FileUtils.sanitizeFilename(galleryInfo.getCid() + "-" + EhUtils.getSuitableTitle(galleryInfo));
            EhDB.putDownloadDirname(galleryInfo.getCid(), dirname);
        }

        return dir.subFile(dirname);
    } else {
        return null;
    }
}
 
Example 8
Source File: GalleryActivity.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
private void shareImage(int page) {
    if (null == mGalleryProvider) {
        return;
    }

    File dir = AppConfig.getExternalTempDir();
    if (null == dir) {
        Toast.makeText(this, R.string.error_cant_create_temp_file, Toast.LENGTH_SHORT).show();
        return;
    }
    UniFile file;
    if (null == (file = mGalleryProvider.save(page, UniFile.fromFile(dir), mGalleryProvider.getImageFilename(page)))) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    String filename = file.getName();
    if (filename == null) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }

    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
            MimeTypeMap.getFileExtensionFromUrl(filename));
    if (TextUtils.isEmpty(mimeType)) {
        mimeType = "image/jpeg";
    }

    Uri uri = new Uri.Builder()
            .scheme(ContentResolver.SCHEME_CONTENT)
            .authority(BuildConfig.FILE_PROVIDER_AUTHORITY)
            .appendPath("temp")
            .appendPath(filename)
            .build();

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intent.setType(mimeType);

    try {
        startActivity(Intent.createChooser(intent, getString(R.string.share_image)));
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        Toast.makeText(this, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
    }
}
 
Example 9
Source File: SimpleFileProvider.java    From Nimingban with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public Cursor query(@NonNull Uri uri, String[] projection,
    String selection, String[] selectionArgs, String sortOrder) {
  final UniFile file = getFileForUri(uri);
  if (file == null) {
    return null;
  }

  if (projection == null) {
    projection = COLUMNS;
  }

  String[] cols = new String[projection.length];
  Object[] values = new Object[projection.length];
  int i = 0;
  for (String col : projection) {
    if (OpenableColumns.DISPLAY_NAME.equals(col)) {
      cols[i] = OpenableColumns.DISPLAY_NAME;
      values[i++] = file.getName();
    } else if (OpenableColumns.SIZE.equals(col)) {
      cols[i] = OpenableColumns.SIZE;
      values[i++] = file.length();
    } else if (MediaStore.MediaColumns.DATA.equals(col)) {
      Uri originUri = file.getUri();
      if (ContentResolver.SCHEME_FILE.equals(originUri.getScheme())) {
        cols[i] = MediaStore.MediaColumns.DATA;
        values[i++] = file.getUri().getPath();
      } else {
        // TODO handle document tree url
      }
    }
  }

  cols = copyOf(cols, i);
  values = copyOf(values, i);

  final MatrixCursor cursor = new MatrixCursor(cols, 1);
  cursor.addRow(values);
  return cursor;
}
 
Example 10
Source File: SpiderDen.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
public static UniFile getGalleryDownloadDir(GalleryInfo galleryInfo) {
    UniFile dir = Settings.getDownloadLocation();
    if (dir != null) {
        // Read from DB
        String dirname = EhDB.getDownloadDirname(galleryInfo.gid);
        if (null != dirname) {
            // Some dirname may be invalid in some version
            dirname = FileUtils.sanitizeFilename(dirname);
            EhDB.putDownloadDirname(galleryInfo.gid, dirname);
        }

        // Find it
        if (null == dirname) {
            UniFile[] files = dir.listFiles(new StartWithFilenameFilter(galleryInfo.gid + "-"));
            if (null != files) {
                // Get max-length-name dir
                int maxLength = -1;
                for (UniFile file : files) {
                    if (file.isDirectory()) {
                        String name = file.getName();
                        int length = name.length();
                        if (length > maxLength) {
                            maxLength = length;
                            dirname = name;
                        }
                    }
                }
                if (null != dirname) {
                    EhDB.putDownloadDirname(galleryInfo.gid, dirname);
                }
            }
        }

        // Create it
        if (null == dirname) {
            dirname = FileUtils.sanitizeFilename(galleryInfo.gid + "-" + EhUtils.getSuitableTitle(galleryInfo));
            EhDB.putDownloadDirname(galleryInfo.gid, dirname);
        }

        return dir.subFile(dirname);
    } else {
        return null;
    }
}
 
Example 11
Source File: GalleryActivity.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
private void shareImage(int page) {
    if (null == mGalleryProvider) {
        return;
    }

    File dir = AppConfig.getExternalTempDir();
    if (null == dir) {
        Toast.makeText(this, R.string.error_cant_create_temp_file, Toast.LENGTH_SHORT).show();
        return;
    }
    UniFile file;
    if (null == (file = mGalleryProvider.save(page, UniFile.fromFile(dir), mGalleryProvider.getImageFilename(page)))) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }
    String filename = file.getName();
    if (filename == null) {
        Toast.makeText(this, R.string.error_cant_save_image, Toast.LENGTH_SHORT).show();
        return;
    }

    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
            MimeTypeMap.getFileExtensionFromUrl(filename));
    if (TextUtils.isEmpty(mimeType)) {
        mimeType = "image/jpeg";
    }

    Uri uri = new Uri.Builder()
            .scheme(ContentResolver.SCHEME_CONTENT)
            .authority(BuildConfig.FILE_PROVIDER_AUTHORITY)
            .appendPath("temp")
            .appendPath(filename)
            .build();

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intent.setType(mimeType);

    try {
        startActivity(Intent.createChooser(intent, getString(R.string.share_image)));
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        Toast.makeText(this, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
    }
}