Android Open Source - Viz Utils






From Project

Back to project page Viz.

License

The source code is released under:

GNU General Public License

If you think the Android project Viz listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2012-2014, First Three LLC/*w w  w . ja va2s. c  o m*/
 *
 * This file is a part of Viz.
 *
 * Viz is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * Viz is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Viz.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.first3.viz.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Rect;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.view.View;
import android.view.Window;

public class Utils {
    public static boolean isExtStorageAvailable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    public static String getVersion(Context context) {
        String version;
        try {
            version = context.getPackageManager().getPackageInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA).versionName;
        } catch (NameNotFoundException e) {
            version = "UnknownVersion";
        }
        return version;
    }

    public static boolean isHoneycombOrHigher() {
        // Can use static final constants like HONEYCOMB, declared in later
        // versions of the OS since they are inlined at compile time. This is guaranteed
        // behavior.
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    }

    public static boolean isGingerBreadMROrLater() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1;
    }

    public static boolean isGingerbreadOrHigher() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
    }

    public static boolean isFroyoOrLower() {
        return Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO;
    }

    public static boolean isLowerThanHoneyComb() {
        return Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
    }

    public static boolean isLowerThanKitkat() {
        return Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT;
    }

    public static boolean isKitkatOrHigher() {
        return !isLowerThanKitkat();
    }

    public static boolean isVersionTwo() {
        return Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
    }

    public static boolean isJellyBeanMR1OrHigher() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;
    }

    public static String filesize_toReadableForm(Long fileSize, boolean pad) {
        float gbyte_val = fileSize/(1024F*1024*1024);
        float mbyte_val = fileSize/(1024F*1024);
        float kbyte_val = fileSize/1024F;
        // longest possible string size is from a filesize:
        // 124.21 MB - a minium of a 6 characters for the number

        if (gbyte_val > 1) {
            if (pad) {
                return String.format("%6.2f GB", gbyte_val);
            } else {
                return String.format("%.2f GB", gbyte_val);
            }
        } else if (mbyte_val > 1) {
            if (pad) {
                return String.format("%6.2f MB", mbyte_val);
            } else {
                return String.format("%.2f MB", mbyte_val);
            }

        } else if (kbyte_val > 1) {
            // precision looks weird on kilobytes
            if (pad) {
                return String.format("%6.0f KB", kbyte_val);
            } else {
                return String.format("%.0f KB", kbyte_val);
            }
        } else {
            if (pad) {
                // ..and stupid on bytes
                return String.format("%6.0f B ", fileSize);
            } else {
                return fileSize.toString() + "B";
            }
        }
    }

    public static String msecs_toReadableForm(int msecs) {
        int seconds = msecs/1000;
        int hours = seconds/3600;
        seconds = seconds%3600;
        int minutes = seconds/60;
        seconds = seconds%60;
        String duration;
        if (hours > 0) {
            duration = String.format("%d:%02d:%02d", hours, minutes, seconds);
        } else if (minutes > 0) {
            duration = String.format("%d:%02d", minutes, seconds);
        } else {
            duration = String.format(":%d", seconds);
        }
        return duration;
    }

    public static float bytesAvailable(File path) {
        if (!path.exists()) {
            return 0;
        }
        StatFs stat = new StatFs(path.getPath());
        return (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
    }

    /**
     * Return the number of megabytes available at the specified path.
     */
    public static float mbAvailable(File path) {
        return bytesAvailable(path) / (1024.f * 1024.f);
    }

    /**
     * Get the memory class of this device (approx. per-app memory limit)
     *
     * @param context
     * @return
     */
    public static int getMemoryClass(Context context) {
        return ((ActivityManager) context.getSystemService(
                        Context.ACTIVITY_SERVICE)).getMemoryClass();
    }

    public static String getCanonicalPath(File f) {
        if (f == null) {
            Log.mw("null file specified");
            return "";
        }
        try {
            return f.getCanonicalPath();
        } catch(IOException e) {
            Log.w("Invalid path: " + f);
            return f.toString();
        }
    }

    public static void maximizeDialog(Activity activity, View view) {
        Rect displayRectangle = new Rect();
        Window window = activity.getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
        view.setMinimumHeight(displayRectangle.height());
        view.setMinimumWidth(displayRectangle.width());
    }

    public static void threadStart(Thread t, String errorMessage) {
        try {
            t.start();
        } catch(Throwable th) {
            Log.e(errorMessage);
        }
    }

    public static boolean directoryCreate(File dir) {
        if (dir == null) {
            return false;
        }
        if (!dir.exists()) {
            dir.mkdir();
            if (!dir.isDirectory()) {
                return false;
            }
        }

        return dir.canWrite();
    }

    public static URL urlFromString(String sURL) {
        URL url = null;

        if (sURL == null || sURL.length() == 0) {
            return null;
        }

        try {
            url = new URL(sURL);
        } catch (MalformedURLException e) {
            Log.d("malformedurlexception " + sURL);
        }
        return url;
    }

    public static byte[] bitmapToByteArray(Bitmap bitmap) {
        ByteArrayOutputStream blob = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 100, blob);
        return blob.toByteArray();
    }
}




Java Source Code List

com.actionbarsherlock.BuildConfig.java
com.first3.viz.Config.java
com.first3.viz.Config.java
com.first3.viz.Constants.java
com.first3.viz.Preferences.java
com.first3.viz.VersionChangeNotifier.java
com.first3.viz.VizApp.java
com.first3.viz.browser.Browser.java
com.first3.viz.browser.VizWebChromeClient.java
com.first3.viz.browser.VizWebViewClient.java
com.first3.viz.builders.BlinkxResourceBuilder.java
com.first3.viz.builders.CombinedResourceBuilder.java
com.first3.viz.builders.ContainerResourceBuilder.java
com.first3.viz.builders.DailyMotionResourceBuilder.java
com.first3.viz.builders.FlashPlayerResourceBuilder.java
com.first3.viz.builders.FunnyOrDieResourceBuilder.java
com.first3.viz.builders.GenericResourceBuilder.java
com.first3.viz.builders.GoGoAnimeResourceBuilder.java
com.first3.viz.builders.JSResourceBuilder.java
com.first3.viz.builders.LiveleakResourceBuilder.java
com.first3.viz.builders.MetacafeResourceBuilder.java
com.first3.viz.builders.NovamovResourceBuilder.java
com.first3.viz.builders.Play44ResourceBuilder.java
com.first3.viz.builders.PornHubBuilder.java
com.first3.viz.builders.RedtubeBuilder.java
com.first3.viz.builders.ResourceBuilder.java
com.first3.viz.builders.VevoResourceBuilder.java
com.first3.viz.builders.Video44ResourceBuilder.java
com.first3.viz.builders.VideoFunResourceBuilder.java
com.first3.viz.builders.VidzurResourceBuilder.java
com.first3.viz.builders.VimeoResourceBuilder.java
com.first3.viz.builders.YouruploadResourceBuilder.java
com.first3.viz.content.ContentSource.java
com.first3.viz.content.ContentSources.java
com.first3.viz.content.ContentType.java
com.first3.viz.content.ContentTypes.java
com.first3.viz.download.Container.java
com.first3.viz.download.DownloadManager.java
com.first3.viz.download.StringContainer.java
com.first3.viz.models.Favorite.java
com.first3.viz.models.Resource.java
com.first3.viz.players.VideoPlayer.java
com.first3.viz.provider.VizContract.java
com.first3.viz.provider.VizDatabase.java
com.first3.viz.provider.VizProvider.java
com.first3.viz.ui.ActivityDelegate.java
com.first3.viz.ui.DirectoryListAdapter.java
com.first3.viz.ui.DownloadDirectoryDialogPreference.java
com.first3.viz.ui.Downloads.java
com.first3.viz.ui.FastBitmapDrawable.java
com.first3.viz.ui.Favorites.java
com.first3.viz.ui.FileManager.java
com.first3.viz.ui.PinSelectorDialogFragment.java
com.first3.viz.ui.ProgressDialogFragment.java
com.first3.viz.ui.Settings.java
com.first3.viz.ui.VizMediaPlayer.java
com.first3.viz.utils.AbstractPauseHandler.java
com.first3.viz.utils.ActivityParent.java
com.first3.viz.utils.DownloadTask.java
com.first3.viz.utils.FetchContainerTask.java
com.first3.viz.utils.FragmentParent.java
com.first3.viz.utils.IOUtilities.java
com.first3.viz.utils.ImageUtilities.java
com.first3.viz.utils.Lists.java
com.first3.viz.utils.Log.java
com.first3.viz.utils.Maps.java
com.first3.viz.utils.SelectionBuilder.java
com.first3.viz.utils.StringBuffer.java
com.first3.viz.utils.TabsAdapter.java
com.first3.viz.utils.Utils.java
com.first3.viz.utils.VizUtils.java