Skip to content

Commit

Permalink
Add remaining regex patterns & fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenHenning committed Sep 15, 2021
1 parent abb2164 commit 2ef0ea7
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.text.TextUtilsCompat
import androidx.core.view.ViewCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import javax.inject.Inject
import org.oppia.android.R
import java.util.Locale
import org.oppia.android.app.translation.AppLanguageResourceHandler
import org.oppia.android.app.view.ViewComponentFactory
import org.oppia.android.app.view.ViewComponentImpl

private const val STROKE_DASH_GAP_IN_DEGREE = 12

Expand All @@ -22,10 +26,14 @@ private const val STROKE_DASH_GAP_IN_DEGREE = 12
* Reference: // https://stackoverflow.com/a/39210676
*/
class SegmentedCircularProgressView : View {
@Inject
lateinit var resourceHandler: AppLanguageResourceHandler

private var sweepAngle = 0f
private var strokeWidth = 0f
private val isRTL = TextUtilsCompat
.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL
private val isRtl by lazy {
resourceHandler.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL
}

private lateinit var baseRect: RectF
private lateinit var chapterFinishedArcPaint: Paint
Expand Down Expand Up @@ -81,8 +89,16 @@ class SegmentedCircularProgressView : View {
}
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()

val viewComponentFactory = FragmentManager.findFragment<Fragment>(this) as ViewComponentFactory
val viewComponent = viewComponentFactory.createViewComponent(this) as ViewComponentImpl
viewComponent.inject(this)
}

override fun onDraw(canvas: Canvas) {
if (isRTL)
if (isRtl)
rotationY = 180f
super.onDraw(canvas)
if (!this::baseRect.isInitialized) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.oppia.android.app.help.faq.faqItemViewModel.FAQContentViewModel
import org.oppia.android.app.help.faq.faqItemViewModel.FAQHeaderViewModel
import org.oppia.android.app.help.faq.faqItemViewModel.FAQItemViewModel
import org.oppia.android.app.viewmodel.ObservableViewModel
import java.util.Locale
import javax.inject.Inject
import org.oppia.android.app.translation.AppLanguageResourceHandler

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import org.oppia.android.util.gcsresource.DefaultResourceBucketName
import org.oppia.android.util.parser.html.ExplorationHtmlParserEntityType
import org.oppia.android.util.parser.html.HtmlParser
import java.lang.IllegalStateException
import java.util.Locale
import javax.inject.Inject
import org.oppia.android.app.model.WrittenTranslationContext
import org.oppia.android.app.translation.AppLanguageResourceHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.view.ContextThemeWrapper
import androidx.fragment.app.DialogFragment
import org.oppia.android.R
import java.util.Locale
import kotlin.collections.ArrayList
Expand Down Expand Up @@ -59,6 +58,7 @@ class LanguageDialogFragment : InjectableDialogFragment() {
if (languageCode == "hi-en") {
languageNameArrayList.add("Hinglish")
} else {
// TODO(#3791): Remove this dependency.
val locale = Locale(languageCode)
val name = locale.getDisplayLanguage(locale)
languageNameArrayList.add(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class AppLanguageResourceHandler @Inject constructor(
fun computeDateTimeString(timestampMillis: Long): String =
getDisplayLocale().computeDateTimeString(timestampMillis)

fun getLayoutDirection(): Int = getDisplayLocale().getLayoutDirection()

private fun getDisplayLocale(): OppiaLocale.DisplayLocale =
appLanguageLocaleHandler.getDisplayLocale()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.view.View
import dagger.BindsInstance
import dagger.Subcomponent
import org.oppia.android.app.customview.LessonThumbnailImageView
import org.oppia.android.app.customview.SegmentedCircularProgressView
import org.oppia.android.app.home.promotedlist.ComingSoonTopicsListView
import org.oppia.android.app.home.promotedlist.PromotedStoryListView
import org.oppia.android.app.player.state.DragDropSortInteractionView
Expand All @@ -30,4 +31,5 @@ interface ViewComponentImpl: ViewComponent {
fun inject(imageRegionSelectionInteractionView: ImageRegionSelectionInteractionView)
fun inject(lessonThumbnailImageView: LessonThumbnailImageView)
fun inject(promotedStoryListView: PromotedStoryListView)
fun inject(segmentedCircularProgressView: SegmentedCircularProgressView)
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ class AppVersionActivityTest {
}
}

// TODO(#3792): Remove this usage of Locale (probably by introducing a test utility in the locale
// package to generate these strings).
private fun getDateTime(dateTimeTimestamp: Long): String? {
return oppiaDateTimeFormatter.formatDateFromDateString(
OppiaDateTimeFormatter.DD_MMM_YYYY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ class AudioFragmentTest {
testCoroutineDispatchers.runCurrent()
onView(withId(R.id.audio_language_icon)).perform(click())

// TODO(#3791): Remove this dependency.
val locale = Locale("es")

testCoroutineDispatchers.runCurrent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class SplashActivityTest {
@Inject
lateinit var fakeMetaDataRetriever: FakeExpirationMetaDataRetriever

// TODO(#3792): Remove this usage of Locale (probably by introducing a test utility in the locale
// package to generate these strings).
private val expirationDateFormat by lazy { SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) }

@Before
Expand Down
1 change: 1 addition & 0 deletions domain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ TEST_DEPS = [
"//third_party:robolectric_android-all",
"//utility/src/main/java/org/oppia/android/util/caching/testing:caching_test_module",
"//utility/src/main/java/org/oppia/android/util/extensions:context_extensions",
"//utility/src/main/java/org/oppia/android/util/locale:prod_module",
"//utility/src/main/java/org/oppia/android/util/networking:debug_module",
"//utility/src/main/java/org/oppia/android/util/networking:debug_util_module",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package org.oppia.android.domain.onboarding

import javax.inject.Inject
import javax.inject.Singleton
import org.oppia.android.app.model.AppStartupState
import org.oppia.android.app.model.AppStartupState.StartupMode
import org.oppia.android.app.model.OnboardingState
import org.oppia.android.data.persistence.PersistentCacheStore
import org.oppia.android.domain.oppialogger.OppiaLogger
import org.oppia.android.util.data.DataProvider
import org.oppia.android.util.data.DataProviders.Companion.transform
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import javax.inject.Inject
import javax.inject.Singleton
import org.oppia.android.util.extensions.getStringFromBundle
import org.oppia.android.util.locale.OppiaLocale

private const val APP_STARTUP_STATE_DATA_PROVIDER_ID = "app_startup_state_data_provider_id"

Expand All @@ -22,10 +19,9 @@ private const val APP_STARTUP_STATE_DATA_PROVIDER_ID = "app_startup_state_data_p
class AppStartupStateController @Inject constructor(
cacheStoreFactory: PersistentCacheStore.Factory,
private val oppiaLogger: OppiaLogger,
private val expirationMetaDataRetriever: ExpirationMetaDataRetriever
private val expirationMetaDataRetriever: ExpirationMetaDataRetriever,
private val machineLocale: OppiaLocale.MachineLocale
) {
private val expirationDateFormat by lazy { SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) }

private val onboardingFlowStore =
cacheStoreFactory.create("on_boarding_flow", OnboardingState.getDefaultInstance())

Expand Down Expand Up @@ -88,18 +84,9 @@ class AppStartupStateController @Inject constructor(
) ?: true
return if (isAppExpirationEnabled) {
val expirationDateString = applicationMetadata?.getStringFromBundle("expiration_date")
val expirationDate = expirationDateString?.let { parseDate(it) }
val expirationDate = expirationDateString?.let { machineLocale.parseOppiaDate(it) }
// Assume the app is in an expired state if something fails when comparing the date.
expirationDate?.before(Date()) ?: true
expirationDate?.isBeforeToday() ?: true
} else false
}

private fun parseDate(dateString: String): Date? {
return try {
expirationDateFormat.parse(dateString)
} catch (e: ParseException) {
oppiaLogger.e("DOMAIN", "Failed to parse date string: $dateString", e)
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import dagger.BindsInstance
import dagger.Component
import dagger.Module
import dagger.Provides
import java.text.SimpleDateFormat
import java.time.Duration
import java.time.Instant
import java.util.Date
import java.util.Locale
import javax.inject.Inject
import javax.inject.Singleton
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -40,20 +47,15 @@ import org.oppia.android.util.data.AsyncResult
import org.oppia.android.util.data.DataProviders.Companion.toLiveData
import org.oppia.android.util.data.DataProvidersInjector
import org.oppia.android.util.data.DataProvidersInjectorProvider
import org.oppia.android.util.locale.MachineLocaleModule
import org.oppia.android.util.logging.EnableConsoleLog
import org.oppia.android.util.logging.EnableFileLog
import org.oppia.android.util.logging.GlobalLogLevel
import org.oppia.android.util.logging.LogLevel
import org.oppia.android.util.networking.NetworkConnectionUtilDebugModule
import org.oppia.android.util.system.OppiaClockModule
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import java.text.SimpleDateFormat
import java.time.Duration
import java.time.Instant
import java.util.Date
import java.util.Locale
import javax.inject.Inject
import javax.inject.Singleton

/** Tests for [AppStartupStateController]. */
@RunWith(AndroidJUnit4::class)
Expand Down Expand Up @@ -85,6 +87,8 @@ class AppStartupStateControllerTest {
@Captor
lateinit var appStartupStateCaptor: ArgumentCaptor<AsyncResult<AppStartupState>>

// TODO(#3792): Remove this usage of Locale (probably by introducing a test utility in the locale
// package to generate these strings).
private val expirationDateFormat by lazy { SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) }

@Test
Expand Down Expand Up @@ -431,6 +435,7 @@ class AppStartupStateControllerTest {
LogStorageModule::class, RobolectricModule::class,
TestModule::class, TestDispatcherModule::class, TestLogReportingModule::class,
NetworkConnectionUtilDebugModule::class,
OppiaClockModule::class, MachineLocaleModule::class,
ExpirationMetaDataRetrieverModule::class // Use real implementation to test closer to prod.
]
)
Expand Down
44 changes: 44 additions & 0 deletions scripts/assets/file_content_validation_checks.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,47 @@ file_content_checks {
prohibited_content_regex: "android:configChanges"
failure_message: "Never explicitly handle configuration changes. Instead, use saved instance states for retaining state across rotations. For other types of configuration changes, follow up with the developer mailing list with how to proceed if you think this is a legitimate case."
}
file_content_checks {
file_path_regex: ".+?.kt"
prohibited_content_regex: "java\\.util\\.Calendar"
failure_message: "Don't use Calendar directly. Instead, use OppiaClock and/or OppiaLocale for calendar-specific operations."
exempted_file_name: "testing/src/main/java/org/oppia/android/testing/time/FakeOppiaClock.kt"
exempted_file_name: "testing/src/test/java/org/oppia/android/testing/time/FakeOppiaClockTest.kt"
exempted_file_name: "utility/src/main/java/org/oppia/android/util/locale/MachineLocaleImpl.kt"
exempted_file_name: "utility/src/main/java/org/oppia/android/util/system/OppiaClock.kt"
}
file_content_checks {
file_path_regex: ".+?.kt"
prohibited_content_regex: "java\\.util\\.Date"
failure_message: "Don't use Date directly. Instead, perform date-based operations using OppiaLocale."
exempted_file_name: "app/src/sharedTest/java/org/oppia/android/app/splash/SplashActivityTest.kt"
exempted_file_name: "domain/src/main/java/org/oppia/android/domain/locale/DisplayLocaleImpl.kt"
exempted_file_name: "domain/src/test/java/org/oppia/android/domain/onboarding/AppStartupStateControllerTest.kt"
exempted_file_name: "testing/src/main/java/org/oppia/android/testing/time/FakeOppiaClock.kt"
exempted_file_name: "utility/src/main/java/org/oppia/android/util/system/OppiaClock.kt"
exempted_file_name: "utility/src/main/java/org/oppia/android/util/locale/MachineLocaleImpl.kt"
}
file_content_checks {
file_path_regex: ".+?.kt"
prohibited_content_regex: "java\\.text"
failure_message: "Don't perform date/time formatting directly. Instead, use OppiaLocale."
exempted_file_name: "app/src/sharedTest/java/org/oppia/android/app/splash/SplashActivityTest.kt"
exempted_file_name: "domain/src/main/java/org/oppia/android/domain/locale/DisplayLocaleImpl.kt"
exempted_file_name: "domain/src/test/java/org/oppia/android/domain/onboarding/AppStartupStateControllerTest.kt"
exempted_file_name: "testing/src/main/java/org/oppia/android/testing/time/FakeOppiaClock.kt"
exempted_file_name: "utility/src/main/java/org/oppia/android/util/locale/MachineLocaleImpl.kt"
}
file_content_checks {
file_path_regex: ".+?.kt"
prohibited_content_regex: "java\\.util\\.Locale"
failure_message: "Don't use Locale directly. Instead, use LocaleController, or OppiaLocale & its subclasses."
exempted_file_name: "app/src/main/java/org/oppia/android/app/player/audio/LanguageDialogFragment.kt"
exempted_file_name: "app/src/sharedTest/java/org/oppia/android/app/administratorcontrols/AppVersionActivityTest.kt"
exempted_file_name: "app/src/sharedTest/java/org/oppia/android/app/player/audio/AudioFragmentTest.kt"
exempted_file_name: "app/src/sharedTest/java/org/oppia/android/app/splash/SplashActivityTest.kt"
exempted_file_patterns: "domain/src/main/java/org/oppia/android/domain/locale/.+?\\.kt"
exempted_file_name: "domain/src/test/java/org/oppia/android/domain/onboarding/AppStartupStateControllerTest.kt"
exempted_file_patterns: "scripts/.+?\\.kt"
exempted_file_patterns: "utility/src/main/java/org/oppia/android/util/locale/.+?\\.kt"
exempted_file_name: "utility/src/main/java/org/oppia/android/util/logging/firebase/FirebaseEventLogger.kt"
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fun main(args: Array<String>) {

// Needed since the codebase isn't yet using Kotlin 1.5, so this function isn't available.
private fun String.toBooleanStrictOrNull(): Boolean? {
return when (toLowerCase(Locale.getDefault())) {
return when (toLowerCase(Locale.US)) {
"false" -> false
"true" -> true
else -> null
Expand Down Expand Up @@ -110,7 +110,7 @@ class ComputeAffectedTests(
println("Current branch: ${gitClient.currentBranch}")
println("Most recent common commit: ${gitClient.branchMergeBase}")

val currentBranch = gitClient.currentBranch.toLowerCase(Locale.getDefault())
val currentBranch = gitClient.currentBranch.toLowerCase(Locale.US)
val affectedTestTargets = if (computeAllTestsSetting || currentBranch == "develop") {
computeAllTestTargets(bazelClient)
} else computeAffectedTargetsForNonDevelopBranch(gitClient, bazelClient, rootDirectory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ kt_android_library(
":log_level",
"//third_party:javax_inject_javax_inject",
"//third_party:org_jetbrains_kotlinx_kotlinx-coroutines-core",
"//utility/src/main/java/org/oppia/android/util/locale:oppia_locale",
"//utility/src/main/java/org/oppia/android/util/threading:annotations",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.oppia.android.util.threading.BlockingDispatcher
import java.io.File
import java.util.Calendar
import javax.inject.Inject
import javax.inject.Singleton
import org.oppia.android.util.locale.OppiaLocale

/** Wrapper class for Android logcat and file logging. All logs in the app should use this class. */
@Singleton
Expand All @@ -18,7 +18,8 @@ class ConsoleLogger @Inject constructor(
@BlockingDispatcher private val blockingDispatcher: CoroutineDispatcher,
@EnableConsoleLog private val enableConsoleLog: Boolean,
@EnableFileLog private val enableFileLog: Boolean,
@GlobalLogLevel private val globalLogLevel: LogLevel
@GlobalLogLevel private val globalLogLevel: LogLevel,
private val machineLocale: OppiaLocale.MachineLocale
) {
private val blockingScope = CoroutineScope(blockingDispatcher)
private val logDirectory = File(context.filesDir, "oppia_app.log")
Expand Down Expand Up @@ -93,7 +94,9 @@ class ConsoleLogger @Inject constructor(
Log.println(logLevel.logLevel, tag, fullLog)
}
if (enableFileLog) {
logToFileInBackground("${Calendar.getInstance().time}\t${logLevel.name}/$tag: $fullLog")
logToFileInBackground(
"${machineLocale.computeCurrentTimeString()}\t${logLevel.name}/$tag: $fullLog"
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class FirebaseEventLogger(
override fun logEvent(eventLog: EventLog) {
bundle = eventBundleCreator.createEventBundle(eventLog)
firebaseAnalytics.logEvent(eventLog.actionName.toString(), bundle)
// TODO(#3792): Remove this usage of Locale.
firebaseAnalytics.setUserProperty(COUNTRY_USER_PROPERTY, Locale.getDefault().displayCountry)
firebaseAnalytics.setUserProperty(
NETWORK_USER_PROPERTY, connectivityManager.activeNetworkInfo.typeName
Expand Down

0 comments on commit 2ef0ea7

Please sign in to comment.