com.hippo.unifile.UniFile Java Examples

The following examples show how to use com.hippo.unifile.UniFile. 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: SpiderDen.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
private boolean removeFromDownloadDir(int index) {
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return false;
    }

    boolean result = false;
    for (int i = 0, n = GalleryProvider2.SUPPORT_IMAGE_EXTENSIONS.length; i < n; i++) {
        String filename = generateImageFilename(index, GalleryProvider2.SUPPORT_IMAGE_EXTENSIONS[i]);
        UniFile file = dir.subFile(filename);
        if (file != null) {
            result |= file.delete();
        }
    }
    return result;
}
 
Example #2
Source File: DirGalleryProvider.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean save(int index, @NonNull UniFile file) {
    UniFile[] fileList = mFileList.get();
    if (null == fileList || index < 0 || index >= fileList.length) {
        return false;
    }

    InputStream is = null;
    OutputStream os = null;
    try {
        is = fileList[index].openInputStream();
        os = file.openOutputStream();
        IOUtils.copy(is, os);
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}
 
Example #3
Source File: SpiderDen.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
private boolean removeFromDownloadDir(int index) {
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return false;
    }

    boolean result = false;
    for (int i = 0, n = GalleryProvider2.SUPPORT_IMAGE_EXTENSIONS.length; i < n; i++) {
        String filename = generateImageFilename(index, GalleryProvider2.SUPPORT_IMAGE_EXTENSIONS[i]);
        UniFile file = dir.subFile(filename);
        if (file != null) {
            result |= file.delete();
        }
    }
    return result;
}
 
Example #4
Source File: SpiderDen.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Nullable
public InputStreamPipe openDownloadInputStreamPipe(int index) {
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return null;
    }

    for (int i = 0; i < 2; i++) {
        UniFile file = findImageFile(dir, index);
        if (file != null) {
            return new UniFileInputStreamPipe(file);
        } else if (!copyFromCacheToDownloadDir(index)) {
            return null;
        }
    }

    return null;
}
 
Example #5
Source File: SpiderDen.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
/**
 * @param extension without dot
 */
@Nullable
private OutputStreamPipe openDownloadOutputStreamPipe(int index, @Nullable String extension) {
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return null;
    }

    extension = fixExtension('.' + extension);
    UniFile file = dir.createFile(generateImageFilename(index, extension));
    if (file != null) {
        return new UniFileOutputStreamPipe(file);
    } else {
        return null;
    }
}
 
Example #6
Source File: DownloadsScene.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(DialogInterface dialog, int which) {
    if (which != DialogInterface.BUTTON_POSITIVE) {
        return;
    }

    // Delete
    if (null != mDownloadManager) {
        mDownloadManager.deleteDownload(mGalleryInfo.gid);
    }

    // Delete image files
    boolean checked = mBuilder.isChecked();
    Settings.putRemoveImageFiles(checked);
    if (checked) {
        // Remove download path
        EhDB.removeDownloadDirname(mGalleryInfo.gid);
        // Delete file
        UniFile file = SpiderDen.getGalleryDownloadDir(mGalleryInfo);
        deleteFileAsync(file);
    }
}
 
Example #7
Source File: GalleryActivity.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
private void buildProvider() {
    if (mGalleryProvider != null) {
        return;
    }

    if (ACTION_DIR.equals(mAction)) {
        if (mFilename != null) {
            mGalleryProvider = new DirGalleryProvider(UniFile.fromFile(new File(mFilename)));
        }
    } else if (ACTION_EH.equals(mAction)) {
        if (mGalleryInfo != null) {
            mGalleryProvider = new EhGalleryProvider(this, mGalleryInfo);
        }
    } else if (Intent.ACTION_VIEW.equals(mAction)) {
        if (mUri != null) {
            // Only support zip now
            mGalleryProvider = new ArchiveGalleryProvider(this, mUri);
        }
    }
}
 
Example #8
Source File: CommonOperations.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
public static void ensureNoMediaFile(UniFile file) {
    if (null == file) {
        return;
    }

    UniFile noMedia = file.createFile(".nomedia");
    if (null == noMedia) {
        return;
    }

    InputStream is = null;
    try {
        is = noMedia.openInputStream();
    } catch (IOException e) {
        // Ignore
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example #9
Source File: CommonOperations.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
public static void ensureNoMediaFile(UniFile file) {
    if (null == file) {
        return;
    }

    UniFile noMedia = file.createFile(".nomedia");
    if (null == noMedia) {
        return;
    }

    InputStream is = null;
    try {
        is = noMedia.openInputStream();
    } catch (IOException e) {
        // Ignore
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example #10
Source File: CleanRedundancyPreference.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
@Override
protected Object doInBackground(Void... params) {
    UniFile dir = Settings.getDownloadLocation();
    if (null == dir) {
        return 0;
    }
    UniFile[] files = dir.listFiles();
    if (null == files) {
        return 0;
    }

    int count = 0;
    for (UniFile f: files) {
        if (clearFile(f)) {
            ++count;
        }
    }

    return count;
}
 
Example #11
Source File: ImageProvider.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
@Override
public UniFile getFile(Uri uri) {
    // Get dir
    UniFile dir = Settings.getImageSaveLocation();
    if (dir == null) {
        return null;
    }

    // Get filename
    List<String> path = uri.getPathSegments();
    if (path == null || path.size() != 1) {
        return null;
    }

    return dir.findFile(path.get(0));
}
 
Example #12
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 #13
Source File: Settings.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
@Nullable
public static UniFile getImageSaveLocation() {
    UniFile dir = null;
    try {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme(getString(KEY_IMAGE_SAVE_SCHEME, null));
        builder.encodedAuthority(getString(KEY_IMAGE_SAVE_AUTHORITY, null));
        builder.encodedPath(getString(KEY_IMAGE_SAVE_PATH, null));
        builder.encodedQuery(getString(KEY_IMAGE_SAVE_QUERY, null));
        builder.encodedFragment(getString(KEY_IMAGE_SAVE_FRAGMENT, null));
        dir = UniFile.fromUri(sContext, builder.build());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dir != null ? dir : UniFile.fromFile(NMBAppConfig.getImageDir());
}
 
Example #14
Source File: DownloadFragment.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
    String key = preference.getKey();
    if (Settings.KEY_MEDIA_SCAN.equals(key)) {
        if (newValue instanceof Boolean) {
            UniFile downloadLocation = Settings.getDownloadLocation();
            if ((Boolean) newValue) {
                CommonOperations.removeNoMediaFile(downloadLocation);
            } else {
                CommonOperations.ensureNoMediaFile(downloadLocation);
            }
        }
        return true;
    } else if (Settings.KEY_IMAGE_RESOLUTION.equals(key)) {
        if (newValue instanceof String) {
            Settings.putImageResolution((String) newValue);
        }
        return true;
    }
    return false;
}
 
Example #15
Source File: GalleryActivity2.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
@Override
public void bindPagerHolder(GalleryHolder holder, int position) {
    String key;
    DataContainer container;
    UniFile dir = Settings.getImageSaveLocation();
    if (Settings.getSaveImageAuto() && dir != null) {
        key = null;
        container = new UniFileDataContain(GalleryActivity2.this, dir,
                mSite.getReadableName(GalleryActivity2.this) + "-" + mId);
    } else {
        key = mKey;
        container = null;
    }

    holder.galleryPage.load(key, mImage, container);
}
 
Example #16
Source File: SpiderDen.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
/**
 * @param extension without dot
 */
@Nullable
private OutputStreamPipe openDownloadOutputStreamPipe(int index, @Nullable String extension) {
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return null;
    }

    extension = fixExtension('.' + extension);
    UniFile file = dir.createFile(generateImageFilename(index, extension));
    if (file != null) {
        return new UniFileOutputStreamPipe(file);
    } else {
        return null;
    }
}
 
Example #17
Source File: UpdateHelper.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
public static void downloadApk(Context context, String url, String failedUrl, String filename) {
    if (sUpdating) {
        return;
    }

    sUpdating = true;

    context = context.getApplicationContext();
    File dir = NMBAppConfig.getExternalAppDir();
    if (dir == null) {
        Toast.makeText(context, R.string.download_update_failde, Toast.LENGTH_SHORT).show();
        return;
    }

    DownloadRequest request = new DownloadRequest();
    request.setUrl(url);
    request.setFilename(filename);
    request.setDir(UniFile.fromFile(dir));
    request.setOkHttpClient(NMBApplication.getOkHttpClient(context));
    new DownloadApkTask(context, request, Uri.fromFile(new File(dir, filename)), failedUrl).execute();
}
 
Example #18
Source File: GalleryActivity.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
private void saveImage(int page) {
    if (null == mGalleryProvider) {
        return;
    }

    File dir = AppConfig.getExternalImageDir();
    if (null == dir) {
        Toast.makeText(this, R.string.error_cant_save_image, 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;
    }

    Toast.makeText(this, getString(R.string.image_saved, file.getUri()), Toast.LENGTH_SHORT).show();

    // Sync media store
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, file.getUri()));
}
 
Example #19
Source File: SimpleFileProvider.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public String getType(@NonNull Uri uri) {
  final UniFile file = getFileForUri(uri);
  if (file != null) {
    final String name = file.getUri().getPath();
    if (name != null) {
      final int lastDot = name.lastIndexOf('.');
      if (lastDot >= 0) {
        final String extension = name.substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
          return mime;
        }
      }
    }
  }

  return "application/octet-stream";
}
 
Example #20
Source File: FileActivity.java    From UniFile with Apache License 2.0 6 votes vote down vote up
private void listFilesFilenameFilter() {
    UniFile[] files = mFile.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(UniFile dir, String filename) {
            return filename.startsWith("a");
        }
    });
    if (files == null) {
        Toast.makeText(this, "null", Toast.LENGTH_SHORT).show();
    } else if (files.length == 0) {
        Toast.makeText(this, "empty", Toast.LENGTH_SHORT).show();
    } else {
        ArrayList<Uri> uris = new ArrayList<>();
        for (UniFile f : files) {
            uris.add(f.getUri());
        }
        Intent intent = new Intent(this, FileListActivity.class);
        intent.putParcelableArrayListExtra(FileListActivity.KEY_URIS, uris);
        startActivity(intent);
    }
}
 
Example #21
Source File: Settings.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static void putDownloadLocation(@NonNull UniFile location) {
    Uri uri = location.getUri();
    putString(KEY_DOWNLOAD_SAVE_SCHEME, uri.getScheme());
    putString(KEY_DOWNLOAD_SAVE_AUTHORITY, uri.getEncodedAuthority());
    putString(KEY_DOWNLOAD_SAVE_PATH, uri.getEncodedPath());
    putString(KEY_DOWNLOAD_SAVE_QUERY, uri.getEncodedQuery());
    putString(KEY_DOWNLOAD_SAVE_FRAGMENT, uri.getEncodedFragment());

    if (getMediaScan()) {
        CommonOperations.removeNoMediaFile(location);
    } else {
        CommonOperations.ensureNoMediaFile(location);
    }
}
 
Example #22
Source File: RestoreDownloadPreference.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Override
protected Object doInBackground(Void... params) {
    UniFile dir = Settings.getDownloadLocation();
    if (null == dir) {
        return null;
    }

    List<RestoreItem> restoreItemList = new ArrayList<>();

    UniFile[] files = dir.listFiles();
    if (files == null) {
        return null;
    }

    for (UniFile file: files) {
        RestoreItem restoreItem = getRestoreItem(file);
        if (null != restoreItem) {
            restoreItemList.add(restoreItem);
        }
    }

    if (0 == restoreItemList.size()) {
        return Collections.EMPTY_LIST;
    }

    try {
        return EhEngine.fillGalleryListByApi(null, mHttpClient, new ArrayList<GalleryInfo>(restoreItemList), EhUrl.getReferer());
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        e.printStackTrace();
        return null;
    }
}
 
Example #23
Source File: EhGalleryProvider.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean save(int index, @NonNull UniFile file) {
    if (null != mSpiderQueen) {
        return mSpiderQueen.save(index, file);
    } else {
        return false;
    }
}
 
Example #24
Source File: DownloadsScene.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private static void deleteFileAsync(UniFile... files) {
    new AsyncTask<UniFile, Void, Void>() {
        @Override
        protected Void doInBackground(UniFile... params) {
            for (UniFile file: params) {
                if (file != null) {
                    file.delete();
                }
            }
            return null;
        }
    }.executeOnExecutor(IoThreadPoolExecutor.getInstance(), files);
}
 
Example #25
Source File: SpiderInfo.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static SpiderInfo read(@Nullable UniFile file) {
    if (file == null) {
        return null;
    }

    InputStream is = null;
    try {
        is = file.openInputStream();
        return read(is);
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example #26
Source File: FileActivity.java    From UniFile with Apache License 2.0 5 votes vote down vote up
private void findFile() {
    String filename = ((EditText) findViewById(R.id.find_file_param)).getText().toString();
    UniFile file = mFile.findFile(filename);
    if (file != null) {
        Intent intent = new Intent(this, FileActivity.class);
        intent.setData(file.getUri());
        startActivity(intent);
    } else {
        Toast.makeText(this, "null", Toast.LENGTH_SHORT).show();
    }
}
 
Example #27
Source File: SpiderDen.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
private static UniFile findImageFile(UniFile dir, int index) {
    for (String extension : GalleryProvider2.SUPPORT_IMAGE_EXTENSIONS) {
        String filename = generateImageFilename(index, extension);
        UniFile file = dir.findFile(filename);
        if (file != null) {
            return file;
        }
    }
    return null;
}
 
Example #28
Source File: EhApplication.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private void clearTempDir() {
    File dir = AppConfig.getTempDir();
    if (null != dir) {
        FileUtils.deleteContent(dir);
    }
    dir = AppConfig.getExternalTempDir();
    if (null != dir) {
        FileUtils.deleteContent(dir);
    }

    // Add .nomedia to external temp dir
    CommonOperations.ensureNoMediaFile(UniFile.fromFile(AppConfig.getExternalTempDir()));
}
 
Example #29
Source File: ListActivity.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
@Override
public void OnLongClickImage(UniFile imageFile) {
    Intent intent = new Intent(this, GalleryActivity2.class);
    intent.setAction(GalleryActivity2.ACTION_IMAGE_FILE);
    intent.putExtra(GalleryActivity2.KEY_UNI_FILE_URI, imageFile.getUri());
    startActivity(intent);
}
 
Example #30
Source File: SettingsActivity.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
public void updateImageSaveLocation() {
    UniFile uniFile = Settings.getImageSaveLocation();
    if (uniFile == null) {
        mImageSaveLocation.setSummary(R.string.main_image_save_locatio_summary_invalid);
    } else {
        mImageSaveLocation.setSummary(uniFile.getUri().toString());
    }
}