Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Binary file added app/src/main/assets/font/default.ttf
Binary file not shown.
Binary file added app/src/main/assets/font/open_sans_medium.ttf
Binary file not shown.
Binary file added app/src/main/assets/font/open_sans_medium2.ttf
Binary file not shown.
51 changes: 51 additions & 0 deletions app/src/main/java/com/darkempire78/opencalculator/FontManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.darkempire78.opencalculator

import android.app.Activity
import android.app.Application
import android.content.Context
import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.TextView

class FontManager(context: Context) {
val context: Context = context.applicationContext
fun applyFont(application: Application) {
val fontName = MyPreferences(context).appFont
if (fontName != null && fontName != "Default") {
val typeface = Typeface.createFromAsset(context.assets, "font/$fontName.ttf")
// Apply the typeface to all activities
application.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
activity.findViewById<View>(android.R.id.content).viewTreeObserver
.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val rootView = activity.findViewById<ViewGroup>(android.R.id.content)
applyFontToViewGroup(rootView, typeface)
rootView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
override fun onActivityStarted(activity: Activity) {}
override fun onActivityResumed(activity: Activity) {}
override fun onActivityPaused(activity: Activity) {}
override fun onActivityStopped(activity: Activity) {}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
override fun onActivityDestroyed(activity: Activity) {}
})
}
}

private fun applyFontToViewGroup(viewGroup: ViewGroup, typeface: Typeface) {
for (i in 0 until viewGroup.childCount) {
val child = viewGroup.getChildAt(i)
if (child is ViewGroup) {
applyFontToViewGroup(child, typeface)
} else if (child is TextView) {
child.typeface = typeface
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.darkempire78.opencalculator

import android.content.Context
import android.graphics.Typeface
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatDelegate.*
import androidx.preference.PreferenceManager
import com.google.gson.Gson
Expand All @@ -25,6 +28,7 @@ class MyPreferences(context: Context) {
private const val KEY_SPLIT_PARENTHESIS_BUTTON = "darkempire78.opencalculator.SPLIT_PARENTHESIS_BUTTON"
private const val KEY_DELETE_HISTORY_ON_SWIPE = "darkempire78.opencalculator.DELETE_HISTORY_ELEMENT_ON_SWIPE"
private const val KEY_AUTO_SAVE_CALCULATION_WITHOUT_EQUAL_BUTTON = "darkempire78.opencalculator.AUTO_SAVE_CALCULATION_WITHOUT_EQUAL_BUTTON"
private const val KEY_APP_FONT = "darkempire78.opencalculator.APP_FONT"
}

private val preferences = PreferenceManager.getDefaultSharedPreferences(context)
Expand Down Expand Up @@ -61,6 +65,8 @@ class MyPreferences(context: Context) {

var autoSaveCalculationWithoutEqualButton = preferences.getBoolean(KEY_AUTO_SAVE_CALCULATION_WITHOUT_EQUAL_BUTTON, true)
set(value) = preferences.edit().putBoolean(KEY_AUTO_SAVE_CALCULATION_WITHOUT_EQUAL_BUTTON, value).apply()
var appFont = preferences.getString(KEY_APP_FONT, "default")
set(value) = preferences.edit().putString(KEY_APP_FONT, value).apply()


fun getHistory(): MutableList<History> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.darkempire78.opencalculator

import android.app.Activity
import android.app.Application
import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.TextView
import androidx.appcompat.app.AppCompatDelegate.*

class OpenCalcApp : Application() {

override fun onCreate() {
super.onCreate()
FontManager(this).applyFont(this)

// if the theme is overriding the system, the first creation doesn't work properly
val forceDayNight = MyPreferences(this).forceDayNight
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package com.darkempire78.opencalculator

import android.content.Intent
import android.content.res.Resources.Theme
import android.graphics.Typeface
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
Expand All @@ -29,9 +32,9 @@ class SettingsActivity : AppCompatActivity() {
setContentView(R.layout.settings_activity)
if (savedInstanceState == null) {
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
}
supportActionBar?.setDisplayHomeAsUpEnabled(true)

Expand All @@ -46,7 +49,6 @@ class SettingsActivity : AppCompatActivity() {
findViewById<ImageView>(R.id.settings_back_button).setOnClickListener {
finish()
}

}

class SettingsFragment : PreferenceFragmentCompat() {
Expand All @@ -55,7 +57,7 @@ class SettingsActivity : AppCompatActivity() {

val appLanguagePreference = findPreference<Preference>("darkempire78.opencalculator.APP_LANGUAGE")

// remove the app language button if you are using an Android version lower than v33 (Android 13)
// Remove the app language button if you are using an Android version lower than v33 (Android 13)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
appLanguagePreference?.isVisible = false
} else {
Expand All @@ -79,6 +81,30 @@ class SettingsActivity : AppCompatActivity() {
Themes.openDialogThemeSelector(this.requireContext())
true
}

// Font preference
val appFontPreference = findPreference<Preference>("darkempire78.opencalculator.APP_FONT")
appFontPreference?.setOnPreferenceChangeListener { _, newValue ->
applyFont(newValue as String)
true
}
}

private fun applyFont(fontName: String) {
val typeface = Typeface.createFromAsset(requireContext().assets, "font/$fontName.ttf")
val rootView = requireActivity().findViewById<ViewGroup>(android.R.id.content)
applyFontToViewGroup(rootView, typeface)
}

private fun applyFontToViewGroup(viewGroup: ViewGroup, typeface: Typeface) {
for (i in 0 until viewGroup.childCount) {
val child = viewGroup.getChildAt(i)
if (child is ViewGroup) {
applyFontToViewGroup(child, typeface)
} else if (child is TextView) {
child.typeface = typeface
}
}
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/font.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="30dp"
android:height="30dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@color/text_color"
android:pathData="M200,760L200,680L760,680L760,760L200,760ZM276,600L440,160L520,160L684,600L608,600L570,488L392,488L352,600L276,600ZM414,424L546,424L482,242L478,242L414,424Z"/>
</vector>
12 changes: 12 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,16 @@
<item>1000</item> <!-- Infinity -->
</string-array>


<!-- App Font Setting-->
<string-array name="app_font_entries">
<item>Default</item>
<item>OpenSans</item>
</string-array>

<string-array name="app_font_values">
<item>default</item>
<item>open_sans_medium</item>
</string-array>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,5 @@
<string name="require_real_number">Real number required</string>
<string name="settings_auto_save_calculation_without_equal_button">Automatically save calculations</string>
<string name="settings_auto_save_calculation_without_equal_button_desc">Automatically save calculations in history without needing to click the equal button</string>
<string name="settings_app_font">Font</string>
</resources>
10 changes: 10 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
app:summary="APP THEME"
app:icon="@drawable/theme" />

<ListPreference
app:key="darkempire78.opencalculator.APP_FONT"
app:title="@string/settings_app_font"
app:entries="@array/app_font_entries"
app:entryValues="@array/app_font_values"
app:defaultValue="Default"
app:useSimpleSummaryProvider="true"
app:singleLineTitle="false"
app:icon="@drawable/font" />

<SwitchPreferenceCompat
app:key="darkempire78.opencalculator.KEY_VIBRATION_STATUS"
app:title="@string/settings_general_vibration"
Expand Down