zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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<String, String> 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<String, String> devInfo = new HashMap<>();
    public ExecutorService executorService = Executors.newCachedThreadPool();
    private List<Activity> activitys = new LinkedList<>();
    private List<Service> services = new LinkedList<>();
    private ModelAdapter backTime;
 
    public static BaseApplication getApplication() {
        return application;
    }
 
    public static Map<String, String> 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<String, String> 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<ModelAdapter> list = BaseManager.findList(expression);
//
//        for (int i = 0; i < list.size(); i++) {
////            Map<String, String> 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<ModelAdapter> 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<ModelAdapter> 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<Activity> iterator = activitys.listIterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity != null) {
                activity.finish();
            }
        }
    }
 
    private void closeServices() {
        ListIterator<Service> iterator = services.listIterator();
        while (iterator.hasNext()) {
            Service service = iterator.next();
            if (service != null) {
                stopService(new Intent(this, service.getClass()));
            }
        }
    }
 
}