forked from qpython-android/qpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
301 lines (255 loc) · 9.26 KB
/
Copy pathApp.java
File metadata and controls
301 lines (255 loc) · 9.26 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package org.qpython.qpy.main.app;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.multidex.MultiDex;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.gson.Gson;
import com.gyf.cactus.Cactus;
import com.gyf.cactus.callback.CactusCallback;
import com.quseit.common.CrashHandler;
import com.quseit.common.updater.downloader.DefaultDownloader;
import com.squareup.leakcanary.LeakCanary;
import org.qpython.qpy.R;
import org.qpython.qpy.main.server.Service;
import org.qpython.qpy.main.server.gist.Api;
import org.qpython.qpy.main.server.gist.TokenManager;
import org.qpython.qpy.main.server.gist.response.GistBean;
import org.qpython.qpy.main.server.http.Retrofitor;
import org.qpython.qpy.utils.NotebookUtil;
import org.qpython.qpysdk.QPyConstants;
import org.qpython.qsl4a.QPyScriptService;
import org.qpython.qsl4a.QSL4APP;
import org.qpython.qsl4a.qsl4a.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class App extends QSL4APP implements CactusCallback{
private static final String TAG = "APP";
private static Context sContext;
private static String sScriptPath;
private static String sProjectPath;
//private static AppCompatActivity sActivity;
private static OkHttpClient okHttpClient;
private static HttpLoggingInterceptor interceptor;
private static Gson gson;
private static DefaultDownloader downloader;
//保存user信息
private static SharedPreferences mPreferences;
private static Retrofit.Builder retrofitBuilder;
private static Service mService;//本地retrofit方法
public static OkHttpClient getOkHttpClient() {
return okHttpClient;
}
public static HttpLoggingInterceptor getInterceptor() {
return interceptor;
}
public static Gson getGson() {
return gson;
}
public static Retrofit.Builder getRetrofit() {
return retrofitBuilder;
}
public static Service getService() {
return mService;
}
public static String getScriptPath() {
return sScriptPath;
}
public static String getProjectPath() {
return sProjectPath;
}
public static Context getContext() {
return sContext;
}
// public static AppCompatActivity getActivity() {
// return sActivity;
// }
//
// public static void setActivity(AppCompatActivity activity) {
// sActivity = activity;
// }
public static User getUser() {
if (mPreferences.getString("email", null) == null) {
return null;
}
User user = new User();
user.setNick(mPreferences.getString("nick", ""));
user.setUserName(mPreferences.getString("name", ""));
user.setUserId(mPreferences.getString("id", ""));
user.setEmail(mPreferences.getString("email", ""));
user.setAvatarUrl(mPreferences.getString("avatar", ""));
return user;
}
public static DefaultDownloader getDownloader() {
return downloader;
}
public static void setUser(User user) {
SharedPreferences.Editor editor = mPreferences.edit();
if (user == null) {
editor.clear();
editor.apply();
return;
}
editor.putString("nick", user.getNick());
editor.putString("name", user.getUserName());
editor.putString("email", user.getEmail());
editor.putString("avatar", user.getAvatarUrl());
editor.putString("id", user.getUserId());
if (!editor.commit()) {
editor.apply();
}
}
private static List<String> favorites = new ArrayList<>();
public static List<String> getFavorites() {
return favorites;
}
public static void addFavorites(GistBean bean) {
favorites.add(bean.getId());
}
public static void setFavorites(List<GistBean> list) {
for (GistBean bean : list) {
favorites.add(bean.getId());
}
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
@Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
sContext = getApplicationContext();
downloader = new DefaultDownloader(sContext);
// init retrofit relate
gson = new Gson();
interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(App.getContext().getCacheDir(), cacheSize);
okHttpClient = new OkHttpClient.Builder().cache(cache).addInterceptor(interceptor).build();
retrofitBuilder = new Retrofit.Builder()
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create());
mService = new Service();
String basePath = QPyConstants.ABSOLUTE_PATH;
sProjectPath = String.format("%s/%s", basePath, "projects");
sScriptPath = String.format("%s/%s", basePath, "scripts");
initLayoutDir();
mPreferences = this.getSharedPreferences("user", 0);
CrashHandler.getInstance().init(this);
AppInit.init(this);
Map<String, String> header = new HashMap<>();
TokenManager.init(this);
header.put("Content-Type", "application/json");
// if (!TextUtils.isEmpty(TokenManager.getToken())) {
// header.put("HTTP_TOKEN", TokenManager.getToken());
// }
Retrofitor
.getInstance()
.setTimeOut(Retrofitor.DEFAULT_TIMEOUT)
// .openDebug(BuildConfig.DEBUG)
// .supportSSL(!BuildConfig.DEBUG)
.addHeaders(header)
.init(Api.BASE_URL);
// if (NotebookUtil.isNotebookLibInstall(getActivity())) {
// if (Utils.httpPing(NotebookUtil.NB_SERVER, 300)) {
// NotebookUtil.startNotebookService(getActivity());
// }
// }
// restart Notebook
if (NotebookUtil.isNBSrvSet(this)) {
NotebookUtil.killNBSrv(this);
NotebookUtil.startNotebookService2(this);
}
initCactus();
}
private void initCactus() {
boolean isKeepAlive = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(getString(R.string.key_alive), false);
LogUtil.e("isKeepAlive:" + isKeepAlive);
if (!isKeepAlive){
LogUtil.e("doWork0000000");
if (!isRunService( "org.qpython.qsl4a.QPyScriptService")) {
startPyService();
}
return;
}
Cactus.getInstance()
.isDebug(true)
.setTitle("QPython")
.setContent("QPython service is alive")
.setLargeIcon(R.drawable.ic_launcher)
.setSmallIcon(R.drawable.ic_launcher)
.hideNotificationAfterO(false)
.addCallback(this)
.register(this);
}
private void initLayoutDir() {
if (Build.VERSION.SDK_INT >= 17) {
Resources resources = getResources();
Configuration config = resources.getConfiguration();
Locale locale = config.locale;
config.setLayoutDirection(locale);
resources.updateConfiguration(config, resources.getDisplayMetrics());
}
}
/**
* 保活的回调接口
* @param i
*/
@Override
public void doWork(int i) {
LogUtil.e("doWork1111111");
if (!isRunService( "org.qpython.qsl4a.QPyScriptService")) {
startPyService();
}
}
/**
* 保活的回调接口
*/
@Override
public void onStop() {}
private void startPyService() {
LogUtil.e("doWork22222");
Log.d(TAG, "startPyService");
Intent intent = new Intent(this, QPyScriptService.class);
startService(intent);
}
/**
* 判断服务是否在运行
*
* @param serviceName
* @return 服务名称为全路径 例如com.ghost.WidgetUpdateService
*/
public boolean isRunService(String serviceName) {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceName.equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}