Java tutorial
/* * Copyright (c) 2016. Anoopam Mission. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at "http://www.gnu.org/licenses/licenses.html" for more details. * */ package org.anoopam.main.anoopamaudio; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.ParcelFileDescriptor; import org.anoopam.main.R; import org.anoopam.main.common.AMConstants; import org.anoopam.ext.smart.caching.SmartCaching; import org.anoopam.ext.smart.framework.SharedPreferenceConstants; import org.json.JSONArray; import org.json.JSONObject; import java.io.FileDescriptor; import java.util.ArrayList; public class UtilFunctions implements SharedPreferenceConstants { static String LOG_CLASS = "UtilFunctions"; /** * Check if service is running or not * @param serviceName * @param context * @return */ public static boolean isServiceRunning(String serviceName, Context context) { ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (serviceName.equals(service.service.getClassName())) { return true; } } return false; } /** * Read the songs present in external storage * @param context * @return */ public static ArrayList<ContentValues> listOfSongs(Context context) { SharedPreferences sharedPreferences = context.getSharedPreferences("Anoopam Mission", Context.MODE_PRIVATE); try { JSONObject data = new JSONObject(sharedPreferences.getString(AMConstants.KEY_CURRENT_AUDIO_LIST, "")); JSONArray audios = data.getJSONArray("audios"); if (audios != null) { ArrayList<ContentValues> audioList = new SmartCaching(context).parseResponse(audios, "audios") .get("audios"); return audioList; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * Get the album image from albumId * @param context * @param album_id * @return */ public static Bitmap getAlbumart(Context context, Long album_id) { Bitmap bm = null; BitmapFactory.Options options = new BitmapFactory.Options(); try { final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id); ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r"); if (pfd != null) { FileDescriptor fd = pfd.getFileDescriptor(); bm = BitmapFactory.decodeFileDescriptor(fd, null, options); pfd = null; fd = null; } } catch (Error ee) { } catch (Exception e) { } return bm; } /** * @param context * @return */ public static Bitmap getDefaultAlbumArt(Context context) { Bitmap bm = null; BitmapFactory.Options options = new BitmapFactory.Options(); try { bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.logo, options); } catch (Error ee) { } catch (Exception e) { } return bm; } /** * Convert milliseconds into time hh:mm:ss * @param milliseconds * @return time in String */ public static String getDuration(long milliseconds) { long sec = (milliseconds / 1000) % 60; long min = (milliseconds / (60 * 1000)) % 60; long hour = milliseconds / (60 * 60 * 1000); String s = (sec < 10) ? "0" + sec : "" + sec; String m = (min < 10) ? "0" + min : "" + min; String h = "" + hour; String time = ""; if (hour > 0) { time = h + ":" + m + ":" + s; } else { time = m + ":" + s; } return time; } public static boolean currentVersionSupportBigNotification() { int sdkVersion = android.os.Build.VERSION.SDK_INT; if (sdkVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN) { return true; } return false; } public static boolean currentVersionSupportLockScreenControls() { int sdkVersion = android.os.Build.VERSION.SDK_INT; if (sdkVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { return true; } return false; } }