-
Notifications
You must be signed in to change notification settings - Fork 7
/
DemoApplication.java
executable file
·159 lines (132 loc) · 4.47 KB
/
DemoApplication.java
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
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package apijson.demo.application;
import apijson.demo.manager.DataManager;
import apijson.demo.model.User;
import android.app.Application;
import android.content.res.Configuration;
import android.support.annotation.NonNull;
import apijson.demo.BuildConfig;
import apijson.demo.R;
import uiauto.UIAutoApp;
import zuo.biao.library.base.BaseApplication;
import zuo.biao.library.util.StringUtil;
import android.util.Log;
import com.alibaba.fastjson.JSONArray;
import java.util.List;
/**Application
* @author Lemon
*/
public class DemoApplication extends BaseApplication {
private static final String TAG = "DemoApplication";
private static DemoApplication context;
public static DemoApplication getInstance() {
return context;
}
// 暂时以继承方式实现,后续改为支持静态调用(需要把 UIAutoApp 成员变量全改为 static)
@Override
public void onCreate() {
super.onCreate();
context = this;
UIAutoApp.getInstance().initUIAuto(this);
Thread.UncaughtExceptionHandler handler = Thread.currentThread().getUncaughtExceptionHandler();
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
if (BuildConfig.DEBUG) {
if (handler != null) {
handler.uncaughtException(t, e);
} else {
t.stop(e);
}
} else {
e.printStackTrace();
// TODO 上传到 Bugly 等日志平台
}
}
});
}
// public static List<Object> getOutputList(int limit, int offset) {
// return getOutputList(UIAutoApp.getInstance(), limit, offset);
// }
// public static List<Object> getOutputList(DemoApplication app, int limit, int offset) {
// return UIAutoApp.getOutputList(UIAutoApp.getInstance(), limit, offset);
// }
//
// public static List<Object> getOutputList(UIAutoApp app, int limit, int offset) {
// if (app == null) {
// app = UIAutoApp.getInstance();
// }
// return UIAutoApp.getOutputList(app, limit, offset);
// }
//
// public static void prepareReplay(JSONArray eventList) {
// UIAutoApp.getInstance().prepareReplay(eventList);
// }
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
UIAutoApp.getInstance().onConfigurationChanged(newConfig);
}
/**获取当前用户id
* @return
*/
public long getCurrentUserId() {
currentUser = getCurrentUser();
Log.d(TAG, "getCurrentUserId currentUserId = " + (currentUser == null ? "null" : currentUser.getId()));
return currentUser == null ? 0 : currentUser.getId();
}
/**获取当前用户phone
* @return
*/
public String getCurrentUserPhone() {
currentUser = getCurrentUser();
return currentUser == null ? null : currentUser.getPhone();
}
private static User currentUser = null;
public User getCurrentUser() {
if (currentUser == null) {
currentUser = DataManager.getInstance().getCurrentUser();
}
return currentUser;
}
public void saveCurrentUser(User user) {
if (user == null) {
Log.e(TAG, "saveCurrentUser currentUser == null >> return;");
return;
}
if (user.getId() <= 0 && StringUtil.isNotEmpty(user.getName(), true) == false) {
Log.e(TAG, "saveCurrentUser user.getId() <= 0" +
" && StringUtil.isNotEmpty(user.getName(), true) == false >> return;");
return;
}
if (currentUser != null && user.getId().equals(currentUser.getId())
&& StringUtil.isNotEmpty(user.getPhone(), true) == false) {
user.setPhone(currentUser.getPhone());
}
currentUser = user;
DataManager.getInstance().saveCurrentUser(currentUser);
}
public void logout() {
currentUser = null;
DataManager.getInstance().saveCurrentUser(currentUser);
}
/**判断是否为当前用户
* @param userId
* @return
*/
public boolean isCurrentUser(long userId) {
return DataManager.getInstance().isCurrentUser(userId);
}
public boolean isLoggedIn() {
return getCurrentUserId() > 0;
}
}