mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-06-02 12:52:15 +00:00
parent
c581e9601b
commit
9a2009d182
9 changed files with 94 additions and 3 deletions
|
@ -7,6 +7,7 @@ import android.content.Intent
|
|||
import android.content.SharedPreferences
|
||||
import android.content.res.Configuration
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Typeface
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
|
@ -36,12 +37,14 @@ import helium314.keyboard.latin.R
|
|||
import helium314.keyboard.latin.common.FileUtils
|
||||
import helium314.keyboard.latin.customIconNames
|
||||
import helium314.keyboard.latin.databinding.ReorderDialogItemBinding
|
||||
import helium314.keyboard.latin.utils.DeviceProtectedUtils
|
||||
import helium314.keyboard.latin.utils.ResourceUtils
|
||||
import helium314.keyboard.latin.utils.confirmDialog
|
||||
import helium314.keyboard.latin.utils.getStringResourceOrName
|
||||
import helium314.keyboard.latin.utils.infoDialog
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.File
|
||||
import java.lang.Float.max
|
||||
import java.lang.Float.min
|
||||
import java.util.*
|
||||
|
@ -86,6 +89,12 @@ class AppearanceSettingsFragment : SubScreenFragment() {
|
|||
loadImage(uri, true, true)
|
||||
}
|
||||
|
||||
private val fontFilePicker = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
||||
if (it.resultCode != Activity.RESULT_OK) return@registerForActivityResult
|
||||
val uri = it.data?.data ?: return@registerForActivityResult
|
||||
saveCustomTypeface(uri)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
addPreferencesFromResource(R.xml.prefs_screen_appearance)
|
||||
|
@ -106,6 +115,7 @@ class AppearanceSettingsFragment : SubScreenFragment() {
|
|||
}
|
||||
findPreference<Preference>("custom_background_image")?.setOnPreferenceClickListener { onClickLoadImage(false) }
|
||||
findPreference<Preference>("custom_background_image_landscape")?.setOnPreferenceClickListener { onClickLoadImage(true) }
|
||||
findPreference<Preference>("custom_font")?.setOnPreferenceClickListener { onClickCustomFont() }
|
||||
findPreference<Preference>(Settings.PREF_CUSTOM_ICON_NAMES)?.setOnPreferenceClickListener {
|
||||
if (needsReload)
|
||||
KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext())
|
||||
|
@ -389,6 +399,44 @@ class AppearanceSettingsFragment : SubScreenFragment() {
|
|||
KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext())
|
||||
}
|
||||
|
||||
private fun onClickCustomFont(): Boolean {
|
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
||||
.addCategory(Intent.CATEGORY_OPENABLE)
|
||||
.setType("*/*")
|
||||
val fontFile = Settings.getCustomFontFile(requireContext())
|
||||
if (fontFile.exists()) {
|
||||
AlertDialog.Builder(requireContext())
|
||||
.setTitle(R.string.custom_font)
|
||||
.setPositiveButton(R.string.load) { _, _ -> fontFilePicker.launch(intent) }
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.setNeutralButton(R.string.delete) { _, _ ->
|
||||
fontFile.delete()
|
||||
Settings.clearCachedTypeface()
|
||||
KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext())
|
||||
}
|
||||
.show()
|
||||
} else {
|
||||
fontFilePicker.launch(intent)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun saveCustomTypeface(uri: Uri) {
|
||||
val fontFile = Settings.getCustomFontFile(requireContext())
|
||||
val tempFile = File(DeviceProtectedUtils.getFilesDir(context), "temp_file")
|
||||
FileUtils.copyContentUriToNewFile(uri, requireContext(), tempFile)
|
||||
try {
|
||||
val typeface = Typeface.createFromFile(tempFile)
|
||||
fontFile.delete()
|
||||
tempFile.renameTo(fontFile)
|
||||
Settings.clearCachedTypeface()
|
||||
KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext())
|
||||
} catch (_: Exception) {
|
||||
infoDialog(requireContext(), R.string.file_read_error)
|
||||
tempFile.delete()
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupScalePrefs(prefKey: String, defaultValue: Float) {
|
||||
val prefs = sharedPreferences
|
||||
val pref = findPreference(prefKey) as? SeekBarDialogPreference
|
||||
|
|
|
@ -14,6 +14,7 @@ import android.content.res.Configuration;
|
|||
import android.content.res.Resources;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Typeface;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
|
@ -196,6 +197,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
|
|||
|
||||
// static cache for background images to avoid potentially slow reload on every settings reload
|
||||
private final static Drawable[] sCachedBackgroundImages = new Drawable[4];
|
||||
private static Typeface sCachedTypeface;
|
||||
private Map<String, Integer> mCustomToolbarKeyCodes = null;
|
||||
private Map<String, Integer> mCustomToolbarLongpressCodes = null;
|
||||
|
||||
|
@ -729,4 +731,25 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
|
|||
mCustomToolbarLongpressCodes = ToolbarUtilsKt.readCustomLongpressCodes(mPrefs);
|
||||
return mCustomToolbarLongpressCodes.get(key.name());
|
||||
}
|
||||
|
||||
public static File getCustomFontFile(final Context context) {
|
||||
return new File(DeviceProtectedUtils.getFilesDir(context), "custom_font");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Typeface readCustomTypeface() {
|
||||
// dammit, dann würde wenns keins gibt bei jedem zugriff gesucht -> 2 variablen nehmen? custom und hasCustom?
|
||||
// ein clear brauchen wir sowieso on theme changed (und auch triggern wenn man ne font setzt/löscht)
|
||||
// try/catch!
|
||||
if (sCachedTypeface == null) {
|
||||
try {
|
||||
sCachedTypeface = Typeface.createFromFile(getCustomFontFile(mContext));
|
||||
} catch (Exception e) { }
|
||||
}
|
||||
return sCachedTypeface;
|
||||
}
|
||||
|
||||
public static void clearCachedTypeface() {
|
||||
sCachedTypeface = null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import android.content.SharedPreferences;
|
|||
import android.content.res.Resources;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.Typeface;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.GradientDrawable;
|
||||
import android.graphics.drawable.ShapeDrawable;
|
||||
|
@ -155,11 +156,14 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
|
|||
mToolbar = findViewById(R.id.toolbar);
|
||||
mToolbarContainer = findViewById(R.id.toolbar_container);
|
||||
|
||||
final Typeface customTypeface = Settings.getInstance().readCustomTypeface();
|
||||
for (int pos = 0; pos < SuggestedWords.MAX_SUGGESTIONS; pos++) {
|
||||
final TextView word = new TextView(context, null, R.attr.suggestionWordStyle);
|
||||
word.setContentDescription(getResources().getString(R.string.spoken_empty_suggestion));
|
||||
word.setOnClickListener(this);
|
||||
word.setOnLongClickListener(this);
|
||||
if (customTypeface != null)
|
||||
word.setTypeface(customTypeface);
|
||||
colors.setBackground(word, ColorType.STRIP_BACKGROUND);
|
||||
mWordViews.add(word);
|
||||
final View divider = inflater.inflate(R.layout.suggestion_divider, null);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue