管控应用安装
/frameworks/base/services/core/java/com/android/server/pm/InstallParams.java
========================================================
import android.os.SystemProperties;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileOutputStream;
.........
//白名单目录和是否管控应用安装开关
public static final String SYS_KEY_DOWNLOAD_LIMIT = "persist.sys.key_download_limit";
//白名单目录,高版本不能动态修改,所以切换到下面目录
public static final String WHITE_PATH_FROM = "/vendor/etc/whitelist.txt";
//客户指定白名单地址,
public static final String WHITE_PATH = "/data/system/whitelist.txt";
InstallParams(OriginInfo originInfo, MoveInfo moveInfo, IPackageInstallObserver2 observer,
int installFlags, InstallSource installSource, String volumeUuid,
UserHandle user, String packageAbiOverride, int packageSource,
PackageLite packageLite, PackageManagerService pm) {
super(user, pm);
.........
initWhiteList();
}
InstallParams(File stagedDir, IPackageInstallObserver2 observer,
PackageInstaller.SessionParams sessionParams, InstallSource installSource,
UserHandle user, SigningDetails signingDetails, int installerUid,
PackageLite packageLite, PackageManagerService pm) {
super(user, pm);
.........
initWhiteList();
}
private void initWhiteList(){
File toFile=new File(WHITE_PATH);
if (!toFile.exists()){//文件白名单如果不存在就先copy过来
try {
toFile.createNewFile();
File fromFile=new File(WHITE_PATH_FROM);
copyFiles(fromFile,toFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void returnInstallError(){
mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR;
handleReturnCode();
}
public void installStage() {
/*final Message msg = mPm.mHandler.obtainMessage(INIT_COPY);
setTraceMethod("installStage").setTraceCookie(System.identityHashCode(this));
msg.obj = this;
Trace.asyncTraceBegin(TRACE_TAG_PACKAGE_MANAGER, "installStage",
System.identityHashCode(msg.obj));
Trace.asyncTraceBegin(TRACE_TAG_PACKAGE_MANAGER, "queueInstall",
System.identityHashCode(msg.obj));
mPm.mHandler.sendMessage(msg);*/
if(!mPm.isSystemReady()){
installStageOrig();
}
int mDownLimit=SystemProperties.getInt(SYS_KEY_DOWNLOAD_LIMIT,2);
if(mDownLimit == 1){
//allow all
installStageOrig();
} else if(mDownLimit == 2){
//request
String whitelistStr=readDevices(WHITE_PATH);//TextUtils.isEmpty(whitelistStr)
if (whitelistStr==null || whitelistStr.contains(mPackageLite.getPackageName())) {
installStageOrig();
}else{
returnInstallError();
}
}
/*Slog.i(TAG,"installStage1 file="+mOriginInfo.mFile+", resolvedFile="+mOriginInfo.mResolvedFile+", resolvedPath="+mOriginInfo.mResolvedPath
+", downLimit="+mDownLimit+", pkg="+mPackageLite.getPackageName()+", pkgPath="+mPackageLite.getPath());*/
}
public void installStageOrig() {
final Message msg = mPm.mHandler.obtainMessage(INIT_COPY);
setTraceMethod("installStage").setTraceCookie(System.identityHashCode(this));
msg.obj = this;
Slog.i(TAG,"installStageOrig params="+this);
Trace.asyncTraceBegin(TRACE_TAG_PACKAGE_MANAGER, "installStage",
System.identityHashCode(msg.obj));
Trace.asyncTraceBegin(TRACE_TAG_PACKAGE_MANAGER, "queueInstall",
System.identityHashCode(msg.obj));
mPm.mHandler.sendMessage(msg);
}
void installStage(List<InstallParams> children)
throws PackageManagerException {
InstallParams params = this;
if(!mPm.isSystemReady()){
installStageOrig(children);
}
int mDownLimit = SystemProperties.getInt(SYS_KEY_DOWNLOAD_LIMIT,2);
if(mDownLimit == 1){
//allow all
installStageOrig(children);
} else if(mDownLimit == 2){
//request
String whitelistStr=readDevices(WHITE_PATH);
if (whitelistStr==null || whitelistStr.contains(params.mPackageLite.getPackageName())) {
installStageOrig(children);
}else{
returnInstallError();
}
}
//Slog.i(TAG,"installStage2 file="+params.mOriginInfo.mFile+", resolvedFile="+params.mOriginInfo.mResolvedFile+", resolvedPath="+params.mOriginInfo.mResolvedPath+", downLimit="+mDownLimit+", pkg="+params.mPackageLite.getPackageName()+", pkgPath="+params.mPackageLite.getPath());
}
private static String readDevices(String path) {
try {
File file=new File(path);
if (file.exists()){
//Log.e("FileUtils","length="+file.length());
byte[] mBuf = new byte[(int) file.length()];
FileInputStream mFIS = new FileInputStream(file);
int rLen = mFIS.read(mBuf);
return new String(mBuf,"UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 拷贝文件
* fromfile
* toFile
*/
private static void copyFiles(File fromfile, File toFile) {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(fromfile);
out = new FileOutputStream(toFile.getAbsolutePath(),true);
byte[] bytes = new byte[1024];
int i;
while ((i = in.read(bytes)) != -1){
out.write(bytes, 0 , i);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (in != null)
in.close();
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void installStageOrig(List<InstallParams> children)
throws PackageManagerException {
final Message msg = mPm.mHandler.obtainMessage(INIT_COPY);
final MultiPackageInstallParams params =
new MultiPackageInstallParams(this, children, mPm);
params.setTraceMethod("installStageMultiPackage")
.setTraceCookie(System.identityHashCode(params));
msg.obj = params;
Slog.i(TAG,"installStageOrig parent="+this+", children="+children);
Trace.asyncTraceBegin(TRACE_TAG_PACKAGE_MANAGER, "installStageMultiPackage",
System.identityHashCode(msg.obj));
Trace.asyncTraceBegin(TRACE_TAG_PACKAGE_MANAGER, "queueInstall",
System.identityHashCode(msg.obj));
mPm.mHandler.sendMessage(msg);
}
网友评论