// Copyright 2019 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. using System; using Google.Play.Common; using Google.Play.Core.Internal; using UnityEngine; namespace Google.Play.AssetDelivery.Internal { /// /// Wraps Play Core's AssetPackManager which manages downloads of asset packs. /// internal class AssetPackManager : IDisposable { private AndroidJavaObject _javaAssetPackManager; public AssetPackManager() { const string factoryClassName = PlayCoreConstants.AssetPackPackagePrefix + "AssetPackManagerFactory"; using (var activity = UnityPlayerHelper.GetCurrentActivity()) using (var assetPackManagerFactory = new AndroidJavaClass(factoryClassName)) { _javaAssetPackManager = assetPackManagerFactory.CallStatic("getInstance", activity); } if (_javaAssetPackManager == null) { throw new NullReferenceException("Play Core returned null AssetPackManager"); } } /// /// Registers a listener that will be called when a pack download changes state. /// Listeners should be subsequently unregistered using /// /// /// An AndroidJavaProxy representing a java object of class: /// com.google.android.play.core.assetpacks.AssetPackStateUpdateListener /// public void RegisterListener(AndroidJavaProxy listener) { _javaAssetPackManager.Call("registerListener", listener); } /// /// Calls Play Core to unregister a listener previously added using /// /// /// An AndroidJavaProxy representing a java object of class: /// com.google.android.play.core.assetpacks.AssetPackStateUpdateListener /// public void UnregisterListener(AndroidJavaProxy listener) { _javaAssetPackManager.Call("unregisterListener", listener); } /// /// Calls Play Core to start a download for the specified asset packs. /// /// The asset packs to include in the download request. /// /// A wrapped Play Core task, which will return a AndroidJavaObject representing an /// AssetPackStates. The caller is responsible for disposing this task. /// public PlayServicesTask Fetch(params string[] assetPackNames) { using (var request = BuildAssetPackList(assetPackNames)) { var javaTask = _javaAssetPackManager.Call("fetch", request); return new PlayServicesTask(javaTask); } } /// /// Calls Play Core to get the states of the specified packs. /// /// /// A wrapped Play Core task, which will return a AndroidJavaObject representing an /// AssetPackStates. The caller is responsible for disposing this task. /// public PlayServicesTask GetPackStates(params string[] assetPackNames) { using (var packList = BuildAssetPackList(assetPackNames)) { var javaTask = _javaAssetPackManager.Call("getPackStates", packList); return new PlayServicesTask(javaTask); } } /// /// Calls Play Core to get the location of the specified asset pack. /// This should contain the AssetBundle associated with the specified asset pack. /// /// /// The AssetPackLocation containing the contents of the specified asset pack, or null if the most up-to-date /// version of this asset pack isn't available on disk. /// public AssetPackLocation GetPackLocation(string assetPackName) { var javaPackLocation = _javaAssetPackManager.Call("getPackLocation", assetPackName); return PlayCoreHelper.IsNull(javaPackLocation) ? null : new AssetPackLocation(javaPackLocation); } /// /// Calls Play Core to get the location of the specified asset within the specified asset pack. /// /// /// The AssetLocation of the specified asset or null if the most up-to-date /// version of this asset pack isn't available on disk. /// public AssetLocation GetAssetLocation(string assetPackName, string assetPath) { var javaAssetLocation = _javaAssetPackManager.Call("getAssetLocation", assetPackName, assetPath); return PlayCoreHelper.IsNull(javaAssetLocation) ? null : new AssetLocationImpl(javaAssetLocation); } /// /// Requests to cancel the download for a list of packs. /// /// /// Returns a AndroidJavaObject representing an AssetPackStates which contains the new states /// of all specified assetPackNames. /// public AndroidJavaObject Cancel(params string[] assetPackNames) { using (var packList = BuildAssetPackList(assetPackNames)) { var javaPackStates = _javaAssetPackManager.Call("cancel", packList); return javaPackStates; } } /// /// Deletes the specified asset pack from internal storage of the app. /// Deleted packs will not be re-downloaded after update. /// If the asset pack is currently being downloaded or installed, this method will not cancel the process. /// /// /// A PlayServicesTask representing a Task{Void} that will only be successful if files were successfully /// deleted. /// public PlayServicesTask RemovePack(string assetPackName) { var javaTask = _javaAssetPackManager.Call("removePack", assetPackName); return new PlayServicesTask(javaTask); } /// /// Calls Play Core to show a confirmation dialog for all downloads that are currently in the /// state. If the user accepts the dialog, then those /// packs are downloaded over cellular data. /// /// /// Returns a that completes once the dialog has been accepted, /// denied or closed. A successful task result contains one of the following values: ///
    ///
  • if the user accepted.
  • ///
  • if the user denied or the dialog has been closed in /// any other way (e.g. backpress).
  • ///
///
public PlayServicesTask ShowCellularDataConfirmation() { var task = _javaAssetPackManager.Call("showCellularDataConfirmation", UnityPlayerHelper.GetCurrentActivity()); return new PlayServicesTask(task); } /// /// Calls the Java Asset Delivery SDK to show a confirmation dialog for all downloads that /// are currently in either the /// state or the state. /// /// /// Returns a that completes once the dialog /// has been accepted, denied or closed. A successful task result contains one of the /// following values: ///
    ///
  • if the user accepted.
  • ///
  • if the user denied or the dialog has /// been closed in /// any other way (e.g. backpress).
  • ///
///
public PlayServicesTask ShowConfirmationDialog() { var task = _javaAssetPackManager.Call("showConfirmationDialog", UnityPlayerHelper.GetCurrentActivity()); return new PlayServicesTask(task); } // Returns a list of asset pack names. private static AndroidJavaObject BuildAssetPackList(params string[] assetPackNames) { var assetPackList = new AndroidJavaObject("java.util.ArrayList"); foreach (var packName in assetPackNames) { assetPackList.Call("add", packName); } return assetPackList; } public void Dispose() { _javaAssetPackManager.Dispose(); } } }