MyApplication.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package com.xzjmyk.pm.activity;
  2. import android.app.Application;
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.Bitmap.Config;
  6. import android.os.Build;
  7. import android.os.Environment;
  8. import android.os.Handler;
  9. import android.os.StrictMode;
  10. import android.support.multidex.MultiDex;
  11. import android.util.Log;
  12. import com.android.volley.RequestQueue;
  13. import com.android.volley.toolbox.Volley;
  14. import com.baidu.mapapi.SDKInitializer;
  15. import com.facebook.stetho.Stetho;
  16. import com.lidroid.xutils.DbUtils;
  17. import com.lidroid.xutils.HttpUtils;
  18. import com.nostra13.universalimageloader.cache.disc.impl.TotalSizeLimitedDiscCache;
  19. import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
  20. import com.nostra13.universalimageloader.cache.memory.MemoryCacheAware;
  21. import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
  22. import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
  23. import com.nostra13.universalimageloader.core.DisplayImageOptions;
  24. import com.nostra13.universalimageloader.core.ImageLoader;
  25. import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
  26. import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
  27. import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
  28. import com.umeng.analytics.MobclickAgent;
  29. import com.umeng.socialize.PlatformConfig;
  30. import com.uuzuche.lib_zxing.activity.ZXingLibrary;
  31. import com.xzjmyk.pm.activity.bean.ConfigBean;
  32. import com.xzjmyk.pm.activity.bean.User;
  33. import com.xzjmyk.pm.activity.db.SQLiteHelper;
  34. import com.xzjmyk.pm.activity.util.Constants;
  35. import com.xzjmyk.pm.activity.util.PreferenceUtils;
  36. import com.xzjmyk.pm.activity.util.SharedUtil;
  37. import com.xzjmyk.pm.activity.volley.FastVolley;
  38. import org.apache.http.cookie.Cookie;
  39. import java.io.File;
  40. import java.util.HashMap;
  41. import java.util.Map;
  42. public class MyApplication extends Application {
  43. /**
  44. * @注释:记录Cookie
  45. */
  46. public String JSESSION_B2B;
  47. public static Cookie cookie;
  48. public static Cookie cookieERP;
  49. public static RequestQueue mRequestQueue;
  50. public static HttpUtils mHttpUtils;
  51. public static DbUtils mDbUtils;
  52. private static MyApplication INSTANCE = null;
  53. public static MyApplication getInstance() {
  54. return INSTANCE;
  55. }
  56. @Override
  57. public void onCreate() {
  58. super.onCreate();
  59. //Stetho调试工具初始化
  60. Stetho.initializeWithDefaults(this);
  61. INSTANCE = this;
  62. ZXingLibrary.initDisplayOpinion(this);
  63. mRequestQueue = Volley.newRequestQueue(this);
  64. mHttpUtils = new HttpUtils();
  65. configHttpUtils();
  66. //initDbUtils();
  67. SharedUtil.init(this);
  68. initShareAPI();
  69. // CrashReport.initCrashReport(getApplicationContext(), "900050585", false);
  70. PreferenceUtils.putBoolean(this, Constants.IS_NOTIFICATION, false);//不进行通知
  71. new Handler().postDelayed(new Runnable() {
  72. @Override
  73. public void run() {
  74. //定位场景设置
  75. MobclickAgent.setScenarioType(MyApplication.this, MobclickAgent.EScenarioType.E_UM_NORMAL);
  76. SDKInitializer.initialize(getApplicationContext());
  77. // 初始化数据库
  78. SQLiteHelper.copyDatabaseFile(getApplicationContext());
  79. // 初始化定位
  80. getBdLocationHelper();
  81. // 初始化App目录
  82. initAppDir();
  83. // 初始化图片加载
  84. initImageLoader();
  85. }
  86. }, 1);
  87. if (AppConfig.DEBUG) {
  88. Log.d(AppConfig.TAG, "MyApplication onCreate");
  89. }
  90. if (AppConfig.DEBUG) {
  91. StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
  92. StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
  93. }
  94. // 初始化网络监听
  95. mNetWorkObservable = new NetWorkObservable(this);
  96. }
  97. /**
  98. * @desc:初始化分享组件
  99. * @author:Arison on 2016/9/29
  100. */
  101. public void initShareAPI(){
  102. PlatformConfig.setWeixin("wx2539cc96bf158e00", "b057aa3e807ba7c505a2f03cc541bbf6");
  103. PlatformConfig.setSinaWeibo("493318722", "7def652c3683603b08bab87d34b1f944");
  104. PlatformConfig.setQQZone("1104894295", "Nk8zMIz5YSqwRQ1F");
  105. }
  106. private final String mDbDir = "/sdcard/uu/database";//数据库存放路径
  107. private final String mDbName = "uuhulian.db";//数据库名称
  108. /**
  109. * 配置DbUtils
  110. */
  111. private void initDbUtils() {
  112. DbUtils.DaoConfig daoConfig = new DbUtils.DaoConfig(this);
  113. daoConfig.setDbDir(mDbDir);
  114. daoConfig.setDbName(mDbName);
  115. mDbUtils = DbUtils.create(daoConfig);
  116. }
  117. /**
  118. * 为HttpUtils配置参数
  119. */
  120. private void configHttpUtils() {
  121. mHttpUtils.configRequestThreadPoolSize(4);
  122. mHttpUtils.configRequestRetryCount(3);
  123. mHttpUtils.configResponseTextCharset("utf-8");
  124. mHttpUtils.configTimeout(10000);
  125. }
  126. /**
  127. * 在程序内部关闭时,调用此方法
  128. */
  129. public void destory() {
  130. if (AppConfig.DEBUG) {
  131. Log.d(AppConfig.TAG, "MyApplication destory");
  132. }
  133. // 结束百度定位
  134. if (mBdLocationHelper != null) {
  135. mBdLocationHelper.release();
  136. }
  137. // 关闭网络状态的监听
  138. if (mNetWorkObservable != null) {
  139. mNetWorkObservable.release();
  140. }
  141. // 清除图片加载
  142. ImageLoader.getInstance().destroy();
  143. //
  144. releaseFastVolley();
  145. // 释放数据库
  146. // SQLiteHelper.release();
  147. android.os.Process.killProcess(android.os.Process.myPid());
  148. }
  149. /*********************
  150. * 百度地图定位服务
  151. ************************/
  152. private BdLocationHelper mBdLocationHelper;
  153. public BdLocationHelper getBdLocationHelper() {
  154. if (mBdLocationHelper == null) {
  155. mBdLocationHelper = new BdLocationHelper(this);
  156. }
  157. return mBdLocationHelper;
  158. }
  159. /*********************
  160. * 提供网络全局监听
  161. ************************/
  162. private NetWorkObservable mNetWorkObservable;
  163. public boolean isNetworkActive() {
  164. if (mNetWorkObservable != null) {
  165. return mNetWorkObservable.isNetworkActive();
  166. }
  167. return true;
  168. }
  169. public void registerNetWorkObserver(NetWorkObservable.NetWorkObserver observer) {
  170. if (mNetWorkObservable != null) {
  171. mNetWorkObservable.registerObserver(observer);
  172. }
  173. }
  174. public void unregisterNetWorkObserver(NetWorkObservable.NetWorkObserver observer) {
  175. if (mNetWorkObservable != null) {
  176. mNetWorkObservable.unregisterObserver(observer);
  177. }
  178. }
  179. /* 文件缓存的目录 */
  180. public String mAppDir;
  181. public String mPicturesDir;
  182. public String mVoicesDir;
  183. public String mVideosDir;
  184. public String mFilesDir;
  185. private void initAppDir() {
  186. File file = getExternalFilesDir(null);
  187. if (file != null) {
  188. if (!file.exists()) {
  189. file.mkdirs();
  190. }
  191. mAppDir = file.getAbsolutePath();
  192. }
  193. file = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  194. if (file != null) {
  195. if (!file.exists()) {
  196. file.mkdirs();
  197. }
  198. mPicturesDir = file.getAbsolutePath();
  199. }
  200. file = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
  201. if (file != null) {
  202. if (!file.exists()) {
  203. file.mkdirs();
  204. }
  205. mVoicesDir = file.getAbsolutePath();
  206. }
  207. file = getExternalFilesDir(Environment.DIRECTORY_MOVIES);
  208. if (file != null) {
  209. if (!file.exists()) {
  210. file.mkdirs();
  211. }
  212. mVideosDir = file.getAbsolutePath();
  213. }
  214. file = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
  215. if (file != null) {
  216. if (!file.exists()) {
  217. file.mkdirs();
  218. }
  219. mFilesDir = file.getAbsolutePath();
  220. }
  221. }
  222. /*******************
  223. * 初始化图片加载
  224. **********************/
  225. // 显示的设置
  226. public static DisplayImageOptions mNormalImageOptions;
  227. public static DisplayImageOptions mAvatarRoundImageOptions;
  228. public static DisplayImageOptions mAvatarNormalImageOptions;
  229. private void initImageLoader() {
  230. int memoryCacheSize = (int) (Runtime.getRuntime().maxMemory() / 5);
  231. MemoryCacheAware<String, Bitmap> memoryCache;
  232. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
  233. memoryCache = new LruMemoryCache(memoryCacheSize);
  234. } else {
  235. memoryCache = new LRULimitedMemoryCache(memoryCacheSize);
  236. }
  237. mNormalImageOptions = new DisplayImageOptions.Builder().bitmapConfig(Config.RGB_565).cacheInMemory(true).cacheOnDisc(true)
  238. .resetViewBeforeLoading(false).showImageForEmptyUri(com.xzjmyk.pm.activity.R.drawable.image_download_fail_icon)
  239. .showImageOnFail(com.xzjmyk.pm.activity.R.drawable.image_download_fail_icon).build();
  240. mAvatarRoundImageOptions = new DisplayImageOptions.Builder().bitmapConfig(Config.RGB_565).cacheInMemory(true).cacheOnDisc(true)
  241. .displayer(new RoundedBitmapDisplayer(10)).resetViewBeforeLoading(true).showImageForEmptyUri(com.xzjmyk.pm.activity.R.drawable.avatar_normal)
  242. .showImageOnFail(com.xzjmyk.pm.activity.R.drawable.avatar_normal).showImageOnLoading(com.xzjmyk.pm.activity.R.drawable.avatar_normal).build();
  243. mAvatarNormalImageOptions = new DisplayImageOptions.Builder().bitmapConfig(Config.RGB_565).cacheInMemory(true).cacheOnDisc(true)
  244. .resetViewBeforeLoading(true).showImageForEmptyUri(com.xzjmyk.pm.activity.R.drawable.avatar_normal).showImageOnFail(com.xzjmyk.pm.activity.R.drawable.avatar_normal)
  245. .showImageOnLoading(com.xzjmyk.pm.activity.R.drawable.avatar_normal).build();
  246. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(mNormalImageOptions)
  247. // .denyCacheImageMultipleSizesInMemory()
  248. .discCache(new TotalSizeLimitedDiscCache(new File(mPicturesDir), 50 * 1024 * 1024))
  249. // 最多缓存50M的图片
  250. .discCacheFileNameGenerator(new Md5FileNameGenerator()).memoryCache(memoryCache).tasksProcessingOrder(QueueProcessingType.LIFO)
  251. .threadPriority(Thread.NORM_PRIORITY - 2)
  252. .threadPoolSize(4)
  253. .build();
  254. // Initialize ImageLoader with configuration.
  255. ImageLoader.getInstance().init(config);
  256. }
  257. /*********************
  258. * 提供全局配置
  259. ************************/
  260. private AppConfig mConfig;
  261. public void setConfig(AppConfig config) {
  262. mConfig = config;
  263. }
  264. public AppConfig getConfig() {
  265. if (mConfig == null) {
  266. mConfig = AppConfig.initConfig(getApplicationContext(), new ConfigBean());
  267. }
  268. return mConfig;
  269. }
  270. /*****************
  271. * 提供全局的Volley
  272. ***************************/
  273. private FastVolley mFastVolley;
  274. public FastVolley getFastVolley() {
  275. if (mFastVolley == null) {
  276. synchronized (MyApplication.class) {
  277. if (mFastVolley == null) {
  278. mFastVolley = new FastVolley(this);
  279. mFastVolley.start();
  280. }
  281. }
  282. }
  283. return mFastVolley;
  284. }
  285. private void releaseFastVolley() {
  286. if (mFastVolley != null) {
  287. mFastVolley.stop();
  288. }
  289. }
  290. @Override
  291. protected void attachBaseContext(Context base) {
  292. super.attachBaseContext(base);
  293. MultiDex.install(this);
  294. }
  295. /***********************
  296. * 保存当前登陆用户的全局信息
  297. ***************/
  298. public String roomName;
  299. public String mAccessToken;
  300. public long mExpiresIn;
  301. public int mUserStatus;
  302. public boolean mUserStatusChecked = false;
  303. public User mLoginUser = new User();// 当前登陆的用户
  304. /***********************
  305. * 保存其他用户坐标信息
  306. ***************/
  307. public String getJSESSION_B2B() {
  308. return JSESSION_B2B;
  309. }
  310. public void setJSESSION_B2B(String jSESSION_B2B) {
  311. JSESSION_B2B = jSESSION_B2B;
  312. }
  313. public static Cookie getCookie() {
  314. return cookie;
  315. }
  316. public static void setCookie(Cookie cookie) {
  317. MyApplication.cookie = cookie;
  318. }
  319. private static Map<String, Object> dataCacheMap;// 数据缓存的集合
  320. /**
  321. * 获取本地缓存
  322. * @param key
  323. * @return
  324. */
  325. public static Object getDataCacheFromMap(String key) {
  326. if (dataCacheMap == null) {
  327. return null;
  328. }
  329. return dataCacheMap.get(key);
  330. }
  331. /**
  332. * 移除本地缓存
  333. * @param key
  334. */
  335. public static void removeDataCacheFromMap(String key) {
  336. if (dataCacheMap != null) {
  337. dataCacheMap.remove(key);
  338. }
  339. }
  340. /**
  341. * 存储本地缓存
  342. * @param key
  343. * @param value
  344. */
  345. public static void putDataCache2Map(String key, Object value) {
  346. if (dataCacheMap == null) {
  347. dataCacheMap = new HashMap<String, Object>();
  348. }
  349. dataCacheMap.put(key, value);
  350. }
  351. }