use of android.content.res.Resources in project buck by facebook.
the class ResourcesLoader method getActiveResourcesFromResourcesManager.
private static Collection<WeakReference<Resources>> getActiveResourcesFromResourcesManager() {
try {
Class resourcesManagerClass;
try {
resourcesManagerClass = Class.forName("android.app.ResourcesManager");
} catch (ClassNotFoundException e) {
return null;
}
Method getResourcesManager = resourcesManagerClass.getDeclaredMethod("getInstance");
getResourcesManager.setAccessible(true);
Object resourcesManager = getResourcesManager.invoke(null);
try {
return ((ArrayMap<?, WeakReference<Resources>>) Reflect.getField(resourcesManager, resourcesManagerClass, "mActiveResources")).values();
} catch (NoSuchFieldException e) {
return (Collection<WeakReference<Resources>>) Reflect.getField(resourcesManager, resourcesManagerClass, "mResourceReferences");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of android.content.res.Resources in project cw-omnibus by commonsguy.
the class SearchView method createVoiceAppSearchIntent.
/**
* Create and return an Intent that can launch the voice search activity, perform a specific
* voice transcription, and forward the results to the searchable activity.
*
* @param baseIntent The voice app search intent to start from
* @return A completely-configured intent ready to send to the voice search activity
*/
private Intent createVoiceAppSearchIntent(Intent baseIntent, SearchableInfo searchable) {
ComponentName searchActivity = searchable.getSearchActivity();
// create the necessary intent to set up a search-and-forward operation
// in the voice search system. We have to keep the bundle separate,
// because it becomes immutable once it enters the PendingIntent
Intent queryIntent = new Intent(Intent.ACTION_SEARCH);
queryIntent.setComponent(searchActivity);
PendingIntent pending = PendingIntent.getActivity(getContext(), 0, queryIntent, PendingIntent.FLAG_ONE_SHOT);
// Now set up the bundle that will be inserted into the pending intent
// when it's time to do the search. We always build it here (even if empty)
// because the voice search activity will always need to insert "QUERY" into
// it anyway.
Bundle queryExtras = new Bundle();
// Now build the intent to launch the voice search. Add all necessary
// extras to launch the voice recognizer, and then all the necessary extras
// to forward the results to the searchable activity
Intent voiceIntent = new Intent(baseIntent);
// Add all of the configuration options supplied by the searchable's metadata
String languageModel = RecognizerIntent.LANGUAGE_MODEL_FREE_FORM;
String prompt = null;
String language = null;
int maxResults = 1;
Resources resources = getResources();
if (searchable.getVoiceLanguageModeId() != 0) {
languageModel = resources.getString(searchable.getVoiceLanguageModeId());
}
if (searchable.getVoicePromptTextId() != 0) {
prompt = resources.getString(searchable.getVoicePromptTextId());
}
if (searchable.getVoiceLanguageId() != 0) {
language = resources.getString(searchable.getVoiceLanguageId());
}
if (searchable.getVoiceMaxResults() != 0) {
maxResults = searchable.getVoiceMaxResults();
}
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel);
voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
voiceIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResults);
voiceIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, searchActivity == null ? null : searchActivity.flattenToShortString());
// Add the values that configure forwarding the results
voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pending);
voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE, queryExtras);
return voiceIntent;
}
use of android.content.res.Resources in project cw-omnibus by commonsguy.
the class SuggestionsAdapter method getDrawableFromResourceValue.
/**
* Gets a drawable given a value provided by a suggestion provider.
*
* This value could be just the string value of a resource id
* (e.g., "2130837524"), in which case we will try to retrieve a drawable from
* the provider's resources. If the value is not an integer, it is
* treated as a Uri and opened with
* {@link ContentResolver#openOutputStream(android.net.Uri, String)}.
*
* All resources and URIs are read using the suggestion provider's context.
*
* If the string is not formatted as expected, or no drawable can be found for
* the provided value, this method returns null.
*
* @param drawableId a string like "2130837524",
* "android.resource://com.android.alarmclock/2130837524",
* or "content://contacts/photos/253".
* @return a Drawable, or null if none found
*/
private Drawable getDrawableFromResourceValue(String drawableId) {
if (drawableId == null || drawableId.length() == 0 || "0".equals(drawableId)) {
return null;
}
try {
// First, see if it's just an integer
int resourceId = Integer.parseInt(drawableId);
// It's an int, look for it in the cache
String drawableUri = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + mProviderContext.getPackageName() + "/" + resourceId;
// Must use URI as cache key, since ints are app-specific
Drawable drawable = checkIconCache(drawableUri);
if (drawable != null) {
return drawable;
}
// Not cached, find it by resource ID
drawable = mProviderContext.getResources().getDrawable(resourceId);
// Stick it in the cache, using the URI as key
storeInIconCache(drawableUri, drawable);
return drawable;
} catch (NumberFormatException nfe) {
// It's not an integer, use it as a URI
Drawable drawable = checkIconCache(drawableId);
if (drawable != null) {
return drawable;
}
Uri uri = Uri.parse(drawableId);
drawable = getDrawable(uri);
storeInIconCache(drawableId, drawable);
return drawable;
} catch (Resources.NotFoundException nfe) {
// It was an integer, but it couldn't be found, bail out
Log.w(LOG_TAG, "Icon resource not found: " + drawableId);
return null;
}
}
use of android.content.res.Resources in project cw-omnibus by commonsguy.
the class SuggestionsAdapter method getTheDrawable.
public Drawable getTheDrawable(Uri uri) throws FileNotFoundException {
String authority = uri.getAuthority();
Resources r;
if (TextUtils.isEmpty(authority)) {
throw new FileNotFoundException("No authority: " + uri);
} else {
try {
r = mContext.getPackageManager().getResourcesForApplication(authority);
} catch (NameNotFoundException ex) {
throw new FileNotFoundException("No package found for authority: " + uri);
}
}
List<String> path = uri.getPathSegments();
if (path == null) {
throw new FileNotFoundException("No path: " + uri);
}
int len = path.size();
int id;
if (len == 1) {
try {
id = Integer.parseInt(path.get(0));
} catch (NumberFormatException e) {
throw new FileNotFoundException("Single path segment is not a resource ID: " + uri);
}
} else if (len == 2) {
id = r.getIdentifier(path.get(1), path.get(0), authority);
} else {
throw new FileNotFoundException("More than two path segments: " + uri);
}
if (id == 0) {
throw new FileNotFoundException("No resource found for: " + uri);
}
return r.getDrawable(id);
}
use of android.content.res.Resources in project android-betterpickers by code-troopers.
the class TimeZonePickerUtils method cacheOverrides.
private void cacheOverrides(Context context) {
Resources res = context.getResources();
mOverrideIds = res.getStringArray(R.array.timezone_rename_ids);
mOverrideLabels = res.getStringArray(R.array.timezone_rename_labels);
}
Aggregations