content: package de.aaronoe.cinematic.util;
  
  import android.Manifest;
  import android.animation.Animator;
  import android.animation.AnimatorListenerAdapter;
  import android.animation.ObjectAnimator;
  import android.animation.ValueAnimator;
  import android.app.Activity;
  import android.content.Context;
  import android.content.pm.PackageManager;
  import android.graphics.Bitmap;
  import android.graphics.ColorMatrixColorFilter;
  import android.net.Uri;
  import android.os.Build;
  import android.os.Environment;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.content.FileProvider;
  import android.util.Log;
  import android.widget.ImageView;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  /**
   * Created by aaron on 31.05.17.
   */
  
  
  public class DisplayUtils {
  
      private int dpi = 0;
      private static final String TAG = "DisplayUtils";
  
      public DisplayUtils(Context context) {
          dpi = context.getResources().getDisplayMetrics().densityDpi;
      }
  
      public float dpToPx(int dp) {
          if (dpi == 0) {
              return 0;
          }
          return (float) (dp * (dpi / 160.0));
      }
  
      /**
       * Execute a saturation animation to make a image from white and black into color.
       *
       * @param c      Context.
       * @param target ImageView which will execute saturation animation.
       * */
      public static void startSaturationAnimation(Context c, final ImageView target, int duration) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
              target.setHasTransientState(true);
              final AnimUtils.ObservableColorMatrix matrix = new AnimUtils.ObservableColorMatrix();
              final ObjectAnimator saturation = ObjectAnimator.ofFloat(
                      matrix, AnimUtils.ObservableColorMatrix.SATURATION, 0f, 1f);
              saturation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener
                      () {
                  @Override
                  public void onAnimationUpdate(ValueAnimator valueAnimator) {
                      target.setColorFilter(new ColorMatrixColorFilter(matrix));
                  }
              });
              saturation.setDuration(duration);
              saturation.setInterpolator(AnimUtils.getFastOutSlowInInterpolator(c));
              saturation.addListener(new AnimatorListenerAdapter() {
                  @Override
                  public void onAnimationEnd(Animator animation) {
                      target.clearColorFilter();
                      target.setHasTransientState(false);
                  }
              });
              saturation.start();
          }
      }
  
      public static Uri getLocalBitmapUri(Context context, Bitmap bmp) {
          // Store image to default external storage directory
          Uri bmpUri = null;
          try {
              File file =  new File(Environment.getExternalStoragePublicDirectory(
                      Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
              file.getParentFile().mkdirs();
              FileOutputStream out = new FileOutputStream(file);
              bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
              out.close();
              //bmpUri = Uri.fromFile(file);
              bmpUri = FileProvider.getUriForFile(context,
                      context.getApplicationContext().getPackageName() + ".provider", file);
          } catch (IOException e) {
              e.printStackTrace();
          }
          return bmpUri;
      }
  
      public static boolean isStoragePermissionGranted(Activity context) {
          if (Build.VERSION.SDK_INT >= 23) {
              if (context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                      == PackageManager.PERMISSION_GRANTED) {
                  Log.v(TAG,"Permission is granted");
                  return true;
              } else {
  
                  Log.v(TAG,"Permission is revoked");
                  ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                  return false;
              }
          }
          else { //permission is automatically granted on sdk<23 upon installation
              Log.v(TAG,"Permission is granted");
              return true;
          }
      }
  
  }
  