content: package com.kessi.statussaver.util;
  
  import android.app.AlertDialog;
  import android.content.Context;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.graphics.Color;
  import android.graphics.drawable.ColorDrawable;
  import android.media.MediaScannerConnection;
  import android.net.Uri;
  import android.os.Build;
  import android.os.Environment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.Toast;
  
  import androidx.annotation.RequiresApi;
  import androidx.core.content.FileProvider;
  import androidx.documentfile.provider.DocumentFile;
  
  import com.kessi.statussaver.R;
  
  import org.apache.commons.io.FileUtils;
  
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.URLConnection;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  
  public class Utils {
  
  
      public static void mediaScanner(Context context, String newFilePath, String oldFilePath, String fileType) {
          try {
                  MediaScannerConnection.scanFile(context, new String[]{newFilePath + new File(oldFilePath).getName()}, new String[]{fileType},
                          new MediaScannerConnection.MediaScannerConnectionClient() {
                              public void onMediaScannerConnected() {
                              }
  
                              public void onScanCompleted(String path, Uri uri) {
                              }
                          });
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public static String getBack(String paramString1, String paramString2) {
          Matcher localMatcher = Pattern.compile(paramString2).matcher(paramString1);
          if (localMatcher.find()) {
              return localMatcher.group(1);
          }
          return "";
      }
  
      @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
      public static boolean download(Context context, String sourceFile) {
          if (copyFileInSavedDir(context, sourceFile)) {
              return true;
          } else {
              return false;
          }
      }
  
      static boolean isImageFile(String path) {
          String mimeType = URLConnection.guessContentTypeFromName(path);
          return mimeType != null && mimeType.startsWith("image");
      }
  
  //    static boolean isVideoFile(String path) {
  //        String mimeType = URLConnection.guessContentTypeFromName(path);
  //        return mimeType != null && mimeType.startsWith("video");
  //    }
  
      public static boolean isVideoFile(Context context, String path) {
          if (path.startsWith("content")) {
              DocumentFile fromTreeUri = DocumentFile.fromSingleUri(context, Uri.parse(path));
              String mimeType = fromTreeUri.getType();
              return mimeType != null && mimeType.startsWith("video");
          } else {
              String mimeType = URLConnection.guessContentTypeFromName(path);
              return mimeType != null && mimeType.startsWith("video");
          }
      }
  
  
      @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
      public static boolean copyFileInSavedDir(Context context, String sourceFile) {
  
          String finalPath;
          if (isVideoFile(context, sourceFile)) {
              finalPath = getDir(context, "Videos").getAbsolutePath();
          } else {
              finalPath = getDir(context, "Images").getAbsolutePath();
          }
  
          String pathWithName = finalPath + File.separator + new File(sourceFile).getName();
          Uri destUri = Uri.fromFile(new File(pathWithName));
  
          InputStream is = null;
          OutputStream os = null;
          try {
              Uri uri= Uri.parse(sourceFile);
              is = context.getContentResolver().openInputStream(uri);
              os = context.getContentResolver().openOutputStream(destUri, "w");
  
              byte[] buffer = new byte[1024];
  
              int length;
              while ((length = is.read(buffer)) > 0)
                  os.write(buffer, 0, length);
  
              is.close();
              os.flush();
              os.close();
  
              Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
              intent.setData(destUri);
              context.sendBroadcast(intent);
              return true;
          } catch (IOException e) {
              e.printStackTrace();
              return false;
          }
  
      }
  
      static File getDir(Context context, String folder) {
  
          File rootFile = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "Download" + File.separator + context.getResources().getString(R.string.app_name) + File.separator + folder);
          rootFile.mkdirs();
  
          return rootFile;
  
      }
  
      public static boolean appInstalledOrNot(Context context, String uri) {
          PackageManager pm = context.getPackageManager();
          try {
              pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
              return true;
          } catch (PackageManager.NameNotFoundException e) {
              return false;
          }
      }
  
      public static void shareFile(Context context, boolean isVideo, String path) {
          Intent share = new Intent();
          share.setAction(Intent.ACTION_SEND);
          if (isVideo)
              share.setType("Video/*");
          else
              share.setType("image/*");
  
          Uri uri;
          if (path.startsWith("content")) {
              uri = Uri.parse(path);
          } else {
              uri = FileProvider.getUriForFile(context,
                      context.getApplicationContext().getPackageName() + ".provider", new File(path));
          }
  
          share.putExtra(Intent.EXTRA_STREAM, uri);
          context.startActivity(share);
      }
  
      public static void repostWhatsApp(Context context, boolean isVideo, String path) {
          Intent share = new Intent();
          share.setAction(Intent.ACTION_SEND);
          if (isVideo)
              share.setType("Video/*");
          else
              share.setType("image/*");
  
          Uri uri;
          if (path.startsWith("content")) {
              uri = Uri.parse(path);
          } else {
              uri = FileProvider.getUriForFile(context,
                      context.getApplicationContext().getPackageName() + ".provider", new File(path));
          }
          share.putExtra(Intent.EXTRA_STREAM, uri);
          share.setPackage("com.whatsapp");
          context.startActivity(share);
      }
  
  
      static AlertDialog alertDialog;
  
      public static AlertDialog loadingPopup(Context context) {
          alertDialog = null;
          LayoutInflater layoutInflaterAndroid = LayoutInflater.from(context);
          View view = layoutInflaterAndroid.inflate(R.layout.dialog_loader, null);
          AlertDialog.Builder alert = null;
          alert = new AlertDialog.Builder(context);
          alert.setView(view);
  
          alertDialog = alert.create();
  
          alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  
          alertDialog.setCancelable(false);
          return alertDialog;
      }
  }
  