Android Open Source - FisgoDroid Smileys






From Project

Back to project page FisgoDroid.

License

The source code is released under:

The smiley icons bundled with this application belong to Meneame.NET and are licensed under the Creative Commons by-sa 3.0 license. For more information, please visit http://creativecommons.org/licens...

If you think the Android project FisgoDroid 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 net.meneame.fisgodroid;
//  w  w  w.  ja va 2s.c om
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.xml.sax.XMLReader;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.Html;
import android.text.Spannable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.TextView;

public class Smileys
{
    private static final Map<String, Smiley> msSmileys = new HashMap<String, Smiley>();
    private static final Map<Smiley, AnimatedGifDrawable> msDrawables = new HashMap<Smiley, AnimatedGifDrawable>();

    private static void addSmiley(Smiley smiley)
    {
        msSmileys.put(smiley.getChatText(), smiley);
    }

    private static void initializeResources()
    {
        // Initialize the resource map
        if ( msSmileys.size() == 0 )
        {
            addSmiley(new Smiley(">:(", "angry", R.drawable.angry));
            addSmiley(new Smiley(":|", "blank", R.drawable.blank));
            addSmiley(new Smiley(":>", "cheesy", R.drawable.cheesy));
            addSmiley(new Smiley(":S", "confused", R.drawable.confused));
            addSmiley(new Smiley("8-)", "cool", R.drawable.cool));
            addSmiley(new Smiley(":'(", "cry", R.drawable.cry));
            addSmiley(new Smiley(":$", "oops", R.drawable.embarassed));
            addSmiley(new Smiley(":ffu:", "ffu", R.drawable.fu));
            addSmiley(new Smiley(":goatse:", "goatse", R.drawable.goat));
            addSmiley(new Smiley(":D", "grin", R.drawable.grin));
            addSmiley(new Smiley(":hug:", "hug", R.drawable.hug));
            addSmiley(new Smiley("?(", "huh", R.drawable.huh));
            addSmiley(new Smiley(":*", "kiss", R.drawable.kiss));
            addSmiley(new Smiley("xD", "lol", R.drawable.laugh));
            addSmiley(new Smiley(":X", "lipssealed", R.drawable.lipsrsealed));
            addSmiley(new Smiley(":palm:", "palm", R.drawable.palm));
            addSmiley(new Smiley(":roll:", "roll", R.drawable.rolleyes));
            addSmiley(new Smiley(":(", "sad", R.drawable.sad));
            addSmiley(new Smiley("??", "shame", R.drawable.shame));
            addSmiley(new Smiley(":shit:", "shit", R.drawable.shit));
            addSmiley(new Smiley(":O", "shocked", R.drawable.shocked));
            addSmiley(new Smiley(":)", "smiley", R.drawable.smiley));
            addSmiley(new Smiley(":P", "tongue", R.drawable.tongue));
            addSmiley(new Smiley(":troll:", "troll", R.drawable.trollface2));
            addSmiley(new Smiley(":/", "undecided", R.drawable.undecided));
            addSmiley(new Smiley(":wall:", "wall", R.drawable.wall));
            addSmiley(new Smiley(";)", "wink", R.drawable.wink));
            addSmiley(new Smiley(":wow:", "wow", R.drawable.wow));
        }
    }

    public static Collection<Smiley> getSmileys()
    {
        initializeResources();
        return msSmileys.values();
    }

    public static Smiley getSmiley(String tag)
    {
        if ( tag.startsWith("{") && tag.endsWith("}") )
        {
            tag = tag.substring(1, tag.length() - 1);
        }
        return msSmileys.get(tag);
    }

    public static AnimatedGifDrawable getAnimatedDrawable(Context context, Smiley smiley)
    {
        AnimatedGifDrawable drawable = msDrawables.get(smiley);
        if ( drawable == null )
        {
            int desiredSize = context.getResources().getInteger(R.integer.smiley_size);

            DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
            int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, desiredSize, metrics);

            InputStream is = context.getResources().openRawResource(smiley.getResource());
            drawable = new AnimatedGifDrawable(context, is, height / (float) desiredSize);
            msDrawables.put(smiley, drawable);
        }
        return drawable;
    }

    public static String parseMessage(String message)
    {
        initializeResources();

        // Search smileys in the string and replace them by an <img> tag
        StringBuilder builder = new StringBuilder();
        int pos = 0;
        int cur;

        while ((cur = message.indexOf("{", pos)) > -1)
        {
            int end = message.indexOf("}", cur);
            if ( end == -1 )
            {
                builder.append(message.substring(pos, cur + 1));
                pos = cur + 1;
            }
            else
            {
                String smileyName = message.substring(cur + 1, end);
                if ( msSmileys.containsKey(smileyName) == false )
                {
                    builder.append(message.substring(pos, end + 1));
                    pos = end + 1;
                }
                else
                {
                    builder.append(message.substring(pos, cur));
                    builder.append("<smiley>{" + smileyName + "}</smiley>");
                    pos = end + 1;
                }
            }
        }
        builder.append(message.substring(pos));
        return "<body>" + builder.toString() + "</body>";
    }

    public static Html.TagHandler getTagHandler(final Context context, final TextView ownerView)
    {
        initializeResources();

        return new Html.TagHandler()
        {
            @Override
            public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader)
            {
                if ( tag.equals("smiley") )
                {
                    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                    boolean animated = prefs.getBoolean("enable_animated_smileys", false);

                    int len = output.length();

                    if ( opening )
                    {
                        SmileySpan span = new SmileySpan(animated == false ? null : new SmileySpan.Listener()
                        {
                            @Override
                            public void update()
                            {
                                ownerView.setText(ownerView.getText());
                                ownerView.postInvalidate();
                            }
                        });
                        output.setSpan(span, len, len, Spannable.SPAN_MARK_MARK);
                    }
                    else
                    {
                        SmileySpan span = (SmileySpan) getLast(output, SmileySpan.class);
                        if ( span != null )
                        {
                            int pos = output.getSpanStart(span);
                            output.removeSpan(span);

                            String smileyText = output.subSequence(pos, len).toString();
                            Smiley smiley = getSmiley(smileyText);

                            if ( pos != len && smiley != null )
                            {
                                AnimatedGifDrawable drawable = getAnimatedDrawable(context.getApplicationContext(), smiley);
                                if ( !animated )
                                {
                                    drawable.rewind();
                                }
                                span.setDrawable(drawable);
                                output.setSpan(span, pos, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            }
                        }
                    }
                }
            }

            private Object getLast(Editable text, Class kind)
            {
                @SuppressWarnings("unchecked")
                Object[] objs = text.getSpans(0, text.length(), kind);

                if ( objs.length == 0 )
                {
                    return null;
                }
                else
                {
                    for (int i = objs.length; i > 0; i--)
                    {
                        if ( text.getSpanFlags(objs[i - 1]) == Spannable.SPAN_MARK_MARK )
                        {
                            return objs[i - 1];
                        }
                    }
                    return null;
                }
            }
        };
    }
}




Java Source Code List

jp.tomorrowkey.android.GifDecoder.java
net.meneame.fisgodroid.AnimatedGifDrawable.java
net.meneame.fisgodroid.ChatActivity.java
net.meneame.fisgodroid.ChatBaseView.java
net.meneame.fisgodroid.ChatBubbleView.java
net.meneame.fisgodroid.ChatLineView.java
net.meneame.fisgodroid.ChatMessage.java
net.meneame.fisgodroid.ChatType.java
net.meneame.fisgodroid.DynamicTextView.java
net.meneame.fisgodroid.FisgoScheduler.java
net.meneame.fisgodroid.FisgoService.java
net.meneame.fisgodroid.FisgodroidApplication.java
net.meneame.fisgodroid.FriendshipStatus.java
net.meneame.fisgodroid.HttpService.java
net.meneame.fisgodroid.IHttpService.java
net.meneame.fisgodroid.ImageUpload.java
net.meneame.fisgodroid.LogSaver.java
net.meneame.fisgodroid.LoginActivity.java
net.meneame.fisgodroid.LoginStatus.java
net.meneame.fisgodroid.Notifications.java
net.meneame.fisgodroid.ProfileActivity.java
net.meneame.fisgodroid.SettingsActivity.java
net.meneame.fisgodroid.SmileyPickerView.java
net.meneame.fisgodroid.SmileySpan.java
net.meneame.fisgodroid.Smiley.java
net.meneame.fisgodroid.Smileys.java
net.meneame.fisgodroid.ThreeStateChecboxHackView.java
net.meneame.fisgodroid.UserProfileFetcher.java
net.meneame.fisgodroid.UserProfile.java
net.meneame.fisgodroid.adapters.BubblesChatAdapter.java
net.meneame.fisgodroid.adapters.ChatMessageAdapter.java
net.meneame.fisgodroid.adapters.LegacyChatAdapter.java
net.meneame.fisgodroid.notifications.ElementAdapter.java
net.meneame.fisgodroid.notifications.NotificationElement.java
net.meneame.fisgodroid.notifications.NotificationView.java
net.meneame.fisgodroid.notifications.NotificationsIndicatorDrawable.java
net.meneame.fisgodroid.notifications.NotificationsLayout.java
net.meneame.fisgodroid.notifications.NotificationsPoller.java