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

Commit 8725a290 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "DocsUI M3: Add force_material3 config" into main

parents 64e4eb90 d9dbef72
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -213,3 +213,8 @@
-keep class com.google.android.material.chip.Chip {
  public android.graphics.drawable.Drawable getChipIcon();
}

-keep class com.android.documentsui.util.Material3Config {
  static int getRes(int);
  static void overrideForTest(java.util.Map);
}
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
@@ -75,4 +75,7 @@

    <!-- The maximum record of search history. -->
    <integer name="config_maximum_search_history">200</integer>

    <!-- The Material3 theme is only enabled IFF this config is true AND the flag `use_material3` is enabled. -->
    <bool name="force_material3">false</bool>
</resources>
+1 −0
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@
            <item type="bool" name="handle_view_downloads_intent"/>
            <item type="bool" name="is_launcher_enabled"/>
            <item type="bool" name="show_search_bar"/>
            <item type="bool" name="force_material3"/>
            <!-- END BOOLEAN CONFIG -->

            <!-- START STRING CONFIG -->
+12 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import com.android.documentsui.clipping.DocumentClipper;
import com.android.documentsui.queries.SearchHistoryManager;
import com.android.documentsui.roots.ProvidersCache;
import com.android.documentsui.theme.ThemeOverlayManager;
import com.android.documentsui.util.Material3Config;
import com.android.modules.utils.build.SdkLevel;

import com.google.common.collect.Lists;
@@ -166,9 +167,20 @@ public class DocumentsApplication extends Application {
        Log.d(TAG, "OverlayManager.setEnabled() result: " + result);
    }

    /**
     * Initializes configurations for Material3.
     *
     * <p>NOTE: It initializes even when the flag is disabled.
     */
    private void initializeMaterial3Config() {
        Material3Config.getInstance()
                .setForceMaterial3(getResources().getBoolean(R.bool.force_material3));
    }

    @SuppressLint("NewApi") // OverlayManager.class is @hide
    @Override
    public void onCreate() {
        initializeMaterial3Config();
        super.onCreate();
        synchronized (DocumentsApplication.class) {
            if (sConfigStore == null) {
+118 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui.util

import android.util.Log
import androidx.annotation.AnyRes
import com.android.documentsui.base.SharedMinimal.DEBUG
import com.android.documentsui.util.FlagUtils.Companion.isUseMaterial3FlagEnabled

/**
 * Mapping of resource IDs from the pre-material3 (aka legacy) theme to the new resource ID in the
 * material3 theme.
 */
private var idMapping: Map<Int, Int> = mapOf()
private var initialized = false

private const val TAG = "ThemeUtils"

/**
 * Only initialize the mapping when the use_material3 flag is enabled, because the IDs for the
 * Material3 version only exists then.
 */
private fun initializeIdMapping() {
  idMapping = mapOf()
}

interface Config {
  /**
   * Material3 is only fully enabled if the config forceMaterial3 is true AND the flag use_material3
   * is enabled.
   */
  var forceMaterial3: Boolean?
}

class Material3ConfigImpl() : Config {
  override var forceMaterial3: Boolean? = null
    set(value) {
      if (field != null) {
        Log.e(TAG, "forceMaterial3 is already set to $forceMaterial3")
        return
      }

      field = value
      if (DEBUG) {
        Log.d(
          TAG,
          "forceMaterial3 initializing with $value use_material3: ${
            isUseMaterial3FlagEnabled()
          }",
        )
      }
    }
}

abstract class Material3Config private constructor() {
  companion object {
    private val _instance: Config by lazy { Material3ConfigImpl() }

    @JvmStatic
    fun getInstance(): Config {
      return _instance
    }

    /**
     * Convert the resource ID from non-Material3 to Material3 version if the Material3 is enabled,
     * otherwise it returns the given ID as is.
     */
    @JvmStatic
    @AnyRes
    fun getRes(@AnyRes originalResourceId: Int): Int {
      if (!isUseMaterial3FlagEnabled()) {
        return originalResourceId
      }
      // TODO(lucmult): Enable this condition when all the resources are merged in one APK.
      // if (!(Material3Config.getInstance().forceMaterial3 ?: false)) {
      //   return originalResourceId
      // }
      if (!initialized) {
        initializeIdMapping()
      }

      val newId = idMapping[originalResourceId] ?: originalResourceId
      if (DEBUG) {
        if (newId != originalResourceId) {
          Log.d(
            TAG,
            "Replacing R ID from ${
              Integer.toHexString(
                originalResourceId
              )
            } to ${Integer.toHexString(newId)}",
          )
        }
      }

      return newId
    }

    @JvmStatic
    fun overrideForTest(overrides: Map<Int, Int>) {
      idMapping = overrides
    }
  }
}
Loading