|
| 1 | +package net.xx.xndroid; |
| 2 | + |
| 3 | + |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.SharedPreferences; |
| 7 | +import android.content.pm.PackageManager; |
| 8 | +import android.net.ConnectivityManager; |
| 9 | +import android.net.NetworkInfo; |
| 10 | +import android.os.Build; |
| 11 | +import android.preference.PreferenceManager; |
| 12 | +import android.widget.Toast; |
| 13 | + |
| 14 | +import net.xx.xndroid.util.LogUtil; |
| 15 | + |
| 16 | +public class AppModel { |
| 17 | + public static String sAppPath; |
| 18 | + public static MainActivity sActivity; |
| 19 | + public static String sFilePath; |
| 20 | + public static String sXndroidFile; |
| 21 | + public static Runnable sUpdateInfoUI; |
| 22 | + public static String sLang; |
| 23 | + public static String sPackageName; |
| 24 | + public static int sVersionCode; |
| 25 | + public static String sVersionName; |
| 26 | + |
| 27 | + public static SharedPreferences sPreferences; |
| 28 | + public static boolean sAutoThread = true; |
| 29 | + public static final String PER_AUTO_THREAD = "XNDROID_AUTO_THREAD"; |
| 30 | + public static final String PER_VERSION = "XNDROID_VERSION"; |
| 31 | + |
| 32 | + |
| 33 | + public static void showToast(final String mesg) { |
| 34 | + sActivity.runOnUiThread(new Runnable() { |
| 35 | + @Override |
| 36 | + public void run() { |
| 37 | + Toast.makeText(sActivity, mesg, Toast.LENGTH_LONG).show(); |
| 38 | + } |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + public static boolean sAppStoped = false; |
| 44 | + public static void appStop(){ |
| 45 | + if(sAppStoped) |
| 46 | + return; |
| 47 | + sAppStoped = true; |
| 48 | + sActivity.runOnUiThread(new Runnable() { |
| 49 | + @Override |
| 50 | + public void run() { |
| 51 | + sActivity.postStop(); |
| 52 | + } |
| 53 | + }); |
| 54 | + new Thread(new Runnable() { |
| 55 | + @Override |
| 56 | + public void run() { |
| 57 | + XXnetService service = XXnetService.getDefaultService(); |
| 58 | + if(service != null) |
| 59 | + service.postStop(); |
| 60 | + android.os.Process.killProcess(android.os.Process.myPid()); |
| 61 | + |
| 62 | + } |
| 63 | + }).start(); |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + public static boolean sDevBatteryLow = false; |
| 69 | + public static boolean sDevMobileWork = false; |
| 70 | + public static boolean sDevScreenOff = false; |
| 71 | + public static boolean sIsForeground = true; |
| 72 | + |
| 73 | + public static void checkNetwork(){ |
| 74 | + sDevMobileWork = false; |
| 75 | + ConnectivityManager connectivityManager = (ConnectivityManager)AppModel |
| 76 | + .sActivity.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 77 | + NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); |
| 78 | + if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) { |
| 79 | + if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) { |
| 80 | + sDevMobileWork = true; |
| 81 | + } |
| 82 | + } |
| 83 | + LogUtil.defaultLogWrite("info", "network change, use_mobile_network=" + AppModel.sDevMobileWork); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public static void appInit(final MainActivity activity){ |
| 88 | + if(sAppStoped) |
| 89 | + return; |
| 90 | + sAppStoped = false; |
| 91 | + sActivity = activity; |
| 92 | + sFilePath = activity.getFilesDir().getAbsolutePath(); |
| 93 | + sAppPath = activity.getFilesDir().getParent(); |
| 94 | + sXndroidFile = sFilePath + "/xndroid_files"; |
| 95 | + sPackageName = activity.getPackageName(); |
| 96 | + try { |
| 97 | + sVersionCode = activity.getPackageManager(). |
| 98 | + getPackageInfo(sPackageName, PackageManager.GET_CONFIGURATIONS).versionCode; |
| 99 | + sVersionName = activity.getPackageManager(). |
| 100 | + getPackageInfo(sPackageName, PackageManager.GET_CONFIGURATIONS).versionName; |
| 101 | + } catch (PackageManager.NameNotFoundException e) { |
| 102 | + e.printStackTrace(); |
| 103 | + } |
| 104 | + sPreferences = PreferenceManager.getDefaultSharedPreferences(activity); |
| 105 | + sAutoThread = sPreferences.getBoolean(PER_AUTO_THREAD, true); |
| 106 | + sPreferences.edit().putInt(PER_VERSION, sVersionCode).apply(); |
| 107 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 108 | + sLang = sActivity.getResources().getConfiguration().getLocales().toString(); |
| 109 | + }else { |
| 110 | + sLang = sActivity.getResources().getConfiguration().locale.toString(); |
| 111 | + } |
| 112 | + Intent intent = new Intent(activity,XXnetService.class); |
| 113 | + activity.startService(intent); |
| 114 | + } |
| 115 | +} |
0 commit comments