Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
[res] Add the grammatical gender qualifier
Browse files Browse the repository at this point in the history
Bug: 237579711
Test: UTs + build + boot

Change-Id: Id0919799a8a364f109ff351974f02e4f151f23cd
  • Loading branch information
Yurii Zubrytskyi committed Jan 14, 2023
1 parent dac535a commit 02d8919
Show file tree
Hide file tree
Showing 23 changed files with 242 additions and 53 deletions.
6 changes: 3 additions & 3 deletions core/api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12984,9 +12984,9 @@ package android.content.res {
field @NonNull public static final android.os.Parcelable.Creator<android.content.res.Configuration> CREATOR;
field public static final int DENSITY_DPI_UNDEFINED = 0; // 0x0
field public static final int FONT_WEIGHT_ADJUSTMENT_UNDEFINED = 2147483647; // 0x7fffffff
field public static final int GRAMMATICAL_GENDER_FEMININE = 3; // 0x3
field public static final int GRAMMATICAL_GENDER_MASCULINE = 4; // 0x4
field public static final int GRAMMATICAL_GENDER_NEUTRAL = 2; // 0x2
field public static final int GRAMMATICAL_GENDER_FEMININE = 2; // 0x2
field public static final int GRAMMATICAL_GENDER_MASCULINE = 3; // 0x3
field public static final int GRAMMATICAL_GENDER_NEUTRAL = 1; // 0x1
field public static final int GRAMMATICAL_GENDER_NOT_SPECIFIED = 0; // 0x0
field public static final int HARDKEYBOARDHIDDEN_NO = 1; // 0x1
field public static final int HARDKEYBOARDHIDDEN_UNDEFINED = 0; // 0x0
Expand Down
5 changes: 3 additions & 2 deletions core/java/android/content/pm/ActivityInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@ public static String launchModeToString(@LaunchMode int launchMode) {
Configuration.NATIVE_CONFIG_DENSITY, // DENSITY
Configuration.NATIVE_CONFIG_LAYOUTDIR, // LAYOUT DIRECTION
Configuration.NATIVE_CONFIG_COLOR_MODE, // COLOR_MODE
Configuration.NATIVE_CONFIG_GRAMMATICAL_GENDER,
};

/**
Expand Down Expand Up @@ -1267,8 +1268,8 @@ public int getRealConfigChanged() {
* {@link #CONFIG_LOCALE}, {@link #CONFIG_TOUCHSCREEN},
* {@link #CONFIG_KEYBOARD}, {@link #CONFIG_NAVIGATION},
* {@link #CONFIG_ORIENTATION}, {@link #CONFIG_SCREEN_LAYOUT},
* {@link #CONFIG_DENSITY}, {@link #CONFIG_LAYOUT_DIRECTION} and
* {@link #CONFIG_COLOR_MODE}.
* {@link #CONFIG_DENSITY}, {@link #CONFIG_LAYOUT_DIRECTION},
* {@link #CONFIG_COLOR_MODE}, and {link #CONFIG_GRAMMATICAL_GENDER}.
* Set from the {@link android.R.attr#configChanges} attribute.
*/
public int configChanges;
Expand Down
6 changes: 3 additions & 3 deletions core/java/android/content/pm/PackageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ private static void collectCertificates(Package pkg, File apkFile, boolean skipV

private static AssetManager newConfiguredAssetManager() {
AssetManager assetManager = new AssetManager();
assetManager.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
assetManager.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Build.VERSION.RESOURCES_SDK_INT);
return assetManager;
}
Expand Down Expand Up @@ -9011,7 +9011,7 @@ public AssetManager getBaseAssetManager() throws PackageParserException {
}

AssetManager assets = new AssetManager();
assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Build.VERSION.RESOURCES_SDK_INT);
assets.setApkAssets(apkAssets, false /*invalidateCaches*/);

Expand Down Expand Up @@ -9086,7 +9086,7 @@ private static ApkAssets loadApkAssets(String path, @ParseFlags int flags)

private static AssetManager createAssetManagerWithAssets(ApkAssets[] apkAssets) {
final AssetManager assets = new AssetManager();
assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Build.VERSION.RESOURCES_SDK_INT);
assets.setApkAssets(apkAssets, false /*invalidateCaches*/);
return assets;
Expand Down
7 changes: 4 additions & 3 deletions core/java/android/content/res/AssetManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1461,13 +1461,14 @@ Configuration[] getSizeConfigurations() {
public void setConfiguration(int mcc, int mnc, @Nullable String locale, int orientation,
int touchscreen, int density, int keyboard, int keyboardHidden, int navigation,
int screenWidth, int screenHeight, int smallestScreenWidthDp, int screenWidthDp,
int screenHeightDp, int screenLayout, int uiMode, int colorMode, int majorVersion) {
int screenHeightDp, int screenLayout, int uiMode, int colorMode, int grammaticalGender,
int majorVersion) {
synchronized (this) {
ensureValidLocked();
nativeSetConfiguration(mObject, mcc, mnc, locale, orientation, touchscreen, density,
keyboard, keyboardHidden, navigation, screenWidth, screenHeight,
smallestScreenWidthDp, screenWidthDp, screenHeightDp, screenLayout, uiMode,
colorMode, majorVersion);
colorMode, grammaticalGender, majorVersion);
}
}

Expand Down Expand Up @@ -1557,7 +1558,7 @@ private static native void nativeSetConfiguration(long ptr, int mcc, int mnc,
@Nullable String locale, int orientation, int touchscreen, int density, int keyboard,
int keyboardHidden, int navigation, int screenWidth, int screenHeight,
int smallestScreenWidthDp, int screenWidthDp, int screenHeightDp, int screenLayout,
int uiMode, int colorMode, int majorVersion);
int uiMode, int colorMode, int grammaticalGender, int majorVersion);
private static native @NonNull SparseArray<String> nativeGetAssignedPackageIdentifiers(
long ptr, boolean includeOverlays, boolean includeLoaders);

Expand Down
67 changes: 50 additions & 17 deletions core/java/android/content/res/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static android.content.ConfigurationProto.DENSITY_DPI;
import static android.content.ConfigurationProto.FONT_SCALE;
import static android.content.ConfigurationProto.FONT_WEIGHT_ADJUSTMENT;
import static android.content.ConfigurationProto.GRAMMATICAL_GENDER;
import static android.content.ConfigurationProto.HARD_KEYBOARD_HIDDEN;
import static android.content.ConfigurationProto.KEYBOARD;
import static android.content.ConfigurationProto.KEYBOARD_HIDDEN;
Expand Down Expand Up @@ -167,19 +168,19 @@ public final class Configuration implements Parcelable, Comparable<Configuration
* Constant for grammatical gender: to indicate the terms of address the user
* preferred in an application is neuter.
*/
public static final int GRAMMATICAL_GENDER_NEUTRAL = 2;
public static final int GRAMMATICAL_GENDER_NEUTRAL = 1;

/**
* Constant for grammatical gender: to indicate the terms of address the user
* preferred in an application is feminine.
*/
public static final int GRAMMATICAL_GENDER_FEMININE = 3;
public static final int GRAMMATICAL_GENDER_FEMININE = 2;

/**
* Constant for grammatical gender: to indicate the terms of address the user
* preferred in an application is masculine.
*/
public static final int GRAMMATICAL_GENDER_MASCULINE = 4;
public static final int GRAMMATICAL_GENDER_MASCULINE = 3;

/** Constant for {@link #colorMode}: bits that encode whether the screen is wide gamut. */
public static final int COLOR_MODE_WIDE_COLOR_GAMUT_MASK = 0x3;
Expand Down Expand Up @@ -529,15 +530,10 @@ public static String configurationDiffToString(int diff) {
if ((diff & ActivityInfo.CONFIG_FONT_WEIGHT_ADJUSTMENT) != 0) {
list.add("CONFIG_AUTO_BOLD_TEXT");
}
StringBuilder builder = new StringBuilder("{");
for (int i = 0, n = list.size(); i < n; i++) {
builder.append(list.get(i));
if (i != n - 1) {
builder.append(", ");
}
if ((diff & ActivityInfo.CONFIG_GRAMMATICAL_GENDER) != 0) {
list.add("CONFIG_GRAMMATICAL_GENDER");
}
builder.append("}");
return builder.toString();
return "{" + TextUtils.join(", ", list) + "}";
}

/**
Expand Down Expand Up @@ -970,6 +966,7 @@ public boolean isLayoutSizeAtLeast(int size) {
NATIVE_CONFIG_SMALLEST_SCREEN_SIZE,
NATIVE_CONFIG_LAYOUTDIR,
NATIVE_CONFIG_COLOR_MODE,
NATIVE_CONFIG_GRAMMATICAL_GENDER,
})
@Retention(RetentionPolicy.SOURCE)
public @interface NativeConfig {}
Expand Down Expand Up @@ -1008,6 +1005,9 @@ public boolean isLayoutSizeAtLeast(int size) {
public static final int NATIVE_CONFIG_LAYOUTDIR = 0x4000;
/** @hide Native-specific bit mask for COLOR_MODE config ; DO NOT USE UNLESS YOU ARE SURE.*/
public static final int NATIVE_CONFIG_COLOR_MODE = 0x10000;
/** @hide Native-specific bit mask for GRAMMATICAL_GENDER config; DO NOT USE UNLESS YOU
* ARE SURE.*/
public static final int NATIVE_CONFIG_GRAMMATICAL_GENDER = 0x20000;

/**
* <p>Construct an invalid Configuration. This state is only suitable for constructing a
Expand Down Expand Up @@ -1112,6 +1112,14 @@ public String toString() {
} else {
sb.append(" ?localeList");
}
if (mGrammaticalGender != 0) {
switch (mGrammaticalGender) {
case GRAMMATICAL_GENDER_NEUTRAL: sb.append(" neuter"); break;
case GRAMMATICAL_GENDER_FEMININE: sb.append(" feminine"); break;
case GRAMMATICAL_GENDER_MASCULINE: sb.append(" masculine"); break;
case GRAMMATICAL_GENDER_NOT_SPECIFIED: sb.append(" ?grgend"); break;
}
}
int layoutDir = (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK);
switch (layoutDir) {
case SCREENLAYOUT_LAYOUTDIR_UNDEFINED: sb.append(" ?layoutDir"); break;
Expand Down Expand Up @@ -1292,6 +1300,7 @@ public void dumpDebug(ProtoOutputStream protoOutputStream, long fieldId, boolean
protoOutputStream.write(ORIENTATION, orientation);
protoOutputStream.write(SCREEN_WIDTH_DP, screenWidthDp);
protoOutputStream.write(SCREEN_HEIGHT_DP, screenHeightDp);
protoOutputStream.write(GRAMMATICAL_GENDER, mGrammaticalGender);
protoOutputStream.end(token);
}

Expand Down Expand Up @@ -1454,6 +1463,9 @@ public void readFromProto(ProtoInputStream protoInputStream, long fieldId) throw
case (int) FONT_WEIGHT_ADJUSTMENT:
fontWeightAdjustment = protoInputStream.readInt(FONT_WEIGHT_ADJUSTMENT);
break;
case (int) GRAMMATICAL_GENDER:
mGrammaticalGender = protoInputStream.readInt(GRAMMATICAL_GENDER);
break;
}
}
} finally {
Expand Down Expand Up @@ -1839,6 +1851,9 @@ public void setTo(@NonNull Configuration delta, @Config int mask,
if ((mask & ActivityInfo.CONFIG_FONT_WEIGHT_ADJUSTMENT) != 0) {
fontWeightAdjustment = delta.fontWeightAdjustment;
}
if ((mask & ActivityInfo.CONFIG_GRAMMATICAL_GENDER) != 0) {
mGrammaticalGender = delta.mGrammaticalGender;
}
}

/**
Expand Down Expand Up @@ -1975,7 +1990,7 @@ && getScreenLayoutNoDirection(screenLayout) !=
changed |= ActivityInfo.CONFIG_FONT_WEIGHT_ADJUSTMENT;
}

if (!publicOnly&& mGrammaticalGender != delta.mGrammaticalGender) {
if (!publicOnly && mGrammaticalGender != delta.mGrammaticalGender) {
changed |= ActivityInfo.CONFIG_GRAMMATICAL_GENDER;
}
return changed;
Expand Down Expand Up @@ -2172,6 +2187,8 @@ public int compareTo(Configuration that) {
if (n != 0) return n;
}

n = this.mGrammaticalGender - that.mGrammaticalGender;
if (n != 0) return n;
n = this.touchscreen - that.touchscreen;
if (n != 0) return n;
n = this.keyboard - that.keyboard;
Expand Down Expand Up @@ -2205,11 +2222,6 @@ public int compareTo(Configuration that) {
n = windowConfiguration.compareTo(that.windowConfiguration);
if (n != 0) return n;
n = this.fontWeightAdjustment - that.fontWeightAdjustment;
if (n != 0) return n;
n = this.mGrammaticalGender - that.mGrammaticalGender;
if (n != 0) return n;

// if (n != 0) return n;
return n;
}

Expand Down Expand Up @@ -2482,6 +2494,20 @@ public static String resourceQualifierString(Configuration config, DisplayMetric
}
}

switch (config.mGrammaticalGender) {
case Configuration.GRAMMATICAL_GENDER_NEUTRAL:
parts.add("neuter");
break;
case Configuration.GRAMMATICAL_GENDER_FEMININE:
parts.add("feminine");
break;
case Configuration.GRAMMATICAL_GENDER_MASCULINE:
parts.add("masculine");
break;
default:
break;
}

switch (config.screenLayout & Configuration.SCREENLAYOUT_LAYOUTDIR_MASK) {
case Configuration.SCREENLAYOUT_LAYOUTDIR_LTR:
parts.add("ldltr");
Expand Down Expand Up @@ -2768,6 +2794,10 @@ public static Configuration generateDelta(
delta.locale = change.locale;
}

if (base.mGrammaticalGender != change.mGrammaticalGender) {
delta.mGrammaticalGender = change.mGrammaticalGender;
}

if (base.touchscreen != change.touchscreen) {
delta.touchscreen = change.touchscreen;
}
Expand Down Expand Up @@ -2881,6 +2911,7 @@ public static Configuration generateDelta(
private static final String XML_ATTR_DENSITY = "density";
private static final String XML_ATTR_APP_BOUNDS = "app_bounds";
private static final String XML_ATTR_FONT_WEIGHT_ADJUSTMENT = "fontWeightAdjustment";
private static final String XML_ATTR_GRAMMATICAL_GENDER = "grammaticalGender";

/**
* Reads the attributes corresponding to Configuration member fields from the Xml parser.
Expand Down Expand Up @@ -2932,6 +2963,8 @@ public static void readXmlAttrs(XmlPullParser parser, Configuration configOut)
DENSITY_DPI_UNDEFINED);
configOut.fontWeightAdjustment = XmlUtils.readIntAttribute(parser,
XML_ATTR_FONT_WEIGHT_ADJUSTMENT, FONT_WEIGHT_ADJUSTMENT_UNDEFINED);
configOut.mGrammaticalGender = XmlUtils.readIntAttribute(parser,
XML_ATTR_GRAMMATICAL_GENDER, GRAMMATICAL_GENDER_NOT_SPECIFIED);

// For persistence, we don't care about assetsSeq and WindowConfiguration, so do not read it
// out.
Expand Down
3 changes: 2 additions & 1 deletion core/java/android/content/res/ResourcesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ public void updateConfiguration(Configuration config, DisplayMetrics metrics,
mConfiguration.smallestScreenWidthDp,
mConfiguration.screenWidthDp, mConfiguration.screenHeightDp,
mConfiguration.screenLayout, mConfiguration.uiMode,
mConfiguration.colorMode, Build.VERSION.RESOURCES_SDK_INT);
mConfiguration.colorMode, mConfiguration.getGrammaticalGender(),
Build.VERSION.RESOURCES_SDK_INT);

if (DEBUG_CONFIG) {
Slog.i(TAG, "**** Updating config of " + this + ": final config is "
Expand Down
5 changes: 3 additions & 2 deletions core/jni/android_util_AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jin
jint screen_width, jint screen_height,
jint smallest_screen_width_dp, jint screen_width_dp,
jint screen_height_dp, jint screen_layout, jint ui_mode,
jint color_mode, jint major_version) {
jint color_mode, jint grammatical_gender, jint major_version) {
ATRACE_NAME("AssetManager::SetConfiguration");

ResTable_config configuration;
Expand All @@ -345,6 +345,7 @@ static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jin
configuration.screenLayout = static_cast<uint8_t>(screen_layout);
configuration.uiMode = static_cast<uint8_t>(ui_mode);
configuration.colorMode = static_cast<uint8_t>(color_mode);
configuration.grammaticalInflection = static_cast<uint8_t>(grammatical_gender);
configuration.sdkVersion = static_cast<uint16_t>(major_version);

if (locale != nullptr) {
Expand Down Expand Up @@ -1448,7 +1449,7 @@ static const JNINativeMethod gAssetManagerMethods[] = {
{"nativeCreate", "()J", (void*)NativeCreate},
{"nativeDestroy", "(J)V", (void*)NativeDestroy},
{"nativeSetApkAssets", "(J[Landroid/content/res/ApkAssets;Z)V", (void*)NativeSetApkAssets},
{"nativeSetConfiguration", "(JIILjava/lang/String;IIIIIIIIIIIIIII)V",
{"nativeSetConfiguration", "(JIILjava/lang/String;IIIIIIIIIIIIIIII)V",
(void*)NativeSetConfiguration},
{"nativeGetAssignedPackageIdentifiers", "(JZZ)Landroid/util/SparseArray;",
(void*)NativeGetAssignedPackageIdentifiers},
Expand Down
1 change: 1 addition & 0 deletions core/proto/android/content/configuration.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ message ConfigurationProto {
optional .android.app.WindowConfigurationProto window_configuration = 19;
optional string locale_list = 20;
optional uint32 font_weight_adjustment = 21;
optional uint32 grammatical_gender = 22;
}

/**
Expand Down

0 comments on commit 02d8919

Please sign in to comment.