Search in sources :

Example 16 with UniFile

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

the class MainActivity method handleIntent.

private boolean handleIntent(Intent intent) {
    if (intent == null) {
        return false;
    }
    String action = intent.getAction();
    if (Intent.ACTION_VIEW.equals(action)) {
        Uri uri = intent.getData();
        if (uri == null) {
            return false;
        }
        Announcer announcer = EhUrlOpener.parseUrl(uri.toString());
        if (announcer != null) {
            startScene(processAnnouncer(announcer));
            return true;
        }
    } else if (Intent.ACTION_SEND.equals(action)) {
        String type = intent.getType();
        if ("text/plain".equals(type)) {
            ListUrlBuilder builder = new ListUrlBuilder();
            builder.setKeyword(intent.getStringExtra(Intent.EXTRA_TEXT));
            startScene(processAnnouncer(GalleryListScene.getStartAnnouncer(builder)));
            return true;
        } else if (type.startsWith("image/")) {
            Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
            if (null != uri) {
                UniFile file = UniFile.fromUri(this, uri);
                File temp = saveImageToTempFile(file);
                if (null != temp) {
                    ListUrlBuilder builder = new ListUrlBuilder();
                    builder.setMode(ListUrlBuilder.MODE_IMAGE_SEARCH);
                    builder.setImagePath(temp.getPath());
                    builder.setUseSimilarityScan(true);
                    builder.setShowExpunged(true);
                    startScene(processAnnouncer(GalleryListScene.getStartAnnouncer(builder)));
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : ListUrlBuilder(com.hippo.ehviewer.client.data.ListUrlBuilder) UniFile(com.hippo.unifile.UniFile) Announcer(com.hippo.scene.Announcer) Uri(android.net.Uri) UniFile(com.hippo.unifile.UniFile) File(java.io.File)

Example 17 with UniFile

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

the class GalleryActivity method shareImage.

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();
    }
}
Also used : UniFile(com.hippo.unifile.UniFile) Intent(android.content.Intent) UniFile(com.hippo.unifile.UniFile) File(java.io.File) Uri(android.net.Uri)

Example 18 with UniFile

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

the class GalleryActivity method saveImageTo.

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();
    }
}
Also used : UniFile(com.hippo.unifile.UniFile) Intent(android.content.Intent) UniFile(com.hippo.unifile.UniFile) File(java.io.File)

Example 19 with UniFile

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

the class DirGalleryProvider method save.

@Nullable
@Override
public UniFile save(int index, @NonNull UniFile dir, @NonNull String filename) {
    UniFile[] fileList = mFileList.get();
    if (null == fileList || index < 0 || index >= fileList.length) {
        return null;
    }
    UniFile src = fileList[index];
    String extension = FileUtils.getExtensionFromFilename(src.getName());
    UniFile dst = dir.subFile(null != extension ? filename + "." + extension : filename);
    if (null == dst) {
        return null;
    }
    InputStream is = null;
    OutputStream os = null;
    try {
        is = src.openInputStream();
        os = dst.openOutputStream();
        IOUtils.copy(is, os);
        return dst;
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}
Also used : UniFile(com.hippo.unifile.UniFile) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Nullable(androidx.annotation.Nullable)

Example 20 with UniFile

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

the class SpiderDen method getGalleryDownloadDir.

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

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