package com.tyron.code.util;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import androidx.core.content.FileProvider;
import java.io.File;
import org.codeassist.unofficial.BuildConfig;

public class ApkInstaller {

  private static final String TAG = ApkInstaller.class.getSimpleName();

  public static void installApplication(Context context, String filePath) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(
        uriFromFile(context, new File(filePath)), "application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    try {
      context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
      e.printStackTrace();
      Log.e(TAG, "Error in opening the file!");
    }
  }

  public static Uri uriFromFile(Context context, File file) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      return FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
    } else {
      return Uri.fromFile(file);
    }
  }
}
