下面列出了android.util.DisplayMetrics#setToDefaults ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Resources getResources(PrintWriter pw) throws RemoteException {
// system resources does not contain all the device configuration, construct it manually.
Configuration config = mInterface.getConfiguration();
if (config == null) {
pw.println("Error: Activity manager has no configuration");
return null;
}
final DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
return new Resources(AssetManager.getSystem(), metrics, config);
}
/**
* Protected so that tests can override and returns something a fixed value.
*/
@VisibleForTesting
protected @NonNull DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments da) {
DisplayMetrics dm = new DisplayMetrics();
final Display display = getAdjustedDisplay(displayId, da);
if (display != null) {
display.getMetrics(dm);
} else {
dm.setToDefaults();
}
return dm;
}
/**
* Only for creating the System resources.
*/
private Resources() {
this(null);
final DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
final Configuration config = new Configuration();
config.setToDefaults();
mResourcesImpl = new ResourcesImpl(AssetManager.getSystem(), metrics, config,
new DisplayAdjustments());
}
/**
* getPackage
*
* @param parser android.content.pm.PackageParser
* @param apkFile APK文件
* @return PackageParser.Package
*/
private static PackageParser.Package getPackage(android.content.pm.PackageParser parser, File apkFile) {
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
final File sourceFile = new File(apkFile.getAbsolutePath());
android.content.pm.PackageParser.Package pkg = null;
// 适配5.0
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
Method mtd = null;
try {
mtd = android.content.pm.PackageParser.class.getDeclaredMethod("parsePackage", File.class,
int.class);
} catch (NoSuchMethodException e1) {
if (Constants.DEBUG) {
e1.printStackTrace();
}
}
if (mtd != null) {
try {
pkg = parser
.parsePackage(apkFile, PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RECEIVERS);
} catch (PackageParserException e) {
if (Constants.DEBUG) {
e.printStackTrace();
}
}
} else { // L的情况
pkg = parser.parsePackage(sourceFile, apkFile.getAbsolutePath(), metrics,
PackageManager.GET_INTENT_FILTERS | PackageManager.GET_RECEIVERS);
}
} else {
pkg = parser.parsePackage(sourceFile, apkFile.getAbsolutePath(), metrics, PackageManager.GET_INTENT_FILTERS
| PackageManager.GET_RECEIVERS);
}
return pkg;
}
/**
* 恢复displayMetrics为系统原生状态,单位pt恢复为长度单位磅
* @see #inactivate()
*
* @param context
*/
public static void restoreDensity(Context context){
context.getResources().getDisplayMetrics().setToDefaults();
DisplayMetrics metrics = getMetricsOnMiui(context.getResources());
if(metrics != null)
metrics.setToDefaults();
}
/**
* Derives an instance from a {@link Display}
*
* @param display The {@link Display} instance from which to construct
*/
public DisplayInfoAndroid(Display display) {
super();
DisplayMetrics tempMetrics = new DisplayMetrics();
display.getMetrics(tempMetrics);
this.metricsWithoutDecoration = new MetricsAndroid(tempMetrics);
tempMetrics.setToDefaults();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
display.getRealMetrics(tempMetrics);
this.realMetrics = new MetricsAndroid(tempMetrics);
} else {
this.realMetrics = null;
}
}
@Override
public void parsePackage(File sourceFile, int flags) throws Exception {
/* public Package parsePackage(File sourceFile, String destCodePath,
DisplayMetrics metrics, int flags)*/
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
String destCodePath = sourceFile.getPath();
mPackageParser = MethodUtils.invokeConstructor(sPackageParserClass, destCodePath);
mPackage = MethodUtils.invokeMethod(mPackageParser, "parsePackage", sourceFile, destCodePath, metrics, flags);
}
private float lengthValue() {
float result = 0.0f;
if (isInTypeSet(LENGTH_SET)) {
switch (currentLexeme.getType()) {
case NUMBER: {
result = (Float) currentLexeme.getValue();
break;
}
case LENGTH: {
PXDimension length = (PXDimension) currentLexeme.getValue();
// FIXME - we need the real DisplayMetrics!
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
result = length.points(metrics).getNumber();
break;
}
default: {
errorWithMessage("Unrecognized token type in LENGTH_SET: " + currentLexeme);
break;
}
}
advance();
advanceIfIsType(PXTransformTokenType.COMMA);
} else {
errorWithMessage("Expected a LENGTH or NUMBER token");
}
return result;
}
/**
* Retrieve overall information about an application package defined
* in a package archive file
*
* @param archiveFilePath The path to the archive file
* @param flags Additional option flags. Use any combination of
* {@link #GET_ACTIVITIES},
* {@link #GET_GIDS},
* {@link #GET_CONFIGURATIONS},
* {@link #GET_INSTRUMENTATION},
* {@link #GET_PERMISSIONS},
* {@link #GET_PROVIDERS},
* {@link #GET_RECEIVERS},
* {@link #GET_SERVICES},
* {@link #GET_SIGNATURES}, to modify the data returned.
*
* @return Returns the information about the package. Returns
* null if the package could not be successfully parsed.
*
* @see #GET_ACTIVITIES
* @see #GET_GIDS
* @see #GET_CONFIGURATIONS
* @see #GET_INSTRUMENTATION
* @see #GET_PERMISSIONS
* @see #GET_PROVIDERS
* @see #GET_RECEIVERS
* @see #GET_SERVICES
* @see #GET_SIGNATURES
*
*/
public PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) {
PackageParser packageParser = new PackageParser(archiveFilePath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
final File sourceFile = new File(archiveFilePath);
PackageParser.Package pkg = packageParser.parsePackage(
sourceFile, archiveFilePath, metrics, 0);
if (pkg == null) {
return null;
}
if ((flags & GET_SIGNATURES) != 0) {
packageParser.collectCertificates(pkg, 0);
}
return PackageParser.generatePackageInfo(pkg, null, flags, 0, 0, null, false,
COMPONENT_ENABLED_STATE_DEFAULT);
}
/**
* Retrieve overall information about an application package defined
* in a package archive file
*
* @param archiveFilePath The path to the archive file
* @param flags Additional option flags. Use any combination of
* {@link #GET_ACTIVITIES},
* {@link #GET_GIDS},
* {@link #GET_CONFIGURATIONS},
* {@link #GET_INSTRUMENTATION},
* {@link #GET_PERMISSIONS},
* {@link #GET_PROVIDERS},
* {@link #GET_RECEIVERS},
* {@link #GET_SERVICES},
* {@link #GET_SIGNATURES}, to modify the data returned.
*
* @return Returns the information about the package. Returns
* null if the package could not be successfully parsed.
*
* @see #GET_ACTIVITIES
* @see #GET_GIDS
* @see #GET_CONFIGURATIONS
* @see #GET_INSTRUMENTATION
* @see #GET_PERMISSIONS
* @see #GET_PROVIDERS
* @see #GET_RECEIVERS
* @see #GET_SERVICES
* @see #GET_SIGNATURES
*
*/
public PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags) {
PackageParser packageParser = new PackageParser(archiveFilePath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
final File sourceFile = new File(archiveFilePath);
PackageParser.Package pkg = packageParser.parsePackage(
sourceFile, archiveFilePath, metrics, 0);
if (pkg == null) {
return null;
}
if ((flags & GET_SIGNATURES) != 0) {
packageParser.collectCertificates(pkg, 0);
}
return PackageParser.generatePackageInfo(pkg, null, flags, 0, 0);
}
/**
* @param archiveFilePath
* @return
*/
private static final Signature[] getPackageArchiveSignaturesInfoAndroid2x(String archiveFilePath) {
//
try {
// 1. 新建PackageParser的实例
Object packageParser = ReflectUtils.invokeConstructor(ReflectUtils.getClass("android.content.pm.PackageParser"),
new Class[]{String.class}, archiveFilePath);
// 2. 调用PackageParser.parsePackage()方法,返回值为Package对象
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
Object pkg = ReflectUtils.invokeMethod(packageParser, "parsePackage", new Class[]{File.class, String.class, DisplayMetrics.class, int.class},
new File(archiveFilePath), archiveFilePath, metrics, 0);
if (pkg == null) {
if (LOG) {
LogDebug.d(MISC_TAG, "failed to parsePackage: f=" + archiveFilePath);
}
return null;
}
// 3. 调用PackageParser.collectCertificates方法
boolean rc = (Boolean) ReflectUtils.invokeMethod(packageParser, "collectCertificates", new Class[]{pkg.getClass(), int.class},
pkg, 0);
if (!rc) {
return null;
}
// 4. 获取Package.mSignatures
Object signatures[] = (Object[]) ReflectUtils.readField(pkg, "mSignatures");
int n = signatures.length;
if (n <= 0) {
if (LOG) {
LogDebug.d(MISC_TAG, "not found signatures: f=" + archiveFilePath);
}
}
if (n > 0) {
if (LOG) {
LogDebug.d(MISC_TAG, "found signatures for android 2.x: length=" + signatures.length);
}
Signature[] a = new Signature[n];
System.arraycopy(signatures, 0, a, 0, n);
return a;
}
} catch (Throwable e) {
if (LOG) {
LogDebug.d(MISC_TAG, e.getMessage(), e);
}
}
return null;
}
/**
* 获取未安装APK的签名, 由于市面上的Android系统版本各一,不推荐使用这种方法获取应用签名
* @param apkPath
* @return
*/
public static String getUninstallAPKSignatures(String apkPath) {
//参数列表类型
Class[] typeArgs = new Class[1];
//参数列表值
Object[] valueArgs = new Object[1];
try {
//2.这是一个Package 解释器, 是隐藏的,获取PackageParser的类
Class pkgParserCls = Class.forName("android.content.pm.PackageParser");
//2.创建PackageParser的实例
typeArgs[0] = String.class;
Constructor pkgParserCt = pkgParserCls.getConstructor(typeArgs);
valueArgs[0] = apkPath;
Object pkgParser = pkgParserCt.newInstance(valueArgs);
//3.获取PackageParser的类的 parsePackage方法;PackageParser.Package mPkgInfo = packageParser.parsePackage(new File(apkPath), apkPath, metrics, 0);
typeArgs = new Class[4];
typeArgs[0] = File.class;
typeArgs[1] = String.class;
typeArgs[2] = DisplayMetrics.class;
typeArgs[3] = int.class;
Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage", typeArgs);
//4.执行parsePackage方法
valueArgs = new Object[4];
valueArgs[0] = new File(apkPath);
valueArgs[1] = apkPath;
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
valueArgs[2] = metrics;
valueArgs[3] = PackageManager.GET_SIGNATURES;
Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);
//5.获取PackageParser类的collectCertificates方法
typeArgs = new Class[2];
typeArgs[0] = pkgParserPkg.getClass();
typeArgs[1] = int.class;
Method pkgParser_collectCertificatesMtd = pkgParserCls.getDeclaredMethod("collectCertificates", typeArgs);
//6.执行collectCertificates方法
valueArgs = new Object[2];
valueArgs[0] = pkgParserPkg;
valueArgs[1] = PackageManager.GET_SIGNATURES;
pkgParser_collectCertificatesMtd.invoke(pkgParser, valueArgs);
//7.获取PackageParser.Package的类的mSignatures属性
Field packageInfoFld = pkgParserPkg.getClass().getDeclaredField("mSignatures");
//8.获取PackageParser.Package的类的mSignatures属性的值
Signature[] info = (Signature[]) packageInfoFld.get(pkgParserPkg);
return info[0].toCharsString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解析APK的manifest
*
* @param info
* 插件信息
*/
private void getPackageInfo(PluginInfo info) {
int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_CONFIGURATIONS
| PackageManager.GET_INSTRUMENTATION | PackageManager.GET_PERMISSIONS | PackageManager.GET_PROVIDERS
| PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES | PackageManager.GET_SIGNATURES;
// 需要获取Package对象,主要是处理隐式启动插件中的activity
PackageParser parser = new PackageParser(info.apkPath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
File sourceFile = new File(info.apkPath);
PackageParser.Package pack = parser.parsePackage(sourceFile, info.apkPath, metrics, 0);
// 因为PackagePaser的generatePackageInfo方法不同版本参数相差太多,所以还是用packagemanager的api
// 但这样导致APK被解析了两次,上面获取Package是一次
PackageInfo packageInfo = mContext.getPackageManager().getPackageArchiveInfo(info.apkPath, flags);
info.packageName = packageInfo.packageName;
info.mPackageObj = pack;
info.mPackageInfo = packageInfo;
ArrayList<PackageParser.Activity> activitys = pack.activities;
int size = activitys.size();
for (int i = 0; i < size; i++) {
mActivitys.addActivity(activitys.get(i));
}
ArrayList<PackageParser.Service> services = pack.services;
size = services.size();
for (int i = 0; i < size; i++) {
mServices.addService(services.get(i));
}
ArrayList<PackageParser.Provider> providers = pack.providers;
size = providers.size();
for (int i = 0; i < size; i++) {
Provider p = providers.get(i);
String names[] = PATTERN_SEMICOLON.split(p.info.authority);
for (int j = 0; j < names.length; j++) {
mProviderInfoMap.put(names[i], p);
}
}
}
private static DisplayMetrics generateDisplayMetrics() {
DisplayMetrics displayMetrics = new DisplayMetrics();
displayMetrics.setToDefaults();
return displayMetrics;
}