// Copyright 2004-present Facebook. All Rights Reserved.

package com.facebook.react.views.imagehelper;

import javax.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;

/**
 * Helper class for obtaining information about local images.
 */
public class ResourceDrawableIdHelper {

  private Map<String, Integer> mResourceDrawableIdMap;

  private static final String LOCAL_RESOURCE_SCHEME = "res";
  private static ResourceDrawableIdHelper sResourceDrawableIdHelper;

  private ResourceDrawableIdHelper() {
    mResourceDrawableIdMap = new HashMap<String, Integer>();
  }

  public static ResourceDrawableIdHelper getInstance() {
    if (sResourceDrawableIdHelper == null) {
      sResourceDrawableIdHelper = new ResourceDrawableIdHelper();
    }
    return sResourceDrawableIdHelper;
  }

  public void clear() {
    mResourceDrawableIdMap.clear();
  }

  public int getResourceDrawableId(Context context, @Nullable String name) {
    if (name == null || name.isEmpty()) {
      return 0;
    }
    name = name.toLowerCase().replace("-", "_");
    if (mResourceDrawableIdMap.containsKey(name)) {
      return mResourceDrawableIdMap.get(name);
    }
    int id = context.getResources().getIdentifier(
        name,
        "drawable",
        context.getPackageName());
    mResourceDrawableIdMap.put(name, id);
    return id;
  }

  public @Nullable Drawable getResourceDrawable(Context context, @Nullable String name) {
    int resId = getResourceDrawableId(context, name);
    return resId > 0 ? context.getResources().getDrawable(resId) : null;
  }

  public Uri getResourceDrawableUri(Context context, @Nullable String name) {
    int resId = getResourceDrawableId(context, name);
    return resId > 0 ? new Uri.Builder()
        .scheme(LOCAL_RESOURCE_SCHEME)
        .path(String.valueOf(resId))
        .build() : Uri.EMPTY;
  }
}

@@ -182,16 +182,56 @@
 l.Map;%0A%0A
+import android.content.ContentResolver;%0A
 import a
@@ -254,16 +254,16 @@
 ontext;%0A
-
 import a
@@ -493,69 +493,8 @@
 p;%0A%0A
-  private static final String LOCAL_RESOURCE_SCHEME = %22res%22;%0A
   pr
@@ -1302,18 +1302,14 @@
     
-  
 name,%0A
-  
     
@@ -1322,18 +1322,16 @@
 wable%22,%0A
-  
       co
@@ -1718,35 +1718,142 @@
 ame) %7B%0A    i
-nt resId = 
+f (name == null %7C%7C name.isEmpty()) %7B%0A      return null;%0A    %7D%0A%0A    int resId = ResourceDrawableIdHelper.getInstance().
 getResourceD
@@ -1870,32 +1870,33 @@
 context, name);%0A
+%0A
     return resId
@@ -1889,28 +1889,16 @@
   return
- resId %3E 0 ?
  new Uri
@@ -1908,18 +1908,16 @@
 ilder()%0A
-  
       .s
@@ -1926,13 +1926,38 @@
 eme(
-LOCAL
+ContentResolver.SCHEME_ANDROID
 _RES
@@ -1965,19 +1965,53 @@
 URCE
-_SCHEME
 )%0A  
+    .authority(context.getPackageName())%0A
     
@@ -2051,18 +2051,16 @@
     
-  
 .build()
  : U
@@ -2059,20 +2059,8 @@
 ld()
- : Uri.EMPTY
 ;%0A  

Revert change on ResourceDrawableIdHelper