// Copyright 2020 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.AppUpdate.Internal
{
///
/// Internal implementation of that wraps Play Core's AppUpdateManager methods.
///
internal class AppUpdateManagerPlayCore : IDisposable
{
private readonly AndroidJavaObject _javaAppUpdateManager;
internal AppUpdateManagerPlayCore()
{
const string factoryClassName =
PlayCoreConstants.PlayCorePackagePrefix + "appupdate.AppUpdateManagerFactory";
using (var activity = UnityPlayerHelper.GetCurrentActivity())
using (var managerFactory = new AndroidJavaClass(factoryClassName))
{
_javaAppUpdateManager = managerFactory.CallStatic("create", activity);
}
if (_javaAppUpdateManager == null)
{
throw new NullReferenceException("Play Core returned null AppUpdateManager");
}
PlayCoreEventHandler.CreateInScene();
}
///
/// Registers a listener for this app that receives state changes for self-update operations.
/// Listeners should be subsequently cleared using .
///
///
/// An AndroidJavaProxy representing a java object of class:
/// com.google.android.play.core.appupdate.InstallStateUpdatedListener
///
public void RegisterListener(AndroidJavaProxy listener)
{
_javaAppUpdateManager.Call("registerListener", listener);
}
///
/// Removes the specified listener previously added using .
///
public void UnregisterListener(AndroidJavaProxy listener)
{
_javaAppUpdateManager.Call("unregisterListener", listener);
}
///
/// Returns a which returns an AppUpdateInfo
/// AndroidJavaObject on the registered on success callback.
///
///
/// A wrapped Play Core task, which will return a AndroidJavaObject representing an
/// AppUpdateInfo. The caller is responsible for disposing this task.
///
public PlayCoreTask GetAppUpdateInfo()
{
var javaTask = _javaAppUpdateManager.Call("getAppUpdateInfo");
return new PlayCoreTask(javaTask);
}
///
/// Starts the desired update flow indicated by asynchronously.
///
/// The on success result of .
/// contains update type options.
///
/// A which gives an int result
/// once the dialog has been accepted, denied or closed.
/// A successful task result contains one of the following values:
///
/// - -1 if the user accepted.
/// - 0 if the user denied or the dialog has been closed in
/// any other way (e.g. backpress).
/// - 1 if the activity created to achieve has failed.
///
///
public PlayCoreTask StartUpdateFlow(AppUpdateInfo appUpdateInfo, AppUpdateOptions appUpdateOptions)
{
var javaTask =
_javaAppUpdateManager.Call("startUpdateFlow",
appUpdateInfo.GetJavaUpdateInfo(), UnityPlayerHelper.GetCurrentActivity(),
appUpdateOptions.GetJavaAppUpdateOptions());
return new PlayCoreTask(javaTask);
}
///
/// Triggers the completion of the update for a flexible update flow.
///
///
/// A wrapped Play Core task, which will return an AndroidJavaObject of void result.
///
public PlayCoreTask CompleteUpdate()
{
var javaTask = _javaAppUpdateManager.Call("completeUpdate");
return new PlayCoreTask(javaTask);
}
public void Dispose()
{
_javaAppUpdateManager.Dispose();
}
}
}