// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/android/device_info.h"

#include <string>

#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/compiler_specific.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/synchronization/lock.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "base/build_info_jni/DeviceInfo_jni.h"

#if __ANDROID_API__ >= 29
// .aidl based NDK generation is only available when our min SDK level is 29 or
// higher.
#include "aidl/org/chromium/base/IDeviceInfo.h"
using aidl::org::chromium::base::IDeviceInfo;
#endif

namespace base::android::device_info {
namespace {
#if __ANDROID_API__ < 29
struct IDeviceInfo {
  std::string gmsVersionCode;
  bool isAutomotive;
  bool isDesktop;
  bool isFoldable;
  bool isTv;
  // Available only on Android T+.
  int32_t vulkanDeqpLevel;
  bool isXr;
  bool wasLaunchedOnLargeDisplay;
};
#endif

static std::optional<IDeviceInfo>& get_holder() {
  static base::NoDestructor<std::optional<IDeviceInfo>> holder;
  return *holder;
}

IDeviceInfo& get_device_info() {
  std::optional<IDeviceInfo>& holder = get_holder();
  if (!holder.has_value()) {
    Java_DeviceInfo_nativeReadyForFields(AttachCurrentThread());
  }
  return *holder;
}

}  // namespace

void Set(const IDeviceInfo& info) {
  static base::NoDestructor<base::Lock> lock;
  base::AutoLock l(*lock);

  std::optional<IDeviceInfo>& holder = get_holder();
  holder.emplace(info);
}

static void JNI_DeviceInfo_FillFields(const std::string& gmsVersionCode,
                                      bool isTV,
                                      bool isAutomotive,
                                      bool isFoldable,
                                      bool isDesktop,
                                      int32_t vulkanDeqpLevel,
                                      bool isXr,
                                      bool wasLaunchedOnLargeDisplay) {
  Set(IDeviceInfo{.gmsVersionCode = gmsVersionCode,
                  .isAutomotive = isAutomotive,
                  .isDesktop = isDesktop,
                  .isFoldable = isFoldable,
                  .isTv = isTV,
                  .vulkanDeqpLevel = vulkanDeqpLevel,
                  .isXr = isXr,
                  .wasLaunchedOnLargeDisplay = wasLaunchedOnLargeDisplay});
}

const std::string& gms_version_code() {
  return get_device_info().gmsVersionCode;
}

void set_gms_version_code_for_test(const std::string& gms_version_code) {
  get_device_info().gmsVersionCode = gms_version_code;
  Java_DeviceInfo_setGmsVersionCodeForTest(AttachCurrentThread(),
                                           gms_version_code);
}

bool is_tv() {
  return get_device_info().isTv;
}
bool is_automotive() {
  return get_device_info().isAutomotive;
}
bool is_foldable() {
  return get_device_info().isFoldable;
}

bool is_desktop() {
  return get_device_info().isDesktop;
}

// Available only on Android T+.
int32_t vulkan_deqp_level() {
  return get_device_info().vulkanDeqpLevel;
}

bool is_xr() {
  return get_device_info().isXr;
}

// Roughly matches the check logic in device_form_factor.h to see if the device
// is a tablet (based on mostly elimination of other possible form-factors and
// screen-width). Where possible, prefer using device_form_factor.h (runtime
// check) as a first choice, but fall back to this if not feasible.
bool is_tablet() {
  return was_launched_on_large_display() && !is_tv() && !is_automotive() &&
         !is_desktop() && !is_xr();
}

// This returns the cached value during initial startup. If you need this
// evaluated at runtime, then use device_form_factor Additionally, this differs
// from device_form_factor in that it does not guarantee that the Android
// resource (-sw600) is respected.
bool was_launched_on_large_display() {
  return get_device_info().wasLaunchedOnLargeDisplay;
}

std::string device_name() {
  return Java_DeviceInfo_getDeviceName(AttachCurrentThread());
}

void set_is_xr_for_testing() {
  Java_DeviceInfo_setIsXrForTesting(AttachCurrentThread(), true);  // IN-TEST
  get_holder().reset();
}

void reset_is_xr_for_testing() {
  Java_DeviceInfo_resetIsXrForTesting(AttachCurrentThread());  // IN-TEST
  get_holder().reset();
}

void set_is_desktop_for_testing(bool is_desktop) {
  Java_DeviceInfo_setIsDesktopForTesting(AttachCurrentThread(),  // IN-TEST
                                         is_desktop);
  get_holder().reset();
}

void reset_is_desktop_for_testing() {
  Java_DeviceInfo_resetIsDesktopForTesting(AttachCurrentThread());  // IN-TEST
  get_holder().reset();
}

void set_is_foldable_for_testing(bool is_foldable) {
  Java_DeviceInfo_setIsFoldableForTesting(AttachCurrentThread(),
                                          is_foldable);  // IN-TEST
  get_holder().reset();
}

void reset_is_foldable_for_testing() {
  Java_DeviceInfo_resetIsFoldableForTesting(AttachCurrentThread());  // IN-TEST
  get_holder().reset();
}

}  // namespace base::android::device_info

DEFINE_JNI(DeviceInfo)
