com.hippo.yorozuya.FileUtils Java Examples

The following examples show how to use com.hippo.yorozuya.FileUtils. 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: Settings.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
public static String getUserID() {
    boolean writeXml = false;
    boolean writeFile = false;
    String userID = getString(KEY_USER_ID, null);
    File file = AppConfig.getFileInExternalAppDir(FILENAME_USER_ID);
    if (null == userID || !isValidUserID(userID)) {
        writeXml = true;
        // Get use ID from out sd card file
        userID = FileUtils.read(file);
        if (null == userID || !isValidUserID(userID)) {
            writeFile = true;
            userID = generateUserID();
        }
    } else {
        writeFile = true;
    }

    if (writeXml) {
        putString(KEY_USER_ID, userID);
    }
    if (writeFile) {
        FileUtils.write(file, userID);
    }

    return userID;
}
 
Example #2
Source File: DownloadService.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
private void onUpdate(DownloadInfo info) {
    if (mNotifyManager == null) {
        return;
    }
    ensureDownloadingBuilder();

    long speed = info.speed;
    if (speed < 0) {
        speed = 0;
    }
    String text = FileUtils.humanReadableByteCount(speed, false) + "/S";
    long remaining = info.remaining;
    if (remaining >= 0) {
        text = getString(R.string.download_speed_text_2, text, ReadableTime.getShortTimeInterval(remaining));
    } else {
        text = getString(R.string.download_speed_text, text);
    }
    mDownloadingBuilder.setContentTitle(EhUtils.getSuitableTitle(info))
            .setContentText(text)
            .setContentInfo(info.total == -1 || info.finished == -1 ? null : info.finished + "/" + info.total)
            .setProgress(info.total, info.finished, false);

    mDownloadingDelay.startForeground();
}
 
Example #3
Source File: UpdateHelper.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
@Override
public void onTick() {
    if (mDownloadingBuilder == null || mNotifyManager == null) {
        return;
    }

    long receivedSize = mListener.receivedSize;
    String speed = FileUtils.humanReadableByteCount((receivedSize - lastReceivedSize) *
            1000 / mInterval, false) + "/s";
    int progress = -1;
    long totalSize = mListener.totalSize;
    if (totalSize != -1l) {
        progress = (int) (receivedSize * 100 / totalSize);
    }
    lastReceivedSize = receivedSize;

    mDownloadingBuilder.setContentText(speed);
    if (progress == -1) {
        mDownloadingBuilder.setProgress(0, 0, true);
    } else {
        mDownloadingBuilder.setProgress(100, progress, false);
    }

    mNotifyManager.notify(NOTIFY_ID_DOWNLOADING, mDownloadingBuilder.build());
}
 
Example #4
Source File: TypeSendFragment.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
private boolean handleSelectedImageUri(Uri uri) {
    if (uri == null) {
        return false;
    }

    ContentResolver resolver = getContext().getContentResolver();
    String type = resolver.getType(uri);
    if (type == null) {
        type =  MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                FileUtils.getExtensionFromFilename(uri.toString()));
    }

    int maxSize = LayoutUtils.dp2pix(getContext(), 256);
    Bitmap bitmap = BitmapUtils.decodeStream(new UriInputStreamPipe(getContext(), uri), maxSize, maxSize);
    if (bitmap != null) {
        setImagePreview(uri, type, bitmap);
        return true;
    }

    return false;
}
 
Example #5
Source File: Settings.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
public static String getUserID() {
    boolean writeXml = false;
    boolean writeFile = false;
    String userID = getString(KEY_USER_ID, null);
    File file = AppConfig.getFileInExternalAppDir(FILENAME_USER_ID);
    if (null == userID || !isValidUserID(userID)) {
        writeXml = true;
        // Get use ID from out sd card file
        userID = FileUtils.read(file);
        if (null == userID || !isValidUserID(userID)) {
            writeFile = true;
            userID = generateUserID();
        }
    } else {
        writeFile = true;
    }

    if (writeXml) {
        putString(KEY_USER_ID, userID);
    }
    if (writeFile) {
        FileUtils.write(file, userID);
    }

    return userID;
}
 
Example #6
Source File: DownloadService.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
private void onUpdate(DownloadInfo info) {
    if (mNotifyManager == null) {
        return;
    }
    ensureDownloadingBuilder();

    long speed = info.speed;
    if (speed < 0) {
        speed = 0;
    }
    String text = FileUtils.humanReadableByteCount(speed, false) + "/S";
    long remaining = info.remaining;
    if (remaining >= 0) {
        text = getString(R.string.download_speed_text_2, text, ReadableTime.getShortTimeInterval(remaining));
    } else {
        text = getString(R.string.download_speed_text, text);
    }
    mDownloadingBuilder.setContentTitle(EhUtils.getSuitableTitle(info))
            .setContentText(text)
            .setContentInfo(info.total == -1 || info.finished == -1 ? null : info.finished + "/" + info.total)
            .setProgress(info.total, info.finished, false);

    mDownloadingDelay.startForeground();
}
 
Example #7
Source File: NMBAppConfig.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
private static @Nullable File getRecordImageDir() {
    File temp = sContext.getDir("record", 0);
    if (FileUtils.ensureDirectory(temp)) {
        return temp;
    } else {
        return null;
    }
}
 
Example #8
Source File: DirGalleryProvider.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #9
Source File: NMBAppConfig.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
public static @Nullable File createTempFileWithFilename(String filename) {
    File dir = getTempDir();
    if (dir == null) {
        return null;
    }

    File file = new File(dir, filename);
    if (FileUtils.ensureFile(file)) {
        return file;
    } else {
        return null;
    }
}
 
Example #10
Source File: UpdateHelper.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
public static void showUpdateDialog(final Activity activity, final UpdateStatus status) {
    if (activity.isFinishing()) {
        return;
    }

    if (status == null) {
        return;
    }

    int versionCode = Settings.getVersionCode();
    if (versionCode >= status.versionCode) {
        return;
    }

    if (status.info == null || status.versionName == null || status.size == 0) {
        return;
    }

    Resources resources = activity.getResources();
    CharSequence message = TextUtils2.combine(
            resources.getString(R.string.version) + ": " + status.versionName + '\n' +
                    resources.getString(R.string.size) + ": " + FileUtils.humanReadableByteCount(status.size, false) + "\n\n",
            Html.fromHtml(status.info));
    new AlertDialog.Builder(activity)
            .setTitle(R.string.download_update)
            .setMessage(message)
            .setPositiveButton(R.string.download, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    showSelectDialog(activity, status);
                }
            })
            .show();
}
 
Example #11
Source File: DirGalleryProvider.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #12
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 #13
Source File: EhApplication.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private void debugPrint() {
    new Runnable() {
        @Override
        public void run() {
            if (DEBUG_PRINT_NATIVE_MEMORY) {
                Log.i(TAG, "Native memory: " + FileUtils.humanReadableByteCount(
                        Debug.getNativeHeapAllocatedSize(), false));
            }
            if (DEBUG_PRINT_IMAGE_COUNT) {
                Log.i(TAG, "Image count: " + Image.getImageCount());
            }
            SimpleHandler.getInstance().postDelayed(this, DEBUG_PRINT_INTERVAL);
        }
    }.run();
}
 
Example #14
Source File: GalleryDetailScene.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Context context = getContext2();
    if (null != context && null != mTorrentList && position < mTorrentList.length) {
        String url = mTorrentList[position].first;
        String name = mTorrentList[position].second;
        // Use system download service
        DownloadManager.Request r = new DownloadManager.Request(Uri.parse(url));
        r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                FileUtils.sanitizeFilename(name + ".torrent"));
        r.allowScanningByMediaScanner();
        r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        r.addRequestHeader("Cookie", EhApplication.getEhCookieStore(context).getCookieHeader(HttpUrl.get(url)));
        DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        if (dm != null) {
            try {
                dm.enqueue(r);
            } catch (Throwable e) {
                ExceptionUtils.throwIfFatal(e);
            }
        }
    }

    if (mDialog != null) {
        mDialog.dismiss();
        mDialog = null;
    }
}
 
Example #15
Source File: CommonOperations.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private void handleResult(JSONObject jo) {
    if (null == jo || mActivity.isFinishing()) {
        return;
    }

    String versionName;
    int versionCode;
    String size;
    CharSequence info;
    String url;

    try {
        PackageManager pm = mActivity.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES);
        int currentVersionCode = pi.versionCode;
        versionCode = jo.getInt("version_code");
        if (currentVersionCode >= versionCode) {
            // Update to date
            if (mFeedback) {
                showUpToDateDialog();
            }
            return;
        }

        versionName = jo.getString("version_name");
        size = FileUtils.humanReadableByteCount(jo.getLong("size"), false);
        info = Html.fromHtml(jo.getString("info"));
        url = jo.getString("url");
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        return;
    }

    if (mFeedback || versionCode != Settings.getSkipUpdateVersion()) {
        showUpdateDialog(versionName, versionCode, size, info, url);
    }
}
 
Example #16
Source File: AppConfig.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
public static File getExternalAppDir() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File dir = new File(Environment.getExternalStorageDirectory(), APP_DIRNAME);
        return FileUtils.ensureDirectory(dir) ? dir : null;
    }
    return null;
}
 
Example #17
Source File: AppConfig.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
/**
 * mkdirs and get
 */
@Nullable
public static File getDirInExternalAppDir(String filename) {
    File appFolder = getExternalAppDir();
    if (appFolder != null) {
        File dir = new File(appFolder, filename);
        return FileUtils.ensureDirectory(dir) ? dir : null;
    }
    return null;
}
 
Example #18
Source File: AppConfig.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
public static File getFileInExternalAppDir(String filename) {
    File appFolder = getExternalAppDir();
    if (appFolder != null) {
        File file = new File(appFolder, filename);
        return FileUtils.ensureFile(file) ? file : null;
    }
    return null;
}
 
Example #19
Source File: AppConfig.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
public static File getTempDir() {
    File dir = sContext.getCacheDir();
    File file;
    if (null != dir && FileUtils.ensureDirectory(file = new File(dir, TEMP))) {
        return file;
    } else {
        return null;
    }
}
 
Example #20
Source File: LogCat.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static boolean save(File file) {
    if (!FileUtils.ensureFile(file)) {
        return false;
    }

    try {
        Process p = Runtime.getRuntime().exec("logcat -d");
        IOUtils.copy(p.getInputStream(), new FileOutputStream(file));
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example #21
Source File: NMBAppConfig.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
/**
 * No dir
 */
public static @Nullable File getFileInAppDir(String filename) {
    File appFolder = getExternalAppDir();
    if (appFolder != null) {
        File file = new File(appFolder, filename);
        if (FileUtils.ensureFile(file)) {
            return file;
        }
    }

    return null;
}
 
Example #22
Source File: EhApplication.java    From MHViewer 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 #23
Source File: EhApplication.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
private void debugPrint() {
    new Runnable() {
        @Override
        public void run() {
            if (DEBUG_PRINT_NATIVE_MEMORY) {
                Log.i(TAG, "Native memory: " + FileUtils.humanReadableByteCount(
                        Debug.getNativeHeapAllocatedSize(), false));
            }
            if (DEBUG_PRINT_IMAGE_COUNT) {
                Log.i(TAG, "Image count: " + Image.getImageCount());
            }
            SimpleHandler.getInstance().postDelayed(this, DEBUG_PRINT_INTERVAL);
        }
    }.run();
}
 
Example #24
Source File: GalleryDetailScene.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Context context = getContext2();
    if (null != context && null != mTorrentList && position < mTorrentList.length) {
        String url = mTorrentList[position].first;
        String name = mTorrentList[position].second;
        // Use system download service
        DownloadManager.Request r = new DownloadManager.Request(Uri.parse(url));
        r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                FileUtils.sanitizeFilename(name + ".torrent"));
        r.allowScanningByMediaScanner();
        r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        if (dm != null) {
            try {
                dm.enqueue(r);
            } catch (Throwable e) {
                ExceptionUtils.throwIfFatal(e);
            }
        }
    }

    if (mDialog != null) {
        mDialog.dismiss();
        mDialog = null;
    }
}
 
Example #25
Source File: CommonOperations.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
private void handleResult(JSONObject jo) {
    if (null == jo || mActivity.isFinishing()) {
        return;
    }

    String versionName;
    int versionCode;
    String size;
    CharSequence info;
    String url;

    try {
        PackageManager pm = mActivity.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES);
        int currentVersionCode = pi.versionCode;
        versionCode = jo.getInt("version_code");
        if (currentVersionCode >= versionCode) {
            // Update to date
            if (mFeedback) {
                showUpToDateDialog();
            }
            return;
        }

        versionName = jo.getString("version_name");
        size = FileUtils.humanReadableByteCount(jo.getLong("size"), false);
        info = Html.fromHtml(jo.getString("info"));
        url = jo.getString("url");
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
        return;
    }

    if (mFeedback || versionCode != Settings.getSkipUpdateVersion()) {
        showUpdateDialog(versionName, versionCode, size, info, url);
    }
}
 
Example #26
Source File: AppConfig.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
public static File getExternalAppDir() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File dir = new File(Environment.getExternalStorageDirectory(), APP_DIRNAME);
        return FileUtils.ensureDirectory(dir) ? dir : null;
    }
    return null;
}
 
Example #27
Source File: AppConfig.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
/**
 * mkdirs and get
 */
@Nullable
public static File getDirInExternalAppDir(String filename) {
    File appFolder = getExternalAppDir();
    if (appFolder != null) {
        File dir = new File(appFolder, filename);
        return FileUtils.ensureDirectory(dir) ? dir : null;
    }
    return null;
}
 
Example #28
Source File: AppConfig.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
public static File getFileInExternalAppDir(String filename) {
    File appFolder = getExternalAppDir();
    if (appFolder != null) {
        File file = new File(appFolder, filename);
        return FileUtils.ensureFile(file) ? file : null;
    }
    return null;
}
 
Example #29
Source File: AppConfig.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
public static File getTempDir() {
    File dir = sContext.getCacheDir();
    File file;
    if (null != dir && FileUtils.ensureDirectory(file = new File(dir, TEMP))) {
        return file;
    } else {
        return null;
    }
}
 
Example #30
Source File: LogCat.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
public static boolean save(File file) {
    if (!FileUtils.ensureFile(file)) {
        return false;
    }

    try {
        Process p = Runtime.getRuntime().exec("logcat -d");
        IOUtils.copy(p.getInputStream(), new FileOutputStream(file));
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}