Skip to content

Make AR module optional for the sample viewer #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />

<!-- Limits app visibility in the Google Play Store to ARCore supported devices
(https://developers.google.com/ar/devices). -->
<uses-feature android:name="android.hardware.camera.ar" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
Expand All @@ -23,11 +20,10 @@
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<!-- "AR Required" app, requires "Google Play Services for AR" (ARCore)
to be installed, as the app does not include any non-AR features. -->
<meta-data
android:name="com.google.ar.core"
android:value="required" />
<!-- Provide support for ARCore via Google Play Services,
by checking to see if device is compatible and ready for AR.
(https://developers.google.com/ar/devices). -->
<meta-data android:name="com.google.ar.core" android:value="optional" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.esri.arcgismaps.sample.augmentrealitytoshowtabletopscene


import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand All @@ -30,12 +29,12 @@ import com.esri.arcgismaps.sample.augmentrealitytoshowtabletopscene.components.A
import com.esri.arcgismaps.sample.augmentrealitytoshowtabletopscene.screens.DisplaySceneInTabletopARScreen
import com.esri.arcgismaps.sample.sampleslib.theme.SampleAppTheme
import com.google.ar.core.ArCoreApk
import com.google.ar.core.exceptions.UnavailableDeviceNotCompatibleException
import com.google.ar.core.exceptions.UnavailableUserDeclinedInstallationException

class MainActivity : ComponentActivity() {

private var userRequestedInstall = true

private var isGooglePlayServicesArInstalled = false
private val sceneViewModel: AugmentRealityToShowTabletopSceneViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -52,31 +51,67 @@ class MainActivity : ComponentActivity() {

override fun onResume() {
super.onResume()
checkGooglePlayServicesArInstalled()
checkARCoreAvailability()
}

/**
* Check if Google Play Services for AR is installed on the device. If it's not installed, this method should get
* called twice: once to request the installation and once to ensure it was installed when the activity resumes.
* Checks if ARCore is supported and handles install/update flow if required.
*/
private fun checkGooglePlayServicesArInstalled() {
private fun checkARCoreAvailability() {
val context = this
try {
when (ArCoreApk.getInstance().requestInstall(this, userRequestedInstall)) {
ArCoreApk.InstallStatus.INSTALL_REQUESTED -> {
userRequestedInstall = false
return
}
ArCoreApk.getInstance().checkAvailabilityAsync(context) { availability ->
when (availability) {
ArCoreApk.Availability.UNSUPPORTED_DEVICE_NOT_CAPABLE -> {
sceneViewModel.messageDialogVM.showMessageDialog(
title = "AR Not Supported",
description = "This device does not support AR."
)
}

ArCoreApk.Availability.SUPPORTED_NOT_INSTALLED,
ArCoreApk.Availability.SUPPORTED_APK_TOO_OLD -> {
try {
val installStatus = ArCoreApk.getInstance().requestInstall(this, true)
if (installStatus == ArCoreApk.InstallStatus.INSTALL_REQUESTED) {
// Installation requested, wait for next resume
}
} catch (e: UnavailableUserDeclinedInstallationException) {
sceneViewModel.messageDialogVM.showMessageDialog(
title = "AR Installation Declined",
description = "User declined to install ARCore."
)
} catch (e: UnavailableDeviceNotCompatibleException) {
sceneViewModel.messageDialogVM.showMessageDialog(
title = "AR Not Compatible",
description = "This device is not compatible with ARCore."
)
} catch (e: Exception) {
sceneViewModel.messageDialogVM.showMessageDialog(
title = "Installation Error",
description = e.localizedMessage ?: "An unknown error occurred."
)
}
}

ArCoreApk.Availability.SUPPORTED_INSTALLED -> {
// ARCore is ready, proceed to use AR features.
}

ArCoreApk.InstallStatus.INSTALLED -> {
isGooglePlayServicesArInstalled = true
return
ArCoreApk.Availability.UNKNOWN_CHECKING,
ArCoreApk.Availability.UNKNOWN_ERROR,
ArCoreApk.Availability.UNKNOWN_TIMED_OUT -> {
sceneViewModel.messageDialogVM.showMessageDialog(
title = "AR Check Error",
description = "Unable to determine ARCore availability. Please try again."
)
}
}
}
} catch (e: Exception) {
val sceneViewModel: AugmentRealityToShowTabletopSceneViewModel by viewModels()
sceneViewModel.messageDialogVM.showMessageDialog(
"Error checking Google Play Services for AR",
e.message.toString()
"Error checking AR availability",
e.localizedMessage ?: "An error occurred."
)
}
}
Expand Down
Loading