From f7b471f141649ce9902c160252787fb5dd972f8c Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 7 Aug 2019 17:53:26 +0200 Subject: [PATCH] Stop using BuildConfig.VERSION_CODE, it is not the correct value --- .../java/im/vector/riotx/VectorApplication.kt | 5 +- .../vector/riotx/core/di/ScreenComponent.kt | 2 + .../core/resources/VersionCodeProvider.kt | 40 ++++++++++++++ .../riotx/features/rageshake/BugReporter.kt | 7 +-- .../VectorUncaughtExceptionHandler.kt | 25 +++------ .../VectorSettingsHelpAboutFragment.kt | 12 ++++- .../vector/riotx/features/version/Version.kt | 49 ----------------- .../riotx/features/version/VersionProvider.kt | 54 +++++++++++++++++++ 8 files changed, 121 insertions(+), 73 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotx/core/resources/VersionCodeProvider.kt delete mode 100644 vector/src/main/java/im/vector/riotx/features/version/Version.kt create mode 100644 vector/src/main/java/im/vector/riotx/features/version/VersionProvider.kt diff --git a/vector/src/main/java/im/vector/riotx/VectorApplication.kt b/vector/src/main/java/im/vector/riotx/VectorApplication.kt index 308daec2..66356ac0 100644 --- a/vector/src/main/java/im/vector/riotx/VectorApplication.kt +++ b/vector/src/main/java/im/vector/riotx/VectorApplication.kt @@ -51,7 +51,7 @@ import im.vector.riotx.features.notifications.PushRuleTriggerListener import im.vector.riotx.features.rageshake.VectorFileLogger import im.vector.riotx.features.rageshake.VectorUncaughtExceptionHandler import im.vector.riotx.features.settings.VectorPreferences -import im.vector.riotx.features.version.getVersion +import im.vector.riotx.features.version.VersionProvider import im.vector.riotx.push.fcm.FcmHelper import timber.log.Timber import java.text.SimpleDateFormat @@ -71,6 +71,7 @@ class VectorApplication : Application(), HasVectorInjector, MatrixConfiguration. @Inject lateinit var notificationDrawerManager: NotificationDrawerManager @Inject lateinit var pushRuleTriggerListener: PushRuleTriggerListener @Inject lateinit var vectorPreferences: VectorPreferences + @Inject lateinit var versionProvider: VersionProvider lateinit var vectorComponent: VectorComponent private var fontThreadHandler: Handler? = null @@ -140,7 +141,7 @@ class VectorApplication : Application(), HasVectorInjector, MatrixConfiguration. } private fun logInfo() { - val appVersion = getVersion(longFormat = true, useBuildNumber = true) + val appVersion = versionProvider.getVersion(longFormat = true, useBuildNumber = true) val sdkVersion = Matrix.getSdkVersion() val date = SimpleDateFormat("MM-dd HH:mm:ss.SSSZ", Locale.US).format(Date()) diff --git a/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt b/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt index bfea80bc..fb1c18f9 100644 --- a/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt +++ b/vector/src/main/java/im/vector/riotx/core/di/ScreenComponent.kt @@ -154,6 +154,8 @@ interface ScreenComponent { fun inject(vectorSettingsSecurityPrivacyFragment: VectorSettingsSecurityPrivacyFragment) + fun inject(vectorSettingsHelpAboutFragment: VectorSettingsHelpAboutFragment) + fun inject(userAvatarPreference: UserAvatarPreference) fun inject(vectorSettingsNotificationsTroubleshootFragment: VectorSettingsNotificationsTroubleshootFragment) diff --git a/vector/src/main/java/im/vector/riotx/core/resources/VersionCodeProvider.kt b/vector/src/main/java/im/vector/riotx/core/resources/VersionCodeProvider.kt new file mode 100644 index 00000000..e05db170 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/core/resources/VersionCodeProvider.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotx.core.resources + +import android.content.Context +import android.os.Build +import androidx.annotation.NonNull +import javax.inject.Inject + +class VersionCodeProvider @Inject constructor(private val context: Context) { + + /** + * Returns the version code, read from the Manifest. It is not the same than BuildConfig.VERSION_CODE due to versionCodeOverride + */ + @NonNull + fun getVersionCode(): Long { + val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0) + + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + packageInfo.longVersionCode + } else { + @Suppress("DEPRECATION") + packageInfo.versionCode.toLong() + } + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt b/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt index d34d2be4..d21ad021 100755 --- a/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt +++ b/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt @@ -35,7 +35,7 @@ import im.vector.riotx.core.extensions.toOnOff import im.vector.riotx.core.utils.getDeviceLocale import im.vector.riotx.features.settings.VectorLocale import im.vector.riotx.features.themes.ThemeUtils -import im.vector.riotx.features.version.getVersion +import im.vector.riotx.features.version.VersionProvider import okhttp3.* import org.json.JSONException import org.json.JSONObject @@ -51,7 +51,8 @@ import javax.inject.Singleton * BugReporter creates and sends the bug reports. */ @Singleton -class BugReporter @Inject constructor(private val activeSessionHolder: ActiveSessionHolder) { +class BugReporter @Inject constructor(private val activeSessionHolder: ActiveSessionHolder, + private val versionProvider: VersionProvider) { var inMultiWindowMode = false companion object { @@ -225,7 +226,7 @@ class BugReporter @Inject constructor(private val activeSessionHolder: ActiveSes .addFormDataPart("user_agent", Matrix.getInstance(context).getUserAgent()) .addFormDataPart("user_id", userId) .addFormDataPart("device_id", deviceId) - .addFormDataPart("version", getVersion(longFormat = true, useBuildNumber = false)) + .addFormDataPart("version", versionProvider.getVersion(longFormat = true, useBuildNumber = false)) .addFormDataPart("branch_name", context.getString(R.string.git_branch_name)) .addFormDataPart("matrix_sdk_version", Matrix.getSdkVersion()) .addFormDataPart("olm_version", olmVersion) diff --git a/vector/src/main/java/im/vector/riotx/features/rageshake/VectorUncaughtExceptionHandler.kt b/vector/src/main/java/im/vector/riotx/features/rageshake/VectorUncaughtExceptionHandler.kt index 86e4d9ee..89e386e4 100644 --- a/vector/src/main/java/im/vector/riotx/features/rageshake/VectorUncaughtExceptionHandler.kt +++ b/vector/src/main/java/im/vector/riotx/features/rageshake/VectorUncaughtExceptionHandler.kt @@ -21,8 +21,8 @@ import android.os.Build import androidx.core.content.edit import androidx.preference.PreferenceManager import im.vector.matrix.android.api.Matrix -import im.vector.riotx.BuildConfig -import im.vector.riotx.features.version.getVersion +import im.vector.riotx.core.resources.VersionCodeProvider +import im.vector.riotx.features.version.VersionProvider import timber.log.Timber import java.io.PrintWriter import java.io.StringWriter @@ -30,16 +30,15 @@ import javax.inject.Inject import javax.inject.Singleton @Singleton -class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter: BugReporter) : Thread.UncaughtExceptionHandler { +class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter: BugReporter, + private val versionProvider: VersionProvider, + private val versionCodeProvider: VersionCodeProvider) : Thread.UncaughtExceptionHandler { // key to save the crash status companion object { private const val PREFS_CRASH_KEY = "PREFS_CRASH_KEY" } - private val vectorVersion = getVersion(longFormat = true, useBuildNumber = true) - private val matrixSdkVersion = Matrix.getSdkVersion() - private var previousHandler: Thread.UncaughtExceptionHandler? = null private lateinit var context: Context @@ -68,9 +67,9 @@ class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter val b = StringBuilder() val appName = "RiotX" // TODO Matrix.getApplicationName() - b.append(appName + " Build : " + BuildConfig.VERSION_CODE + "\n") - b.append("$appName Version : $vectorVersion\n") - b.append("SDK Version : $matrixSdkVersion\n") + b.append(appName + " Build : " + versionCodeProvider.getVersionCode() + "\n") + b.append("$appName Version : ${versionProvider.getVersion(longFormat = true, useBuildNumber = true)}\n") + b.append("SDK Version : ${Matrix.getSdkVersion()}\n") b.append("Phone : " + Build.MODEL.trim() + " (" + Build.VERSION.INCREMENTAL + " " + Build.VERSION.RELEASE + " " + Build.VERSION.CODENAME + ")\n") b.append("Memory statuses \n") @@ -94,14 +93,6 @@ class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter b.append("Thread: ") b.append(thread.name) - /* - val a = VectorApp.getCurrentActivity() - if (a != null) { - b.append(", Activity:") - b.append(a.localClassName) - } - */ - b.append(", Exception: ") val sw = StringWriter() diff --git a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsHelpAboutFragment.kt b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsHelpAboutFragment.kt index d27ae075..5408eb4e 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsHelpAboutFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsHelpAboutFragment.kt @@ -23,15 +23,23 @@ import androidx.preference.Preference import com.google.android.gms.oss.licenses.OssLicensesMenuActivity import im.vector.matrix.android.api.Matrix import im.vector.riotx.R +import im.vector.riotx.core.di.ScreenComponent import im.vector.riotx.core.utils.copyToClipboard import im.vector.riotx.core.utils.displayInWebView -import im.vector.riotx.features.version.getVersion +import im.vector.riotx.features.version.VersionProvider +import javax.inject.Inject class VectorSettingsHelpAboutFragment : VectorSettingsBaseFragment() { override var titleRes = R.string.preference_root_help_about override val preferenceXmlRes = R.xml.vector_settings_help_about + @Inject lateinit var versionProvider: VersionProvider + + override fun injectWith(injector: ScreenComponent) { + injector.inject(this) + } + override fun bindPref() { // preference to start the App info screen, to facilitate App permissions access findPreference(APP_INFO_LINK_PREFERENCE_KEY) @@ -54,7 +62,7 @@ class VectorSettingsHelpAboutFragment : VectorSettingsBaseFragment() { // application version (findPreference(VectorPreferences.SETTINGS_VERSION_PREFERENCE_KEY)).let { - it.summary = getVersion(longFormat = false, useBuildNumber = true) + it.summary = versionProvider.getVersion(longFormat = false, useBuildNumber = true) it.setOnPreferenceClickListener { pref -> copyToClipboard(requireContext(), pref.summary) diff --git a/vector/src/main/java/im/vector/riotx/features/version/Version.kt b/vector/src/main/java/im/vector/riotx/features/version/Version.kt deleted file mode 100644 index af916e6d..00000000 --- a/vector/src/main/java/im/vector/riotx/features/version/Version.kt +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package im.vector.riotx.features.version - -import im.vector.riotx.BuildConfig - -fun getVersion(longFormat: Boolean, useBuildNumber: Boolean): String { - var result = "${BuildConfig.VERSION_NAME} [${BuildConfig.VERSION_CODE}]" - - var flavor = BuildConfig.SHORT_FLAVOR_DESCRIPTION - - if (flavor.isNotBlank()) { - flavor += "-" - } - - var gitVersion = BuildConfig.GIT_REVISION - val gitRevisionDate = BuildConfig.GIT_REVISION_DATE - val buildNumber = BuildConfig.BUILD_NUMBER - - var useLongFormat = longFormat - - if (useBuildNumber && buildNumber != "0") { - // It's a build from CI - gitVersion = "b$buildNumber" - useLongFormat = false - } - - result += if (useLongFormat) { - " ($flavor$gitVersion-$gitRevisionDate)" - } else { - " ($flavor$gitVersion)" - } - - return result -} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotx/features/version/VersionProvider.kt b/vector/src/main/java/im/vector/riotx/features/version/VersionProvider.kt new file mode 100644 index 00000000..4891abaa --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/version/VersionProvider.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotx.features.version + +import im.vector.riotx.BuildConfig +import im.vector.riotx.core.resources.VersionCodeProvider +import javax.inject.Inject + +class VersionProvider @Inject constructor(private val versionCodeProvider: VersionCodeProvider) { + + fun getVersion(longFormat: Boolean, useBuildNumber: Boolean): String { + var result = "${BuildConfig.VERSION_NAME} [${versionCodeProvider.getVersionCode()}]" + + var flavor = BuildConfig.SHORT_FLAVOR_DESCRIPTION + + if (flavor.isNotBlank()) { + flavor += "-" + } + + var gitVersion = BuildConfig.GIT_REVISION + val gitRevisionDate = BuildConfig.GIT_REVISION_DATE + val buildNumber = BuildConfig.BUILD_NUMBER + + var useLongFormat = longFormat + + if (useBuildNumber && buildNumber != "0") { + // It's a build from CI + gitVersion = "b$buildNumber" + useLongFormat = false + } + + result += if (useLongFormat) { + " ($flavor$gitVersion-$gitRevisionDate)" + } else { + " ($flavor$gitVersion)" + } + + return result + } +} \ No newline at end of file