Clean ups. Replaced deprecated getResources.getColor() with ContextCompat.getColor(this, int color)

This commit is contained in:
Karim Abou Zeid 2015-08-19 13:43:38 +02:00
commit c0e345fcef
9 changed files with 37 additions and 22 deletions

View file

@ -67,6 +67,8 @@ android {
lintOptions {
disable 'MissingTranslation'
disable 'InvalidPackage'
// remove after it has been fixed (probably 1.4 preview 3 + gradle 1.4 alpha2)
disable 'PrivateResource'
}
}

View file

@ -14,6 +14,7 @@ import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
@ -72,6 +73,7 @@ public class ColorChooserDialog extends LeakDetectDialogFragment implements View
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
MaterialDialog dialog = new MaterialDialog.Builder(getActivity())
@ -94,9 +96,9 @@ public class ColorChooserDialog extends LeakDetectDialogFragment implements View
public void onNeutral(MaterialDialog dialog) {
super.onNeutral(dialog);
if (getArguments().getInt("title", 0) == R.string.primary_color) {
getArguments().putInt("preselect", getResources().getColor(R.color.indigo_500));
getArguments().putInt("preselect", ContextCompat.getColor(getContext(), R.color.indigo_500));
} else if (getArguments().getInt("title", 0) == R.string.accent_color) {
getArguments().putInt("preselect", getResources().getColor(R.color.pink_A200));
getArguments().putInt("preselect", ContextCompat.getColor(getContext(), R.color.pink_A200));
}
invalidateGrid();
}

View file

@ -16,6 +16,7 @@ import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.RemoteViews;
@ -268,10 +269,10 @@ public class PlayingNotificationHelper {
isDark = setDark;
if (notificationLayout != null && notificationLayoutBig != null) {
int darkContentColor = service.getResources().getColor(R.color.primary_text_default_material_light);
int darkContentSecondaryColor = service.getResources().getColor(R.color.secondary_text_default_material_light);
int contentColor = service.getResources().getColor(R.color.primary_text_default_material_dark);
int contentSecondaryColor = service.getResources().getColor(R.color.secondary_text_default_material_dark);
int darkContentColor = ContextCompat.getColor(service, R.color.primary_text_default_material_light);
int darkContentSecondaryColor = ContextCompat.getColor(service, R.color.secondary_text_default_material_light);
int contentColor = ContextCompat.getColor(service, R.color.primary_text_default_material_dark);
int contentSecondaryColor = ContextCompat.getColor(service, R.color.secondary_text_default_material_dark);
notificationLayout.setTextColor(R.id.title, setDark ? darkContentColor : contentColor);
notificationLayout.setTextColor(R.id.text, setDark ? darkContentSecondaryColor : contentSecondaryColor);

View file

@ -21,6 +21,7 @@ import android.support.design.widget.AppBarLayout.OnOffsetChangedListener;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
@ -195,7 +196,7 @@ public class MainActivity extends AbsSlidingMusicPanelActivity
new int[]{
// 0,
colorAccent,
ThemeSingleton.get().darkTheme ? getResources().getColor(R.color.primary_text_default_material_dark) : getResources().getColor(R.color.primary_text_default_material_light)
ThemeSingleton.get().darkTheme ? ContextCompat.getColor(this, R.color.primary_text_default_material_dark) : ContextCompat.getColor(this, R.color.primary_text_default_material_light)
}
));
navigationView.setItemIconTintList(new ColorStateList(
@ -207,7 +208,7 @@ public class MainActivity extends AbsSlidingMusicPanelActivity
new int[]{
// 0,
colorAccent,
ThemeSingleton.get().darkTheme ? getResources().getColor(R.color.secondary_text_default_material_dark) : getResources().getColor(R.color.secondary_text_default_material_light)
ThemeSingleton.get().darkTheme ? ContextCompat.getColor(this, R.color.secondary_text_default_material_dark) : ContextCompat.getColor(this, R.color.secondary_text_default_material_light)
}
));
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

View file

@ -1156,8 +1156,7 @@ public abstract class AbsSlidingMusicPanelActivity extends AbsMusicServiceActivi
@Override
public void set(FloatingActionButton object, Integer value) {
object.setBackgroundTintList(ColorStateList.valueOf(value));
object.setRippleColor(shiftColorDown(value));
object.getDrawable().setColorFilter(ColorUtil.getDrawableColorForBackground(object.getContext(), value), PorterDuff.Mode.SRC_IN);
object.getDrawable().setColorFilter(ColorUtil.getFabDrawableColorForBackground(object.getContext(), value), PorterDuff.Mode.SRC_IN);
}
@Override

View file

@ -196,7 +196,7 @@ public abstract class AbsTagEditorActivity extends AbsBaseActivity {
});
int fabColor = getThemeColorAccent();
int fabDrawableColor = ColorUtil.getDrawableColorForBackground(this, fabColor);
int fabDrawableColor = ColorUtil.getFabDrawableColorForBackground(this, fabColor);
fab.setBackgroundTintList(ColorStateList.valueOf(fabColor));
fab.getDrawable().setColorFilter(fabDrawableColor, PorterDuff.Mode.SRC_IN);
}

View file

@ -8,6 +8,7 @@ import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.graphics.Palette;
import com.kabouzeid.gramophone.R;
@ -82,6 +83,14 @@ public class ColorUtil {
return (alpha << 24) + (0x00ffffff & Color.HSVToColor(hsv));
}
public static int getPrimaryTextColor(final Context context, boolean dark) {
return dark ? ContextCompat.getColor(context, R.color.primary_text_default_material_light) : ContextCompat.getColor(context, R.color.primary_text_default_material_dark);
}
public static int getSecondaryTextColor(final Context context, boolean dark) {
return dark ? ContextCompat.getColor(context, R.color.secondary_text_default_material_light) : ContextCompat.getColor(context, R.color.secondary_text_default_material_dark);
}
public static float getLuminance(@ColorInt int color) {
return (Color.red(color) * 0.299f + Color.green(color) * 0.587f + Color.blue(color) * 0.114f);
}
@ -90,19 +99,19 @@ public class ColorUtil {
return getLuminance(backgroundColor) > (255f / 2f);
}
public static int getPrimaryTextColorForBackground(final Context context, @ColorInt int backgroundColor) {
return useDarkTextColorOnBackground(backgroundColor) ? context.getResources().getColor(R.color.primary_text_default_material_light) : context.getResources().getColor(R.color.primary_text_default_material_dark);
}
public static int getSecondaryTextColorForBackground(final Context context, @ColorInt int backgroundColor) {
return useDarkTextColorOnBackground(backgroundColor) ? context.getResources().getColor(R.color.secondary_text_default_material_light) : context.getResources().getColor(R.color.secondary_text_default_material_dark);
}
public static boolean useDarkDrawableColorOnBackground(@ColorInt int backgroundColor) {
public static boolean useDarkFabDrawableOnBackground(@ColorInt int backgroundColor) {
return getLuminance(backgroundColor) > (255f / 1.3f);
}
public static int getDrawableColorForBackground(final Context context, @ColorInt int backgroundColor) {
return useDarkDrawableColorOnBackground(backgroundColor) ? context.getResources().getColor(R.color.primary_text_default_material_light) : context.getResources().getColor(R.color.primary_text_default_material_dark);
public static int getPrimaryTextColorForBackground(final Context context, @ColorInt int backgroundColor) {
return getPrimaryTextColor(context, useDarkTextColorOnBackground(backgroundColor));
}
public static int getSecondaryTextColorForBackground(final Context context, @ColorInt int backgroundColor) {
return getSecondaryTextColor(context, useDarkTextColorOnBackground(backgroundColor));
}
public static int getFabDrawableColorForBackground(final Context context, @ColorInt int backgroundColor) {
return getPrimaryTextColor(context, useDarkFabDrawableOnBackground(backgroundColor));
}
}

View file

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.helper.SortOrder;
@ -106,7 +107,7 @@ public final class PreferenceUtil {
}
public int getThemeColorPrimary(Context context) {
return mPreferences.getInt("primary_color", context.getResources().getColor(R.color.indigo_500));
return mPreferences.getInt("primary_color", ContextCompat.getColor(context, R.color.indigo_500));
}
@SuppressLint("CommitPrefEdits")
@ -115,7 +116,7 @@ public final class PreferenceUtil {
}
public int getThemeColorAccent(Context context) {
return mPreferences.getInt("accent_color", context.getResources().getColor(R.color.pink_A200));
return mPreferences.getInt("accent_color", ContextCompat.getColor(context, R.color.pink_A200));
}
@SuppressLint("CommitPrefEdits")

View file

@ -68,7 +68,7 @@ public class ColorView extends FrameLayout {
public void setBackgroundColor(int color) {
paint.setColor(color);
paintBorder.setColor(ColorUtil.shiftColorDown(color));
paintCheck.setColorFilter(new PorterDuffColorFilter(ColorUtil.getDrawableColorForBackground(getContext(), color), PorterDuff.Mode.SRC_IN));
paintCheck.setColorFilter(new PorterDuffColorFilter(ColorUtil.getFabDrawableColorForBackground(getContext(), color), PorterDuff.Mode.SRC_IN));
requestLayout();
invalidate();
}