Java Code Examples for android.content.res.Resources#getResourcePackageName()

The following examples show how to use android.content.res.Resources#getResourcePackageName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyCodeDescriptionMapper.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
private static String getSpokenEmoticonDescription(final Context context,
        final String outputText) {
    final StringBuilder sb = new StringBuilder(SPOKEN_EMOTICON_RESOURCE_NAME_PREFIX);
    final int textLength = outputText.length();
    for (int index = 0; index < textLength; index = outputText.offsetByCodePoints(index, 1)) {
        final int codePoint = outputText.codePointAt(index);
        sb.append(String.format(Locale.ROOT, SPOKEN_EMOTICON_CODE_POINT_FORMAT, codePoint));
    }
    final String resourceName = sb.toString();
    final Resources resources = context.getResources();
    // Note that the resource package name may differ from the context package name.
    final String resourcePackageName = resources.getResourcePackageName(
            R.string.spoken_description_unknown);
    final int resId = resources.getIdentifier(resourceName, "string", resourcePackageName);
    return (resId == 0) ? null : resources.getString(resId);
}
 
Example 2
Source File: ResourceInspector.java    From ResourceInspector with Apache License 2.0 6 votes vote down vote up
@Override
public View inflate(int resourceId, ViewGroup root, boolean attachToRoot) {
    final Resources res = getContext().getResources();
    final String packageName = res.getResourcePackageName(resourceId);
    final String resName = res.getResourceEntryName(resourceId);
    final View view = original.inflate(resourceId, root, attachToRoot);

    if (!appPackageName.equals(packageName)) {
        return view;
    }

    View targetView = view;
    if (root != null && attachToRoot) {
        targetView = root.getChildAt(root.getChildCount() - 1);
    }

    targetView.setTag(TAG_RES_NAME, resName);

    return view;
}
 
Example 3
Source File: KeyCodeDescriptionMapper.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
private int getSpokenDescriptionId(final Context context, final int code,
        final String resourceNameFormat) {
    final String resourceName = String.format(Locale.ROOT, resourceNameFormat, code);
    final Resources resources = context.getResources();
    // Note that the resource package name may differ from the context package name.
    final String resourcePackageName = resources.getResourcePackageName(
            R.string.spoken_description_unknown);
    final int resId = resources.getIdentifier(resourceName, "string", resourcePackageName);
    if (resId != 0) {
        mKeyCodeMap.append(code, resId);
    }
    return resId;
}
 
Example 4
Source File: XmlConfigSource.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private NetworkSecurityConfig.Builder parseDebugOverridesResource()
        throws IOException, XmlPullParserException, ParserException {
    Resources resources = mContext.getResources();
    String packageName = resources.getResourcePackageName(mResourceId);
    String entryName = resources.getResourceEntryName(mResourceId);
    int resId = resources.getIdentifier(entryName + "_debug", "xml", packageName);
    // No debug-overrides resource was found, nothing to parse.
    if (resId == 0) {
        return null;
    }
    NetworkSecurityConfig.Builder debugConfigBuilder = null;
    // Parse debug-overrides out of the _debug resource.
    try (XmlResourceParser parser = resources.getXml(resId)) {
        XmlUtils.beginDocument(parser, "network-security-config");
        int outerDepth = parser.getDepth();
        boolean seenDebugOverrides = false;
        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
            if ("debug-overrides".equals(parser.getName())) {
                if (seenDebugOverrides) {
                    throw new ParserException(parser, "Only one debug-overrides allowed");
                }
                if (mDebugBuild) {
                    debugConfigBuilder =
                            parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
                } else {
                    XmlUtils.skipCurrentTag(parser);
                }
                seenDebugOverrides = true;
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }
    }

    return debugConfigBuilder;
}
 
Example 5
Source File: ResourcesUtil.java    From stetho with MIT License 5 votes vote down vote up
public static String getIdString(@Nullable Resources r, int resourceId)
    throws Resources.NotFoundException {
  if (r == null) {
    return getFallbackIdString(resourceId);
  }

  String prefix;
  String prefixSeparator;
  switch (getResourcePackageId(resourceId)) {
    case 0x7f:
      prefix = "";
      prefixSeparator = "";
      break;
    default:
      prefix = r.getResourcePackageName(resourceId);
      prefixSeparator = ":";
      break;
  }

  String typeName = r.getResourceTypeName(resourceId);
  String entryName = r.getResourceEntryName(resourceId);

  StringBuilder sb = new StringBuilder(
      1 + prefix.length() + prefixSeparator.length() +
          typeName.length() + 1 + entryName.length());
  sb.append("@");
  sb.append(prefix);
  sb.append(prefixSeparator);
  sb.append(typeName);
  sb.append("/");
  sb.append(entryName);

  return sb.toString();
}
 
Example 6
Source File: ResourcesUtil.java    From weex with Apache License 2.0 5 votes vote down vote up
public static String getIdString(@Nullable Resources r, int resourceId)
    throws Resources.NotFoundException {
  if (r == null) {
    return getFallbackIdString(resourceId);
  }

  String prefix;
  String prefixSeparator;
  switch (getResourcePackageId(resourceId)) {
    case 0x7f:
      prefix = "";
      prefixSeparator = "";
      break;
    default:
      prefix = r.getResourcePackageName(resourceId);
      prefixSeparator = ":";
      break;
  }

  String typeName = r.getResourceTypeName(resourceId);
  String entryName = r.getResourceEntryName(resourceId);

  StringBuilder sb = new StringBuilder(
      1 + prefix.length() + prefixSeparator.length() +
          typeName.length() + 1 + entryName.length());
  sb.append("@");
  sb.append(prefix);
  sb.append(prefixSeparator);
  sb.append(typeName);
  sb.append("/");
  sb.append(entryName);

  return sb.toString();
}
 
Example 7
Source File: KeyboardTextsSet.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
public void setLocale(final Locale locale, final Context context) {
    final Resources res = context.getResources();
    // Null means the current system locale.
    final String resourcePackageName = res.getResourcePackageName(
            context.getApplicationInfo().labelRes);
    setLocale(locale, res, resourcePackageName);
}
 
Example 8
Source File: KeyCodeDescriptionMapper.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
private int getSpokenDescriptionId(final Context context, final int code,
        final String resourceNameFormat) {
    final String resourceName = String.format(Locale.ROOT, resourceNameFormat, code);
    final Resources resources = context.getResources();
    // Note that the resource package name may differ from the context package name.
    final String resourcePackageName = resources.getResourcePackageName(
            R.string.spoken_description_unknown);
    final int resId = resources.getIdentifier(resourceName, "string", resourcePackageName);
    if (resId != 0) {
        mKeyCodeMap.append(code, resId);
    }
    return resId;
}
 
Example 9
Source File: KeyboardTextsSet.java    From simple-keyboard with Apache License 2.0 5 votes vote down vote up
public void setLocale(final Locale locale, final Context context) {
    final Resources res = context.getResources();
    // Null means the current system locale.
    final String resourcePackageName = res.getResourcePackageName(
            context.getApplicationInfo().labelRes);
    setLocale(locale, res, resourcePackageName);
}
 
Example 10
Source File: KeyboardTextsSet.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
public void setLocale(final Locale locale, final Context context) {
    final Resources res = context.getResources();
    // Null means the current system locale.
    final String resourcePackageName = res.getResourcePackageName(
            context.getApplicationInfo().labelRes);
    setLocale(locale, res, resourcePackageName);
}
 
Example 11
Source File: MoreKeySpecSplitTests.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();

    final Context targetContext = getContext();
    final Resources targetRes = targetContext.getResources();
    final String targetPackageName = targetRes.getResourcePackageName(
            R.string.english_ime_name);
    mTextsSet.setLocale(TEST_LOCALE, targetRes, targetPackageName);
}
 
Example 12
Source File: KeyboardTextsSet.java    From LokiBoard-Android-Keylogger with Apache License 2.0 5 votes vote down vote up
public void setLocale(final Locale locale, final Context context) {
    final Resources res = context.getResources();
    // Null means the current system locale.
    final String resourcePackageName = res.getResourcePackageName(
            context.getApplicationInfo().labelRes);
    setLocale(locale, res, resourcePackageName);
}
 
Example 13
Source File: KeyboardLayoutSet.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 4 votes vote down vote up
private static int getXmlId(final Resources resources, final String keyboardLayoutSetName) {
    final String packageName = resources.getResourcePackageName(
            R.xml.keyboard_layout_set_qwerty);
    return resources.getIdentifier(keyboardLayoutSetName, "xml", packageName);
}
 
Example 14
Source File: XmlConfigSource.java    From cwac-netsecurity with Apache License 2.0 4 votes vote down vote up
private NetworkSecurityConfig.Builder parseDebugOverridesResource()
        throws IOException, XmlPullParserException, ParserException {
    Resources resources = mContext.getResources();
    String packageName = resources.getResourcePackageName(mResourceId);
    String entryName = resources.getResourceEntryName(mResourceId);
    int resId = resources.getIdentifier(entryName + "_debug", "xml", packageName);
    // No debug-overrides resource was found, nothing to parse.
    if (resId == 0) {
        return null;
    }
    NetworkSecurityConfig.Builder debugConfigBuilder = null;
    // Parse debug-overrides out of the _debug resource.
    XmlResourceParser parser=null;

    try {
        parser = resources.getXml(resId);

        XmlUtils.beginDocument(parser, "network-security-config");
        int outerDepth = parser.getDepth();
        boolean seenDebugOverrides = false;
        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
            if ("debug-overrides".equals(parser.getName())) {
                if (seenDebugOverrides) {
                    throw new ParserException(parser, "Only one debug-overrides allowed");
                }
                if (mDebugBuild) {
                    debugConfigBuilder =
                            parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
                } else {
                    XmlUtils.skipCurrentTag(parser);
                }
                seenDebugOverrides = true;
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }
    }
    finally {
        if (parser!=null) parser.close();
    }

    return debugConfigBuilder;
}
 
Example 15
Source File: FragmentActivity.java    From android-recipes-app with Apache License 2.0 4 votes vote down vote up
private static String viewToString(View view) {
    StringBuilder out = new StringBuilder(128);
    out.append(view.getClass().getName());
    out.append('{');
    out.append(Integer.toHexString(System.identityHashCode(view)));
    out.append(' ');
    switch (view.getVisibility()) {
        case View.VISIBLE: out.append('V'); break;
        case View.INVISIBLE: out.append('I'); break;
        case View.GONE: out.append('G'); break;
        default: out.append('.'); break;
    }
    out.append(view.isFocusable() ? 'F' : '.');
    out.append(view.isEnabled() ? 'E' : '.');
    out.append(view.willNotDraw() ? '.' : 'D');
    out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
    out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
    out.append(view.isClickable() ? 'C' : '.');
    out.append(view.isLongClickable() ? 'L' : '.');
    out.append(' ');
    out.append(view.isFocused() ? 'F' : '.');
    out.append(view.isSelected() ? 'S' : '.');
    out.append(view.isPressed() ? 'P' : '.');
    out.append(' ');
    out.append(view.getLeft());
    out.append(',');
    out.append(view.getTop());
    out.append('-');
    out.append(view.getRight());
    out.append(',');
    out.append(view.getBottom());
    final int id = view.getId();
    if (id != View.NO_ID) {
        out.append(" #");
        out.append(Integer.toHexString(id));
        final Resources r = view.getResources();
        if (id != 0 && r != null) {
            try {
                String pkgname;
                switch (id&0xff000000) {
                    case 0x7f000000:
                        pkgname="app";
                        break;
                    case 0x01000000:
                        pkgname="android";
                        break;
                    default:
                        pkgname = r.getResourcePackageName(id);
                        break;
                }
                String typename = r.getResourceTypeName(id);
                String entryname = r.getResourceEntryName(id);
                out.append(" ");
                out.append(pkgname);
                out.append(":");
                out.append(typename);
                out.append("/");
                out.append(entryname);
            } catch (Resources.NotFoundException e) {
            }
        }
    }
    out.append("}");
    return out.toString();
}
 
Example 16
Source File: KeyboardLayoutSet.java    From Indic-Keyboard with Apache License 2.0 4 votes vote down vote up
private static int getXmlId(final Resources resources, final String keyboardLayoutSetName) {
    final String packageName = resources.getResourcePackageName(
            R.xml.keyboard_layout_set_qwerty);
    return resources.getIdentifier(keyboardLayoutSetName, "xml", packageName);
}
 
Example 17
Source File: FragmentActivity.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
private static String viewToString(View view) {
    StringBuilder out = new StringBuilder(128);
    out.append(view.getClass().getName());
    out.append('{');
    out.append(Integer.toHexString(System.identityHashCode(view)));
    out.append(' ');
    switch (view.getVisibility()) {
        case View.VISIBLE: out.append('V'); break;
        case View.INVISIBLE: out.append('I'); break;
        case View.GONE: out.append('G'); break;
        default: out.append('.'); break;
    }
    out.append(view.isFocusable() ? 'F' : '.');
    out.append(view.isEnabled() ? 'E' : '.');
    out.append(view.willNotDraw() ? '.' : 'D');
    out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
    out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
    out.append(view.isClickable() ? 'C' : '.');
    out.append(view.isLongClickable() ? 'L' : '.');
    out.append(' ');
    out.append(view.isFocused() ? 'F' : '.');
    out.append(view.isSelected() ? 'S' : '.');
    out.append(view.isPressed() ? 'P' : '.');
    out.append(' ');
    out.append(view.getLeft());
    out.append(',');
    out.append(view.getTop());
    out.append('-');
    out.append(view.getRight());
    out.append(',');
    out.append(view.getBottom());
    final int id = view.getId();
    if (id != View.NO_ID) {
        out.append(" #");
        out.append(Integer.toHexString(id));
        final Resources r = view.getResources();
        if (id != 0 && r != null) {
            try {
                String pkgname;
                switch (id&0xff000000) {
                    case 0x7f000000:
                        pkgname="app";
                        break;
                    case 0x01000000:
                        pkgname="android";
                        break;
                    default:
                        pkgname = r.getResourcePackageName(id);
                        break;
                }
                String typename = r.getResourceTypeName(id);
                String entryname = r.getResourceEntryName(id);
                out.append(" ");
                out.append(pkgname);
                out.append(":");
                out.append(typename);
                out.append("/");
                out.append(entryname);
            } catch (Resources.NotFoundException e) {
            }
        }
    }
    out.append("}");
    return out.toString();
}
 
Example 18
Source File: FragmentActivity.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
private static String viewToString(View view) {
    StringBuilder out = new StringBuilder(128);
    out.append(view.getClass().getName());
    out.append('{');
    out.append(Integer.toHexString(System.identityHashCode(view)));
    out.append(' ');
    switch (view.getVisibility()) {
        case View.VISIBLE: out.append('V'); break;
        case View.INVISIBLE: out.append('I'); break;
        case View.GONE: out.append('G'); break;
        default: out.append('.'); break;
    }
    out.append(view.isFocusable() ? 'F' : '.');
    out.append(view.isEnabled() ? 'E' : '.');
    out.append(view.willNotDraw() ? '.' : 'D');
    out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
    out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
    out.append(view.isClickable() ? 'C' : '.');
    out.append(view.isLongClickable() ? 'L' : '.');
    out.append(' ');
    out.append(view.isFocused() ? 'F' : '.');
    out.append(view.isSelected() ? 'S' : '.');
    out.append(view.isPressed() ? 'P' : '.');
    out.append(' ');
    out.append(view.getLeft());
    out.append(',');
    out.append(view.getTop());
    out.append('-');
    out.append(view.getRight());
    out.append(',');
    out.append(view.getBottom());
    final int id = view.getId();
    if (id != View.NO_ID) {
        out.append(" #");
        out.append(Integer.toHexString(id));
        final Resources r = view.getResources();
        if (id != 0 && r != null) {
            try {
                String pkgname;
                switch (id&0xff000000) {
                    case 0x7f000000:
                        pkgname="app";
                        break;
                    case 0x01000000:
                        pkgname="android";
                        break;
                    default:
                        pkgname = r.getResourcePackageName(id);
                        break;
                }
                String typename = r.getResourceTypeName(id);
                String entryname = r.getResourceEntryName(id);
                out.append(" ");
                out.append(pkgname);
                out.append(":");
                out.append(typename);
                out.append("/");
                out.append(entryname);
            } catch (Resources.NotFoundException e) {
            }
        }
    }
    out.append("}");
    return out.toString();
}
 
Example 19
Source File: KeyboardLayoutSet.java    From openboard with GNU General Public License v3.0 4 votes vote down vote up
private static int getXmlId(final Resources resources, final String keyboardLayoutSetName) {
    final String packageName = resources.getResourcePackageName(
            R.xml.keyboard_layout_set_qwerty);
    return resources.getIdentifier(keyboardLayoutSetName, "xml", packageName);
}
 
Example 20
Source File: TypefaceCompat.java    From Carbon with Apache License 2.0 2 votes vote down vote up
/**
 * Create a unique id for a given Resource and id.
 *
 * @param resources Resources instance
 * @param id        a resource id
 * @param style     style to be used for this resource, -1 if not available.
 * @return Unique id for a given resource and id.
 */
private static String createResourceUid(final Resources resources, int id, boolean italic, int fontWeight) {
    return resources.getResourcePackageName(id) + "-" + id + "-" + italic + "-" + fontWeight;
}