com.ccxt.whl.utils.CommonUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.ccxt.whl.utils.CommonUtils.java

Source

/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ccxt.whl.utils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class CommonUtils {
    private static MyLogger Log = MyLogger.yLog();

    /**
     * ??
     * 
     * @param context
     * @return
     */
    public static boolean isNetWorkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }

        return false;
    }

    /**
     * Sdcard?
     * 
     * @return
     */
    public static boolean isExitsSdcard() {
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            return true;
        else
            return false;
    }

    public static String getTopActivity(Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

        if (runningTaskInfos != null)
            return runningTaskInfos.get(0).topActivity.getClassName();
        else
            return "";
    }

    /**
     * ??: null?0??Mapempty
     * 
     * @param obj
     * @return
     */
    public static boolean isNullOrEmpty(Object obj) {
        Log.d("obj--->" + obj);

        if (obj != null) {
            Log.d("--->not null");
        } else {
            return true;
        }

        if (obj.toString() != null) {//???
            Log.d("--->not null");
        } else {
            return true;
        }

        /*if (!isNullOrEmpty(obj.toString())){//???0904|??
           Log.d("isNullOrEmpty", "=not null");
        }else{
           return true;
        }*/

        if (obj instanceof CharSequence) {
            Log.d("--->CharSequence");
            return ((CharSequence) obj).length() == 0;
        }
        if (obj instanceof Collection) {
            Log.d("--->Collection");
            return ((Collection) obj).isEmpty();
        }

        if (obj instanceof Map) {
            Log.d("--->Map");
            return ((Map) obj).isEmpty();
        }

        if (obj instanceof Object[]) {
            Log.d("--->Object[]");
            Object[] object = (Object[]) obj;
            if (object.length == 0) {
                return true;
            }
            boolean empty = true;
            for (int i = 0; i < object.length; i++) {
                if (!isNullOrEmpty(object[i])) {
                    empty = false;
                    break;
                }
            }
            return empty;
        }

        if (obj instanceof JSONArray) {
            if (((JSONArray) obj).length() == 0) {
                return true;
            }
        }

        if (obj instanceof JSONObject) {
            if (((JSONObject) obj).length() == 0) {
                return true;
            }
        }

        return false;
    }

    /**
     * ??(?)?
     * 
     * @param long1
     *            ?
     * @param lat1
     *            
     * @param long2
     *            ?
     * @param lat2
     *            
     * @return ? ??
     */
    public static double Distance(double long1, double lat1, double long2, double lat2) {
        double a, b, R;
        R = 6378137; // ??
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (long1 - long2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
        return d;
    }

    /**
     * emoji  ?
     * @param source
     * @return
     */
    public static String filterEmoji(String source) {

        if (!containsEmoji(source)) {
            return source;//??
        }
        //??
        StringBuilder buf = null;

        int len = source.length();

        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);

            if (!isEmojiCharacter(codePoint)) {
                if (buf == null) {
                    buf = new StringBuilder(source.length());
                }

                buf.append(codePoint);
            } else {
            }
        }

        if (buf == null) {
            return "";
        } else {
            if (buf.length() == len) {//??toString??
                buf = null;
                return source;
            } else {
                return buf.toString();
            }
        }

    }

    private static boolean isEmojiCharacter(char codePoint) {
        return !((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
    }

    /**
     * ?emoji
     * @param source
     * @return ?
     */
    public static boolean containsEmoji(String source) {
        // if (StringUtils.isBlank(source)) { //?commons-lang-2.5.jar
        //     return false; 
        // }

        int len = source.length();

        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);

            if (isEmojiCharacter(codePoint)) {
                //do nothing
                return true;
            }
        }

        return false;
    }

}