Skip to content
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

fix: null pointer on get app context #1878

Closed
Changes from 1 commit
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
Next Next commit
fix: null pointer on get app context
Arthur Geron committed Oct 23, 2023
commit f4abcf07a360b7ed8ce8aa7cb052b6cca13e288d
Original file line number Diff line number Diff line change
@@ -344,3 +344,25 @@ class ApplicationService() : IApplicationService, ActivityLifecycleCallbacks, On
}
}
}

object ApplicationServiceUtil {
fun isValidInstance(_applicationService: IApplicationService?): Boolean {
return try {
if (_applicationService == null) {
println("Error: _applicationService is null")
false
} else if (_applicationService.appContext == null) {
println("Error: _applicationService.appContext is null")
false
} else {
true
}
} catch (e: NullPointerException) {
println("ApplicationService's AppContext not initialized: ${e.message}")
false
} catch (e: Exception) {
println("Exception thrown while checking ApplicationService's App Context: ${e.message}")
false
}
}
}
Original file line number Diff line number Diff line change
@@ -5,8 +5,9 @@ import android.os.Build
import com.onesignal.common.AndroidUtils
import com.onesignal.core.internal.application.IApplicationService
import com.onesignal.core.internal.device.IDeviceService
import com.onesignal.core.internal.application.impl.ApplicationServiceUtil

internal class DeviceService(private val _applicationService: IApplicationService) :
internal class DeviceService(private val _applicationService: IApplicationService?) :
IDeviceService {
companion object {
private const val HMS_CORE_SERVICES_PACKAGE = "com.huawei.hwid" // = HuaweiApiAvailability.SERVICES_PACKAGE
@@ -67,11 +68,13 @@ internal class DeviceService(private val _applicationService: IApplicationServic

// If running on Android O and targeting O we need version 26.0.0 for
// the new compat NotificationCompat.Builder constructor.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
AndroidUtils.getTargetSdkVersion(_applicationService.appContext) >= Build.VERSION_CODES.O
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!ApplicationServiceUtil.isValidInstance(_applicationService)) {
return IDeviceService.AndroidSupportLibraryStatus.MISSING
}

// Class was added in 26.0.0-beta2
if (!AndroidUtils.hasJobIntentService()) {
if (!AndroidUtils.hasJobIntentService() && AndroidUtils.getTargetSdkVersion(_applicationService!!.appContext) >= Build.VERSION_CODES.O) {
return IDeviceService.AndroidSupportLibraryStatus.OUTDATED
}
}
@@ -103,7 +106,11 @@ internal class DeviceService(private val _applicationService: IApplicationServic

private fun packageInstalledAndEnabled(packageName: String): Boolean {
return try {
val pm = _applicationService.appContext.packageManager
if (!ApplicationServiceUtil.isValidInstance(_applicationService)) {
return false;
}

val pm = _applicationService!!.appContext.packageManager
val info = pm.getPackageInfo(packageName, PackageManager.GET_META_DATA)
info.applicationInfo.enabled
} catch (e: PackageManager.NameNotFoundException) {
@@ -137,7 +144,11 @@ internal class DeviceService(private val _applicationService: IApplicationServic
val isHuaweiMobileServicesAvailableMethod = clazz.getMethod("isHuaweiMobileServicesAvailable", android.content.Context::class.java)
val availabilityInstance = newInstanceMethod.invoke(null)

val result = isHuaweiMobileServicesAvailableMethod.invoke(availabilityInstance, _applicationService.appContext) as Int
if (!ApplicationServiceUtil.isValidInstance(_applicationService)) {
return false;
}

val result = isHuaweiMobileServicesAvailableMethod.invoke(availabilityInstance, _applicationService!!.appContext) as Int

return result == HMS_AVAILABLE_SUCCESSFUL
} catch (e: ClassNotFoundException) {
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import android.content.Context
import android.content.SharedPreferences
import com.onesignal.common.threading.Waiter
import com.onesignal.core.internal.application.IApplicationService
import com.onesignal.core.internal.application.impl.ApplicationServiceUtil
import com.onesignal.core.internal.preferences.IPreferencesService
import com.onesignal.core.internal.preferences.PreferenceStores
import com.onesignal.core.internal.startup.IStartableService
@@ -17,7 +18,7 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.delay

internal class PreferencesService(
private val _applicationService: IApplicationService,
private val _applicationService: IApplicationService?,
private val _time: ITime,
) : IPreferencesService, IStartableService {
private val _prefsToApply: Map<String, MutableMap<String, Any?>> = mapOf(
@@ -154,7 +155,12 @@ internal class PreferencesService(

@Synchronized
private fun getSharedPrefsByName(store: String): SharedPreferences? {
return _applicationService.appContext.getSharedPreferences(store, Context.MODE_PRIVATE)

if (!ApplicationServiceUtil.isValidInstance(_applicationService)) {
return null
}

return _applicationService!!.appContext.getSharedPreferences(store, Context.MODE_PRIVATE)
}

companion object {