fun sdkInitialize()

in facebook-core/src/main/java/com/facebook/FacebookSdk.kt [409:506]


  fun sdkInitialize(applicationContext: Context, callback: InitializeCallback?) {
    if (sdkInitialized.get()) {
      callback?.onInitialized()
      return
    }

    // Don't throw for these validations here, just log an error. We'll throw when we actually
    // need them
    Validate.hasFacebookActivity(applicationContext, false)
    Validate.hasInternetPermissions(applicationContext, false)
    FacebookSdk.applicationContext = applicationContext.applicationContext

    // Make sure anon_id doesn't get overridden
    AppEventsLogger.getAnonymousAppDeviceGUID(applicationContext)

    // Make sure we've loaded default settings if we haven't already.
    loadDefaultsFromMetadata(FacebookSdk.applicationContext)

    // We should have an application id by now if not throw
    if (applicationId.isNullOrEmpty()) {
      throw FacebookException(
          "A valid Facebook app id must be set in the " +
              "AndroidManifest.xml or set by calling FacebookSdk.setApplicationId " +
              "before initializing the sdk.")
    }
    // We should have an client token by now if not throw
    if (appClientToken.isNullOrEmpty()) {
      throw FacebookException(
          "A valid Facebook app client token must be set in the " +
              "AndroidManifest.xml or set by calling FacebookSdk.setClientToken " +
              "before initializing the sdk.")
    }
    // Set sdkInitialized to true now so the bellow async tasks don't throw not initialized
    // exceptions.
    sdkInitialized.set(true)

    // Set sdkFullyInitialzed if auto init enabled.
    if (getAutoInitEnabled()) {
      fullyInitialize()
    }

    // Register ActivityLifecycleTracker callbacks now, so will log activate app event properly
    if (FacebookSdk.applicationContext is Application &&
        UserSettingsManager.getAutoLogAppEventsEnabled()) {
      startTracking(FacebookSdk.applicationContext as Application, applicationId)
    }

    // Load app settings from network so that dialog configs are available
    loadAppSettingsAsync()

    // Fetch available protocol versions from the apps on the device
    updateAllAvailableProtocolVersionsAsync()
    BoltsMeasurementEventListener.getInstance(FacebookSdk.applicationContext)
    cacheDir = LockOnGetVariable<File> { FacebookSdk.applicationContext.cacheDir }
    FeatureManager.checkFeature(FeatureManager.Feature.Instrument) { enabled ->
      if (enabled) {
        InstrumentManager.start()
      }
    }
    FeatureManager.checkFeature(FeatureManager.Feature.AppEvents) { enabled ->
      if (enabled) {
        AppEventsManager.start()
      }
    }
    FeatureManager.checkFeature(FeatureManager.Feature.ChromeCustomTabsPrefetching) { enabled ->
      if (enabled) {
        hasCustomTabsPrefetching = true
      }
    }
    FeatureManager.checkFeature(FeatureManager.Feature.IgnoreAppSwitchToLoggedOut) { enabled ->
      if (enabled) {
        ignoreAppSwitchToLoggedOut = true
      }
    }
    FeatureManager.checkFeature(FeatureManager.Feature.BypassAppSwitch) { enabled ->
      if (enabled) {
        bypassAppSwitch = true
      }
    }
    val futureTask =
        FutureTask<Void> {
          AccessTokenManager.getInstance().loadCurrentAccessToken()
          ProfileManager.getInstance().loadCurrentProfile()
          if (AccessToken.isCurrentAccessTokenActive() && Profile.getCurrentProfile() == null) {
            // Access token and profile went out of sync due to a network or caching
            // issue, retry
            Profile.fetchProfileForCurrentAccessToken()
          }
          callback?.onInitialized()
          AppEventsLogger.initializeLib(getApplicationContext(), applicationId)
          UserSettingsManager.logIfAutoAppLinkEnabled()

          // Flush any app events that might have been persisted during last run.
          AppEventsLogger.newLogger(getApplicationContext().applicationContext).flush()
          null
        }
    getExecutor().execute(futureTask)
  }