のねのBlog

パソコンやソフト開発で起きた問題など書きます。よろしくお願いします。

applyConfigurationToResourcesLocked

Cross Reference: /frameworks/base/core/java/android/app/ResourcesManager.java

    763     public final boolean applyConfigurationToResourcesLocked(@NonNull Configuration config,
    764                                                              @Nullable CompatibilityInfo compat) {
    765         try {
    766             Trace.traceBegin(Trace.TRACE_TAG_RESOURCES,
    767                     "ResourcesManager#applyConfigurationToResourcesLocked");
    768 
    769             if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
    770                 if (DEBUG || DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
    771                         + mResConfiguration.seq + ", newSeq=" + config.seq);
    772                 return false;
    773             }
    774             int changes = mResConfiguration.updateFrom(config);
    775             // Things might have changed in display manager, so clear the cached displays.
    776             mDisplays.clear();
    777             DisplayMetrics defaultDisplayMetrics = getDisplayMetrics();
    778 
    779             if (compat != null && (mResCompatibilityInfo == null ||
    780                     !mResCompatibilityInfo.equals(compat))) {
    781                 mResCompatibilityInfo = compat;
    782                 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
    783                         | ActivityInfo.CONFIG_SCREEN_SIZE
    784                         | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
    785             }
    786 
    787             Resources.updateSystemConfiguration(config, defaultDisplayMetrics, compat); <==========
    788 
    789             ApplicationPackageManager.configurationChanged();                           <==========
    790             //Slog.i(TAG, "Configuration changed in " + currentPackageName());
    791 
    792             Configuration tmpConfig = null;
 (略)
    837             return changes != 0;
    838         } finally {
    839             Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
    840         }
    841     }

Cross Reference: /frameworks/base/core/java/android/content/res/Resources.java

   101     static Resources mSystem = null;

   1784     public static void updateSystemConfiguration(Configuration config, DisplayMetrics metrics,
   1785             CompatibilityInfo compat) {
   1786         if (mSystem != null) {
   1787             mSystem.updateConfiguration(config, metrics, compat);                       <==========
   1788             //Log.i(TAG, "Updated system resources " + mSystem
   1789             //        + ": " + mSystem.getConfiguration());
   1790         }
   1791     }

   1773     public void updateConfiguration(Configuration config, DisplayMetrics metrics,
   1774                                     CompatibilityInfo compat) {
   1775         mResourcesImpl.updateConfiguration(config, metrics, compat);                    <==========
   1776     }

Cross Reference: /frameworks/base/core/java/android/content/res/ResourcesImpl.java

    320     public void updateConfiguration(Configuration config, DisplayMetrics metrics,
    321                                     CompatibilityInfo compat) {
    322         Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "ResourcesImpl#updateConfiguration");
    323         try {
    324             synchronized (mAccessLock) {
    325                 if (false) {
    326                     Slog.i(TAG, "**** Updating config of " + this + ": old config is "
    327                             + mConfiguration + " old compat is "
    328                             + mDisplayAdjustments.getCompatibilityInfo());
    329                     Slog.i(TAG, "**** Updating config of " + this + ": new config is "
    330                             + config + " new compat is " + compat);
    331                 }
    332                 if (compat != null) {
    333                     mDisplayAdjustments.setCompatibilityInfo(compat);
    334                 }
    335                 if (metrics != null) {
    336                     mMetrics.setTo(metrics);
    337                 }
    338                 // NOTE: We should re-arrange this code to create a Display
    339                 // with the CompatibilityInfo that is used everywhere we deal
    340                 // with the display in relation to this app, rather than
    341                 // doing the conversion here.  This impl should be okay because
    342                 // we make sure to return a compatible display in the places
    343                 // where there are public APIs to retrieve the display...  but
    344                 // it would be cleaner and more maintainable to just be
    345                 // consistently dealing with a compatible display everywhere in
    346                 // the framework.
    347                 mDisplayAdjustments.getCompatibilityInfo().applyToDisplayMetrics(mMetrics);
    348 
    349                 final @Config int configChanges = calcConfigChanges(config);
    350 
    351                 // If even after the update there are no Locales set, grab the default locales.
    352                 LocaleList locales = mConfiguration.getLocales();
    353                 if (locales.isEmpty()) {
    354                     locales = LocaleList.getDefault();
    355                     mConfiguration.setLocales(locales);
    356                 }
    357 
    358                 if ((configChanges & ActivityInfo.CONFIG_LOCALE) != 0) {
    359                     if (locales.size() > 1) {
    360                         // The LocaleList has changed. We must query the AssetManager's available
    361                         // Locales and figure out the best matching Locale in the new LocaleList.
    362                         String[] availableLocales = mAssets.getNonSystemLocales();
    363                         if (LocaleList.isPseudoLocalesOnly(availableLocales)) {
    364                             // No app defined locales, so grab the system locales.
    365                             availableLocales = mAssets.getLocales();
    366                             if (LocaleList.isPseudoLocalesOnly(availableLocales)) {
    367                                 availableLocales = null;
    368                             }
    369                         }
    370 
    371                         if (availableLocales != null) {
    372                             final Locale bestLocale = locales.getFirstMatchWithEnglishSupported(
    373                                     availableLocales);
    374                             if (bestLocale != null && bestLocale != locales.get(0)) {
    375                                 mConfiguration.setLocales(new LocaleList(bestLocale, locales));
    376                             }
    377                         }
    378                     }
    379                 }
    380 
    381                 if (mConfiguration.densityDpi != Configuration.DENSITY_DPI_UNDEFINED) {
    382                     mMetrics.densityDpi = mConfiguration.densityDpi;
    383                     mMetrics.density =
    384                             mConfiguration.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
    385                 }
    386                 mMetrics.scaledDensity = mMetrics.density * mConfiguration.fontScale;
    387 
    388                 final int width, height;
    389                 if (mMetrics.widthPixels >= mMetrics.heightPixels) {
    390                     width = mMetrics.widthPixels;
    391                     height = mMetrics.heightPixels;
    392                 } else {
    393                     //noinspection SuspiciousNameCombination
    394                     width = mMetrics.heightPixels;
    395                     //noinspection SuspiciousNameCombination
    396                     height = mMetrics.widthPixels;
    397                 }
    398 
    399                 final int keyboardHidden;
    400                 if (mConfiguration.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO
    401                         && mConfiguration.hardKeyboardHidden
    402                         == Configuration.HARDKEYBOARDHIDDEN_YES) {
    403                     keyboardHidden = Configuration.KEYBOARDHIDDEN_SOFT;
    404                 } else {
    405                     keyboardHidden = mConfiguration.keyboardHidden;
    406                 }
    407 
    408                 mAssets.setConfiguration(mConfiguration.mcc, mConfiguration.mnc,
    409                         adjustLanguageTag(mConfiguration.getLocales().get(0).toLanguageTag()),
    410                         mConfiguration.orientation,
    411                         mConfiguration.touchscreen,
    412                         mConfiguration.densityDpi, mConfiguration.keyboard,
    413                         keyboardHidden, mConfiguration.navigation, width, height,
    414                         mConfiguration.smallestScreenWidthDp,
    415                         mConfiguration.screenWidthDp, mConfiguration.screenHeightDp,
    416                         mConfiguration.screenLayout, mConfiguration.uiMode,
    417                         Build.VERSION.RESOURCES_SDK_INT);
    418 
    419                 if (DEBUG_CONFIG) {
    420                     Slog.i(TAG, "**** Updating config of " + this + ": final config is "
    421                             + mConfiguration + " final compat is "
    422                             + mDisplayAdjustments.getCompatibilityInfo());
    423                 }
    424 
    425                 mDrawableCache.onConfigurationChange(configChanges);
    426                 mColorDrawableCache.onConfigurationChange(configChanges);
    427                 mComplexColorCache.onConfigurationChange(configChanges);
    428                 mAnimatorCache.onConfigurationChange(configChanges);
    429                 mStateListAnimatorCache.onConfigurationChange(configChanges);
    430 
    431                 flushLayoutCache();
    432             }
    433             synchronized (sSync) {
    434                 if (mPluralRule != null) {
    435                     mPluralRule = PluralRules.forLocale(mConfiguration.getLocales().get(0));
    436                 }
    437             }
    438         } finally {
    439             Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
    440         }
    441     }

Cross Reference: /frameworks/base/core/java/android/app/ApplicationPackageManager.java

ApplicationPackageManager.configurationChanged()
   1337     static void configurationChanged() {
   1338         synchronized (sSync) {
   1339             sIconCache.clear();
   1340             sStringCache.clear();
   1341         }
   1342     }

Cross Reference: /frameworks/base/core/java/android/app/ActivityThread.java

   5888     private void attach(boolean system) {
 (略)
   5949         ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
   5950             @Override
   5951             public void onConfigurationChanged(Configuration newConfig) {
   5952                 synchronized (mResourcesManager) {
   5953                     // We need to apply this change to the resources
   5954                     // immediately, because upon returning the view
   5955                     // hierarchy will be informed about it.
   5956                     if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {  <==========
   5957                         updateLocaleListFromAppContext(mInitialApplication.getApplicationContext(),
   5958                                 mResourcesManager.getConfiguration().getLocales());
   5959 
   5960                         // This actually changed the resources!  Tell
   5961                         // everyone about it.
   5962                         if (mPendingConfiguration == null ||
   5963                                 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
   5964                             mPendingConfiguration = newConfig;
   5965 
   5966                             sendMessage(H.CONFIGURATION_CHANGED, newConfig);
   5967                         }
   5968                     }
   5969                 }
   5970             }
   5971             @Override
   5972             public void onLowMemory() {
   5973             }
   5974             @Override
   5975             public void onTrimMemory(int level) {
   5976             }
   5977         });
   5978     }
   5979 

Cross Reference: /frameworks/base/core/java/android/view/ViewRootImpl.java

    457     public static void addConfigCallback(ComponentCallbacks callback) {
    458         synchronized (sConfigCallbacks) {
    459             sConfigCallbacks.add(callback);
    460         }
    461     }
      • -

Cross Reference: /frameworks/base/core/java/android/content/ComponentCallbacks.java

     30  */
     31 public interface ComponentCallbacks {
     47     void onConfigurationChanged(Configuration newConfig);
     67     void onLowMemory();
     68 }

Cross Reference: /frameworks/base/core/java/android/content/ComponentCallbacks2.java

     84 public interface ComponentCallbacks2 extends ComponentCallbacks {
     91     static final int TRIM_MEMORY_COMPLETE         = 80;
     98     static final int TRIM_MEMORY_MODERATE         = 60;
    105     static final int TRIM_MEMORY_BACKGROUND       = 40;
    113     static final int TRIM_MEMORY_UI_HIDDEN        = 20;
    125     static final int TRIM_MEMORY_RUNNING_CRITICAL = 15;
    133     static final int TRIM_MEMORY_RUNNING_LOW      = 10;
    142     static final int TRIM_MEMORY_RUNNING_MODERATE = 5;
    164     void onTrimMemory(int level);
    165 }
      • -

Cross Reference: /frameworks/base/core/java/android/view/ViewRootImpl.java

    158     static final ArrayList<ComponentCallbacks> sConfigCallbacks = new ArrayList();

Cross Reference: /frameworks/base/core/java/android/view/ViewRootImpl.java

   3302     void updateConfiguration(Configuration config, boolean force) {
   3303         if (DEBUG_CONFIGURATION) Log.v(mTag,
   3304                 "Applying new config to window "
   3305                 + mWindowAttributes.getTitle()
   3306                 + ": " + config);
   3307 
   3308         CompatibilityInfo ci = mDisplay.getDisplayAdjustments().getCompatibilityInfo();
   3309         if (!ci.equals(CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO)) {
   3310             config = new Configuration(config);
   3311             ci.applyToConfiguration(mNoncompatDensity, config);
   3312         }
   3313 
   3314         synchronized (sConfigCallbacks) {
   3315             for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
   3316                 sConfigCallbacks.get(i).onConfigurationChanged(config); <=========================
   3317             }
   3318         }
   3319         if (mView != null) {
   3320             // At this point the resources have been updated to
   3321             // have the most recent config, whatever that is.  Use
   3322             // the one in them which may be newer.
   3323             final Resources localResources = mView.getResources();
   3324             config = localResources.getConfiguration();
   3325             if (force || mLastConfiguration.diff(config) != 0) {
   3326                 // Update the display with new DisplayAdjustments.
   3327                 mDisplay = ResourcesManager.getInstance().getAdjustedDisplay(
   3328                         mDisplay.getDisplayId(), localResources.getDisplayAdjustments());
   3329 
   3330                 final int lastLayoutDirection = mLastConfiguration.getLayoutDirection();
   3331                 final int currentLayoutDirection = config.getLayoutDirection();
   3332                 mLastConfiguration.setTo(config);
   3333                 if (lastLayoutDirection != currentLayoutDirection &&
   3334                         mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) {
   3335                     mView.setLayoutDirection(currentLayoutDirection);
   3336                 }
   3337                 mView.dispatchConfigurationChanged(config);
   3338             }
   3339         }
   3340     }

Cross Reference: /frameworks/base/core/java/android/app/Application.java

    109     @CallSuper
    110     public void onConfigurationChanged(Configuration newConfig) {
    111         Object[] callbacks = collectComponentCallbacks();
    112         if (callbacks != null) {
    113             for (int i=0; i<callbacks.length; i++) {
    114                 ((ComponentCallbacks)callbacks[i]).onConfigurationChanged(newConfig);
    115             }
    116         }
    117     }