fun getAttributionIdentifiers()

in facebook-core/src/main/java/com/facebook/internal/AttributionIdentifiers.kt [177:239]


    fun getAttributionIdentifiers(context: Context): AttributionIdentifiers? {
      val identifiers = getAndroidId(context)
      var c: Cursor? = null
      try {
        // We can't call getAdvertisingIdInfo on the main thread or the app will potentially
        // freeze, if this is the case throw:
        if (Looper.myLooper() == Looper.getMainLooper()) {
          throw FacebookException("getAttributionIdentifiers cannot be called on the main thread.")
        }
        val cachedIdentifiers = this.cachedIdentifiers
        if (cachedIdentifiers != null &&
            System.currentTimeMillis() - cachedIdentifiers.fetchTime <
                IDENTIFIER_REFRESH_INTERVAL_MILLIS) {
          return cachedIdentifiers
        }
        val projection =
            arrayOf(ATTRIBUTION_ID_COLUMN_NAME, ANDROID_ID_COLUMN_NAME, LIMIT_TRACKING_COLUMN_NAME)
        var providerUri: Uri? = null
        val contentProviderInfo =
            context.packageManager.resolveContentProvider(ATTRIBUTION_ID_CONTENT_PROVIDER, 0)
        val wakizashiProviderInfo =
            context.packageManager.resolveContentProvider(
                ATTRIBUTION_ID_CONTENT_PROVIDER_WAKIZASHI, 0)
        if (contentProviderInfo != null &&
            validateSignature(context, contentProviderInfo.packageName)) {
          providerUri = Uri.parse("content://$ATTRIBUTION_ID_CONTENT_PROVIDER")
        } else if (wakizashiProviderInfo != null &&
            validateSignature(context, wakizashiProviderInfo.packageName)) {
          providerUri = Uri.parse("content://$ATTRIBUTION_ID_CONTENT_PROVIDER_WAKIZASHI")
        }
        val installerPackageName = getInstallerPackageName(context)
        if (installerPackageName != null) {
          identifiers.androidInstallerPackage = installerPackageName
        }
        if (providerUri == null) {
          return cacheAndReturnIdentifiers(identifiers)
        }
        c = context.contentResolver.query(providerUri, projection, null, null, null)
        if (c == null || !c.moveToFirst()) {
          return cacheAndReturnIdentifiers(identifiers)
        }
        val attributionColumnIndex = c.getColumnIndex(ATTRIBUTION_ID_COLUMN_NAME)
        val androidIdColumnIndex = c.getColumnIndex(ANDROID_ID_COLUMN_NAME)
        val limitTrackingColumnIndex = c.getColumnIndex(LIMIT_TRACKING_COLUMN_NAME)
        identifiers.attributionId = c.getString(attributionColumnIndex)

        // if we failed to call Google's APIs directly (due to improper integration by the
        // client), it may be possible for the local facebook application to relay it to us.
        if (androidIdColumnIndex > 0 &&
            limitTrackingColumnIndex > 0 &&
            identifiers.androidAdvertiserId == null) {
          identifiers.androidAdvertiserIdValue = c.getString(androidIdColumnIndex)
          identifiers.isTrackingLimited =
              java.lang.Boolean.parseBoolean(c.getString(limitTrackingColumnIndex))
        }
      } catch (e: Exception) {
        logd(TAG, "Caught unexpected exception in getAttributionId(): $e")
        return null
      } finally {
        c?.close()
      }
      return cacheAndReturnIdentifiers(identifiers)
    }