Skip to content

Commit

Permalink
[Teacher][Parent] Add custom language pack support (#120)
Browse files Browse the repository at this point in the history
* [WIP][Teacher][Parent] Add custom language pack support

* Made canvas locale work for parent, minor cleanup

* Update tapes. Set locale during token login to avoid app restart.

* Fix missing strings for unimelb, also changed langugaes to use new 'editDashboard' instead of 'editFavorites'

* Revert "Fix missing strings for unimelb, also changed langugaes to use new 'editDashboard' instead of 'editFavorites'"

This reverts commit 9e965ce.

* Fix missing strings for unimelb, also changed languages to use new 'editDashboard' instead of 'editFavorites'
  • Loading branch information
JordanMarshall authored and CalvinKern committed Jun 27, 2019
1 parent 4d1d729 commit 6156efc
Show file tree
Hide file tree
Showing 43 changed files with 99 additions and 88 deletions.
2 changes: 1 addition & 1 deletion android-vault
4 changes: 3 additions & 1 deletion apps/parent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
import com.instructure.android.buildtools.transform.ProjectTransformer
import com.instructure.android.buildtools.transform.MasqueradeUITransformer
import com.instructure.android.buildtools.transform.LocaleTransformer

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
Expand Down Expand Up @@ -149,7 +150,8 @@ android {
registerTransform(
new ProjectTransformer(
android,
new MasqueradeUITransformer('com.instructure.parentapp.activity.LoginActivity.class')
new MasqueradeUITransformer('com.instructure.parentapp.activity.LoginActivity.class'),
new LocaleTransformer()
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.instructure.canvasapi2.managers.UserManager
import com.instructure.canvasapi2.models.Account
import com.instructure.canvasapi2.models.BecomeUserPermission
import com.instructure.canvasapi2.models.Enrollment
import com.instructure.canvasapi2.models.User
import com.instructure.canvasapi2.utils.*
import com.instructure.canvasapi2.utils.weave.StatusCallbackError
import com.instructure.canvasapi2.utils.weave.awaitApi
Expand All @@ -38,6 +39,8 @@ import com.instructure.canvasapi2.utils.weave.tryWeave
import com.instructure.loginapi.login.tasks.LogoutTask
import com.instructure.pandautils.utils.Const
import com.instructure.pandautils.utils.setGone
import com.instructure.pandautils.utils.toast
import com.instructure.parentapp.BuildConfig
import com.instructure.parentapp.R
import com.instructure.parentapp.fragments.NotAParentFragment
import com.instructure.parentapp.tasks.ParentLogoutTask
Expand Down Expand Up @@ -75,6 +78,14 @@ class SplashActivity : AppCompatActivity() {
val token = ApiPrefs.token
ApiPrefs.protocol = "https"

val user = awaitApi<User> { UserManager.getSelf(true, it) }
val shouldRestartForLocaleChange = setupUser(user)
if (shouldRestartForLocaleChange) {
if (BuildConfig.DEBUG) toast(R.string.localeRestartMessage)
LocaleUtils.restartApp(this@SplashActivity, LoginActivity::class.java)
return@tryWeave
}

if (ParentPrefs.isObserver == null) {
val enrollments = awaitApi<List<Enrollment>> { EnrollmentManager.getObserveeEnrollments(true, it) }
ParentPrefs.isObserver = enrollments.any { it.isObserver }
Expand Down Expand Up @@ -133,6 +144,13 @@ class SplashActivity : AppCompatActivity() {
}
}

/** Caches the user in ApiPrefs. Returns true if the user's locale has changed and an app restart is required. */
private fun setupUser(user: User): Boolean {
val oldLocale = ApiPrefs.effectiveLocale
ApiPrefs.user = user
return ApiPrefs.effectiveLocale != oldLocale
}

private fun navigateLoginLandingPage() {
startActivity(LoginActivity.createIntent(ContextKeeper.appContext))
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import com.instructure.student.BuildConfig
import com.instructure.student.R
import com.instructure.student.fragment.InboxFragment
import com.instructure.student.service.StudentPageViewService
import com.instructure.student.util.AppManager
import com.instructure.student.util.StudentPrefs
import kotlinx.coroutines.Job
import retrofit2.Call
Expand Down Expand Up @@ -165,7 +164,7 @@ abstract class CallbackActivity : ParentActivity(), InboxFragment.OnUnreadCountI
val shouldRestartForLocaleChange = setupUser(user, type)
if (shouldRestartForLocaleChange) {
if (BuildConfig.DEBUG) toast(R.string.localeRestartMessage)
AppManager.restartApp(this@CallbackActivity)
LocaleUtils.restartApp(this@CallbackActivity, LoginActivity::class.java)
} else {
loadInitialData()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ import com.instructure.canvasapi2.managers.CourseManager
import com.instructure.canvasapi2.managers.GroupManager
import com.instructure.canvasapi2.managers.UserManager
import com.instructure.canvasapi2.models.*
import com.instructure.canvasapi2.utils.APIHelper
import com.instructure.canvasapi2.utils.ApiPrefs
import com.instructure.canvasapi2.utils.Logger
import com.instructure.canvasapi2.utils.MasqueradeHelper
import com.instructure.canvasapi2.utils.*
import com.instructure.canvasapi2.utils.weave.*
import com.instructure.interactions.FragmentInteractions
import com.instructure.interactions.FullScreenInteractions
Expand All @@ -70,16 +67,13 @@ import com.instructure.pandautils.dialogs.UploadFilesDialog
import com.instructure.pandautils.models.PushNotification
import com.instructure.pandautils.receivers.PushExternalReceiver
import com.instructure.pandautils.utils.*
import com.instructure.pandautils.utils.Const.LANGUAGES_PENDING_INTENT_ID
import com.instructure.pandautils.utils.Const.LANGUAGES_PENDING_INTENT_KEY
import com.instructure.student.R
import com.instructure.student.dialog.BookmarkCreationDialog
import com.instructure.student.events.CoreDataFinishedLoading
import com.instructure.student.events.CourseColorOverlayToggledEvent
import com.instructure.student.events.ShowGradesToggledEvent
import com.instructure.student.events.UserUpdatedEvent
import com.instructure.student.fragment.*
import com.instructure.student.mobius.common.ui.SubmissionService
import com.instructure.student.router.RouteMatcher
import com.instructure.student.router.RouteResolver
import com.instructure.student.tasks.StudentLogoutTask
Expand Down Expand Up @@ -817,8 +811,8 @@ class NavigationActivity : BaseRouterActivity(), Navigation, MasqueradingDialog.
}

private fun hasPendingLanguageIntent(extras: Bundle?): Boolean {
return extras != null && extras.containsKey(LANGUAGES_PENDING_INTENT_KEY)
&& extras.getInt(LANGUAGES_PENDING_INTENT_KEY, 0) != LANGUAGES_PENDING_INTENT_ID
return extras != null && extras.containsKey(LocaleUtils.LANGUAGES_PENDING_INTENT_KEY)
&& extras.getInt(LocaleUtils.LANGUAGES_PENDING_INTENT_KEY, 0) != LocaleUtils.LANGUAGES_PENDING_INTENT_ID
}

//endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import android.widget.AdapterView
import android.widget.BaseAdapter
import androidx.core.content.ContextCompat
import com.instructure.canvasapi2.utils.ApiPrefs
import com.instructure.canvasapi2.utils.LocaleUtils
import com.instructure.canvasapi2.utils.cleanDisplayName
import com.instructure.canvasapi2.utils.pageview.PageView
import com.instructure.pandautils.utils.*
import com.instructure.student.BuildConfig
import com.instructure.student.R
import com.instructure.student.activity.LoginActivity
import com.instructure.student.util.AppManager
import com.instructure.student.util.StudentPrefs
import kotlinx.android.synthetic.main.fragment_account_preferences.*
import kotlinx.android.synthetic.main.settings_spinner.view.*
Expand Down Expand Up @@ -121,7 +122,7 @@ class AccountPreferencesFragment : ParentFragment() {
.setPositiveButton(R.string.yes) { _, _ ->
// Set the language
ApiPrefs.selectedLocale = languages[position].first
AppManager.restartApp(requireContext())
LocaleUtils.restartApp(requireContext(), LoginActivity::class.java)
}
.setNegativeButton(R.string.no, null)
.setCancelable(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,19 @@

package com.instructure.student.util

import android.content.Context
import android.content.res.Configuration
import android.webkit.WebView
import androidx.core.content.ContextCompat
import com.crashlytics.android.Crashlytics
import com.crashlytics.android.core.CrashlyticsCore
import com.google.android.gms.analytics.GoogleAnalytics
import com.google.android.gms.analytics.HitBuilders
import com.google.android.gms.analytics.Tracker
import com.instructure.canvasapi2.utils.ContextKeeper
import com.instructure.canvasapi2.utils.Logger
import com.instructure.canvasapi2.utils.pageview.PageViewUploadService
import com.instructure.loginapi.login.tasks.LogoutTask
import com.instructure.pandautils.utils.ColorKeeper
import com.instructure.pandautils.utils.LocaleUtils
import com.instructure.student.BuildConfig
import com.instructure.student.R
import com.instructure.student.activity.LoginActivity
import com.instructure.student.service.StudentPageViewService
import com.instructure.student.tasks.StudentLogoutTask
import com.pspdfkit.PSPDFKit
Expand Down Expand Up @@ -73,18 +68,6 @@ class AppManager : com.instructure.canvasapi2.AppManager(), AnalyticsEventHandli
PageViewUploadService.schedule(this, StudentPageViewService::class.java)
}

override fun attachBaseContext(base: Context) {
/* Need to set context in ContextKeeper here because LocaleUtils.wrapContext() accesses shared prefs
which uses ContextKeeper, but attachBaseContext occurs before ContentProvider init */
ContextKeeper.appContext = base
super.attachBaseContext(LocaleUtils.wrapContext(base))
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
LocaleUtils.wrapContext(this)
}

override fun trackButtonPressed(buttonName: String?, buttonValue: Long?) {
if (buttonName == null) return

Expand Down Expand Up @@ -190,12 +173,4 @@ class AppManager : com.instructure.canvasapi2.AppManager(), AnalyticsEventHandli
StudentLogoutTask(LogoutTask.Type.LOGOUT).execute()
}

companion object {

fun restartApp(context: Context) {
LocaleUtils.restartApp(context, LoginActivity::class.java)
}

}

}
4 changes: 3 additions & 1 deletion apps/teacher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
import com.instructure.android.buildtools.transform.ProjectTransformer
import com.instructure.android.buildtools.transform.MasqueradeUITransformer
import com.instructure.android.buildtools.transform.LocaleTransformer

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
Expand Down Expand Up @@ -168,7 +169,8 @@ android {
registerTransform(
new ProjectTransformer(
android,
new MasqueradeUITransformer('com.instructure.teacher.activities.LoginActivity.class')
new MasqueradeUITransformer('com.instructure.teacher.activities.LoginActivity.class'),
new LocaleTransformer()
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ fun TeacherTest.logIn(
id = teacher.id,
name = teacher.name,
shortName = teacher.shortName,
avatarUrl = teacher.avatarUrl
avatarUrl = teacher.avatarUrl,
locale = "en" // Must specify 'en' here to avoid restarting app due to locale change
),
skipSplash
)
Expand Down Expand Up @@ -343,7 +344,8 @@ fun TeacherTest.tokenLogin(teacher: CanvasUserApiModel, skipSplash: Boolean = tr
id = teacher.id,
name = teacher.name,
shortName = teacher.shortName,
avatarUrl = teacher.avatarUrl
avatarUrl = teacher.avatarUrl,
locale = "en" // Must specify 'en' here to avoid restarting app due to locale change
),
skipSplash
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.instructure.canvasapi2.managers.CourseNicknameManager
import com.instructure.canvasapi2.managers.UserManager
import com.instructure.canvasapi2.models.*
import com.instructure.canvasapi2.utils.ApiPrefs
import com.instructure.canvasapi2.utils.LocaleUtils
import com.instructure.canvasapi2.utils.MasqueradeHelper
import com.instructure.canvasapi2.utils.weave.awaitApi
import com.instructure.canvasapi2.utils.weave.catch
Expand Down Expand Up @@ -158,7 +159,7 @@ class InitActivity : BasePresenterActivity<InitActivityPresenter, InitActivityVi
super.onNewIntent(intent)
// Switching languages will also trigger this method; check for our Pending intent id
intent?.let {
if (it.hasExtra(Const.LANGUAGES_PENDING_INTENT_KEY) && it.getIntExtra(Const.LANGUAGES_PENDING_INTENT_KEY, 0) != Const.LANGUAGES_PENDING_INTENT_ID) {
if (it.hasExtra(LocaleUtils.LANGUAGES_PENDING_INTENT_KEY) && it.getIntExtra(LocaleUtils.LANGUAGES_PENDING_INTENT_KEY, 0) != LocaleUtils.LANGUAGES_PENDING_INTENT_ID) {
handlePushNotification(hasUnreadPushNotification(it.extras))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ import com.instructure.canvasapi2.managers.CourseManager
import com.instructure.canvasapi2.managers.ThemeManager
import com.instructure.canvasapi2.managers.UserManager
import com.instructure.canvasapi2.models.*
import com.instructure.canvasapi2.utils.ApiPrefs
import com.instructure.canvasapi2.utils.ApiType
import com.instructure.canvasapi2.utils.LinkHeaders
import com.instructure.canvasapi2.utils.Logger
import com.instructure.canvasapi2.utils.*
import com.instructure.canvasapi2.utils.weave.StatusCallbackError
import com.instructure.canvasapi2.utils.weave.awaitApi
import com.instructure.canvasapi2.utils.weave.weave
import com.instructure.pandautils.utils.ColorKeeper
import com.instructure.pandautils.utils.ThemePrefs
import com.instructure.pandautils.utils.setGone
import com.instructure.pandautils.utils.toast
import com.instructure.teacher.BuildConfig
import com.instructure.teacher.R
import com.instructure.teacher.fragments.NotATeacherFragment
import com.instructure.teacher.utils.TeacherPrefs
Expand Down Expand Up @@ -69,6 +68,14 @@ class SplashActivity : AppCompatActivity() {
// Grab user teacher status
try {

val user = awaitApi<User> { UserManager.getSelf(true, it) }
val shouldRestartForLocaleChange = setupUser(user)
if (shouldRestartForLocaleChange) {
if (BuildConfig.DEBUG) toast(R.string.localeRestartMessage)
LocaleUtils.restartApp(this@SplashActivity, LoginActivity::class.java)
return@weave
}

// Determine if user is a Teacher, Ta, or Designer
if (!TeacherPrefs.isConfirmedTeacher) {
TeacherPrefs.isConfirmedTeacher =
Expand Down Expand Up @@ -142,6 +149,13 @@ class SplashActivity : AppCompatActivity() {
}
}

/** Caches the user in ApiPrefs. Returns true if the user's locale has changed and an app restart is required. */
private fun setupUser(user: User): Boolean {
val oldLocale = ApiPrefs.effectiveLocale
ApiPrefs.user = user
return ApiPrefs.effectiveLocale != oldLocale
}

override fun onStop() {
super.onStop()
startUp?.cancel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class LocaleTransformer : ClassTransformer() {
// If method is overridden, ensure that the context is being wrapped
var callsWrap = false
method.instrument(object : ExprEditor() {
val expectedCall = "com.instructure.pandautils.utils.LocaleUtils.wrapContext(android.content.Context)"
val expectedCall = "com.instructure.canvasapi2.utils.LocaleUtils.wrapContext(android.content.Context)"
override fun edit(m: MethodCall) {
if (m.method.longName == expectedCall) callsWrap = true
}
Expand All @@ -77,7 +77,7 @@ class LocaleTransformer : ClassTransformer() {
val newMethod = CtNewMethod.make(
"""
protected void attachBaseContext(android.content.Context base) {
android.content.Context newBase = com.instructure.pandautils.utils.LocaleUtils.wrapContext(base);
android.content.Context newBase = com.instructure.canvasapi2.utils.LocaleUtils.wrapContext(base);
super.attachBaseContext(newBase);
}
""".trimIndent(), this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.instructure.pandautils.utils
package com.instructure.canvasapi2.utils

import android.app.AlarmManager
import android.app.PendingIntent
Expand All @@ -25,20 +25,20 @@ import android.os.Build
import android.os.Handler
import android.os.LocaleList
import androidx.core.os.ConfigurationCompat
import com.instructure.canvasapi2.utils.ApiPrefs
import com.instructure.canvasapi2.utils.ContextKeeper
import com.instructure.canvasapi2.utils.isValid
import java.util.*

object LocaleUtils {

const val LANGUAGES_PENDING_INTENT_KEY = "languagesPendingIntentKey"
const val LANGUAGES_PENDING_INTENT_ID = 654321

@JvmStatic
fun getSupportedLanguageTags() : Array<String> {
return emptyArray() // Populated by LocaleTransformer at build time
}

@JvmStatic
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "UNUSED")
fun wrapContext(base: Context): Context {
var localeString = ApiPrefs.effectiveLocale

Expand Down Expand Up @@ -73,8 +73,8 @@ object LocaleUtils {
// Restart the App to apply language after a short delay to guarantee shared prefs are saved
Handler().postDelayed({
val intent = Intent(context, startingClass)
intent.putExtra(Const.LANGUAGES_PENDING_INTENT_KEY, Const.LANGUAGES_PENDING_INTENT_ID)
val mPendingIntent = PendingIntent.getActivity(context, Const.LANGUAGES_PENDING_INTENT_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT)
intent.putExtra(LANGUAGES_PENDING_INTENT_KEY, LANGUAGES_PENDING_INTENT_ID)
val mPendingIntent = PendingIntent.getActivity(context, LANGUAGES_PENDING_INTENT_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT)
val mgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent)
System.exit(0)
Expand Down
2 changes: 1 addition & 1 deletion libs/pandares/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@
<string name="courseNickname">لقب المساق</string>

<string name="notAvailableOffline">ميزة غير متوفرة دون الاتصال بالإنترنت</string>
<string name="editFavorites">تحرير مساقات</string>
<string name="editDashboard">تحرير مساقات</string>

<string name="dashboard">لوحة المعلومات</string>
<string name="previous">السابق</string>
Expand Down
2 changes: 1 addition & 1 deletion libs/pandares/src/main/res/values-b+da+instk12/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@
<string name="courseNickname">Fagets kaldenavn</string>

<string name="notAvailableOffline">Funktionalitet ikke tilgængelig mens offline</string>
<string name="editFavorites">Rediger fag</string>
<string name="editDashboard">Rediger fag</string>

<string name="dashboard">Oversigt</string>
<string name="previous">Forrige</string>
Expand Down
Loading

0 comments on commit 6156efc

Please sign in to comment.