Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.inject;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.content.res.AssetManager;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- /**
- *
- * @author tastypear
- * www.CoolApk.com 酷安网
- *
- */
- public class Injection extends Activity {
- public static String ZIP_FILE = "data.zip";
- public static String PACKAGE_NAME;
- public static String DATA_DIR;
- public ProgressDialog progressDialog = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- PACKAGE_NAME = getPackageName();
- DATA_DIR = getFilesDir().getParentFile().getParent()+"/";
- progressDialog = ProgressDialog.show(this, "", "");
- if (isFirstRun()) {
- copyData.start();
- } else {
- progressDialog.dismiss();
- startActivity();
- }
- }
- public void startActivity() {
- // 跳至原始MainActivity
- Intent intent = new Intent();
- try {
- String mainActivityName = getMainActivityName();
- intent.setClass(this, Class.forName(mainActivityName));
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- this.startActivity(intent);
- // 返回直接退出
- this.finish();
- }
- final Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- progressDialog.dismiss();
- startActivity();
- super.handleMessage(msg);
- }
- };
- Thread copyData = new Thread() {
- public void run() {
- writeDefaultData();
- handler.sendEmptyMessage(0);
- };
- };
- public boolean isFirstRun() {
- File firstRun = new File(DATA_DIR + PACKAGE_NAME + "/tas");
- return !firstRun.exists();
- }
- // 通过文件获得原MainActivity
- public String getMainActivityName() {
- String activityName = "";
- try {
- InputStream is = getAssets().open("MAIN");
- byte[] buffer = new byte[is.available()];
- is.read(buffer);
- is.close();
- activityName = (new String(buffer)).trim();
- } catch (Exception e) {
- }
- // 解析全名或简写
- if (!activityName.contains(".")){
- return PACKAGE_NAME + "." + activityName;
- } else if (activityName.charAt(0) == '.') {
- return PACKAGE_NAME + activityName;
- } else {
- return activityName;
- }
- }
- // 写入注入数据
- public void writeDefaultData() {
- copyDataFromAsset();
- (new Decompress(DATA_DIR + PACKAGE_NAME + "/" + ZIP_FILE, DATA_DIR
- + PACKAGE_NAME + "/")).unzip();
- (new File(DATA_DIR + PACKAGE_NAME + "/"+ZIP_FILE)).delete();
- File firstRun = new File(DATA_DIR + PACKAGE_NAME + "/tas");
- try {
- firstRun.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // 拷贝zip至/data/data
- public void copyDataFromAsset() {
- AssetManager am = getAssets();
- try {
- String fileName = ZIP_FILE; // destination
- File destinationFile = new File(DATA_DIR + PACKAGE_NAME + "/"
- + fileName);
- InputStream in = am.open(ZIP_FILE); // source
- FileOutputStream f = new FileOutputStream(destinationFile);
- byte[] buffer = new byte[1024];
- int len1 = 0;
- while ((len1 = in.read(buffer)) > 0) {
- f.write(buffer, 0, len1);
- }
- f.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- // 通过路径构造的解压类
- class Decompress {
- private String zipFile;
- private String targetDir;
- public Decompress(String zipFile, String targetDir) {
- this.zipFile = zipFile;
- this.targetDir = targetDir;
- dirChecker("");
- }
- public void unzip() {
- int BUFFER = 2048;
- String strEntry;
- try {
- BufferedOutputStream bos = null;
- FileInputStream fis = new FileInputStream(zipFile);
- ZipInputStream zis = new ZipInputStream(
- new BufferedInputStream(fis));
- ZipEntry entry;
- while ((entry = zis.getNextEntry()) != null) {
- if (entry.isDirectory()) {
- dirChecker(entry.getName());
- }
- try {
- int count;
- byte data[] = new byte[BUFFER];
- strEntry = entry.getName();
- File entryFile = new File(targetDir + strEntry);
- FileOutputStream fos = new FileOutputStream(entryFile);
- bos = new BufferedOutputStream(fos, BUFFER);
- while ((count = zis.read(data, 0, BUFFER)) != -1) {
- bos.write(data, 0, count);
- }
- bos.flush();
- bos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- zis.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //some baksmali hack
- public void dirChecker(String dir) {
- File f = new File(targetDir + dir);
- if (!f.isDirectory()) {
- f.mkdirs();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement