Skip to content

Commit

Permalink
Fixes mozilla-mobile#4894: added workaround for SDK<24 vector drawabl…
Browse files Browse the repository at this point in the history
…e with gradient color (mozilla-mobile#4944)

Removed drawableStart and added ImageView in layouts
Set ImageView logo programmatically: bitmap for SDK<24, vector for SDK>=24
Added onClickPendingIntent for ImageView in large and medium layouts
  • Loading branch information
mcarare authored and boek committed Aug 29, 2019
1 parent f740d43 commit 9b911f3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 8 deletions.
22 changes: 22 additions & 0 deletions app/src/main/java/org/mozilla/fenix/widget/SearchWidgetProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.IntentReceiverActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.utils.Settings
import android.os.Build
import androidx.appcompat.widget.AppCompatDrawableManager
import androidx.core.graphics.drawable.toBitmap

@Suppress("TooManyFunctions")
class SearchWidgetProvider : AppWidgetProvider() {

override fun onEnabled(context: Context) {
Expand Down Expand Up @@ -126,6 +130,7 @@ class SearchWidgetProvider : AppWidgetProvider() {
text: String?
): RemoteViews {
return RemoteViews(context.packageName, layout).apply {
setIcon(context)
when (layout) {
R.layout.search_widget_extra_small_v1,
R.layout.search_widget_extra_small_v2,
Expand All @@ -140,6 +145,7 @@ class SearchWidgetProvider : AppWidgetProvider() {
R.layout.search_widget_large -> {
setOnClickPendingIntent(R.id.button_search_widget_new_tab, textSearchIntent)
setOnClickPendingIntent(R.id.button_search_widget_voice, voiceSearchIntent)
setOnClickPendingIntent(R.id.button_search_widget_new_tab_icon, textSearchIntent)
setTextViewText(R.id.button_search_widget_new_tab, text)
// Unlike "small" widget, "medium" and "large" sizes do not have separate layouts
// that exclude the microphone icon, which is why we must hide it accordingly here.
Expand All @@ -151,6 +157,22 @@ class SearchWidgetProvider : AppWidgetProvider() {
}
}

private fun RemoteViews.setIcon(context: Context) {
// gradient color available for android:fillColor only on SDK 24+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setImageViewResource(
R.id.button_search_widget_new_tab_icon,
R.drawable.ic_logo_widget)
} else {
setImageViewBitmap(
R.id.button_search_widget_new_tab_icon,
AppCompatDrawableManager.get().getDrawable(
context,
R.drawable.ic_logo_widget
)?.toBitmap())
}
}

// Cell sizes obtained from the actual dimensions listed in search widget specs
companion object {
private const val DP_EXTRA_SMALL = 64
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/search_widget_extra_small_v1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
android:layout_gravity="center">

<ImageView
android:id="@+id/button_search_widget_new_tab_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_logo_widget"
android:layout_gravity="center"/>
</FrameLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/search_widget_extra_small_v2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
android:layout_gravity="center">

<ImageView
android:id="@+id/button_search_widget_new_tab_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_logo_widget"
android:layout_gravity="center"/>
</FrameLayout>
10 changes: 8 additions & 2 deletions app/src/main/res/layout/search_widget_large.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
android:background="@drawable/rounded_white_corners"
android:layout_gravity="center">

<ImageView
android:id="@+id/button_search_widget_new_tab_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:padding="12dp"/>

<TextView
android:id="@+id/button_search_widget_new_tab"
android:layout_toEndOf="@id/button_search_widget_new_tab_icon"
android:layout_width="match_parent"
android:layout_height="32dp"
android:gravity="center_vertical"
android:textSize="15sp"
android:textColor="@color/search_widget_text"
android:letterSpacing="-0.025"
android:drawableStart="@drawable/ic_logo_widget"
android:drawablePadding="12dp"
android:layout_marginStart="9dp"
android:layout_marginTop="9dp"/>

Expand Down
10 changes: 8 additions & 2 deletions app/src/main/res/layout/search_widget_medium.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
android:background="@drawable/rounded_white_corners"
android:layout_gravity="center">

<ImageView
android:id="@+id/button_search_widget_new_tab_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:padding="12dp"/>

<TextView
android:id="@+id/button_search_widget_new_tab"
android:layout_toEndOf="@id/button_search_widget_new_tab_icon"
android:layout_width="match_parent"
android:layout_height="32dp"
android:gravity="center_vertical"
android:textSize="15sp"
android:textColor="@color/search_widget_text"
android:letterSpacing="-0.025"
android:drawableStart="@drawable/ic_logo_widget"
android:drawablePadding="12dp"
android:layout_marginStart="9dp"
android:layout_marginTop="9dp"/>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/search_widget_small.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
android:layout_marginTop="9dp">

<ImageView
android:id="@+id/button_search_widget_new_tab_icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="@drawable/ic_logo_widget"
android:layout_marginStart="9dp"/>
</FrameLayout>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/search_widget_small_no_mic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
android:orientation="vertical">

<ImageView
android:id="@+id/button_search_widget_new_tab_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_logo_widget"
android:layout_gravity="center"/>
</FrameLayout>

0 comments on commit 9b911f3

Please sign in to comment.