react-native-navigation的迁移库

ResourceDrawableIdHelper.java 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.reactnativenavigation.react;// Copyright 2004-present Facebook. All Rights Reserved.
  2. import android.content.Context;
  3. import android.graphics.drawable.Drawable;
  4. import android.net.Uri;
  5. import com.facebook.common.util.UriUtil;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import javax.annotation.Nullable;
  9. /**
  10. * Direct copy paste from react-native, because they made that class package scope. -_-"
  11. * Can be deleted in react-native ^0.29
  12. */
  13. public class ResourceDrawableIdHelper {
  14. public static final ResourceDrawableIdHelper instance = new ResourceDrawableIdHelper();
  15. private Map<String, Integer> mResourceDrawableIdMap;
  16. public ResourceDrawableIdHelper() {
  17. mResourceDrawableIdMap = new HashMap<>();
  18. }
  19. public int getResourceDrawableId(Context context, @Nullable String name) {
  20. if (name == null || name.isEmpty()) {
  21. return 0;
  22. }
  23. name = name.toLowerCase().replace("-", "_");
  24. if (mResourceDrawableIdMap.containsKey(name)) {
  25. return mResourceDrawableIdMap.get(name);
  26. }
  27. int id = context.getResources().getIdentifier(
  28. name,
  29. "drawable",
  30. context.getPackageName());
  31. mResourceDrawableIdMap.put(name, id);
  32. return id;
  33. }
  34. @Nullable
  35. public Drawable getResourceDrawable(Context context, @Nullable String name) {
  36. int resId = getResourceDrawableId(context, name);
  37. return resId > 0 ? context.getResources().getDrawable(resId) : null;
  38. }
  39. public Uri getResourceDrawableUri(Context context, @Nullable String name) {
  40. int resId = getResourceDrawableId(context, name);
  41. return resId > 0 ? new Uri.Builder()
  42. .scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
  43. .path(String.valueOf(resId))
  44. .build() : Uri.EMPTY;
  45. }
  46. }