Skip to content

Commit

Permalink
Change to using ContextCompat.getDrawable()
Browse files Browse the repository at this point in the history
* Resources.getDrawable() is deprecated starting with API Level 22
* See http://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22
* This could potentially be related to #289 - maybe this fixes it?
  • Loading branch information
barbeau committed Aug 31, 2015
1 parent 9525a23 commit 2f87aa4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
Expand Up @@ -43,6 +43,7 @@
import org.onebusaway.android.io.elements.ObaStop;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
Expand All @@ -58,6 +59,7 @@
import android.os.Build;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
Expand Down Expand Up @@ -220,12 +222,13 @@ private static final void loadIcons() {
* @return a bus stop icon bitmap with the arrow pointing the given direction, or with no arrow
* if direction is NO_DIRECTION
*/
private static Bitmap createBusStopIcon(String direction) {
private static Bitmap createBusStopIcon(String direction) throws NullPointerException {
if (direction == null) {
throw new IllegalArgumentException(direction);
}

Resources r = Application.get().getResources();
Context context = Application.get();

Float directionAngle = null; // 0-360 degrees
Bitmap bm;
Expand All @@ -241,13 +244,13 @@ private static Bitmap createBusStopIcon(String direction) {
// Don't draw the arrow
bm = Bitmap.createBitmap(mPx, mPx, Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, 0, bm.getWidth(), bm.getHeight());
} else if (direction.equals(NORTH)) {
directionAngle = 0f;
bm = Bitmap.createBitmap(mPx, (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, (int) mBuffer, mPx, bm.getHeight());
// Shade with darkest color at tip of arrow
arrowPaintFill.setShader(
Expand All @@ -262,7 +265,7 @@ private static Bitmap createBusStopIcon(String direction) {
bm = Bitmap.createBitmap((int) (mPx + mBuffer),
(int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds((int) mBuffer, (int) mBuffer, bm.getWidth(), bm.getHeight());
// Shade with darkest color at tip of arrow
arrowPaintFill.setShader(
Expand All @@ -276,7 +279,7 @@ private static Bitmap createBusStopIcon(String direction) {
directionAngle = 0f; // Arrow is drawn pointing West, so no rotation
bm = Bitmap.createBitmap((int) (mPx + mBuffer), mPx, Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds((int) mBuffer, 0, bm.getWidth(), bm.getHeight());
arrowPaintFill.setShader(
new LinearGradient(0, bm.getHeight() / 2, mArrowHeightPx, bm.getHeight() / 2,
Expand All @@ -290,7 +293,7 @@ private static Bitmap createBusStopIcon(String direction) {
bm = Bitmap.createBitmap((int) (mPx + mBuffer),
(int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds((int) mBuffer, 0, bm.getWidth(), mPx);
arrowPaintFill.setShader(
new LinearGradient(0, bm.getHeight(), mBuffer, bm.getHeight() - mBuffer,
Expand All @@ -303,7 +306,7 @@ private static Bitmap createBusStopIcon(String direction) {
directionAngle = 180f; // Arrow is drawn N, rotate 180 degrees
bm = Bitmap.createBitmap(mPx, (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, 0, bm.getWidth(), (int) (bm.getHeight() - mBuffer));
arrowPaintFill.setShader(
new LinearGradient(bm.getWidth() / 2, bm.getHeight(), bm.getWidth() / 2,
Expand All @@ -317,7 +320,7 @@ private static Bitmap createBusStopIcon(String direction) {
bm = Bitmap.createBitmap((int) (mPx + mBuffer),
(int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, 0, mPx, mPx);
arrowPaintFill.setShader(
new LinearGradient(bm.getWidth(), bm.getHeight(), bm.getWidth() - mBuffer,
Expand All @@ -331,7 +334,7 @@ private static Bitmap createBusStopIcon(String direction) {
directionAngle = 180f; // Arrow is drawn pointing West, so rotate 180
bm = Bitmap.createBitmap((int) (mPx + mBuffer), mPx, Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, 0, mPx, bm.getHeight());
arrowPaintFill.setShader(
new LinearGradient(bm.getWidth(), bm.getHeight() / 2,
Expand All @@ -345,7 +348,7 @@ private static Bitmap createBusStopIcon(String direction) {
bm = Bitmap.createBitmap((int) (mPx + mBuffer),
(int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, (int) mBuffer, mPx, bm.getHeight());
// Shade with darkest color at tip of arrow
arrowPaintFill.setShader(
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.onebusaway.android.io.elements.ObaStop;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
Expand All @@ -47,6 +48,7 @@
import android.os.Build;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
Expand Down Expand Up @@ -209,12 +211,13 @@ private static final void loadIcons() {
* @return a bus stop icon bitmap with the arrow pointing the given direction, or with no arrow
* if direction is NO_DIRECTION
*/
private static Bitmap createBusStopIcon(String direction) {
private static Bitmap createBusStopIcon(String direction) throws NullPointerException {
if (direction == null) {
throw new IllegalArgumentException(direction);
}

Resources r = Application.get().getResources();
Context context = Application.get();

Float directionAngle = null; // 0-360 degrees
Bitmap bm;
Expand All @@ -230,13 +233,13 @@ private static Bitmap createBusStopIcon(String direction) {
// Don't draw the arrow
bm = Bitmap.createBitmap(mPx, mPx, Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, 0, bm.getWidth(), bm.getHeight());
} else if (direction.equals(NORTH)) {
directionAngle = 0f;
bm = Bitmap.createBitmap(mPx, (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, (int) mBuffer, mPx, bm.getHeight());
// Shade with darkest color at tip of arrow
arrowPaintFill.setShader(
Expand All @@ -251,7 +254,7 @@ private static Bitmap createBusStopIcon(String direction) {
bm = Bitmap.createBitmap((int) (mPx + mBuffer),
(int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds((int) mBuffer, (int) mBuffer, bm.getWidth(), bm.getHeight());
// Shade with darkest color at tip of arrow
arrowPaintFill.setShader(
Expand All @@ -265,7 +268,7 @@ private static Bitmap createBusStopIcon(String direction) {
directionAngle = 0f; // Arrow is drawn pointing West, so no rotation
bm = Bitmap.createBitmap((int) (mPx + mBuffer), mPx, Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds((int) mBuffer, 0, bm.getWidth(), bm.getHeight());
arrowPaintFill.setShader(
new LinearGradient(0, bm.getHeight() / 2, mArrowHeightPx, bm.getHeight() / 2,
Expand All @@ -279,7 +282,7 @@ private static Bitmap createBusStopIcon(String direction) {
bm = Bitmap.createBitmap((int) (mPx + mBuffer),
(int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds((int) mBuffer, 0, bm.getWidth(), mPx);
arrowPaintFill.setShader(
new LinearGradient(0, bm.getHeight(), mBuffer, bm.getHeight() - mBuffer,
Expand All @@ -292,7 +295,7 @@ private static Bitmap createBusStopIcon(String direction) {
directionAngle = 180f; // Arrow is drawn N, rotate 180 degrees
bm = Bitmap.createBitmap(mPx, (int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, 0, bm.getWidth(), (int) (bm.getHeight() - mBuffer));
arrowPaintFill.setShader(
new LinearGradient(bm.getWidth() / 2, bm.getHeight(), bm.getWidth() / 2,
Expand All @@ -306,7 +309,7 @@ private static Bitmap createBusStopIcon(String direction) {
bm = Bitmap.createBitmap((int) (mPx + mBuffer),
(int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, 0, mPx, mPx);
arrowPaintFill.setShader(
new LinearGradient(bm.getWidth(), bm.getHeight(), bm.getWidth() - mBuffer,
Expand All @@ -320,7 +323,7 @@ private static Bitmap createBusStopIcon(String direction) {
directionAngle = 180f; // Arrow is drawn pointing West, so rotate 180
bm = Bitmap.createBitmap((int) (mPx + mBuffer), mPx, Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, 0, mPx, bm.getHeight());
arrowPaintFill.setShader(
new LinearGradient(bm.getWidth(), bm.getHeight() / 2,
Expand All @@ -334,7 +337,7 @@ private static Bitmap createBusStopIcon(String direction) {
bm = Bitmap.createBitmap((int) (mPx + mBuffer),
(int) (mPx + mBuffer), Bitmap.Config.ARGB_8888);
c = new Canvas(bm);
shape = r.getDrawable(R.drawable.map_stop_icon);
shape = ContextCompat.getDrawable(context, R.drawable.map_stop_icon);
shape.setBounds(0, (int) mBuffer, mPx, bm.getHeight());
// Shade with darkest color at tip of arrow
arrowPaintFill.setShader(
Expand Down

0 comments on commit 2f87aa4

Please sign in to comment.