121 lines
3.8 KiB
Java
121 lines
3.8 KiB
Java
package androidx.appcompat.view;
|
|
|
|
import android.content.Context;
|
|
import android.content.ContextWrapper;
|
|
import android.content.res.AssetManager;
|
|
import android.content.res.Configuration;
|
|
import android.content.res.Resources;
|
|
import android.view.LayoutInflater;
|
|
import androidx.annotation.StyleRes;
|
|
import androidx.appcompat.R;
|
|
public class ContextThemeWrapper extends ContextWrapper {
|
|
private LayoutInflater mInflater;
|
|
private Configuration mOverrideConfiguration;
|
|
private Resources mResources;
|
|
private Resources.Theme mTheme;
|
|
private int mThemeResource;
|
|
|
|
public ContextThemeWrapper() {
|
|
super(null);
|
|
}
|
|
|
|
public ContextThemeWrapper(Context context, @StyleRes int i) {
|
|
super(context);
|
|
this.mThemeResource = i;
|
|
}
|
|
|
|
public ContextThemeWrapper(Context context, Resources.Theme theme) {
|
|
super(context);
|
|
this.mTheme = theme;
|
|
}
|
|
|
|
private Resources getResourcesInternal() {
|
|
if (this.mResources == null) {
|
|
Configuration configuration = this.mOverrideConfiguration;
|
|
if (configuration == null) {
|
|
this.mResources = super.getResources();
|
|
} else {
|
|
this.mResources = createConfigurationContext(configuration).getResources();
|
|
}
|
|
}
|
|
return this.mResources;
|
|
}
|
|
|
|
private void initializeTheme() {
|
|
boolean z2 = this.mTheme == null;
|
|
if (z2) {
|
|
this.mTheme = getResources().newTheme();
|
|
Resources.Theme theme = getBaseContext().getTheme();
|
|
if (theme != null) {
|
|
this.mTheme.setTo(theme);
|
|
}
|
|
}
|
|
onApplyThemeResource(this.mTheme, this.mThemeResource, z2);
|
|
}
|
|
|
|
public void applyOverrideConfiguration(Configuration configuration) {
|
|
if (this.mResources != null) {
|
|
throw new IllegalStateException("getResources() or getAssets() has already been called");
|
|
} else if (this.mOverrideConfiguration == null) {
|
|
this.mOverrideConfiguration = new Configuration(configuration);
|
|
} else {
|
|
throw new IllegalStateException("Override configuration has already been set");
|
|
}
|
|
}
|
|
|
|
@Override // android.content.ContextWrapper
|
|
public void attachBaseContext(Context context) {
|
|
super.attachBaseContext(context);
|
|
}
|
|
|
|
@Override // android.content.ContextWrapper, android.content.Context
|
|
public AssetManager getAssets() {
|
|
return getResources().getAssets();
|
|
}
|
|
|
|
@Override // android.content.ContextWrapper, android.content.Context
|
|
public Resources getResources() {
|
|
return getResourcesInternal();
|
|
}
|
|
|
|
@Override // android.content.ContextWrapper, android.content.Context
|
|
public Object getSystemService(String str) {
|
|
if (!"layout_inflater".equals(str)) {
|
|
return getBaseContext().getSystemService(str);
|
|
}
|
|
if (this.mInflater == null) {
|
|
this.mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this);
|
|
}
|
|
return this.mInflater;
|
|
}
|
|
|
|
@Override // android.content.ContextWrapper, android.content.Context
|
|
public Resources.Theme getTheme() {
|
|
Resources.Theme theme = this.mTheme;
|
|
if (theme != null) {
|
|
return theme;
|
|
}
|
|
if (this.mThemeResource == 0) {
|
|
this.mThemeResource = R.style.Theme_AppCompat_Light;
|
|
}
|
|
initializeTheme();
|
|
return this.mTheme;
|
|
}
|
|
|
|
public int getThemeResId() {
|
|
return this.mThemeResource;
|
|
}
|
|
|
|
public void onApplyThemeResource(Resources.Theme theme, int i, boolean z2) {
|
|
theme.applyStyle(i, true);
|
|
}
|
|
|
|
@Override // android.content.ContextWrapper, android.content.Context
|
|
public void setTheme(int i) {
|
|
if (this.mThemeResource != i) {
|
|
this.mThemeResource = i;
|
|
initializeTheme();
|
|
}
|
|
}
|
|
}
|