Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
Support huaji
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed Oct 15, 2015
1 parent b3ba2c2 commit 11d50c3
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 8 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/com/hippo/nimingban/NMBApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
import com.hippo.nimingban.util.Crash;
import com.hippo.nimingban.util.DB;
import com.hippo.nimingban.util.ReadableTime;
import com.hippo.nimingban.util.ResImageGetter;
import com.hippo.nimingban.util.Settings;
import com.hippo.nimingban.widget.SimpleDrawableHelper;
import com.hippo.okhttp.GoodHttpClient;
import com.hippo.okhttp.GoodRequestBuilder;
import com.hippo.okhttp.ResponseUtils;
import com.hippo.text.Html;
import com.hippo.util.NetworkUtils;
import com.hippo.yorozuya.FileUtils;
import com.hippo.yorozuya.Messenger;
Expand Down Expand Up @@ -85,6 +87,8 @@ public void onCreate() {
GoodRequestBuilder.initialize(this);
ResponseUtils.initialize(this);
BitmapUtils.initialize(this);
Html.initialize(this);
ResImageGetter.initialize(this);

LeakCanary.install(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void generate(Site site) {
mUser = Html.fromHtml(userid);
}

mContent = ACPost.generateContent(content);
mContent = ACPost.generateContent(content, "", title, name, email);

if (!TextUtils.isEmpty(img)) {
mThumb = ACUrl.HOST + "/Public/Upload/thumb/" + img + ext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@
import android.text.style.StyleSpan;
import android.text.style.URLSpan;

import com.hippo.nimingban.R;
import com.hippo.nimingban.client.ReferenceSpan;
import com.hippo.nimingban.client.ac.ACUrl;
import com.hippo.nimingban.client.data.ACSite;
import com.hippo.nimingban.client.data.Post;
import com.hippo.nimingban.client.data.Reply;
import com.hippo.nimingban.client.data.Site;
import com.hippo.nimingban.util.ResImageGetter;
import com.hippo.text.Html;
import com.hippo.yorozuya.NumberUtils;
import com.hippo.yorozuya.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -271,7 +274,8 @@ public static CharSequence handleEmail(CharSequence content, String email) {

public static CharSequence generateContent(String content) {
CharSequence charSequence;
charSequence = Html.fromHtml(content);
charSequence = Html.fromHtml(StringUtils.replace(content, "#滑稽", "<img src=\"" + R.drawable.emoji_huaji + "\">"),
new ResImageGetter(), null);
charSequence = fixURLSpan(charSequence);
charSequence = handleReference(charSequence);
charSequence = handleTextUrl(charSequence);
Expand Down
49 changes: 49 additions & 0 deletions app/src/main/java/com/hippo/nimingban/util/ResImageGetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2015 Hippo Seven
*
* 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.hippo.nimingban.util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;

import com.hippo.text.Html;
import com.hippo.yorozuya.StringUtils;

public class ResImageGetter implements Html.ImageGetter {

private static Resources sResources;

public static void initialize(Context context) {
sResources = context.getResources();
}

@Override
public Drawable getDrawable(String source) {
if (StringUtils.isAllDigit(source)) {
int resId = Integer.parseInt(source);
Drawable d = sResources.getDrawable(resId);
if (d != null) {
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
} else {
return null;
}
} else {
return null;
}
}
}
21 changes: 16 additions & 5 deletions app/src/main/java/com/hippo/text/Html.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hippo.text;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
Expand Down Expand Up @@ -67,6 +68,13 @@
* Not all HTML tags are supported.
*/
public class Html {

static Resources sResources;

public static void initialize(Context context) {
sResources = context.getResources();
}

/**
* Retrieves images for HTML &lt;img&gt; tags.
*/
Expand Down Expand Up @@ -654,8 +662,7 @@ private static void startImg(SpannableStringBuilder text,
}

if (d == null) {
d = Resources.getSystem().
getDrawable(R.drawable.unknown_image);
d = Html.sResources.getDrawable(R.drawable.unknown_image);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
}

Expand Down Expand Up @@ -876,9 +883,13 @@ public Header(int level) {
}

/**
* Converts an HTML color (named or numeric) to an integer RGB value.
*
* @param colorString Non-null color string.
* Parse the color string, and return the corresponding color-int.
* If the string cannot be parsed, throws an IllegalArgumentException
* exception. Supported formats are:
* #RRGGBB
* #AARRGGBB
* rgb(255, 255, 255)
* or color name
*/
@ColorInt
public static int getHtmlColor(@NonNull String colorString) {
Expand Down
Binary file added app/src/main/res/drawable-hdpi/emoji_huaji.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/emoji_huaji.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/emoji_huaji.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion yorozuya

0 comments on commit 11d50c3

Please sign in to comment.