package com.hanghui.senic.common; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.ConnectivityManager; import android.net.LinkAddress; import android.net.LinkProperties; import android.net.Network; import android.net.NetworkInfo; import android.net.Uri; import android.net.wifi.WifiManager; import android.text.TextUtils; import android.text.format.Formatter; import android.util.Base64; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import com.hanghui.senic.BuildConfig; import com.hanghui.senic.MyAppliction; import com.hanghui.senic.baiduface.OnePxActivity; import com.hanghui.senic.service.usbserialdemo.utile.ConfigManager; import com.hanghui.senic.service.usbserialdemo.utile.X1.RelaysAndLedUtile; import com.hanghui.senic.service.usbserialdemo.utile.X5.X5RelaysAndLedUtile; import com.hanghui.senic.service.usbserialdemo.utile.loacat.AppLogUtils; import java.net.InetAddress; public class CommonUtil { public static final String TAG = "CommonUtil"; public static View getContentView(Activity activity) { ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); FrameLayout content = (FrameLayout) decorView.findViewById(android.R.id.content); return content.getChildAt(0); } public static View getDecorView(Activity activity) { ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); return decorView; } public static Activity getCurrentActivity() { MyAppliction appliction = (MyAppliction) MyAppliction.getContext(); Activity currentActivity = appliction.getCurrentActivity(); if (currentActivity == null) { AppLogUtils.e(true, TAG, "currentActivity == null"); } return currentActivity; } public static void gotoOnePxActivity() { Activity activity = CommonUtil.getCurrentActivity(); if (activity == null) { return; } Intent intent = new Intent(activity, OnePxActivity.class); activity.startActivity(intent); } public static String byteArrayToBase64String(byte[] data) { if (data == null) { return ""; } String base64Str = Base64.encodeToString(data, Base64.DEFAULT); return base64Str; } public static byte[] base64StringToByteArray(String base64Str) { if (TextUtils.isEmpty(base64Str)) { return null; } byte[] data = Base64.decode(base64Str, Base64.DEFAULT); return data; } public static Bitmap base64ToBitmap(String base64String) { base64String = Uri.decode(base64String); byte[] decode = Base64.decode(base64String, Base64.DEFAULT); Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length); return bitmap; } public static String getVersionName() { Context context = MyAppliction.getContext(); PackageInfo packageInfo = null; try { packageInfo = context.getPackageManager() .getPackageInfo(context.getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } //获取APP版本versionName String versionName = packageInfo.versionName; //获取APP版本versionCode int versionCode = packageInfo.versionCode; return versionName; } //获取有线网的ip地址 public static String getEthernetIpAddress(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Network[] networks = connectivityManager.getAllNetworks(); for (Network network : networks) { NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); //判断网络类型 有线网 if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) { LinkProperties linkProperties = connectivityManager.getLinkProperties(network); if (linkProperties != null) { for (LinkAddress linkAddress : linkProperties.getLinkAddresses()) { InetAddress inetAddress = linkAddress.getAddress(); return inetAddress.getHostAddress(); } } } } return ""; } //获取无线网的ip地址 public static String getWifiIpAddress(Context context) { WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (wifiMgr != null && wifiMgr.getConnectionInfo() != null) { int ipAddress = wifiMgr.getConnectionInfo().getIpAddress(); return Formatter.formatIpAddress(ipAddress); } return ""; } public static String getIPAddress() { Context context = MyAppliction.getContext().getApplicationContext(); String ip = getEthernetIpAddress(context); if(TextUtils.isEmpty(ip)) { return getWifiIpAddress(context); } return ""; } public static boolean isDebug() { if( BuildConfig.BUILD_TYPE.equals("debug") ){ return true; } return false; } /** * 打开/关闭 补光灯 * * @param isOpenLedLight */ public static void setLedLight(boolean isOpenLedLight) { if (ConfigManager.getAuthenticationBean() != null && ConfigManager.getAuthenticationBean().getData() != null) { // RelaysAndLedUtile.getInstance().setLedLight(ConfigManager.getAuthenticationBean().getData().get(0).getLightMode(), isOpenLedLight); X5RelaysAndLedUtile.getInstance().setLedLight(ConfigManager.getAuthenticationBean().getData().get(0).getLightMode(), isOpenLedLight); } } }