制作一个更改 Galaxy 手机设备字体的应用程序

问题描述 投票:0回答:2

我想制作一个应用程序,可以在安装我的应用程序后更改 Galaxy 手机中的系统字体样式,用户可以从“设置”>“我的设备”>“字体样式”更改字体,并从列表中进行选择,而无需 root 权限。我在 true type 文件中有自己的字体样式。

曼尼斯特

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="2" android:versionName="1.1" package="com.monotype.android.font.presentltroman" platformBuildVersionCode="23" platformBuildVersionName="6.0-2438415">
    <uses-sdk android:minSdkVersion="7" />
    <application android:label="@string/app_name" android:icon="@drawable/icon">
        <provider android:name=".FontContentProvider" android:authorities="com.example.myfont" />
        <support-screens android:largeScreens="true" android:xlargeScreens="true" />
    </application>
</manifest>

只有一项活动

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import java.io.FileNotFoundException;

public class FontContentProvider extends ContentProvider {
    private static final UriMatcher uriMatcher = new UriMatcher(-1);

    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        return null;
    }

    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
        String file_name = uri.getPath();
        if (file_name == null) {
            throw new FileNotFoundException();
        }
        if (file_name.startsWith("/")) {
            file_name = file_name.substring(1);
        }
        AssetFileDescriptor ad = null;
        try {
            ad = getContext().getAssets().openFd(file_name);
        } catch (Exception e) {
            Log.v("CPFontTest", "cp - openAssetFile EXCEPTION");
        }
        return ad;
    }

    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    public String getType(Uri uri) {
        AssetManager am = getContext().getAssets();
        StringBuilder xmlfileStringBuilder = new StringBuilder();
        try {
            for (String s : am.list("xml")) {
                xmlfileStringBuilder.append(s + "\n");
            }
            return xmlfileStringBuilder.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    public boolean onCreate() {
        return true;
    }

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        return null;
    }

    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

    static {
        uriMatcher.addURI(".fontcontentprovider", "fonts", 1);
    }
}
android android-studio fonts font-face font-style
2个回答
2
投票

TL;DR:要创建 FlipFont,您需要在资源中提供有效的字体文件和 XML 文件。 APK 需要使用 Monotype 的私有密钥库进行签名,这是不可能的。


三星等厂商使用Monotype的FlipFont无需root即可更改系统字体。三星和 Monotype Imaging Inc. 希望您付费更换字体,并共同努力阻止安装免费字体。

您不需要任何代码来为 FlipFont 创建字体,因此您可以删除

FontContentProvider.java

要创建与 FlipFont 配合使用的 APK,您需要以下 3 件事:

1)

assets/fonts/ReplaceWithFontName.ttf

中的有效字体文件

2)

assets/xml/ReplaceWithFontName.xml

中的 XML

3) 使用 Monotype 的 private 密钥库构建并签署应用程序。

资产的 XML 示例:

<?xml version="1.0" encoding="utf-8"?>
<font displayname="ReplaceWithFontName">
  <sans>
    <file>
      <filename>ReplaceWithFontName.ttf</filename>
      <droidname>DroidSans.ttf</droidname>
    </file>
    <file>
      <filename>ReplaceWithFontName.ttf</filename>
      <droidname>DroidSans-Bold.ttf</droidname>
    </file>
  </sans>
</font>

现在,再看看#3。您需要使用 Monotype 的密钥库对 APK 进行签名。您无权访问此内容。因此,您的 APK 将无法在三星设备上运行。

以下方法是从三星设备上的“设置”应用程序反编译的,用于检查 FlipFont APK 是否使用正确的密钥进行签名:

protected boolean checkFont(String apkname) {
  if (DEBUG) {
    Log.secD("FlipFont", "checkFont - checking apkname" + apkname);
  }
  if ("com.monotype.android.font.foundation".equals(apkname)) {
    return false;
  }
  PackageManager pm = this.mContext.getPackageManager();
  for (int i = 0; i < apkNameList.length; i++) {
    if (apkname != null) {
      if (apkname.equals(apkNameList[i])) {
        this.isCheckPlatformSignatures = pm.checkSignatures("android", apkNameList[i]) == 0;
        this.isCheckReleaseSignatures = Utils.isSignatureMatch(this.mContext, apkNameList[i]);
        Log.i("FontPreviewTablet", "apkname : " + apkname + ", isCheckPlatformSignatures : " + this.isCheckPlatformSignatures + ", isCheckReleaseSignatures : " + this.isCheckReleaseSignatures);
        if (!(this.isCheckPlatformSignatures || this.isCheckReleaseSignatures)) {
          if (apkname.equals("")) {
          }
        }
        return false;
      }
      continue;
    }
  }
  if (DEBUG) {
    Log.secD("FlipFont", "checkFont - check if valid certificate");
  }
  PackageInfo packageInfo = null;
  try {
    packageInfo = this.mFontListAdapter.mPackageManager.getPackageInfo(apkname, 64);
  } catch (Exception e) {
  }
  if (packageInfo != null) {
    Signature[] signatures = packageInfo.signatures;
    byte[] cert = signatures[0].toByteArray();
    try {
      MessageDigest md = MessageDigest.getInstance("SHA");
      md.update(signatures[0].toByteArray());
      if ("T84drf8v3ZMOLvt2SFG/K7ODXgI=".equals(Base64.encodeToString(md.digest(), 0).trim())) {
        if (DEBUG) {
          Log.v("FlipFont", "**Signature is correct**");
        }
        return false;
      }
      if (DEBUG) {
        Log.v("FlipFont", "**Signature is incorrect**");
      }
      return true;
    } catch (Exception e2) {
      e2.printStackTrace();
      InputStream input = new ByteArrayInputStream(cert);
      CertificateFactory cf = null;
      try {
        cf = CertificateFactory.getInstance("X509");
      } catch (CertificateException e3) {
        e3.printStackTrace();
      }
      X509Certificate c = null;
      try {
        c = (X509Certificate) cf.generateCertificate(input);
      } catch (CertificateException e32) {
        e32.printStackTrace();
      }
      if (DEBUG) {
        Log.secD("Example", "APK name: " + apkname);
        if (c != null) {
          Log.secD("Example", "Certificate for: " + c.getSubjectDN());
          Log.secD("Example", "Certificate issued by: " + c.getIssuerDN());
          Log.secD("Example", "The certificate is valid from " + c.getNotBefore() + " to " + c.getNotAfter());
          Log.secD("Example", "Certificate SN# " + c.getSerialNumber());
          Log.secD("Example", "Generated with " + c.getSigAlgName());
        }
      }
      String certIssuedByString = "CN=Ed Platz, OU=Display Imaging, O=Monotype Imanging Inc., L=Woburn, ST=MA, C=US";
      if (c != null && certIssuedByString.equals(c.getIssuerDN().toString())) {
        if (DEBUG) {
          Log.secD("FlipFont", "**Certificate data is correct**");
        }
        return false;
      }
    }
  }
  return true;
}

如果您查看上述方法,您会发现,如果 APK 的包名称为“com.monotype.android.font.foundation”,则不会检查 APK 签名。


1
投票

我不确定是否可以在不生根的情况下更改系统字体类型,但您可以更改系统字体比例。您应该为此使用 FONT_SCALE 字符串和浮点值。下面是示例代码片段。

Settings.System.putFloat(getBaseContext().getContentResolver(),
        Settings.System.FONT_SCALE, (float) 1.0);

请注意,您应该将必要的 WRITE_SETTINGS 权限添加到您的 Android 清单文件中。

您可以在此处

找到其余文档
© www.soinside.com 2019 - 2024. All rights reserved.