Java tutorial
/* * Copyright (c) 2017 James Oliver * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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.jameswolfeoliver.pigeon.Utilities; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.content.ContextCompat; import java.util.Arrays; public class Utils { public static int convertToPixels(double sizeInDp) { float scale = PigeonApplication.getAppContext().getResources().getDisplayMetrics().density; return (int) (sizeInDp * scale + 0.5f); } public static float convertToDp(int sizeInPixels) { int densityDpi = PigeonApplication.getAppContext().getResources().getDisplayMetrics().densityDpi; return sizeInPixels / (densityDpi / 160f); } public static float screenWidthInPixels() { return PigeonApplication.getAppContext().getResources().getDisplayMetrics().widthPixels; } public static float screenHeightInPixels() { return PigeonApplication.getAppContext().getResources().getDisplayMetrics().heightPixels; } public static boolean isStringNullOrEmpty(String s) { return s == null || s.isEmpty(); } public static String intentToString(Intent intent) { if (intent == null) { return null; } return intent.toString() + " " + bundleToString(intent.getExtras()); } public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } public static String bundleToString(Bundle bundle) { StringBuilder out = new StringBuilder("Bundle["); if (bundle == null) { out.append("null"); } else { boolean first = true; for (String key : bundle.keySet()) { if (!first) { out.append(", "); } out.append(key).append('='); Object value = bundle.get(key); if (value instanceof int[]) { out.append(Arrays.toString((int[]) value)); } else if (value instanceof byte[]) { out.append(Arrays.toString((byte[]) value)); } else if (value instanceof boolean[]) { out.append(Arrays.toString((boolean[]) value)); } else if (value instanceof short[]) { out.append(Arrays.toString((short[]) value)); } else if (value instanceof long[]) { out.append(Arrays.toString((long[]) value)); } else if (value instanceof float[]) { out.append(Arrays.toString((float[]) value)); } else if (value instanceof double[]) { out.append(Arrays.toString((double[]) value)); } else if (value instanceof String[]) { out.append(Arrays.toString((String[]) value)); } else if (value instanceof CharSequence[]) { out.append(Arrays.toString((CharSequence[]) value)); } else if (value instanceof Parcelable[]) { out.append(Arrays.toString((Parcelable[]) value)); } else if (value instanceof Bundle) { out.append(bundleToString((Bundle) value)); } else { out.append(value); } first = false; } } out.append("]"); return out.toString(); } }