18
18
import net .xndroid .utils .LogUtils ;
19
19
import net .xndroid .utils .ShellUtils ;
20
20
21
+ import java .io .OutputStreamWriter ;
22
+
21
23
public class AppModel {
22
24
public static String sAppPath ;
25
+ /*use sActivity carefully, it may be null!*/
23
26
public static MainActivity sActivity ;
24
27
public static LaunchService sService ;
25
28
public static String sFilePath ;
@@ -30,6 +33,7 @@ public class AppModel {
30
33
public static int sVersionCode ;
31
34
public static int sLastVersion ;
32
35
public static String sVersionName ;
36
+ public static Context sContext ;
33
37
34
38
public static boolean sDebug = false ;
35
39
public static boolean sLastFail = false ;
@@ -41,65 +45,92 @@ public class AppModel {
41
45
public static final String PER_LAST_FAIL = "XNDROID_LAST_FAIL" ;
42
46
43
47
public static void showToast (final String msg ) {
44
- if (sActivity != null ) {
45
- sActivity .runOnUiThread (new Runnable () {
46
- @ Override
47
- public void run () {
48
- Toast .makeText (sActivity , msg , Toast .LENGTH_LONG ).show ();
49
- }
50
- });
51
- return ;
52
- }
53
- if (sService != null ){
54
- Looper .prepare ();
55
- Toast .makeText (sService .getApplicationContext (), msg , Toast .LENGTH_LONG ).show ();
56
- Looper .loop ();
48
+ try {
49
+ if (sActivity != null ) {
50
+ sActivity .runOnUiThread (new Runnable () {
51
+ @ Override
52
+ public void run () {
53
+ Toast .makeText (sActivity , msg , Toast .LENGTH_LONG ).show ();
54
+ }
55
+ });
56
+ return ;
57
+ }
58
+ if (sService != null ){
59
+ Looper .prepare ();
60
+ Toast .makeText (sService .getApplicationContext (), msg , Toast .LENGTH_LONG ).show ();
61
+ Looper .loop ();
62
+ }
63
+ }catch (Exception e ){
64
+ e .printStackTrace ();
57
65
}
66
+
58
67
}
59
68
60
69
public static void exportLogs (){
61
- ShellUtils .execBusybox ("tar -czf /sdcard/xndroid-logs.tar.gz log/ fqrouter/log/" );
70
+ ShellUtils .execBusybox ("tar -czf /sdcard/xndroid-logs.tar.gz -C " + sXndroidFile + " log/ fqrouter/log/" );
71
+ showToast (sContext .getString (R .string .log_exported ));
62
72
}
63
73
64
74
public static void forceStop (){
65
- // android.os.Process.killProcess(android.os.Process.myPid());
66
-
67
75
String cmd = "busybox ps |busybox grep net.xndroid | busybox cut -c 1-6 |busybox xargs kill -9" ;
68
- cmd = cmd .replace ("busybox" , ShellUtils .sBusyBox );
69
- ShellUtils .exec (cmd );
76
+ cmd = cmd .replace ("busybox" , sXndroidFile + "/busybox" );
77
+ try {
78
+ ShellUtils .exec (cmd );
79
+ }catch (Exception e ){
80
+ e .printStackTrace ();
81
+ try {
82
+ Process process = Runtime .getRuntime ().exec (sXndroidFile + "/busybox sh" );
83
+ OutputStreamWriter sInStream = new OutputStreamWriter (process .getOutputStream ());
84
+ sInStream .write (cmd );
85
+ sInStream .write ('\n' );
86
+ sInStream .flush ();
87
+ process .waitFor ();
88
+ }catch (Exception ee ){
89
+ ee .printStackTrace ();
90
+ android .os .Process .killProcess (android .os .Process .myPid ());
91
+ }
92
+ }
93
+
70
94
}
71
95
72
96
73
97
public static void fatalError (final String msg ){
74
- LogUtils .e ("FatalError: " + msg );
75
- exportLogs ();
76
-
77
- if (sActivity != null ) {
78
- sActivity .runOnUiThread (new Runnable () {
79
- @ Override
80
- public void run () {
81
- new AlertDialog .Builder (AppModel .sActivity )
82
- .setTitle ("FatalError" )
83
- .setMessage (msg + "\n \n logs will be exported to /sdcard/xndroid-logs.tar.gz" )
84
- .setNegativeButton ("exit" , new DialogInterface .OnClickListener () {
85
- @ Override
86
- public void onClick (DialogInterface dialog , int which ) {
87
- forceStop ();
88
- }
89
- }).create ().show ();
98
+ try {
99
+ LogUtils .e ("FatalError: " + msg );
100
+ exportLogs ();
101
+
102
+ if (sActivity != null ) {
103
+ sActivity .runOnUiThread (new Runnable () {
104
+ @ Override
105
+ public void run () {
106
+ new AlertDialog .Builder (AppModel .sActivity )
107
+ .setTitle (R .string .fatalerror )
108
+ .setMessage (msg )
109
+ .setCancelable (false )
110
+ .setNegativeButton (R .string .exit , new DialogInterface .OnClickListener () {
111
+ @ Override
112
+ public void onClick (DialogInterface dialog , int which ) {
113
+ forceStop ();
114
+ }
115
+ }).create ().show ();
116
+ }
117
+ });
118
+ } else {
119
+ showToast (sContext .getString (R .string .fatalerror ) + ": " + msg );
120
+ forceStop ();
121
+ }
122
+ while (true ) {
123
+ try {
124
+ Thread .sleep (1000 );
125
+ } catch (InterruptedException e ) {
126
+ e .printStackTrace ();
90
127
}
91
- });
92
- }else {
93
- showToast ("FatalError: " + msg + " logs will be exported to /sdcard/xndroid-logs.tar.gz" );
94
- forceStop ();
95
- }
96
- while (true ){
97
- try {
98
- Thread .sleep (1000 );
99
- } catch (InterruptedException e ) {
100
- e .printStackTrace ();
101
128
}
129
+ }catch (Exception e ){
130
+ e .printStackTrace ();
131
+ forceStop ();
102
132
}
133
+
103
134
}
104
135
105
136
@@ -109,6 +140,7 @@ private static boolean isApkInDebug(Context context) {
109
140
ApplicationInfo info = context .getApplicationInfo ();
110
141
return (info .flags & ApplicationInfo .FLAG_DEBUGGABLE ) != 0 ;
111
142
} catch (Exception e ) {
143
+ e .printStackTrace ();
112
144
return false ;
113
145
}
114
146
}
@@ -119,27 +151,30 @@ public static void appStop(){
119
151
if (sAppStoped )
120
152
return ;
121
153
sAppStoped = true ;
122
- sPreferences .edit ().putBoolean (PER_LAST_FAIL , false ).commit ();
123
- if (sActivity != null ) {
124
- sActivity .runOnUiThread (new Runnable () {
154
+ try {
155
+ sPreferences .edit ().putBoolean (PER_LAST_FAIL , false ).commit ();
156
+ if (sActivity != null ) {
157
+ sActivity .runOnUiThread (new Runnable () {
158
+ @ Override
159
+ public void run () {
160
+ sActivity .postStop ();
161
+ }
162
+ });
163
+ }
164
+
165
+ new Thread (new Runnable () {
125
166
@ Override
126
167
public void run () {
127
- sActivity .postStop ();
168
+ LogUtils .i ("appStop" );
169
+ LaunchService .postStop ();
170
+ forceStop ();
128
171
}
129
- });
172
+ }).start ();
173
+ }catch (Exception e ){
174
+ e .printStackTrace ();
175
+ forceStop ();
130
176
}
131
177
132
- new Thread (new Runnable () {
133
- @ Override
134
- public void run () {
135
- // XXnetService service = XXnetService.getDefaultService();
136
- // if(service != null)
137
- // service.postStop();
138
- LogUtils .i ("appStop" );
139
- LaunchService .postStop ();
140
- forceStop ();
141
- }
142
- }).start ();
143
178
144
179
}
145
180
@@ -149,17 +184,19 @@ public void run() {
149
184
public static boolean sDevScreenOff = false ;
150
185
public static boolean sIsForeground = true ;
151
186
152
- public static void checkNetwork (){
187
+ private static ConnectivityManager sConnectivityManager ;
188
+ public static void getNetworkState (){
189
+ if (sConnectivityManager == null ){
190
+ sConnectivityManager = (ConnectivityManager )sContext .getSystemService (Context .CONNECTIVITY_SERVICE );
191
+ }
153
192
sDevMobileWork = false ;
154
- ConnectivityManager connectivityManager = (ConnectivityManager )AppModel
155
- .sActivity .getSystemService (Context .CONNECTIVITY_SERVICE );
156
- NetworkInfo activeNetworkInfo = connectivityManager .getActiveNetworkInfo ();
193
+ NetworkInfo activeNetworkInfo = sConnectivityManager .getActiveNetworkInfo ();
157
194
if (activeNetworkInfo != null && activeNetworkInfo .isConnected ()) {
158
195
if (activeNetworkInfo .getType () == (ConnectivityManager .TYPE_MOBILE )) {
159
196
sDevMobileWork = true ;
160
197
}
161
198
}
162
- LogUtils .i ("network change, use_mobile_network=" + AppModel . sDevMobileWork );
199
+ LogUtils .i ("network change, use_mobile_network=" + sDevMobileWork );
163
200
}
164
201
165
202
private static void updataEnv (int lastVersion ){
@@ -172,6 +209,7 @@ public static void appInit(final MainActivity activity){
172
209
return ;
173
210
sAppStoped = false ;
174
211
sActivity = activity ;
212
+ sContext = activity .getApplicationContext ();
175
213
sFilePath = activity .getFilesDir ().getAbsolutePath ();
176
214
sAppPath = activity .getFilesDir ().getParent ();
177
215
sXndroidFile = sFilePath + "/xndroid_files" ;
0 commit comments