Last active
December 22, 2016 19:27
-
-
Save justintuchek/3d8ec17a26d4a3f650652542c229b04a to your computer and use it in GitHub Desktop.
A safer way to loosely obtain Android String resources by name (instead of id.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class dji.midware.g.a { | |
| public static String b(Context var0, String var1) { | |
| return StringUtil.getValueSafe(var0, var1); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| The MIT License (MIT) | |
| Copyright (c) 2016 DJI | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| **************************************************************************************************************************** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.botlink.stringresourceresolver; | |
| import android.content.Context; | |
| import android.support.annotation.NonNull; | |
| public final class StringResolver { | |
| /** | |
| * Value returned from {@link android.content.res.Resources#getIdentifier(String, String, String)} | |
| * when resource name does not yield a valid resource id. | |
| */ | |
| private static final int KEY_NOT_FOUND = 0; | |
| /** | |
| * Value returned from {@link StringResolver#getValueSafe(Context, String)} | |
| * when a value is not found. | |
| */ | |
| public static final String VALUE_NOT_AVAILABLE = "unknown value"; | |
| @NonNull | |
| public static String getValueUnsafe(Context context, String name) throws ResourceMissingException { | |
| final int key = context.getResources().getIdentifier(name, "string", context.getPackageName()); | |
| if(KEY_NOT_FOUND == key) { | |
| throw ResourceMissingException.create(name); | |
| } else { | |
| return context.getResources().getString(key); | |
| } | |
| } | |
| @NonNull | |
| public static String getValueSafe(Context context, String name) { | |
| try { | |
| return getValueUnsafe(context, name); | |
| } catch (ResourceMissingException ex) { | |
| /** | |
| * SDK developer friendliness. | |
| * Now you can rethrow an exception during testing, debugging, etc. | |
| * Now you can log exceptions in an analytics tracker for logging purposes. | |
| */ | |
| /** | |
| * Option 1: emit exception message so SDK developers can report missing resources | |
| * specifically by name requested | |
| */ | |
| return ex.getMessage(); | |
| /** | |
| * Option 2: emit non-changing value so SDK developers can filter out failing results | |
| * and not show debug messages to application users. | |
| */ | |
| return VALUE_NOT_AVAILABLE; | |
| } | |
| } | |
| private StringResolver() { | |
| throw new IllegalStateException("not designed for instantiation."); | |
| } | |
| public static final class ResourceMissingException extends Exception { | |
| public static ResourceMissingException create(String name) { | |
| final String message = String.format("Identifier `%s` not found in package", name); | |
| return new ResourceMissingException(message); | |
| } | |
| private ResourceMissingException(String message) { | |
| super(message); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment