/*  Given a local index, return (initialized if needed) a rec containing the
    encoded data and length. The bitmap field is initialized to 0, and is not
    filled in by this routine per-se.
 */
static EncodeDataRec* get_encoderec(int index) {
    if ((unsigned)index >= GMOJI_PUA_COUNT) {
        SkDebugf("bad index passed to EncodeDataRec& get_encode_data %d\n",
                 index);
        return NULL;
    }

    // lazily fill in the data
    EncodeDataRec* rec = &gGmojiEncodeData[index];

    if (NOT_AVAILABLE_ENCODE_SIZE == rec->fSize) {
        return NULL;
    }
    if (UNINITIALIZED_ENCODE_SIZE == rec->fSize) {
        EmojiFactory* fact = get_emoji_factory();
        if (NULL == fact) {
            return NULL;
        }

        int32_t pua = GMOJI_PUA_MIN + gGmojiPUA[index];
        rec->fData = fact->GetImageBinaryFromAndroidPua(pua, &rec->fSize);
        if (NULL == rec->fData) {
            // flag this entry is not available, so we won't ask again
            rec->fSize = NOT_AVAILABLE_ENCODE_SIZE;
            return NULL;
        }
    }
    return rec;
}
const char *EmojiFont::GetShiftJisConverterName() {
    EmojiFactory* fact = get_emoji_factory();
    if (NULL != fact) {
        if (strcmp(fact->Name(), "kddi") == 0) {
            return "kddi-emoji";
        } else if (strcmp(fact->Name(), "softbank") == 0) {
            return "softbank-emoji";
        }
    }

    // Until Eclair, we have used DoCoMo's Shift_JIS table.
    return "docomo-emoji";
}
static jobject android_emoji_EmojiFactory_getBitmapFromAndroidPua(
    JNIEnv* env, jobject clazz, jlong nativeEmojiFactory, jint pua) {
  EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);

  int size;
  const char *bytes = factory->GetImageBinaryFromAndroidPua(pua, &size);
  if (bytes == NULL) {
    return NULL;
  }

  SkBitmap *bitmap = new SkBitmap;
  if (!SkImageDecoder::DecodeMemory(bytes, size, bitmap)) {
    ALOGE("SkImageDecoder::DecodeMemory() failed.");
    return NULL;
  }

  return GraphicsJNI::createBitmap(env, bitmap,
      GraphicsJNI::kBitmapCreateFlag_Premultiplied, NULL);
}
static jobject android_emoji_EmojiFactory_newAvailableInstance(
    JNIEnv* env, jobject clazz) {
  pthread_once(&g_once, InitializeCaller);
  if (!lib_emoji_factory_is_ready) {
    return NULL;
  }

  EmojiFactory *factory = gCaller->TryCallGetAvailableImplementation();
  // EmojiFactory *factory = EmojiFactory::GetAvailableImplementation();
  if (NULL == factory) {
    return NULL;
  }
  String16 name_16(String8(factory->Name()));
  jstring jname = env->NewString((const jchar*)name_16.string(), name_16.size());
  if (NULL == jname) {
    return NULL;
  }

  return create_java_EmojiFactory(env, factory, jname);
}
static jobject android_emoji_EmojiFactory_getBitmapFromAndroidPua(
    JNIEnv* env, jobject clazz, jint nativeEmojiFactory, jint pua) {
  EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);

  int size;
  const char *bytes = factory->GetImageBinaryFromAndroidPua(pua, &size);
  if (bytes == NULL) {
    return NULL;
  }

  SkBitmap *bitmap = new SkBitmap;
  if (!SkImageDecoder::DecodeMemory(bytes, size, bitmap)) {
    LOGE("SkImageDecoder::DecodeMemory() failed.");
    return NULL;
  }

  jobject obj = env->NewObject(gBitmap_class, gBitmap_constructorMethodID,
      static_cast<jint>(reinterpret_cast<uintptr_t>(bitmap)), NULL, false, NULL, -1);
  if (env->ExceptionCheck() != 0) {
    LOGE("*** Uncaught exception returned from Java call!\n");
    env->ExceptionDescribe();
  }
  return obj;
}
static jint android_emoji_EmojiFactory_getMinimumAndroidPua(
    JNIEnv* env, jobject obj, jint nativeEmojiFactory) {
  EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
  return factory->GetMinimumAndroidPua();
}
static jint android_emoji_EmojiFactory_getVendorSpecificPuaFromAndroidPua(
    JNIEnv* env, jobject obj, jint nativeEmojiFactory, jint pua) {
  EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
  return factory->GetVendorSpecificPuaFromAndroidPua(pua);
}
static jint android_emoji_EmojiFactory_getAndroidPuaFromVendorSpecificSjis(
    JNIEnv* env, jobject obj, jint nativeEmojiFactory, jchar sjis) {
  EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
  return factory->GetAndroidPuaFromVendorSpecificSjis(sjis);
}
static jint android_emoji_EmojiFactory_getMinimumVendorSpecificPua(
    JNIEnv* env, jobject obj, jlong nativeEmojiFactory) {
  EmojiFactory *factory = reinterpret_cast<EmojiFactory *>(nativeEmojiFactory);
  return factory->GetMinimumVendorSpecificPua();
}