| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- package com.uas.xuslectronic.application;
- import android.app.Application;
- import android.content.Context;
- import android.media.AudioAttributes;
- import android.media.SoundPool;
- import android.os.Build;
- import com.android.volley.RequestQueue;
- import com.android.volley.toolbox.Volley;
- import com.facebook.stetho.Stetho;
- import com.uas.xuslectronic.R;
- import com.uas.xuslectronic.util.AndroidUtil;
- import com.uas.xuslectronic.util.FakeX509TrustManager;
- import com.uas.xuslectronic.util.MyLog;
- import com.uas.xuslectronic.util.SoundUtil;
- import com.umeng.analytics.MobclickAgent;
- import com.umeng.commonsdk.UMConfigure;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.security.KeyManagementException;
- import java.security.KeyStore;
- import java.security.KeyStoreException;
- import java.security.NoSuchAlgorithmException;
- import java.security.cert.CertificateException;
- import java.security.cert.CertificateFactory;
- import java.util.HashMap;
- import java.util.Map;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLSocketFactory;
- import javax.net.ssl.TrustManagerFactory;
- /**
- * Created by RaoMeng on 2016/7/28.
- */
- public class PdaApplication extends Application {
- private static Map<String, Object> dataCacheMap;// 数据缓存的集合
- public static RequestQueue mRequestQueue;
- private static Context mContext;
- public static AudioAttributes mAudioAttributes;
- public static SoundPool mSoundPool;
- public static HashMap<Integer, Integer> mSoundMap;
- public static int mVersionCode = 1;
- @Override
- public void onCreate() {
- super.onCreate();
- mContext = getApplicationContext();
- FakeX509TrustManager.allowAllSSL();//去掉SSL证书验证
- if (mRequestQueue == null)
- mRequestQueue = Volley.newRequestQueue(this);
- //Stetho调试工具初始化
- Stetho.initializeWithDefaults(this);
- mVersionCode = AndroidUtil.getVersionCode(this);
- if (Build.VERSION.SDK_INT >= 21) {
- mAudioAttributes = new AudioAttributes.Builder()
- .setUsage(AudioAttributes.USAGE_NOTIFICATION)
- .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
- .build();
- mSoundPool = new SoundPool.Builder()
- .setAudioAttributes(mAudioAttributes)
- .setMaxStreams(4)
- .build();
- } else {
- mSoundPool = new SoundPool(4, AudioAttributes.CONTENT_TYPE_MUSIC, 0);
- }
- mSoundMap = new HashMap<>();
- mSoundMap.put(SoundUtil.SOUND_ARIEL, mSoundPool.load(this, R.raw.ariel, 1));
- mSoundMap.put(SoundUtil.SOUND_CARME, mSoundPool.load(this, R.raw.carme, 1));
- mSoundMap.put(SoundUtil.SOUND_CERES, mSoundPool.load(this, R.raw.ceres, 1));
- mSoundMap.put(SoundUtil.SOUND_ELARA, mSoundPool.load(this, R.raw.elara, 1));
- mSoundMap.put(SoundUtil.SOUND_EUROPA, mSoundPool.load(this, R.raw.europa, 1));
- mSoundMap.put(SoundUtil.SOUND_IAPETUS, mSoundPool.load(this, R.raw.iapetus, 1));
- mSoundMap.put(SoundUtil.SOUND_IO, mSoundPool.load(this, R.raw.io, 1));
- mSoundMap.put(SoundUtil.SOUND_RHEA, mSoundPool.load(this, R.raw.rhea, 1));
- mSoundMap.put(SoundUtil.SOUND_SALACIA, mSoundPool.load(this, R.raw.salacia, 1));
- mSoundMap.put(SoundUtil.SOUND_TETHYS, mSoundPool.load(this, R.raw.tethys, 1));
- mSoundMap.put(SoundUtil.SOUND_TITAN, mSoundPool.load(this, R.raw.titan, 1));
- //友盟统计SDK
- initUmeng();
- //删除非当天的Log日志, Android8.0以下用不了
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- MyLog.delNotDayFile();
- }else {
- MyLog.isFileCreatedTodayDel();
- }
- }
- private void initUmeng() {
- //2.在Application.oncreate()中调用预初始化函数
- UMConfigure.preInit(this, "6170b8b61c91e0671bb0da5a","umeng");
- UMConfigure.setLogEnabled(true);
- //3.客户端用户同意隐私政策后,正式初始化友盟+SDK
- UMConfigure.init(
- this,
- "6170b8b61c91e0671bb0da5a",
- "umeng",
- UMConfigure.DEVICE_TYPE_PHONE, ""
- );
- // 页面数据采集模式
- // setPageCollectionMode接口参数说明:
- // 1. MobclickAgent.PageMode.AUTO: 建议大多数用户使用本采集模式,SDK在此模式下自动采集Activity
- // 页面访问路径,开发者不需要针对每一个Activity在onResume/onPause函数中进行手动埋点。在此模式下,
- // 开发者如需针对Fragment、CustomView等自定义页面进行页面统计,直接调用MobclickAgent.onPageStart/
- // MobclickAgent.onPageEnd手动埋点即可。此采集模式简化埋点工作,唯一缺点是在Android 4.0以下设备中
- // 统计不到Activity页面数据和各类基础指标(提示:目前Android 4.0以下设备市场占比已经极小)。
- MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
- }
- @Override
- public void onTerminate() {
- super.onTerminate();
- SoundUtil.close();
- }
- public static Context getmContext() {
- return mContext;
- }
- /**
- * 获取本地缓存
- *
- * @param key
- * @return
- */
- public static Object getDataCacheFromMap(String key) {
- if (dataCacheMap == null) {
- return null;
- }
- return dataCacheMap.get(key);
- }
- /**
- * 移除本地缓存
- *
- * @param key
- */
- public static void removeDataCacheFromMap(String key) {
- if (dataCacheMap != null) {
- dataCacheMap.remove(key);
- }
- }
- /**
- * 清除本地所有缓存
- */
- public static void removeAllDataCache() {
- if (dataCacheMap != null) {
- dataCacheMap.clear();
- }
- }
- /**
- * 存储本地缓存
- *
- * @param key
- * @param value
- */
- public static void putDataCache2Map(String key, Object value) {
- if (dataCacheMap == null) {
- dataCacheMap = new HashMap<String, Object>();
- }
- dataCacheMap.put(key, value);
- }
- /**
- * 生成SSLSocketFactory
- *
- * @return
- */
- private SSLSocketFactory initSSLSocketFactory() {
- //生成证书:Certificate
- CertificateFactory cf = null;
- SSLSocketFactory factory = null;
- try {
- cf = CertificateFactory.getInstance("X.509");
- // InputStream caInput = new ByteArrayInputStream(load.getBytes());
- InputStream caInput = new ByteArrayInputStream("".getBytes());
- java.security.cert.Certificate ca = null;
- try {
- ca = cf.generateCertificate(caInput);
- } finally {
- try {
- caInput.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //初始化公钥:keyStore
- String keyType = KeyStore.getDefaultType();
- KeyStore keyStore = KeyStore.getInstance(keyType);
- keyStore.load(null, null);
- keyStore.setCertificateEntry("ca", ca);
- //初始化TrustManagerFactory
- String algorithm = TrustManagerFactory.getDefaultAlgorithm();
- TrustManagerFactory managerFactory = TrustManagerFactory.getInstance(algorithm);
- managerFactory.init(keyStore);
- //初始化sslContext
- SSLContext sslContext = SSLContext.getInstance("TLS");
- sslContext.init(null, managerFactory.getTrustManagers(), null);
- factory = sslContext.getSocketFactory();
- } catch (CertificateException e) {
- e.printStackTrace();
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (KeyStoreException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (KeyManagementException e) {
- e.printStackTrace();
- }
- return factory;
- }
- }
|