|
6 | 6 | import android.text.Spannable; |
7 | 7 |
|
8 | 8 | import java.util.ArrayList; |
| 9 | +import java.util.List; |
9 | 10 |
|
10 | | -import cn.hadcn.keyboard.emoticon.db.EmoticonDBHelper; |
11 | 11 | import cn.hadcn.keyboard.emoticon.EmoticonBean; |
| 12 | +import cn.hadcn.keyboard.emoticon.db.EmoticonDBHelper; |
12 | 13 | import cn.hadcn.keyboard.utils.EmoticonLoader; |
13 | 14 | import cn.hadcn.keyboard.view.VerticalImageSpan; |
14 | 15 |
|
|
17 | 18 | * Created by 90Chris on 2015/11/25. |
18 | 19 | */ |
19 | 20 | public class EmoticonHandler { |
20 | | - private static ArrayList<EmoticonBean> mEmoticonBeans = new ArrayList<>(); |
| 21 | + private List<EmoticonBean> mEmoticonBeans = new ArrayList<>(); |
21 | 22 | private static EmoticonHandler sEmoticonHandler = null; |
22 | 23 | private Context mContext; |
23 | | - EmoticonDBHelper emoticonDbHelper = null; |
| 24 | + private EmoticonDBHelper emoticonDbHelper = null; |
24 | 25 |
|
25 | | - public static EmoticonHandler getInstance( @NonNull Context context ) { |
26 | | - if ( sEmoticonHandler == null ) { |
27 | | - sEmoticonHandler = new EmoticonHandler( context ); |
| 26 | + public static EmoticonHandler getInstance(@NonNull Context context) { |
| 27 | + if (sEmoticonHandler == null) { |
| 28 | + sEmoticonHandler = new EmoticonHandler(context.getApplicationContext()); |
28 | 29 | } |
29 | 30 | return sEmoticonHandler; |
30 | 31 | } |
31 | 32 |
|
32 | | - private EmoticonHandler( Context context ) { |
| 33 | + private EmoticonHandler(Context context) { |
33 | 34 | mContext = context; |
34 | 35 | emoticonDbHelper = new EmoticonDBHelper(context); |
35 | 36 | } |
36 | 37 |
|
37 | 38 | public EmoticonDBHelper getEmoticonDbHelper() { |
38 | | - if ( emoticonDbHelper == null ) { |
| 39 | + if (emoticonDbHelper == null) { |
39 | 40 | emoticonDbHelper = new EmoticonDBHelper(mContext); |
40 | 41 | } |
41 | 42 | return emoticonDbHelper; |
42 | 43 | } |
43 | 44 |
|
44 | | - public ArrayList<EmoticonBean> loadEmoticonsToMemory() { |
| 45 | + public List<EmoticonBean> loadEmoticonsToMemory() { |
45 | 46 | mEmoticonBeans = emoticonDbHelper.queryAllEmoticonBeans(); |
46 | 47 | emoticonDbHelper.cleanup(); |
47 | 48 |
|
48 | 49 | return mEmoticonBeans; |
49 | 50 | } |
50 | 51 |
|
51 | | - public String getEmoticonUriByTag( String tag ) { |
| 52 | + public String getEmoticonUriByTag(String tag) { |
52 | 53 | return emoticonDbHelper.getUriByTag(tag); |
53 | 54 | } |
54 | 55 |
|
55 | | - public void setTextFace( String content, Spannable spannable, int start, int size ) { |
56 | | - if ( mEmoticonBeans == null ) { |
| 56 | + public void setTextFace(String content, Spannable spannable, int start, int size) { |
| 57 | + if (mEmoticonBeans == null) { |
57 | 58 | mEmoticonBeans = emoticonDbHelper.queryAllEmoticonBeans(); |
58 | 59 | emoticonDbHelper.cleanup(); |
59 | 60 | } |
60 | | - if ( content.length() <= 0 ) { |
| 61 | + if (content.length() <= 0) { |
61 | 62 | return; |
62 | 63 | } |
63 | 64 | int keyIndex = start; |
64 | | - for ( EmoticonBean bean : mEmoticonBeans ) { |
| 65 | + for (EmoticonBean bean : mEmoticonBeans) { |
65 | 66 | String key = bean.getTag(); |
66 | 67 | int keyLength = key.length(); |
67 | | - while ( keyIndex >= 0 ) { |
| 68 | + while (keyIndex >= 0) { |
68 | 69 | keyIndex = content.indexOf(key, keyIndex); //when do not find, get -1 |
69 | | - if ( keyIndex < 0 ) { |
| 70 | + if (keyIndex < 0) { |
70 | 71 | break; |
71 | 72 | } |
72 | | - Drawable drawable = EmoticonLoader.getInstance(mContext).getDrawable(bean.getIconUri()); |
| 73 | + Drawable drawable = EmoticonLoader.getInstance(mContext).getDrawable(bean |
| 74 | + .getIconUri()); |
73 | 75 | drawable.setBounds(0, 0, size, size); |
74 | 76 | VerticalImageSpan imageSpan = new VerticalImageSpan(drawable); |
75 | | - spannable.setSpan(imageSpan, keyIndex, keyIndex + keyLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 77 | + spannable.setSpan(imageSpan, keyIndex, keyIndex + keyLength, Spannable |
| 78 | + .SPAN_EXCLUSIVE_EXCLUSIVE); |
76 | 79 | keyIndex += keyLength; |
77 | 80 | } |
78 | 81 | keyIndex = start; |
|
0 commit comments