Fixed a crash under API level 21 on the main timeline due to trying to tint the status buttons with selectors.

This commit is contained in:
Vavassor 2017-02-17 23:10:46 -05:00
parent 26b988aab7
commit 0439fabd79
15 changed files with 67 additions and 132 deletions

View file

@ -626,11 +626,13 @@ public class ComposeActivity extends BaseActivity {
private void enableMediaPicking() {
mediaPick.setEnabled(true);
ThemeUtils.setImageViewTint(mediaPick, R.attr.compose_media_button_tint);
mediaPick.setImageResource(R.drawable.ic_media);
}
private void disableMediaPicking() {
mediaPick.setEnabled(false);
ThemeUtils.setImageViewTint(mediaPick, R.attr.compose_media_button_disabled_tint);
mediaPick.setImageResource(R.drawable.ic_media_disabled);
}

View file

@ -1,54 +0,0 @@
/* Copyright 2017 Andrew Dawson
*
* This file is part of Tusky.
*
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky. If not, see
* <http://www.gnu.org/licenses/>. */
package com.keylesspalace.tusky;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.AppCompatImageButton;
import android.util.AttributeSet;
public class StatusButton extends AppCompatImageButton {
private static final int[] STATE_MARKED = { R.attr.state_marked };
private boolean marked;
public StatusButton(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
TypedArray array = context.getTheme().obtainStyledAttributes(
attributeSet, R.styleable.StatusButton, 0, 0);
try {
marked = array.getBoolean(R.styleable.StatusButton_state_marked, false);
} finally {
array.recycle();
}
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
if (marked) {
extraSpace += 1;
}
int[] drawableState = super.onCreateDrawableState(extraSpace);
if (marked) {
mergeDrawableStates(drawableState, STATE_MARKED);
}
return drawableState;
}
public void setMarked(boolean marked) {
this.marked = marked;
}
}

View file

@ -44,8 +44,8 @@ public class StatusViewHolder extends RecyclerView.ViewHolder {
private View rebloggedBar;
private TextView rebloggedByDisplayName;
private ImageButton replyButton;
private StatusButton reblogButton;
private StatusButton favouriteButton;
private ImageButton reblogButton;
private ImageButton favouriteButton;
private ImageButton moreButton;
private boolean favourited;
private boolean reblogged;
@ -71,8 +71,8 @@ public class StatusViewHolder extends RecyclerView.ViewHolder {
rebloggedBar = itemView.findViewById(R.id.status_reblogged_bar);
rebloggedByDisplayName = (TextView) itemView.findViewById(R.id.status_reblogged);
replyButton = (ImageButton) itemView.findViewById(R.id.status_reply);
reblogButton = (StatusButton) itemView.findViewById(R.id.status_reblog);
favouriteButton = (StatusButton) itemView.findViewById(R.id.status_favourite);
reblogButton = (ImageButton) itemView.findViewById(R.id.status_reblog);
favouriteButton = (ImageButton) itemView.findViewById(R.id.status_favourite);
moreButton = (ImageButton) itemView.findViewById(R.id.status_more);
reblogged = false;
favourited = false;
@ -189,21 +189,33 @@ public class StatusViewHolder extends RecyclerView.ViewHolder {
public void setReblogged(boolean reblogged) {
this.reblogged = reblogged;
reblogButton.setMarked(reblogged);
int attribute;
if (reblogged) {
attribute = R.attr.status_reblog_button_marked_tint;
} else {
attribute = R.attr.status_reblog_button_tint;
}
ThemeUtils.setImageViewTint(reblogButton, attribute);
}
/** This should only be called after setReblogged, in order to override the tint correctly. */
public void setRebloggingEnabled(boolean enabled) {
reblogButton.setEnabled(enabled);
if (enabled) {
reblogButton.setImageResource(R.drawable.ic_reblog);
} else {
if (!enabled) {
ThemeUtils.setImageViewTint(reblogButton, R.attr.status_reblog_button_disabled_tint);
reblogButton.setImageResource(R.drawable.ic_reblog_disabled);
}
}
public void setFavourited(boolean favourited) {
this.favourited = favourited;
favouriteButton.setMarked(favourited);
int attribute;
if (favourited) {
attribute = R.attr.status_favourite_button_marked_tint;
} else {
attribute = R.attr.status_favourite_button_tint;
}
ThemeUtils.setImageViewTint(favouriteButton, attribute);
}
public void setMediaPreviews(final Status.MediaAttachment[] attachments,

View file

@ -16,9 +16,11 @@
package com.keylesspalace.tusky;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.widget.ImageView;
public class ThemeUtils {
public static Drawable getDrawable(Context context, int attribute, int fallbackDrawable) {
@ -49,4 +51,8 @@ public class ThemeUtils {
return android.R.color.black;
}
}
public static void setImageViewTint(ImageView view, int attribute) {
view.setColorFilter(getColor(view.getContext(), attribute), PorterDuff.Mode.SRC_IN);
}
}

View file

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:color="#DFCF00" app:state_marked="true" />
<item android:color="#CFCFCF" />
</selector>

View file

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:color="#BFAF00" app:state_marked="true" />
<item android:color="#4F4F4F" />
</selector>

View file

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#8F8F8F" android:state_enabled="false" />
<item android:color="#FFFFFF" />
</selector>

View file

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#8F8F8F" android:state_enabled="false" />
<item android:color="#000000" />
</selector>

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:color="#00DFAF" app:state_marked="true" />
<item android:color="#6F6F6F" android:state_enabled="false" />
<item android:color="#CFCFCF" />
</selector>

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:color="#009F6F" app:state_marked="true" />
<item android:color="#BFBFBF" android:state_enabled="false" />
<item android:color="#4F4F4F" />
</selector>

View file

@ -16,7 +16,6 @@
android:layout_height="48dp"
app:srcCompat="@drawable/ic_media"
style="?attr/image_button_style"
android:tint="?attr/compose_media_button_tint"
android:id="@+id/compose_photo_pick"
android:layout_marginLeft="8dp" />

View file

@ -209,10 +209,10 @@
android:layout_height="match_parent"
android:layout_weight="1" />
<com.keylesspalace.tusky.StatusButton
<ImageButton
app:srcCompat="@drawable/ic_reblog"
android:id="@+id/status_reblog"
style="?attr/status_reblog_button_style"
style="?attr/image_button_style"
android:layout_width="32dp"
android:layout_height="32dp" />
@ -221,10 +221,10 @@
android:layout_height="match_parent"
android:layout_weight="1" />
<com.keylesspalace.tusky.StatusButton
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
style="?attr/status_favourite_button_style"
style="?attr/image_button_style"
app:srcCompat="@drawable/ic_favourite"
android:id="@+id/status_favourite" />

View file

@ -14,8 +14,11 @@
<attr name="toolbar_background_color" format="reference" />
<attr name="toolbar_icon_tint" format="reference" />
<attr name="image_button_style" format="reference" />
<attr name="status_favourite_button_style" format="reference" />
<attr name="status_reblog_button_style" format="reference" />
<attr name="status_reblog_button_tint" format="reference|color" />
<attr name="status_reblog_button_marked_tint" format="reference|color" />
<attr name="status_reblog_button_disabled_tint" format="reference|color" />
<attr name="status_favourite_button_tint" format="reference|color" />
<attr name="status_favourite_button_marked_tint" format="reference|color" />
<attr name="content_warning_button" format="reference" />
<attr name="sensitive_media_warning_background_color" format="reference|color" />
<attr name="status_text_color_secondary" format="reference|color" />
@ -26,6 +29,7 @@
<attr name="notification_icon_tint" format="reference|color" />
<attr name="account_header_background_color" format="reference|color" />
<attr name="compose_media_button_tint" format="reference|color" />
<attr name="compose_media_button_disabled_tint" format="reference|color" />
<attr name="compose_mention_color" format="reference|color" />
</resources>

View file

@ -20,12 +20,19 @@
<color name="toolbar_background_dark">#44A673</color>
<color name="toolbar_icon_dark">#FFFFFF</color>
<color name="image_button_dark">#CFCFCF</color>
<color name="status_reblog_button_dark">#CFCFCF</color>
<color name="status_reblog_button_marked_dark">#00DFAF</color>
<color name="status_reblog_button_disabled_dark">#6F6F6F</color>
<color name="status_favourite_button_dark">#CFCFCF</color>
<color name="status_favourite_button_marked_dark">#DFCF00</color>
<color name="sensitive_media_warning_background_dark">#303030</color>
<color name="media_preview_unloaded_background_dark">#2F2F2F</color>
<color name="status_text_secondary_dark">#A4B4BC</color>
<color name="status_divider_dark">#000000</color>
<color name="tab_page_margin_dark">#4C534B</color>
<color name="account_header_background_dark">#000000</color>
<color name="compose_media_button_dark">#FFFFFF</color>
<color name="compose_media_button_disabled_dark">#8F8F8F</color>
<color name="compose_mention_dark">#AFBFCF</color>
<color name="notification_content_faded_dark">#9F9F9F</color>
<color name="notification_icon_tint_dark">#CFCFCF</color>
@ -46,12 +53,19 @@
<color name="toolbar_background_light">#f6f7f7</color>
<color name="toolbar_icon_light">#000000</color>
<color name="image_button_light">#2F2F2F</color>
<color name="status_reblog_button_light">#4F4F4F</color>
<color name="status_reblog_button_marked_light">#009F6F</color>
<color name="status_reblog_button_disabled_light">#BFBFBF</color>
<color name="status_favourite_button_light">#4F4F4F</color>
<color name="status_favourite_button_marked_light">#BFAF00</color>
<color name="sensitive_media_warning_background_light">#B0B0B0</color>
<color name="media_preview_unloaded_background_light">#CFCFCF</color>
<color name="status_text_secondary_light">#34444C</color>
<color name="status_divider_light">#CFCFCF</color>
<color name="tab_page_margin_light">#9F9F9F</color>
<color name="account_header_background_light">#EFEFEF</color>
<color name="compose_media_button_light">#000000</color>
<color name="compose_media_button_disabled_light">#8F8F8F</color>
<color name="compose_mention_light">#2F5F6F</color>
<color name="notification_content_faded_light">#7F7F7F</color>
<color name="notification_icon_tint_light">#1F1F1F</color>

View file

@ -27,8 +27,11 @@
<item name="toolbar_background_color">@color/toolbar_background_dark</item>
<item name="toolbar_icon_tint">@color/toolbar_icon_dark</item>
<item name="image_button_style">@style/AppTheme.ImageButton.Dark</item>
<item name="status_reblog_button_style">@style/AppTheme.ReblogButton.Dark</item>
<item name="status_favourite_button_style">@style/AppTheme.FavouriteButton.Dark</item>
<item name="status_reblog_button_tint">@color/status_reblog_button_dark</item>
<item name="status_reblog_button_marked_tint">@color/status_reblog_button_marked_dark</item>
<item name="status_reblog_button_disabled_tint">@color/status_reblog_button_disabled_dark</item>
<item name="status_favourite_button_tint">@color/status_favourite_button_dark</item>
<item name="status_favourite_button_marked_tint">@color/status_favourite_button_marked_dark</item>
<item name="content_warning_button">@drawable/toggle_small</item>
<item name="sensitive_media_warning_background_color">@color/sensitive_media_warning_background_dark</item>
<item name="status_text_color_secondary">@color/status_text_secondary_dark</item>
@ -36,7 +39,8 @@
<item name="status_divider_drawable">@drawable/status_divider_dark</item>
<item name="tab_page_margin_drawable">@drawable/tab_page_margin_dark</item>
<item name="account_header_background_color">@color/account_header_background_dark</item>
<item name="compose_media_button_tint">@color/media_button_dark</item>
<item name="compose_media_button_tint">@color/compose_media_button_dark</item>
<item name="compose_media_button_disabled_tint">@color/compose_media_button_disabled_dark</item>
<item name="compose_mention_color">@color/compose_mention_dark</item>
<item name="notification_content">@color/notification_content_faded_dark</item>
<item name="notification_icon_tint">@color/notification_icon_tint_dark</item>
@ -46,14 +50,6 @@
<item name="android:tint">@color/image_button_dark</item>
</style>
<style name="AppTheme.ReblogButton.Dark" parent="@style/Widget.AppCompat.Button.Borderless.Colored">
<item name="android:tint">@color/reblog_button_dark</item>
</style>
<style name="AppTheme.FavouriteButton.Dark" parent="@style/Widget.AppCompat.Button.Borderless.Colored">
<item name="android:tint">@color/favourite_button_dark</item>
</style>
<style name="AppTheme.BottomSheetDialog.Dark" parent="@style/Theme.Design.BottomSheetDialog">
<item name="colorAccent">@color/color_accent_dark</item>
<item name="android:colorBackground">@color/color_background_dark</item>
@ -82,8 +78,11 @@
<item name="toolbar_background_color">@color/toolbar_background_light</item>
<item name="toolbar_icon_tint">@color/toolbar_icon_light</item>
<item name="image_button_style">@style/AppTheme.ImageButton.Light</item>
<item name="status_reblog_button_style">@style/AppTheme.ReblogButton.Light</item>
<item name="status_favourite_button_style">@style/AppTheme.FavouriteButton.Light</item>
<item name="status_reblog_button_tint">@color/status_reblog_button_light</item>
<item name="status_reblog_button_marked_tint">@color/status_reblog_button_marked_light</item>
<item name="status_reblog_button_disabled_tint">@color/status_reblog_button_disabled_light</item>
<item name="status_favourite_button_tint">@color/status_favourite_button_light</item>
<item name="status_favourite_button_marked_tint">@color/status_favourite_button_marked_light</item>
<item name="content_warning_button">@drawable/toggle_small_light</item>
<item name="sensitive_media_warning_background_color">@color/sensitive_media_warning_background_light</item>
<item name="status_text_color_secondary">@color/status_text_secondary_light</item>
@ -91,7 +90,8 @@
<item name="status_divider_drawable">@drawable/status_divider_light</item>
<item name="tab_page_margin_drawable">@drawable/tab_page_margin_light</item>
<item name="account_header_background_color">@color/account_header_background_light</item>
<item name="compose_media_button_tint">@color/media_button_light</item>
<item name="compose_media_button_tint">@color/compose_media_button_light</item>
<item name="compose_media_button_disabled_tint">@color/compose_media_button_disabled_light</item>
<item name="compose_mention_color">@color/compose_mention_light</item>
<item name="notification_content">@color/notification_content_faded_light</item>
<item name="notification_icon_tint">@color/notification_icon_tint_light</item>
@ -101,14 +101,6 @@
<item name="android:tint">@color/image_button_light</item>
</style>
<style name="AppTheme.ReblogButton.Light" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="android:tint">@color/reblog_button_light</item>
</style>
<style name="AppTheme.FavouriteButton.Light" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="android:tint">@color/favourite_button_light</item>
</style>
<style name="AppTheme.BottomSheetDialog.Light" parent="@style/Theme.Design.Light.BottomSheetDialog">
<item name="colorAccent">@color/color_accent_light</item>
<item name="android:colorBackground">@color/color_background_light</item>