LicenseActivity.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package com.wenting.License;
  2. import android.app.Activity;
  3. import android.app.Application;
  4. import android.app.Dialog;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.content.pm.ActivityInfo;
  9. import android.content.pm.PackageManager;
  10. import android.os.Bundle;
  11. import android.text.Html;
  12. import android.text.Spannable;
  13. import android.text.SpannableStringBuilder;
  14. import android.text.Spanned;
  15. import android.text.TextPaint;
  16. import android.text.method.LinkMovementMethod;
  17. import android.text.style.ClickableSpan;
  18. import android.text.style.URLSpan;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.webkit.WebView;
  22. import android.widget.RelativeLayout;
  23. import android.widget.TextView;
  24. import org.json.JSONObject;
  25. import java.io.BufferedReader;
  26. import java.io.InputStreamReader;
  27. import java.lang.reflect.Method;
  28. import java.util.HashMap;
  29. public class LicenseActivity extends Activity {
  30. private static final String WentingLicensePreference = "WentingLicense";
  31. private static final String LicenseAccpetKey = "LicenseAccpet";
  32. private static final String WentingLicenseConfigPath = "wt/license";
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. if (getLicenseAcceptState()) {
  37. enterGame();
  38. return;
  39. }
  40. showLicense();
  41. }
  42. public void showLicense() {
  43. Dialog dialog = new Dialog(this, Util.getIdByName(getApplication(), "style", "wenting_license_dlg"));
  44. dialog.setCancelable(false);
  45. dialog.setContentView(Util.getIdByName(getApplication(), "layout", "wenting_license"));
  46. RelativeLayout dlg = (RelativeLayout)dialog.findViewById(Util.getIdByName(getApplication(), "id", "dlg"));
  47. TextView textTitle = (TextView)dialog.findViewById(Util.getIdByName(getApplication(), "id", "title"));
  48. TextView textDesc = (TextView)dialog.findViewById(Util.getIdByName(getApplication(), "id", "desc"));
  49. TextView textView1 = (TextView)dialog.findViewById(Util.getIdByName(getApplication(), "id", "no"));
  50. TextView textView2 = (TextView)dialog.findViewById(Util.getIdByName(getApplication(), "id", "yes"));
  51. String title = "";
  52. String desc = "";
  53. HashMap<String, String> configMap = ReadConfig();
  54. if (configMap != null) {
  55. title = configMap.get("title");
  56. desc = configMap.get("desc");
  57. }
  58. ViewGroup.LayoutParams params = dlg.getLayoutParams();
  59. dlg.setLayoutParams(params);
  60. if (!title.isEmpty())
  61. textTitle.setText(title);
  62. if (!desc.isEmpty()) {
  63. Spanned spanned = Html.fromHtml(desc);
  64. URLSpan[] urls = spanned.getSpans(0, spanned.length(), URLSpan.class);
  65. SpannableStringBuilder style = new SpannableStringBuilder(spanned);
  66. style.clearSpans();//should clear old spans
  67. for(URLSpan url : urls){
  68. MyClickSpan myURLSpan = new MyClickSpan(this, null, null, url.getURL());
  69. //设置样式其中参数what是具体样式的实现对象,start则是该样式开始的位置,end对应的是样式结束的位置,
  70. // 参数 flags,定义在Spannable中的常量
  71. style.setSpan(myURLSpan, spanned.getSpanStart(url), spanned.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  72. }
  73. textDesc.setText(style);
  74. }
  75. textDesc.setMovementMethod(LinkMovementMethod.getInstance());
  76. textView1.setOnClickListener(new View.OnClickListener() {
  77. @Override
  78. public void onClick(View param1View) {
  79. dialog.dismiss();
  80. exitGame();
  81. }
  82. });
  83. textView2.setOnClickListener(new View.OnClickListener() {
  84. @Override
  85. public void onClick(View param1View) {
  86. setLicenseAcceptState(true);
  87. enterGame();
  88. dialog.dismiss();
  89. }
  90. });
  91. dialog.show();
  92. }
  93. private void exitGame() {
  94. Activity activity = (Activity)this;
  95. activity.runOnUiThread(new Runnable() {
  96. @Override
  97. public void run() {
  98. activity.finish();
  99. }
  100. });
  101. }
  102. private void enterGame() {
  103. try {
  104. Class<?> lebianSdk = Class.forName("com.excelliance.lbsdk.LebianSdk");
  105. Method method = lebianSdk.getMethod("setPrivacyChecked", Application.class, Context.class);
  106. method.invoke(null, new Object[] { getApplication(), getApplicationContext() });
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. Class<?> mainActivity = null;
  111. try {
  112. ActivityInfo ai = getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_META_DATA);
  113. if (ai != null) {
  114. if (ai.metaData.containsKey("MainActivity")) {
  115. mainActivity = Class.forName(ai.metaData.getString("MainActivity"));
  116. }
  117. }
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. return;
  121. }
  122. Intent intent = new Intent(LicenseActivity.this, mainActivity);
  123. startActivity(intent);
  124. finish();
  125. }
  126. private int dip2pix(Context context, float dpValue) {
  127. float scale = getResources().getDisplayMetrics().density;
  128. return Math.round(dpValue * scale);
  129. }
  130. private boolean getLicenseAcceptState() {
  131. SharedPreferences sharedPreferences = getSharedPreferences(WentingLicensePreference, 0);
  132. if (sharedPreferences != null) {
  133. return sharedPreferences.getBoolean(LicenseAccpetKey, false);
  134. }
  135. return false;
  136. }
  137. private void setLicenseAcceptState(boolean accepted) {
  138. SharedPreferences sharedPreferences = getSharedPreferences(WentingLicensePreference, 0);
  139. if (sharedPreferences != null) {
  140. SharedPreferences.Editor editor = sharedPreferences.edit();
  141. editor.putBoolean(LicenseAccpetKey, accepted);
  142. editor.commit();
  143. }
  144. }
  145. private HashMap<String, String> ReadConfig() {
  146. try {
  147. HashMap<String, String> configMap = new HashMap<>();
  148. InputStreamReader isr = new InputStreamReader(getAssets().open(WentingLicenseConfigPath), "UTF-8");
  149. BufferedReader br = new BufferedReader(isr);
  150. String line;
  151. StringBuilder builder = new StringBuilder();
  152. while((line = br.readLine()) != null){
  153. builder.append(line);
  154. }
  155. br.close();
  156. isr.close();
  157. JSONObject jsonObject = new JSONObject(builder.toString());
  158. configMap.put("title", jsonObject.getString("title"));
  159. configMap.put("desc", jsonObject.getString("desc"));
  160. return configMap;
  161. } catch (Exception e) {
  162. e.printStackTrace();
  163. }
  164. return null;
  165. }
  166. private class MyClickSpan extends ClickableSpan {
  167. private Activity activity;
  168. private View web;
  169. private WebView webView;
  170. private String url;
  171. public MyClickSpan(Activity activity, View web, WebView webView, String url) {
  172. this.activity = activity;
  173. this.web = web;
  174. this.webView = webView;
  175. this.url = url;
  176. }
  177. @Override
  178. public void updateDrawState(TextPaint ds) {
  179. ds.setUnderlineText(false);
  180. super.updateDrawState(ds);
  181. }
  182. @Override
  183. public void onClick(View widget) {
  184. Intent intent = new Intent(LicenseActivity.this, LicenseWebViewActivity.class);
  185. intent.putExtra("url", url);
  186. startActivity(intent);
  187. }
  188. }
  189. }