|
| 1 | +package net.coding.program.common; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.res.AssetManager; |
| 7 | +import android.text.Html; |
| 8 | +import android.text.Spannable; |
| 9 | +import android.text.style.ImageSpan; |
| 10 | +import android.text.style.QuoteSpan; |
| 11 | +import android.text.style.URLSpan; |
| 12 | +import android.view.View; |
| 13 | +import android.webkit.WebView; |
| 14 | + |
| 15 | +import net.coding.program.MyApp; |
| 16 | +import net.coding.program.WebActivity; |
| 17 | +import net.coding.program.common.activity.WebviewDetailActivity_; |
| 18 | +import net.coding.program.common.enter.DrawableTool; |
| 19 | +import net.coding.program.common.enter.GifImageSpan; |
| 20 | +import net.coding.program.common.htmltext.GrayQuoteSpan; |
| 21 | +import net.coding.program.common.htmltext.URLSpanNoUnderline; |
| 22 | +import net.coding.program.login.auth.AuthListActivity; |
| 23 | +import net.coding.program.login.auth.Login2FATipActivity; |
| 24 | +import net.coding.program.model.AccountInfo; |
| 25 | +import net.coding.program.model.GitFileObject; |
| 26 | +import net.coding.program.user.UserDetailActivity_; |
| 27 | + |
| 28 | +import pl.droidsonroids.gif.GifDrawable; |
| 29 | + |
| 30 | +/** |
| 31 | + * Created by chenchao on 2017/11/22. |
| 32 | + */ |
| 33 | + |
| 34 | +public class GlobalCommon { |
| 35 | + public static View.OnClickListener clickUser = v -> { |
| 36 | + String globalKey = (String) v.getTag(); |
| 37 | + UserDetailActivity_.intent(v.getContext()) |
| 38 | + .globalKey(globalKey) |
| 39 | + .start(); |
| 40 | + }; |
| 41 | + public static View.OnClickListener clickJumpWebView = v -> { |
| 42 | + Object object = v.getTag(); |
| 43 | + if (object instanceof String) { |
| 44 | + WebviewDetailActivity_.intent(v.getContext()) |
| 45 | + .comment((String) object) |
| 46 | + .start(); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + public static int dpToPx(int dpValue) { |
| 51 | + return (int) (dpValue * MyApp.sScale + 0.5f); |
| 52 | + } |
| 53 | + |
| 54 | + public static int dpToPx(double dpValue) { |
| 55 | + return (int) (dpValue * MyApp.sScale + 0.5f); |
| 56 | + } |
| 57 | + |
| 58 | + public static int pxToDp(float pxValue) { |
| 59 | + return (int) (pxValue / MyApp.sScale + 0.5f); |
| 60 | + } |
| 61 | + |
| 62 | + public static void start2FAActivity(Activity activity) { |
| 63 | + Intent intent; |
| 64 | + if (AccountInfo.loadAuthDatas(activity).isEmpty()) { |
| 65 | + intent = new Intent(activity, Login2FATipActivity.class); |
| 66 | + } else { |
| 67 | + intent = new Intent(activity, AuthListActivity.class); |
| 68 | + } |
| 69 | + |
| 70 | + activity.startActivity(intent); |
| 71 | + } |
| 72 | + |
| 73 | + static public void setWebViewContent(WebView webview, GitFileObject gitFile) { |
| 74 | + Context context = webview.getContext(); |
| 75 | + if (gitFile.lang.equals("markdown")) { |
| 76 | + try { |
| 77 | + String template = Global.readTextFile(context.getAssets().open("markdown.html")); |
| 78 | + webview.loadDataWithBaseURL(Global.HOST, template.replace("${webview_content}", gitFile.preview), "text/html", "UTF-8", null); |
| 79 | + |
| 80 | + } catch (Exception e) { |
| 81 | + Global.errorLog(e); |
| 82 | + } |
| 83 | + } else { |
| 84 | + try { |
| 85 | + String template = Global.readTextFile(context.getAssets().open("code.html")); |
| 86 | + String replaceData = gitFile.data.replace("<", "<").replace(">", ">").replace("\u2028", "").replace("\u2029", ""); |
| 87 | + webview.loadDataWithBaseURL(Global.HOST, template.replace("${file_code}", replaceData).replace("${file_lang}", gitFile.lang), "text/html", "UTF-8", null); |
| 88 | + } catch (Exception e) { |
| 89 | + Global.errorLog(e); |
| 90 | + } |
| 91 | + } |
| 92 | + webview.setWebViewClient(new WebActivity.CustomWebViewClient(webview.getContext())); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + public static Spannable changeHyperlinkColor(String content) { |
| 97 | + return changeHyperlinkColor(content, null, null); |
| 98 | + } |
| 99 | + |
| 100 | + public static Spannable changeHyperlinkColor(String content, MyImageGetter imageGetter) { |
| 101 | + return changeHyperlinkColor(content, imageGetter, null); |
| 102 | + } |
| 103 | + |
| 104 | + public static Spannable changeHyperlinkColor(String content, int linkColor) { |
| 105 | + return changeHyperlinkColor(content, null, Global.tagHandler, linkColor); |
| 106 | + } |
| 107 | + |
| 108 | + public static Spannable changeHyperlinkColor(String content, int color, MyImageGetter imageGetter) { |
| 109 | + return changeHyperlinkColor(content, imageGetter, null, color); |
| 110 | + } |
| 111 | + |
| 112 | + public static Spannable changeHyperlinkColor(String content, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) { |
| 113 | + return changeHyperlinkColor(content, imageGetter, tagHandler, CodingColor.fontGreen); |
| 114 | + } |
| 115 | + |
| 116 | + public static Spannable changeHyperlinkColorMaopao(String content, Html.ImageGetter imageGetter, Html.TagHandler tagHandler, AssetManager assetManager) { |
| 117 | + Spannable s = changeHyperlinkColor(content, imageGetter, tagHandler, CodingColor.fontGreen); |
| 118 | + return spannToGif(s, assetManager); |
| 119 | + } |
| 120 | + |
| 121 | + public static Spannable changeHyperlinkColor(String content, Html.ImageGetter imageGetter, Html.TagHandler tagHandler, int color) { |
| 122 | + Spannable s = (Spannable) Html.fromHtml(content, imageGetter, tagHandler); |
| 123 | + return getCustomSpannable(color, s); |
| 124 | + } |
| 125 | + |
| 126 | + static Spannable getCustomSpannable(int color, Spannable s) { |
| 127 | + URLSpan[] urlSpan = s.getSpans(0, s.length(), URLSpan.class); |
| 128 | + for (URLSpan span : urlSpan) { |
| 129 | + int start = s.getSpanStart(span); |
| 130 | + int end = s.getSpanEnd(span); |
| 131 | + s.removeSpan(span); |
| 132 | + span = new URLSpanNoUnderline(span.getURL(), color); |
| 133 | + s.setSpan(span, start, end, 0); |
| 134 | + } |
| 135 | + |
| 136 | + QuoteSpan quoteSpans[] = s.getSpans(0, s.length(), QuoteSpan.class); |
| 137 | + for (QuoteSpan span : quoteSpans) { |
| 138 | + int start = s.getSpanStart(span); |
| 139 | + int end = s.getSpanEnd(span); |
| 140 | + s.removeSpan(span); |
| 141 | + GrayQuoteSpan grayQuoteSpan = new GrayQuoteSpan(); |
| 142 | + s.setSpan(grayQuoteSpan, start, end, 0); |
| 143 | + } |
| 144 | + |
| 145 | + return s; |
| 146 | + } |
| 147 | + |
| 148 | + public static Spannable recentMessage(String content, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) { |
| 149 | + String parse = HtmlContent.parseToText(content); |
| 150 | + |
| 151 | + Spannable s = (Spannable) Html.fromHtml(parse, imageGetter, null); |
| 152 | + return GlobalCommon.getCustomSpannable(CodingColor.font3, s); |
| 153 | + } |
| 154 | + |
| 155 | + private static Spannable spannToGif(Spannable s, AssetManager assetManager) { |
| 156 | + ImageSpan[] imageSpans = s.getSpans(0, s.length(), ImageSpan.class); |
| 157 | + |
| 158 | + final String[] gifEmojiName = new String[]{ |
| 159 | + "festival-emoji-01.gif", |
| 160 | + "festival-emoji-02.gif", |
| 161 | + "festival-emoji-03.gif", |
| 162 | + "festival-emoji-04.gif", |
| 163 | + "festival-emoji-05.gif", |
| 164 | + "festival-emoji-06.gif", |
| 165 | + "festival-emoji-07.gif", |
| 166 | + "festival-emoji-08.gif", |
| 167 | + }; |
| 168 | + |
| 169 | + for (ImageSpan imageSpan : imageSpans) { |
| 170 | + int start = s.getSpanStart(imageSpan); |
| 171 | + int end = s.getSpanEnd(imageSpan); |
| 172 | + |
| 173 | + String imageSource = imageSpan.getSource(); |
| 174 | + for (String endString : gifEmojiName) { |
| 175 | + if (imageSource.endsWith(endString)) { |
| 176 | + try { |
| 177 | + GifDrawable gifDrawable = new GifDrawable(assetManager, endString); |
| 178 | + DrawableTool.zoomDrwable(gifDrawable, true); |
| 179 | + gifDrawable.setLoopCount(100); |
| 180 | + GifImageSpan gifImageSpan = new GifImageSpan(gifDrawable); |
| 181 | + s.removeSpan(imageSpan); |
| 182 | + s.setSpan(gifImageSpan, start, end, 0); |
| 183 | + } catch (Exception e) { |
| 184 | + Global.errorLog(e); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return s; |
| 191 | + } |
| 192 | +} |
0 commit comments