PdaApplication.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.uas.xuslectronic.application;
  2. import android.app.Application;
  3. import android.content.Context;
  4. import android.media.AudioAttributes;
  5. import android.media.SoundPool;
  6. import android.os.Build;
  7. import com.android.volley.RequestQueue;
  8. import com.android.volley.toolbox.Volley;
  9. import com.facebook.stetho.Stetho;
  10. import com.uas.xuslectronic.R;
  11. import com.uas.xuslectronic.util.AndroidUtil;
  12. import com.uas.xuslectronic.util.FakeX509TrustManager;
  13. import com.uas.xuslectronic.util.MyLog;
  14. import com.uas.xuslectronic.util.SoundUtil;
  15. import com.umeng.analytics.MobclickAgent;
  16. import com.umeng.commonsdk.UMConfigure;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.security.KeyManagementException;
  21. import java.security.KeyStore;
  22. import java.security.KeyStoreException;
  23. import java.security.NoSuchAlgorithmException;
  24. import java.security.cert.CertificateException;
  25. import java.security.cert.CertificateFactory;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import javax.net.ssl.SSLContext;
  29. import javax.net.ssl.SSLSocketFactory;
  30. import javax.net.ssl.TrustManagerFactory;
  31. /**
  32. * Created by RaoMeng on 2016/7/28.
  33. */
  34. public class PdaApplication extends Application {
  35. private static Map<String, Object> dataCacheMap;// 数据缓存的集合
  36. public static RequestQueue mRequestQueue;
  37. private static Context mContext;
  38. public static AudioAttributes mAudioAttributes;
  39. public static SoundPool mSoundPool;
  40. public static HashMap<Integer, Integer> mSoundMap;
  41. public static int mVersionCode = 1;
  42. @Override
  43. public void onCreate() {
  44. super.onCreate();
  45. mContext = getApplicationContext();
  46. FakeX509TrustManager.allowAllSSL();//去掉SSL证书验证
  47. if (mRequestQueue == null)
  48. mRequestQueue = Volley.newRequestQueue(this);
  49. //Stetho调试工具初始化
  50. Stetho.initializeWithDefaults(this);
  51. mVersionCode = AndroidUtil.getVersionCode(this);
  52. if (Build.VERSION.SDK_INT >= 21) {
  53. mAudioAttributes = new AudioAttributes.Builder()
  54. .setUsage(AudioAttributes.USAGE_NOTIFICATION)
  55. .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
  56. .build();
  57. mSoundPool = new SoundPool.Builder()
  58. .setAudioAttributes(mAudioAttributes)
  59. .setMaxStreams(4)
  60. .build();
  61. } else {
  62. mSoundPool = new SoundPool(4, AudioAttributes.CONTENT_TYPE_MUSIC, 0);
  63. }
  64. mSoundMap = new HashMap<>();
  65. mSoundMap.put(SoundUtil.SOUND_ARIEL, mSoundPool.load(this, R.raw.ariel, 1));
  66. mSoundMap.put(SoundUtil.SOUND_CARME, mSoundPool.load(this, R.raw.carme, 1));
  67. mSoundMap.put(SoundUtil.SOUND_CERES, mSoundPool.load(this, R.raw.ceres, 1));
  68. mSoundMap.put(SoundUtil.SOUND_ELARA, mSoundPool.load(this, R.raw.elara, 1));
  69. mSoundMap.put(SoundUtil.SOUND_EUROPA, mSoundPool.load(this, R.raw.europa, 1));
  70. mSoundMap.put(SoundUtil.SOUND_IAPETUS, mSoundPool.load(this, R.raw.iapetus, 1));
  71. mSoundMap.put(SoundUtil.SOUND_IO, mSoundPool.load(this, R.raw.io, 1));
  72. mSoundMap.put(SoundUtil.SOUND_RHEA, mSoundPool.load(this, R.raw.rhea, 1));
  73. mSoundMap.put(SoundUtil.SOUND_SALACIA, mSoundPool.load(this, R.raw.salacia, 1));
  74. mSoundMap.put(SoundUtil.SOUND_TETHYS, mSoundPool.load(this, R.raw.tethys, 1));
  75. mSoundMap.put(SoundUtil.SOUND_TITAN, mSoundPool.load(this, R.raw.titan, 1));
  76. //友盟统计SDK
  77. initUmeng();
  78. //删除非当天的Log日志, Android8.0以下用不了
  79. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  80. MyLog.delNotDayFile();
  81. }else {
  82. MyLog.isFileCreatedTodayDel();
  83. }
  84. }
  85. private void initUmeng() {
  86. //2.在Application.oncreate()中调用预初始化函数
  87. UMConfigure.preInit(this, "6170b8b61c91e0671bb0da5a","umeng");
  88. UMConfigure.setLogEnabled(true);
  89. //3.客户端用户同意隐私政策后,正式初始化友盟+SDK
  90. UMConfigure.init(
  91. this,
  92. "6170b8b61c91e0671bb0da5a",
  93. "umeng",
  94. UMConfigure.DEVICE_TYPE_PHONE, ""
  95. );
  96. // 页面数据采集模式
  97. // setPageCollectionMode接口参数说明:
  98. // 1. MobclickAgent.PageMode.AUTO: 建议大多数用户使用本采集模式,SDK在此模式下自动采集Activity
  99. // 页面访问路径,开发者不需要针对每一个Activity在onResume/onPause函数中进行手动埋点。在此模式下,
  100. // 开发者如需针对Fragment、CustomView等自定义页面进行页面统计,直接调用MobclickAgent.onPageStart/
  101. // MobclickAgent.onPageEnd手动埋点即可。此采集模式简化埋点工作,唯一缺点是在Android 4.0以下设备中
  102. // 统计不到Activity页面数据和各类基础指标(提示:目前Android 4.0以下设备市场占比已经极小)。
  103. MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
  104. }
  105. @Override
  106. public void onTerminate() {
  107. super.onTerminate();
  108. SoundUtil.close();
  109. }
  110. public static Context getmContext() {
  111. return mContext;
  112. }
  113. /**
  114. * 获取本地缓存
  115. *
  116. * @param key
  117. * @return
  118. */
  119. public static Object getDataCacheFromMap(String key) {
  120. if (dataCacheMap == null) {
  121. return null;
  122. }
  123. return dataCacheMap.get(key);
  124. }
  125. /**
  126. * 移除本地缓存
  127. *
  128. * @param key
  129. */
  130. public static void removeDataCacheFromMap(String key) {
  131. if (dataCacheMap != null) {
  132. dataCacheMap.remove(key);
  133. }
  134. }
  135. /**
  136. * 清除本地所有缓存
  137. */
  138. public static void removeAllDataCache() {
  139. if (dataCacheMap != null) {
  140. dataCacheMap.clear();
  141. }
  142. }
  143. /**
  144. * 存储本地缓存
  145. *
  146. * @param key
  147. * @param value
  148. */
  149. public static void putDataCache2Map(String key, Object value) {
  150. if (dataCacheMap == null) {
  151. dataCacheMap = new HashMap<String, Object>();
  152. }
  153. dataCacheMap.put(key, value);
  154. }
  155. /**
  156. * 生成SSLSocketFactory
  157. *
  158. * @return
  159. */
  160. private SSLSocketFactory initSSLSocketFactory() {
  161. //生成证书:Certificate
  162. CertificateFactory cf = null;
  163. SSLSocketFactory factory = null;
  164. try {
  165. cf = CertificateFactory.getInstance("X.509");
  166. // InputStream caInput = new ByteArrayInputStream(load.getBytes());
  167. InputStream caInput = new ByteArrayInputStream("".getBytes());
  168. java.security.cert.Certificate ca = null;
  169. try {
  170. ca = cf.generateCertificate(caInput);
  171. } finally {
  172. try {
  173. caInput.close();
  174. } catch (IOException e) {
  175. e.printStackTrace();
  176. }
  177. }
  178. //初始化公钥:keyStore
  179. String keyType = KeyStore.getDefaultType();
  180. KeyStore keyStore = KeyStore.getInstance(keyType);
  181. keyStore.load(null, null);
  182. keyStore.setCertificateEntry("ca", ca);
  183. //初始化TrustManagerFactory
  184. String algorithm = TrustManagerFactory.getDefaultAlgorithm();
  185. TrustManagerFactory managerFactory = TrustManagerFactory.getInstance(algorithm);
  186. managerFactory.init(keyStore);
  187. //初始化sslContext
  188. SSLContext sslContext = SSLContext.getInstance("TLS");
  189. sslContext.init(null, managerFactory.getTrustManagers(), null);
  190. factory = sslContext.getSocketFactory();
  191. } catch (CertificateException e) {
  192. e.printStackTrace();
  193. } catch (NoSuchAlgorithmException e) {
  194. e.printStackTrace();
  195. } catch (KeyStoreException e) {
  196. e.printStackTrace();
  197. } catch (IOException e) {
  198. e.printStackTrace();
  199. } catch (KeyManagementException e) {
  200. e.printStackTrace();
  201. }
  202. return factory;
  203. }
  204. }