org.sltpaya.tool.Utils.java Source code

Java tutorial

Introduction

Here is the source code for org.sltpaya.tool.Utils.java

Source

/*
 * Copyright (c) 2012-2016 Arne Schwabe
 * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
 */

package org.sltpaya.tool;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static android.content.ContentValues.TAG;

public class Utils {

    private static float density = 0;
    private static int widthPixels = 0;
    private static int heightPixels = 0;
    private static Handler mHandler;
    private static WifiManager mWifiManager;

    /**
     * ?ApplicationContext?Application?mApplication
     * {@link Context}
     *
     * @return Context
     */
    public static Context getContext() {
        return ToolApplication.getContext();
    }

    /**
     * ?
     * @return LayoutInflater
     */
    public static LayoutInflater getLayoutInflater() {
        return (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * ?Resources
     * {@link Resources}
     *
     * @return Resources
     */
    public static Resources getResources() {
        return getContext().getResources();
    }

    /**
     * ?ID?
     * {@link Resources}
     *
     * @param id ?
     * @return String
     * @throws Resources.NotFoundException ??ID?
     */
    public static String getString(@StringRes int id) {
        return getResources().getString(id);
    }

    /**
     * ContextgetDrawable()???ContextCompat.getDrawable(Context,resId)?
     * Drawable
     * {@link ContextCompat}
     * {@link android.support.v4.content.ContextCompatApi23}
     *
     * @param id Drawable?ID
     * @return Drawable
     * @throws Resources.NotFoundException ??ID?
     */
    public static Drawable getDrawable(@DrawableRes int id) {
        return ContextCompat.getDrawable(getContext(), id);
    }

    /**
     * ?
     *
     * @param runnable r
     */
    public static void runOnUiThread(Runnable runnable) {
        if (mHandler == null) {
            mHandler = new Handler(Looper.getMainLooper());
        }
        mHandler.post(runnable);
    }

    /**
     * <p>ContextgetColor???ContextCompat.getColor(Context,resID)?</p>
     * <p>api23ContextCompatApi23.getColor(Context,id);?</p>
     * {@link ContextCompat}
     * {@link android.support.v4.content.ContextCompatApi23}
     *
     * @return 0xAARRGGBB
     * @throws Resources.NotFoundException ??ID?
     */
    @ColorInt
    public static int getColor(@ColorRes int resId) {
        return ContextCompat.getColor(getContext(), resId);
    }

    /**
     * ???
     */
    private static void initMonitor() {
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        density = displayMetrics.density;
        widthPixels = displayMetrics.widthPixels;
        heightPixels = displayMetrics.heightPixels;
    }

    /**
     * ??
     *
     * @return density
     */
    public static float getDensity() {
        if (density == 0)
            initMonitor();
        return density;
    }

    /**
     * ???
     *
     * @return widthPixels
     */
    public static int getWidthPixels() {
        if (widthPixels == 0)
            initMonitor();
        return widthPixels;
    }

    /**
     * ???
     *
     * @return HeightPixels
     */
    public static int getHeightPixels() {
        if (heightPixels == 0)
            initMonitor();
        return heightPixels;
    }

    /**
     * dp?
     *
     * @param dp dp
     * @return ?
     */
    public static int dp2px(int dp) {
        return (int) (dp * getDensity() + 0.5f);
    }

    /**
     * ?dp
     *
     * @param px ?
     * @return dp
     */
    public static int px2dp(int px) {
        return (int) (px / getDensity() + 0.5f);
    }

    /**
     * ????
     * {@link  PackageManager}
     *
     * @return ?
     */
    public static String getApplicationVersionName() {
        Context context = getContext();
        PackageManager packageManager = context.getPackageManager();
        String versionName = null;
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * ???VersionCode
     *
     * @return VersionCode
     */
    public static int getApplicationVersionCode() {
        Context context = getContext();
        PackageManager packageManager = context.getPackageManager();
        int versionCode = -1;
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * ?SDK? API-xx
     *
     * @return SDK?
     */
    public static int getSDKVersion() {
        return Build.VERSION.SDK_INT;
    }

    /**
     * ??
     *
     * @return ???
     * private final int WIFI_AP_STATE_DISABLING = 10;
     * private final int WIFI_AP_STATE_DISABLED = 11;
     * private final int WIFI_AP_STATE_ENABLING = 12;
     * private final int WIFI_AP_STATE_ENABLED = 13;
     * private final int WIFI_AP_STATE_FAILED = 14;
     * ???<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
     */
    public static int getWifiApState() {
        if (mWifiManager == null)
            mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
        try {
            Method getWifiApState = mWifiManager.getClass().getDeclaredMethod("getWifiApState");
            return (int) getWifiApState.invoke(mWifiManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 14;
    }

    /**
     * wifi???
     *
     * @return boolean
     */
    public static boolean isWifiApEnabled() {
        if (mWifiManager == null)
            mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
        try {
            Method method = mWifiManager.getClass().getMethod("isWifiApEnabled");
            method.setAccessible(true);
            return (Boolean) method.invoke(mWifiManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * WIFI?
     *
     * @return WIFI??
     */
    private static HashMap<String, ArrayList<String>> getWifiApInfo() {
        HashMap<String, ArrayList<String>> mWifiInfoMap = new HashMap<>();
        ArrayList<String> ipList = new ArrayList<>();
        ArrayList<String> macList = new ArrayList<>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/net/arp"));
            String line, ip = null;
            String mac = null;
            while ((line = reader.readLine()) != null) {
                String[] part = line.split(" +");
                for (String item : part) {
                    ip = item.matches(".{1,3}\\..{1,3}\\..{1,3}\\..{1,3}") ? item : ip;
                    mac = item.matches(".{1,3}:.{1,3}:.{1,3}:.{1,3}:.{1,3}:.{1,3}") ? item : mac;
                }
                if (ip != null && mac != null) {
                    ipList.add(ip);
                    macList.add(mac);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert reader != null;
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        mWifiInfoMap.put("ip", ipList);
        mWifiInfoMap.put("mac", macList);
        return mWifiInfoMap;
    }

    /**
     * ?WIFI AP
     *
     * @return int
     * @throws RuntimeException WIFI?
     * @see #getWifiApInfo()
     * @see #getWifiApState()
     */
    public static int getWifiApClientCount() {
        if (getWifiApState() == 13 || getWifiApState() == 12) {
            HashMap<String, ArrayList<String>> map = getWifiApInfo();
            ArrayList<String> ip = map.get("ip");
            return ip.size();
        } else {
            throw new RuntimeException("The WifiAp is not enable!");
        }
    }

    /**
     * ?IP?
     *
     * @return String
     * @throws RuntimeException WIFI?
     * @see #getWifiApInfo()
     * @see #getWifiApState()
     */
    public static String getWifiApIp() {
        if (getWifiApState() == 13 || getWifiApState() == 12) {
            StringBuilder builder = new StringBuilder();
            HashMap<String, ArrayList<String>> map = getWifiApInfo();
            ArrayList<String> ip = map.get("ip");
            for (String ipItem : ip) {
                builder.append(ipItem).append("\n");
            }
            return builder.toString();
        } else {
            throw new RuntimeException("The WifiAp is not enable!");
        }
    }

    /**
     * ?
     *
     * @param ssid   ??
     * @param passwd ?
     *               ??? <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
     */
    @SuppressWarnings("unused")
    public static void startWifiAp(String ssid, String passwd) {
        if (mWifiManager == null)
            mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
        //wifi???wifi
        if (mWifiManager.isWifiEnabled())
            mWifiManager.setWifiEnabled(false);
        if (!isWifiApEnabled()) {
            Method method;
            try {
                //??0
                method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class,
                        boolean.class);
                WifiConfiguration netConfig = new WifiConfiguration();

                netConfig.SSID = ssid;
                netConfig.preSharedKey = passwd;

                netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                method.invoke(mWifiManager, netConfig, true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * WiFi
     */
    public static void closeWifiAp() {
        if (mWifiManager == null)
            mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
        if (isWifiApEnabled()) {
            try {
                Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");
                method.setAccessible(true);
                WifiConfiguration config = (WifiConfiguration) method.invoke(mWifiManager);
                Method method2 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class,
                        boolean.class);
                method2.invoke(mWifiManager, config, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * ?APN?
     * @return ?
     */
    private boolean getApnStatus() {
        Matcher matcher;
        String name;
        try {
            Enumeration<NetworkInterface> niList = NetworkInterface.getNetworkInterfaces();
            if (niList != null) {
                for (NetworkInterface intf : Collections.list(niList)) {
                    if (!intf.isUp() || intf.getInterfaceAddresses().size() == 0) {
                        continue;
                    }
                    Log.d(TAG, "isVpnUsed() NetworkInterface Name: " + intf.getName());
                    name = intf.getName();
                    matcher = Pattern.compile("tun\\d").matcher(name);
                    if (matcher.find()) {
                        System.out.println("vpn??????" + matcher.group());
                        return true;
                    }
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * ?SharedPreferenes
     * @param name ??
     * @return SharedPreferenes
     */
    public static SharedPreferences getMainSharedPreferences(String name) {
        return getContext().getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    public static void putBoolean(SharedPreferences preferences, String key, boolean value) {
        SharedPreferences.Editor edit = preferences.edit();
        edit.putBoolean(key, value).apply();
    }

    public static void putString(SharedPreferences preferences, String key, String value) {
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(key, value).apply();
    }

    /**
     * Application??ApplicationContext
     *
     * @see Application
     * {@link }
     */
    public static class ToolApplication extends Application {

        static Context[] mContexts = new Context[1];

        public static Context getContext() {
            return mContexts[0];
        }

        @Override
        public void onCreate() {
            super.onCreate();
            mContexts[0] = getApplicationContext();
        }

    }
}