For #8296 - Adds user account avatar to Account Preference

fennec/beta
ekager 4 years ago committed by Emily Kager
parent 93bc87e273
commit feb6215fae

@ -5,15 +5,20 @@
package org.mozilla.fenix.ext package org.mozilla.fenix.ext
import android.content.Context import android.content.Context
import android.graphics.BitmapFactory
import android.net.Uri import android.net.Uri
import android.util.Patterns import android.util.Patterns
import android.webkit.URLUtil import android.webkit.URLUtil
import androidx.core.graphics.drawable.RoundedBitmapDrawable
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import androidx.core.net.toUri import androidx.core.net.toUri
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import mozilla.components.lib.publicsuffixlist.PublicSuffixList import mozilla.components.lib.publicsuffixlist.PublicSuffixList
import mozilla.components.lib.publicsuffixlist.ext.urlToTrimmedHost import mozilla.components.lib.publicsuffixlist.ext.urlToTrimmedHost
import mozilla.components.support.ktx.android.net.hostWithoutCommonPrefixes import mozilla.components.support.ktx.android.net.hostWithoutCommonPrefixes
import java.net.IDN import java.net.IDN
import java.net.MalformedURLException
import java.net.URL
import java.util.Locale import java.util.Locale
const val FILE_PREFIX = "file://" const val FILE_PREFIX = "file://"
@ -106,3 +111,19 @@ fun String.simplifiedUrl(): String {
} }
return afterScheme return afterScheme
} }
/**
* Gets a rounded drawable from a URL if possible, else null. Must be called off main thread.
*/
fun String.decodeUrlToRoundedDrawable(context: Context): RoundedBitmapDrawable? {
val avatarUrl = try {
URL(this)
} catch (e: MalformedURLException) {
return null
}
val bitmap = BitmapFactory.decodeStream(avatarUrl.openConnection().getInputStream())
val roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.resources, bitmap)
roundedBitmapDrawable.isCircular = true
roundedBitmapDrawable.setAntiAlias(true)
return roundedBitmapDrawable
}

@ -9,6 +9,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.content.res.AppCompatResources
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavDirections import androidx.navigation.NavDirections
import androidx.navigation.findNavController import androidx.navigation.findNavController
@ -17,7 +18,10 @@ import androidx.preference.Preference
import androidx.preference.Preference.OnPreferenceClickListener import androidx.preference.Preference.OnPreferenceClickListener
import androidx.preference.PreferenceCategory import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import mozilla.components.concept.sync.AccountObserver import mozilla.components.concept.sync.AccountObserver
import mozilla.components.concept.sync.AuthType import mozilla.components.concept.sync.AuthType
import mozilla.components.concept.sync.OAuthAccount import mozilla.components.concept.sync.OAuthAccount
@ -30,6 +34,7 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.application import org.mozilla.fenix.ext.application
import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.decodeUrlToRoundedDrawable
import org.mozilla.fenix.ext.getPreferenceKey import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.metrics import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.requireComponents
@ -320,6 +325,19 @@ class SettingsFragment : PreferenceFragmentCompat() {
// Signed-in, no problems. // Signed-in, no problems.
if (account != null && !accountManager.accountNeedsReauth()) { if (account != null && !accountManager.accountNeedsReauth()) {
preferenceSignIn?.isVisible = false preferenceSignIn?.isVisible = false
profile?.avatar?.url?.let {
lifecycleScope.launch(IO) {
val roundedDrawable = it.decodeUrlToRoundedDrawable(context)
withContext(Main) {
preferenceFirefoxAccount?.icon =
roundedDrawable ?: AppCompatResources.getDrawable(
context,
R.drawable.ic_account
)
}
}
}
preferenceSignIn?.onPreferenceClickListener = null preferenceSignIn?.onPreferenceClickListener = null
preferenceFirefoxAccountAuthError?.isVisible = false preferenceFirefoxAccountAuthError?.isVisible = false
preferenceFirefoxAccount?.isVisible = true preferenceFirefoxAccount?.isVisible = true

@ -1,29 +1,22 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?><!-- This Source Code Form is subject to the terms of the Mozilla Public
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this - License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. --> - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/account_preference_background" android:id="@+id/account_preference_background"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground" android:foreground="?android:attr/selectableItemBackground"
android:gravity="center_vertical" android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingStart="?android:attr/listPreferredItemPaddingStart" android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"> android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
<FrameLayout <ImageView
android:id="@+id/icon_frame" android:id="@android:id/icon"
android:layout_width="wrap_content" android:layout_width="24dp"
android:layout_height="wrap_content"> android:layout_height="24dp"
android:layout_gravity="center"
<androidx.preference.internal.PreferenceImageView android:scaleType="fitCenter" />
android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:maxHeight="48dp"
app:maxWidth="48dp" />
</FrameLayout>
<RelativeLayout <RelativeLayout
android:layout_width="0dp" android:layout_width="0dp"
@ -32,8 +25,8 @@
android:layout_marginEnd="6dp" android:layout_marginEnd="6dp"
android:layout_weight="1" android:layout_weight="1"
android:paddingStart="16dp" android:paddingStart="16dp"
android:paddingEnd="0dp"
android:paddingTop="16dp" android:paddingTop="16dp"
android:paddingEnd="0dp"
android:paddingBottom="16dp"> android:paddingBottom="16dp">
<TextView <TextView

Loading…
Cancel
Save