static void SetWallpaperBitmap(AndroidJavaObject bitmapAjo, AndroidRect visibleCropHint = null,
                                bool allowBackup = true, WallpaperType which = WallpaperType.Unspecified)
 {
     if (Check.IsSdkGreaterOrEqual(AGDeviceInfo.VersionCodes.N))
     {
         WallpaperManager.CallInt(SetBitmapMethodName, bitmapAjo,
                                  visibleCropHint != null ? visibleCropHint.ajo : null, allowBackup, (int)which);
     }
     else
     {
         WallpaperManager.Call(SetBitmapMethodName, bitmapAjo);
     }
 }
 static void StartCropAndSetWallpaperActivity(AndroidJavaObject uri, AndroidRect visibleCropHint, bool allowBackup, WallpaperType which)
 {
     try
     {
         var intent = WallpaperManager.CallAJO("getCropAndSetWallpaperIntent", uri);
         AGUtils.StartActivity(intent);
     }
     catch (Exception)
     {
         Debug.Log("Setting wallpaper with crop failed, falling back to setting just image");
         var bitmapAjo = C.AndroidProviderMediaStoreImagesMedia.AJCCallStaticOnceAJO("getBitmap", AGUtils.ContentResolver, uri);
         SetWallpaperBitmap(bitmapAjo, visibleCropHint, allowBackup, which);
     }
 }
        /// <summary>
        /// Sets provided image on the provided filepath as wallpaper. File MUST exist.
        /// </summary>
        /// <param name="imagePath"> A path to the image file </param>
        /// <param name="visibleCropHint">
        /// The rectangular subregion of fullImage that should be displayed as wallpaper. Passing null for this parameter means
        /// that the full image should be displayed if possible given the image's and device's aspect ratios, etc.
        /// </param>
        /// <param name="allowBackup">
        /// True if the OS is permitted to back up this wallpaper image for restore to a future device; false otherwise.
        /// </param>
        /// <param name="which">
        /// Which wallpaper to configure with the new imagery.
        /// </param>
        public static void SetWallpaper([NotNull] string imagePath,
                                        AndroidRect visibleCropHint = null, bool allowBackup = true, WallpaperType which = WallpaperType.Unspecified)
        {
            if (imagePath == null)
            {
                throw new ArgumentNullException("imagePath");
            }

            if (AGUtils.IsNotAndroid())
            {
                return;
            }

            CheckIfFileExists(imagePath);

            SetWallpaperBitmap(AGUtils.DecodeBitmap(imagePath), visibleCropHint, allowBackup, which);
        }
        /// <summary>
        /// <remarks>
        /// WARNING: This method works on my devices but always crashes on emulators and I can't find a way to fix it.
        /// It may be something with emulator but use this method with care and test carefully before using it.
        /// </remarks>
        ///  Sets provided texture as wallpaper allowing user to crop beforehand
        /// </summary>
        /// <param name="wallpaperTexture"> A texture, that will supply the wallpaper imagery.</param>
        /// <param name="visibleCropHint">
        /// The rectangular subregion of fullImage that should be displayed as wallpaper. Passing null for this parameter means
        /// that the full image should be displayed if possible given the image's and device's aspect ratios, etc.
        /// </param>
        /// <param name="allowBackup">
        /// True if the OS is permitted to back up this wallpaper image for restore to a future device; false otherwise.
        /// </param>
        /// <param name="which">
        /// Which wallpaper to configure with the new imagery.
        /// </param>
        public static void ShowCropAndSetWallpaperChooser([NotNull] Texture2D wallpaperTexture,
                                                          AndroidRect visibleCropHint = null, bool allowBackup = true, WallpaperType which = WallpaperType.Unspecified)
        {
            if (wallpaperTexture == null)
            {
                throw new ArgumentNullException("wallpaperTexture");
            }

            if (AGUtils.IsNotAndroid())
            {
                return;
            }

            if (AGDeviceInfo.SDK_INT < AGDeviceInfo.VersionCodes.KITKAT)
            {
                return;
            }

            var uri = AndroidPersistanceUtilsInternal.SaveWallpaperImageToExternalStorageUri(wallpaperTexture);

            StartCropAndSetWallpaperActivity(uri, visibleCropHint, allowBackup, which);
        }
        /// <summary>
        /// Sets provided texture as wallpaper
        /// </summary>
        /// <param name="wallpaperTexture"> A texture, that will supply the wallpaper imagery.</param>
        /// <param name="visibleCropHint">
        /// The rectangular subregion of fullImage that should be displayed as wallpaper. Passing null for this parameter means
        /// that the full image should be displayed if possible given the image's and device's aspect ratios, etc.
        /// </param>
        /// <param name="allowBackup">
        /// True if the OS is permitted to back up this wallpaper image for restore to a future device; false otherwise.
        /// </param>
        /// <param name="which">
        /// Which wallpaper to configure with the new imagery.
        /// </param>
        public static void SetWallpaper([NotNull] Texture2D wallpaperTexture,
                                        AndroidRect visibleCropHint = null, bool allowBackup = true, WallpaperType which = WallpaperType.Unspecified)
        {
            if (wallpaperTexture == null)
            {
                throw new ArgumentNullException("wallpaperTexture");
            }

            if (AGUtils.IsNotAndroid())
            {
                return;
            }

            var wallpaperPath = AndroidPersistanceUtilsInternal.SaveWallpaperImageToExternalStorage(wallpaperTexture);

            if (Check.IsSdkGreaterOrEqual(AGDeviceInfo.VersionCodes.N))
            {
                SetWallpaperBitmap(AGUtils.DecodeBitmap(wallpaperPath), visibleCropHint, allowBackup, which);
                return;
            }

            SetWallpaperBitmap(AGUtils.DecodeBitmap(wallpaperPath));
        }
        /// <summary>
        ///  <remarks>
        /// WARNING: This method works on my devices but always crashes on emulators and I can't find a way to fix it.
        /// It may be something with emulator but use this method with care and test carefully before using it.
        /// </remarks>
        ///  Sets provided image on the provided filepath as wallpaper allowing user to crop beforehand. File MUST exist.
        /// </summary>
        /// <param name="imagePath"> A path to the image file </param>
        /// <param name="visibleCropHint">
        /// The rectangular subregion of fullImage that should be displayed as wallpaper. Passing null for this parameter means
        /// that the full image should be displayed if possible given the image's and device's aspect ratios, etc.
        /// </param>
        /// <param name="allowBackup">
        /// True if the OS is permitted to back up this wallpaper image for restore to a future device; false otherwise.
        /// </param>
        /// <param name="which">
        /// Which wallpaper to configure with the new imagery.
        /// </param>
        public static void ShowCropAndSetWallpaperChooser([NotNull] string imagePath,
                                                          AndroidRect visibleCropHint = null, bool allowBackup = true, WallpaperType which = WallpaperType.Unspecified)
        {
            if (imagePath == null)
            {
                throw new ArgumentNullException("imagePath");
            }

            if (AGUtils.IsNotAndroid())
            {
                return;
            }

            if (AGDeviceInfo.SDK_INT < AGDeviceInfo.VersionCodes.KITKAT)
            {
                return;
            }

            CheckIfFileExists(imagePath);
            var uri = AndroidPersistanceUtilsInternal.GetUriFromFilePath(imagePath);

            StartCropAndSetWallpaperActivity(uri, visibleCropHint, allowBackup, which);
        }