diff --git a/src/com/zftlive/android/MApplication.java b/src/com/zftlive/android/MApplication.java index d3e900f..32f6139 100644 --- a/src/com/zftlive/android/MApplication.java +++ b/src/com/zftlive/android/MApplication.java @@ -1,12 +1,71 @@ package com.zftlive.android; +import java.util.HashMap; +import java.util.Map; +import java.util.Stack; + +import android.app.Activity; import android.app.Application; +import android.content.Context; /** - * 整个应用程序Applicaiton - * @author 曾繁添 - * + * 鏁翠釜搴旂敤绋嬪簭Applicaiton + * + * @author 鏇剧箒娣 + * */ public class MApplication extends Application { + private static Context instance; + + public static Stack activitys = new Stack(); + + public static Context gainContext() { + return instance; + } + + public void onCreate() { + super.onCreate(); + instance = this; + } + + private static Map data = new HashMap(); + + public static void assignData(String strKey, Object strValue) { + if (data.size() > 5) { + throw new RuntimeException("瓒呰繃鍏佽鏈澶ф暟"); + } + data.put(strKey, strValue); + } + + public static Object gainData(String strKey) { + return data.get(strKey); + } + + public static void pushTask(Activity task) { + activitys.push(task); + } + + public static void removeTask(Activity task) { + activitys.remove(task); + } + + public static void removeTask(int taskIndex) { + if (activitys.size() > taskIndex) + activitys.remove(taskIndex); + } + + public static void removeToTop() { + int end = activitys.size(); + int start = 1; + for (int i = end - 1; i >= start; i--) { + activitys.get(i).finish(); + } + } + + public static void removeAll() { + for (Activity task : activitys) { + task.finish(); + } + } } diff --git a/src/com/zftlive/android/common/VersionChecker.java b/src/com/zftlive/android/common/VersionChecker.java new file mode 100644 index 0000000..1a96eeb --- /dev/null +++ b/src/com/zftlive/android/common/VersionChecker.java @@ -0,0 +1,5 @@ +package com.zftlive.android.common; + +public class VersionChecker { + +} diff --git a/src/com/zftlive/android/entity/Config.java b/src/com/zftlive/android/entity/Config.java new file mode 100644 index 0000000..a583538 --- /dev/null +++ b/src/com/zftlive/android/entity/Config.java @@ -0,0 +1,7 @@ +package com.zftlive.android.entity; + +import com.zftlive.android.base.BaseEntity; + +public class Config extends BaseEntity { + +} diff --git a/src/com/zftlive/android/exception/NoFreeMbException.java b/src/com/zftlive/android/exception/NoFreeMbException.java new file mode 100644 index 0000000..f842ccd --- /dev/null +++ b/src/com/zftlive/android/exception/NoFreeMbException.java @@ -0,0 +1,12 @@ +package com.zftlive.android.exception; + +import com.zftlive.android.base.BaseException; + +/** + * SD鍗″墿浣欑┖闂村紓甯 + * @author 鏇剧箒娣 + * + */ +public class NoFreeMbException extends BaseException { + +} diff --git a/src/com/zftlive/android/model/Node.java b/src/com/zftlive/android/model/Node.java new file mode 100644 index 0000000..ebe79fa --- /dev/null +++ b/src/com/zftlive/android/model/Node.java @@ -0,0 +1,258 @@ +package com.zftlive.android.model; + +import java.util.ArrayList; +import java.util.List; + +/** + * 鏍戣妭鐐筂odel + * @author 鏇剧箒娣 + * + */ +public class Node { + private String label;//鑺傜偣鏄剧ず鐨勬枃瀛 + private String value;//鑺傜偣鐨勫 + private int icon = -1;//鏄惁鏄剧ず灏忓浘鏍,-1琛ㄧず闅愯棌鍥炬爣 + private int tipCount = 0; //鏄剧ずtips鏁伴噺 + private String linkURL;//鎵撳紑杩炴帴URL + private Node parent;//鐖惰妭鐐 + private List children = new ArrayList();//瀛愯妭鐐 + private boolean isChecked = false;//鏄惁澶勪簬閫変腑鐘舵 + private boolean isExpanded = true;//鏄惁澶勪簬灞曞紑鐘舵 + private boolean hasCheckBox = true;//鏄惁鎷ユ湁澶嶉夋 + private boolean isShowTips = true; //鏄惁鏄剧ず鎻愰啋鏁扮洰 + + /** + * Node鏋勯犲嚱鏁 + * @param label 鑺傜偣鏄剧ず鐨勬枃瀛 + * @param value 鑺傜偣鐨勫 + */ + public Node(String label,String value){ + this.label = label; + this.value = value; + } + + /** + * 璁剧疆鐖惰妭鐐 + * @param node + */ + public void setParent(Node node){ + this.parent = node; + } + /** + * 鑾峰緱鐖惰妭鐐 + * @return + */ + public Node getParent(){ + return this.parent; + } + /** + * 璁剧疆鑺傜偣鏂囨湰 + * @param label + */ + public void setLabel(String label){ + this.label = label; + } + /** + * 鑾峰緱鑺傜偣鏂囨湰 + * @return + */ + public String getLabel(){ + return this.label; + } + /** + * 璁剧疆鑺傜偣鍊 + * @param value + */ + public void setValue(String value){ + this.value = value; + } + /** + * 鑾峰緱鑺傜偣鍊 + * @return + */ + public String getValue(){ + return this.value; + } + /** + * 璁剧疆鑺傜偣鍥炬爣鏂囦欢 + * @param icon + */ + public void setIcon(int icon){ + this.icon = icon; + } + /** + * 鑾峰緱鍥炬爣鏂囦欢 + * @return + */ + public int getIcon(){ + return icon; + } + /** + * 鑾峰緱鑺傜偣杩炴帴URL + * @return + */ + public String getLinkURL() { + return linkURL; + } + /** + * 璁剧疆鑺傜偣杩炴帴URL + * @param linkURL + */ + public void setLinkURL(String linkURL) { + this.linkURL = linkURL; + } + + /** + * 鑾峰緱鑺傜偣tip鏁伴噺 + * @return + */ + public int getTipCount() { + return tipCount; + } + + /** + * 璁剧疆鑺傜偣tip鏁伴噺 + * @param tip鏁伴噺 + */ + public void setTipCount(int tipCount) { + this.tipCount = tipCount; + } + + /** + * 璁剧疆鏄惁鏄剧ずtips + * @return + */ + public void setIsShowTips(boolean isShowTips){ + this.isShowTips = isShowTips; + } + + /** + * 鑾峰彇鏄惁鏄剧ずtips + * @return + */ + public boolean getIsShowTips(){ + return this.isShowTips; + } + + /** + * 鏄惁鏍硅妭鐐 + * @return + */ + public boolean isRoot(){ + return parent==null?true:false; + } + /** + * 鑾峰緱瀛愯妭鐐 + * @return + */ + public List getChildren(){ + return this.children; + } + /** + * 娣诲姞瀛愯妭鐐 + * @param node + */ + public void add(Node node){ + if(!children.contains(node)){ + children.add(node); + } + } + /** + * 娓呴櫎鎵鏈夊瓙鑺傜偣 + */ + public void clear(){ + children.clear(); + } + /** + * 鍒犻櫎涓涓瓙鑺傜偣 + * @param node + */ + public void remove(Node node){ + if(!children.contains(node)){ + children.remove(node); + } + } + /** + * 鍒犻櫎鎸囧畾浣嶇疆鐨勫瓙鑺傜偣 + * @param location + */ + public void remove(int location){ + children.remove(location); + } + /** + * 鑾峰緱鑺傜偣鐨勭骇鏁,鏍硅妭鐐逛负0 + * @return + */ + public int getLevel(){ + return parent==null?0:parent.getLevel()+1; + } + /** + * 璁剧疆鑺傜偣閫変腑鐘舵 + * @param isChecked + */ + public void setChecked(boolean isChecked){ + this.isChecked = isChecked; + } + /** + * 鑾峰緱鑺傜偣閫変腑鐘舵 + * @return + */ + public boolean isChecked(){ + return isChecked; + } + /** + * 璁剧疆鏄惁鎷ユ湁澶嶉夋 + * @param hasCheckBox + */ + public void setCheckBox(boolean hasCheckBox){ + this.hasCheckBox = hasCheckBox; + } + /** + * 鏄惁鎷ユ湁澶嶉夋 + * @return + */ + public boolean hasCheckBox(){ + return hasCheckBox; + } + + /** + * 鏄惁鍙惰妭鐐,鍗虫病鏈夊瓙鑺傜偣鐨勮妭鐐 + * @return + */ + public boolean isLeaf(){ + return children.size()<1?true:false; + } + /** + * 褰撳墠鑺傜偣鏄惁澶勪簬灞曞紑鐘舵 + * @return + */ + public boolean isExpanded(){ + return isExpanded; + } + /** + * 璁剧疆鑺傜偣灞曞紑鐘舵 + * @return + */ + public void setExpanded(boolean isExpanded){ + this.isExpanded = isExpanded; + } + /** + * 閫掑綊鍒ゆ柇鐖惰妭鐐规槸鍚﹀浜庢姌鍙犵姸鎬,鏈変竴涓埗鑺傜偣鎶樺彔鍒欒涓烘槸鎶樺彔鐘舵 + * @return + */ + public boolean isParentCollapsed(){ + if(parent==null)return !isExpanded; + if(!parent.isExpanded())return true; + return parent.isParentCollapsed(); + } + /** + * 閫掑綊鍒ゆ柇鎵缁欑殑鑺傜偣鏄惁褰撳墠鑺傜偣鐨勭埗鑺傜偣 + * @param node 鎵缁欒妭鐐 + * @return + */ + public boolean isParent(Node node){ + if(parent==null)return false; + if(node.equals(parent))return true; + return parent.isParent(node); + } +} diff --git a/src/com/zftlive/android/model/Option.java b/src/com/zftlive/android/model/Option.java new file mode 100644 index 0000000..090effb --- /dev/null +++ b/src/com/zftlive/android/model/Option.java @@ -0,0 +1,54 @@ +package com.zftlive.android.model; + +import java.io.Serializable; + +/** + * 涓嬫媺妗嗐佸崟閫夋銆佸閫夋閫夐」Bean + * @author 鏇剧箒娣 + * @version 1.0 + */ +public class Option implements Serializable,Cloneable{ + + private static final long serialVersionUID = -724868344947644938L; + + private String label = "璇烽夋嫨..."; + + private String value = ""; + + public Option() { + + } + + public Option(String value,String label) { + this.label = label; + this.value = value; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + protected Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + @Override + public String toString() { + return label; + } + + +} diff --git a/src/com/zftlive/android/model/SysEnv.java b/src/com/zftlive/android/model/SysEnv.java new file mode 100644 index 0000000..c1aa02d --- /dev/null +++ b/src/com/zftlive/android/model/SysEnv.java @@ -0,0 +1,154 @@ +package com.zftlive.android.model; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.List; + +import android.content.Context; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.hardware.Camera; +import android.net.wifi.WifiInfo; +import android.net.wifi.WifiManager; +import android.os.Build; +import android.os.SystemClock; +import android.telephony.TelephonyManager; +import android.util.DisplayMetrics; +import android.util.Log; +import android.view.WindowManager; + +import com.zftlive.android.MApplication; + +/** + * 杩愯鐜淇℃伅 + * @author 鏇剧箒娣 + * @version 1.0 + */ +public final class SysEnv { + + /***Log杈撳嚭鏍囪瘑**/ + private static final String TAG = SysEnv.class.getSimpleName(); + + /***灞忓箷鏄剧ず鏉愯川**/ + private static final DisplayMetrics mDisplayMetrics = new DisplayMetrics(); + + /**涓婁笅鏂**/ + private static final Context context = MApplication.gainContext(); + + /**鎿嶄綔绯荤粺鍚嶇О(GT-I9100G)***/ + public static final String MODEL_NUMBER = Build.MODEL; + + /**鎿嶄綔绯荤粺鍚嶇О(I9100G)***/ + public static final String DISPLAY_NAME = Build.DISPLAY; + + /**鎿嶄綔绯荤粺鐗堟湰(4.4)***/ + public static final String OS_VERSION = Build.VERSION.RELEASE;; + + /**搴旂敤绋嬪簭鐗堟湰***/ + public static final String APP_VERSION = getVersion(); + + /***灞忓箷瀹藉害**/ + public static final int SCREEN_WIDTH = getDisplayMetrics().widthPixels; + + /***灞忓箷楂樺害**/ + public static final int SCREEN_HEIGHT = getDisplayMetrics().heightPixels; + + /***鏈満鎵嬫満鍙风爜**/ + public static final String PHONE_NUMBER = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number(); + + /***璁惧ID**/ + public static final String DEVICE_ID = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId(); + + /***璁惧IMEI鍙风爜**/ + public static final String IMEI = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getSimSerialNumber(); + + /***璁惧IMSI鍙风爜**/ + public static final String IMSI = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getSubscriberId(); + + /**鑾峰彇绯荤粺鏄剧ず鏉愯川***/ + public static DisplayMetrics getDisplayMetrics(){ + WindowManager windowMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); + windowMgr.getDefaultDisplay().getMetrics(mDisplayMetrics); + return mDisplayMetrics; + } + + /**鑾峰彇鎽勫儚澶存敮鎸佺殑鍒嗚鲸鐜***/ + public static List getSupportedPreviewSizes(Camera camera){ + Camera.Parameters parameters = camera.getParameters(); + List sizeList = parameters.getSupportedPreviewSizes(); + return sizeList; + } + + /** + * 鑾峰彇搴旂敤绋嬪簭鐗堟湰锛坴ersionName锛 + * @return 褰撳墠搴旂敤鐨勭増鏈彿 + */ + public static String getVersion() { + PackageManager manager = context.getPackageManager(); + PackageInfo info = null; + try { + info = manager.getPackageInfo(context.getPackageName(), 0); + } catch (NameNotFoundException e) { + Log.e(TAG, "鑾峰彇搴旂敤绋嬪簭鐗堟湰澶辫触锛屽師鍥狅細"+e.getMessage()); + return ""; + } + + return info.versionName; + } + + /** + * 鑾峰彇绯荤粺鍐呮牳鐗堟湰 + * @return + */ + public static String getKernelVersion(){ + String strVersion= ""; + FileReader mFileReader = null; + BufferedReader mBufferedReader = null; + try { + mFileReader = new FileReader("/proc/version"); + mBufferedReader = new BufferedReader(mFileReader, 8192); + String str2 = mBufferedReader.readLine(); + strVersion = str2.split("\\s+")[2];//KernelVersion + + } catch (Exception e) { + Log.e(TAG, "鑾峰彇绯荤粺鍐呮牳鐗堟湰澶辫触锛屽師鍥狅細"+e.getMessage()); + }finally{ + try { + mBufferedReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return strVersion; + } + + + /*** + * 鑾峰彇MAC鍦板潃 + * @return + */ + public static String getMacAddress(){ + WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + WifiInfo wifiInfo = wifiManager.getConnectionInfo(); + if(wifiInfo.getMacAddress()!=null){ + return wifiInfo.getMacAddress(); + } else { + return ""; + } + } + + /** + * 鑾峰彇杩愯鏃堕棿 + * @return 杩愯鏃堕棿(鍗曚綅/s) + */ + public static long getRunTimes() { + long ut = SystemClock.elapsedRealtime() / 1000; + if (ut == 0) { + ut = 1; + } + return ut; + } +} diff --git a/src/com/zftlive/android/service/NetworkStateService.java b/src/com/zftlive/android/service/NetworkStateService.java new file mode 100644 index 0000000..e59032f --- /dev/null +++ b/src/com/zftlive/android/service/NetworkStateService.java @@ -0,0 +1,15 @@ +package com.zftlive.android.service; + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; + +public class NetworkStateService extends Service { + + @Override + public IBinder onBind(Intent intent) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/com/zftlive/android/tools/ToolNetwork.java b/src/com/zftlive/android/tools/ToolNetwork.java new file mode 100644 index 0000000..2ea77d8 --- /dev/null +++ b/src/com/zftlive/android/tools/ToolNetwork.java @@ -0,0 +1,142 @@ +package com.zftlive.android.tools; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.AlertDialog.Builder; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.provider.Settings; +import android.util.Log; + +/** + * 鍩轰簬闈欐佸唴閮ㄧ被瀹炵幇鐨勫崟渚嬶紝淇濊瘉绾跨▼瀹夊叏鐨勭綉缁滀俊鎭伐鍏风被 浣跨敤璇ュ伐鍏风被涔嬪墠锛岃寰楀湪AndroidManifest.xml娣诲姞鏉冮檺璁稿彲 + * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> + * + * + * 瀹夊崜鍒ゆ柇缃戠粶鐘舵侊紝鍙渶瑕佸湪鐩稿簲鐨凙ctivity鐨勭浉鍏虫柟娉曪紙onCreat/onResum锛夎皟鐢ㄤ竴琛屼唬鐮佸嵆鍙 + * NetWorkUtils.getInstance(getActivity()).validateNetWork(); + * + * + * + * @author 鏇剧箒娣 + * @version 1.0 + */ +public class ToolNetwork { + + public final static String NETWORK_CMNET = "CMNET"; + public final static String NETWORK_CMWAP = "CMWAP"; + public final static String NETWORK_WIFI = "WIFI"; + public final static String TAG = "NetWorkUtils"; + private static NetworkInfo networkInfo = null; + private Context mContext = null; + + private ToolNetwork() { + } + + public static ToolNetwork getInstance() { + return SingletonHolder.instance; + } + + public ToolNetwork init(Context context){ + this.mContext = context; + return this; + } + + /** + * 鍒ゆ柇缃戠粶鏄惁鍙敤 + * + * @return 鏄/鍚 + */ + public boolean isAvailable() { + ConnectivityManager manager = (ConnectivityManager) mContext + .getApplicationContext().getSystemService( + Context.CONNECTIVITY_SERVICE); + if (null == manager) { + return false; + } + networkInfo = manager.getActiveNetworkInfo(); + if (null == networkInfo || !networkInfo.isAvailable()) { + return false; + } + return true; + } + + /** + * 鍒ゆ柇缃戠粶鏄惁宸茶繛鎺 + * + * @return 鏄/鍚 + */ + public boolean isConnected() { + if (!isAvailable()) { + return false; + } + if (!networkInfo.isConnected()) { + return false; + } + return true; + } + + /** + * 妫鏌ュ綋鍓嶇幆澧冪綉缁滄槸鍚﹀彲鐢紝涓嶅彲鐢ㄨ烦杞嚦寮鍚綉缁滅晫闈,涓嶈缃綉缁滃己鍒跺叧闂綋鍓岮ctivity + */ + public void validateNetWork() { + + if (!isConnected()) { + Builder dialogBuilder = new AlertDialog.Builder(mContext); + dialogBuilder.setTitle("缃戠粶璁剧疆"); + dialogBuilder.setMessage("缃戠粶涓嶅彲鐢紝鏄惁鐜板湪璁剧疆缃戠粶锛"); + dialogBuilder.setPositiveButton(android.R.string.ok, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + ((Activity) mContext).startActivityForResult( + new Intent( + Settings.ACTION_SETTINGS), + which); + } + }); + dialogBuilder.setNegativeButton(android.R.string.cancel, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + dialogBuilder.create(); + dialogBuilder.show(); + } + } + + /** + * 鑾峰彇缃戠粶杩炴帴淇℃伅
鏃犵綉缁滐細
WIFI缃戠粶锛歐IFI
WAP缃戠粶锛欳MWAP
+ * NET缃戠粶锛欳MNET
+ * + * @return + */ + public String getNetworkType() { + if (isConnected()) { + int type = networkInfo.getType(); + if (ConnectivityManager.TYPE_MOBILE == type) { + Log.i(TAG, + "networkInfo.getExtraInfo()-->" + + networkInfo.getExtraInfo()); + if (NETWORK_CMWAP.equals(networkInfo.getExtraInfo() + .toLowerCase())) { + return NETWORK_CMWAP; + } else { + return NETWORK_CMNET; + } + } else if (ConnectivityManager.TYPE_WIFI == type) { + return NETWORK_WIFI; + } + } + + return ""; + } + + private static class SingletonHolder { + + private static ToolNetwork instance = new ToolNetwork(); + } +} diff --git a/src/com/zftlive/android/tools/ToolPicture.java b/src/com/zftlive/android/tools/ToolPicture.java new file mode 100644 index 0000000..e6114fa --- /dev/null +++ b/src/com/zftlive/android/tools/ToolPicture.java @@ -0,0 +1,467 @@ +package com.zftlive.android.tools; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Hashtable; +import java.util.Random; + +import android.app.Activity; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Bitmap.Config; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.ColorMatrix; +import android.graphics.ColorMatrixColorFilter; +import android.graphics.Matrix; +import android.graphics.Paint; +import android.graphics.PixelFormat; +import android.graphics.PorterDuff.Mode; +import android.graphics.PorterDuffXfermode; +import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.NinePatchDrawable; +import android.media.ExifInterface; +import android.view.View; + +import com.google.zxing.BarcodeFormat; +import com.google.zxing.EncodeHintType; +import com.google.zxing.WriterException; +import com.google.zxing.common.BitMatrix; +import com.google.zxing.qrcode.QRCodeWriter; +import com.zftlive.android.model.SysEnv; + +/** + * 鍥剧墖宸ュ叿绫 + * @author 鏇剧箒娣 + * @version 1.0 + */ +public class ToolPicture { + + /** + * 鎴彇搴旂敤绋嬪簭鐣岄潰锛堝幓闄ょ姸鎬佹爮锛 + * @param activity 鐣岄潰Activity + * @return Bitmap瀵硅薄 + */ + public static Bitmap takeScreenShot(Activity activity){ + View view =activity.getWindow().getDecorView(); + view.setDrawingCacheEnabled(true); + view.buildDrawingCache(); + Bitmap bitmap = view.getDrawingCache(); + Rect rect = new Rect(); + activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); + int statusBarHeight = rect.top; + + Bitmap bitmap2 = Bitmap.createBitmap(bitmap,0,statusBarHeight, SysEnv.SCREEN_WIDTH, SysEnv.SCREEN_HEIGHT - statusBarHeight); + view.destroyDrawingCache(); + return bitmap2; + } + + /** + * 鎴彇搴旂敤绋嬪簭鐣岄潰 + * @param activity 鐣岄潰Activity + * @return Bitmap瀵硅薄 + */ + public static Bitmap takeFullScreenShot(Activity activity){ + + activity.getWindow().getDecorView().setDrawingCacheEnabled(true); + + Bitmap bmp = activity.getWindow().getDecorView().getDrawingCache(); + + + View view = activity.getWindow().getDecorView(); + + Bitmap bmp2 = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888); + + //view.draw(new Canvas(bmp2)); + + //bmp灏辨槸鎴彇鐨勫浘鐗囦簡锛屽彲閫氳繃bmp.compress(CompressFormat.PNG, 100, new FileOutputStream(file));鎶婂浘鐗囦繚瀛樹负鏂囦欢銆 + + //1銆佸緱鍒扮姸鎬佹爮楂樺害 + Rect rect = new Rect(); + view.getWindowVisibleDisplayFrame(rect); + int statusBarHeight = rect.top; + System.out.println("鐘舵佹爮楂樺害锛" + statusBarHeight); + + //2銆佸緱鍒版爣棰樻爮楂樺害 + int wintop = activity.getWindow().findViewById(android.R.id.content).getTop(); + int titleBarHeight = wintop - statusBarHeight; + System.out.println("鏍囬鏍忛珮搴:" + titleBarHeight); + +// //鎶婁袱涓猙itmap鍚堝埌涓璧 +// Bitmap bmpall=Biatmap.createBitmap(width,height,Config.ARGB_8888); +// Canvas canvas=new Canvas(bmpall); +// canvas.drawBitmap(bmp1,x,y,paint); +// canvas.drawBitmap(bmp2,x,y,paint); + + return bmp; + } + + /** + * 鏍规嵁鎸囧畾鍐呭鐢熸垚鑷畾涔夊楂樼殑浜岀淮鐮佸浘鐗 + * @param content 闇瑕佺敓鎴愪簩缁寸爜鐨勫唴瀹 + * @param width 浜岀淮鐮佸搴 + * @param height 浜岀淮鐮侀珮搴 + * @throws WriterException 鐢熸垚浜岀淮鐮佸紓甯 + */ + public static Bitmap makeQRImage(String content, int width, int height) + throws WriterException { + // 鍒ゆ柇URL鍚堟硶鎬 + if (ToolString.isNoBlankAndNoNull(content)) + return null; + + Hashtable hints = new Hashtable(); + hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); + // 鍥惧儚鏁版嵁杞崲锛屼娇鐢ㄤ簡鐭╅樀杞崲 + BitMatrix bitMatrix = new QRCodeWriter().encode(content, + BarcodeFormat.QR_CODE, width, height, hints); + int[] pixels = new int[width * height]; + // 鎸夌収浜岀淮鐮佺殑绠楁硶锛岄愪釜鐢熸垚浜岀淮鐮佺殑鍥剧墖锛屼袱涓猣or寰幆鏄浘鐗囨í鍒楁壂鎻忕殑缁撴灉 + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + if (bitMatrix.get(x, y)) + pixels[y * width + x] = 0xff000000; + else + pixels[y * width + x] = 0xffffffff; + } + } + // 鐢熸垚浜岀淮鐮佸浘鐗囩殑鏍煎紡锛屼娇鐢ˋRGB_8888 + Bitmap bitmap = Bitmap.createBitmap(width, height, + Bitmap.Config.ARGB_8888); + bitmap.setPixels(pixels, 0, width, 0, 0, width, height); + + return bitmap; + } + + /** + * 璇诲彇鍥剧墖灞炴э細鏃嬭浆鐨勮搴 + * + * @param path 鍥剧墖缁濆璺緞 + * @return degree 鏃嬭浆鐨勮搴 + * @throws IOException + */ + public static int gainPictureDegree(String path) throws Exception { + int degree = 0; + try { + ExifInterface exifInterface = new ExifInterface(path); + int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL); + switch (orientation) { + case ExifInterface.ORIENTATION_ROTATE_90: + degree = 90; + break; + case ExifInterface.ORIENTATION_ROTATE_180: + degree = 180; + break; + case ExifInterface.ORIENTATION_ROTATE_270: + degree = 270; + break; + } + } catch (Exception e) { + throw(e); + } + + return degree; + } + + /** + * 鏃嬭浆鍥剧墖 + * @param angle 瑙掑害 + * @param bitmap 婧恇itmap + * @return Bitmap 鏃嬭浆瑙掑害涔嬪悗鐨刡itmap + */ + public static Bitmap rotaingBitmap(int angle,Bitmap bitmap) { + //鏃嬭浆鍥剧墖 鍔ㄤ綔 + Matrix matrix = new Matrix();; + matrix.postRotate(angle); + //閲嶆柊鏋勫缓Bitmap + Bitmap resizedBitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix, true); + return resizedBitmap; + } + + /** + * Drawable杞垚Bitmap + * @param drawable + * @return + */ + public static Bitmap drawableToBitmap(Drawable drawable) { + if (drawable instanceof BitmapDrawable) { + return ((BitmapDrawable) drawable).getBitmap(); + } else if (drawable instanceof NinePatchDrawable) { + Bitmap bitmap = Bitmap + .createBitmap( + drawable.getIntrinsicWidth(), + drawable.getIntrinsicHeight(), + drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 + : Bitmap.Config.RGB_565); + Canvas canvas = new Canvas(bitmap); + drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), + drawable.getIntrinsicHeight()); + drawable.draw(canvas); + return bitmap; + } else { + return null; + } + } + + /** + * 浠庤祫婧愭枃浠朵腑鑾峰彇鍥剧墖 + * @param context 涓婁笅鏂 + * @param drawableId 璧勬簮鏂囦欢id + * @return + */ + public static Bitmap gainBitmap(Context context,int drawableId){ + Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), drawableId); + return bmp; + } + + /** + * 鐏扮櫧鍥剧墖锛堝幓鑹诧級 + * @param bitmap 闇瑕佺伆搴︾殑鍥剧墖 + * @return 鍘昏壊涔嬪悗鐨勫浘鐗 + */ + public static Bitmap toBlack(Bitmap bitmap) { + Bitmap resultBMP = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), + Bitmap.Config.RGB_565); + Canvas c = new Canvas(resultBMP); + Paint paint = new Paint(); + ColorMatrix cm = new ColorMatrix(); + cm.setSaturation(0); + ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); + paint.setColorFilter(f); + c.drawBitmap(bitmap, 0, 0, paint); + return resultBMP; + } + + /** + * 灏哹itmap杞垚 byte鏁扮粍 + * + * @param bitmap + * @return + */ + public static byte[] toBtyeArray(Bitmap bitmap) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); + return baos.toByteArray(); + } + + /** + * 灏哹yte鏁扮粍杞垚 bitmap + * + * @param b + * @return + */ + public static Bitmap bytesToBimap(byte[] b) { + if (b.length != 0) { + return BitmapFactory.decodeByteArray(b, 0, b.length); + } else { + return null; + } + } + + /** + * 灏咮itmap杞崲鎴愭寚瀹氬ぇ灏 + * + * @param bitmap 闇瑕佹敼鍙樺ぇ灏忕殑鍥剧墖 + * @param width 瀹 + * @param height 楂 + * @return + */ + public static Bitmap createBitmapBySize(Bitmap bitmap, int width, int height) { + return Bitmap.createScaledBitmap(bitmap, width, height, true); + } + + + /** + * 鍦ㄥ浘鐗囧彸涓嬭娣诲姞姘村嵃 + * @param srcBMP 鍘熷浘 + * @param markBMP 姘村嵃鍥剧墖 + * @return 鍚堟垚姘村嵃鍚庣殑鍥剧墖 + */ + public static Bitmap composeWatermark(Bitmap srcBMP, Bitmap markBMP) { + if (srcBMP == null) { + return null; + } + + // 鍒涘缓涓涓柊鐨勫拰SRC闀垮害瀹藉害涓鏍风殑浣嶅浘 + Bitmap newb = Bitmap.createBitmap(srcBMP.getWidth(), srcBMP.getHeight(), Config.ARGB_8888); + Canvas cv = new Canvas(newb); + // 鍦 0锛0鍧愭爣寮濮嬬敾鍏ュ師鍥 + cv.drawBitmap(srcBMP, 0, 0, null); + // 鍦ㄥ師鍥剧殑鍙充笅瑙掔敾鍏ユ按鍗 + cv.drawBitmap(markBMP, srcBMP.getWidth() - markBMP.getWidth() + 5, srcBMP.getHeight() - markBMP.getHeight() + 5, null); + // 淇濆瓨 + cv.save(Canvas.ALL_SAVE_FLAG); + // 瀛樺偍 + cv.restore(); + + return newb; + } + + /** + * 灏嗗浘鐗囪浆鎴愭寚瀹氬姬搴︼紙瑙掑害锛夌殑鍥剧墖 + * + * @param bitmap 闇瑕佷慨鏀圭殑鍥剧墖 + * @param pixels 鍦嗚鐨勫姬搴 + * @return 鍦嗚鍥剧墖 + */ + public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { + Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); + //鏍规嵁鍥剧墖鍒涘缓鐢诲竷 + Canvas canvas = new Canvas(output); + final Paint paint = new Paint(); + final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); + final RectF rectF = new RectF(rect); + final float roundPx = pixels; + paint.setAntiAlias(true); + canvas.drawARGB(0, 0, 0, 0); + paint.setColor(0xff424242); + canvas.drawRoundRect(rectF, roundPx, roundPx, paint); + paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); + canvas.drawBitmap(bitmap, rect, rect, paint); + return output; + } + + /** + * 缂╂斁鍥剧墖 + * + * @param bmp 闇瑕佺缉鏀剧殑鍥剧墖婧 + * @param newW 闇瑕佺缉鏀炬垚鐨勫浘鐗囧搴 + * @param newH 闇瑕佺缉鏀炬垚鐨勫浘鐗囬珮搴 + * @return 缂╂斁鍚庣殑鍥剧墖 + */ + public static Bitmap zoom(Bitmap bmp, int newW, int newH) { + + // 鑾峰緱鍥剧墖鐨勫楂 + int width = bmp.getWidth(); + int height = bmp.getHeight(); + + // 璁$畻缂╂斁姣斾緥 + float scaleWidth = ((float) newW) / width; + float scaleHeight = ((float) newH) / height; + + // 鍙栧緱鎯宠缂╂斁鐨刴atrix鍙傛暟 + Matrix matrix = new Matrix(); + matrix.postScale(scaleWidth, scaleHeight); + + // 寰楀埌鏂扮殑鍥剧墖 + Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix,true); + + return newbm; + } + + /** + * 鑾峰彇楠岃瘉鐮佸浘鐗 + * @param width 楠岃瘉鐮佸搴 + * @param height 楠岃瘉鐮侀珮搴 + * @return 楠岃瘉鐮丅itmap瀵硅薄 + */ + public synchronized static Bitmap makeValidateCode(int width, int height){ + return ValidateCodeGenerator.createBitmap(width, height); + } + + /** + * 鑾峰彇楠岃瘉鐮佸 + * @return 楠岃瘉鐮佸瓧绗︿覆 + */ + public synchronized static String gainValidateCodeValue(){ + return ValidateCodeGenerator.getCode(); + } + + /** + * 闅忔満鐢熸垚楠岃瘉鐮佸唴閮ㄧ被 + * + */ + final static class ValidateCodeGenerator{ + private static final char[] CHARS = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' + }; + + //default settings + private static final int DEFAULT_CODE_LENGTH = 4; + private static final int DEFAULT_FONT_SIZE = 20; + private static final int DEFAULT_LINE_NUMBER = 3; + private static final int BASE_PADDING_LEFT = 5, RANGE_PADDING_LEFT = 10, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 10; + private static final int DEFAULT_WIDTH = 60, DEFAULT_HEIGHT = 30; + + //variables + private static String value; + private static int padding_left, padding_top; + private static Random random = new Random(); + + public static Bitmap createBitmap(int width,int height) { + padding_left = 0; + //鍒涘缓鐢诲竷 + Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888); + Canvas c = new Canvas(bp); + + //闅忔満鐢熸垚楠岃瘉鐮佸瓧绗 + StringBuilder buffer = new StringBuilder(); + for (int i = 0; i < DEFAULT_CODE_LENGTH; i++) { + buffer.append(CHARS[random.nextInt(CHARS.length)]); + } + value = buffer.toString(); + + //璁剧疆棰滆壊 + c.drawColor(Color.WHITE); + + //璁剧疆鐢荤瑪澶у皬 + Paint paint = new Paint(); + paint.setTextSize(DEFAULT_FONT_SIZE); + for (int i = 0; i < value.length(); i++) { + //闅忔満鏍峰紡 + randomTextStyle(paint); + padding_left += BASE_PADDING_LEFT + random.nextInt(RANGE_PADDING_LEFT); + padding_top = BASE_PADDING_TOP + random.nextInt(RANGE_PADDING_TOP); + c.drawText(value.charAt(i) + "", padding_left, padding_top, paint); + } + for (int i = 0; i < DEFAULT_LINE_NUMBER; i++) { + drawLine(c, paint); + } + //淇濆瓨 + c.save(Canvas.ALL_SAVE_FLAG); + c.restore(); + + return bp; + } + + public static String getCode() { + return value; + } + + private static void randomTextStyle(Paint paint) { + int color = randomColor(1); + paint.setColor(color); + paint.setFakeBoldText(random.nextBoolean());//true涓虹矖浣擄紝false涓洪潪绮椾綋 + float skewX = random.nextInt(11) / 10; + skewX = random.nextBoolean() ? skewX : -skewX; + paint.setTextSkewX(skewX); //float绫诲瀷鍙傛暟锛岃礋鏁拌〃绀哄彸鏂滐紝鏁存暟宸︽枩 + paint.setUnderlineText(true); //true涓轰笅鍒掔嚎锛宖alse涓洪潪涓嬪垝绾 + paint.setStrikeThruText(true); //true涓哄垹闄ょ嚎锛宖alse涓洪潪鍒犻櫎绾 + } + + private static void drawLine(Canvas canvas, Paint paint) { + int color = randomColor(1); + int startX = random.nextInt(DEFAULT_WIDTH); + int startY = random.nextInt(DEFAULT_HEIGHT); + int stopX = random.nextInt(DEFAULT_WIDTH); + int stopY = random.nextInt(DEFAULT_HEIGHT); + paint.setStrokeWidth(1); + paint.setColor(color); + canvas.drawLine(startX, startY, stopX, stopY, paint); + } + + private static int randomColor(int rate) { + int red = random.nextInt(256) / rate; + int green = random.nextInt(256) / rate; + int blue = random.nextInt(256) / rate; + return Color.rgb(red, green, blue); + } + } +} diff --git a/src/com/zftlive/android/tools/ToolProperties.java b/src/com/zftlive/android/tools/ToolProperties.java new file mode 100644 index 0000000..2681748 --- /dev/null +++ b/src/com/zftlive/android/tools/ToolProperties.java @@ -0,0 +1,72 @@ +package com.zftlive.android.tools; + +import java.io.InputStream; +import java.util.Properties; + +import android.content.Context; + +import com.zftlive.android.MApplication; + +/** + * 閰嶇疆鏂囦欢宸ュ叿绫 + * + * @author 鏇剧箒娣 + * @version 1.0 + * + */ +public final class ToolProperties extends Properties { + + private static Properties property = new Properties(); + + public static String readAssetsProp(String fileName, String key) { + String value = ""; + try { + InputStream in = MApplication.gainContext().getAssets().open(fileName); + property.load(in); + value = property.getProperty(key); + } catch (Exception e1) { + e1.printStackTrace(); + } + + return value; + } + + public static String readAssetsProp(Context context,String fileName, String key) { + String value = ""; + try { + InputStream in = context.getAssets().open(fileName); + property.load(in); + value = property.getProperty(key); + } catch (Exception e1) { + e1.printStackTrace(); + } + + return value; + } + + public static String readAssetsProp(String fileName, String key,String defaultValue) { + String value = ""; + try { + InputStream in = MApplication.gainContext().getAssets().open(fileName); + property.load(in); + value = property.getProperty(key, defaultValue); + } catch (Exception e1) { + e1.printStackTrace(); + } + + return value; + } + + public static String readAssetsProp(Context context,String fileName, String key,String defaultValue) { + String value = ""; + try { + InputStream in = context.getAssets().open(fileName); + property.load(in); + value = property.getProperty(key, defaultValue); + } catch (Exception e1) { + e1.printStackTrace(); + } + + return value; + } +} diff --git a/src/com/zftlive/android/tools/ToolResource.java b/src/com/zftlive/android/tools/ToolResource.java new file mode 100644 index 0000000..b1c460e --- /dev/null +++ b/src/com/zftlive/android/tools/ToolResource.java @@ -0,0 +1,94 @@ +package com.zftlive.android.tools; + +import java.lang.reflect.Field; + +import android.content.Context; +import android.util.Log; + +import com.zftlive.android.MApplication; + +/** + * 鑾峰彇璧勬簮宸ュ叿绫 + * @author 鏇剧箒娣 + * @version 1.0 + */ +public class ToolResource { + private static final String TAG = ToolResource.class.getName(); + + private static Context mContext = MApplication.gainContext(); + + private static Class CDrawable = null; + + private static Class CLayout = null; + + private static Class CId = null; + + private static Class CAnim = null; + + private static Class CStyle = null; + + private static Class CString = null; + + private static Class CArray = null; + + static{ + try{ + CDrawable = Class.forName(mContext.getPackageName() + ".R$drawable"); + CLayout = Class.forName(mContext.getPackageName() + ".R$layout"); + CId = Class.forName(mContext.getPackageName() + ".R$id"); + CAnim = Class.forName(mContext.getPackageName() + ".R$anim"); + CStyle = Class.forName(mContext.getPackageName() + ".R$style"); + CString = Class.forName(mContext.getPackageName() + ".R$string"); + CArray = Class.forName(mContext.getPackageName() + ".R$array"); + + }catch(ClassNotFoundException e){ + Log.i(TAG,e.getMessage()); + } + } + + public static int getDrawableId(String resName){ + return getResId(CDrawable,resName); + } + + public static int getLayoutId(String resName){ + return getResId(CLayout,resName); + } + + public static int getIdId(String resName){ + return getResId(CId,resName); + } + + public static int getAnimId(String resName){ + return getResId(CAnim,resName); + } + + public static int getStyleId(String resName){ + return getResId(CStyle,resName); + } + + public static int getStringId(String resName){ + return getResId(CString,resName); + } + + public static int getArrayId(String resName){ + return getResId(CArray,resName); + } + + private static int getResId(Class resClass,String resName){ + if(resClass == null){ + Log.i(TAG,"getRes(null," + resName + ")"); + throw new IllegalArgumentException("ResClass is not initialized. Please make sure you have added neccessary resources. Also make sure you have " + mContext.getPackageName() + ".R$* configured in obfuscation. field=" + resName); + } + + try { + Field field = resClass.getField(resName); + return field.getInt(resName); + } catch (Exception e) { + Log.i(TAG, "getRes(" + resClass.getName() + ", " + resName + ")"); + Log.i(TAG, "Error getting resource. Make sure you have copied all resources (res/) from SDK to your project. "); + Log.i(TAG, e.getMessage()); + } + + return -1; + } +} diff --git a/src/com/zftlive/android/tools/ToolSMS.java b/src/com/zftlive/android/tools/ToolSMS.java new file mode 100644 index 0000000..3663e88 --- /dev/null +++ b/src/com/zftlive/android/tools/ToolSMS.java @@ -0,0 +1,105 @@ +package com.zftlive.android.tools; + +import android.content.Context; +import android.os.Handler; +import android.os.Message; +import cn.smssdk.EventHandler; +import cn.smssdk.SMSSDK; + +import com.zftlive.android.MApplication; + + +/** + * 鍙戦佺煭淇¢獙璇佺爜宸ュ叿绫 + * @author 鏇剧箒娣 + * @version 1.0 + * + */ +public class ToolSMS { + + public static String APPKEY = "25c13dc2e1c4"; + public static String APPSECRET = "14340f710d155024867d4870786d4c10"; + public static String CHINA = "86"; + private static IValidateSMSCode mIValidateSMSCode; + private static Handler mSMSHandle = new MySMSHandler(); + private static Context context = MApplication.gainContext(); + + public static void initSDK(String appkey, String appSecrect){ + // 鍒濆鍖栫煭淇DK + SMSSDK.initSDK(context, appkey, appSecrect); + //娉ㄥ唽鍥炶皟鐩戝惉鎺ュ彛 + SMSSDK.registerEventHandler(new EventHandler() { + public void afterEvent(int event, int result, Object data) { + Message msg = new Message(); + msg.arg1 = event; + msg.arg2 = result; + msg.obj = data; + mSMSHandle.sendMessage(msg); + } + }); + } + + /** + * 璇锋眰鑾峰彇鐭俊楠岃瘉鐮 + * @param phone 鎵嬫満鍙 + */ + public static void getVerificationCode(String phone){ + SMSSDK.getVerificationCode(CHINA, phone); + } + + /** + * 鎻愪氦鐭俊楠岃瘉鐮侊紝鏍¢獙鏄惁姝g‘ + * @param phone 鎵嬫満鍙 + * @param validateCode 鎵嬫満鐭俊楠岃瘉鐮 + */ + public static void submitVerificationCode(String phone, String validateCode,IValidateSMSCode callback){ + mIValidateSMSCode = callback; + SMSSDK.submitVerificationCode(CHINA, phone, validateCode); + } + + /** + * 閲婃斁璧勬簮 + */ + public static void release(){ + // 閿姣佸洖璋冪洃鍚帴鍙 + SMSSDK.unregisterAllEventHandler(); + } + + /** + * 娑堟伅澶勭悊Handle + */ + private static class MySMSHandler extends Handler{ + @Override + public void handleMessage(Message msg) { + super.handleMessage(msg); + + int event = msg.arg1; + int result = msg.arg2; + Object data = msg.obj; + + if (result == SMSSDK.RESULT_COMPLETE) { + //鎻愪氦楠岃瘉鐮佹垚鍔 + if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { + //楠岃瘉鎴愬姛鍥炶皟 + if(null != mIValidateSMSCode){ + mIValidateSMSCode.onSucced(); + } + } + } else { + Throwable exption = ((Throwable) data); + //楠岃瘉鎴愬姛鍥炶皟 + if(null != mIValidateSMSCode){ + mIValidateSMSCode.onFailed(exption); + } + } + } + } + + /** + * 鎻愪氦鐭俊楠岃瘉鐮佸洖璋冩帴鍙 + */ + public interface IValidateSMSCode{ + void onSucced(); + void onFailed(Throwable e); + } +} diff --git a/src/com/zftlive/android/tools/ToolSOAP.java b/src/com/zftlive/android/tools/ToolSOAP.java new file mode 100644 index 0000000..b412632 --- /dev/null +++ b/src/com/zftlive/android/tools/ToolSOAP.java @@ -0,0 +1,110 @@ +package com.zftlive.android.tools; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.ksoap2.SoapEnvelope; +import org.ksoap2.serialization.SoapObject; +import org.ksoap2.serialization.SoapSerializationEnvelope; +import org.ksoap2.transport.HttpTransportSE; + +import android.os.Handler; +import android.os.Message; + +/** + * 璁块棶WebService鐨勫伐鍏风被, + * + * @author 鏇剧箒娣 + * @version 1.0 + * + */ +public class ToolSOAP { + + // 鍚湁3涓嚎绋嬬殑绾跨▼姹 + private static final ExecutorService executorService = Executors.newFixedThreadPool(3); + + /** + * + * @param url WebService鏈嶅姟鍣ㄥ湴鍧 + * @param namespace 鍛藉悕绌洪棿 + * @param methodName WebService鐨勮皟鐢ㄦ柟娉曞悕 + * @param properties WebService鐨勫弬鏁 + * @param webServiceCallBack 杩斿洖缁撴灉鍥炶皟鎺ュ彛 + */ + public static void callService(String url,final String namespace,final String methodName,HashMap properties,final WebServiceCallBack webServiceCallBack) { + // 鍒涘缓HttpTransportSE瀵硅薄锛屼紶閫扺ebService鏈嶅姟鍣ㄥ湴鍧 + final HttpTransportSE httpTransportSE = new HttpTransportSE(url); + // 鍒涘缓SoapObject瀵硅薄 + SoapObject soapObject = new SoapObject(namespace, methodName); + + // SoapObject娣诲姞鍙傛暟 + if (properties != null) { + for (Iterator> it = properties.entrySet().iterator(); it.hasNext();) { + Map.Entry entry = it.next(); + soapObject.addProperty(entry.getKey(), entry.getValue()); + } + } + + // 瀹炰緥鍖朣oapSerializationEnvelope锛屼紶鍏ebService鐨凷OAP鍗忚鐨勭増鏈彿 + final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); + // 璁剧疆鏄惁璋冪敤鐨勬槸.Net寮鍙戠殑WebService + soapEnvelope.setOutputSoapObject(soapObject); + soapEnvelope.dotNet = true; + httpTransportSE.debug = true; + + // 鐢ㄤ簬瀛愮嚎绋嬩笌涓荤嚎绋嬮氫俊鐨凥andler + final Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + super.handleMessage(msg); + if(msg.what == 0){ + webServiceCallBack.onSucced((SoapObject) msg.obj); + }else{ + webServiceCallBack.onFailure((String)msg.obj); + } + } + }; + + // 寮鍚嚎绋嬪幓璁块棶WebService + executorService.submit(new Runnable() { + + @Override + public void run() { + SoapObject resultSoapObject = null; + Message mgs = mHandler.obtainMessage(); + try { + + httpTransportSE.call(namespace + methodName, soapEnvelope); + if (soapEnvelope.getResponse() != null) { + // 鑾峰彇鏈嶅姟鍣ㄥ搷搴旇繑鍥炵殑SoapObject + resultSoapObject = (SoapObject) soapEnvelope.bodyIn; + } + mgs.what = 0; + mgs.obj = resultSoapObject; + + } catch (Exception e) { + mgs.what = 1; + mgs.obj = e.getMessage(); + } + + // 灏嗚幏鍙栫殑娑堟伅鍒╃敤Handler鍙戦佸埌涓荤嚎绋 + mHandler.sendMessage(mgs); + } + }); + } + + /** + * WebService鍥炶皟鎺ュ彛 + * + */ + public interface WebServiceCallBack { + + public void onSucced(SoapObject result); + + public void onFailure(String result); + } + +} \ No newline at end of file diff --git a/src/com/zftlive/android/tools/ToolString.java b/src/com/zftlive/android/tools/ToolString.java new file mode 100644 index 0000000..df10308 --- /dev/null +++ b/src/com/zftlive/android/tools/ToolString.java @@ -0,0 +1,67 @@ +package com.zftlive.android.tools; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.UUID; + +/** + * 瀛楃涓插伐鍏风被 + * @author 鏇剧箒娣 + * + */ +public class ToolString { + + /** + * 鑾峰彇UUID + * @return 32UUID灏忓啓瀛楃涓 + */ + public static String gainUUID(){ + String strUUID = UUID.randomUUID().toString(); + strUUID = strUUID.replaceAll("-", "").toLowerCase(); + return strUUID; + } + + /** + * 鍒ゆ柇瀛楃涓叉槸鍚﹂潪绌洪潪null + * @param strParm 闇瑕佸垽鏂殑瀛楃涓 + * @return 鐪熷亣 + */ + public static boolean isNoBlankAndNoNull(String strParm) + { + return (strParm == null) || (strParm.equals("")); + } + + /** + * 灏嗘祦杞垚瀛楃涓 + * @param is 杈撳叆娴 + * @return + * @throws Exception + */ + public static String convertStreamToString(InputStream is) throws Exception { + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + StringBuilder sb = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line).append("\n"); + } + return sb.toString(); + } + + /** + * 灏嗘枃浠惰浆鎴愬瓧绗︿覆 + * @param file 鏂囦欢 + * @return + * @throws Exception + */ + public static String getStringFromFile(File file) throws Exception { + FileInputStream fin = new FileInputStream(file); + String ret = convertStreamToString(fin); + //Make sure you close all streams. + fin.close(); + return ret; + } + +} diff --git a/src/com/zftlive/android/tools/ToolToast.java b/src/com/zftlive/android/tools/ToolToast.java new file mode 100644 index 0000000..548efe1 --- /dev/null +++ b/src/com/zftlive/android/tools/ToolToast.java @@ -0,0 +1,155 @@ +package com.zftlive.android.tools; + + +import android.annotation.SuppressLint; +import android.content.Context; +import android.graphics.Color; +import android.graphics.drawable.GradientDrawable; +import android.os.Handler; +import android.util.TypedValue; +import android.view.Gravity; +import android.widget.LinearLayout; +import android.widget.LinearLayout.LayoutParams; +import android.widget.TextView; +import android.widget.Toast; +import com.zftlive.android.MApplication; + +/** + * 鑷畾涔塗oast鎺т欢 + * @author 鏇剧箒娣 + * @version 1.0 + */ +public class ToolToast { + + private static Toast mToast; + private static Handler mHandler = new Handler(); + private static Runnable r = new Runnable() { + public void run() { + mToast.cancel(); + } + }; + + /** + * 寮瑰嚭杈冮暱鏃堕棿鎻愮ず淇℃伅 + * @param context 涓婁笅鏂囧璞 + * @param msg 瑕佹樉绀虹殑淇℃伅 + */ + public static void showLong(Context context, String msg){ + buildToast(context,msg,Toast.LENGTH_LONG).show(); + } + + /** + * 寮瑰嚭杈冮暱鏃堕棿鎻愮ず淇℃伅 + * @param msg 瑕佹樉绀虹殑淇℃伅 + */ + public static void showLong(String msg){ + buildToast(MApplication.gainContext(),msg,Toast.LENGTH_LONG).show(); + } + + /** + * 寮瑰嚭杈冪煭鏃堕棿鎻愮ず淇℃伅 + * @param context 涓婁笅鏂囧璞 + * @param msg 瑕佹樉绀虹殑淇℃伅 + */ + public static void showShort(Context context, String msg){ + buildToast(context,msg,Toast.LENGTH_SHORT).show(); + } + + /** + * 寮瑰嚭杈冪煭鏃堕棿鎻愮ず淇℃伅 + * @param msg 瑕佹樉绀虹殑淇℃伅 + */ + public static void showShort(String msg){ + buildToast(MApplication.gainContext(),msg,Toast.LENGTH_SHORT).show(); + } + + /** + * 鏋勯燭oast + * @param context 涓婁笅鏂 + * @return + */ + private static Toast buildToast(Context context,String msg,int duration){ + return buildToast(context,msg,duration,"#d83636",16); + } + + + /** + * 鏋勯燭oast + * @param context 涓婁笅鏂 + * @param msg 娑堟伅 + * @param duration 鏄剧ず鏃堕棿 + * @param bgColor 鑳屾櫙棰滆壊 + * @return + */ + public static Toast buildToast(Context context,String msg,int duration,String bgColor){ + return buildToast(context,msg,duration,bgColor,16); + } + + + /** + * 鏋勯燭oast + * @param context 涓婁笅鏂 + * @param msg 娑堟伅 + * @param duration 鏄剧ず鏃堕棿 + * @param bgColor 鑳屾櫙棰滆壊 + * @param textSp 鏂囧瓧澶у皬 + * @return + */ + public static Toast buildToast(Context context,String msg,int duration,String bgColor,int textSp){ + return buildToast(context,msg,duration,bgColor,textSp,10); + } + + /** + * 鏋勯燭oast + * @param context 涓婁笅鏂 + * @param msg 娑堟伅 + * @param duration 鏄剧ず鏃堕棿 + * @param bgColor 鑳屾櫙棰滆壊 + * @param textSp 鏂囧瓧澶у皬 + * @param cornerRadius 鍥涜竟鍦嗚寮у害 + * @return + */ + @SuppressLint("NewApi") + public static Toast buildToast(Context context,String msg,int duration,String bgColor,int textSp,int cornerRadius){ + mHandler.removeCallbacks(r); + + if(null == mToast){ + //鏋勫缓Toast + mToast = Toast.makeText(context, null, duration); + mToast.setGravity(Gravity.CENTER, 0, 0); + //鍙栨秷toast + mHandler.postDelayed(r, duration); + } + + //璁剧疆Toast鏂囧瓧 + TextView tv = new TextView(context); + int dpPadding = ToolUnit.dipTopx(10); + tv.setPadding(dpPadding, dpPadding, dpPadding, dpPadding); + tv.setGravity(Gravity.CENTER); + tv.setText(msg); + tv.setTextColor(Color.WHITE); + tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSp); + + //Toast鏂囧瓧TextView瀹瑰櫒 + LinearLayout mLayout = new LinearLayout(context); + GradientDrawable shape = new GradientDrawable(); + shape.setColor(Color.parseColor(bgColor)); + shape.setCornerRadius(cornerRadius); + shape.setStroke(1, Color.parseColor(bgColor)); + shape.setAlpha(180); + mLayout.setBackground(shape); + mLayout.setOrientation(LinearLayout.VERTICAL); + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); + //璁剧疆layout_gravity + params.gravity = Gravity.CENTER; + mLayout.setLayoutParams(params); + //璁剧疆gravity + mLayout.setGravity(Gravity.CENTER); + mLayout.addView(tv); + + //灏嗚嚜瀹氫箟View瑕嗙洊Toast鐨刅iew + mToast.setView(mLayout); + + return mToast; + } +} diff --git a/src/com/zftlive/android/tools/ToolUnit.java b/src/com/zftlive/android/tools/ToolUnit.java new file mode 100644 index 0000000..5a4c2c0 --- /dev/null +++ b/src/com/zftlive/android/tools/ToolUnit.java @@ -0,0 +1,60 @@ +package com.zftlive.android.tools; + +import android.util.DisplayMetrics; + +import com.zftlive.android.model.SysEnv; + +/** + * 鍗曚綅鎹㈢畻宸ュ叿绫 + * @author 鏇剧箒娣
+ * px 锛氬儚绱
+ in 锛氳嫳瀵
+ mm 锛氭绫
+ pt 锛氱锛1/72 鑻卞
+ dp 锛氫竴涓熀浜巇ensity鐨勬娊璞″崟浣嶏紝濡傛灉涓涓160dpi鐨勫睆骞曪紝1dp=1px
+ dip 锛氱瓑鍚屼簬dp
+ sp 锛氬悓dp鐩镐技锛屼絾杩樹細鏍规嵁鐢ㄦ埛鐨勫瓧浣撳ぇ灏忓亸濂芥潵缂╂斁銆
+ 寤鸿浣跨敤sp浣滀负鏂囨湰鐨勫崟浣嶏紝鍏跺畠鐢╠ip
+ 甯冨眬鏃跺敖閲忎娇鐢ㄥ崟浣峝ip锛屽皯浣跨敤px
+ */ +public class ToolUnit { + + /**璁惧鏄剧ず鏉愯川**/ + private static DisplayMetrics mDisplayMetrics = SysEnv.getDisplayMetrics(); + + /** + * sp杞崲px + * @param spValue sp鏁板 + * @return px鏁板 + */ + public static int spTopx(float spValue) { + return (int) (spValue * mDisplayMetrics.scaledDensity + 0.5f); + } + + /** + * px杞崲sp + * @param pxValue px鏁板 + * @return sp鏁板 + */ + public static int pxTosp(float pxValue) { + return (int) (pxValue / mDisplayMetrics.scaledDensity + 0.5f); + } + + /** + * dip杞崲px + * @param dipValue dip鏁板 + * @return px鏁板 + */ + public static int dipTopx(int dipValue) { + return (int) (dipValue * mDisplayMetrics.density + 0.5f); + } + + /** + * px杞崲dip + * @param pxValue px鏁板 + * @return dip鏁板 + */ + public static int pxTodip(float pxValue) { + return (int) (pxValue / mDisplayMetrics.density + 0.5f); + } +} diff --git a/src/com/zftlive/android/tools/ToolXMPP.java b/src/com/zftlive/android/tools/ToolXMPP.java new file mode 100644 index 0000000..c0af25a --- /dev/null +++ b/src/com/zftlive/android/tools/ToolXMPP.java @@ -0,0 +1,306 @@ +package com.zftlive.android.tools; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.jivesoftware.smack.Chat; +import org.jivesoftware.smack.ChatManagerListener; +import org.jivesoftware.smack.ConnectionConfiguration; +import org.jivesoftware.smack.MessageListener; +import org.jivesoftware.smack.Roster; +import org.jivesoftware.smack.RosterEntry; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.packet.Packet; +import org.jivesoftware.smack.packet.Presence; +import org.jivesoftware.smack.util.StringUtils; +import org.json.JSONException; +import org.json.JSONObject; + +import android.util.Log; + +/** + * XMPP宸ュ叿绫 + * @author 鏇剧箒娣 + * @version 1.0 + */ +public class ToolXMPP { + + private static ToolXMPP mToolXMPP = null; + private static XMPPConnection connection = null; + /**鑱婂ぉ绐楀彛绠$悊map闆嗗悎**/ + private Map chatManage = new HashMap(); + + + private ToolXMPP() { + + } + + /** + * 瀵瑰鍏紑鐨勫疄渚嬪寲鎺ュ彛 + * @return + */ + public static ToolXMPP newInstance(String address,int port){ + try { + if(mToolXMPP == null){ + mToolXMPP = new ToolXMPP(); + ConnectionConfiguration config = new ConnectionConfiguration(address,port); + /** 鏄惁鍚敤璋冭瘯 */ + config.setDebuggerEnabled(true); + connection = new XMPPConnection(config); + } + } catch (Exception e) { + System.out.println("鍒涘缓XMMPP澶辫触锛屽師鍥狅細"+e.getMessage()); + e.printStackTrace(); + } + return mToolXMPP; + } + + /** + * 鑾峰彇Openfire杩炴帴 + * @return + */ + public static XMPPConnection getConnection(){ + return connection; + } + + /** + * 杩炴帴鏈嶅姟鍣 + * @return + */ + public boolean connect() { + try { + if(null != connection){ + connection.connect(); + return true; + } + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } + + /** + * 鏂紑杩炴帴 + */ + public void disConnect() { + if(null != connection){ + connection.disconnect(); + } + } + + /** + * 鐧诲綍 + * + * @param username 鐧诲綍甯愬彿 + * @param pswd 鐧诲綍瀵嗙爜 + * @return 鏄惁鐧诲綍鎴愬姛 + */ + public boolean login(String username, String pswd) { + try { + if (connection == null) return false; + connection.login(username, pswd); + return true; + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } + + /** + * 鑾峰彇鎴栧垱寤鸿亰澶╃獥鍙 + * @param friend 濂藉弸鍚 + * @param listenter 鑱婂ぉ鐩戝惉鍣 + * @return + */ + public Chat getFriendChat(String friend, MessageListener listenter) { + if(getConnection()==null) + return null; + + /** 鍒ゆ柇鏄惁鍒涘缓鑱婂ぉ绐楀彛 */ + for (String fristr : chatManage.keySet()) { + if (fristr.equals(friend)) { + // 瀛樺湪鑱婂ぉ绐楀彛锛屽垯杩斿洖瀵瑰簲鑱婂ぉ绐楀彛 + return chatManage.get(fristr); + } + } + /** 鍒涘缓鑱婂ぉ绐楀彛 */ + Chat chat = getConnection().getChatManager().createChat(friend + "@"+getConnection().getServiceName(), listenter); + + /** 娣诲姞鑱婂ぉ绐楀彛鍒癱hatManage */ + chatManage.put(friend, chat); + + return chat; + } + + /** + * 鍒涘缓鑱婂ぉ浼氳瘽 + * @param toUser 鑱婂ぉ瀵硅薄id + * @param domain 鍩 + */ + public Chat createChat(String toUser,String domain){ + Chat mChat = connection.getChatManager().createChat(toUser+domain,mMessageListener); + return mChat; + } + + /** + * 鍙戦佹秷鎭 + * @param strMsg 娑堟伅鍐呭 + */ + public void sendMessage(Chat chat,String strMsg){ + try { + //鍙戦佹秷鎭 + chat.sendMessage(strMsg); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 淇敼瀵嗙爜 + * @param newPswd 鏂板瘑鐮 + * @return 鏄惁淇敼鎴愬姛 + */ + public static boolean changePassword(String newPswd) + { + try { + connection.getAccountManager().changePassword(newPswd); + return true; + } catch (Exception e) { + return false; + } + } + + /** + * 璁剧疆鐢ㄦ埛鐘舵 + */ + public void setPresence(int code) { + if (connection == null) + return; + Presence presence; + switch (code) { + case 0: + presence = new Presence(Presence.Type.available); + connection.sendPacket(presence); + Log.v("state", "璁剧疆鍦ㄧ嚎"); + break; + case 1: + presence = new Presence(Presence.Type.available); + presence.setMode(Presence.Mode.chat); + connection.sendPacket(presence); + Log.v("state", "璁剧疆Q鎴戝惂"); + System.out.println(presence.toXML()); + break; + case 2: + presence = new Presence(Presence.Type.available); + presence.setMode(Presence.Mode.dnd); + connection.sendPacket(presence); + Log.v("state", "璁剧疆蹇欑"); + System.out.println(presence.toXML()); + break; + case 3: + presence = new Presence(Presence.Type.available); + presence.setMode(Presence.Mode.away); + connection.sendPacket(presence); + Log.v("state", "璁剧疆绂诲紑"); + System.out.println(presence.toXML()); + break; + case 4: + Roster roster = connection.getRoster(); + Collection entries = roster.getEntries(); + for (RosterEntry entry : entries) { + presence = new Presence(Presence.Type.unavailable); + presence.setPacketID(Packet.ID_NOT_AVAILABLE); + presence.setFrom(connection.getUser()); + presence.setTo(entry.getUser()); + connection.sendPacket(presence); + System.out.println(presence.toXML()); + } + // 鍚戝悓涓鐢ㄦ埛鐨勫叾浠栧鎴风鍙戦侀殣韬姸鎬 + presence = new Presence(Presence.Type.unavailable); + presence.setPacketID(Packet.ID_NOT_AVAILABLE); + presence.setFrom(connection.getUser()); + presence.setTo(StringUtils.parseBareAddress(connection.getUser())); + connection.sendPacket(presence); + Log.v("state", "璁剧疆闅愯韩"); + break; + case 5: + presence = new Presence(Presence.Type.unavailable); + connection.sendPacket(presence); + Log.v("state", "璁剧疆绂荤嚎"); + break; + default: + break; + } + } + + /** + * 鍒犻櫎褰撳墠鐢ㄦ埛 + * @param connection + * @return + */ + public boolean deleteAccount() + { + try { + connection.getAccountManager().deleteAccount(); + return true; + } catch (Exception e) { + return false; + } + } + + /** + * 閫鍑 + */ + public void exit() { + // 鐘舵 + Presence presence = new Presence(Presence.Type.unavailable); + connection.sendPacket(presence); + //鏂紑杩炴帴 + disConnect(); + } + + /** + * 娑堟伅鐩戝惉鍣 + */ + public MessageListener mMessageListener = new MessageListener(){ + + @Override + public void processMessage(Chat arg0, Message msg) { + Log.i("ToolXMPP", "from="+msg.getFrom() + "to="+msg.getTo() + "body="+msg.getBody() + "subject="+msg.getSubject()); + + //鐧诲綍鐢ㄦ埛 + StringUtils.parseName(getConnection().getUser()); + //鍙戦佹秷鎭敤鎴 + msg.getFrom(); + //娑堟伅鍐呭 + String body = msg.getBody(); + boolean left = body.substring(0, 1).equals("{"); + boolean right = body.substring(body.length()-1, body.length()).equals("}"); + if(left&&right){ + try { + JSONObject obj = new JSONObject(body); + String type = obj.getString("messageType"); + String chanId = obj.getString("chanId"); + String chanName = obj.getString("chanName"); + } catch (JSONException e) { + e.printStackTrace(); + } + } + } + }; + + /** + * 鍗曚汉鑱婂ぉ淇℃伅鐩戝惉绫 + * + */ + public ChatManagerListener mChatManagerListener = new ChatManagerListener(){ + + public void chatCreated(Chat chat, boolean arg1) + { + chat.addMessageListener(mMessageListener); + } + }; + +} diff --git a/src/com/zftlive/android/tools/ToolXml.java b/src/com/zftlive/android/tools/ToolXml.java new file mode 100644 index 0000000..4c07d69 --- /dev/null +++ b/src/com/zftlive/android/tools/ToolXml.java @@ -0,0 +1,39 @@ +package com.zftlive.android.tools; + +import com.thoughtworks.xstream.XStream; + +/** + * XML宸ュ叿绫 + * @author 鏇剧箒娣 + * + */ +public class ToolXml { + + /** + * java 杞崲鎴恱ml + * + * @Title: toXml + * @Description: 灏咼avaBean杞垚XML + * @param bean 瀵硅薄瀹炰緥 + * @return String xml瀛楃涓 + */ + public static String toXml(Object bean) { + XStream xstream = new XStream(); + xstream.processAnnotations(bean.getClass()); + return xstream.toXML(bean); + } + + /** + * 灏嗕紶鍏ml鏂囨湰杞崲鎴怞ava瀵硅薄 + * @param xml + * @param bean xml瀵瑰簲鐨刢lass绫 + * @return T xml瀵瑰簲鐨刢lass绫荤殑瀹炰緥瀵硅薄 璋冪敤鐨勬柟娉曞疄渚嬶細 + * PersonBean person=XmlUtil.toBean(xmlStr, PersonBean.class); + */ + public static T toBean(String xml, Class bean) { + XStream xstream = new XStream(); + xstream.processAnnotations(bean); + T obj = (T) xstream.fromXML(xml); + return obj; + } +} diff --git a/src/com/zftlive/android/view/IView.java b/src/com/zftlive/android/view/IView.java new file mode 100644 index 0000000..4c5c676 --- /dev/null +++ b/src/com/zftlive/android/view/IView.java @@ -0,0 +1,51 @@ +package com.zftlive.android.view; + +import com.zftlive.android.MApplication; + +/** + * 鑷畾涔塚iew闇瑕佺敤鍒扮殑甯搁噺銆佹柟娉 + * @author 鏇剧箒娣 + * + */ +public interface IView { + + /**搴旂敤鐨勫寘鍚嶇О**/ + String PACKAGE_NAME = MApplication.gainContext().getPackageName(); + + /***璧勬簮绫诲瀷-array**/ + String ARRAY = "array"; + + /***璧勬簮绫诲瀷-attr**/ + String ATTR = "attr"; + + /***璧勬簮绫诲瀷-bool**/ + String BOOL = "bool"; + + /***璧勬簮绫诲瀷-color**/ + String COLOR = "color"; + + /***璧勬簮绫诲瀷-dimen**/ + String DIMEN = "dimen"; + + /***璧勬簮绫诲瀷-drawable**/ + String DRAWABLE = "drawable"; + + /***璧勬簮绫诲瀷-id**/ + String ID = "id"; + + /***璧勬簮绫诲瀷-id**/ + String INTEGER = "integer"; + + /***璧勬簮绫诲瀷-layout**/ + String LAYOUT = "layout"; + + /***璧勬簮绫诲瀷-drawable**/ + String STRING = "string"; + + /***璧勬簮绫诲瀷-style**/ + String STYLE = "style"; + + /***璧勬簮绫诲瀷-styleable**/ + String STYLEABLE = "styleable"; + +} diff --git a/src/com/zftlive/android/widget/MyToast.java b/src/com/zftlive/android/widget/MyToast.java new file mode 100644 index 0000000..e9c88b8 --- /dev/null +++ b/src/com/zftlive/android/widget/MyToast.java @@ -0,0 +1,254 @@ +package com.zftlive.android.widget; + + +import android.content.Context; +import android.graphics.Color; +import android.graphics.PixelFormat; +import android.graphics.drawable.GradientDrawable; +import android.os.Handler; +import android.view.Gravity; +import android.view.View; +import android.view.WindowManager; +import android.widget.LinearLayout; +import android.widget.TextView; + +/** + * 鑷畾涔夋椂闀跨殑Toast + * @author 鏇剧箒娣 + * @version 1.0 + * + */ +public class MyToast { + + public static MyToast makeText(Context context, CharSequence text, int duration) + { + MyToast result = new MyToast(context); + + LinearLayout mLayout=new LinearLayout(context); + TextView tv = new TextView(context); + tv.setText(text); + tv.setTextColor(Color.WHITE); + tv.setGravity(Gravity.CENTER); + + GradientDrawable shape = new GradientDrawable(); + shape.setColor(Color.parseColor("#FF6666")); + shape.setCornerRadius(0.9f); + shape.setStroke(1, Color.parseColor("#FF6666")); + shape.setAlpha(180); + mLayout.setBackground(shape); + + int w=context.getResources().getDisplayMetrics().widthPixels / 2; + int h=context.getResources().getDisplayMetrics().widthPixels / 10; + mLayout.addView(tv, w, h); + result.mNextView = mLayout; + result.mDuration = duration; + + return result; + } + + public static final int LENGTH_SHORT = 2000; + public static final int LENGTH_LONG = 3500; + + private final Handler mHandler = new Handler(); + private int mDuration=LENGTH_SHORT; + private int mGravity = Gravity.CENTER; + private int mX, mY; + private float mHorizontalMargin; + private float mVerticalMargin; + private View mView; + private View mNextView; + + private WindowManager mWM; + private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(); + + + public MyToast(Context context) { + init(context); + } + + /** + * Set the view to show. + * @see #getView + */ + public void setView(View view) { + mNextView = view; + } + + /** + * Return the view. + * @see #setView + */ + public View getView() { + return mNextView; + } + + /** + * Set how long to show the view for. + * @see #LENGTH_SHORT + * @see #LENGTH_LONG + */ + public void setDuration(int duration) { + mDuration = duration; + } + + /** + * Return the duration. + * @see #setDuration + */ + public int getDuration() { + return mDuration; + } + + /** + * Set the margins of the view. + * + * @param horizontalMargin The horizontal margin, in percentage of the + * container width, between the container's edges and the + * notification + * @param verticalMargin The vertical margin, in percentage of the + * container height, between the container's edges and the + * notification + */ + public void setMargin(float horizontalMargin, float verticalMargin) { + mHorizontalMargin = horizontalMargin; + mVerticalMargin = verticalMargin; + } + + /** + * Return the horizontal margin. + */ + public float getHorizontalMargin() { + return mHorizontalMargin; + } + + /** + * Return the vertical margin. + */ + public float getVerticalMargin() { + return mVerticalMargin; + } + + /** + * Set the location at which the notification should appear on the screen. + * @see android.view.Gravity + * @see #getGravity + */ + public void setGravity(int gravity, int xOffset, int yOffset) { + mGravity = gravity; + mX = xOffset; + mY = yOffset; + } + + /** + * Get the location at which the notification should appear on the screen. + * @see android.view.Gravity + * @see #getGravity + */ + public int getGravity() { + return mGravity; + } + + /** + * Return the X offset in pixels to apply to the gravity's location. + */ + public int getXOffset() { + return mX; + } + + /** + * Return the Y offset in pixels to apply to the gravity's location. + */ + public int getYOffset() { + return mY; + } + + /** + * schedule handleShow into the right thread + */ + public void show() { + mHandler.post(mShow); + + if(mDuration>0) + { + mHandler.postDelayed(mHide, mDuration); + } + } + + /** + * schedule handleHide into the right thread + */ + public void hide() { + mHandler.post(mHide); + } + + private final Runnable mShow = new Runnable() { + public void run() { + handleShow(); + } + }; + + private final Runnable mHide = new Runnable() { + public void run() { + handleHide(); + } + }; + + private void init(Context context) + { + final WindowManager.LayoutParams params = mParams; + params.height = WindowManager.LayoutParams.WRAP_CONTENT; + params.width = WindowManager.LayoutParams.WRAP_CONTENT; + params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE + | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE + | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; + params.format = PixelFormat.TRANSLUCENT; + params.windowAnimations = android.R.style.Animation_Toast; + params.type = WindowManager.LayoutParams.TYPE_TOAST; + params.setTitle("Toast"); + + mWM = (WindowManager) context.getApplicationContext() + .getSystemService(Context.WINDOW_SERVICE); + } + + + private void handleShow() { + + if (mView != mNextView) { + // remove the old view if necessary + handleHide(); + mView = mNextView; +// mWM = WindowManagerImpl.getDefault(); + final int gravity = mGravity; + mParams.gravity = gravity; + if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) + { + mParams.horizontalWeight = 1.0f; + } + if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) + { + mParams.verticalWeight = 1.0f; + } + mParams.x = mX; + mParams.y = mY; + mParams.verticalMargin = mVerticalMargin; + mParams.horizontalMargin = mHorizontalMargin; + if (mView.getParent() != null) + { + mWM.removeView(mView); + } + mWM.addView(mView, mParams); + } + } + + private void handleHide() + { + if (mView != null) + { + if (mView.getParent() != null) + { + mWM.removeView(mView); + } + mView = null; + } + } +} \ No newline at end of file