package com.wenting.License; import android.app.Activity; import android.app.Application; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.text.Html; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextPaint; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.text.style.URLSpan; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView; import android.widget.RelativeLayout; import android.widget.TextView; import org.json.JSONObject; import java.io.BufferedReader; import java.io.InputStreamReader; import java.lang.reflect.Method; import java.util.HashMap; public class LicenseActivity extends Activity { private static final String WentingLicensePreference = "WentingLicense"; private static final String LicenseAccpetKey = "LicenseAccpet"; private static final String WentingLicenseConfigPath = "wt/license"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getLicenseAcceptState()) { enterGame(); return; } showLicense(); } public void showLicense() { Dialog dialog = new Dialog(this, Util.getIdByName(getApplication(), "style", "wenting_license_dlg")); dialog.setCancelable(false); dialog.setContentView(Util.getIdByName(getApplication(), "layout", "wenting_license")); RelativeLayout dlg = (RelativeLayout)dialog.findViewById(Util.getIdByName(getApplication(), "id", "dlg")); TextView textTitle = (TextView)dialog.findViewById(Util.getIdByName(getApplication(), "id", "title")); TextView textDesc = (TextView)dialog.findViewById(Util.getIdByName(getApplication(), "id", "desc")); TextView textView1 = (TextView)dialog.findViewById(Util.getIdByName(getApplication(), "id", "no")); TextView textView2 = (TextView)dialog.findViewById(Util.getIdByName(getApplication(), "id", "yes")); String title = ""; String desc = ""; HashMap configMap = ReadConfig(); if (configMap != null) { title = configMap.get("title"); desc = configMap.get("desc"); } ViewGroup.LayoutParams params = dlg.getLayoutParams(); dlg.setLayoutParams(params); if (!title.isEmpty()) textTitle.setText(title); if (!desc.isEmpty()) { Spanned spanned = Html.fromHtml(desc); URLSpan[] urls = spanned.getSpans(0, spanned.length(), URLSpan.class); SpannableStringBuilder style = new SpannableStringBuilder(spanned); style.clearSpans();//should clear old spans for(URLSpan url : urls){ MyClickSpan myURLSpan = new MyClickSpan(this, null, null, url.getURL()); //设置样式其中参数what是具体样式的实现对象,start则是该样式开始的位置,end对应的是样式结束的位置, // 参数 flags,定义在Spannable中的常量 style.setSpan(myURLSpan, spanned.getSpanStart(url), spanned.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } textDesc.setText(style); } textDesc.setMovementMethod(LinkMovementMethod.getInstance()); textView1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View param1View) { dialog.dismiss(); exitGame(); } }); textView2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View param1View) { setLicenseAcceptState(true); enterGame(); dialog.dismiss(); } }); dialog.show(); } private void exitGame() { Activity activity = (Activity)this; activity.runOnUiThread(new Runnable() { @Override public void run() { activity.finish(); } }); } private void enterGame() { try { Class lebianSdk = Class.forName("com.excelliance.lbsdk.LebianSdk"); Method method = lebianSdk.getMethod("setPrivacyChecked", Application.class, Context.class); method.invoke(null, new Object[] { getApplication(), getApplicationContext() }); } catch (Exception e) { e.printStackTrace(); } Class mainActivity = null; try { ActivityInfo ai = getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_META_DATA); if (ai != null) { if (ai.metaData.containsKey("MainActivity")) { mainActivity = Class.forName(ai.metaData.getString("MainActivity")); } } } catch (Exception e) { e.printStackTrace(); return; } Intent intent = new Intent(LicenseActivity.this, mainActivity); startActivity(intent); finish(); } private int dip2pix(Context context, float dpValue) { float scale = getResources().getDisplayMetrics().density; return Math.round(dpValue * scale); } private boolean getLicenseAcceptState() { SharedPreferences sharedPreferences = getSharedPreferences(WentingLicensePreference, 0); if (sharedPreferences != null) { return sharedPreferences.getBoolean(LicenseAccpetKey, false); } return false; } private void setLicenseAcceptState(boolean accepted) { SharedPreferences sharedPreferences = getSharedPreferences(WentingLicensePreference, 0); if (sharedPreferences != null) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean(LicenseAccpetKey, accepted); editor.commit(); } } private HashMap ReadConfig() { try { HashMap configMap = new HashMap<>(); InputStreamReader isr = new InputStreamReader(getAssets().open(WentingLicenseConfigPath), "UTF-8"); BufferedReader br = new BufferedReader(isr); String line; StringBuilder builder = new StringBuilder(); while((line = br.readLine()) != null){ builder.append(line); } br.close(); isr.close(); JSONObject jsonObject = new JSONObject(builder.toString()); configMap.put("title", jsonObject.getString("title")); configMap.put("desc", jsonObject.getString("desc")); return configMap; } catch (Exception e) { e.printStackTrace(); } return null; } private class MyClickSpan extends ClickableSpan { private Activity activity; private View web; private WebView webView; private String url; public MyClickSpan(Activity activity, View web, WebView webView, String url) { this.activity = activity; this.web = web; this.webView = webView; this.url = url; } @Override public void updateDrawState(TextPaint ds) { ds.setUnderlineText(false); super.updateDrawState(ds); } @Override public void onClick(View widget) { Intent intent = new Intent(LicenseActivity.this, LicenseWebViewActivity.class); intent.putExtra("url", url); startActivity(intent); } } }