Search in sources :

Example 6 with UniFile

use of com.hippo.unifile.UniFile in project EhViewer by seven332.

the class SpiderQueen method save.

@Nullable
public UniFile save(int index, @NonNull UniFile dir, @NonNull String filename) {
    int state = getPageState(index);
    if (STATE_FINISHED != state) {
        return null;
    }
    InputStreamPipe pipe = mSpiderDen.openInputStreamPipe(index);
    if (null == pipe) {
        return null;
    }
    OutputStream os = null;
    try {
        pipe.obtain();
        // Get dst file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(pipe.open(), null, options);
        pipe.close();
        String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType);
        UniFile dst = dir.subFile(null != extension ? filename + "." + extension : filename);
        if (null == dst) {
            return null;
        }
        // Copy
        os = dst.openOutputStream();
        IOUtils.copy(pipe.open(), os);
        return dst;
    } catch (IOException e) {
        return null;
    } finally {
        pipe.close();
        pipe.release();
        IOUtils.closeQuietly(os);
    }
}
Also used : InputStreamPipe(com.hippo.streampipe.InputStreamPipe) UniFile(com.hippo.unifile.UniFile) OutputStream(java.io.OutputStream) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException) Nullable(androidx.annotation.Nullable)

Example 7 with UniFile

use of com.hippo.unifile.UniFile in project EhViewer by seven332.

the class SpiderQueen method readSpiderInfoFromLocal.

private synchronized SpiderInfo readSpiderInfoFromLocal() {
    SpiderInfo spiderInfo = mSpiderInfo.get();
    if (spiderInfo != null) {
        return spiderInfo;
    }
    // Read from download dir
    UniFile downloadDir = mSpiderDen.getDownloadDir();
    if (downloadDir != null) {
        UniFile file = downloadDir.findFile(SPIDER_INFO_FILENAME);
        spiderInfo = SpiderInfo.read(file);
        if (spiderInfo != null && spiderInfo.gid == mGalleryInfo.gid && spiderInfo.token.equals(mGalleryInfo.token)) {
            return spiderInfo;
        }
    }
    // Read from cache
    InputStreamPipe pipe = mSpiderInfoCache.getInputStreamPipe(Long.toString(mGalleryInfo.gid));
    if (null != pipe) {
        try {
            pipe.obtain();
            spiderInfo = SpiderInfo.read(pipe.open());
            if (spiderInfo != null && spiderInfo.gid == mGalleryInfo.gid && spiderInfo.token.equals(mGalleryInfo.token)) {
                return spiderInfo;
            }
        } catch (IOException e) {
        // Ignore
        } finally {
            pipe.close();
            pipe.release();
        }
    }
    return null;
}
Also used : UniFile(com.hippo.unifile.UniFile) InputStreamPipe(com.hippo.streampipe.InputStreamPipe) IOException(java.io.IOException)

Example 8 with UniFile

use of com.hippo.unifile.UniFile in project EhViewer by seven332.

the class DirGalleryProvider method run.

@Override
public void run() {
    // It may take a long time, so run it in new thread
    UniFile[] files = mDir.listFiles(imageFilter);
    if (files == null) {
        mSize = STATE_ERROR;
        mError = GetText.getString(R.string.error_not_folder_path);
        // Notify to to show error
        notifyDataChanged();
        Log.i(TAG, "ImageDecoder end with error");
        return;
    }
    // Sort it
    Arrays.sort(files, naturalComparator);
    // Put file list
    mFileList.lazySet(files);
    // Set state normal and notify
    mSize = files.length;
    notifyDataChanged();
    while (!Thread.currentThread().isInterrupted()) {
        int index;
        synchronized (mRequests) {
            if (mRequests.isEmpty()) {
                try {
                    mRequests.wait();
                } catch (InterruptedException e) {
                    // Interrupted
                    break;
                }
                continue;
            }
            index = mRequests.pop();
            mDecodingIndex.lazySet(index);
        }
        // Check index valid
        if (index < 0 || index >= files.length) {
            mDecodingIndex.lazySet(GalleryPageView.INVALID_INDEX);
            notifyPageFailed(index, GetText.getString(R.string.error_out_of_range));
            continue;
        }
        InputStream is = null;
        try {
            is = files[index].openInputStream();
            Image image = Image.decode(is, true);
            mDecodingIndex.lazySet(GalleryPageView.INVALID_INDEX);
            if (image != null) {
                notifyPageSucceed(index, image);
            } else {
                notifyPageFailed(index, GetText.getString(R.string.error_decoding_failed));
            }
        } catch (IOException e) {
            mDecodingIndex.lazySet(GalleryPageView.INVALID_INDEX);
            notifyPageFailed(index, GetText.getString(R.string.error_reading_failed));
        } finally {
            IOUtils.closeQuietly(is);
        }
        mDecodingIndex.lazySet(GalleryPageView.INVALID_INDEX);
    }
    // Clear file list
    mFileList.lazySet(null);
    Log.i(TAG, "ImageDecoder end");
}
Also used : UniFile(com.hippo.unifile.UniFile) InputStream(java.io.InputStream) IOException(java.io.IOException) Image(com.hippo.image.Image)

Example 9 with UniFile

use of com.hippo.unifile.UniFile in project EhViewer by seven332.

the class SpiderDen method removeFromDownloadDir.

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;
}
Also used : UniFile(com.hippo.unifile.UniFile)

Example 10 with UniFile

use of com.hippo.unifile.UniFile in project EhViewer by seven332.

the class SpiderDen method openDownloadOutputStreamPipe.

/**
 * @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;
    }
}
Also used : UniFile(com.hippo.unifile.UniFile) UniFileOutputStreamPipe(com.hippo.io.UniFileOutputStreamPipe) Nullable(androidx.annotation.Nullable)

Aggregations

UniFile (com.hippo.unifile.UniFile)23 IOException (java.io.IOException)10 File (java.io.File)6 OutputStream (java.io.OutputStream)6 Nullable (androidx.annotation.Nullable)5 Intent (android.content.Intent)4 InputStream (java.io.InputStream)4 Uri (android.net.Uri)3 UniFileInputStreamPipe (com.hippo.io.UniFileInputStreamPipe)3 InputStreamPipe (com.hippo.streampipe.InputStreamPipe)3 SuppressLint (android.annotation.SuppressLint)2 Bitmap (android.graphics.Bitmap)2 BitmapFactory (android.graphics.BitmapFactory)2 ListUrlBuilder (com.hippo.ehviewer.client.data.ListUrlBuilder)2 FileOutputStream (java.io.FileOutputStream)2 Context (android.content.Context)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 FavListUrlBuilder (com.hippo.ehviewer.client.data.FavListUrlBuilder)1 GalleryInfo (com.hippo.ehviewer.client.data.GalleryInfo)1