com.yutong.axxc.parents.view.common.ActivityUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.yutong.axxc.parents.view.common.ActivityUtils.java

Source

/*******************************************************************************
 * @(#)ActivityUtils.java 2013-2-25
 *
 * Copyright 2013 Neusoft Group Ltd. All rights reserved.
 * Neusoft PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *******************************************************************************/

package com.yutong.axxc.parents.view.common;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.ColorStateList;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.SpannableStringBuilder;
import android.widget.Toast;

import com.yutong.axxc.parents.R;
import com.yutong.axxc.parents.common.GlobleConstants;
import com.yutong.axxc.parents.common.Logger;
import com.yutong.axxc.parents.common.context.StringUtil;

/**
 * @author <a href="mailto:wenxw@neusoft.com">sherly.wen </a>
 * @version $Revision 1.1 $ 2013-2-25 ?03:20:19
 */
public final class ActivityUtils {
    private static Context context;

    private ActivityUtils() {

    }

    public static Context getContext() {
        return context;
    }

    public static void setContext(Context context) {
        ActivityUtils.context = context;
    }

    /**
     * ?????
     * @param context
     */
    public static void stopAllThread(Context context) {
        ActivityManager.releaseAllResource();

    }

    public static String getIMEI(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getDeviceId();
    }

    public static String getSoftwareVersion(Context context) {
        String version = null;
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            version = pInfo.versionName + pInfo.versionCode;

        } catch (NameNotFoundException e) {
            Logger.e(ActivityUtils.class, "???", e);
        }
        return version;
    }

    /**
     * activity
     * @param from ?activity
     * @param to activity
     * @param extras ??
     */
    public static void changeActivity(Activity from, Class<?> to, Bundle extras) {
        // Intent intent = setIntent(from, to, extras);
        Intent intent = new Intent();
        if (extras != null)
            intent.putExtras(extras);
        intent.setClass(from.getBaseContext(), to);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        from.startActivity(intent);
        //from.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        from.overridePendingTransition(R.anim.enter_righttoleft, R.anim.exit_righttoleft);
    }

    /**
     * @param vMonitorBatchActivity TODO
     * @param from ?activity
     * @param to biactivity
     * @param vBeanList ??
     */
    public static void changeActivityWithData(Activity from, Class<?> to, Intent mIntent) {
        if (mIntent != null) {
            mIntent.setClass(from, to);
            from.startActivity(mIntent);
        }
    }

    /**
     * ?
     * @param context
     * @param msg
     */
    public static void toast(Context context, Object msg) {
        Toast.makeText(context, StringUtil.parseObj2Str(msg), Toast.LENGTH_SHORT).show();
    }

    /**
     * ????
     * @param activity
     * @param telNumber
     */
    public static void call(Activity activity, String telNumber) {
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telNumber));
        activity.startActivity(intent);
    }

    /**
     * ??
     * @return
     */
    public static ColorStateList createSelector(Context context, int pressedColor, int defaultColor) {
        int statePressed = android.R.attr.state_pressed;
        int stateFocesed = android.R.attr.state_focused;
        int[][] state = { { statePressed }, { -statePressed }, { stateFocesed }, { -stateFocesed } };
        int color1 = context.getResources().getColor(pressedColor);
        int color2 = context.getResources().getColor(defaultColor);
        int color3 = context.getResources().getColor(pressedColor);
        int color4 = context.getResources().getColor(defaultColor);
        int[] color = { color1, color2, color3, color4 };
        ColorStateList colorStateList = new ColorStateList(state, color);
        return colorStateList;
    }

    public static String subString(String str) {
        if (str != null) {
            if (str.length() >= 7)
                return str.substring(str.length() - 7);
            else
                return str;
        } else {
            return GlobleConstants.EMPTY_STR;
        }
    }

    /**
     * 
     * @param str
     * @return
     */
    public static SpannableStringBuilder str2Span(String str) {
        // str = qj2bj(str).replace(",", ", ");
        if (StringUtils.isBlank(str))
            return new SpannableStringBuilder();
        SpannableStringBuilder builder = new SpannableStringBuilder(str);
        // ??
        // char[] chars = str.toCharArray();
        //
        // for (char ch : chars) {
        // if ((ch >= '0' && ch <= '9') || ch == '.') {
        // SpannableString string = new SpannableString(StringUtil.parseStr(ch));
        // string.setSpan(new RelativeSizeSpan(1.0f), 0, 1, 0);
        // string.setSpan(new StyleSpan(Typeface.BOLD), 0, 1, 0);
        // builder.append(string);
        // continue;
        // }
        // builder.append(ch);
        // }
        return builder;
    }

    /**
     * ASCII??!???33(Decimal)
     */
    private static final char DBC_CHAR_START = 33; // ?!

    /**
     * ASCII??~????126(Decimal)
     */
    private static final char DBC_CHAR_END = 126; // ?~

    /**
     * ASCII?????65281
     */
    private static final char SBC_CHAR_START = 65281; // ?

    /**
     * ASCII?????65374
     */
    private static final char SBC_CHAR_END = 65374; // 

    /**
     * ASCII????
     */
    private static final int CONVERT_STEP = 65248; // ??

    /**
     * ?ASCII????
     */
    private static final char SBC_SPACE = 12288; //  12288

    /**
     * ?ASCII32(Decimal)
     */
    private static final char DBC_SPACE = ' '; // ?

    /**
     * <PRE>
     * ->??   
     * ???
     * </PRE>
     */
    public static String qj2bj(String src) {
        if (src == null) {
            return src;
        }
        StringBuilder buf = new StringBuilder(src.length());
        char[] ca = src.toCharArray();
        for (int i = 0; i < src.length(); i++) {
            if (ca[i] >= SBC_CHAR_START && ca[i] <= SBC_CHAR_END) { // ??
                buf.append((char) (ca[i] - CONVERT_STEP));
            } else if (ca[i] == SBC_SPACE) { // 
                buf.append(DBC_SPACE);
            } else { // ???
                buf.append(ca[i]);
            }
        }
        return buf.toString();
    }

    /**
     * <PRE>
     * ?->?   
     * ??!&tilde;
     * </PRE>
     */
    public static String bj2qj(String src) {
        if (src == null) {
            return src;
        }
        StringBuilder buf = new StringBuilder(src.length());
        char[] ca = src.toCharArray();
        for (int i = 0; i < ca.length; i++) {
            if (ca[i] == DBC_SPACE) { // ?
                buf.append(SBC_SPACE);
            } else if ((ca[i] >= DBC_CHAR_START) && (ca[i] <= DBC_CHAR_END)) { // !~??
                buf.append((char) (ca[i] + CONVERT_STEP));
            } else { // ??ascii????
                buf.append(ca[i]);
            }
        }
        return buf.toString();
    }

    /**
     * ?
     * @param QJstr
     * @return
     */
    public static final String QBchange(String QJstr) {
        String outStr = "";
        String Tstr = "";
        byte[] b = null;

        for (int i = 0; i < QJstr.length(); i++) {
            try {
                Tstr = QJstr.substring(i, i + 1);
                b = Tstr.getBytes("unicode");
            } catch (java.io.UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (b[3] == -1) {
                b[2] = (byte) (b[2] + 32);
                b[3] = 0;
                try {
                    outStr = outStr + new String(b, "unicode");
                } catch (java.io.UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            } else
                outStr = outStr + Tstr;
        }
        return outStr;
    }

    public static void clearCash(final Context acitvity, String server_org_id, String local_org_id) {
        // ID?ID?????
        Logger.i(acitvity.getClass(), "? ??ID", server_org_id,
                "?ID:", local_org_id);
        if (!server_org_id.equals(local_org_id)) {
            // TODO 
            // ?
            //AlertInfoDao alertInfoDao = new AlertInfoDao(acitvity);
            // alertInfoDao.delAlertInfos(YtApplication.getInstance().getUid());
            // 
            //PhotoDao photoDao = new PhotoDao(acitvity);
            // photoDao.delAllPhotoInfos(YtApplication.getInstance().getUid());
            // 
            // VehicleDao vehicleDao = new VehicleDao(acitvity);
            // vehicleDao.delVehicleByUid(YtApplication.getInstance().getUid());
            // 
            // NewsInfoDao newsInfoDao = new NewsInfoDao(loginAcitvity);
            // newsInfoDao.delNewsInfos(userInfoBean.getUsrId());
        }
    }
}