Skip to content

Commit 3372858

Browse files
QunheQunhe
authored andcommitted
fix memory leak
1 parent 7e32ee2 commit 3372858

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

keyboard/src/main/java/cn/hadcn/keyboard/emoticon/util/EmoticonHandler.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import android.text.Spannable;
77

88
import java.util.ArrayList;
9+
import java.util.List;
910

10-
import cn.hadcn.keyboard.emoticon.db.EmoticonDBHelper;
1111
import cn.hadcn.keyboard.emoticon.EmoticonBean;
12+
import cn.hadcn.keyboard.emoticon.db.EmoticonDBHelper;
1213
import cn.hadcn.keyboard.utils.EmoticonLoader;
1314
import cn.hadcn.keyboard.view.VerticalImageSpan;
1415

@@ -17,62 +18,64 @@
1718
* Created by 90Chris on 2015/11/25.
1819
*/
1920
public class EmoticonHandler {
20-
private static ArrayList<EmoticonBean> mEmoticonBeans = new ArrayList<>();
21+
private List<EmoticonBean> mEmoticonBeans = new ArrayList<>();
2122
private static EmoticonHandler sEmoticonHandler = null;
2223
private Context mContext;
23-
EmoticonDBHelper emoticonDbHelper = null;
24+
private EmoticonDBHelper emoticonDbHelper = null;
2425

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());
2829
}
2930
return sEmoticonHandler;
3031
}
3132

32-
private EmoticonHandler( Context context ) {
33+
private EmoticonHandler(Context context) {
3334
mContext = context;
3435
emoticonDbHelper = new EmoticonDBHelper(context);
3536
}
3637

3738
public EmoticonDBHelper getEmoticonDbHelper() {
38-
if ( emoticonDbHelper == null ) {
39+
if (emoticonDbHelper == null) {
3940
emoticonDbHelper = new EmoticonDBHelper(mContext);
4041
}
4142
return emoticonDbHelper;
4243
}
4344

44-
public ArrayList<EmoticonBean> loadEmoticonsToMemory() {
45+
public List<EmoticonBean> loadEmoticonsToMemory() {
4546
mEmoticonBeans = emoticonDbHelper.queryAllEmoticonBeans();
4647
emoticonDbHelper.cleanup();
4748

4849
return mEmoticonBeans;
4950
}
5051

51-
public String getEmoticonUriByTag( String tag ) {
52+
public String getEmoticonUriByTag(String tag) {
5253
return emoticonDbHelper.getUriByTag(tag);
5354
}
5455

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) {
5758
mEmoticonBeans = emoticonDbHelper.queryAllEmoticonBeans();
5859
emoticonDbHelper.cleanup();
5960
}
60-
if ( content.length() <= 0 ) {
61+
if (content.length() <= 0) {
6162
return;
6263
}
6364
int keyIndex = start;
64-
for ( EmoticonBean bean : mEmoticonBeans ) {
65+
for (EmoticonBean bean : mEmoticonBeans) {
6566
String key = bean.getTag();
6667
int keyLength = key.length();
67-
while ( keyIndex >= 0 ) {
68+
while (keyIndex >= 0) {
6869
keyIndex = content.indexOf(key, keyIndex); //when do not find, get -1
69-
if ( keyIndex < 0 ) {
70+
if (keyIndex < 0) {
7071
break;
7172
}
72-
Drawable drawable = EmoticonLoader.getInstance(mContext).getDrawable(bean.getIconUri());
73+
Drawable drawable = EmoticonLoader.getInstance(mContext).getDrawable(bean
74+
.getIconUri());
7375
drawable.setBounds(0, 0, size, size);
7476
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);
7679
keyIndex += keyLength;
7780
}
7881
keyIndex = start;

0 commit comments

Comments
 (0)