Skip to content

Commit

Permalink
feat: android local sync update (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Mar 29, 2022
1 parent d14cb55 commit e25ecd4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pull-request-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
java-version: '11'
distribution: 'zulu'
- uses: actions/checkout@v2
- name: Build
run: ./gradlew build
- name: Unit Test
run: ./gradlew testDebugUnitTest
- name: Lint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.amplitude.android.utilities

import com.amplitude.common.Logger
import com.amplitude.common.android.AndroidLogger
import com.amplitude.common.android.LogcatLogger
import com.amplitude.core.Amplitude
import com.amplitude.core.LoggerProvider

class AndroidLoggerProvider() : LoggerProvider {
override fun getLogger(amplitude: Amplitude): Logger {
return AndroidLogger()
return LogcatLogger()
}
}
1 change: 1 addition & 0 deletions common-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amplitude.common.android">

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.amplitude.common.android
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.res.Resources
import android.location.Geocoder
import android.location.Location
import android.location.LocationManager
Expand Down Expand Up @@ -178,11 +179,26 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
return null
}

private val locale: Locale
private get() {
val configuration = Resources.getSystem().configuration
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val localeList = configuration.locales
if (localeList.isEmpty) {
return Locale.getDefault()
} else {
return localeList.get(0)
}
} else {
return configuration.locale
}
}

private val countryFromLocale: String
private get() = Locale.getDefault().country
private get() = locale.country

private fun fetchLanguage(): String {
return Locale.getDefault().language
return locale.language
}

private fun fetchAdvertisingId(): String {
Expand All @@ -209,12 +225,12 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
val getId = appSetInfo.javaClass.getMethod("getId")
appSetId = getId.invoke(appSetInfo) as String
} catch (e: ClassNotFoundException) {
AndroidLogger.logger
LogcatLogger.logger
.warn("Google Play Services SDK not found for app set id!")
} catch (e: InvocationTargetException) {
AndroidLogger.logger.warn("Google Play Services not available for app set id")
LogcatLogger.logger.warn("Google Play Services not available for app set id")
} catch (e: Exception) {
AndroidLogger.logger.error(
LogcatLogger.logger.error(
"Encountered an error connecting to Google Play Services for app set id"
)
}
Expand Down Expand Up @@ -248,13 +264,13 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
val getId = advertisingInfo.javaClass.getMethod("getId")
advertisingId = getId.invoke(advertisingInfo) as String
} catch (e: ClassNotFoundException) {
AndroidLogger.logger
LogcatLogger.logger
.warn("Google Play Services SDK not found for advertising id!")
} catch (e: InvocationTargetException) {
AndroidLogger.logger
LogcatLogger.logger
.warn("Google Play Services not available for advertising id")
} catch (e: Exception) {
AndroidLogger.logger.error(
LogcatLogger.logger.error(
"Encountered an error connecting to Google Play Services for advertising id"
)
}
Expand All @@ -274,17 +290,17 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
// status 0 corresponds to com.google.android.gms.common.ConnectionResult.SUCCESS;
return status != null && status == 0
} catch (e: NoClassDefFoundError) {
AndroidLogger.logger.warn("Google Play Services Util not found!")
LogcatLogger.logger.warn("Google Play Services Util not found!")
} catch (e: ClassNotFoundException) {
AndroidLogger.logger.warn("Google Play Services Util not found!")
LogcatLogger.logger.warn("Google Play Services Util not found!")
} catch (e: NoSuchMethodException) {
AndroidLogger.logger.warn("Google Play Services not available")
LogcatLogger.logger.warn("Google Play Services not available")
} catch (e: InvocationTargetException) {
AndroidLogger.logger.warn("Google Play Services not available")
LogcatLogger.logger.warn("Google Play Services not available")
} catch (e: IllegalAccessException) {
AndroidLogger.logger.warn("Google Play Services not available")
LogcatLogger.logger.warn("Google Play Services not available")
} catch (e: Exception) {
AndroidLogger.logger.warn(
LogcatLogger.logger.warn(
"Error when checking for Google Play Services: $e"
)
}
Expand Down Expand Up @@ -363,9 +379,9 @@ class AndroidContextProvider(private val context: Context, locationListening: Bo
try {
location = locationManager.getLastKnownLocation(provider!!)
} catch (e: SecurityException) {
AndroidLogger.logger.warn("Failed to get most recent location")
LogcatLogger.logger.warn("Failed to get most recent location")
} catch (e: Exception) {
AndroidLogger.logger.warn("Failed to get most recent location")
LogcatLogger.logger.warn("Failed to get most recent location")
}
if (location != null) {
locations.add(location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.amplitude.common.android
import android.util.Log
import com.amplitude.common.Logger

class AndroidLogger() : Logger {
class LogcatLogger() : Logger {
override var logMode: Logger.LogMode = Logger.LogMode.INFO
private val tag = "Amplitude"

Expand Down Expand Up @@ -32,6 +32,6 @@ class AndroidLogger() : Logger {
}

companion object {
val logger = AndroidLogger()
val logger = LogcatLogger()
}
}

0 comments on commit e25ecd4

Please sign in to comment.