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;
}
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();
}
}
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();
}
}
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);
}
}
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;
}
}
Aggregations