public class

XResources

extends Resources
java.lang.Object
   ↳ android.content.res.Resources
     ↳ android.content.res.XResources

Class Overview

Resources subclass that allows replacing individual resources.

Xposed replaces the standard resources with this class, which overrides the methods used for retrieving individual resources and adds possibilities to replace them. These replacements can be set using the methods made available via the API methods in this class.

Summary

Nested Classes
class XResources.DimensionReplacement Callback for dimension replacements. 
class XResources.DrawableLoader Callback for drawable replacements. 
class XResources.ResourceNames Wrapper for information about an indiviual resource. 
Public Methods
int addResource(Resources res, int id)
Makes any individual resource available from another Resources instance available in this XResources instance.
static int getFakeResId(String resName)
Generates a fake resource ID.
static int getFakeResId(Resources res, int id)
Generates a fake resource ID.
String getPackageName()
Returns the name of the package that these resources belong to, or "android" for system resources.
static String getPackageNameDuringConstruction()
Special case of getPackageName() during object creation.
XC_LayoutInflated.Unhook hookLayout(String pkg, String type, String name, XC_LayoutInflated callback)
Hook the inflation of a layout.
XC_LayoutInflated.Unhook hookLayout(int id, XC_LayoutInflated callback)
Hook the inflation of a layout.
XC_LayoutInflated.Unhook hookLayout(String fullName, XC_LayoutInflated callback)
This method was deprecated in API level 81. Use hookLayout(String, String, String, XC_LayoutInflated) instead.
static XC_LayoutInflated.Unhook hookSystemWideLayout(String fullName, XC_LayoutInflated callback)
This method was deprecated in API level 81. Use hookSystemWideLayout(String, String, String, XC_LayoutInflated) instead.
static XC_LayoutInflated.Unhook hookSystemWideLayout(int id, XC_LayoutInflated callback)
Hook the inflation of an Android framework layout (in the android package).
static XC_LayoutInflated.Unhook hookSystemWideLayout(String pkg, String type, String name, XC_LayoutInflated callback)
Hook the inflation of an Android framework layout (in the android package).
void setReplacement(String fullName, Object replacement)
This method was deprecated in API level 81. Use setReplacement(String, String, String, Object) instead.
void setReplacement(String pkg, String type, String name, Object replacement)
Sets a replacement for an individual resource.
void setReplacement(int id, Object replacement)
Sets a replacement for an individual resource.
static void setSystemWideReplacement(String fullName, Object replacement)
This method was deprecated in API level 81. Use setSystemWideReplacement(String, String, String, Object) instead.
static void setSystemWideReplacement(int id, Object replacement)
Sets a replacement for an individual Android framework resource (in the android package).
static void setSystemWideReplacement(String pkg, String type, String name, Object replacement)
Sets a replacement for an individual Android framework resource (in the android package).
[Expand]
Inherited Methods
From class android.content.res.Resources
From class java.lang.Object

Public Methods

public int addResource (Resources res, int id)

Makes any individual resource available from another Resources instance available in this XResources instance.

This method combines calls to getFakeResId(Resources, int) and setReplacement(int, Object) to generate a fake resource ID and set up a replacement for it which forwards to the given resource.

The returned ID can only be used to retrieve the resource, it won't work for methods like Resources.getResourceName(int) etc.

Parameters
res The target Resources instance.
id The target resource ID.
Returns
  • The fake resource ID (see above).

public static int getFakeResId (String resName)

Generates a fake resource ID.

The parameter is just hashed, it doesn't have a deeper meaning. However, it's recommended to use values with a low risk for conflicts, such as a full resource name. Calling this method multiple times will return the same ID.

Parameters
resName A used for hashing, see above.
Returns
  • The fake resource ID.

public static int getFakeResId (Resources res, int id)

Generates a fake resource ID.

This variant uses the result of Resources.getResourceName(int) to create the hash that the ID is based on. The given resource doesn't need to match the XResources instance for which the fake resource ID is going to be used.

Parameters
res The Resources object to be used for hashing.
id The resource ID to be used for hashing.
Returns
  • The fake resource ID.

public String getPackageName ()

Returns the name of the package that these resources belong to, or "android" for system resources.

public static String getPackageNameDuringConstruction ()

Added in API level 52

Special case of getPackageName() during object creation.

For a short moment during/after the creation of a new Resources object, it isn't an instance of XResources yet. For any hooks that need information about the just created object during this particular stage, this method will return the package name.

If you call this method outside of getTopLevelResources(), it throws an IllegalStateException.

public XC_LayoutInflated.Unhook hookLayout (String pkg, String type, String name, XC_LayoutInflated callback)

Hook the inflation of a layout.

Parameters
pkg The package name, e.g. com.android.systemui. See Resources.getResourcePackageName(int).
type The type name, e.g. layout. See Resources.getResourceTypeName(int).
name The entry name, e.g. statusbar. See Resources.getResourceEntryName(int).
callback The callback to be executed when the layout has been inflated.
Returns
  • An object which can be used to remove the callback again.

public XC_LayoutInflated.Unhook hookLayout (int id, XC_LayoutInflated callback)

Hook the inflation of a layout.

Parameters
id The ID of the resource which should be replaced.
callback The callback to be executed when the layout has been inflated.
Returns
  • An object which can be used to remove the callback again.

public XC_LayoutInflated.Unhook hookLayout (String fullName, XC_LayoutInflated callback)

This method was deprecated in API level 81.
Use hookLayout(String, String, String, XC_LayoutInflated) instead.

Hook the inflation of a layout.

Parameters
fullName The full resource name, e.g. com.android.systemui:layout/statusbar. See Resources.getResourceName(int).
callback The callback to be executed when the layout has been inflated.
Returns
  • An object which can be used to remove the callback again.

public static XC_LayoutInflated.Unhook hookSystemWideLayout (String fullName, XC_LayoutInflated callback)

This method was deprecated in API level 81.
Use hookSystemWideLayout(String, String, String, XC_LayoutInflated) instead.

Hook the inflation of an Android framework layout (in the android package). See hookSystemWideLayout(String, String, String, XC_LayoutInflated).

Parameters
fullName The full resource name, e.g. android:layout/simple_list_item_1. See Resources.getResourceName(int).
callback The callback to be executed when the layout has been inflated.
Returns
  • An object which can be used to remove the callback again.

public static XC_LayoutInflated.Unhook hookSystemWideLayout (int id, XC_LayoutInflated callback)

Hook the inflation of an Android framework layout (in the android package). See hookSystemWideLayout(String, String, String, XC_LayoutInflated).

Parameters
id The ID of the resource which should be replaced.
callback The callback to be executed when the layout has been inflated.
Returns
  • An object which can be used to remove the callback again.

public static XC_LayoutInflated.Unhook hookSystemWideLayout (String pkg, String type, String name, XC_LayoutInflated callback)

Hook the inflation of an Android framework layout (in the android package).

Some layouts are part of the Android framework and can be used in any app. They're accessible via android.R.layout and are not bound to a specific Resources instance. Such resources can be replaced in initZygote() for all apps. As there is no XResources object easily available in that scope, this static method can be used to hook layouts.

Parameters
pkg The package name, e.g. android. See Resources.getResourcePackageName(int).
type The type name, e.g. layout. See Resources.getResourceTypeName(int).
name The entry name, e.g. simple_list_item_1. See Resources.getResourceEntryName(int).
callback The callback to be executed when the layout has been inflated.
Returns
  • An object which can be used to remove the callback again.

public void setReplacement (String fullName, Object replacement)

This method was deprecated in API level 81.
Use setReplacement(String, String, String, Object) instead.

Sets a replacement for an individual resource. See setReplacement(String, String, String, Object).

Parameters
fullName The full resource name, e.g. com.example.myapplication:string/app_name. See Resources.getResourceName(int).
replacement The replacement.

public void setReplacement (String pkg, String type, String name, Object replacement)

Sets a replacement for an individual resource. If called more than once for the same ID, the replacement from the last call is used. Setting the replacement to null removes it.

The allowed replacements depend on the type of the source. All types accept an XResForwarder object, which is usually created with XModuleResources.fwd(int). The resource request will then be forwarded to another Resources object. In addition to that, the following replacement types are accepted:

Resource type Additional allowed replacement types (*) Returned from (**)
Animation  none Resources.getAnimation(int)
Bool Boolean Resources.getBoolean(int)
Color Integer (you might want to use Color.parseColor(String)) Resources.getColor(int)
Resources.getDrawable(int) (creates a ColorDrawable)
Resources.getColorStateList(int) (calls ColorStateList.valueOf(int))
Color State List ColorStateList
Integer (calls ColorStateList.valueOf(int))
Resources.getColorStateList(int)
Dimension XResources.DimensionReplacement (since v50) Resources.getDimension(int)
Resources.getDimensionPixelOffset(int)
Resources.getDimensionPixelSize(int)
Drawable (including mipmap) XResources.DrawableLoader
Integer (creates a ColorDrawable)
Resources.getDrawable(int)
Resources.getDrawableForDensity(int, int)
Fraction  none Resources.getFraction(int, int, int)
Integer Integer Resources.getInteger(int)
Integer Array int[] Resources.getIntArray(int)
Layout  none, but see hookLayout(int, XC_LayoutInflated) Resources.getLayout(int)
Movie  none Resources.getMovie(int)
Quantity Strings (Plurals)  none Resources.getQuantityString(int, int)
Resources.getQuantityText(int, int)
String String
CharSequence (for styled texts, see also Html.fromHtml(String))
Resources.getString(int)
Resources.getText(int)
String Array String[]
CharSequence[] (for styled texts, see also Html.fromHtml(String))
Resources.getStringArray(int)
Resources.getTextArray(int)
XML  none Resources.getXml(int)
Resources.getQuantityText(int, int)

Other resource types, such as styles/themes, raw resources and typed arrays can't be replaced.

* Auto-boxing allows you to use literals like 123 where an Integer is accepted, so you don't neeed to call methods like Integer.valueOf(int) manually.
** Some of these methods have multiple variants, only one of them is mentioned here.

Parameters
pkg The package name, e.g. com.example.myapplication. See Resources.getResourcePackageName(int).
type The type name, e.g. string. See Resources.getResourceTypeName(int).
name The entry name, e.g. app_name. See Resources.getResourceEntryName(int).
replacement The replacement.

public void setReplacement (int id, Object replacement)

Sets a replacement for an individual resource. See setReplacement(String, String, String, Object).

Parameters
id The ID of the resource which should be replaced.
replacement The replacement, see above.

public static void setSystemWideReplacement (String fullName, Object replacement)

This method was deprecated in API level 81.
Use setSystemWideReplacement(String, String, String, Object) instead.

Sets a replacement for an individual Android framework resource (in the android package). See setSystemWideReplacement(String, String, String, Object).

Parameters
fullName The full resource name, e.g. android:string/yes. See Resources.getResourceName(int).
replacement The replacement.

public static void setSystemWideReplacement (int id, Object replacement)

Sets a replacement for an individual Android framework resource (in the android package). See setSystemWideReplacement(String, String, String, Object).

Parameters
id The ID of the resource which should be replaced.
replacement The replacement.

public static void setSystemWideReplacement (String pkg, String type, String name, Object replacement)

Sets a replacement for an individual Android framework resource (in the android package).

Some resources are part of the Android framework and can be used in any app. They're accessible via android.R and are not bound to a specific Resources instance. Such resources can be replaced in initZygote() for all apps. As there is no XResources object easily available in that scope, this static method can be used to set resource replacements. All other details (e.g. how certain types can be replaced) are mentioned in setReplacement(String, String, String, Object).

Parameters
pkg The package name, should always be android here. See Resources.getResourcePackageName(int).
type The type name, e.g. string. See Resources.getResourceTypeName(int).
name The entry name, e.g. yes. See Resources.getResourceEntryName(int).
replacement The replacement.