package com.marverenic.music.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.media.audiofx.AudioEffect;
import android.os.Build;

import com.marverenic.music.instances.Song;
import com.marverenic.music.player.PlayerController;

import java.util.UUID;

public final class Util {

    /**
     * This UUID corresponds to the UUID of an Equalizer Audio Effect. It has been copied from
     * {@link AudioEffect#EFFECT_TYPE_EQUALIZER} for backwards compatibility since this field was
     * added in API level 18.
     */
    private static final UUID EQUALIZER_UUID;

    static {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            EQUALIZER_UUID = AudioEffect.EFFECT_TYPE_EQUALIZER;
        } else {
            EQUALIZER_UUID = UUID.fromString("0bed4300-ddd6-11db-8f34-0002a5d5c51b");
        }
    }

    /**
     * This class is never instantiated
     */
    private Util() {

    }

    @TargetApi(Build.VERSION_CODES.M)
    public static boolean hasPermission(Context context, String permission) {
        return context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
    }

    @TargetApi(Build.VERSION_CODES.M)
    public static boolean hasPermissions(Context context, String... permissions) {
        for (String permission : permissions) {
            if (!hasPermission(context, permission)) {
                return false;
            }
        }
        return true;
    }

    public static Intent getSystemEqIntent(Context c) {
        Intent systemEq = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
        systemEq.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, c.getPackageName());
        systemEq.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, PlayerController.getAudioSessionId());

        ActivityInfo info = systemEq.resolveActivityInfo(c.getPackageManager(), 0);
        if (info != null && !info.name.startsWith("com.android.musicfx")) {
            return systemEq;
        } else {
            return null;
        }
    }

    /**
     * Checks whether the current device is capable of instantiating and using an
     * {@link android.media.audiofx.Equalizer}
     * @return True if an Equalizer may be used at runtime
     */
    public static boolean hasEqualizer() {
        for (AudioEffect.Descriptor effect : AudioEffect.queryEffects()) {
            if (EQUALIZER_UUID.equals(effect.type)) {
                return true;
            }
        }
        return false;
    }

    public static Bitmap fetchFullArt(Song song) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();

        try {
            retriever.setDataSource(song.getLocation());
            byte[] stream = retriever.getEmbeddedPicture();
            if (stream != null) {
                return BitmapFactory.decodeByteArray(stream, 0, stream.length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

@@ -370,16 +370,88 @@
 Effect;%0A
+import android.net.ConnectivityManager;%0Aimport android.net.NetworkInfo;%0A
 import a
@@ -467,16 +467,16 @@
 .Build;%0A
-
 %0Aimport 
@@ -590,16 +590,77 @@
 .UUID;%0A%0A
+import static android.content.Context.CONNECTIVITY_SERVICE;%0A%0A
 public f
@@ -1313,32 +1313,1770 @@
 til() %7B%0A%0A    %7D%0A%0A
+    /**%0A     * Checks whether the device is in a state where we're able to access the internet. If the%0A     * device is not connected to the internet, this will return %7B@code false%7D. If the device is%0A     * only connected to a mobile network, this will return %7B@code allowMobileNetwork%7D. If the%0A     * device is connected to an active WiFi network, this will return %7B@code true.%7D%0A     * @param context A context used to check the current network status%0A     * @param allowMobileNetwork Whether or not the user allows the application to use mobile%0A     *                           data. This is an internal implementation that is not enforced%0A     *                           by the system, but is exposed to the user in our app's settings.%0A     * @return Whether network calls should happen in the current connection state or not%0A     */%0A    @SuppressWarnings(%22SimplifiableIfStatement%22)%0A    public static boolean canAccessInternet(Context context, boolean allowMobileNetwork) %7B%0A        ConnectivityManager connectivityManager;%0A        connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);%0A%0A        NetworkInfo info = connectivityManager.getActiveNetworkInfo();%0A        if (info == null) %7B%0A            // No network connections are active%0A            return false;%0A        %7D else if (!info.isAvailable() %7C%7C info.isRoaming()) %7B%0A            // The network isn't active, or is a roaming network%0A            return false;%0A        %7D else if (info.getType() == ConnectivityManager.TYPE_MOBILE) %7B%0A            // If it's a mobile network, return the user preference%0A            return allowMobileNetwork;%0A        %7D else %7B%0A            // The network is a wifi network%0A            return true;%0A        %7D%0A    %7D%0A%0A
     @TargetApi(B

Create canAccessInternet helper method