// 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.Text;
using Google.Play.AppUpdate.Internal;
using Google.Play.Core.Internal;
using UnityEngine;
namespace Google.Play.AppUpdate
{
// Note: This wrapper around Java AppUpdateInfo exists to hide the AndroidJavaObject from the public API.
///
/// Contains information about in-app update availability and installation progress.
///
public class AppUpdateInfo
{
private readonly AndroidJavaObject _javaAppUpdateInfo;
///
/// Returns the app version code of an available or in-progress update, or an arbitrary value if no update
/// is available.
///
public int AvailableVersionCode
{
get { return _javaAppUpdateInfo.Call("availableVersionCode"); }
}
///
/// Returns update availability.
///
public UpdateAvailability UpdateAvailability
{
get
{
return AppUpdatePlayCoreTranslator.TranslatePlayCoreUpdateAvailabilities(
_javaAppUpdateInfo.Call("updateAvailability"));
}
}
///
/// Returns update status.
///
public AppUpdateStatus AppUpdateStatus
{
get
{
return AppUpdatePlayCoreTranslator.TranslatePlayCoreUpdateStatus(
_javaAppUpdateInfo.Call("installStatus"));
}
}
///
/// Returns the number of days that the Google Play Store app on the user's device has known about an
/// available update, or null if update or staleness information is unavailable.
///
public int? ClientVersionStalenessDays
{
get
{
var clientVersionStalenessDaysJavaObject =
_javaAppUpdateInfo.Call("clientVersionStalenessDays");
if (PlayCoreHelper.IsNull(clientVersionStalenessDaysJavaObject))
{
return null;
}
// Convert the java.lang.Integer object to an int primitive.
return clientVersionStalenessDaysJavaObject.Call("intValue");
}
}
///
/// Returns the priority for this update, as defined by the developer using the "inAppUpdatePriority" field
/// of the Google Play Developer API's
/// edits.track.
///
public int UpdatePriority
{
get { return _javaAppUpdateInfo.Call("updatePriority"); }
}
///
/// Returns the number of bytes already downloaded for the update.
///
public ulong BytesDownloaded
{
get { return _javaAppUpdateInfo.Call("bytesDownloaded"); }
}
///
/// Returns the total number of bytes to download for the update.
///
public ulong TotalBytesToDownload
{
get { return _javaAppUpdateInfo.Call("totalBytesToDownload"); }
}
///
/// Returns true if an update with the specified options is allowed, false otherwise.
///
/// The type of update to perform.
public bool IsUpdateTypeAllowed(AppUpdateOptions appUpdateOptions)
{
return _javaAppUpdateInfo.Call("isUpdateTypeAllowed", appUpdateOptions.GetJavaAppUpdateOptions());
}
///
/// Returns a debug string containing all fields of this class.
///
public override string ToString()
{
var infoDescription = new StringBuilder();
infoDescription.AppendFormat("version={0} ", AvailableVersionCode);
infoDescription.AppendFormat("status={0} ", AppUpdateStatus);
infoDescription.AppendFormat("availability={0} ", UpdateAvailability);
infoDescription.AppendFormat("downloaded={0} ", BytesDownloaded);
infoDescription.AppendFormat("priority={0} ", UpdatePriority);
infoDescription.AppendFormat("staleness={0} ", ClientVersionStalenessDays);
infoDescription.AppendFormat("totalBytes={0} ", TotalBytesToDownload);
infoDescription.AppendFormat("immediateAllowed={0}",
IsUpdateTypeAllowed(AppUpdateOptions.ImmediateAppUpdateOptions()));
infoDescription.AppendFormat("flexibleAllowed={0}",
IsUpdateTypeAllowed(AppUpdateOptions.FlexibleAppUpdateOptions()));
return infoDescription.ToString();
}
internal AppUpdateInfo(AndroidJavaObject javaAppUpdateInfo)
{
_javaAppUpdateInfo = javaAppUpdateInfo;
}
internal AndroidJavaObject GetJavaUpdateInfo()
{
return _javaAppUpdateInfo;
}
}
}