123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699 |
- package com.edufound.mobile.xxt.util;
- import android.app.Activity;
- import android.app.AlarmManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.os.Build;
- import android.support.v4.content.FileProvider;
- import android.text.Editable;
- import android.text.Selection;
- import android.text.TextWatcher;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.EditText;
- import android.widget.TextView;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.InetAddress;
- import java.net.MalformedURLException;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.net.URL;
- import java.security.MessageDigest;
- import java.util.Enumeration;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class EduFoundUtil {
- /**
- * getVersionCode 得到名称
- */
- public static String getVersion(Context context)// 得到版本
- {
- try {
- PackageInfo pi = context.getPackageManager()
- .getPackageInfo(context.getPackageName(), 0);
- return pi.versionName;
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * getVersionCode 得到版本号
- */
- public static int getVersionCode(Context context) {
- try {
- PackageInfo pi = context.getPackageManager()
- .getPackageInfo(context.getPackageName(), 0);
- return pi.versionCode;
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- return 0;
- }
- }
- private static String parseByte(byte b) {
- String s = "00" + Integer.toHexString(b) + ":";
- return s.substring(s.length() - 3);
- }
- public static void openFile(Context context, File file) {
- // TODO Auto-generated method stub
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
- Uri uri = FileProvider.getUriForFile(context, "com.edufound.mobile.fileprovider", file);
- intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
- intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
- intent.setDataAndType(uri, "application/vnd.android.package-archive");
- } else {
- String cmd = "chmod 777 " + file;
- try {
- Runtime.getRuntime().exec(cmd);
- Log.e("OpenFile", file.getName());
- intent.setAction(android.content.Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- context.startActivity(intent);
- }
- /**
- * long转换成String时间
- */
- public static String getTime(int mss) {
- int hours = (mss / (1000 * 60 * 60));
- int minutes = (mss - hours * (1000 * 60 * 60)) / (1000 * 60);
- int m = hours * 60 + minutes;
- int seconds = (mss - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
- return String.format("%02d:%02d", m, seconds);
- }
- /**
- * 得到宽高缩放比
- */
- public static float initDesignSize(Context context, int screenWidth, int screenHeight) {
- int designWidth = 1280;
- int designHeight = 720;
- float ds = 1.0f * designWidth / designHeight;
- float ts = 1.0f * screenWidth / screenHeight;
- if (ds >= ts) {
- int targetWidth = screenWidth;
- float designScale = 1.0f * targetWidth / designWidth;
- return designScale;
- } else {
- int targetHeight = screenHeight;
- float designScale = 1.0f * targetHeight / designHeight;
- return designScale;
- }
- }
- /**
- * 设置dpi缩放比
- */
- public static void setDisplay(Context context, float designScale) {
- DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
- displayMetrics.density = designScale;
- displayMetrics.scaledDensity = displayMetrics.density;
- displayMetrics.xdpi = displayMetrics.density * 160;
- displayMetrics.ydpi = displayMetrics.xdpi;
- displayMetrics.densityDpi = Float.valueOf(displayMetrics.xdpi).intValue();
- }
- public static String getMacAddress() {
- String result = "";
- String Mac = "";
- result = callCmd("busybox ifconfig", "HWaddr");
- if (result == null) {
- return "网络出错,请检查网络";
- }
- if (result.length() > 0 && result.contains("HWaddr")) {
- Mac = result.substring(result.indexOf("HWaddr") + 6, result.length() - 1);
- if (Mac.length() > 1) {
- result = Mac.toLowerCase();
- }
- }
- return result.trim();
- }
- public static String getEthernetMac() {
- String mac = null;
- try {
- Process p = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address");
- InputStream is = p.getInputStream();
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader bf = new BufferedReader(isr);
- String line = null;
- if ((line = bf.readLine()) != null) {
- mac = line;
- }
- bf.close();
- isr.close();
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return mac;
- }
- public static String callCmd(String cmd, String filter) {
- String result = "";
- String line = "";
- try {
- Process proc = Runtime.getRuntime().exec(cmd);
- InputStreamReader is = new InputStreamReader(proc.getInputStream());
- BufferedReader br = new BufferedReader(is);
- // 执行命令cmd,只取结果中含有filter的这一行
- while ((line = br.readLine()) != null && line.contains(filter) == false) {
- // result += line;
- Log.i("test", "line: " + line);
- }
- result = line;
- Log.i("test", "result: " + result);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- public static void runCmd(String cmd) {
- Runtime mRuntime = Runtime.getRuntime();
- try {
- //Process中封装了返回的结果和执行错误的结果
- Process mProcess = mRuntime.exec(cmd);
- BufferedReader mReader = new BufferedReader(new InputStreamReader(mProcess.getInputStream()));
- StringBuffer mRespBuff = new StringBuffer();
- char[] buff = new char[1024];
- int ch = 0;
- while ((ch = mReader.read(buff)) != -1) {
- mRespBuff.append(buff, 0, ch);
- }
- mReader.close();
- Logger.DebugE(mRespBuff.toString());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * 根据wifi信息获取本地mac
- *
- * @param context
- * @return
- */
- public static String getLocalMacAddressFromWifiInfo(Context context) {
- WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
- WifiInfo winfo = wifi.getConnectionInfo();
- String mac = winfo.getMacAddress();
- return mac;
- }
- // 有线ip得mac
- public static String getLocalMacAddressFromIp(Context context) {
- String mac_s = "";
- String macAddress = "";
- try {
- byte[] mac;
- NetworkInterface ne = NetworkInterface.getByInetAddress(InetAddress
- .getByName(getLocalIpAddress()));
- mac = ne.getHardwareAddress();
- mac_s = bytes2hex02(mac);
- macAddress = mac_s.substring(0, 2) + ":" + mac_s.substring(2, 4) + ":"
- + mac_s.substring(4, 6) + ":" + mac_s.substring(6, 8) + ":"
- + mac_s.substring(8, 10) + ":" + mac_s.substring(10, 12);
- return macAddress;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return macAddress;
- }
- /**
- * 二进制转十六进制
- *
- * @param
- * @return
- */
- public static String bytes2hex02(byte[] bytes) {
- StringBuilder sb = new StringBuilder();
- String tmp = null;
- for (byte b : bytes) {
- // 将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制
- tmp = Integer.toHexString(0xFF & b);
- if (tmp.length() == 1)// 每个字节8为,转为16进制标志,2个16进制位
- {
- tmp = "0" + tmp;
- }
- sb.append(tmp);
- }
- return sb.toString();
- }
- // 获得ip地址
- public static String getLocalIpAddress() {
- try {
- for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
- NetworkInterface intf = (NetworkInterface) en.nextElement();
- for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
- InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
- if (!inetAddress.isLoopbackAddress()) {
- return inetAddress.getHostAddress().toString();
- }
- }
- }
- } catch (SocketException ex) {
- ex.printStackTrace();
- }
- return null;
- }
- /**
- * 判断String是否为空
- *
- * @return boolean是否为空
- */
- public static Boolean isEmpty(String s) {
- if (s == null || s.length() == 0) {
- return true;
- } else {
- return false;
- }
- }
- public static String getIP(String ip) {
- StringBuffer s = new StringBuffer();
- String ss = null;
- Process p;
- try {
- p = Runtime.getRuntime().exec("/system/bin/ping " + ip);
- BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
- String str = new String();
- // 读出所有信息并显示
- while ((str = buf.readLine()) != null && s.length() < 100) {
- str = str + "\r\n";
- s.append(str);
- }
- ss = EduFoundUtil.getTelnum(s.toString()).split(",")[0];
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return ss;
- }
- public static void saveIsHavePhone(Context context, boolean isHavephone) {
- SharedPreferences preferences = context.getSharedPreferences("isHavePhone",
- Context.MODE_PRIVATE);
- Editor editor = preferences.edit();
- editor.putBoolean("isHadPhone", isHavephone);
- editor.commit();
- }
- public static boolean getIsHavePhone(Context context) {
- SharedPreferences preferences = context.getSharedPreferences("isHavePhone",
- Context.MODE_PRIVATE);
- boolean isHave = preferences.getBoolean("isHadPhone", false);
- return isHave;
- }
- public static boolean isMobile(String str) {
- Pattern p = null;
- Matcher m = null;
- boolean b = false;
- p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
- m = p.matcher(str);
- b = m.matches();
- return b;
- }
- public static String getTelnum(String sParam) {
- if (sParam.length() <= 0)
- return "";
- // Pattern pattern = Pattern.compile("(1|[0-9])([0-9])\\d{9}$*");
- // Pattern pattern =
- // Pattern.compile("((13[0-9])|(14[7])|(15[0-9])|17[0-9]|(18[0-9]))\\d{8}$*");
- Pattern pattern = Pattern
- .compile("(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$*");
- Matcher matcher = pattern.matcher(sParam);
- StringBuffer bf = new StringBuffer();
- while (matcher.find()) {
- bf.append(matcher.group()).append(",");
- }
- int len = bf.length();
- if (len > 0) {
- bf.deleteCharAt(len - 1);
- }
- return bf.toString();
- }
- public static String getSerial() {
- String s;
- if (EduFoundUtil.isEmpty(EduFoundUtil.getMacAddress())) {
- s = "35" + Build.BOARD.length() + Build.BRAND.length() + Build.CPU_ABI.length()
- + Build.DEVICE.length() + Build.DISPLAY.length() + Build.HOST.length()
- + Build.ID.length() + Build.MANUFACTURER.length() + Build.MODEL.length()
- + Build.PRODUCT.length() + Build.TAGS.length() + Build.TYPE.length()
- + Build.USER.length() + EduFoundUtil.getEthernetMac();
- } else {
- s = "35" + Build.BOARD.length() + Build.BRAND.length() + Build.CPU_ABI.length()
- + Build.DEVICE.length() + Build.DISPLAY.length() + Build.HOST.length()
- + Build.ID.length() + Build.MANUFACTURER.length() + Build.MODEL.length()
- + Build.PRODUCT.length() + Build.TAGS.length() + Build.TYPE.length()
- + Build.USER.length() + EduFoundUtil.getMacAddress();
- }
- Logger.e("serial=======" + s);
- return md5Encode(s);
- }
- // md5加密
- public static String md5Encode(String inStr) {
- MessageDigest md5 = null;
- try {
- md5 = MessageDigest.getInstance("MD5");
- } catch (Exception e) {
- System.out.println(e.toString());
- e.printStackTrace();
- return "";
- }
- byte[] byteArray = null;
- try {
- byteArray = inStr.getBytes("UTF-8");
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- byte[] md5Bytes = md5.digest(byteArray);
- StringBuffer hexValue = new StringBuffer();
- for (int i = 0; i < md5Bytes.length; i++) {
- int val = ((int) md5Bytes[i]) & 0xff;
- if (val < 16) {
- hexValue.append("0");
- }
- hexValue.append(Integer.toHexString(val));
- }
- return hexValue.toString();
- }
- public static void savePlayMode(Context context, String value) {
- SharedPreferences preferences = context.getSharedPreferences("PlayMode",
- Context.MODE_PRIVATE);
- Editor editor = preferences.edit();
- editor.putString("playmode", value);
- editor.commit();
- }
- public static String getPlayMode(Context context) {
- SharedPreferences preferences = context.getSharedPreferences("PlayMode",
- Context.MODE_PRIVATE);
- String playMode = preferences.getString("playmode", "default");
- return playMode;
- }
- public static void saveStuNo(Context context, String value) {
- SharedPreferences preferences = context.getSharedPreferences("STUNO", Context.MODE_PRIVATE);
- Editor editor = preferences.edit();
- editor.putString("stu_no", value);
- editor.commit();
- }
- public static String getStuNo(Context context) {
- SharedPreferences preferences = context.getSharedPreferences("STUNO", Context.MODE_PRIVATE);
- String stu_no = preferences.getString("stu_no", "");
- return stu_no;
- }
- /**
- * 银行卡四位加空格
- *
- * @param mEditText
- */
- public static void bankCardNumAddSpace(final EditText mEditText) {
- mEditText.addTextChangedListener(new TextWatcher() {
- int beforeTextLength = 0;
- int onTextLength = 0;
- boolean isChanged = false;
- int location = 0;// 记录光标的位置
- private char[] tempChar;
- private StringBuffer buffer = new StringBuffer();
- int konggeNumberB = 0;
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- beforeTextLength = s.length();
- if (buffer.length() > 0) {
- buffer.delete(0, buffer.length());
- }
- konggeNumberB = 0;
- for (int i = 0; i < s.length(); i++) {
- if (s.charAt(i) == ' ') {
- konggeNumberB++;
- }
- }
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- onTextLength = s.length();
- buffer.append(s.toString());
- if (onTextLength == beforeTextLength || onTextLength <= 3 || isChanged) {
- isChanged = false;
- return;
- }
- isChanged = true;
- }
- @Override
- public void afterTextChanged(Editable s) {
- if (isChanged) {
- location = mEditText.getSelectionEnd();
- int index = 0;
- while (index < buffer.length()) {
- if (buffer.charAt(index) == ' ') {
- buffer.deleteCharAt(index);
- } else {
- index++;
- }
- }
- index = 0;
- int konggeNumberC = 0;
- while (index < buffer.length()) {
- if ((index == 4 || index == 9 || index == 14 || index == 19)) {
- buffer.insert(index, ' ');
- konggeNumberC++;
- }
- index++;
- }
- if (konggeNumberC > konggeNumberB) {
- location += (konggeNumberC - konggeNumberB);
- }
- tempChar = new char[buffer.length()];
- buffer.getChars(0, buffer.length(), tempChar, 0);
- String str = buffer.toString();
- if (location > str.length()) {
- location = str.length();
- } else if (location < 0) {
- location = 0;
- }
- mEditText.setText(str);
- Editable etable = mEditText.getText();
- Selection.setSelection(etable, location);
- isChanged = false;
- }
- }
- });
- }
- public static Bitmap returnBitMap(String url) {
- URL myFileUrl = null;
- Bitmap bitmap = null;
- try {
- myFileUrl = new URL(url);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- try {
- HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
- conn.setDoInput(true);
- conn.connect();
- InputStream is = conn.getInputStream();
- bitmap = BitmapFactory.decodeStream(is);
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return bitmap;
- }
- public static void bankCardNumAddSpace1(final TextView mEditText) {
- mEditText.addTextChangedListener(new TextWatcher() {
- int beforeTextLength = 0;
- int onTextLength = 0;
- boolean isChanged = false;
- int location = 0;// 记录光标的位置
- private char[] tempChar;
- private StringBuffer buffer = new StringBuffer();
- int konggeNumberB = 0;
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- beforeTextLength = s.length();
- if (buffer.length() > 0) {
- buffer.delete(0, buffer.length());
- }
- konggeNumberB = 0;
- for (int i = 0; i < s.length(); i++) {
- if (s.charAt(i) == ' ') {
- konggeNumberB++;
- }
- }
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- onTextLength = s.length();
- buffer.append(s.toString());
- if (onTextLength == beforeTextLength || onTextLength <= 3 || isChanged) {
- isChanged = false;
- return;
- }
- isChanged = true;
- }
- @Override
- public void afterTextChanged(Editable s) {
- if (isChanged) {
- location = mEditText.getSelectionEnd();
- int index = 0;
- while (index < buffer.length()) {
- if (buffer.charAt(index) == ' ') {
- buffer.deleteCharAt(index);
- } else {
- index++;
- }
- }
- index = 0;
- int konggeNumberC = 0;
- while (index < buffer.length()) {
- if ((index == 4 || index == 9 || index == 14 || index == 19)) {
- buffer.insert(index, ' ');
- konggeNumberC++;
- }
- index++;
- }
- if (konggeNumberC > konggeNumberB) {
- location += (konggeNumberC - konggeNumberB);
- }
- tempChar = new char[buffer.length()];
- buffer.getChars(0, buffer.length(), tempChar, 0);
- String str = buffer.toString();
- if (location > str.length()) {
- location = str.length();
- } else if (location < 0) {
- location = 0;
- }
- mEditText.setText(str);
- // Editable etable = mEditText.getText();
- // Selection.setSelection(etable, location);
- isChanged = false;
- }
- }
- });
- }
- public static void restartApplication(Context context) {
- Intent intent = context.getPackageManager()
- .getLaunchIntentForPackage(context.getPackageName());
- PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
- AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
- mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用
- System.exit(0);
- }
- public static void setHideVirtualKey(Window window) {
- //保持布局状态
- int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
- //布局位于状态栏下方
- View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
- //全屏
- View.SYSTEM_UI_FLAG_FULLSCREEN |
- //隐藏导航栏
- View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
- View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
- if (Build.VERSION.SDK_INT >= 19) {
- uiOptions |= 0x00001000;
- } else {
- uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
- }
- window.getDecorView().setSystemUiVisibility(uiOptions);
- }
- public static void openFullScreenModel(Activity activity) {
- activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
- WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
- lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
- activity.getWindow().setAttributes(lp);
- View decorView = activity.getWindow().getDecorView();
- int systemUiVisibility = decorView.getSystemUiVisibility();
- int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_FULLSCREEN;
- systemUiVisibility |= flags;
- activity.getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
- }
- }
|