Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Feat: Added custom fonts and removed icon font toggle.
Browse files Browse the repository at this point in the history
  • Loading branch information
CreativeCodeCat committed Jul 7, 2024
1 parent bd86afe commit 3e57981
Show file tree
Hide file tree
Showing 28 changed files with 344 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.github.droidworksstudio.mlauncher.data

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Typeface
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.core.content.res.ResourcesCompat
import com.github.droidworksstudio.mlauncher.R
import java.util.Locale

Expand Down Expand Up @@ -485,6 +487,65 @@ object Constants {
}
}

enum class Fonts : EnumOption {
System,
Bitter,
Dotness,
DroidSans,
GreatVibes,
Lato,
Lobster,
Merriweather,
Montserrat,
OpenSans,
Pacifico,
Quicksand,
Raleway,
Roboto,
SourceCodePro;

fun getFont(context: Context): Typeface? {
return when (this) {
System -> Typeface.DEFAULT
Bitter -> ResourcesCompat.getFont(context, R.font.bitter)
Dotness -> ResourcesCompat.getFont(context, R.font.dotness)
DroidSans -> ResourcesCompat.getFont(context, R.font.open_sans)
GreatVibes -> ResourcesCompat.getFont(context, R.font.great_vibes)
Lato -> ResourcesCompat.getFont(context, R.font.lato)
Lobster -> ResourcesCompat.getFont(context, R.font.lobster)
Merriweather -> ResourcesCompat.getFont(context, R.font.merriweather)
Montserrat -> ResourcesCompat.getFont(context, R.font.montserrat)
OpenSans -> ResourcesCompat.getFont(context, R.font.open_sans)
Pacifico -> ResourcesCompat.getFont(context, R.font.pacifico)
Quicksand -> ResourcesCompat.getFont(context, R.font.quicksand)
Raleway -> ResourcesCompat.getFont(context, R.font.raleway)
Roboto -> ResourcesCompat.getFont(context, R.font.roboto)
SourceCodePro -> ResourcesCompat.getFont(context, R.font.source_code_pro)
}
}

@Composable
override fun string(): String {
return when (this) {
System -> stringResource(R.string.settings_font_system)
Bitter -> stringResource(R.string.settings_font_bitter)
Dotness -> stringResource(R.string.settings_font_dotness)
DroidSans -> stringResource(R.string.settings_font_droidsans)
GreatVibes -> stringResource(R.string.settings_font_greatvibes)
Lato -> stringResource(R.string.settings_font_lato)
Lobster -> stringResource(R.string.settings_font_lobster)
Merriweather -> stringResource(R.string.settings_font_merriweather)
Montserrat -> stringResource(R.string.settings_font_montserrat)
OpenSans -> stringResource(R.string.settings_font_opensans)
Pacifico -> stringResource(R.string.settings_font_pacifico)
Quicksand -> stringResource(R.string.settings_font_quicksand)
Raleway -> stringResource(R.string.settings_font_raleway)
Roboto -> stringResource(R.string.settings_font_roboto)
SourceCodePro -> stringResource(R.string.settings_font_sourcecodepro)
}
}
}

const val URL_DUCK_SEARCH = "https://duckduckgo.com/?q="
const val URL_GOOGLE_SEARCH = "https://google.com/search?q="
const val URL_YAHOO_SEARCH = "https://search.yahoo.com/search?p="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ private const val HIDDEN_APPS = "HIDDEN_APPS"
private const val HIDDEN_APPS_DISPLAYED = "HIDDEN_APPS_DISPLAYED"
private const val SEARCH_ENGINE = "SEARCH_ENGINE"

private const val LAUNCHER_FONT = "LAUNCHER_FONT"

private const val APP_NAME = "APP_NAME"
private const val APP_PACKAGE = "APP_PACKAGE"
private const val APP_USER = "APP_USER"
Expand All @@ -77,7 +79,6 @@ private const val CLICK_CLOCK = "CLICK_CLOCK"
private const val CLICK_USAGE = "CLICK_USAGE"
private const val CLICK_DATE = "CLICK_DATE"
private const val DOUBLE_TAP = "DOUBLE_TAP"
private const val CUSTOM_FONT = "CUSTOM_FONT"
private const val ALL_APPS_TEXT = "ALL_APPS_TEXT"

private const val APP_SIZE_TEXT = "APP_SIZE_TEXT"
Expand Down Expand Up @@ -246,10 +247,6 @@ class Prefs(val context: Context) {
get() = prefs.getBoolean(SETTINGS_LOCKED, false)
set(value) = prefs.edit().putBoolean(SETTINGS_LOCKED, value).apply()

var useCustomIconFont: Boolean
get() = prefs.getBoolean(CUSTOM_FONT, false)
set(value) = prefs.edit().putBoolean(CUSTOM_FONT, value).apply()

var useAllAppsText: Boolean
get() = prefs.getBoolean(ALL_APPS_TEXT, true)
set(value) = prefs.edit().putBoolean(ALL_APPS_TEXT, value).apply()
Expand Down Expand Up @@ -384,6 +381,21 @@ class Prefs(val context: Context) {
}
set(value) = prefs.edit().putString(SEARCH_ENGINE, value.name).apply()

var launcherFont: Constants.Fonts
get() {
return try {
Constants.Fonts.valueOf(
prefs.getString(
LAUNCHER_FONT,
Constants.Fonts.System.name
).toString()
)
} catch (_: Exception) {
Constants.Fonts.System
}
}
set(value) = prefs.edit().putString(LAUNCHER_FONT, value.name).apply()

var hiddenApps: MutableSet<String>
get() = prefs.getStringSet(HIDDEN_APPS, mutableSetOf()) as MutableSet<String>
set(value) = prefs.edit().putStringSet(HIDDEN_APPS, value).apply()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.droidworksstudio.mlauncher.helper

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper

object AppReloader {
fun restartApp(context: Context) {
val packageManager = context.packageManager
val intent = packageManager.getLaunchIntentForPackage(context.packageName)
val componentName = intent?.component
val mainIntent = Intent.makeRestartActivityTask(componentName)

// Delay the restart slightly to ensure all current activities are finished
Handler(Looper.getMainLooper()).post {
context.startActivity(mainIntent)
Runtime.getRuntime().exit(0)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.github.droidworksstudio.mlauncher

import android.app.Application
import android.content.Context
import android.graphics.Typeface
import android.util.Log
import com.github.droidworksstudio.mlauncher.data.Prefs
import org.acra.ReportField
import org.acra.config.dialog
Expand All @@ -15,7 +18,9 @@ class Mlauncher : Application() {
super.onCreate()

// Initialize prefs here
prefs = Prefs(this)
prefs = Prefs(applicationContext)

setCustomFont(applicationContext)

val pkgName = getString(R.string.app_name)
val pkgVersion = this.packageManager.getPackageInfo(
Expand Down Expand Up @@ -64,4 +69,32 @@ class Mlauncher : Application() {
}
}
}


private fun setCustomFont(context: Context) {
// Load the custom font from resources
val customFont = prefs.launcherFont.getFont(context)

// Apply the custom font to different font families
if (customFont != null) {
TypefaceUtil.setDefaultFont("DEFAULT", customFont)
TypefaceUtil.setDefaultFont("MONOSPACE", customFont)
TypefaceUtil.setDefaultFont("SERIF", customFont)
TypefaceUtil.setDefaultFont("SANS_SERIF", customFont)
}
}
}

object TypefaceUtil {

fun setDefaultFont(staticTypefaceFieldName: String, fontAssetName: Typeface) {
Log.e("setDefaultFont", "$staticTypefaceFieldName | $fontAssetName")
try {
val staticField = Typeface::class.java.getDeclaredField(staticTypefaceFieldName)
staticField.isAccessible = true
staticField.set(null, fontAssetName)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import android.view.inputmethod.EditorInfo
import android.widget.*
import androidx.annotation.RequiresApi
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.RecyclerView
import com.github.droidworksstudio.fuzzywuzzy.FuzzyFinder
Expand Down Expand Up @@ -55,10 +54,6 @@ class AppDrawerAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
binding = AdapterAppDrawerBinding.inflate(LayoutInflater.from(parent.context), parent, false)
prefs = Prefs(parent.context)
if (prefs.useCustomIconFont) {
val typeface = ResourcesCompat.getFont(parent.context, R.font.roboto)
binding.appTitle.typeface = typeface
}
if (prefs.followAccentColors) {
val fontColor = getHexFontColor(parent.context, prefs)
binding.appTitle.setTextColor(fontColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.SearchView
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
Expand Down Expand Up @@ -129,10 +128,6 @@ class AppDrawerFragment : Fragment() {
val searchTextView = binding.search.findViewById<TextView>(R.id.search_src_text)
if (searchTextView != null) searchTextView.gravity = gravity

if (prefs.useCustomIconFont) {
val typeface = ResourcesCompat.getFont(requireActivity(), R.font.roboto)
searchTextView.typeface = typeface
}
val textSize = prefs.appSize.toFloat()
searchTextView.textSize = textSize

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
override fun onStart() {
super.onStart()
if (prefs.showStatusBar) showStatusBar(requireActivity()) else hideStatusBar(requireActivity())
val typeface = ResourcesCompat.getFont(requireActivity(), R.font.roboto)

batteryReceiver = BatteryReceiver()
/* register battery changes */
Expand Down Expand Up @@ -148,21 +147,18 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
binding.date.textSize = prefs.dateSize.toFloat()
binding.batteryText.textSize = prefs.batterySize.toFloat()
binding.homeScreenPager.textSize = prefs.appSize.toFloat()
binding.batteryIcon.textSize = prefs.batterySize.toFloat()

if(prefs.showBatteryIcon) {
binding.batteryIcon.visibility = View.VISIBLE
val typeface = ResourcesCompat.getFont(requireActivity(), R.font.roboto)
binding.batteryIcon.typeface = typeface
binding.batteryIcon.textSize = prefs.batterySize.toFloat()
}

if (prefs.useCustomIconFont) {
binding.clock.typeface = typeface
binding.date.typeface = typeface
binding.batteryText.typeface = typeface
binding.setTotalScreenTime.typeface = typeface
binding.setDefaultLauncher.typeface = typeface
if (prefs.showBattery) {
binding.batteryLayout.visibility = View.VISIBLE
}
binding.homeScreenPager.typeface = typeface

binding.mainLayout.setBackgroundColor(colors.background(requireContext(), prefs))
if (prefs.followAccentColors) {
val fontColor = getHexFontColor(requireContext(), prefs)
Expand Down Expand Up @@ -743,10 +739,6 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
}
val padding: Int = prefs.textPaddingSize
setPadding(0, padding, 0, padding)
if (prefs.useCustomIconFont) {
val typeface = ResourcesCompat.getFont(requireActivity(), R.font.roboto)
typeface?.let { setTypeface(it) }
}
if (prefs.followAccentColors) {
val fontColor = getHexFontColor(requireContext(), prefs)
setTextColor(fontColor)
Expand All @@ -772,10 +764,6 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
}
val padding: Int = prefs.textPaddingSize
setPadding(0, padding, 0, padding)
if (prefs.useCustomIconFont) {
val typeface = ResourcesCompat.getFont(requireActivity(), R.font.roboto)
typeface?.let { setTypeface(it) }
}
if (prefs.followAccentColors) {
val fontColor = getHexFontColor(requireContext(), prefs)
setTextColor(fontColor)
Expand Down Expand Up @@ -851,10 +839,6 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
isFocusableInTouchMode = true
val padding: Int = prefs.textPaddingSize
setPadding(0, padding, 0, padding)
if (prefs.useCustomIconFont) {
val typeface = ResourcesCompat.getFont(requireActivity(), R.font.roboto)
typeface?.let { setTypeface(it) }
}
if (prefs.followAccentColors) {
val fontColor = getHexFontColor(requireContext(), prefs)
setTextColor(fontColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.children
import androidx.core.view.size
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -192,8 +191,6 @@ class ReorderHomeAppsFragment : Fragment() {
}
val padding: Int = prefs.textPaddingSize
view.setPadding(0, padding, 0, padding)
val typeface = ResourcesCompat.getFont(requireActivity(), R.font.roboto)
typeface.also { view.typeface = it }
binding.pageName.text = getString(R.string.reorder_apps)
binding.pageName.textSize = prefs.appSize * 1.5f

Expand Down
Loading

0 comments on commit 3e57981

Please sign in to comment.