Remove global app context casts that can crash Android 6.0

This commit is contained in:
dosiecki 2015-10-30 01:03:53 -07:00
parent 7126e20702
commit 8807710a1b
11 changed files with 38 additions and 49 deletions

View file

@ -8,6 +8,9 @@ import com.newsblur.util.ImageLoader;
public class NewsBlurApplication extends Application {
// these need to be app-level singletons, but they can't be safely initiated until
// the app is fully started. create them here and then vend them via FeedUtils like
// most other utility functions.
private ImageLoader imageLoader;
private BlurDatabaseHelper dbHelper;
@ -17,10 +20,7 @@ public class NewsBlurApplication extends Application {
imageLoader = new ImageLoader(this);
dbHelper = new BlurDatabaseHelper(this);
FeedUtils.offerDB(dbHelper);
FeedUtils.offerImageLoader(imageLoader);
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}

View file

@ -32,8 +32,8 @@ import com.newsblur.activity.NewsBlurApplication;
import com.newsblur.domain.Feed;
import com.newsblur.domain.Folder;
import com.newsblur.domain.SocialFeed;
import com.newsblur.util.FeedUtils;
import com.newsblur.util.AppConstants;
import com.newsblur.util.ImageLoader;
import com.newsblur.util.StateFilter;
/**
@ -86,16 +86,12 @@ public class FolderListAdapter extends BaseExpandableListAdapter {
private int savedStoriesCount;
private Context context;
private LayoutInflater inflater;
private ImageLoader imageLoader;
private StateFilter currentState;
public FolderListAdapter(Context context, StateFilter currentState) {
this.context = context;
this.currentState = currentState;
imageLoader = ((NewsBlurApplication) context.getApplicationContext()).getImageLoader();
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@ -187,7 +183,7 @@ public class FolderListAdapter extends BaseExpandableListAdapter {
}
SocialFeed f = socialFeedsOrdered.get(childPosition);
((TextView) v.findViewById(R.id.row_socialfeed_name)).setText(f.feedTitle);
imageLoader.displayImage(f.photoUrl, ((ImageView) v.findViewById(R.id.row_socialfeed_icon)), false);
FeedUtils.imageLoader.displayImage(f.photoUrl, ((ImageView) v.findViewById(R.id.row_socialfeed_icon)), false);
TextView neutCounter = ((TextView) v.findViewById(R.id.row_socialsumneu));
if (f.neutralCount > 0 && currentState != StateFilter.BEST) {
neutCounter.setVisibility(View.VISIBLE);
@ -210,7 +206,7 @@ public class FolderListAdapter extends BaseExpandableListAdapter {
v = convertView;
}
((TextView) v.findViewById(R.id.row_feedname)).setText(f.title);
imageLoader.displayImage(f.faviconUrl, ((ImageView) v.findViewById(R.id.row_feedfavicon)), false);
FeedUtils.imageLoader.displayImage(f.faviconUrl, ((ImageView) v.findViewById(R.id.row_feedfavicon)), false);
TextView neutCounter = ((TextView) v.findViewById(R.id.row_feedneutral));
if (f.neutralCount > 0 && currentState != StateFilter.BEST) {
neutCounter.setVisibility(View.VISIBLE);

View file

@ -12,19 +12,17 @@ import android.widget.TextView;
import com.newsblur.R;
import com.newsblur.activity.NewsBlurApplication;
import com.newsblur.domain.Story;
import com.newsblur.util.ImageLoader;
import com.newsblur.util.FeedUtils;
import com.newsblur.util.PrefsUtils;
import com.newsblur.util.ThemeUtils;
public class MultipleFeedItemsAdapter extends StoryItemsAdapter {
private ImageLoader imageLoader;
private int storyTitleUnread, storyContentUnread, storyAuthorUnread, storyTitleRead, storyContentRead, storyAuthorRead, storyDateUnread, storyDateRead, storyFeedUnread, storyFeedRead;
private boolean ignoreReadStatus;
public MultipleFeedItemsAdapter(Context context, int layout, Cursor c, String[] from, int[] to, boolean ignoreReadStatus) {
super(context, layout, c, from, to);
imageLoader = ((NewsBlurApplication) context.getApplicationContext()).getImageLoader();
storyTitleUnread = ThemeUtils.getStoryTitleUnreadColor(context);
storyTitleRead = ThemeUtils.getStoryTitleReadColor(context);
@ -55,7 +53,7 @@ public class MultipleFeedItemsAdapter extends StoryItemsAdapter {
String feedFade = cursor.getString(cursor.getColumnIndex(DatabaseConstants.FEED_FAVICON_COLOR));
String faviconUrl = cursor.getString(cursor.getColumnIndex(DatabaseConstants.FEED_FAVICON_URL));
imageLoader.displayImage(faviconUrl, ((ImageView) v.findViewById(R.id.row_item_feedicon)), false);
FeedUtils.imageLoader.displayImage(faviconUrl, ((ImageView) v.findViewById(R.id.row_item_feedicon)), false);
if (!TextUtils.equals(feedColor, "#null") && !TextUtils.equals(feedFade, "#null")) {
borderOne.setBackgroundColor(Color.parseColor(feedColor));

View file

@ -19,7 +19,7 @@ import com.newsblur.R;
import com.newsblur.activity.NewsBlurApplication;
import com.newsblur.domain.UserDetails;
import com.newsblur.network.APIManager;
import com.newsblur.util.ImageLoader;
import com.newsblur.util.FeedUtils;
import com.newsblur.util.PrefsUtils;
import com.newsblur.util.UIUtils;
@ -30,14 +30,12 @@ public class ProfileDetailsFragment extends Fragment implements OnClickListener
private View locationIcon;
private ImageView imageView;
private boolean viewingSelf = false;
private ImageLoader imageLoader;
private Button followButton, unfollowButton;
private APIManager apiManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageLoader = ((NewsBlurApplication) getActivity().getApplicationContext()).getImageLoader();
apiManager = new APIManager(getActivity());
}
@ -102,7 +100,7 @@ public class ProfileDetailsFragment extends Fragment implements OnClickListener
followingCount.setText("" + user.followingCount);
if (!viewingSelf) {
imageLoader.displayImage(user.photoUrl, imageView);
FeedUtils.imageLoader.displayImage(user.photoUrl, imageView);
if (user.followedByYou) {
unfollowButton.setVisibility(View.VISIBLE);
followButton.setVisibility(View.GONE);

View file

@ -45,7 +45,6 @@ import com.newsblur.service.NBSyncService;
import com.newsblur.util.DefaultFeedView;
import com.newsblur.util.FeedUtils;
import com.newsblur.util.ImageCache;
import com.newsblur.util.ImageLoader;
import com.newsblur.util.PrefsUtils;
import com.newsblur.util.StoryUtils;
import com.newsblur.util.UIUtils;
@ -65,7 +64,6 @@ public class ReadingItemFragment extends NbFragment implements ClassifierDialogF
public static final String TEXT_SIZE_VALUE = "textSizeChangeValue";
public Story story;
private LayoutInflater inflater;
private ImageLoader imageLoader;
private String feedColor, feedTitle, feedFade, feedBorder, feedIconUrl, faviconText;
private Classifier classifier;
@FindView(R.id.reading_webview) NewsblurWebview web;
@ -133,7 +131,6 @@ public class ReadingItemFragment extends NbFragment implements ClassifierDialogF
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageLoader = ((NewsBlurApplication) getActivity().getApplicationContext()).getImageLoader();
story = getArguments() != null ? (Story) getArguments().getSerializable("story") : null;
inflater = getActivity().getLayoutInflater();
@ -307,7 +304,7 @@ public class ReadingItemFragment extends NbFragment implements ClassifierDialogF
}
private void setupItemCommentsAndShares() {
new SetupCommentSectionTask(this, view, inflater, story, imageLoader).execute();
new SetupCommentSectionTask(this, view, inflater, story).execute();
}
private void setupItemMetadata() {
@ -344,7 +341,7 @@ public class ReadingItemFragment extends NbFragment implements ClassifierDialogF
itemFeed.setVisibility(View.GONE);
feedIcon.setVisibility(View.GONE);
} else {
imageLoader.displayImage(feedIconUrl, feedIcon, false);
FeedUtils.imageLoader.displayImage(feedIconUrl, feedIcon, false);
itemFeed.setText(feedTitle);
}

View file

@ -31,7 +31,6 @@ import com.newsblur.domain.UserDetails;
import com.newsblur.domain.UserProfile;
import com.newsblur.fragment.ReplyDialogFragment;
import com.newsblur.util.FeedUtils;
import com.newsblur.util.ImageLoader;
import com.newsblur.util.PrefsUtils;
import com.newsblur.util.ViewUtils;
import com.newsblur.view.FlowLayout;
@ -45,20 +44,18 @@ public class SetupCommentSectionTask extends AsyncTask<Void, Void, Void> {
private final ReadingItemFragment fragment;
private final Story story;
private final LayoutInflater inflater;
private final ImageLoader imageLoader;
private WeakReference<View> viewHolder;
private final Context context;
private UserDetails user;
private final FragmentManager manager;
private List<Comment> comments;
public SetupCommentSectionTask(ReadingItemFragment fragment, View view, LayoutInflater inflater, Story story, ImageLoader imageLoader) {
public SetupCommentSectionTask(ReadingItemFragment fragment, View view, LayoutInflater inflater, Story story) {
this.fragment = fragment;
this.context = fragment.getActivity();
this.manager = fragment.getFragmentManager();
this.inflater = inflater;
this.story = story;
this.imageLoader = imageLoader;
viewHolder = new WeakReference<View>(view);
user = PrefsUtils.getUserDetails(context);
}
@ -109,7 +106,7 @@ public class SetupCommentSectionTask extends AsyncTask<Void, Void, Void> {
ImageView favouriteImage = new ImageView(context);
UserProfile user = FeedUtils.dbHelper.getUserProfile(id);
if (user != null) {
imageLoader.displayImage(user.photoUrl, favouriteImage, 10f);
FeedUtils.imageLoader.displayImage(user.photoUrl, favouriteImage, 10f);
favouriteContainer.addView(favouriteImage);
}
}
@ -153,7 +150,7 @@ public class SetupCommentSectionTask extends AsyncTask<Void, Void, Void> {
final UserProfile replyUser = FeedUtils.dbHelper.getUserProfile(reply.userId);
if (replyUser != null) {
imageLoader.displayImage(replyUser.photoUrl, replyImage);
FeedUtils.imageLoader.displayImage(replyUser.photoUrl, replyImage);
replyImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
@ -200,11 +197,11 @@ public class SetupCommentSectionTask extends AsyncTask<Void, Void, Void> {
UserProfile sourceUser = FeedUtils.dbHelper.getUserProfile(comment.sourceUserId);
if (sourceUser != null) {
imageLoader.displayImage(sourceUser.photoUrl, sourceUserImage, 10f);
imageLoader.displayImage(userPhoto, usershareImage, 10f);
FeedUtils.imageLoader.displayImage(sourceUser.photoUrl, sourceUserImage, 10f);
FeedUtils.imageLoader.displayImage(userPhoto, usershareImage, 10f);
}
} else {
imageLoader.displayImage(userPhoto, commentImage, 10f);
FeedUtils.imageLoader.displayImage(userPhoto, commentImage, 10f);
}
commentImage.setOnClickListener(new OnClickListener() {
@ -265,7 +262,7 @@ public class SetupCommentSectionTask extends AsyncTask<Void, Void, Void> {
if (!commentingUserIds.contains(userId)) {
UserProfile user = FeedUtils.dbHelper.getUserProfile(userId);
if (user != null) {
ImageView image = ViewUtils.createSharebarImage(context, imageLoader, user.photoUrl, user.userId);
ImageView image = ViewUtils.createSharebarImage(context, user.photoUrl, user.userId);
sharedGrid.addView(image);
}
}
@ -275,7 +272,7 @@ public class SetupCommentSectionTask extends AsyncTask<Void, Void, Void> {
for (String userId : commentingUserIds) {
UserProfile user = FeedUtils.dbHelper.getUserProfile(userId);
if (user != null) {
ImageView image = ViewUtils.createSharebarImage(context, imageLoader, user.photoUrl, user.userId);
ImageView image = ViewUtils.createSharebarImage(context, user.photoUrl, user.userId);
commentGrid.addView(image);
}
}

View file

@ -25,7 +25,11 @@ import com.newsblur.service.NBSyncService;
public class FeedUtils {
// these are app-level singletons stored here for convenience. however, they
// cannot be created lazily or via static init, they have to be created when
// the main app context is created and then pushed here. see NewsBlurApplication.
public static BlurDatabaseHelper dbHelper;
public static ImageLoader imageLoader;
public static void offerDB(BlurDatabaseHelper _dbHelper) {
if (_dbHelper.isOpen()) {
@ -33,6 +37,10 @@ public class FeedUtils {
}
}
public static void offerImageLoader(ImageLoader _imageLoader) {
imageLoader = _imageLoader;
}
private static void triggerSync(Context c) {
Intent i = new Intent(c, NBSyncService.class);
c.startService(i);

View file

@ -90,7 +90,7 @@ public class UIUtils {
*/
public static void setCustomActionBar(Activity activity, String imageUrl, String title) {
ImageView iconView = setupCustomActionbar(activity, title);
((NewsBlurApplication) activity.getApplicationContext()).getImageLoader().displayImage(imageUrl, iconView, false);
FeedUtils.imageLoader.displayImage(imageUrl, iconView, false);
}
public static void setCustomActionBar(Activity activity, int imageId, String title) {

View file

@ -47,7 +47,7 @@ public class ViewUtils {
}
}
public static ImageView createSharebarImage(final Context context, final ImageLoader imageLoader, final String photoUrl, final String userId) {
public static ImageView createSharebarImage(final Context context, final String photoUrl, final String userId) {
ImageView image = new ImageView(context);
int imageLength = UIUtils.dp2px(context, 15);
image.setMaxHeight(imageLength);
@ -62,7 +62,7 @@ public class ViewUtils {
image.setMaxWidth(imageLength);
image.setLayoutParams(imageParameters);
imageLoader.displayImage(photoUrl, image, 10f);
FeedUtils.imageLoader.displayImage(photoUrl, image, 10f);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {

View file

@ -16,25 +16,22 @@ import com.newsblur.domain.UserDetails;
import com.newsblur.domain.ActivityDetails;
import com.newsblur.domain.ActivityDetails.Category;
import com.newsblur.network.APIConstants;
import com.newsblur.util.FeedUtils;
import com.newsblur.util.ImageLoader;
import com.newsblur.util.PrefsUtils;
public abstract class ActivityDetailsAdapter extends ArrayAdapter<ActivityDetails> {
private LayoutInflater inflater;
private ImageLoader imageLoader;
protected final String ago;
protected ForegroundColorSpan linkColor, contentColor, quoteColor;
private String TAG = "ActivitiesAdapter";
private Context context;
protected UserDetails currentUserDetails;
protected final boolean userIsYou;
public ActivityDetailsAdapter(final Context context, UserDetails user) {
super(context, R.id.row_activity_text);
inflater = LayoutInflater.from(context);
imageLoader = ((NewsBlurApplication) context.getApplicationContext()).getImageLoader();
this.context = context;
currentUserDetails = user;
@ -70,13 +67,13 @@ public abstract class ActivityDetailsAdapter extends ArrayAdapter<ActivityDetail
activityTime.setText(activity.timeSince.toUpperCase() + " " + ago);
if (activity.category == Category.FEED_SUBSCRIPTION) {
imageLoader.displayImage(APIConstants.S3_URL_FEED_ICONS + activity.feedId + ".png", imageView);
FeedUtils.imageLoader.displayImage(APIConstants.S3_URL_FEED_ICONS + activity.feedId + ".png", imageView);
} else if (activity.category == Category.SHARED_STORY) {
imageLoader.displayImage(currentUserDetails.photoUrl, imageView, 10f);
FeedUtils.imageLoader.displayImage(currentUserDetails.photoUrl, imageView, 10f);
} else if (activity.category == Category.STAR) {
imageView.setImageResource(R.drawable.clock);
} else if (activity.user != null) {
imageLoader.displayImage(activity.user.photoUrl, imageView);
FeedUtils.imageLoader.displayImage(activity.user.photoUrl, imageView);
} else {
imageView.setImageResource(R.drawable.logo);
}

View file

@ -13,7 +13,7 @@ import android.widget.TextView;
import com.newsblur.R;
import com.newsblur.activity.NewsBlurApplication;
import com.newsblur.database.DatabaseConstants;
import com.newsblur.util.ImageLoader;
import com.newsblur.util.FeedUtils;
import com.newsblur.util.StoryUtils;
import java.util.Date;
@ -21,12 +21,10 @@ import java.util.Date;
public class SocialItemViewBinder implements ViewBinder {
private final Context context;
private ImageLoader imageLoader;
private boolean ignoreIntel;
public SocialItemViewBinder(final Context context, boolean ignoreIntel) {
this.context = context;
this.imageLoader = ((NewsBlurApplication) context.getApplicationContext()).getImageLoader();
this.ignoreIntel = ignoreIntel;
}
@ -40,7 +38,7 @@ public class SocialItemViewBinder implements ViewBinder {
final int hasBeenRead = cursor.getInt(cursor.getColumnIndex(DatabaseConstants.STORY_READ));
if (TextUtils.equals(cursor.getColumnName(columnIndex), DatabaseConstants.FEED_FAVICON_URL)) {
String faviconUrl = cursor.getString(columnIndex);
imageLoader.displayImage(faviconUrl, ((ImageView) view), true);
FeedUtils.imageLoader.displayImage(faviconUrl, ((ImageView) view), true);
return true;
} else if (TextUtils.equals(columnName, DatabaseConstants.SUM_STORY_TOTAL)) {
if (! this.ignoreIntel) {