Android Open Source - leedReader Utils






From Project

Back to project page leedReader.

License

The source code is released under:

CC by-nc-sa 4.0

If you think the Android project leedReader 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

package com.barbogogo.leedreader;
//from   w w  w  . jav a  2 s  .c  om
import java.io.IOException;
import java.lang.StringBuilder;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Formatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.leed.reader.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.NetworkOnMainThreadException;

public class Utils
{
    public static String hex(byte[] src)
    {
        Formatter fmt = new Formatter(new StringBuilder(src.length * 2));

        for (byte b : src)
        {
            fmt.format("%02x", b);
        }

        String hex = fmt.toString();
        fmt.close();

        return hex;
    }

    public static String htmlspecialchars(String src)
    {
        src = src.replace("&", "&");
        src = src.replace("\"", """);
        src = src.replace("'", "'");
        src = src.replace("<", "&lt;");
        src = src.replace(">", "&gt;");

        return src;
    }

    public static int versionCompare(String actualVersion, String serverVersion)
    {
        int versionCompare = 0;

        actualVersion = actualVersion.replace("Beta", "").trim();
        serverVersion = serverVersion.replace("Beta", "").trim();

        String[] vals1 = serverVersion.split("\\.");
        String[] vals2 = actualVersion.split("\\.");
        int i = 0;
        while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i]))
        {
            i++;
        }

        if (i < vals1.length && i < vals2.length)
        {
            int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
            versionCompare = diff < 0 ? -1 : diff == 0 ? 0 : 1;
        }
        else
            versionCompare = vals1.length < vals2.length ? -1 : vals1.length == vals2.length ? 0 : 1;

        return versionCompare;
    }

    public static String extractArticle(String article, int length)
    {
        String output = "";
        String pattern = "<([^><]*)>";
        Matcher matches2 = Pattern.compile(pattern).matcher(article);

        while (matches2.find())
        {
            output = matches2.replaceAll("");
        }

        String[] test = output.split(" ");

        output = "";

        for (int i = 0; i < length; i++)
        {
            if (test.length > i)
            {
                output += test[i] + " ";
            }
        }

        output = output.replaceAll("&Aacute;", "??");
        output = output.replaceAll("&aacute;", "");
        output = output.replaceAll("&Agrave;", "");
        output = output.replaceAll("&agrave;", "");
        output = output.replaceAll("&Acirc;", "");
        output = output.replaceAll("&acirc;", "");
        output = output.replaceAll("&Auml;", "");
        output = output.replaceAll("&auml;", "");
        output = output.replaceAll("&Atilde;", "");
        output = output.replaceAll("&atilde;", "");
        output = output.replaceAll("&Aring;", "");
        output = output.replaceAll("&aring;", "");
        output = output.replaceAll("&Aelig;", "");
        output = output.replaceAll("&aelig;", "");
        output = output.replaceAll("&Ccedil;", "");
        output = output.replaceAll("&ccedil;", "");
        output = output.replaceAll("&Eth;", "??");
        output = output.replaceAll("&eth;", "");
        output = output.replaceAll("&Eacute;", "");
        output = output.replaceAll("&eacute;", "");
        output = output.replaceAll("&Egrave;", "");
        output = output.replaceAll("&egrave;", "");
        output = output.replaceAll("&Ecirc;", "");
        output = output.replaceAll("&ecirc;", "");
        output = output.replaceAll("&Euml;", "");
        output = output.replaceAll("&euml;", "");
        output = output.replaceAll("&Iacute;", "??");
        output = output.replaceAll("&iacute;", "");
        output = output.replaceAll("&Igrave;", "");
        output = output.replaceAll("&igrave;", "");
        output = output.replaceAll("&Icirc;", "");
        output = output.replaceAll("&icirc;", "");
        output = output.replaceAll("&Iuml;", "??");
        output = output.replaceAll("&iuml;", "");
        output = output.replaceAll("&Ntilde;", "");
        output = output.replaceAll("&ntilde;", "");
        output = output.replaceAll("&Oacute;", "");
        output = output.replaceAll("&oacute;", "");
        output = output.replaceAll("&Ograve;", "");
        output = output.replaceAll("&ograve;", "");
        output = output.replaceAll("&Ocirc;", "");
        output = output.replaceAll("&ocirc;", "");
        output = output.replaceAll("&Ouml;", "");
        output = output.replaceAll("&ouml;", "");
        output = output.replaceAll("&Otilde;", "");
        output = output.replaceAll("&otilde;", "");
        output = output.replaceAll("&Oslash;", "");
        output = output.replaceAll("&oslash;", "");
        output = output.replaceAll("&oelig;", "");
        output = output.replaceAll("&OElig;", "");
        output = output.replaceAll("&szlig;", "");
        output = output.replaceAll("&Thorn;", "");
        output = output.replaceAll("&thorn;", "");
        output = output.replaceAll("&Uacute;", "");
        output = output.replaceAll("&uacute;", "");
        output = output.replaceAll("&Ugrave;", "");
        output = output.replaceAll("&ugrave;", "");
        output = output.replaceAll("&Ucirc;", "");
        output = output.replaceAll("&ucirc;", "");
        output = output.replaceAll("&Uuml;", "");
        output = output.replaceAll("&uuml;", "");
        output = output.replaceAll("&Yacute;", "??");
        output = output.replaceAll("&yacute;", "");
        output = output.replaceAll("&yuml;", "");
        output = output.replaceAll("&copy;", "");
        output = output.replaceAll("&reg;", "");
        output = output.replaceAll("&trade;", "");
        output = output.replaceAll("&amp;", "&");
        output = output.replaceAll("&lt;", "<");
        output = output.replaceAll("&gt;", ">");
        output = output.replaceAll("&euro;", "");
        output = output.replaceAll("&cent;", "");
        output = output.replaceAll("&pound;", "");
        output = output.replaceAll("&quot;", "\"");
        output = output.replaceAll("&lsquo;", "");
        output = output.replaceAll("&rsquo;", "");
        output = output.replaceAll("&ldquo;", "");
        output = output.replaceAll("&rdquo;", "??");
        output = output.replaceAll("&laquo;", "");
        output = output.replaceAll("&raquo;", "");
        output = output.replaceAll("&mdash;", "");
        output = output.replaceAll("&ndash;", "");
        output = output.replaceAll("&deg;", "");
        output = output.replaceAll("&plusmn;", "");
        output = output.replaceAll("&frac14;", "");
        output = output.replaceAll("&frac12;", "");
        output = output.replaceAll("&frac34;", "");
        output = output.replaceAll("&times;", "");
        output = output.replaceAll("&divide;", "");
        output = output.replaceAll("&alpha;", "?");
        output = output.replaceAll("&beta;", "");
        output = output.replaceAll("&infin;", "?");
        output = output.replaceAll("&nbsp;", " ");

        output = output + "...";

        return output;
    }

    public static String extractImage(Context context, String article)
    {
        String output = "";

        String pattern = "<img ([^><]*) src=\"([^=\"><]*)\" ([^><]*)>";

        Matcher matches2 = Pattern.compile(pattern).matcher(article);

        if (matches2.find())
        {
            output = matches2.group(2);
        }

        if (output.isEmpty())
        {
            output = null;
        }

        // Bitmap outputBmp = getBitmapFromURL(context, output);

        return output;
    }

    public static Bitmap getBitmapFromURL(Context context, String src)
    {
        Bitmap myBitmap = null;

        URL url;
        try
        {
            url = new URL(src);
            myBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        }
        catch (MalformedURLException e)
        {
            myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.logo);
        }
        catch (IOException e)
        {
            myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.logo);
        }
        catch (NetworkOnMainThreadException e)
        {
            myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.logo);
        }

        return myBitmap;
    }
}




Java Source Code List

com.barbogogo.leedreader.APIConnection.java
com.barbogogo.leedreader.Article.java
com.barbogogo.leedreader.DataManagement.java
com.barbogogo.leedreader.FeedAdapter.java
com.barbogogo.leedreader.Flux.java
com.barbogogo.leedreader.FolderAdapter.java
com.barbogogo.leedreader.Folder.java
com.barbogogo.leedreader.LeedReader.java
com.barbogogo.leedreader.LocalData.java
com.barbogogo.leedreader.MenuAdapter.java
com.barbogogo.leedreader.MobileArrayAdapter.java
com.barbogogo.leedreader.MySQLiteHelper.java
com.barbogogo.leedreader.SettingsActivity.java
com.barbogogo.leedreader.Utils.java
com.barbogogo.leedreader.WebviewAdapter.java