Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2ad05399 authored by William Xiao's avatar William Xiao Committed by Android (Google) Code Review
Browse files

Merge "Revert "[res] Don't recalculate configuration twice on init"" into main

parents 74f2a6ae d38bd518
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -550,7 +550,7 @@ public class ResourcesManager {
    @UnsupportedAppUsage
    protected @Nullable AssetManager createAssetManager(@NonNull final ResourcesKey key,
            @Nullable ApkAssetsSupplier apkSupplier) {
        final AssetManager.Builder builder = new AssetManager.Builder().setNoInit();
        final AssetManager.Builder builder = new AssetManager.Builder();

        final ArrayList<ApkKey> apkKeys = extractApkKeys(key);
        for (int i = 0, n = apkKeys.size(); i < n; i++) {
@@ -1555,7 +1555,7 @@ public class ResourcesManager {
        } else if(overlayPaths == null) {
            return ArrayUtils.cloneOrNull(resourceDirs);
        } else {
            final var paths = new ArrayList<String>(overlayPaths.length + resourceDirs.length);
            final ArrayList<String> paths = new ArrayList<>();
            for (final String path : overlayPaths) {
                paths.add(path);
            }
+6 −30
Original line number Diff line number Diff line
@@ -137,8 +137,6 @@ public final class AssetManager implements AutoCloseable {
        private ArrayList<ApkAssets> mUserApkAssets = new ArrayList<>();
        private ArrayList<ResourcesLoader> mLoaders = new ArrayList<>();

        private boolean mNoInit = false;

        public Builder addApkAssets(ApkAssets apkAssets) {
            mUserApkAssets.add(apkAssets);
            return this;
@@ -149,11 +147,6 @@ public final class AssetManager implements AutoCloseable {
            return this;
        }

        public Builder setNoInit() {
            mNoInit = true;
            return this;
        }

        public AssetManager build() {
            // Retrieving the system ApkAssets forces their creation as well.
            final ApkAssets[] systemApkAssets = getSystem().getApkAssets();
@@ -195,7 +188,7 @@ public final class AssetManager implements AutoCloseable {
            final AssetManager assetManager = new AssetManager(false /*sentinel*/);
            assetManager.mApkAssets = apkAssets;
            AssetManager.nativeSetApkAssets(assetManager.mObject, apkAssets,
                    false /*invalidateCaches*/, mNoInit /*preset*/);
                    false /*invalidateCaches*/);
            assetManager.mLoaders = mLoaders.isEmpty() ? null
                    : mLoaders.toArray(new ResourcesLoader[0]);

@@ -336,7 +329,7 @@ public final class AssetManager implements AutoCloseable {
        synchronized (this) {
            ensureOpenLocked();
            mApkAssets = newApkAssets;
            nativeSetApkAssets(mObject, mApkAssets, invalidateCaches, false);
            nativeSetApkAssets(mObject, mApkAssets, invalidateCaches);
            if (invalidateCaches) {
                // Invalidate all caches.
                invalidateCachesLocked(-1);
@@ -503,7 +496,7 @@ public final class AssetManager implements AutoCloseable {

            mApkAssets = Arrays.copyOf(mApkAssets, count + 1);
            mApkAssets[count] = assets;
            nativeSetApkAssets(mObject, mApkAssets, true, false);
            nativeSetApkAssets(mObject, mApkAssets, true);
            invalidateCachesLocked(-1);
            return count + 1;
        }
@@ -1510,29 +1503,12 @@ public final class AssetManager implements AutoCloseable {
            int navigation, int screenWidth, int screenHeight, int smallestScreenWidthDp,
            int screenWidthDp, int screenHeightDp, int screenLayout, int uiMode, int colorMode,
            int grammaticalGender, int majorVersion) {
        setConfigurationInternal(mcc, mnc, defaultLocale, locales, orientation,
                touchscreen, density, keyboard, keyboardHidden, navigation, screenWidth,
                screenHeight, smallestScreenWidthDp, screenWidthDp, screenHeightDp,
                screenLayout, uiMode, colorMode, grammaticalGender, majorVersion, false);
    }

    /**
     * Change the configuration used when retrieving resources, and potentially force a refresh of
     * the state.  Not for use by applications.
     * @hide
     */
    void setConfigurationInternal(int mcc, int mnc, String defaultLocale, String[] locales,
            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 grammaticalGender, int majorVersion, boolean forceRefresh) {
        synchronized (this) {
            ensureValidLocked();
            nativeSetConfiguration(mObject, mcc, mnc, defaultLocale, locales, orientation,
                    touchscreen, density, keyboard, keyboardHidden, navigation, screenWidth,
                    screenHeight, smallestScreenWidthDp, screenWidthDp, screenHeightDp,
                    screenLayout, uiMode, colorMode, grammaticalGender, majorVersion,
                    forceRefresh);
                    screenLayout, uiMode, colorMode, grammaticalGender, majorVersion);
        }
    }

@@ -1617,13 +1593,13 @@ public final class AssetManager implements AutoCloseable {
    private static native long nativeCreate();
    private static native void nativeDestroy(long ptr);
    private static native void nativeSetApkAssets(long ptr, @NonNull ApkAssets[] apkAssets,
            boolean invalidateCaches, boolean preset);
            boolean invalidateCaches);
    private static native void nativeSetConfiguration(long ptr, int mcc, int mnc,
            @Nullable String defaultLocale, @NonNull String[] locales, 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 grammaticalGender,
            int majorVersion, boolean forceRefresh);
            int majorVersion);
    private static native @NonNull SparseArray<String> nativeGetAssignedPackageIdentifiers(
            long ptr, boolean includeOverlays, boolean includeLoaders);

+4 −9
Original line number Diff line number Diff line
@@ -200,7 +200,7 @@ public class ResourcesImpl {
        mMetrics.setToDefaults();
        mDisplayAdjustments = displayAdjustments;
        mConfiguration.setToDefaults();
        updateConfigurationImpl(config, metrics, displayAdjustments.getCompatibilityInfo(), true);
        updateConfiguration(config, metrics, displayAdjustments.getCompatibilityInfo());
    }

    public DisplayAdjustments getDisplayAdjustments() {
@@ -403,11 +403,6 @@ public class ResourcesImpl {

    public void updateConfiguration(Configuration config, DisplayMetrics metrics,
                                    CompatibilityInfo compat) {
        updateConfigurationImpl(config, metrics, compat, false);
    }

    private void updateConfigurationImpl(Configuration config, DisplayMetrics metrics,
                                    CompatibilityInfo compat, boolean forceAssetsRefresh) {
        Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "ResourcesImpl#updateConfiguration");
        try {
            synchronized (mAccessLock) {
@@ -533,7 +528,7 @@ public class ResourcesImpl {
                    keyboardHidden = mConfiguration.keyboardHidden;
                }

                mAssets.setConfigurationInternal(mConfiguration.mcc, mConfiguration.mnc,
                mAssets.setConfiguration(mConfiguration.mcc, mConfiguration.mnc,
                        defaultLocale,
                        selectedLocales,
                        mConfiguration.orientation,
@@ -544,7 +539,7 @@ public class ResourcesImpl {
                        mConfiguration.screenWidthDp, mConfiguration.screenHeightDp,
                        mConfiguration.screenLayout, mConfiguration.uiMode,
                        mConfiguration.colorMode, mConfiguration.getGrammaticalGender(),
                        Build.VERSION.RESOURCES_SDK_INT, forceAssetsRefresh);
                        Build.VERSION.RESOURCES_SDK_INT);

                if (DEBUG_CONFIG) {
                    Slog.i(TAG, "**** Updating config of " + this + ": final config is "
+6 −11
Original line number Diff line number Diff line
@@ -314,8 +314,7 @@ static void NativeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) {
}

static void NativeSetApkAssets(JNIEnv* env, jclass /*clazz*/, jlong ptr,
                               jobjectArray apk_assets_array, jboolean invalidate_caches,
                               jboolean preset) {
                               jobjectArray apk_assets_array, jboolean invalidate_caches) {
  ATRACE_NAME("AssetManager::SetApkAssets");

  const jsize apk_assets_len = env->GetArrayLength(apk_assets_array);
@@ -344,12 +343,8 @@ static void NativeSetApkAssets(JNIEnv* env, jclass /*clazz*/, jlong ptr,
  }

  auto assetmanager = LockAndStartAssetManager(ptr);
  if (preset) {
    assetmanager->PresetApkAssets(apk_assets);
  } else {
  assetmanager->SetApkAssets(apk_assets, invalidate_caches);
}
}

static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint mcc, jint mnc,
                                   jstring default_locale, jobjectArray locales, jint orientation,
@@ -358,7 +353,7 @@ static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jin
                                   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 grammatical_gender,
                                   jint major_version, jboolean force_refresh) {
                                   jint major_version) {
  ATRACE_NAME("AssetManager::SetConfiguration");

  const jsize locale_count = (locales == NULL) ? 0 : env->GetArrayLength(locales);
@@ -418,7 +413,7 @@ static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jin
  }

  auto assetmanager = LockAndStartAssetManager(ptr);
  assetmanager->SetConfigurations(std::move(configs), force_refresh != JNI_FALSE);
  assetmanager->SetConfigurations(configs);
  assetmanager->SetDefaultLocale(default_locale_int);
}

@@ -1527,8 +1522,8 @@ static const JNINativeMethod gAssetManagerMethods[] = {
        // AssetManager setup methods.
        {"nativeCreate", "()J", (void*)NativeCreate},
        {"nativeDestroy", "(J)V", (void*)NativeDestroy},
        {"nativeSetApkAssets", "(J[Landroid/content/res/ApkAssets;ZZ)V", (void*)NativeSetApkAssets},
        {"nativeSetConfiguration", "(JIILjava/lang/String;[Ljava/lang/String;IIIIIIIIIIIIIIIIZ)V",
        {"nativeSetApkAssets", "(J[Landroid/content/res/ApkAssets;Z)V", (void*)NativeSetApkAssets},
        {"nativeSetConfiguration", "(JIILjava/lang/String;[Ljava/lang/String;IIIIIIIIIIIIIIII)V",
         (void*)NativeSetConfiguration},
        {"nativeGetAssignedPackageIdentifiers", "(JZZ)Landroid/util/SparseArray;",
         (void*)NativeGetAssignedPackageIdentifiers},
+6 −14
Original line number Diff line number Diff line
@@ -117,10 +117,6 @@ bool AssetManager2::SetApkAssets(ApkAssetsList apk_assets, bool invalidate_cache
  return true;
}

void AssetManager2::PresetApkAssets(ApkAssetsList apk_assets) {
  BuildDynamicRefTable(apk_assets);
}

bool AssetManager2::SetApkAssets(std::initializer_list<ApkAssetsPtr> apk_assets,
                                 bool invalidate_caches) {
  return SetApkAssets(ApkAssetsList(apk_assets.begin(), apk_assets.size()), invalidate_caches);
@@ -436,12 +432,8 @@ bool AssetManager2::ContainsAllocatedTable() const {
  return false;
}

void AssetManager2::SetConfigurations(std::vector<ResTable_config> configurations,
    bool force_refresh) {
void AssetManager2::SetConfigurations(std::vector<ResTable_config> configurations) {
  int diff = 0;
  if (force_refresh) {
    diff = -1;
  } else {
  if (configurations_.size() != configurations.size()) {
    diff = -1;
  } else {
@@ -449,7 +441,6 @@ void AssetManager2::SetConfigurations(std::vector<ResTable_config> configuration
      diff |= configurations_[i].diff(configurations[i]);
    }
  }
  }
  configurations_ = std::move(configurations);

  if (diff) {
@@ -784,7 +775,8 @@ base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntry(
    bool has_locale = false;
    if (result->config.locale == 0) {
      if (default_locale_ != 0) {
        ResTable_config conf = {.locale = default_locale_};
        ResTable_config conf;
        conf.locale = default_locale_;
        // Since we know conf has a locale and only a locale, match will tell us if that locale
        // matches
        has_locale = conf.match(config);
Loading