Java tutorial
/* * Copyright (C) 2014 The Android Open Source Project * * 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 jp.seikeidenron.androidtv.common; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager; import android.graphics.Point; import android.media.MediaMetadataRetriever; import android.os.Build; import android.util.DisplayMetrics; import android.util.Log; import android.view.Display; import android.view.WindowManager; import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URLConnection; import java.util.HashMap; /** * A collection of utility methods, all static. */ public class Utils { private static final String TAG = Utils.class.getSimpleName(); /* * Making sure public utility methods remain static */ private Utils() { } /** * Returns the screen/display size */ public static Point getDisplaySize(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); display.getSize(size); return size; } /** * Shows a (long) toast */ public static void showToast(Context context, String msg) { Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); } /** * Shows a (long) toast. */ public static void showToast(Context context, int resourceId) { Toast.makeText(context, context.getString(resourceId), Toast.LENGTH_LONG).show(); } public static int convertDpToPixel(Context ctx, int dp) { float density = ctx.getResources().getDisplayMetrics().density; return Math.round((float) dp * density); } /** * Formats time in milliseconds to hh:mm:ss string format. */ public static String formatMillis(int millis) { String result = ""; int hr = millis / 3600000; millis %= 3600000; int min = millis / 60000; millis %= 60000; int sec = millis / 1000; if (hr > 0) { result += hr + ":"; } if (min >= 0) { if (min > 9) { result += min + ":"; } else { result += "0" + min + ":"; } } if (sec > 9) { result += sec; } else { result += "0" + sec; } return result; } public static long getDuration(String videoUrl) { MediaMetadataRetriever mmr = new MediaMetadataRetriever(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { mmr.setDataSource(videoUrl, new HashMap<String, String>()); } else { mmr.setDataSource(videoUrl); } return Long.parseLong(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)); } public static boolean hasPermission(final Context context, final String permission) { return PackageManager.PERMISSION_GRANTED == context.getPackageManager().checkPermission(permission, context.getPackageName()); } /*--- Network APIs ---*/ public static JSONObject parseUrl(String urlString) { Log.v(TAG, "Parse URL: " + urlString); BufferedReader reader = null; try { java.net.URL url = new java.net.URL(urlString); URLConnection urlConnection = url.openConnection(); reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); //urlConnection.getInputStream(), "iso-8859-1")); return convertBufferedReaderToJSONObject(reader); } catch (Exception e) { Log.w(TAG, "Failed to parse the json for media list", e); return null; } finally { if (null != reader) { try { reader.close(); } catch (IOException e) { Log.w(TAG, "JSON feed closed", e); } } } } public static JSONObject convertBufferedReaderToJSONObject(BufferedReader reader) throws IOException, JSONException { StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } String json = sb.toString(); return new JSONObject(json); } public static int getDisplayWidthInPx(Activity activity) { DisplayMetrics metrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); return metrics.widthPixels; } public static int getDisplayHeightInPx(Activity activity) { DisplayMetrics metrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); return metrics.heightPixels; } }