package com.basic.security.base; import android.app.Activity; import android.app.Application; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Build; import com.basic.security.R; import com.basic.security.activity.MainActivity; import com.basic.security.model.ModelAdapter; import com.basic.security.utils.Base64Util; import com.basic.security.utils.ElastisearchUtil; import com.scwang.smartrefresh.layout.SmartRefreshLayout; import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator; import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator; import com.scwang.smartrefresh.layout.api.RefreshFooter; import com.scwang.smartrefresh.layout.api.RefreshHeader; import com.scwang.smartrefresh.layout.api.RefreshLayout; import com.scwang.smartrefresh.layout.footer.ClassicsFooter; import com.scwang.smartrefresh.layout.header.ClassicsHeader; import org.xutils.x; import java.lang.reflect.Field; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import interfaces.heweather.com.interfacesmodule.view.HeConfig; //import com.facebook.drawee.backends.pipeline.Fresco; public class BaseApplication extends Application { static Map localSetting = new HashMap(); // 获取设置的设备基本信息 private static BaseApplication application; static { //设置全局的Header构建器 SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() { @Override public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) { layout.setPrimaryColorsId(R.color.transparent, android.R.color.white);//全局设置主题颜色 return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header,默认是 贝塞尔雷达Header } }); //设置全局的Footer构建器 SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() { @Override public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) { //指定为经典Footer,默认是 BallPulseFooter return new ClassicsFooter(context).setDrawableSize(20); } }); } public final Lock detectLock = new ReentrantLock(); public MainActivity activity; public Map devInfo = new HashMap<>(); public ExecutorService executorService = Executors.newCachedThreadPool(); private List activitys = new LinkedList<>(); private List services = new LinkedList<>(); private ModelAdapter backTime; public static BaseApplication getApplication() { return application; } public static Map getLocalSetting() { return localSetting; } public void onTerminate() { super.onTerminate(); } @Override public void onCreate() { super.onCreate(); // System.out.println("BaseApplication.onCreate"); x.Ext.init(this); Base64Util.init(); // 检测内存泄漏 // LeakCanary.install(this); // try { // File errorLogFile = new File("/sdcard/crash/crash-"+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+".log"); // if (errorLogFile.exists()){ // errorLogFile.delete(); // } // } catch (Exception e) { // e.printStackTrace(); // } ElastisearchUtil.test(); application = this; // Fresco.initialize(this); // registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { // public void onActivityCreated(Activity activity, Bundle bundle) { } // public void onActivityStarted(Activity activity) { } // public void onActivityPaused(Activity activity) { } // public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { } // public void onActivityStopped(Activity activity) { // application.activity = null; // } // public void onActivityResumed(Activity activity) { // application.activity = activity; // } // public void onActivityDestroyed(Activity activity) { // application.activity = null; // } // }); init(); HeConfig.init("HE1811151724201473", "b5fe07c5d66c43d089e60baf2fa655ac"); HeConfig.switchToFreeServerNode(); // WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE); // String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress()); // System.out.println("erlangLocalNode="+Constants.erlangLocalNode); // if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // initLocalSetting(); // } testFunction(); // Map m = CouchbaseUtil.getDocumentStringMap(list.get(i)); // Log.e("", "") // Log.e("AndroidUUID", "获取UUID:" + DeviceUuidFactory.getInstance(this).getDeviceUuid()); // CouchbaseUtil.database != null 为了解决CouchbaseUtil.database.getDocument为null // if (CouchbaseUtil.database != null && CouchbaseUtil.database.getDocument(DeviceManager.getDeviceId()) == null) { // ModelAdapter d = new ModelAdapter(DeviceManager.getDeviceId()); // d.setString("table", "device"); // d.setString("device_id", d.getId()); // d.setString("company_id", DeviceManager.getCompareId()); // try { // CouchbaseUtil.database.save(d); // } catch (CouchbaseLiteException e) { // e.printStackTrace(); // } // } } private void testFunction() { // Expression expression = Expression.property("table").equalTo(Expression.string("device")); //// Expression expression = Expression.property("table").equalTo(Expression.string("user")); // List list = BaseManager.findList(expression); // // for (int i = 0; i < list.size(); i++) { //// Map map = CouchbaseUtil.getDocumentStringMap(list.get(i)); //// Log.e("", ""); // if (!list.get(i).getId().equals(DeviceManager.getDeviceId())) { // BaseManager.delete(list.get(i)); // } //// BaseManager.delete(list.get(i)); // } // for (ModelAdapter k : list) { // String device_id = k.getString("device_id"); // String company_id = k.getString("company_id"); // if (CouchbaseUtil.database.getDocument(k.getString("person_id")) == null) { // try { // CouchbaseUtil.database.delete(k); // } catch (CouchbaseLiteException e) { // e.printStackTrace(); // } // } // } // Expression expression = Expression.property("table").equalTo(Expression.string("person_identity")); // List list = BaseManager.findList(expression); // for (ModelAdapter k : list) { // if (CouchbaseUtil.database.getDocument(k.getString("identity_id")) == null) { // try { // CouchbaseUtil.database.delete(k); // } catch (CouchbaseLiteException e) { // e.printStackTrace(); // } // } // } } // ModelAdapter d = new ModelAdapter("9b9754f0-6f53-45fc-b7ad-fedeb4c067f4") // .setString("name", "系统管理员") // .setString("sign_up_status", "已注册") // .setString("verify_status", "不需要验证身份证") // .setString("sign_up_method", "系统初始化分配") // .setString("sign_up_time", System.currentTimeMillis() + "") // .setString("device_id", DeviceManager.getDeviceId()) // //是系统初始化默认设定的 不能删除这个person的管理员身份 // .setString("auto_init", "1") // .setString("del_flag", "0") // .setString("company_id", DeviceManager.getCompareId()) // .setString("table", "person"); // BaseManager.save(d); // List list = BaseManager.findList(Expression.property("table").equalTo(Expression.string("person"))); // for (ModelAdapter document : list) { // if (document.getString("sign_up_method") != null && document.getString("sign_up_method").equals("统一导入")) { // document.setString("del_flag", "0"); // BaseManager.save(document); // } else { // if (document.getString("del_flag") == null) { // BaseManager.delete(document); // } // } // } public void init() { CrashHandler.getCrashHandler().init(this); com.basic.security.utils.CrashHandler.getInstance().init(this); PackageManager packageManager = getPackageManager(); try { PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES); if (packageInfo != null) { devInfo.put("versionName", packageInfo.versionName); devInfo.put("versionCode", packageInfo.versionCode + ""); } Field[] fields = Build.class.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); devInfo.put(field.getName(), field.get(null).toString()); } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } Config.init(); } public void closeApplication() { closeActivitys(); closeServices(); } private void closeActivitys() { ListIterator iterator = activitys.listIterator(); while (iterator.hasNext()) { Activity activity = iterator.next(); if (activity != null) { activity.finish(); } } } private void closeServices() { ListIterator iterator = services.listIterator(); while (iterator.hasNext()) { Service service = iterator.next(); if (service != null) { stopService(new Intent(this, service.getClass())); } } } }