Java Code Examples for com.hippo.yorozuya.FileUtils

The following examples show how to use com.hippo.yorozuya.FileUtils. These examples are extracted from open source projects. 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 Project: MHViewer   Source File: DownloadService.java    License: 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 2
Source Project: MHViewer   Source File: Settings.java    License: 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 3
Source Project: Nimingban   Source File: UpdateHelper.java    License: 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 Project: Nimingban   Source File: TypeSendFragment.java    License: 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 Project: EhViewer   Source File: DownloadService.java    License: 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 6
Source Project: EhViewer   Source File: Settings.java    License: 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 7
Source Project: MHViewer   Source File: DirGalleryProvider.java    License: 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 8
Source Project: MHViewer   Source File: EhApplication.java    License: 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 9
Source Project: MHViewer   Source File: EhApplication.java    License: 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 10
Source Project: MHViewer   Source File: GalleryDetailScene.java    License: 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 11
Source Project: MHViewer   Source File: CommonOperations.java    License: 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 12
Source Project: MHViewer   Source File: AppConfig.java    License: 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 13
Source Project: MHViewer   Source File: AppConfig.java    License: 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 14
Source Project: MHViewer   Source File: AppConfig.java    License: 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 15
Source Project: MHViewer   Source File: AppConfig.java    License: 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 16
Source Project: MHViewer   Source File: LogCat.java    License: 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 17
Source Project: Nimingban   Source File: LogCat.java    License: 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 18
Source Project: Nimingban   Source File: NMBAppConfig.java    License: Apache License 2.0 5 votes vote down vote up
public static @Nullable File getExternalAppDir() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File file = new File(Environment.getExternalStorageDirectory(), APP_DIRNAME);
        if (FileUtils.ensureDirectory(file)) {
            return file;
        } else {
            return null;
        }
    } else {
        return null;
    }
}
 
Example 19
Source Project: Nimingban   Source File: NMBAppConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * mkdirs and get
 */
public static @Nullable File getDirInAppDir(String filename) {
    File appFolder = getExternalAppDir();
    if (appFolder != null) {
        File dir = new File(appFolder, filename);
        if (FileUtils.ensureDirectory(dir)) {
            return dir;
        }
    }

    return null;
}
 
Example 20
Source Project: Nimingban   Source File: NMBAppConfig.java    License: 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 21
Source Project: Nimingban   Source File: NMBAppConfig.java    License: Apache License 2.0 5 votes vote down vote up
public static @Nullable File getTempDir() {
    File temp = new File(sContext.getCacheDir(), "temp");
    if (FileUtils.ensureDirectory(temp)) {
        return temp;
    } else {
        return null;
    }
}
 
Example 22
Source Project: Nimingban   Source File: NMBAppConfig.java    License: 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 23
Source Project: Nimingban   Source File: NMBAppConfig.java    License: 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 24
Source Project: Nimingban   Source File: UpdateHelper.java    License: 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 25
Source Project: EhViewer   Source File: DirGalleryProvider.java    License: 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 26
Source Project: EhViewer   Source File: EhApplication.java    License: 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 27
Source Project: EhViewer   Source File: EhApplication.java    License: 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 28
Source Project: EhViewer   Source File: GalleryDetailScene.java    License: 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 29
Source Project: EhViewer   Source File: CommonOperations.java    License: 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 30
Source Project: EhViewer   Source File: AppConfig.java    License: 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;
}