diff options
author | lookshe <github@lookshe.org> | 2015-08-15 23:21:31 +0200 |
---|---|---|
committer | lookshe <github@lookshe.org> | 2015-08-15 23:24:46 +0200 |
commit | 9e785f265858b5e475bbb720ae8cb89b12675594 (patch) | |
tree | c63c74cf305a4edef0a5a279b2229c281854f7f9 /libs/emojicon/src/main/java | |
parent | 79c585144d0967a2e5d4b1cfb6c486332d0b0cfb (diff) |
first attempt for trying to change emojicon to https://github.com/rockerhieu/emojicontrz/emojicon-new
Diffstat (limited to 'libs/emojicon/src/main/java')
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiAdapter.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiAdapter.java) | 48 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconEditText.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconEditText.java) | 31 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconGridFragment.java | 119 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconHandler.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java) | 52 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconRecents.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconRecents.java) | 6 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconRecentsGridFragment.java | 85 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconRecentsManager.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconRecentsManager.java) | 49 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconSpan.java | 95 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconTextView.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconTextView.java) | 31 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconsFragment.java | 291 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconsPopup.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconsPopup.java) | 87 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Emojicon.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Emojicon.java) | 85 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Nature.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Nature.java) | 4 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Objects.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Objects.java) | 4 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/People.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/People.java) | 4 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Places.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Places.java) | 4 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Symbols.java (renamed from libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Symbols.java) | 4 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconGridView.java | 78 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconRecentsGridView.java | 62 | ||||
-rw-r--r-- | libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconSpan.java | 51 |
20 files changed, 868 insertions, 322 deletions
diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiAdapter.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiAdapter.java index e3dc221d..9c98aedf 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiAdapter.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,61 +14,59 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon; - -import github.ankushsachdeva.emojicon.EmojiconGridView.OnEmojiconClickedListener; -import github.ankushsachdeva.emojicon.emoji.Emojicon; +package com.rockerhieu.emojicon; import java.util.List; import android.content.Context; import android.view.View; -import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; -import android.widget.TextView; - -import github.ankushsachdeva.emojicon.R; +import com.rockerhieu.emojicon.emoji.Emojicon; /** - * @author Ankush Sachdeva (sankush@yahoo.co.in) + * @author Hieu Rocker (rockerhieu@gmail.com) */ class EmojiAdapter extends ArrayAdapter<Emojicon> { - OnEmojiconClickedListener emojiClickListener; + private boolean mUseSystemDefault = false; + public EmojiAdapter(Context context, List<Emojicon> data) { super(context, R.layout.emojicon_item, data); + mUseSystemDefault = false; + } + + public EmojiAdapter(Context context, List<Emojicon> data, boolean useSystemDefault) { + super(context, R.layout.emojicon_item, data); + mUseSystemDefault = useSystemDefault; } public EmojiAdapter(Context context, Emojicon[] data) { super(context, R.layout.emojicon_item, data); + mUseSystemDefault = false; } - - public void setEmojiClickListener(OnEmojiconClickedListener listener){ - this.emojiClickListener = listener; + + public EmojiAdapter(Context context, Emojicon[] data, boolean useSystemDefault) { + super(context, R.layout.emojicon_item, data); + mUseSystemDefault = useSystemDefault; } - + @Override - public View getView(final int position, View convertView, ViewGroup parent) { + public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { v = View.inflate(getContext(), R.layout.emojicon_item, null); ViewHolder holder = new ViewHolder(); - holder.icon = (TextView) v.findViewById(R.id.emojicon_icon); + holder.icon = (EmojiconTextView) v.findViewById(R.id.emojicon_icon); + holder.icon.setUseSystemDefault(mUseSystemDefault); v.setTag(holder); } Emojicon emoji = getItem(position); ViewHolder holder = (ViewHolder) v.getTag(); holder.icon.setText(emoji.getEmoji()); - holder.icon.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - emojiClickListener.onEmojiconClicked(getItem(position)); - } - }); return v; } - class ViewHolder { - TextView icon; + static class ViewHolder { + EmojiconTextView icon; } }
\ No newline at end of file diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconEditText.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconEditText.java index 8301dd14..2d009ce5 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconEditText.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconEditText.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,12 +14,11 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon; - -import github.ankushsachdeva.emojicon.R; +package com.rockerhieu.emojicon; import android.content.Context; import android.content.res.TypedArray; +import android.text.style.DynamicDrawableSpan; import android.util.AttributeSet; import android.widget.EditText; @@ -28,11 +27,14 @@ import android.widget.EditText; */ public class EmojiconEditText extends EditText { private int mEmojiconSize; + private int mEmojiconAlignment; + private int mEmojiconTextSize; + private boolean mUseSystemDefault = false; public EmojiconEditText(Context context) { super(context); mEmojiconSize = (int) getTextSize(); - + mEmojiconTextSize = (int) getTextSize(); } public EmojiconEditText(Context context, AttributeSet attrs) { @@ -48,14 +50,16 @@ public class EmojiconEditText extends EditText { private void init(AttributeSet attrs) { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.Emojicon); mEmojiconSize = (int) a.getDimension(R.styleable.Emojicon_emojiconSize, getTextSize()); + mEmojiconAlignment = a.getInt(R.styleable.Emojicon_emojiconAlignment, DynamicDrawableSpan.ALIGN_BASELINE); + mUseSystemDefault = a.getBoolean(R.styleable.Emojicon_emojiconUseSystemDefault, false); a.recycle(); + mEmojiconTextSize = (int) getTextSize(); setText(getText()); } @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { - super.onTextChanged(text,start,lengthBefore,lengthAfter); - EmojiconHandler.addEmojis(getContext(), getText(), mEmojiconSize); + updateText(); } /** @@ -63,5 +67,18 @@ public class EmojiconEditText extends EditText { */ public void setEmojiconSize(int pixels) { mEmojiconSize = pixels; + + updateText(); + } + + private void updateText() { + EmojiconHandler.addEmojis(getContext(), getText(), mEmojiconSize, mEmojiconAlignment, mEmojiconTextSize, mUseSystemDefault); + } + + /** + * Set whether to use system default emojicon + */ + public void setUseSystemDefault(boolean useSystemDefault) { + mUseSystemDefault = useSystemDefault; } } diff --git a/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconGridFragment.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconGridFragment.java new file mode 100644 index 00000000..0e13c4c3 --- /dev/null +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconGridFragment.java @@ -0,0 +1,119 @@ +/* + * Copyright 2014 Hieu Rocker + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.rockerhieu.emojicon; + +import com.rockerhieu.emojicon.emoji.Emojicon; +import com.rockerhieu.emojicon.emoji.People; + +import android.app.Activity; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.GridView; + +/** + * @author Hieu Rocker (rockerhieu@gmail.com) + */ +public class EmojiconGridFragment extends Fragment implements AdapterView.OnItemClickListener { + private OnEmojiconClickedListener mOnEmojiconClickedListener; + private EmojiconRecents mRecents; + private Emojicon[] mData; + private boolean mUseSystemDefault = false; + + private static final String USE_SYSTEM_DEFAULT_KEY = "useSystemDefaults"; + private static final String EMOJICONS_KEY = "emojicons"; + + protected static EmojiconGridFragment newInstance(Emojicon[] emojicons, EmojiconRecents recents) { + return newInstance(emojicons, recents, false); + } + + protected static EmojiconGridFragment newInstance(Emojicon[] emojicons, EmojiconRecents recents, boolean useSystemDefault) { + EmojiconGridFragment emojiGridFragment = new EmojiconGridFragment(); + Bundle args = new Bundle(); + args.putParcelableArray(EMOJICONS_KEY, emojicons); + args.putBoolean(USE_SYSTEM_DEFAULT_KEY, useSystemDefault); + emojiGridFragment.setArguments(args); + emojiGridFragment.setRecents(recents); + return emojiGridFragment; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.emojicon_grid, container, false); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + GridView gridView = (GridView) view.findViewById(R.id.Emoji_GridView); + Bundle bundle = getArguments(); + if (bundle == null) { + mData = People.DATA; + mUseSystemDefault = false; + } else { + mData = (Emojicon[]) getArguments().getParcelableArray(EMOJICONS_KEY); + mUseSystemDefault = bundle.getBoolean(USE_SYSTEM_DEFAULT_KEY); + } + gridView.setAdapter(new EmojiAdapter(view.getContext(), mData, mUseSystemDefault)); + gridView.setOnItemClickListener(this); + } + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putParcelableArray(EMOJICONS_KEY, mData); + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + if (activity instanceof OnEmojiconClickedListener) { + mOnEmojiconClickedListener = (OnEmojiconClickedListener) activity; + } else if (getParentFragment() instanceof OnEmojiconClickedListener) { + mOnEmojiconClickedListener = (OnEmojiconClickedListener) getParentFragment(); + } else { + throw new IllegalArgumentException(activity + " must implement interface " + OnEmojiconClickedListener.class.getSimpleName()); + } + } + + @Override + public void onDetach() { + mOnEmojiconClickedListener = null; + super.onDetach(); + } + + @Override + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + if (mOnEmojiconClickedListener != null) { + mOnEmojiconClickedListener.onEmojiconClicked((Emojicon) parent.getItemAtPosition(position)); + } + if (mRecents != null) { + mRecents.addRecentEmoji(view.getContext(), ((Emojicon) parent + .getItemAtPosition(position))); + } + } + + private void setRecents(EmojiconRecents recents) { + mRecents = recents; + } + + public interface OnEmojiconClickedListener { + void onEmojiconClicked(Emojicon emojicon); + } +} diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconHandler.java index c84bebb8..2f819fd6 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconHandler.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,9 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package github.ankushsachdeva.emojicon; - -import github.ankushsachdeva.emojicon.R; +package com.rockerhieu.emojicon; import android.content.Context; import android.text.Spannable; @@ -1379,9 +1377,11 @@ public final class EmojiconHandler { * @param context * @param text * @param emojiSize + * @param emojiAlignment + * @param textSize */ - public static void addEmojis(Context context, Spannable text, int emojiSize) { - addEmojis(context, text, emojiSize, 0, -1); + public static void addEmojis(Context context, Spannable text, int emojiSize, int emojiAlignment, int textSize) { + addEmojis(context, text, emojiSize, emojiAlignment, textSize, 0, -1, false); } /** @@ -1390,10 +1390,46 @@ public final class EmojiconHandler { * @param context * @param text * @param emojiSize + * @param emojiAlignment + * @param textSize * @param index * @param length */ - public static void addEmojis(Context context, Spannable text, int emojiSize, int index, int length) { + public static void addEmojis(Context context, Spannable text, int emojiSize, int emojiAlignment, int textSize, int index, int length) { + addEmojis(context, text, emojiSize, emojiAlignment, textSize, index, length, false); + } + + /** + * Convert emoji characters of the given Spannable to the according emojicon. + * + * @param context + * @param text + * @param emojiSize + * @param emojiAlignment + * @param textSize + * @param useSystemDefault + */ + public static void addEmojis(Context context, Spannable text, int emojiSize, int emojiAlignment, int textSize, boolean useSystemDefault) { + addEmojis(context, text, emojiSize, emojiAlignment, textSize, 0, -1, useSystemDefault); + } + + /** + * Convert emoji characters of the given Spannable to the according emojicon. + * + * @param context + * @param text + * @param emojiSize + * @param emojiAlignment + * @param textSize + * @param index + * @param length + * @param useSystemDefault + */ + public static void addEmojis(Context context, Spannable text, int emojiSize, int emojiAlignment, int textSize, int index, int length, boolean useSystemDefault) { + if (useSystemDefault) { + return; + } + int textLength = text.length(); int textLengthToProcessMax = textLength - index; int textLengthToProcess = length < 0 || length >= textLengthToProcessMax ? textLength : (length+index); @@ -1508,7 +1544,7 @@ public final class EmojiconHandler { } if (icon > 0) { - text.setSpan(new EmojiconSpan(context, icon, emojiSize), i, i + skip, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + text.setSpan(new EmojiconSpan(context, icon, emojiSize, emojiAlignment, textSize), i, i + skip, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } } diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconRecents.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconRecents.java index 195c86ab..8adfe5e3 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconRecents.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconRecents.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,9 +14,9 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon; +package com.rockerhieu.emojicon; -import github.ankushsachdeva.emojicon.emoji.Emojicon; +import com.rockerhieu.emojicon.emoji.Emojicon; import android.content.Context; diff --git a/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconRecentsGridFragment.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconRecentsGridFragment.java new file mode 100644 index 00000000..620c192a --- /dev/null +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconRecentsGridFragment.java @@ -0,0 +1,85 @@ +/* + * Copyright 2014 Hieu Rocker + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.rockerhieu.emojicon; + +import com.rockerhieu.emojicon.emoji.Emojicon; + +import android.content.Context; +import android.os.Bundle; +import android.view.View; +import android.widget.GridView; + +/** + * @author Daniele Ricci + */ +public class EmojiconRecentsGridFragment extends EmojiconGridFragment implements EmojiconRecents { + private EmojiAdapter mAdapter; + private boolean mUseSystemDefault = false; + + private static final String USE_SYSTEM_DEFAULT_KEY = "useSystemDefaults"; + + protected static EmojiconRecentsGridFragment newInstance() { + return newInstance(false); + } + + protected static EmojiconRecentsGridFragment newInstance(boolean useSystemDefault) { + EmojiconRecentsGridFragment fragment = new EmojiconRecentsGridFragment(); + Bundle bundle = new Bundle(); + bundle.putBoolean(USE_SYSTEM_DEFAULT_KEY, useSystemDefault); + fragment.setArguments(bundle); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mUseSystemDefault = getArguments().getBoolean(USE_SYSTEM_DEFAULT_KEY); + } else { + mUseSystemDefault = false; + } + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + EmojiconRecentsManager recents = EmojiconRecentsManager + .getInstance(view.getContext()); + + mAdapter = new EmojiAdapter(view.getContext(), recents, mUseSystemDefault); + GridView gridView = (GridView) view.findViewById(R.id.Emoji_GridView); + gridView.setAdapter(mAdapter); + gridView.setOnItemClickListener(this); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + mAdapter = null; + } + + @Override + public void addRecentEmoji(Context context, Emojicon emojicon) { + EmojiconRecentsManager recents = EmojiconRecentsManager + .getInstance(context); + recents.push(emojicon); + + // notify dataset changed + if (mAdapter != null) + mAdapter.notifyDataSetChanged(); + } + +} diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconRecentsManager.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconRecentsManager.java index 9fbb987e..2c56ce3d 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconRecentsManager.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconRecentsManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,28 +14,28 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon; - -import github.ankushsachdeva.emojicon.emoji.Emojicon; +package com.rockerhieu.emojicon; import java.util.ArrayList; import java.util.StringTokenizer; +import com.rockerhieu.emojicon.emoji.Emojicon; + import android.content.Context; import android.content.SharedPreferences; - /** * @author Daniele Ricci */ public class EmojiconRecentsManager extends ArrayList<Emojicon> { - + private static final String DELIMITER = ","; private static final String PREFERENCE_NAME = "emojicon"; private static final String PREF_RECENTS = "recent_emojis"; private static final String PREF_PAGE = "recent_page"; private static final Object LOCK = new Object(); private static EmojiconRecentsManager sInstance; + private static int maximumSize = 40; private Context mContext; @@ -75,20 +75,39 @@ public class EmojiconRecentsManager extends ArrayList<Emojicon> { @Override public boolean add(Emojicon object) { boolean ret = super.add(object); + + while (this.size() > EmojiconRecentsManager.maximumSize) { + super.remove(0); + } + + saveRecents(); return ret; } @Override public void add(int index, Emojicon object) { super.add(index, object); + + if (index == 0) { + while (this.size() > EmojiconRecentsManager.maximumSize) { + super.remove(EmojiconRecentsManager.maximumSize); + } + } else { + while (this.size() > EmojiconRecentsManager.maximumSize) { + super.remove(0); + } + } + + saveRecents(); } @Override public boolean remove(Object object) { boolean ret = super.remove(object); + saveRecents(); return ret; } - + private SharedPreferences getPreferences() { return mContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); } @@ -96,17 +115,12 @@ public class EmojiconRecentsManager extends ArrayList<Emojicon> { private void loadRecents() { SharedPreferences prefs = getPreferences(); String str = prefs.getString(PREF_RECENTS, ""); - StringTokenizer tokenizer = new StringTokenizer(str, "~"); + StringTokenizer tokenizer = new StringTokenizer(str, EmojiconRecentsManager.DELIMITER); while (tokenizer.hasMoreTokens()) { - try { - add(new Emojicon(tokenizer.nextToken())); - } - catch (NumberFormatException e) { - // ignored - } + add(Emojicon.fromChars(tokenizer.nextToken())); } } - + public void saveRecents() { StringBuilder str = new StringBuilder(); int c = size(); @@ -114,11 +128,14 @@ public class EmojiconRecentsManager extends ArrayList<Emojicon> { Emojicon e = get(i); str.append(e.getEmoji()); if (i < (c - 1)) { - str.append('~'); + str.append(EmojiconRecentsManager.DELIMITER); } } SharedPreferences prefs = getPreferences(); prefs.edit().putString(PREF_RECENTS, str.toString()).commit(); } + public static void setMaximumSize(int maximumSize) { + EmojiconRecentsManager.maximumSize = maximumSize; + } } diff --git a/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconSpan.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconSpan.java new file mode 100644 index 00000000..94ebdc0d --- /dev/null +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconSpan.java @@ -0,0 +1,95 @@ +/* + * Copyright 2014 Hieu Rocker + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.rockerhieu.emojicon; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.drawable.Drawable; +import android.text.style.DynamicDrawableSpan; + +import java.lang.ref.WeakReference; + +/** + * @author Hieu Rocker (rockerhieu@gmail.com) + */ +class EmojiconSpan extends DynamicDrawableSpan { + + private final Context mContext; + + private final int mResourceId; + + private final int mSize; + + private final int mTextSize; + + private int mHeight; + + private int mWidth; + + private int mTop; + + private Drawable mDrawable; + + private WeakReference<Drawable> mDrawableRef; + + public EmojiconSpan(Context context, int resourceId, int size, int alignment, int textSize) { + super(alignment); + mContext = context; + mResourceId = resourceId; + mWidth = mHeight = mSize = size; + mTextSize = textSize; + } + + public Drawable getDrawable() { + if (mDrawable == null) { + try { + mDrawable = mContext.getResources().getDrawable(mResourceId); + mHeight = mSize; + mWidth = mHeight * mDrawable.getIntrinsicWidth() / mDrawable.getIntrinsicHeight(); + mTop = (mTextSize - mHeight) / 2; + mDrawable.setBounds(0, mTop, mWidth, mTop + mHeight); + } catch (Exception e) { + // swallow + } + } + return mDrawable; + } + + @Override + public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { + //super.draw(canvas, text, start, end, x, top, y, bottom, paint); + Drawable b = getCachedDrawable(); + canvas.save(); + + int transY = bottom - b.getBounds().bottom; + if (mVerticalAlignment == ALIGN_BASELINE) { + transY = top + ((bottom - top) / 2) - ((b.getBounds().bottom - b.getBounds().top) / 2) - mTop; + } + + canvas.translate(x, transY); + b.draw(canvas); + canvas.restore(); + } + + private Drawable getCachedDrawable() { + if (mDrawableRef == null || mDrawableRef.get() == null) { + mDrawableRef = new WeakReference<Drawable>(getDrawable()); + } + return mDrawableRef.get(); + } +}
\ No newline at end of file diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconTextView.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconTextView.java index 5822e478..3e502aef 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconTextView.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconTextView.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,13 +14,13 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon; - -import github.ankushsachdeva.emojicon.R; +package com.rockerhieu.emojicon; import android.content.Context; import android.content.res.TypedArray; import android.text.SpannableStringBuilder; +import android.text.TextUtils; +import android.text.style.DynamicDrawableSpan; import android.util.AttributeSet; import android.widget.TextView; @@ -29,8 +29,11 @@ import android.widget.TextView; */ public class EmojiconTextView extends TextView { private int mEmojiconSize; + private int mEmojiconAlignment; + private int mEmojiconTextSize; private int mTextStart = 0; private int mTextLength = -1; + private boolean mUseSystemDefault = false; public EmojiconTextView(Context context) { super(context); @@ -48,13 +51,16 @@ public class EmojiconTextView extends TextView { } private void init(AttributeSet attrs) { + mEmojiconTextSize = (int) getTextSize(); if (attrs == null) { mEmojiconSize = (int) getTextSize(); } else { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.Emojicon); mEmojiconSize = (int) a.getDimension(R.styleable.Emojicon_emojiconSize, getTextSize()); + mEmojiconAlignment = a.getInt(R.styleable.Emojicon_emojiconAlignment, DynamicDrawableSpan.ALIGN_BASELINE); mTextStart = a.getInteger(R.styleable.Emojicon_emojiconTextStart, 0); mTextLength = a.getInteger(R.styleable.Emojicon_emojiconTextLength, -1); + mUseSystemDefault = a.getBoolean(R.styleable.Emojicon_emojiconUseSystemDefault, false); a.recycle(); } setText(getText()); @@ -62,9 +68,12 @@ public class EmojiconTextView extends TextView { @Override public void setText(CharSequence text, BufferType type) { - SpannableStringBuilder builder = new SpannableStringBuilder(text); - EmojiconHandler.addEmojis(getContext(), builder, mEmojiconSize, mTextStart, mTextLength); - super.setText(builder, type); + if (!TextUtils.isEmpty(text)) { + SpannableStringBuilder builder = new SpannableStringBuilder(text); + EmojiconHandler.addEmojis(getContext(), builder, mEmojiconSize, mEmojiconAlignment, mEmojiconTextSize, mTextStart, mTextLength, mUseSystemDefault); + text = builder; + } + super.setText(text, type); } /** @@ -72,5 +81,13 @@ public class EmojiconTextView extends TextView { */ public void setEmojiconSize(int pixels) { mEmojiconSize = pixels; + super.setText(getText()); + } + + /** + * Set whether to use system default emojicon + */ + public void setUseSystemDefault(boolean useSystemDefault) { + mUseSystemDefault = useSystemDefault; } } diff --git a/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconsFragment.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconsFragment.java new file mode 100644 index 00000000..88a2dc4f --- /dev/null +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconsFragment.java @@ -0,0 +1,291 @@ +/* + * Copyright 2014 Hieu Rocker + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.rockerhieu.emojicon; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.os.Handler; +import android.os.SystemClock; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.view.PagerAdapter; +import android.support.v4.view.ViewPager; +import android.view.*; +import android.widget.EditText; +import com.rockerhieu.emojicon.emoji.*; + +import java.util.Arrays; +import java.util.List; + +/** + * @author Hieu Rocker (rockerhieu@gmail.com). + */ +public class EmojiconsFragment extends Fragment implements ViewPager.OnPageChangeListener, EmojiconRecents { + private OnEmojiconBackspaceClickedListener mOnEmojiconBackspaceClickedListener; + private int mEmojiTabLastSelectedIndex = -1; + private View[] mEmojiTabs; + private PagerAdapter mEmojisAdapter; + private EmojiconRecentsManager mRecentsManager; + private boolean mUseSystemDefault = false; + + private static final String USE_SYSTEM_DEFAULT_KEY = "useSystemDefaults"; + + public static EmojiconsFragment newInstance(boolean useSystemDefault) { + EmojiconsFragment fragment = new EmojiconsFragment(); + Bundle bundle = new Bundle(); + bundle.putBoolean(USE_SYSTEM_DEFAULT_KEY, useSystemDefault); + fragment.setArguments(bundle); + return fragment; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.emojicons, container, false); + final ViewPager emojisPager = (ViewPager) view.findViewById(R.id.emojis_pager); + emojisPager.setOnPageChangeListener(this); + // we handle recents + EmojiconRecents recents = this; + mEmojisAdapter = new EmojisPagerAdapter(getFragmentManager(), Arrays.asList( + EmojiconRecentsGridFragment.newInstance(mUseSystemDefault), + EmojiconGridFragment.newInstance(People.DATA, recents, mUseSystemDefault), + EmojiconGridFragment.newInstance(Nature.DATA, recents, mUseSystemDefault), + EmojiconGridFragment.newInstance(Objects.DATA, recents, mUseSystemDefault), + EmojiconGridFragment.newInstance(Places.DATA, recents, mUseSystemDefault), + EmojiconGridFragment.newInstance(Symbols.DATA, recents, mUseSystemDefault) + )); + emojisPager.setAdapter(mEmojisAdapter); + + mEmojiTabs = new View[6]; + mEmojiTabs[0] = view.findViewById(R.id.emojis_tab_0_recents); + mEmojiTabs[1] = view.findViewById(R.id.emojis_tab_1_people); + mEmojiTabs[2] = view.findViewById(R.id.emojis_tab_2_nature); + mEmojiTabs[3] = view.findViewById(R.id.emojis_tab_3_objects); + mEmojiTabs[4] = view.findViewById(R.id.emojis_tab_4_cars); + mEmojiTabs[5] = view.findViewById(R.id.emojis_tab_5_punctuation); + for (int i = 0; i < mEmojiTabs.length; i++) { + final int position = i; + mEmojiTabs[i].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + emojisPager.setCurrentItem(position); + } + }); + } + view.findViewById(R.id.emojis_backspace).setOnTouchListener(new RepeatListener(1000, 50, new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mOnEmojiconBackspaceClickedListener != null) { + mOnEmojiconBackspaceClickedListener.onEmojiconBackspaceClicked(v); + } + } + })); + + // get last selected page + mRecentsManager = EmojiconRecentsManager.getInstance(view.getContext()); + int page = mRecentsManager.getRecentPage(); + // last page was recents, check if there are recents to use + // if none was found, go to page 1 + if (page == 0 && mRecentsManager.size() == 0) { + page = 1; + } + + if (page == 0) { + onPageSelected(page); + } + else { + emojisPager.setCurrentItem(page, false); + } + return view; + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + if (getActivity() instanceof OnEmojiconBackspaceClickedListener) { + mOnEmojiconBackspaceClickedListener = (OnEmojiconBackspaceClickedListener) getActivity(); + } else if(getParentFragment() instanceof OnEmojiconBackspaceClickedListener) { + mOnEmojiconBackspaceClickedListener = (OnEmojiconBackspaceClickedListener) getParentFragment(); + } else { + throw new IllegalArgumentException(activity + " must implement interface " + OnEmojiconBackspaceClickedListener.class.getSimpleName()); + } + } + + @Override + public void onDetach() { + mOnEmojiconBackspaceClickedListener = null; + super.onDetach(); + } + + public static void input(EditText editText, Emojicon emojicon) { + if (editText == null || emojicon == null) { + return; + } + + int start = editText.getSelectionStart(); + int end = editText.getSelectionEnd(); + if (start < 0) { + editText.append(emojicon.getEmoji()); + } else { + editText.getText().replace(Math.min(start, end), Math.max(start, end), emojicon.getEmoji(), 0, emojicon.getEmoji().length()); + } + } + + @Override + public void addRecentEmoji(Context context, Emojicon emojicon) { + final ViewPager emojisPager = (ViewPager) getView().findViewById(R.id.emojis_pager); + EmojiconRecentsGridFragment fragment = (EmojiconRecentsGridFragment) mEmojisAdapter.instantiateItem(emojisPager, 0); + fragment.addRecentEmoji(context, emojicon); + } + + public static void backspace(EditText editText) { + KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL); + editText.dispatchKeyEvent(event); + } + + @Override + public void onPageScrolled(int i, float v, int i2) { + } + + @Override + public void onPageSelected(int i) { + if (mEmojiTabLastSelectedIndex == i) { + return; + } + switch (i) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + if (mEmojiTabLastSelectedIndex >= 0 && mEmojiTabLastSelectedIndex < mEmojiTabs.length) { + mEmojiTabs[mEmojiTabLastSelectedIndex].setSelected(false); + } + mEmojiTabs[i].setSelected(true); + mEmojiTabLastSelectedIndex = i; + mRecentsManager.setRecentPage(i); + break; + } + } + + @Override + public void onPageScrollStateChanged(int i) { + } + + private static class EmojisPagerAdapter extends FragmentStatePagerAdapter { + private List<EmojiconGridFragment> fragments; + + public EmojisPagerAdapter(FragmentManager fm, List<EmojiconGridFragment> fragments) { + super(fm); + this.fragments = fragments; + } + + @Override + public Fragment getItem(int i) { + return fragments.get(i); + } + + @Override + public int getCount() { + return fragments.size(); + } + } + + /** + * A class, that can be used as a TouchListener on any view (e.g. a Button). + * It cyclically runs a clickListener, emulating keyboard-like behaviour. First + * click is fired immediately, next before initialInterval, and subsequent before + * normalInterval. + * <p/> + * <p>Interval is scheduled before the onClick completes, so it has to run fast. + * If it runs slow, it does not generate skipped onClicks. + */ + public static class RepeatListener implements View.OnTouchListener { + + private Handler handler = new Handler(); + + private int initialInterval; + private final int normalInterval; + private final View.OnClickListener clickListener; + + private Runnable handlerRunnable = new Runnable() { + @Override + public void run() { + if (downView == null) { + return; + } + handler.removeCallbacksAndMessages(downView); + handler.postAtTime(this, downView, SystemClock.uptimeMillis() + normalInterval); + clickListener.onClick(downView); + } + }; + + private View downView; + + /** + * @param initialInterval The interval before first click event + * @param normalInterval The interval before second and subsequent click + * events + * @param clickListener The OnClickListener, that will be called + * periodically + */ + public RepeatListener(int initialInterval, int normalInterval, View.OnClickListener clickListener) { + if (clickListener == null) + throw new IllegalArgumentException("null runnable"); + if (initialInterval < 0 || normalInterval < 0) + throw new IllegalArgumentException("negative interval"); + + this.initialInterval = initialInterval; + this.normalInterval = normalInterval; + this.clickListener = clickListener; + } + + public boolean onTouch(View view, MotionEvent motionEvent) { + switch (motionEvent.getAction()) { + case MotionEvent.ACTION_DOWN: + downView = view; + handler.removeCallbacks(handlerRunnable); + handler.postAtTime(handlerRunnable, downView, SystemClock.uptimeMillis() + initialInterval); + clickListener.onClick(view); + return true; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + case MotionEvent.ACTION_OUTSIDE: + handler.removeCallbacksAndMessages(downView); + downView = null; + return true; + } + return false; + } + } + + public interface OnEmojiconBackspaceClickedListener { + void onEmojiconBackspaceClicked(View v); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mUseSystemDefault = getArguments().getBoolean(USE_SYSTEM_DEFAULT_KEY); + } else { + mUseSystemDefault = false; + } + } +} diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconsPopup.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconsPopup.java index 12bc16e0..9a8d8769 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconsPopup.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/EmojiconsPopup.java @@ -14,15 +14,14 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon; +package com.rockerhieu.emojicon; -import github.ankushsachdeva.emojicon.EmojiconGridView.OnEmojiconClickedListener; -import github.ankushsachdeva.emojicon.emoji.Emojicon; -import github.ankushsachdeva.emojicon.emoji.Nature; -import github.ankushsachdeva.emojicon.emoji.Objects; -import github.ankushsachdeva.emojicon.emoji.People; -import github.ankushsachdeva.emojicon.emoji.Places; -import github.ankushsachdeva.emojicon.emoji.Symbols; +import com.rockerhieu.emojicon.emoji.Emojicon; +import com.rockerhieu.emojicon.emoji.Nature; +import com.rockerhieu.emojicon.emoji.Objects; +import com.rockerhieu.emojicon.emoji.People; +import com.rockerhieu.emojicon.emoji.Places; +import com.rockerhieu.emojicon.emoji.Symbols; import java.util.Arrays; import java.util.List; @@ -32,6 +31,10 @@ import android.content.Context; import android.graphics.Rect; import android.os.Handler; import android.os.SystemClock; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.app.FragmentManager; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.Gravity; @@ -43,6 +46,7 @@ import android.view.ViewGroup; import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.WindowManager.LayoutParams; import android.widget.PopupWindow; +import com.rockerhieu.emojicon.EmojiconGridFragment.OnEmojiconClickedListener; /** @@ -62,6 +66,7 @@ public class EmojiconsPopup extends PopupWindow implements ViewPager.OnPageChang OnSoftKeyboardOpenCloseListener onSoftKeyboardOpenCloseListener; View rootView; Context mContext; + private boolean mUseSystemDefault = false; private ViewPager emojisPager; /** @@ -93,6 +98,7 @@ public class EmojiconsPopup extends PopupWindow implements ViewPager.OnPageChang this.onEmojiconClickedListener = listener; } + /** * Set the listener for the event when backspace on emojicon popup is clicked */ @@ -200,16 +206,14 @@ public class EmojiconsPopup extends PopupWindow implements ViewPager.OnPageChang emojisPager = (ViewPager) view.findViewById(R.id.emojis_pager); emojisPager.setOnPageChangeListener(this); EmojiconRecents recents = this; - mEmojisAdapter = new EmojisPagerAdapter( - Arrays.asList( - new EmojiconRecentsGridView(mContext, null, null, this), - new EmojiconGridView(mContext, People.DATA, recents, this), - new EmojiconGridView(mContext, Nature.DATA, recents, this), - new EmojiconGridView(mContext, Objects.DATA, recents, this), - new EmojiconGridView(mContext, Places.DATA, recents, this), - new EmojiconGridView(mContext, Symbols.DATA, recents, this) - ) - ); + mEmojisAdapter = new EmojisPagerAdapter(Arrays.asList( + EmojiconRecentsGridFragment.newInstance(mUseSystemDefault), + EmojiconGridFragment.newInstance(People.DATA, recents, mUseSystemDefault), + EmojiconGridFragment.newInstance(Nature.DATA, recents, mUseSystemDefault), + EmojiconGridFragment.newInstance(Objects.DATA, recents, mUseSystemDefault), + EmojiconGridFragment.newInstance(Places.DATA, recents, mUseSystemDefault), + EmojiconGridFragment.newInstance(Symbols.DATA, recents, mUseSystemDefault) + )); emojisPager.setAdapter(mEmojisAdapter); mEmojiTabs = new View[6]; mEmojiTabs[0] = view.findViewById(R.id.emojis_tab_0_recents); @@ -256,7 +260,7 @@ public class EmojiconsPopup extends PopupWindow implements ViewPager.OnPageChang @Override public void addRecentEmoji(Context context, Emojicon emojicon) { - EmojiconRecentsGridView fragment = ((EmojisPagerAdapter)emojisPager.getAdapter()).getRecentFragment(); + EmojiconRecentsGridFragment fragment = ((EmojisPagerAdapter)emojisPager.getAdapter()).getRecentFragment(); fragment.addRecentEmoji(context, emojicon); } @@ -292,28 +296,29 @@ public class EmojiconsPopup extends PopupWindow implements ViewPager.OnPageChang } private static class EmojisPagerAdapter extends PagerAdapter { - private List<EmojiconGridView> views; - public EmojiconRecentsGridView getRecentFragment(){ - for (EmojiconGridView it : views) { - if(it instanceof EmojiconRecentsGridView) - return (EmojiconRecentsGridView)it; + private List<EmojiconGridFragment> fragments; + + public EmojiconRecentsGridFragment getRecentFragment(){ + for (EmojiconGridFragment it : fragments) { + if(it instanceof EmojiconRecentsGridFragment) + return (EmojiconRecentsGridFragment)it; } return null; } - public EmojisPagerAdapter(List<EmojiconGridView> views) { + + public EmojisPagerAdapter(List<EmojiconGridFragment> fragments) { super(); - this.views = views; + this.fragments = fragments; } @Override public int getCount() { - return views.size(); + return fragments.size(); } - @Override public Object instantiateItem(ViewGroup container, int position) { - View v = views.get(position).rootView; + View v = fragments.get(position).getView(); ((ViewPager)container).addView(v, 0); return v; } @@ -380,18 +385,18 @@ public class EmojiconsPopup extends PopupWindow implements ViewPager.OnPageChang public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { - case MotionEvent.ACTION_DOWN: - downView = view; - handler.removeCallbacks(handlerRunnable); - handler.postAtTime(handlerRunnable, downView, SystemClock.uptimeMillis() + initialInterval); - clickListener.onClick(view); - return true; - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_CANCEL: - case MotionEvent.ACTION_OUTSIDE: - handler.removeCallbacksAndMessages(downView); - downView = null; - return true; + case MotionEvent.ACTION_DOWN: + downView = view; + handler.removeCallbacks(handlerRunnable); + handler.postAtTime(handlerRunnable, downView, SystemClock.uptimeMillis() + initialInterval); + clickListener.onClick(view); + return true; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + case MotionEvent.ACTION_OUTSIDE: + handler.removeCallbacksAndMessages(downView); + downView = null; + return true; } return false; } diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Emojicon.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Emojicon.java index 2ba8ca1e..db4088cf 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Emojicon.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Emojicon.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,20 +14,60 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon.emoji; +package com.rockerhieu.emojicon.emoji; -import java.io.Serializable; +import android.os.Parcel; +import android.os.Parcelable; /** * @author Hieu Rocker (rockerhieu@gmail.com) */ -public class Emojicon implements Serializable { - private static final long serialVersionUID = 1L; +public class Emojicon implements Parcelable { + + public static final Creator<Emojicon> CREATOR = new Creator<Emojicon>() { + @Override + public Emojicon createFromParcel(Parcel in) { + return new Emojicon(in); + } + + @Override + public Emojicon[] newArray(int size) { + return new Emojicon[size]; + } + }; + + private int icon; + + private char value; + private String emoji; + public Emojicon(int icon, char value, String emoji) { + this.icon = icon; + this.value = value; + this.emoji = emoji; + } + + public Emojicon(Parcel in) { + this.icon = in.readInt(); + this.value = (char) in.readInt(); + this.emoji = in.readString(); + } + private Emojicon() { } + public Emojicon(String emoji) { + this.emoji = emoji; + } + + public static Emojicon fromResource(int icon, int value) { + Emojicon emoji = new Emojicon(); + emoji.icon = icon; + emoji.value = (char) value; + return emoji; + } + public static Emojicon fromCodePoint(int codePoint) { Emojicon emoji = new Emojicon(); emoji.emoji = newString(codePoint); @@ -46,8 +86,32 @@ public class Emojicon implements Serializable { return emoji; } - public Emojicon(String emoji) { - this.emoji = emoji; + public static final String newString(int codePoint) { + if (Character.charCount(codePoint) == 1) { + return String.valueOf(codePoint); + } else { + return new String(Character.toChars(codePoint)); + } + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(icon); + dest.writeInt(value); + dest.writeString(emoji); + } + + public char getValue() { + return value; + } + + public int getIcon() { + return icon; } public String getEmoji() { @@ -64,11 +128,4 @@ public class Emojicon implements Serializable { return emoji.hashCode(); } - public static final String newString(int codePoint) { - if (Character.charCount(codePoint) == 1) { - return String.valueOf(codePoint); - } else { - return new String(Character.toChars(codePoint)); - } - } } diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Nature.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Nature.java index b354c9a0..7d43906b 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Nature.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Nature.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon.emoji; +package com.rockerhieu.emojicon.emoji; /** * @author Hieu Rocker (rockerhieu@gmail.com) diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Objects.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Objects.java index 222c9bac..354dccc6 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Objects.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Objects.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon.emoji; +package com.rockerhieu.emojicon.emoji; /** * @author Hieu Rocker (rockerhieu@gmail.com) diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/People.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/People.java index 93e34f59..c64da08e 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/People.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/People.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon.emoji; +package com.rockerhieu.emojicon.emoji; /** * @author Hieu Rocker (rockerhieu@gmail.com) diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Places.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Places.java index cf2f0e5a..617cf61e 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Places.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Places.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon.emoji; +package com.rockerhieu.emojicon.emoji; /** * @author Hieu Rocker (rockerhieu@gmail.com) diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Symbols.java b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Symbols.java index b81c322f..82655946 100644 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/emoji/Symbols.java +++ b/libs/emojicon/src/main/java/com/rockerhieu/emojicon/emoji/Symbols.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Ankush Sachdeva + * Copyright 2014 Hieu Rocker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package github.ankushsachdeva.emojicon.emoji; +package com.rockerhieu.emojicon.emoji; /** * @author Hieu Rocker (rockerhieu@gmail.com) diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconGridView.java b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconGridView.java deleted file mode 100644 index 38df8b13..00000000 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconGridView.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2014 Ankush Sachdeva - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github.ankushsachdeva.emojicon; - -import github.ankushsachdeva.emojicon.emoji.Emojicon; -import github.ankushsachdeva.emojicon.emoji.People; - -import java.util.Arrays; - -import android.app.Activity; -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.GridView; - -import github.ankushsachdeva.emojicon.R; - -/** - * @author Hieu Rocker (rockerhieu@gmail.com) - * @author Ankush Sachdeva (sankush@yahoo.co.in) - */ -public class EmojiconGridView{ - public View rootView; - EmojiconsPopup mEmojiconPopup; - EmojiconRecents mRecents; - Emojicon[] mData; - - public EmojiconGridView(Context context, Emojicon[] emojicons, EmojiconRecents recents, EmojiconsPopup emojiconPopup) { - LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); - mEmojiconPopup = emojiconPopup; - rootView = inflater.inflate(R.layout.emojicon_grid, null); - setRecents(recents); - GridView gridView = (GridView) rootView.findViewById(R.id.Emoji_GridView); - if (emojicons== null) { - mData = People.DATA; - } else { - Object[] o = (Object[]) emojicons; - mData = Arrays.asList(o).toArray(new Emojicon[o.length]); - } - EmojiAdapter mAdapter = new EmojiAdapter(rootView.getContext(), mData); - mAdapter.setEmojiClickListener(new OnEmojiconClickedListener() { - - @Override - public void onEmojiconClicked(Emojicon emojicon) { - if (mEmojiconPopup.onEmojiconClickedListener != null) { - mEmojiconPopup.onEmojiconClickedListener.onEmojiconClicked(emojicon); - } - if (mRecents != null) { - mRecents.addRecentEmoji(rootView.getContext(), emojicon); - } - } - }); - gridView.setAdapter(mAdapter); - } - - private void setRecents(EmojiconRecents recents) { - mRecents = recents; - } - - public interface OnEmojiconClickedListener { - void onEmojiconClicked(Emojicon emojicon); - } - -} diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconRecentsGridView.java b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconRecentsGridView.java deleted file mode 100644 index 2aecbb59..00000000 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconRecentsGridView.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2014 Ankush Sachdeva - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github.ankushsachdeva.emojicon; - -import github.ankushsachdeva.emojicon.emoji.Emojicon; -import android.content.Context; -import android.widget.GridView; - -import github.ankushsachdeva.emojicon.R; - -/** - * @author Daniele Ricci - * @author Ankush Sachdeva (sankush@yahoo.co.in) - */ -public class EmojiconRecentsGridView extends EmojiconGridView implements EmojiconRecents { - EmojiAdapter mAdapter; - - public EmojiconRecentsGridView(Context context, Emojicon[] emojicons, - EmojiconRecents recents,EmojiconsPopup emojiconsPopup) { - super(context, emojicons, recents, emojiconsPopup); - EmojiconRecentsManager recents1 = EmojiconRecentsManager - .getInstance(rootView.getContext()); - mAdapter = new EmojiAdapter(rootView.getContext(), recents1); - mAdapter.setEmojiClickListener(new OnEmojiconClickedListener() { - - @Override - public void onEmojiconClicked(Emojicon emojicon) { - if (mEmojiconPopup.onEmojiconClickedListener != null) { - mEmojiconPopup.onEmojiconClickedListener.onEmojiconClicked(emojicon); - } - } - }); - GridView gridView = (GridView) rootView.findViewById(R.id.Emoji_GridView); - gridView.setAdapter(mAdapter); - } - - @Override - public void addRecentEmoji(Context context, Emojicon emojicon) { - EmojiconRecentsManager recents = EmojiconRecentsManager - .getInstance(context); - recents.push(emojicon); - - // notify dataset changed - if (mAdapter != null) - mAdapter.notifyDataSetChanged(); - } - -} diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconSpan.java b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconSpan.java deleted file mode 100644 index 1afeea95..00000000 --- a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/EmojiconSpan.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2014 Ankush Sachdeva - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package github.ankushsachdeva.emojicon; - -import android.content.Context; -import android.graphics.drawable.Drawable; -import android.text.style.DynamicDrawableSpan; - -/** - * @author Hieu Rocker (rockerhieu@gmail.com) - */ -class EmojiconSpan extends DynamicDrawableSpan { - private final Context mContext; - private final int mResourceId; - private final int mSize; - private Drawable mDrawable; - - public EmojiconSpan(Context context, int resourceId, int size) { - super(); - mContext = context; - mResourceId = resourceId; - mSize = size; - } - - public Drawable getDrawable() { - if (mDrawable == null) { - try { - mDrawable = mContext.getResources().getDrawable(mResourceId); - int size = mSize; - mDrawable.setBounds(0, 0, size, size); - } catch (Exception e) { - // swallow - } - } - return mDrawable; - } -}
\ No newline at end of file |