|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase; |
| 16 | + |
| 17 | +import android.content.pm.ApplicationInfo; |
| 18 | +import android.content.pm.PackageManager; |
| 19 | +import android.os.Build; |
| 20 | +import com.google.firebase.components.Component; |
| 21 | +import com.google.firebase.components.ComponentRegistrar; |
| 22 | +import com.google.firebase.heartbeatinfo.DefaultHeartBeatInfo; |
| 23 | +import com.google.firebase.platforminfo.DefaultUserAgentPublisher; |
| 24 | +import com.google.firebase.platforminfo.KotlinDetector; |
| 25 | +import com.google.firebase.platforminfo.LibraryVersionComponent; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +/** @hide */ |
| 30 | +public class FirebaseCommonRegistrar implements ComponentRegistrar { |
| 31 | + private static final String FIREBASE_ANDROID = "fire-android"; |
| 32 | + private static final String FIREBASE_COMMON = "fire-core"; |
| 33 | + private static final String DEVICE_NAME = "device-name"; |
| 34 | + private static final String DEVICE_MODEL = "device-model"; |
| 35 | + private static final String DEVICE_BRAND = "device-brand"; |
| 36 | + private static final String TARGET_SDK = "android-target-sdk"; |
| 37 | + private static final String MIN_SDK = "android-min-sdk"; |
| 38 | + private static final String ANDROID_PLATFORM = "android-platform"; |
| 39 | + private static final String ANDROID_INSTALLER = "android-installer"; |
| 40 | + private static final String KOTLIN = "kotlin"; |
| 41 | + |
| 42 | + @Override |
| 43 | + public List<Component<?>> getComponents() { |
| 44 | + List<Component<?>> result = new ArrayList<>(); |
| 45 | + result.add(DefaultUserAgentPublisher.component()); |
| 46 | + result.add(DefaultHeartBeatInfo.component()); |
| 47 | + result.add( |
| 48 | + LibraryVersionComponent.create(FIREBASE_ANDROID, String.valueOf(Build.VERSION.SDK_INT))); |
| 49 | + result.add(LibraryVersionComponent.create(FIREBASE_COMMON, BuildConfig.VERSION_NAME)); |
| 50 | + result.add(LibraryVersionComponent.create(DEVICE_NAME, safeValue(Build.PRODUCT))); |
| 51 | + result.add(LibraryVersionComponent.create(DEVICE_MODEL, safeValue(Build.DEVICE))); |
| 52 | + result.add(LibraryVersionComponent.create(DEVICE_BRAND, safeValue(Build.BRAND))); |
| 53 | + result.add( |
| 54 | + LibraryVersionComponent.fromContext( |
| 55 | + TARGET_SDK, |
| 56 | + ctx -> { |
| 57 | + ApplicationInfo info = ctx.getApplicationInfo(); |
| 58 | + if (info != null) { |
| 59 | + return String.valueOf(info.targetSdkVersion); |
| 60 | + } |
| 61 | + return ""; |
| 62 | + })); |
| 63 | + result.add( |
| 64 | + LibraryVersionComponent.fromContext( |
| 65 | + MIN_SDK, |
| 66 | + ctx -> { |
| 67 | + ApplicationInfo info = ctx.getApplicationInfo(); |
| 68 | + if (info != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 69 | + return String.valueOf(info.minSdkVersion); |
| 70 | + } |
| 71 | + return ""; |
| 72 | + })); |
| 73 | + result.add( |
| 74 | + LibraryVersionComponent.fromContext( |
| 75 | + ANDROID_PLATFORM, |
| 76 | + ctx -> { |
| 77 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN |
| 78 | + && ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEVISION)) { |
| 79 | + return "tv"; |
| 80 | + } |
| 81 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH |
| 82 | + && ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH)) { |
| 83 | + return "watch"; |
| 84 | + } |
| 85 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M |
| 86 | + && ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) { |
| 87 | + return "auto"; |
| 88 | + } |
| 89 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O |
| 90 | + && ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_EMBEDDED)) { |
| 91 | + return "embedded"; |
| 92 | + } |
| 93 | + return ""; |
| 94 | + })); |
| 95 | + result.add( |
| 96 | + LibraryVersionComponent.fromContext( |
| 97 | + ANDROID_INSTALLER, |
| 98 | + ctx -> { |
| 99 | + String installer = |
| 100 | + ctx.getPackageManager().getInstallerPackageName(ctx.getPackageName()); |
| 101 | + return (installer != null) ? safeValue(installer) : ""; |
| 102 | + })); |
| 103 | + |
| 104 | + String kotlinVersion = KotlinDetector.detectVersion(); |
| 105 | + if (kotlinVersion != null) { |
| 106 | + result.add(LibraryVersionComponent.create(KOTLIN, kotlinVersion)); |
| 107 | + } |
| 108 | + return result; |
| 109 | + } |
| 110 | + |
| 111 | + private static String safeValue(String value) { |
| 112 | + return value.replace(' ', '_').replace('/', '_'); |
| 113 | + } |
| 114 | +} |
0 commit comments