Java tutorial
/* * Copyright (C) 2013 Lee Hong (http://blog.csdn.net/leehong2005) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.lee.sdk.utils; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.List; import java.util.regex.Matcher; import java.util.zip.GZIPInputStream; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.HttpConnectionParams; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.Intent.ShortcutIconResource; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.database.Cursor; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.Environment; import android.os.storage.StorageManager; import android.text.TextUtils; import android.util.DisplayMetrics; import android.util.Log; import android.util.Patterns; import android.view.View; import android.view.inputmethod.InputMethodManager; import com.lee.sdk.Configuration; /** * * @author lihong06 * @since 2013-7-9 */ public class Utils { private static final boolean DEBUG = Configuration.DEBUG; private static final String TAG = "Utils"; /** Stream buffer size. */ public static final int STREAM_BUFFER_SIZE = 8192; /** * ???? * * @param activity ?Activity??? * @param nameId ???? * @param iconId ?? * @param appendFlags ????IntentFlag */ public static void addShortcut(Activity activity, int nameId, int iconId, int appendFlags) { Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); // ???? shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getString(nameId)); shortcut.putExtra("duplicate", false); // ???? // ?Activity??? ComponentName comp = new ComponentName(activity.getPackageName(), activity.getClass().getName()); Intent intent = new Intent(Intent.ACTION_MAIN).setComponent(comp); if (appendFlags != 0) { intent.addFlags(appendFlags); } shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); // ?? ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(activity, iconId); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); activity.sendBroadcast(shortcut); } /** * ????????? * * @param context {@link Context} * @param shortcutName ?? * @param packageName ?? * * @return ???DB?true */ public static boolean hasShortcut(Context context, String shortcutName, String packageName) { boolean res = true; if (context != null && !TextUtils.isEmpty(shortcutName) && !TextUtils.isEmpty(packageName)) { Uri uri = getShortcutUri(); res = hasShortcut(context, shortcutName, packageName, uri); } return res; } /** * ????????? * * @param context {@link Context} * @param shortcutName ?? * @param packageName ?? * @param uri {@link Uri} * * @return ???DB?true */ private static boolean hasShortcut(Context context, String shortcutName, String packageName, Uri uri) { if (context == null || TextUtils.isEmpty(shortcutName) || TextUtils.isEmpty(packageName) || uri == null) { return true; } boolean res = false; Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, new String[] { "title", "intent" }, "title=?", new String[] { shortcutName }, null); if (cursor != null && cursor.moveToFirst()) { int index = cursor.getColumnIndex("intent"); if (index >= 0 && index < cursor.getColumnCount()) { do { String intentString = cursor.getString(index); if (intentString != null && intentString.contains(packageName)) { res = true; break; } } while (cursor.moveToNext()); } } } catch (Exception e) { res = true; } finally { if (cursor != null) { cursor.close(); cursor = null; } } return res; } /** * ??URI * * @return URI */ private static Uri getShortcutUri() { String authority = "com.android.launcher.settings"; if (APIUtils.hasFroyo()) { authority = "com.android.launcher2.settings"; } final Uri contentUri = Uri.parse("content://" + authority + "/favorites?notify=true"); return contentUri; } /** * Hides the input method. * * @param context context * @param view The currently focused view * @return success or not. */ public static boolean hideInputMethod(Context context, View view) { if (context == null || view == null) { return false; } InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { return imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } return false; } /** * Show the input method. * * @param context context * @param view The currently focused view, which would like to receive soft keyboard input * @return success or not. */ public static boolean showInputMethod(Context context, View view) { if (context == null || view == null) { return false; } InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { return imm.showSoftInput(view, 0); } return false; } public static float pixelToDp(Context context, float val) { float density = context.getResources().getDisplayMetrics().density; return val * density; } public static String getHashedFileName(String url) { if (url == null || url.endsWith("/")) { return null; } String suffix = getSuffix(url); StringBuilder sb = null; try { MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] dstbytes = digest.digest(url.getBytes("UTF-8")); // GMaFroid uses UTF-16LE sb = new StringBuilder(); for (int i = 0; i < dstbytes.length; i++) { sb.append(Integer.toHexString(dstbytes[i] & 0xff)); } } catch (Exception e) { e.printStackTrace(); } if (null != sb && null != suffix) { return sb.toString() + "." + suffix; } return null; } private static String getSuffix(String fileName) { int dot_point = fileName.lastIndexOf("."); int sl_point = fileName.lastIndexOf("/"); if (dot_point < sl_point) { return ""; } if (dot_point != -1) { return fileName.substring(dot_point + 1); } return null; } /** * Indicates whether the specified action can be used as an intent. This method queries the * package manager for installed packages that can respond to an intent with the specified * action. If no suitable package is found, this method returns false. * * @param context The application's environment. * @param intent The Intent action to check for availability. * * @return True if an Intent with the specified action can be sent and responded to, false * otherwise. */ public static boolean isIntentAvailable(Context context, Intent intent) { final PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0; } /** * DisplayMetrics */ private static DisplayMetrics sDisplayMetrics; /** * * * @param context Context * * @return */ public static int getDisplayWidth(Context context) { initDisplayMetrics(context); return sDisplayMetrics.widthPixels; } /** * * * @param context Context * * @return */ public static int getDisplayHeight(Context context) { initDisplayMetrics(context); return sDisplayMetrics.heightPixels; } /** * * * @param context Context * * @return */ public static float getDensity(Context context) { initDisplayMetrics(context); return sDisplayMetrics.density; } /** * DPI * * @param context Context * * @return DPI */ public static int getDensityDpi(Context context) { initDisplayMetrics(context); return sDisplayMetrics.densityDpi; } /** * ?DisplayMetrics * * @param context Context */ private static void initDisplayMetrics(Context context) { if (null == sDisplayMetrics) { if (null != context) { sDisplayMetrics = context.getResources().getDisplayMetrics(); } } } /** * ??url * * @param query String * @return true: ?url */ public static boolean isUrl(String query) { Matcher matcher = Patterns.WEB_URL.matcher(query); if (matcher.matches()) { return true; } return false; } /** * ??( * * @param context context * @return ? true */ public static boolean isNetworkConnected(Context context) { NetworkInfo networkInfo = getActiveNetworkInfo(context); // return networkInfo != null && networkInfo.isConnected(); boolean flag = networkInfo != null && networkInfo.isAvailable(); return flag; } /** * ? * * @param context context * @return ? */ private static NetworkInfo getActiveNetworkInfo(Context context) { if (context == null) { return null; } ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return null; } return connectivity.getActiveNetworkInfo(); } /** * A hashing method that changes a string (like a URL) into a hash suitable for using as a disk * filename. */ public static String hashKeyForDisk(String key) { String cacheKey; try { final MessageDigest mDigest = MessageDigest.getInstance("MD5"); mDigest.update(key.getBytes()); cacheKey = bytesToHexString(mDigest.digest()); } catch (NoSuchAlgorithmException e) { cacheKey = String.valueOf(key.hashCode()); } return cacheKey; } private static String bytesToHexString(byte[] bytes) { // http://stackoverflow.com/questions/332079 StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(0xFF & bytes[i]); if (hex.length() == 1) { sb.append('0'); } sb.append(hex); } return sb.toString(); } /** * ??volume * * @param volume * @return */ public static String getVolumePath(Object volume) { String result = ""; Object o = invokeHideMethodForObject(volume, "getPath", null, null); if (o != null) { result = (String) o; } return result; } /** * ?volume * * @param context context * @return Volume */ public static Object[] getVolumeList(Context context) { StorageManager manager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Object[] result = null; Object o = invokeHideMethodForObject(manager, "getVolumeList", null, null); if (o != null) { result = (Object[]) o; } return result; } /** * ??volume? * * @param context context * @param volumePath volumePath * @return result */ public static String getVolumeState(Context context, String volumePath) { StorageManager manager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); String result = ""; Object o = invokeHideMethodForObject(manager, "getVolumeState", new Class[] { String.class }, new Object[] { volumePath }); if (o != null) { result = (String) o; } return result; } /** * invoke object's method including private method * * @param owner : target object * @param methodName : name of the target method * @param parameterTypes : types of the target method's parameters * @param parameters : parameters of the target method * @return result of invoked method * @throws NoSuchMethodException NoSuchMethodException * @throws IllegalArgumentException IllegalArgumentException * @throws IllegalAccessException IllegalAccessException * @throws InvocationTargetException InvocationTargetException */ public static Object invokePublicMethod(Object owner, String methodName, Class<?>[] parameterTypes, Object[] parameters) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { if (null == owner) { return null; } // ?public Method method = owner.getClass().getMethod(methodName, parameterTypes); Object result = method.invoke(owner, parameters); return result; } /** * ?? * * @param obj . * @param methodName ?? * @param types ? * @param args ? * @return ?? */ public static Object invokeHideMethodForObject(Object obj, String methodName, Class<?>[] types, Object[] args) { Object o = null; try { Class<?> cls; if (obj instanceof Class<?>) { // ?? cls = (Class<?>) obj; } else { // ??? cls = obj.getClass(); } Method method = cls.getMethod(methodName, types); o = method.invoke(obj, args); if (Configuration.DEBUG) { Log.d("Utils", "Method \"" + methodName + "\" invoked success!"); } } catch (Exception e) { if (Configuration.DEBUG) { Log.d("Utils", "Method \"" + methodName + "\" invoked failed: " + e.getMessage()); } } return o; } /** * ?? * * ?? * * @return true:?; false ?/mounted/?? */ public static boolean isExternalStorageWriteable() { boolean writealbe = false; long start = System.currentTimeMillis(); if (TextUtils.equals(Environment.MEDIA_MOUNTED, Environment.getExternalStorageState())) { File esd = Environment.getExternalStorageDirectory(); if (esd.exists() && esd.canWrite()) { File file = new File(esd, ".696E5309-E4A7-27C0-A787-0B2CEBF1F1AB"); if (file.exists()) { writealbe = true; } else { try { writealbe = file.createNewFile(); } catch (IOException e) { if (DEBUG) { Log.w(TAG, "isExternalStorageWriteable() can't create test file."); } } } } } long end = System.currentTimeMillis(); if (DEBUG) { Log.i(TAG, "Utility.isExternalStorageWriteable(" + writealbe + ") cost " + (end - start) + "ms."); } return writealbe; } /** * component?? * * @param ctx context * @param className class name * @return ?? */ public static boolean isComponentEnable(Context ctx, String className) { PackageManager pm = ctx.getPackageManager(); ComponentName cn = new ComponentName(ctx.getPackageName(), className); int ret = pm.getComponentEnabledSetting(cn); if (ret == PackageManager.COMPONENT_ENABLED_STATE_ENABLED || ret == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { return true; } return false; } /** * ?????.????. * * @param r Runnable * @param name ?? * @return Thread */ public static Thread newThread(Runnable r, String name) { if (TextUtils.isEmpty(name)) { throw new RuntimeException("thread name should not be empty"); } return new Thread(r, getStandardThreadName(name)); } /** * ??? * * @param name ?? * @return ??? */ public static String getStandardThreadName(String name) { if (name != null) { final String PREFIX = "THREAD_"; if (!name.startsWith(PREFIX)) { return PREFIX + name; } } return name; } /** * * * @param context Context Object * @param file ?? * @param data ??? * @param mode ? * @return ??? */ public static boolean cache(Context context, String file, String data, int mode) { return cache(context, file, data.getBytes(), mode); } /** * * * @param context Context Object * @param file ?? * @param data ??? * @param mode ? * @return ??? */ public static boolean cache(Context context, String file, byte[] data, int mode) { boolean bResult = false; if (null == data) { data = new byte[0]; } FileOutputStream fos = null; try { fos = context.openFileOutput(file, mode); fos.write(data); fos.flush(); bResult = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != fos) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } return bResult; } /** * ???data/data/package-name/files * * @param context context * @param name ????? * @return true?false */ public static boolean deleteCache(Context context, String name) { boolean succeed = false; try { succeed = context.deleteFile(name); } catch (Exception e) { e.printStackTrace(); } return succeed; } /** * ? * * @param inputStream ? * @param file */ public static void saveToFile(InputStream inputStream, File file) { OutputStream outputStream = null; try { outputStream = new FileOutputStream(file); copyStream(inputStream, outputStream); } catch (FileNotFoundException e) { if (DEBUG) { Log.d(TAG, "catch FileNotFoundException"); } e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { if (DEBUG) { Log.d(TAG, "catch IOException"); } e.printStackTrace(); } } } } /** * ?? * * @param is ? * @param os ? */ private static void copyStream(InputStream is, OutputStream os) { final int BUFFER_SIZE = 1024; try { byte[] bytes = new byte[BUFFER_SIZE]; for (;;) { int count = is.read(bytes, 0, BUFFER_SIZE); if (count == -1) { break; } os.write(bytes, 0, count); } } catch (IOException e) { if (DEBUG) { Log.d(TAG, "copyStream: catch IOException"); } e.printStackTrace(); } } /** * . * * @param closeable Closeable. */ public static void closeSafely(Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (Exception e) { e.printStackTrace(); } } /** * stream to bytes * * @param is inputstream * @return bytes */ public static byte[] streamToBytes(InputStream is) { if (null == is) { return null; } ByteArrayOutputStream output = new ByteArrayOutputStream(); try { byte[] buffer = new byte[STREAM_BUFFER_SIZE]; int n = 0; while (-1 != (n = is.read(buffer))) { output.write(buffer, 0, n); } } catch (Exception e) { e.printStackTrace(); } finally { closeSafely(is); } return output.toByteArray(); } /** * ?Stream?string * * @param is Stream? * @return String */ public static String streamToString(InputStream is) { return streamToString(is, "UTF-8"); } /** * ???Stream?string * * @param is Stream? * @param enc ?? * @return String */ public static String streamToString(InputStream is, String enc) { if (null == is) { return null; } StringBuilder buffer = new StringBuilder(); String line = null; try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, enc), STREAM_BUFFER_SIZE); while (null != (line = reader.readLine())) { buffer.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { closeSafely(is); } return buffer.toString(); } /** * ??? * * @param is ? * @param file * @return true:??false:? */ public static boolean streamToFile(InputStream is, File file) { boolean bRet = false; if (null == is || null == file) { return bRet; } // ? File dir = file.getParentFile(); if (!dir.exists()) { dir.mkdirs(); } if (file.exists()) { file.delete(); } FileOutputStream fos = null; try { fos = new FileOutputStream(file); byte[] buffer = new byte[STREAM_BUFFER_SIZE]; int length = -1; while ((length = is.read(buffer)) != -1) { fos.write(buffer, 0, length); } fos.flush(); bRet = true; } catch (Exception e) { e.printStackTrace(); } finally { closeSafely(fos); closeSafely(is); } return bRet; } /** * http client * * @param context * Context. * @return ProxyHttpClient */ public static DefaultHttpClient createHttpClient(Context context) { DefaultHttpClient httpclient = new DefaultHttpClient(); // httpclient.getParams().setParameter("Accept-Encoding", "gzip"); final int httpTimeout = 30000; final int socketTimeout = 50000; HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), httpTimeout); HttpConnectionParams.setSoTimeout(httpclient.getParams(), socketTimeout); return httpclient; } /** * ?server?Content-Encoding?inputstream.content-encodinggzipGzipInputStream * ?inputStream * * @param resEntity * {@link HttpEntity} * @return InputStream or null * @throws IOException * {@link IOException} */ public static InputStream getSuitableInputStream(HttpEntity resEntity) throws IOException { if (resEntity == null) { return null; } InputStream inputStream = resEntity.getContent(); if (inputStream != null) { Header header = resEntity.getContentEncoding(); if (header != null) { String contentEncoding = header.getValue(); if (!TextUtils.isEmpty(contentEncoding) && contentEncoding.toLowerCase().indexOf("gzip") > -1) { inputStream = new GZIPInputStream(inputStream); } } } return inputStream; } /** * URL???URL? * * @param url URL * @return ?MD5? */ public static String getHashedString(String url) { return getHashedString(url, true); } /** * URL???URL? * * @param url URL * @param appendSuffix ?URL?? * @return ?MD5? */ public static String getHashedString(String url, boolean appendSuffix) { if (url == null || url.endsWith("/")) { return null; } String suffix = appendSuffix ? getSuffix(url) : null; StringBuilder sb = null; try { MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] dstbytes = digest.digest(url.getBytes("UTF-8")); // GMaFroid uses UTF-16LE sb = new StringBuilder(); for (int i = 0; i < dstbytes.length; i++) { sb.append(Integer.toHexString(dstbytes[i] & 0xff));// SUPPRESS CHECKSTYLE } } catch (Exception e) { e.printStackTrace(); } if (null != sb && null != suffix) { return sb.toString() + "." + suffix; } return (null != sb) ? sb.toString() : null; } }