|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 AxionOS |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.rising.settings.fragments.ui.fonts |
| 17 | + |
| 18 | +import android.content.Context |
| 19 | +import android.graphics.Typeface |
| 20 | +import android.graphics.fonts.FontFamilyUpdateRequest |
| 21 | +import android.graphics.fonts.FontFileUpdateRequest |
| 22 | +import android.graphics.fonts.FontFileUtil |
| 23 | +import android.graphics.fonts.FontManager |
| 24 | +import android.graphics.fonts.FontStyle |
| 25 | +import android.net.Uri |
| 26 | +import android.os.FileUtils |
| 27 | +import android.os.ParcelFileDescriptor |
| 28 | +import android.os.ServiceManager |
| 29 | +import android.os.UserHandle |
| 30 | +import android.provider.Settings |
| 31 | +import android.util.Log |
| 32 | +import android.widget.Toast |
| 33 | +import com.android.internal.statusbar.IStatusBarService |
| 34 | +import com.android.settings.R |
| 35 | +import kotlinx.coroutines.Dispatchers |
| 36 | +import kotlinx.coroutines.withContext |
| 37 | +import org.json.JSONObject |
| 38 | +import java.io.File |
| 39 | +import java.io.FileInputStream |
| 40 | +import java.nio.channels.FileChannel |
| 41 | + |
| 42 | +class ExternalFontInstaller(private val context: Context) { |
| 43 | + |
| 44 | + companion object { |
| 45 | + private const val TAG = "ExternalFontInstaller" |
| 46 | + private const val CUSTOM_FONT_FILE = "cust_font.ttf" |
| 47 | + private const val TEMP_PREVIEW_FONT = "preview_font.ttf" |
| 48 | + private const val OVERLAY_CATEGORY_FONT = "android.theme.customization.font" |
| 49 | + const val DEFAULT_FONT_FAMILY = "Rookery-Regular" |
| 50 | + private const val DEFAULT_FONT_OVERLAY = "com.android.theme.font.rookery" |
| 51 | + |
| 52 | + fun rebootDevice() { |
| 53 | + runCatching { |
| 54 | + val binder = ServiceManager.getService("statusbar") |
| 55 | + val statusBarService = IStatusBarService.Stub.asInterface(binder) |
| 56 | + statusBarService.reboot(false, "system_font_change") |
| 57 | + }.onFailure { |
| 58 | + Log.e(TAG, "Failed to reboot device via statusbar service", it) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private val fontManager: FontManager = context.getSystemService(FontManager::class.java) |
| 64 | + |
| 65 | + suspend fun loadTypefaceFromUri(uri: Uri): Typeface? = withContext(Dispatchers.IO) { |
| 66 | + val tempFile = copyUriToCache(uri, TEMP_PREVIEW_FONT) ?: return@withContext null |
| 67 | + val postScriptName = extractPostScriptName(tempFile) ?: run { |
| 68 | + tempFile.delete() |
| 69 | + return@withContext null |
| 70 | + } |
| 71 | + return@withContext Typeface.createFromFile(tempFile) |
| 72 | + } |
| 73 | + |
| 74 | + suspend fun installFontFromUri(uri: Uri): String? { |
| 75 | + val fontFile = copyUriToCache(uri, CUSTOM_FONT_FILE) ?: run { |
| 76 | + showToast(R.string.toast_failed_apply_font) |
| 77 | + return null |
| 78 | + } |
| 79 | + |
| 80 | + val postScriptName = extractPostScriptName(fontFile) ?: run { |
| 81 | + showToast(R.string.toast_invalid_font_file) |
| 82 | + fontFile.delete() |
| 83 | + return null |
| 84 | + } |
| 85 | + |
| 86 | + if (!applyFontToSystem(fontFile, postScriptName)) { |
| 87 | + fontFile.delete() |
| 88 | + return null |
| 89 | + } |
| 90 | + |
| 91 | + updateThemeOverlays() |
| 92 | + cleanupPreviewFont() |
| 93 | + return postScriptName |
| 94 | + } |
| 95 | + |
| 96 | + private suspend fun copyUriToCache(uri: Uri, fileName: String): File? = withContext(Dispatchers.IO) { |
| 97 | + try { |
| 98 | + val cacheFile = File(context.cacheDir, fileName) |
| 99 | + context.contentResolver.openFileDescriptor(uri, "r")?.use { pfd -> |
| 100 | + FileInputStream(pfd.fileDescriptor).use { input -> |
| 101 | + cacheFile.outputStream().use { output -> |
| 102 | + FileUtils.copy(input, output) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + cacheFile |
| 107 | + } catch (e: Exception) { |
| 108 | + Log.e(TAG, "Failed to copy URI to cache", e) |
| 109 | + null |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private fun extractPostScriptName(fontFile: File): String? = |
| 114 | + FileInputStream(fontFile).use { fis -> |
| 115 | + val buffer = fis.channel.map(FileChannel.MapMode.READ_ONLY, 0, fis.channel.size()) |
| 116 | + FontFileUtil.getPostScriptName(buffer, 0) |
| 117 | + } |
| 118 | + |
| 119 | + private fun applyFontToSystem(fontFile: File, postScriptName: String): Boolean { |
| 120 | + return runCatching { |
| 121 | + val pfd = ParcelFileDescriptor.open(fontFile, ParcelFileDescriptor.MODE_READ_ONLY) |
| 122 | + val fontFileUpdateRequest = FontFileUpdateRequest(pfd, ByteArray(0)) |
| 123 | + |
| 124 | + val fontRegular = FontFamilyUpdateRequest.Font.Builder( |
| 125 | + postScriptName, |
| 126 | + FontStyle() |
| 127 | + ).build() |
| 128 | + |
| 129 | + val familyRegular = FontFamilyUpdateRequest.FontFamily.Builder( |
| 130 | + DEFAULT_FONT_FAMILY, |
| 131 | + listOf(fontRegular) |
| 132 | + ).build() |
| 133 | + |
| 134 | + val updateRequest = FontFamilyUpdateRequest.Builder() |
| 135 | + .addFontFileUpdateRequest(fontFileUpdateRequest) |
| 136 | + .addFontFamily(familyRegular) |
| 137 | + .build() |
| 138 | + |
| 139 | + val result = fontManager.updateFontFamily( |
| 140 | + updateRequest, |
| 141 | + fontManager.fontConfig.configVersion |
| 142 | + ) |
| 143 | + |
| 144 | + if (result != FontManager.RESULT_SUCCESS) { |
| 145 | + showToast(R.string.toast_failed_apply_font) |
| 146 | + false |
| 147 | + } else true |
| 148 | + }.getOrElse { |
| 149 | + Log.e(TAG, "Failed to update system font", it) |
| 150 | + false |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private fun updateThemeOverlays() { |
| 155 | + val resolver = context.contentResolver |
| 156 | + val userId = UserHandle.myUserId() |
| 157 | + |
| 158 | + val json = runCatching { |
| 159 | + val current = Settings.Secure.getStringForUser( |
| 160 | + resolver, |
| 161 | + Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES, |
| 162 | + userId |
| 163 | + ) |
| 164 | + if (current.isNullOrEmpty()) JSONObject() else JSONObject(current) |
| 165 | + }.getOrElse { JSONObject() } |
| 166 | + |
| 167 | + runCatching { |
| 168 | + if (json.has(OVERLAY_CATEGORY_FONT)) json.remove(OVERLAY_CATEGORY_FONT) |
| 169 | + json.put(OVERLAY_CATEGORY_FONT, DEFAULT_FONT_OVERLAY) |
| 170 | + |
| 171 | + Settings.Secure.putStringForUser( |
| 172 | + resolver, |
| 173 | + Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES, |
| 174 | + json.toString(), |
| 175 | + userId |
| 176 | + ) |
| 177 | + }.onFailure { Log.e(TAG, "Failed to persist custom font overlay", it) } |
| 178 | + } |
| 179 | + |
| 180 | + private fun showToast(resId: Int) { |
| 181 | + Toast.makeText(context, resId, Toast.LENGTH_SHORT).show() |
| 182 | + } |
| 183 | + |
| 184 | + fun resetFontUpdates() { |
| 185 | + fontManager.clearUpdates() |
| 186 | + cleanupPreviewFont() |
| 187 | + } |
| 188 | + |
| 189 | + private fun cleanupPreviewFont() { |
| 190 | + try { |
| 191 | + val previewFile = File(context.cacheDir, TEMP_PREVIEW_FONT) |
| 192 | + if (previewFile.exists()) previewFile.delete() |
| 193 | + } catch (e: Exception) { |
| 194 | + Log.e(TAG, "Failed to cleanup preview font", e) |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments