public class InstallCommand{
public static void main(String[] args) {
int returnCode = installPackageCore(packageName, strInstallPath, installParam);
}
//核心安装方法
private static int installPackageCore(String packageName, String path, String installToParam) {
Object ipm = ReflectUtils.invokeStaticMethod("android.app.ActivityThread", "getPackageManager", null);
final AtomicInteger resultType = new AtomicInteger(Code.SilentlyInstallCommandCoreInit);
if (ipm == null) {
resultType.set(Code.SilentlyInstallIPackageManagerNULL);
return resultType.get();
}
Constructor constructor = null;
try {
constructor = ReflectUtils.getDeclaredConstructor("android.app.ApplicationPackageManager", Class.forName("android.app.ContextImpl"), Class.forName("android.content.pm.IPackageManager"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
log(TAG, "installPackageCore_getDeclaredConstructor:" + e.getMessage());
}
if (constructor == null) {
resultType.set(Code.SilentlyInstallApplicationPackageManagerConstructorException);
return resultType.get();
}
Object mApplicationPackageManager = null;
try {
Object context = null;
if (Build.VERSION.SDK_INT >= 24) {
context = createFakeContextImpl();
if (context == null) {
resultType.set(Code.SilentlyInstallCreateFakeContextException);
return resultType.get();
}
}
mApplicationPackageManager = constructor.newInstance(context, ipm);
} catch (InstantiationException e) {
e.printStackTrace();
log(TAG, "installPackageCore_ConstructApplicationPackageManager:" + e.getMessage());
} catch (IllegalAccessException e) {
e.printStackTrace();
log(TAG, "installPackageCore_ConstructApplicationPackageManager:" + e.getMessage());
} catch (InvocationTargetException e) {
e.printStackTrace();
log(TAG, "installPackageCore_ConstructApplicationPackageManager:" + e.getMessage());
}
if (mApplicationPackageManager == null) {
resultType.set(Code.SilentlyInstallConstructApplicationPackageManagerException);
return resultType.get();
}
final Method method;
final CountDownLatch countDownLatch = new CountDownLatch(1);
try {
method = ReflectUtils.getMethod(mApplicationPackageManager.getClass().getName(), "installPackage", Uri.class, Class.forName("android.content.pm.IPackageInstallObserver"), int.class, String.class);
method.setAccessible(true);
Object IPackageInstallObserver = WrapIPackageHelper.wrapIPackageInstallObserverStub(new IPackageInstallObserver.Stub() {
@Override
public void packageInstalled(String packageName, int returnCode) throws RemoteException {
log(TAG, String.format("IPackageInstallObserver packageInstalled packageName:%s,returnCode:%s", packageName, returnCode));
resultType.set(returnCode);
countDownLatch.countDown();
}
});
method.invoke(mApplicationPackageManager, Uri.fromFile(new File(path)), IPackageInstallObserver, parseInstallParam(installToParam), packageName);
countDownLatch.await(90, TimeUnit.SECONDS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
log(TAG, "installPackageCore_installPackage:" + e.getMessage());
resultType.set(Code.SilentlyInstallClassNotFoundException);
} catch (InvocationTargetException e) {
e.printStackTrace();
log(TAG, "installPackageCore_installPackage:" + e.getMessage());
resultType.set(Code.SilentlyInstallInvocationTargetException);
} catch (IllegalAccessException e) {
e.printStackTrace();
log(TAG, "installPackageCore_installPackage:" + e.getMessage());
resultType.set(Code.SilentlyInstallIllegalAccessException);
} catch (InterruptedException e) {
e.printStackTrace();
log(TAG, "installPackageCore_installPackage:" + e.getMessage());
resultType.set(Code.SilentlyInstallInterruptedException);
} catch (Exception e) {
e.printStackTrace();
log(TAG, "installPackageCore_installPackage:" + e.getMessage());
resultType.set(Code.SilentlyInstallOtherException);
}
return resultType.get();
}
@TargetApi(24)
private static Object createFakeContextImpl() {
Looper.prepare();
Constructor constructor = ReflectUtils.getDeclaredConstructor("android.app.ActivityThread");
Object mActivityThread = null;
try {
mActivityThread = constructor.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
log(TAG, "createFakeContextImpl:" + e);
} catch (IllegalAccessException e) {
e.printStackTrace();
log(TAG, "createFakeContextImpl:" + e);
} catch (InvocationTargetException e) {
e.printStackTrace();
StringBuffer s = new StringBuffer(e.getTargetException().toString() + "\n");
StackTraceElement elements[] = e.getTargetException().getStackTrace();
for (StackTraceElement element : elements) {
s.append(element.toString()).append("\n");
}
log(TAG, "createFakeContextImpl:" + s.toString());
}
if (mActivityThread == null) {
mActivityThread = ReflectUtils.invokeStaticMethod("android.app.ActivityThread", "systemMain", null);
}
if (mActivityThread != null) {
Object mContext = ReflectUtils.invokeMethod(mActivityThread, "getSystemContext", null);
if (mContext == null) {
try {
mContext = ReflectUtils.invokeStaticMethod("android.app.ContextImpl", "createSystemContext", new Class[]{Class.forName("android.app.ActivityThread")}, mActivityThread);
} catch (Exception e) {
e.printStackTrace();
}
}
log(TAG, "createFakeContextImpl mContext:" + mContext);
return mContext;
}
return null;
}
}