Fix font issues

This commit is contained in:
serso 2016-04-29 18:52:25 +02:00
parent 10daedc21b
commit f618f2cd44
9 changed files with 44 additions and 38 deletions

View File

@ -87,7 +87,13 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
public static void setFont(@Nonnull View view, @Nonnull Typeface newTypeface) { public static void setFont(@Nonnull View view, @Nonnull Typeface newTypeface) {
if (view instanceof TextView) { if (view instanceof TextView) {
setFont((TextView) view, newTypeface); final TextView textView = (TextView) view;
final Typeface oldTypeface = textView.getTypeface();
if (oldTypeface == newTypeface) {
return;
}
final int style = oldTypeface != null ? oldTypeface.getStyle() : Typeface.NORMAL;
textView.setTypeface(newTypeface, style);
} else if (view instanceof DirectionDragImageButton) { } else if (view instanceof DirectionDragImageButton) {
((DirectionDragImageButton) view).setTypeface(newTypeface); ((DirectionDragImageButton) view).setTypeface(newTypeface);
} }
@ -98,15 +104,6 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
return mode; return mode;
} }
public static void setFont(@Nonnull TextView view, @Nonnull Typeface newTypeface) {
final Typeface oldTypeface = view.getTypeface();
if (oldTypeface == newTypeface) {
return;
}
final int style = oldTypeface != null ? oldTypeface.getStyle() : Typeface.NORMAL;
view.setTypeface(newTypeface, style);
}
public boolean restartIfModeChanged() { public boolean restartIfModeChanged() {
final Preferences.Gui.Mode newMode = Preferences.Gui.mode.getPreference(preferences); final Preferences.Gui.Mode newMode = Preferences.Gui.mode.getPreference(preferences);
if (newMode != mode) { if (newMode != mode) {
@ -162,7 +159,7 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
} }
ButterKnife.bind(this, this); ButterKnife.bind(this, this);
fixFonts(mainView); fixFonts(mainView, typeface);
initToolbar(); initToolbar();
populateTabs(tabs); populateTabs(tabs);
tabs.onCreate(); tabs.onCreate();
@ -285,14 +282,15 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
fab.setOnClickListener(listener); fab.setOnClickListener(listener);
} }
protected void fixFonts(@Nonnull View root) { public static void fixFonts(@Nonnull View view, @Nonnull Typeface typeface) {
// some devices ship own fonts which causes issues with rendering. Let's use our own font for all text views if (view instanceof ViewGroup) {
App.processViewsOfType(root, TextView.class, new App.ViewProcessor<TextView>() { final ViewGroup group = (ViewGroup) view;
@Override for (int index = 0; index < group.getChildCount(); index++) {
public void process(@Nonnull TextView view) { fixFonts(group.getChildAt(index), typeface);
setFont(view, typeface);
} }
}); } else {
setFont(view, typeface);
}
} }
@Override @Override

View File

@ -3,6 +3,7 @@ package org.solovyev.android.calculator;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -29,6 +30,8 @@ public abstract class BaseDialogFragment extends DialogFragment implements View.
protected SharedPreferences preferences; protected SharedPreferences preferences;
@Inject @Inject
Ga ga; Ga ga;
@Inject
Typeface typeface;
@Nullable @Nullable
private Button positiveButton; private Button positiveButton;
@Nullable @Nullable
@ -57,6 +60,7 @@ public abstract class BaseDialogFragment extends DialogFragment implements View.
if (view != null) { if (view != null) {
final int spacing = context.getResources().getDimensionPixelSize(R.dimen.cpp_dialog_spacing); final int spacing = context.getResources().getDimensionPixelSize(R.dimen.cpp_dialog_spacing);
b.setView(view, spacing, spacing, spacing, spacing); b.setView(view, spacing, spacing, spacing, spacing);
BaseActivity.fixFonts(view, typeface);
} }
onPrepareDialog(b); onPrepareDialog(b);
final AlertDialog dialog = b.create(); final AlertDialog dialog = b.create();

View File

@ -1,31 +1,29 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import static android.view.Menu.NONE; import android.graphics.Typeface;
import static org.solovyev.android.calculator.App.cast;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.LayoutRes; import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.view.ContextMenu; import android.view.*;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import org.solovyev.android.calculator.ads.AdUi; import org.solovyev.android.calculator.ads.AdUi;
import org.solovyev.android.plotter.Check; import org.solovyev.android.plotter.Check;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.inject.Inject; import javax.inject.Inject;
import static android.view.Menu.NONE;
import static org.solovyev.android.calculator.App.cast;
public abstract class BaseFragment extends Fragment { public abstract class BaseFragment extends Fragment {
private final int layout; private final int layout;
@Inject @Inject
AdUi adUi; AdUi adUi;
@Inject
Typeface typeface;
protected BaseFragment(@LayoutRes int layout) { protected BaseFragment(@LayoutRes int layout) {
this.layout = layout; this.layout = layout;
@ -61,6 +59,7 @@ public abstract class BaseFragment extends Fragment {
Bundle savedInstanceState) { Bundle savedInstanceState) {
final View view = inflater.inflate(layout, container, false); final View view = inflater.inflate(layout, container, false);
adUi.onCreateView(view); adUi.onCreateView(view);
BaseActivity.fixFonts(view, typeface);
return view; return view;
} }

View File

@ -266,6 +266,7 @@ public class FloatingCalculatorView {
final Preferences.SimpleTheme resolvedTheme = theme.resolveThemeFor(appTheme); final Preferences.SimpleTheme resolvedTheme = theme.resolveThemeFor(appTheme);
this.context = new ContextThemeWrapper(context, resolvedTheme.light ? R.style.Cpp_Theme_Light : R.style.Cpp_Theme); this.context = new ContextThemeWrapper(context, resolvedTheme.light ? R.style.Cpp_Theme_Light : R.style.Cpp_Theme);
this.root = View.inflate(this.context, theme.getOnscreenLayout(appTheme), null); this.root = View.inflate(this.context, theme.getOnscreenLayout(appTheme), null);
BaseActivity.fixFonts(this.root, typeface);
final State persistedState = State.fromPrefs(myPreferences); final State persistedState = State.fromPrefs(myPreferences);
if (persistedState != null) { if (persistedState != null) {
this.state = persistedState; this.state = persistedState;
@ -357,7 +358,6 @@ public class FloatingCalculatorView {
} else { } else {
BaseKeyboardUi.adjustButton(button); BaseKeyboardUi.adjustButton(button);
} }
BaseActivity.setFont(button, typeface);
} }
final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

View File

@ -4,7 +4,6 @@ import android.app.Activity;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.text.TextUtils; import android.text.TextUtils;
@ -47,8 +46,6 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
@Inject @Inject
SharedPreferences preferences; SharedPreferences preferences;
@Inject @Inject
Typeface typeface;
@Inject
Keyboard keyboard; Keyboard keyboard;
@Inject @Inject
Editor editor; Editor editor;
@ -133,7 +130,6 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
// we call android.view.View.performHapticFeedback(int, int) from #onClick // we call android.view.View.performHapticFeedback(int, int) from #onClick
button.setHapticFeedbackEnabled(false); button.setHapticFeedbackEnabled(false);
button.setOnClickListener(this); button.setOnClickListener(this);
BaseActivity.setFont(button, typeface);
} }
protected final void prepareButton(@Nullable DirectionDragImageButton button) { protected final void prepareButton(@Nullable DirectionDragImageButton button) {

View File

@ -28,7 +28,6 @@ import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.Preferences; import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.keyboard.BaseKeyboardUi; import org.solovyev.android.calculator.keyboard.BaseKeyboardUi;
@ -73,7 +72,6 @@ public class ChooseModeWizardStep extends WizardFragment implements AdapterView.
spinner.setOnItemSelectedListener(this); spinner.setOnItemSelectedListener(this);
button = (DirectionDragButton) root.findViewById(R.id.wizard_mode_button); button = (DirectionDragButton) root.findViewById(R.id.wizard_mode_button);
BaseActivity.setFont(button, typeface);
Adjuster.adjustText(button, BaseKeyboardUi.getTextScale(getActivity())); Adjuster.adjustText(button, BaseKeyboardUi.getTextScale(getActivity()));
description = (TextView) root.findViewById(R.id.wizard_mode_description); description = (TextView) root.findViewById(R.id.wizard_mode_description);
updateDescription(mode); updateDescription(mode);

View File

@ -30,6 +30,7 @@ import android.widget.AdapterView;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.Spinner; import android.widget.Spinner;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.Preferences; import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.keyboard.BaseKeyboardUi; import org.solovyev.android.calculator.keyboard.BaseKeyboardUi;
@ -69,7 +70,7 @@ public class ChooseThemeWizardStep extends WizardFragment implements AdapterView
spinner.setOnItemSelectedListener(this); spinner.setOnItemSelectedListener(this);
preview = (FrameLayout) root.findViewById(R.id.wizard_theme_preview); preview = (FrameLayout) root.findViewById(R.id.wizard_theme_preview);
updateImage(theme); updatePreview(theme);
} }
private int findPosition(@Nonnull Preferences.Gui.Theme theme) { private int findPosition(@Nonnull Preferences.Gui.Theme theme) {
@ -82,7 +83,7 @@ public class ChooseThemeWizardStep extends WizardFragment implements AdapterView
return 0; return 0;
} }
private void updateImage(@Nonnull Preferences.Gui.Theme theme) { private void updatePreview(@Nonnull Preferences.Gui.Theme theme) {
preview.removeAllViews(); preview.removeAllViews();
final ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), theme.theme); final ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), theme.theme);
LayoutInflater.from(context).inflate(R.layout.cpp_wizard_step_choose_theme_preview, preview); LayoutInflater.from(context).inflate(R.layout.cpp_wizard_step_choose_theme_preview, preview);
@ -90,6 +91,7 @@ public class ChooseThemeWizardStep extends WizardFragment implements AdapterView
@Override @Override
public void process(@Nonnull View view) { public void process(@Nonnull View view) {
BaseKeyboardUi.adjustButton(view); BaseKeyboardUi.adjustButton(view);
BaseActivity.setFont(view, typeface);
} }
}); });
} }
@ -98,7 +100,7 @@ public class ChooseThemeWizardStep extends WizardFragment implements AdapterView
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final ThemeUi theme = adapter.getItem(position); final ThemeUi theme = adapter.getItem(position);
Preferences.Gui.theme.putPreference(preferences, theme.theme); Preferences.Gui.theme.putPreference(preferences, theme.theme);
updateImage(theme.theme); updatePreview(theme.theme);
} }
@Override @Override

View File

@ -83,7 +83,6 @@ public class DragButtonWizardStep extends WizardFragment {
return false; return false;
} }
}); });
BaseActivity.setFont(dragButton, typeface);
Adjuster.adjustText(dragButton, BaseKeyboardUi.getTextScale(getActivity())); Adjuster.adjustText(dragButton, BaseKeyboardUi.getTextScale(getActivity()));
actionTextView = (TextView) root.findViewById(R.id.wizard_dragbutton_action_textview); actionTextView = (TextView) root.findViewById(R.id.wizard_dragbutton_action_textview);
if (savedInstanceState != null) { if (savedInstanceState != null) {

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator.wizard; package org.solovyev.android.calculator.wizard;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.LayoutRes; import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
@ -10,6 +11,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import org.solovyev.android.calculator.AppComponent; import org.solovyev.android.calculator.AppComponent;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.release.ChooseThemeReleaseNoteFragment; import org.solovyev.android.calculator.release.ChooseThemeReleaseNoteFragment;
import org.solovyev.android.calculator.release.ChooseThemeReleaseNoteStep; import org.solovyev.android.calculator.release.ChooseThemeReleaseNoteStep;
@ -35,6 +37,8 @@ public abstract class WizardFragment extends Fragment implements View.OnClickLis
protected TextView prevButton; protected TextView prevButton;
@Inject @Inject
SharedPreferences preferences; SharedPreferences preferences;
@Inject
Typeface typeface;
private WizardStep step; private WizardStep step;
@Override @Override
@ -112,6 +116,12 @@ public abstract class WizardFragment extends Fragment implements View.OnClickLis
return view; return view;
} }
@Override
public void onViewCreated(View view, @android.support.annotation.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
BaseActivity.fixFonts(view, typeface);
}
protected final void setupNextButton(int textResId) { protected final void setupNextButton(int textResId) {
assert nextButton != null; assert nextButton != null;
nextButton.setText(textResId); nextButton.setText(textResId);