Renamed package to gramophone
This commit is contained in:
parent
200c50babf
commit
c28a75c61a
95 changed files with 412 additions and 415 deletions
|
|
@ -0,0 +1,92 @@
|
|||
package com.kabouzeid.gramophone.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.kabouzeid.gramophone.R;
|
||||
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
|
||||
import com.nostra13.universalimageloader.core.assist.FailReason;
|
||||
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
|
||||
import com.nostra13.universalimageloader.utils.L;
|
||||
|
||||
/**
|
||||
* Created by karim on 28.12.14.
|
||||
*/
|
||||
public class ImageLoaderUtil {
|
||||
public static void initImageLoader(Context context) {
|
||||
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
|
||||
.cacheInMemory(true)
|
||||
.cacheOnDisk(false)
|
||||
.build();
|
||||
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
|
||||
.defaultDisplayImageOptions(defaultOptions)
|
||||
//.memoryCache(new LRULimitedMemoryCache(1024*1024*CACHE_SIZE_MB))
|
||||
.build();
|
||||
ImageLoader.getInstance().init(config);
|
||||
|
||||
L.writeLogs(false);
|
||||
}
|
||||
|
||||
public static DisplayImageOptions getCacheOnDiskOptions() {
|
||||
return new DisplayImageOptions.Builder()
|
||||
.cacheInMemory(true)
|
||||
.cacheOnDisk(true)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static DisplayImageOptions getCacheInMemoryOptions() {
|
||||
return new DisplayImageOptions.Builder()
|
||||
.cacheInMemory(true)
|
||||
.cacheOnDisk(false)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static class defaultAlbumArtOnFailed implements ImageLoadingListener {
|
||||
@Override
|
||||
public void onLoadingStarted(String imageUri, View view) {
|
||||
if (view != null) ((ImageView) view).setImageResource(R.drawable.default_album_art);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||
if (view != null) ((ImageView) view).setImageResource(R.drawable.default_album_art);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingCancelled(String imageUri, View view) {
|
||||
if (view != null) ((ImageView) view).setImageResource(R.drawable.default_album_art);
|
||||
}
|
||||
}
|
||||
|
||||
public static class defaultArtistArtOnFailed implements ImageLoadingListener {
|
||||
|
||||
@Override
|
||||
public void onLoadingStarted(String imageUri, View view) {
|
||||
if (view != null) ((ImageView) view).setImageResource(R.drawable.default_artist_image);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||
if (view != null) ((ImageView) view).setImageResource(R.drawable.default_artist_image);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingCancelled(String imageUri, View view) {
|
||||
if (view != null) ((ImageView) view).setImageResource(R.drawable.default_artist_image);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
package com.kabouzeid.gramophone.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
/**
|
||||
* Created by karim on 22.12.14.
|
||||
*/
|
||||
public final class InternalStorageUtil {
|
||||
private static final String TAG = InternalStorageUtil.class.getSimpleName();
|
||||
|
||||
public static synchronized void writeObject(final Context context, final String key, final Object object) throws IOException {
|
||||
try {
|
||||
String tempFileName = "TEMP_" + key;
|
||||
FileOutputStream fos;
|
||||
fos = context.openFileOutput(tempFileName, Context.MODE_PRIVATE);
|
||||
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
||||
oos.writeObject(object);
|
||||
oos.close();
|
||||
fos.close();
|
||||
renameAppFile(context, tempFileName, key);
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Writing Object to internal storage failed! Maybe the Object is not serializable?", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void renameAppFile(final Context context, String originalFileName, String newFileName) {
|
||||
File originalFile = context.getFileStreamPath(originalFileName);
|
||||
File newFile = new File(originalFile.getParent(), newFileName);
|
||||
if (newFile.exists()) {
|
||||
context.deleteFile(newFileName);
|
||||
}
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
originalFile.renameTo(newFile);
|
||||
}
|
||||
|
||||
public static synchronized Object readObject(final Context context, String key) throws IOException,
|
||||
ClassNotFoundException {
|
||||
FileInputStream fis = context.openFileInput(key);
|
||||
ObjectInputStream ois = new ObjectInputStream(fis);
|
||||
return ois.readObject();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
package com.kabouzeid.gramophone.util;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentUris;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by karim on 29.12.14.
|
||||
*/
|
||||
public class MusicUtil {
|
||||
public static final String TAG = MusicUtil.class.getSimpleName();
|
||||
|
||||
public static Uri getAlbumArtUri(int album_id) {
|
||||
final Uri sArtworkUri = Uri
|
||||
.parse("content://media/external/audio/albumart");
|
||||
|
||||
return ContentUris.withAppendedId(sArtworkUri, album_id);
|
||||
}
|
||||
|
||||
public static String getReadableDurationString(long songDurationMillis) {
|
||||
long minutes = (songDurationMillis / 1000) / 60;
|
||||
long seconds = (songDurationMillis / 1000) % 60;
|
||||
return String.format("%02d:%02d", minutes, seconds);
|
||||
}
|
||||
|
||||
|
||||
//iTunes uses for example 1002 for track 2 CD1 or 3011 for track 11 CD3.
|
||||
//this method converts those values to normal tracknumbers
|
||||
public static int getFixedTrackNumber(int trackNumberToFix) {
|
||||
return trackNumberToFix % 1000;
|
||||
}
|
||||
|
||||
public static void insertAlbumArt(Context context, int albumId, String path) {
|
||||
ContentResolver contentResolver = context.getContentResolver();
|
||||
|
||||
Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
|
||||
contentResolver.delete(ContentUris.withAppendedId(artworkUri, albumId), null, null);
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("album_id", albumId);
|
||||
values.put("_data", path);
|
||||
|
||||
contentResolver.insert(artworkUri, values);
|
||||
}
|
||||
|
||||
public static void deleteAlbumArt(Context context, int albumId) {
|
||||
ContentResolver contentResolver = context.getContentResolver();
|
||||
Uri localUri = Uri.parse("content://media/external/audio/albumart");
|
||||
contentResolver.delete(ContentUris.withAppendedId(localUri, albumId), null, null);
|
||||
}
|
||||
|
||||
public static File getAlbumArtFile(Context context, String name)
|
||||
throws IOException {
|
||||
return new File(createAlbumArtDir(context), name + System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static File createAlbumArtDir(Context paramContext) {
|
||||
File albumArtDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "/.albumart/");
|
||||
if (!albumArtDir.exists()) {
|
||||
albumArtDir.mkdirs();
|
||||
try {
|
||||
new File(albumArtDir, ".nomedia").createNewFile();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "error while creating .nomedia file", e);
|
||||
}
|
||||
}
|
||||
return albumArtDir;
|
||||
}
|
||||
}
|
||||
203
app/src/main/java/com/kabouzeid/gramophone/util/Util.java
Normal file
203
app/src/main/java/com/kabouzeid/gramophone/util/Util.java
Normal file
|
|
@ -0,0 +1,203 @@
|
|||
package com.kabouzeid.gramophone.util;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.TypedValue;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
|
||||
import com.kabouzeid.gramophone.App;
|
||||
import com.kabouzeid.gramophone.R;
|
||||
import com.kabouzeid.gramophone.misc.AppKeys;
|
||||
|
||||
/**
|
||||
* Created by karim on 12.12.14.
|
||||
*/
|
||||
public class Util {
|
||||
private static int albumArtSize = 600;
|
||||
|
||||
public static int resolveDrawable(Context context, int drawable) {
|
||||
TypedArray a = context.obtainStyledAttributes(new int[]{drawable});
|
||||
int resId = a.getResourceId(0, 0);
|
||||
a.recycle();
|
||||
return resId;
|
||||
}
|
||||
|
||||
public static int resolveColor(Context context, int color) {
|
||||
TypedArray a = context.obtainStyledAttributes(new int[]{color});
|
||||
int resId = a.getColor(0, context.getResources().getColor(R.color.materialmusic_color));
|
||||
a.recycle();
|
||||
return resId;
|
||||
}
|
||||
|
||||
public static boolean isWindowTranslucent(Context context) {
|
||||
TypedArray a = context.obtainStyledAttributes(new int[]{android.R.attr.windowTranslucentStatus});
|
||||
boolean result = a.getBoolean(0, false);
|
||||
a.recycle();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int getActionBarSize(Context context) {
|
||||
TypedValue typedValue = new TypedValue();
|
||||
int[] textSizeAttr = new int[]{R.attr.actionBarSize};
|
||||
int indexOfAttrTextSize = 0;
|
||||
TypedArray a = context.obtainStyledAttributes(typedValue.data, textSizeAttr);
|
||||
int actionBarSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
|
||||
a.recycle();
|
||||
return actionBarSize;
|
||||
}
|
||||
|
||||
public static int getStatusBarHeight(Context context) {
|
||||
int result = 0;
|
||||
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
|
||||
if (resourceId > 0) {
|
||||
result = context.getResources().getDimensionPixelSize(resourceId);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int getNavigationBarHeight(Context context) {
|
||||
int result = 0;
|
||||
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
|
||||
if (resourceId > 0) {
|
||||
result = context.getResources().getDimensionPixelSize(resourceId);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@TargetApi(19)
|
||||
public static void setNavBarTranslucent(Window window, boolean translucent) {
|
||||
if (translucent) {
|
||||
window.setFlags(
|
||||
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
|
||||
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
|
||||
return;
|
||||
}
|
||||
|
||||
final WindowManager.LayoutParams attrs = window
|
||||
.getAttributes();
|
||||
attrs.flags &= (~WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
|
||||
window.setAttributes(attrs);
|
||||
window.clearFlags(
|
||||
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
|
||||
}
|
||||
|
||||
@TargetApi(19)
|
||||
public static void setStatusBarTranslucent(Window window, boolean translucent) {
|
||||
if (translucent) {
|
||||
window.setFlags(
|
||||
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
|
||||
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
||||
return;
|
||||
}
|
||||
|
||||
final WindowManager.LayoutParams attrs = window
|
||||
.getAttributes();
|
||||
attrs.flags &= (~WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
||||
window.setAttributes(attrs);
|
||||
window.clearFlags(
|
||||
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
|
||||
}
|
||||
|
||||
public static final boolean isOnline(final Context context) {
|
||||
if (context == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean state = false;
|
||||
final boolean onlyOnWifi = ((App) context.getApplicationContext()).getDefaultSharedPreferences().getBoolean(AppKeys.SP_ONLY_ON_WIFI, true);
|
||||
|
||||
/* Monitor network connections */
|
||||
final ConnectivityManager connectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
/* Wi-Fi connection */
|
||||
final NetworkInfo wifiNetwork = connectivityManager
|
||||
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||
if (wifiNetwork != null) {
|
||||
state = wifiNetwork.isConnectedOrConnecting();
|
||||
}
|
||||
|
||||
/* Mobile data connection */
|
||||
final NetworkInfo mbobileNetwork = connectivityManager
|
||||
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
||||
if (mbobileNetwork != null) {
|
||||
if (!onlyOnWifi) {
|
||||
state = mbobileNetwork.isConnectedOrConnecting();
|
||||
}
|
||||
}
|
||||
|
||||
/* Other networks */
|
||||
final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
|
||||
if (activeNetwork != null) {
|
||||
if (!onlyOnWifi) {
|
||||
state = activeNetwork.isConnectedOrConnecting();
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
public static String getFileSizeString(long sizeInBytes) {
|
||||
long fileSizeInKB = sizeInBytes / 1024;
|
||||
long fileSizeInMB = fileSizeInKB / 1024;
|
||||
return fileSizeInMB + " MB";
|
||||
}
|
||||
|
||||
public static String getFilePathFromContentProviderUri(Context context, Uri uri) {
|
||||
String path = "";
|
||||
String[] projection = {MediaStore.MediaColumns.DATA};
|
||||
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
|
||||
if (cursor == null) return null;
|
||||
int column_index = cursor.getColumnIndexOrThrow(projection[0]);
|
||||
if (cursor.moveToFirst()) {
|
||||
path = cursor.getString(column_index);
|
||||
}
|
||||
cursor.close();
|
||||
return path;
|
||||
}
|
||||
|
||||
public static Bitmap getAlbumArtScaledBitmap(final Bitmap bitmap, boolean keepAspectRatio) {
|
||||
if (keepAspectRatio) {
|
||||
double aspectRatio = (double) bitmap.getHeight() / (double) bitmap.getWidth();
|
||||
int targetWidth = albumArtSize;
|
||||
int targetHeight = (int) (targetWidth * aspectRatio);
|
||||
return Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, false);
|
||||
} else {
|
||||
return getScaledBitmap(bitmap);
|
||||
}
|
||||
}
|
||||
|
||||
private static Bitmap getScaledBitmap(final Bitmap bitmap) {
|
||||
return Bitmap.createScaledBitmap(bitmap, albumArtSize, albumArtSize, false);
|
||||
}
|
||||
|
||||
public static void hideSoftKeyboard(Activity activity) {
|
||||
if (activity != null) {
|
||||
View currentFocus = activity.getCurrentFocus();
|
||||
if (currentFocus != null) {
|
||||
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
|
||||
inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean hasLollipopSDK() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
|
||||
}
|
||||
|
||||
public static boolean hasKitKatSDK() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
package com.kabouzeid.gramophone.util;
|
||||
|
||||
import android.animation.ArgbEvaluator;
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.os.Build;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewTreeObserver;
|
||||
import android.view.animation.PathInterpolator;
|
||||
import android.widget.ListAdapter;
|
||||
import android.widget.ListView;
|
||||
|
||||
/**
|
||||
* Created by karim on 06.12.14.
|
||||
*/
|
||||
public class ViewUtil {
|
||||
public final static int DEFAULT_COLOR_ANIMATION_DURATION = 1000;
|
||||
|
||||
public static void disableViews(ViewGroup layout) {
|
||||
for (int i = 0; i < layout.getChildCount(); i++) {
|
||||
View child = layout.getChildAt(i);
|
||||
if (child instanceof ViewGroup) {
|
||||
disableViews((ViewGroup) child);
|
||||
} else {
|
||||
child.setEnabled(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void enableViews(ViewGroup layout) {
|
||||
for (int i = 0; i < layout.getChildCount(); i++) {
|
||||
View child = layout.getChildAt(i);
|
||||
if (child instanceof ViewGroup) {
|
||||
enableViews((ViewGroup) child);
|
||||
} else {
|
||||
child.setEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void setListViewHeightBasedOnChildren(ListView listView) {
|
||||
ListAdapter listAdapter = listView.getAdapter();
|
||||
if (listAdapter == null)
|
||||
return;
|
||||
|
||||
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
|
||||
int totalHeight = 0;
|
||||
View view = null;
|
||||
for (int i = 0; i < listAdapter.getCount(); i++) {
|
||||
view = listAdapter.getView(i, view, listView);
|
||||
if (i == 0)
|
||||
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||
|
||||
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
|
||||
totalHeight += view.getMeasuredHeight();
|
||||
}
|
||||
ViewGroup.LayoutParams params = listView.getLayoutParams();
|
||||
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
|
||||
listView.setLayoutParams(params);
|
||||
listView.requestLayout();
|
||||
}
|
||||
|
||||
public static void animateViewColor(final View v, final int startColor, final int endColor) {
|
||||
animateViewColor(v, startColor, endColor, DEFAULT_COLOR_ANIMATION_DURATION);
|
||||
}
|
||||
|
||||
public static void animateViewColor(final View v, final int startColor, final int endColor, final int duration) {
|
||||
ObjectAnimator animator = ObjectAnimator.ofObject(v, "backgroundColor",
|
||||
new ArgbEvaluator(), startColor, endColor);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
animator.setInterpolator(new PathInterpolator(0.4f, 0f, 1f, 1f));
|
||||
}
|
||||
animator.setDuration(duration);
|
||||
animator.start();
|
||||
}
|
||||
|
||||
public static void setBackgroundAlpha(View view, float alpha, int baseColor) {
|
||||
int a = Math.min(255, Math.max(0, (int) (alpha * 255))) << 24;
|
||||
int rgb = 0x00ffffff & baseColor;
|
||||
view.setBackgroundColor(a + rgb);
|
||||
}
|
||||
|
||||
public static void addOnGlobalLayoutListener(final View view, final Runnable runnable) {
|
||||
ViewTreeObserver vto = view.getViewTreeObserver();
|
||||
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
|
||||
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
|
||||
} else {
|
||||
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
|
||||
}
|
||||
runnable.run();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue