Set the current page of both the ViewPager and indicator.
44 | * 45 | *This must be used if you need to set the page before 46 | * the views are drawn on screen (e.g., default start page).
47 | * 48 | * @param item 49 | */ 50 | void setCurrentItem(int item); 51 | 52 | /** 53 | * Set a page change listener which will receive forwarded events. 54 | * 55 | * @param listener 56 | */ 57 | void setOnPageChangeListener(ViewPager.OnPageChangeListener listener); 58 | 59 | /** 60 | * Notify the indicator that the fragment list has changed. 61 | */ 62 | void notifyDataSetChanged(); 63 | } 64 | -------------------------------------------------------------------------------- /Weather/src/com/way/plistview/BladeView.java: -------------------------------------------------------------------------------- 1 | package com.way.plistview; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.Color; 6 | import android.graphics.Paint; 7 | import android.graphics.Typeface; 8 | import android.os.Handler; 9 | import android.util.AttributeSet; 10 | import android.view.Gravity; 11 | import android.view.MotionEvent; 12 | import android.view.View; 13 | import android.widget.PopupWindow; 14 | import android.widget.TextView; 15 | 16 | public class BladeView extends View { 17 | private OnItemClickListener mOnItemClickListener; 18 | String[] b = { "#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", 19 | "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", 20 | "Y", "Z" }; 21 | int choose = -1; 22 | Paint paint = new Paint(); 23 | boolean showBkg = false; 24 | private PopupWindow mPopupWindow; 25 | private TextView mPopupText; 26 | private Handler handler = new Handler(); 27 | 28 | public BladeView(Context context, AttributeSet attrs, int defStyle) { 29 | super(context, attrs, defStyle); 30 | } 31 | 32 | public BladeView(Context context, AttributeSet attrs) { 33 | super(context, attrs); 34 | } 35 | 36 | public BladeView(Context context) { 37 | super(context); 38 | } 39 | 40 | @Override 41 | protected void onDraw(Canvas canvas) { 42 | super.onDraw(canvas); 43 | if (showBkg) { 44 | canvas.drawColor(Color.parseColor("#00000000")); 45 | } 46 | 47 | int height = getHeight(); 48 | int width = getWidth(); 49 | int singleHeight = height / b.length; 50 | for (int i = 0; i < b.length; i++) { 51 | paint.setColor(Color.parseColor("#ff2f2f2f")); 52 | paint.setTypeface(Typeface.DEFAULT_BOLD); 53 | paint.setFakeBoldText(true); 54 | paint.setAntiAlias(true); 55 | if (i == choose) { 56 | paint.setColor(Color.parseColor("#3399ff")); 57 | } 58 | float xPos = width / 2 - paint.measureText(b[i]) / 2; 59 | float yPos = singleHeight * i + singleHeight; 60 | canvas.drawText(b[i], xPos, yPos, paint); 61 | paint.reset(); 62 | } 63 | 64 | } 65 | 66 | @Override 67 | public boolean dispatchTouchEvent(MotionEvent event) { 68 | final int action = event.getAction(); 69 | final float y = event.getY(); 70 | final int oldChoose = choose; 71 | final int c = (int) (y / getHeight() * b.length); 72 | 73 | switch (action) { 74 | case MotionEvent.ACTION_DOWN: 75 | showBkg = true; 76 | if (oldChoose != c) { 77 | if (c > 0 && c < b.length) { 78 | performItemClicked(c); 79 | choose = c; 80 | invalidate(); 81 | } 82 | } 83 | 84 | break; 85 | case MotionEvent.ACTION_MOVE: 86 | if (oldChoose != c) { 87 | if (c > 0 && c < b.length) { 88 | performItemClicked(c); 89 | choose = c; 90 | invalidate(); 91 | } 92 | } 93 | break; 94 | case MotionEvent.ACTION_UP: 95 | showBkg = false; 96 | choose = -1; 97 | dismissPopup(); 98 | invalidate(); 99 | break; 100 | } 101 | return true; 102 | } 103 | 104 | private void showPopup(int item) { 105 | if (mPopupWindow == null) { 106 | handler.removeCallbacks(dismissRunnable); 107 | mPopupText = new TextView(getContext()); 108 | mPopupText.setBackgroundColor(Color.GRAY); 109 | mPopupText.setTextColor(Color.CYAN); 110 | mPopupText.setTextSize(50); 111 | mPopupText.setGravity(Gravity.CENTER_HORIZONTAL 112 | | Gravity.CENTER_VERTICAL); 113 | mPopupWindow = new PopupWindow(mPopupText, 100, 100); 114 | } 115 | 116 | String text = ""; 117 | if (item == 0) { 118 | text = "#"; 119 | } else { 120 | text = Character.toString((char) ('A' + item - 1)); 121 | } 122 | mPopupText.setText(text); 123 | if (mPopupWindow.isShowing()) { 124 | mPopupWindow.update(); 125 | } else { 126 | mPopupWindow.showAtLocation(getRootView(), 127 | Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0); 128 | } 129 | } 130 | 131 | private void dismissPopup() { 132 | handler.postDelayed(dismissRunnable, 800); 133 | } 134 | 135 | Runnable dismissRunnable = new Runnable() { 136 | 137 | @Override 138 | public void run() { 139 | // TODO Auto-generated method stub 140 | if (mPopupWindow != null) { 141 | mPopupWindow.dismiss(); 142 | } 143 | } 144 | }; 145 | 146 | public boolean onTouchEvent(MotionEvent event) { 147 | return super.onTouchEvent(event); 148 | } 149 | 150 | public void setOnItemClickListener(OnItemClickListener listener) { 151 | mOnItemClickListener = listener; 152 | } 153 | 154 | private void performItemClicked(int item) { 155 | if (mOnItemClickListener != null) { 156 | mOnItemClickListener.onItemClick(b[item]); 157 | showPopup(item); 158 | } 159 | } 160 | 161 | public interface OnItemClickListener { 162 | void onItemClick(String s); 163 | } 164 | 165 | } 166 | -------------------------------------------------------------------------------- /Weather/src/com/way/plistview/PinnedHeaderListView.java: -------------------------------------------------------------------------------- 1 | package com.way.plistview; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.util.AttributeSet; 6 | import android.view.View; 7 | import android.widget.ListAdapter; 8 | import android.widget.ListView; 9 | 10 | public class PinnedHeaderListView extends ListView { 11 | 12 | public interface PinnedHeaderAdapter { 13 | public static final int PINNED_HEADER_GONE = 0; 14 | public static final int PINNED_HEADER_VISIBLE = 1; 15 | public static final int PINNED_HEADER_PUSHED_UP = 2; 16 | 17 | int getPinnedHeaderState(int position); 18 | 19 | void configurePinnedHeader(View header, int position, int alpha); 20 | } 21 | 22 | private static final int MAX_ALPHA = 255; 23 | 24 | private PinnedHeaderAdapter mAdapter; 25 | private View mHeaderView; 26 | private boolean mHeaderViewVisible; 27 | private int mHeaderViewWidth; 28 | private int mHeaderViewHeight; 29 | 30 | public PinnedHeaderListView(Context context) { 31 | super(context); 32 | } 33 | 34 | public PinnedHeaderListView(Context context, AttributeSet attrs) { 35 | super(context, attrs); 36 | } 37 | 38 | public PinnedHeaderListView(Context context, AttributeSet attrs, 39 | int defStyle) { 40 | super(context, attrs, defStyle); 41 | } 42 | 43 | protected void onLayout(boolean changed, int left, int top, int right, 44 | int bottom) { 45 | super.onLayout(changed, left, top, right, bottom); 46 | if (mHeaderView != null) { 47 | mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight); 48 | configureHeaderView(getFirstVisiblePosition()); 49 | } 50 | } 51 | 52 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 53 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 54 | if (mHeaderView != null) { 55 | measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec); 56 | mHeaderViewWidth = mHeaderView.getMeasuredWidth(); 57 | mHeaderViewHeight = mHeaderView.getMeasuredHeight(); 58 | } 59 | } 60 | 61 | public void setPinnedHeaderView(View view) { 62 | mHeaderView = view; 63 | if (mHeaderView != null) { 64 | setFadingEdgeLength(0); 65 | } 66 | requestLayout(); 67 | } 68 | 69 | public void setAdapter(ListAdapter adapter) { 70 | super.setAdapter(adapter); 71 | mAdapter = (PinnedHeaderAdapter) adapter; 72 | } 73 | 74 | public void configureHeaderView(int position) { 75 | if (mHeaderView == null) { 76 | return; 77 | } 78 | int state = mAdapter.getPinnedHeaderState(position); 79 | switch (state) { 80 | case PinnedHeaderAdapter.PINNED_HEADER_GONE: { 81 | mHeaderViewVisible = false; 82 | break; 83 | } 84 | 85 | case PinnedHeaderAdapter.PINNED_HEADER_VISIBLE: { 86 | mAdapter.configurePinnedHeader(mHeaderView, position, MAX_ALPHA); 87 | if (mHeaderView.getTop() != 0) { 88 | mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight); 89 | } 90 | mHeaderViewVisible = true; 91 | break; 92 | } 93 | 94 | case PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP: { 95 | View firstView = getChildAt(0); 96 | int bottom = firstView.getBottom(); 97 | int headerHeight = mHeaderView.getHeight(); 98 | int y; 99 | int alpha; 100 | if (bottom < headerHeight) { 101 | y = (bottom - headerHeight); 102 | alpha = MAX_ALPHA * (headerHeight + y) / headerHeight; 103 | } else { 104 | y = 0; 105 | alpha = MAX_ALPHA; 106 | } 107 | mAdapter.configurePinnedHeader(mHeaderView, position, alpha); 108 | if (mHeaderView.getTop() != y) { 109 | mHeaderView.layout(0, y, mHeaderViewWidth, mHeaderViewHeight 110 | + y); 111 | } 112 | mHeaderViewVisible = true; 113 | break; 114 | } 115 | } 116 | } 117 | 118 | protected void dispatchDraw(Canvas canvas) { 119 | super.dispatchDraw(canvas); 120 | if (mHeaderViewVisible) { 121 | drawChild(canvas, mHeaderView, getDrawingTime()); 122 | } 123 | } 124 | } -------------------------------------------------------------------------------- /Weather/src/com/way/util/ConfigCache.java: -------------------------------------------------------------------------------- 1 | package com.way.util; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | import android.util.Log; 7 | 8 | import com.way.app.Application; 9 | 10 | public class ConfigCache { 11 | private static final String TAG = ConfigCache.class.getName(); 12 | 13 | public static final int CONFIG_CACHE_MOBILE_TIMEOUT = 7200000; // 2 hour 14 | public static final int CONFIG_CACHE_WIFI_TIMEOUT = 1800000; // 30 minute 15 | 16 | public static String getUrlCache(String url) { 17 | if (url == null) { 18 | return null; 19 | } 20 | 21 | String result = null; 22 | File file = new File(Application.getInstance().getCacheDir() 23 | + File.separator + replaceUrlWithPlus(url)); 24 | if (file.exists() && file.isFile()) { 25 | long expiredTime = System.currentTimeMillis() - file.lastModified(); 26 | Log.d(TAG, file.getAbsolutePath() + " expiredTime:" + expiredTime 27 | / 60000 + "min"); 28 | // 1. in case the system time is incorrect (the time is turn back 29 | // long ago) 30 | // 2. when the network is invalid, you can only read the cache 31 | if (Application.mNetWorkState != NetUtil.NETWORN_NONE 32 | && expiredTime < 0) { 33 | return null; 34 | } 35 | if (Application.mNetWorkState == NetUtil.NETWORN_WIFI 36 | && expiredTime > CONFIG_CACHE_WIFI_TIMEOUT) { 37 | return null; 38 | } else if (Application.mNetWorkState == NetUtil.NETWORN_MOBILE 39 | && expiredTime > CONFIG_CACHE_MOBILE_TIMEOUT) { 40 | return null; 41 | } 42 | try { 43 | result = FileUtils.readTextFile(file); 44 | } catch (IOException e) { 45 | e.printStackTrace(); 46 | } 47 | } 48 | return result; 49 | } 50 | 51 | public static void setUrlCache(String data, String url) { 52 | if (Application.getInstance().getCacheDir() == null) { 53 | return; 54 | } 55 | // File dir = new File(BaseApplication.mSdcardDataDir); 56 | // if (!dir.exists() && 57 | // Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 58 | // { 59 | // dir.mkdirs(); 60 | // } 61 | File file = new File(Application.getInstance().getCacheDir() 62 | + File.separator + replaceUrlWithPlus(url)); 63 | try { 64 | // 创建缓存数据到磁盘,就是创建文件 65 | FileUtils.writeTextFile(file, data); 66 | } catch (Exception e) { 67 | Log.d(TAG, "write " + file.getAbsolutePath() + " data failed!"); 68 | e.printStackTrace(); 69 | } 70 | } 71 | 72 | /** 73 | * delete cahce file recursively 74 | * 75 | * @param cacheFile 76 | * if null means clear cache function, or clear cache file 77 | */ 78 | public static void clearCache(File cacheFile) { 79 | if (cacheFile == null) { 80 | try { 81 | File cacheDir = Application.getInstance().getCacheDir(); 82 | if (cacheDir.exists()) { 83 | clearCache(cacheDir); 84 | } 85 | } catch (Exception e) { 86 | e.printStackTrace(); 87 | } 88 | } else if (cacheFile.isFile()) { 89 | cacheFile.delete(); 90 | } else if (cacheFile.isDirectory()) { 91 | File[] childFiles = cacheFile.listFiles(); 92 | for (int i = 0; i < childFiles.length; i++) { 93 | clearCache(childFiles[i]); 94 | } 95 | } 96 | } 97 | 98 | public static String replaceUrlWithPlus(String url) { 99 | // 1. 处理特殊字符 100 | // 2. 去除后缀名带来的文件浏览器的视图凌乱(特别是图片更需要如此类似处理,否则有的手机打开图库,全是我们的缓存图片) 101 | if (url != null) { 102 | return url.replaceAll("http://(.)*?/", "") 103 | .replaceAll("[.:/,%?&=]", "+").replaceAll("[+]+", "+"); 104 | } 105 | return null; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /Weather/src/com/way/util/FileUtils.java: -------------------------------------------------------------------------------- 1 | package com.way.util; 2 | 3 | import java.io.BufferedInputStream; 4 | import java.io.BufferedOutputStream; 5 | import java.io.BufferedReader; 6 | import java.io.DataOutputStream; 7 | import java.io.File; 8 | import java.io.FileInputStream; 9 | import java.io.FileOutputStream; 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.io.InputStreamReader; 13 | 14 | public class FileUtils { 15 | private static final int BUFFER = 8192; 16 | 17 | // 读取文件 18 | public static String readTextFile(File file) throws IOException { 19 | String text = null; 20 | InputStream is = null; 21 | try { 22 | is = new FileInputStream(file); 23 | text = readTextInputStream(is); 24 | ; 25 | } finally { 26 | if (is != null) { 27 | is.close(); 28 | } 29 | } 30 | return text; 31 | } 32 | 33 | // 从流中读取文件 34 | public static String readTextInputStream(InputStream is) throws IOException { 35 | StringBuffer strbuffer = new StringBuffer(); 36 | String line; 37 | BufferedReader reader = null; 38 | try { 39 | reader = new BufferedReader(new InputStreamReader(is)); 40 | while ((line = reader.readLine()) != null) { 41 | strbuffer.append(line).append("\r\n"); 42 | } 43 | } finally { 44 | if (reader != null) { 45 | reader.close(); 46 | } 47 | } 48 | return strbuffer.toString(); 49 | } 50 | 51 | // 将文本内容写入文件 52 | public static void writeTextFile(File file, String str) throws IOException { 53 | DataOutputStream out = null; 54 | try { 55 | out = new DataOutputStream(new FileOutputStream(file)); 56 | out.write(str.getBytes()); 57 | } finally { 58 | if (out != null) { 59 | out.close(); 60 | } 61 | } 62 | } 63 | 64 | // 复制文件 65 | public static void copyFile(File sourceFile, File targetFile) 66 | throws IOException { 67 | BufferedInputStream inBuff = null; 68 | BufferedOutputStream outBuff = null; 69 | try { 70 | inBuff = new BufferedInputStream(new FileInputStream(sourceFile)); 71 | outBuff = new BufferedOutputStream(new FileOutputStream(targetFile)); 72 | byte[] buffer = new byte[BUFFER]; 73 | int length; 74 | while ((length = inBuff.read(buffer)) != -1) { 75 | outBuff.write(buffer, 0, length); 76 | } 77 | outBuff.flush(); 78 | } finally { 79 | if (inBuff != null) { 80 | inBuff.close(); 81 | } 82 | if (outBuff != null) { 83 | outBuff.close(); 84 | } 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /Weather/src/com/way/util/IphoneDialog.java: -------------------------------------------------------------------------------- 1 | package com.way.util; 2 | 3 | import android.app.Activity; 4 | import android.app.Dialog; 5 | import android.view.View; 6 | import android.view.View.OnClickListener; 7 | import android.view.Window; 8 | import android.widget.Button; 9 | import android.widget.TextView; 10 | 11 | import com.way.weather.R; 12 | 13 | public class IphoneDialog { 14 | public static Dialog getTwoBtnDialog(Activity activity, String title, 15 | String msg) { 16 | final Dialog dialog = new Dialog(activity, 17 | android.R.style.Theme_Translucent_NoTitleBar); 18 | dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 19 | dialog.setContentView(R.layout.r_okcanceldialogview); 20 | ((TextView) dialog.findViewById(R.id.dialog_title)).setText(title); 21 | ((TextView) dialog.findViewById(R.id.dialog_message)).setText(msg); 22 | ((Button) dialog.findViewById(R.id.ok)).setText(android.R.string.ok); 23 | ((Button) dialog.findViewById(R.id.cancel)) 24 | .setText(android.R.string.cancel); 25 | ((Button) dialog.findViewById(R.id.cancel)) 26 | .setOnClickListener(new OnClickListener() { 27 | 28 | @Override 29 | public void onClick(View v) { 30 | // write your code to do things after users clicks 31 | // CANCEL 32 | dialog.dismiss(); 33 | } 34 | }); 35 | return dialog; 36 | // ((Button) dialog.findViewById(R.id.ok)) 37 | // .setOnClickListener(new OnClickListener() { 38 | // 39 | // @Override 40 | // public void onClick(View v) { 41 | // // write your code to do things after users clicks OK 42 | // 43 | // dialog.dismiss(); 44 | // } 45 | // }); 46 | // dialog.show(); 47 | 48 | } 49 | 50 | /** 51 | * it will show the OK dialog like iphone, make sure no keyboard is visible 52 | * 53 | * @param title 54 | * title for dialog 55 | * @param msg 56 | * msg for body 57 | */ 58 | public static Dialog getOneBtnDialog(Activity activity, String title, 59 | String msg) { 60 | final Dialog dialog = new Dialog(activity, 61 | android.R.style.Theme_Translucent_NoTitleBar); 62 | dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 63 | dialog.setContentView(R.layout.r_okdialogview); 64 | ((TextView) dialog.findViewById(R.id.dialog_title)).setText(title); 65 | ((TextView) dialog.findViewById(R.id.dialog_message)).setText(msg); 66 | 67 | return dialog; 68 | // ((Button) dialog.findViewById(R.id.ok)).setText("Ok"); 69 | // ((Button) dialog.findViewById(R.id.ok)) 70 | // .setOnClickListener(new OnClickListener() { 71 | // @Override 72 | // public void onClick(View v) { 73 | // // write your code to do things after users clicks OK 74 | // dialog.dismiss(); 75 | // } 76 | // }); 77 | // dialog.show(); 78 | 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Weather/src/com/way/util/L.java: -------------------------------------------------------------------------------- 1 | package com.way.util; 2 | 3 | import android.util.Log; 4 | 5 | /** 6 | * Log统一管理类 7 | * 8 | * @author way 9 | * 10 | */ 11 | public class L { 12 | public static boolean isDebug = true;// 是否需要打印bug,可以在application的onCreate函数里面初始化 13 | private static final String TAG = "way"; 14 | 15 | // 下面四个是默认tag的函数 16 | public static void i(String msg) { 17 | if (isDebug) 18 | Log.i(TAG, msg); 19 | } 20 | 21 | public static void d(String msg) { 22 | if (isDebug) 23 | Log.d(TAG, msg); 24 | } 25 | 26 | public static void e(String msg) { 27 | if (isDebug) 28 | Log.e(TAG, msg); 29 | } 30 | 31 | public static void v(String msg) { 32 | if (isDebug) 33 | Log.v(TAG, msg); 34 | } 35 | 36 | // 下面是传入自定义tag的函数 37 | public static void i(String tag, String msg) { 38 | if (isDebug) 39 | Log.i(tag, msg); 40 | } 41 | 42 | public static void d(String tag, String msg) { 43 | if (isDebug) 44 | Log.i(tag, msg); 45 | } 46 | 47 | public static void e(String tag, String msg) { 48 | if (isDebug) 49 | Log.i(tag, msg); 50 | } 51 | 52 | public static void v(String tag, String msg) { 53 | if (isDebug) 54 | Log.i(tag, msg); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Weather/src/com/way/util/Lunar.java: -------------------------------------------------------------------------------- 1 | package com.way.util; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Calendar; 6 | import java.util.Date; 7 | import java.util.TimeZone; 8 | 9 | /** 10 | * 11 | * @author way 12 | * 13 | */ 14 | public class Lunar { 15 | private int year; 16 | private int month; 17 | private int day; 18 | private boolean leap; 19 | final static String chineseNumber[] = { "一", "二", "三", "四", "五", "六", "七", 20 | "八", "九", "十", "十一", "十二" }; 21 | static SimpleDateFormat chineseDateFormat = new SimpleDateFormat( 22 | "yyyy年MM月dd日"); 23 | final static long[] lunarInfo = new long[] { 0x04bd8, 0x04ae0, 0x0a570, 24 | 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 25 | 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 26 | 0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 27 | 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, 28 | 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 29 | 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 30 | 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 31 | 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 32 | 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 33 | 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 34 | 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 35 | 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 36 | 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, 37 | 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 38 | 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 39 | 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 40 | 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 41 | 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 42 | 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 43 | 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 44 | 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0 }; 45 | 46 | // ====== 传回农历 y年的总天数 47 | final private static int yearDays(int y) { 48 | int i, sum = 348; 49 | for (i = 0x8000; i > 0x8; i >>= 1) { 50 | if ((lunarInfo[y - 1900] & i) != 0) 51 | sum += 1; 52 | } 53 | return (sum + leapDays(y)); 54 | } 55 | 56 | // ====== 传回农历 y年闰月的天数 57 | final private static int leapDays(int y) { 58 | if (leapMonth(y) != 0) { 59 | if ((lunarInfo[y - 1900] & 0x10000) != 0) 60 | return 30; 61 | else 62 | return 29; 63 | } else 64 | return 0; 65 | } 66 | 67 | // ====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0 68 | final private static int leapMonth(int y) { 69 | return (int) (lunarInfo[y - 1900] & 0xf); 70 | } 71 | 72 | // ====== 传回农历 y年m月的总天数 73 | final private static int monthDays(int y, int m) { 74 | if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0) 75 | return 29; 76 | else 77 | return 30; 78 | } 79 | 80 | // ====== 传回农历 y年的生肖 81 | final public String animalsYear() { 82 | final String[] Animals = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", 83 | "马", "羊", "猴", "鸡", "狗", "猪" }; 84 | return Animals[(year - 4) % 12]; 85 | } 86 | 87 | // ====== 传入 月日的offset 传回干支, 0=甲子 88 | final private static String cyclicalm(int num) { 89 | final String[] Gan = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚", 90 | "辛", "壬", "癸" }; 91 | final String[] Zhi = new String[] { "子", "丑", "寅", "卯", "辰", "巳", "午", 92 | "未", "申", "酉", "戌", "亥" }; 93 | return (Gan[num % 10] + Zhi[num % 12]); 94 | } 95 | 96 | // ====== 传入 offset 传回干支, 0=甲子 97 | final public String cyclical() { 98 | int num = year - 1900 + 36; 99 | return (cyclicalm(num)); 100 | } 101 | 102 | public String getLunarMonthString() { 103 | // TODO Auto-generated method stub 104 | return null; 105 | } 106 | 107 | public Lunar(Calendar cal) { 108 | @SuppressWarnings("unused") 109 | int yearCyl, monCyl, dayCyl; 110 | int leapMonth = 0; 111 | Date baseDate = null; 112 | try { 113 | baseDate = chineseDateFormat.parse("1900年1月31日"); 114 | } catch (ParseException e) { 115 | e.printStackTrace(); 116 | } 117 | 118 | // 求出和1900年1月31日相差的天数 119 | int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L); 120 | dayCyl = offset + 40; 121 | monCyl = 14; 122 | 123 | // 用offset减去每农历年的天数 124 | // 计算当天是农历第几天 125 | // i最终结果是农历的年份 126 | // offset是当年的第几天 127 | int iYear, daysOfYear = 0; 128 | for (iYear = 1900; iYear < 2050 && offset > 0; iYear++) { 129 | daysOfYear = yearDays(iYear); 130 | offset -= daysOfYear; 131 | monCyl += 12; 132 | } 133 | if (offset < 0) { 134 | offset += daysOfYear; 135 | iYear--; 136 | monCyl -= 12; 137 | } 138 | // 农历年份 139 | year = iYear; 140 | 141 | yearCyl = iYear - 1864; 142 | leapMonth = leapMonth(iYear); // 闰哪个月,1-12 143 | leap = false; 144 | 145 | // 用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天 146 | int iMonth, daysOfMonth = 0; 147 | for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) { 148 | // 闰月 149 | if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) { 150 | --iMonth; 151 | leap = true; 152 | daysOfMonth = leapDays(year); 153 | } else 154 | daysOfMonth = monthDays(year, iMonth); 155 | 156 | offset -= daysOfMonth; 157 | // 解除闰月 158 | if (leap && iMonth == (leapMonth + 1)) 159 | leap = false; 160 | if (!leap) 161 | monCyl++; 162 | } 163 | // offset为0时,并且刚才计算的月份是闰月,要校正 164 | if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) { 165 | if (leap) { 166 | leap = false; 167 | } else { 168 | leap = true; 169 | --iMonth; 170 | --monCyl; 171 | } 172 | } 173 | // offset小于0时,也要校正 174 | if (offset < 0) { 175 | offset += daysOfMonth; 176 | --iMonth; 177 | --monCyl; 178 | } 179 | month = iMonth; 180 | day = offset + 1; 181 | } 182 | 183 | public static String getChinaDayString(int day) { 184 | String chineseTen[] = { "初", "十", "廿", "三" }; 185 | int n = day % 10 == 0 ? 9 : day % 10 - 1; 186 | if (day > 30) 187 | return ""; 188 | if (day == 10) 189 | return "初十"; 190 | else 191 | return chineseTen[day / 10] + chineseNumber[n]; 192 | } 193 | 194 | public String toString() { 195 | return year + "年" + (leap ? "闰" : "") + chineseNumber[month - 1] + "月" 196 | + getChinaDayString(day); 197 | } 198 | 199 | public String toDay() { 200 | return (leap ? "闰" : "") + chineseNumber[month - 1] + "月" 201 | + getChinaDayString(day); 202 | } 203 | 204 | public static String getDay() { 205 | Calendar c = Calendar.getInstance(); 206 | c.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); 207 | Lunar lunar = new Lunar(c); 208 | return lunar.toDay(); 209 | } 210 | 211 | // 测试 212 | // public static void main(String[] args) { 213 | // Calendar c = Calendar.getInstance(); 214 | // c.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); 215 | // c.setTimeInMillis(System.currentTimeMillis()); 216 | // Lunar lunar = new Lunar(c); 217 | // System.out.println(lunar.toString()); 218 | // System.out.println(Lunar.getDay()); 219 | // String lunarStr = ""; 220 | // lunarStr = lunar.animalsYear() + "年("; 221 | // lunarStr += lunar.cyclical() + "年)"; 222 | // lunarStr += lunar.toString(); 223 | // System.out.println(lunarStr);//龙年(壬辰年)2012年八月廿五 224 | // } 225 | } 226 | -------------------------------------------------------------------------------- /Weather/src/com/way/util/NetUtil.java: -------------------------------------------------------------------------------- 1 | package com.way.util; 2 | 3 | import android.content.Context; 4 | import android.net.ConnectivityManager; 5 | import android.net.NetworkInfo.State; 6 | 7 | public class NetUtil { 8 | public static final int NETWORN_NONE = 0; 9 | public static final int NETWORN_WIFI = 1; 10 | public static final int NETWORN_MOBILE = 2; 11 | 12 | public static int getNetworkState(Context context) { 13 | ConnectivityManager connManager = (ConnectivityManager) context 14 | .getSystemService(Context.CONNECTIVITY_SERVICE); 15 | 16 | // Wifi 17 | State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) 18 | .getState(); 19 | if (state == State.CONNECTED || state == State.CONNECTING) { 20 | return NETWORN_WIFI; 21 | } 22 | 23 | // 3G 24 | state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) 25 | .getState(); 26 | if (state == State.CONNECTED || state == State.CONNECTING) { 27 | return NETWORN_MOBILE; 28 | } 29 | return NETWORN_NONE; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Weather/src/com/way/util/SharePreferenceUtil.java: -------------------------------------------------------------------------------- 1 | package com.way.util; 2 | 3 | import android.content.Context; 4 | import android.content.SharedPreferences; 5 | 6 | public class SharePreferenceUtil { 7 | public static final String CITY_SHAREPRE_FILE = "city"; 8 | private static final String CASH_CITY = "_city"; 9 | private static final String SIMPLE_CLIMATE = "simple_climate"; 10 | private static final String SIMPLE_TEMP = "simple_temp"; 11 | private static final String TIMESAMP = "timesamp"; 12 | private static final String TIME = "time"; 13 | private SharedPreferences sp; 14 | private SharedPreferences.Editor editor; 15 | 16 | public SharePreferenceUtil(Context context, String file) { 17 | sp = context.getSharedPreferences(file, Context.MODE_PRIVATE); 18 | editor = sp.edit(); 19 | } 20 | 21 | // city 22 | public void setCity(String city) { 23 | // TODO Auto-generated method stub 24 | editor.putString(CASH_CITY, city); 25 | editor.commit(); 26 | } 27 | 28 | public String getCity() { 29 | return sp.getString(CASH_CITY, ""); 30 | } 31 | 32 | // SimpleClimate 33 | public void setSimpleClimate(String climate) { 34 | editor.putString(SIMPLE_CLIMATE, climate); 35 | editor.commit(); 36 | } 37 | 38 | public String getSimpleClimate() { 39 | return sp.getString(SIMPLE_CLIMATE, "N/A"); 40 | } 41 | 42 | // SimpleTemp 43 | public void setSimpleTemp(String temp) { 44 | editor.putString(SIMPLE_TEMP, temp); 45 | editor.commit(); 46 | } 47 | 48 | public String getSimpleTemp() { 49 | return sp.getString(SIMPLE_TEMP, ""); 50 | } 51 | 52 | // timesamp 53 | public void setTimeSamp(long time) { 54 | editor.putLong(TIMESAMP, time); 55 | editor.commit(); 56 | } 57 | 58 | public long getTimeSamp() { 59 | return sp.getLong(TIMESAMP, System.currentTimeMillis()); 60 | } 61 | 62 | // time 63 | public void setTime(String time) { 64 | editor.putString(TIME, time); 65 | editor.commit(); 66 | } 67 | 68 | public String getTime() { 69 | return sp.getString(TIME, ""); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Weather/src/com/way/util/T.java: -------------------------------------------------------------------------------- 1 | package com.way.util; 2 | 3 | import android.content.Context; 4 | import android.widget.Toast; 5 | 6 | /** 7 | * Toast统一管理类 8 | * 9 | * @author way 10 | * 11 | */ 12 | public class T { 13 | // Toast 14 | private static Toast toast; 15 | 16 | /** 17 | * 短时间显示Toast 18 | * 19 | * @param context 20 | * @param message 21 | */ 22 | public static void showShort(Context context, CharSequence message) { 23 | if (null == toast) { 24 | toast = Toast.makeText(context, message, Toast.LENGTH_SHORT); 25 | // toast.setGravity(Gravity.CENTER, 0, 0); 26 | } else { 27 | toast.setText(message); 28 | } 29 | toast.show(); 30 | } 31 | 32 | /** 33 | * 短时间显示Toast 34 | * 35 | * @param context 36 | * @param message 37 | */ 38 | public static void showShort(Context context, int message) { 39 | if (null == toast) { 40 | toast = Toast.makeText(context, message, Toast.LENGTH_SHORT); 41 | // toast.setGravity(Gravity.CENTER, 0, 0); 42 | } else { 43 | toast.setText(message); 44 | } 45 | toast.show(); 46 | } 47 | 48 | /** 49 | * 长时间显示Toast 50 | * 51 | * @param context 52 | * @param message 53 | */ 54 | public static void showLong(Context context, CharSequence message) { 55 | if (null == toast) { 56 | toast = Toast.makeText(context, message, Toast.LENGTH_LONG); 57 | // toast.setGravity(Gravity.CENTER, 0, 0); 58 | } else { 59 | toast.setText(message); 60 | } 61 | toast.show(); 62 | } 63 | 64 | /** 65 | * 长时间显示Toast 66 | * 67 | * @param context 68 | * @param message 69 | */ 70 | public static void showLong(Context context, int message) { 71 | if (null == toast) { 72 | toast = Toast.makeText(context, message, Toast.LENGTH_LONG); 73 | // toast.setGravity(Gravity.CENTER, 0, 0); 74 | } else { 75 | toast.setText(message); 76 | } 77 | toast.show(); 78 | } 79 | 80 | /** 81 | * 自定义显示Toast时间 82 | * 83 | * @param context 84 | * @param message 85 | * @param duration 86 | */ 87 | public static void show(Context context, CharSequence message, int duration) { 88 | if (null == toast) { 89 | toast = Toast.makeText(context, message, duration); 90 | // toast.setGravity(Gravity.CENTER, 0, 0); 91 | } else { 92 | toast.setText(message); 93 | } 94 | toast.show(); 95 | } 96 | 97 | /** 98 | * 自定义显示Toast时间 99 | * 100 | * @param context 101 | * @param message 102 | * @param duration 103 | */ 104 | public static void show(Context context, int message, int duration) { 105 | if (null == toast) { 106 | toast = Toast.makeText(context, message, duration); 107 | // toast.setGravity(Gravity.CENTER, 0, 0); 108 | } else { 109 | toast.setText(message); 110 | } 111 | toast.show(); 112 | } 113 | 114 | /** Hide the toast, if any. */ 115 | public static void hideToast() { 116 | if (null != toast) { 117 | toast.cancel(); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /Weather/src/com/way/util/TimeUtil.java: -------------------------------------------------------------------------------- 1 | package com.way.util; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | import java.util.TimeZone; 7 | 8 | public class TimeUtil { 9 | private static final String[] WEEK = { "天", "一", "二", "三", "四", "五", "六" }; 10 | public static final String XING_QI = "星期"; 11 | public static final String ZHOU = "周"; 12 | 13 | public static String getWeek(int num, String format) { 14 | final Calendar c = Calendar.getInstance(); 15 | c.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); 16 | int weekNum = c.get(Calendar.DAY_OF_WEEK) + num; 17 | if (weekNum > 7) 18 | weekNum = weekNum - 7; 19 | return format + WEEK[weekNum - 1]; 20 | } 21 | 22 | public static String getZhouWeek() { 23 | SimpleDateFormat format = new SimpleDateFormat("MM/dd"); 24 | return format.format(new Date(System.currentTimeMillis())) + " " 25 | + getWeek(0, ZHOU); 26 | } 27 | 28 | public static String getDay(long timesamp) { 29 | String result = ""; 30 | SimpleDateFormat sdf = new SimpleDateFormat("dd"); 31 | Date today = new Date(System.currentTimeMillis()); 32 | Date otherDay = new Date(timesamp); 33 | int temp = Integer.parseInt(sdf.format(today)) 34 | - Integer.parseInt(sdf.format(otherDay)); 35 | 36 | switch (temp) { 37 | case 0: 38 | result = "今天"; 39 | break; 40 | case 1: 41 | result = "昨天"; 42 | break; 43 | case 2: 44 | result = "前天"; 45 | break; 46 | 47 | default: 48 | result = temp + "天前"; 49 | break; 50 | } 51 | 52 | return result; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Weather/src/com/way/util/ToPinYin.java: -------------------------------------------------------------------------------- 1 | package com.way.util; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | import net.sourceforge.pinyin4j.PinyinHelper; 8 | import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; 9 | import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; 10 | import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; 11 | import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; 12 | import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; 13 | 14 | public class ToPinYin { 15 | 16 | /** 17 | * 将一个list