Advertisement
tastypear

Data Injection

May 21st, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.99 KB | None | 0 0
  1. package com.inject;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.zip.ZipEntry;
  11. import java.util.zip.ZipInputStream;
  12. import android.app.Activity;
  13. import android.app.ProgressDialog;
  14. import android.content.Intent;
  15. import android.content.res.AssetManager;
  16. import android.os.Bundle;
  17. import android.os.Handler;
  18. import android.os.Message;
  19.  
  20. /**
  21.  *
  22.  * @author tastypear
  23.  * www.CoolApk.com 酷安网
  24.  *
  25.  */
  26.  
  27. public class Injection extends Activity {
  28.     public static String ZIP_FILE = "data.zip";
  29.     public static String PACKAGE_NAME;
  30.     public static String DATA_DIR;
  31.     public ProgressDialog progressDialog = null;
  32.  
  33.     @Override
  34.     protected void onCreate(Bundle savedInstanceState) {
  35.     super.onCreate(savedInstanceState);
  36.     PACKAGE_NAME = getPackageName();
  37.     DATA_DIR = getFilesDir().getParentFile().getParent()+"/";
  38.     progressDialog = ProgressDialog.show(this, "", "");
  39.  
  40.     if (isFirstRun()) {
  41.         copyData.start();
  42.     } else {
  43.         progressDialog.dismiss();
  44.         startActivity();
  45.     }
  46.     }
  47.  
  48.     public void startActivity() {
  49.     // 跳至原始MainActivity
  50.     Intent intent = new Intent();
  51.     try {
  52.         String mainActivityName = getMainActivityName();
  53.         intent.setClass(this, Class.forName(mainActivityName));
  54.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  55.     } catch (ClassNotFoundException e) {
  56.         e.printStackTrace();
  57.     }
  58.     this.startActivity(intent);
  59.  
  60.     // 返回直接退出
  61.     this.finish();
  62.     }
  63.  
  64.     final Handler handler = new Handler(){
  65.     @Override
  66.     public void handleMessage(Message msg) {
  67.         progressDialog.dismiss();
  68.         startActivity();
  69.         super.handleMessage(msg);
  70.     }
  71.     };
  72.  
  73.     Thread copyData = new Thread() {
  74.     public void run() {
  75.         writeDefaultData();
  76.         handler.sendEmptyMessage(0);
  77.     };
  78.     };
  79.  
  80.     public boolean isFirstRun() {
  81.     File firstRun = new File(DATA_DIR + PACKAGE_NAME + "/tas");
  82.     return !firstRun.exists();
  83.     }
  84.  
  85.     // 通过文件获得原MainActivity
  86.     public String getMainActivityName() {
  87.     String activityName = "";
  88.     try {
  89.         InputStream is = getAssets().open("MAIN");
  90.         byte[] buffer = new byte[is.available()];
  91.         is.read(buffer);
  92.         is.close();
  93.         activityName = (new String(buffer)).trim();
  94.     } catch (Exception e) {
  95.     }
  96.     // 解析全名或简写
  97.     if (!activityName.contains(".")){
  98.         return PACKAGE_NAME + "." + activityName;
  99.     } else if (activityName.charAt(0) == '.') {
  100.         return PACKAGE_NAME + activityName;
  101.     } else {
  102.         return activityName;
  103.     }
  104.     }
  105.  
  106.     // 写入注入数据
  107.     public void writeDefaultData() {
  108.     copyDataFromAsset();
  109.     (new Decompress(DATA_DIR + PACKAGE_NAME + "/" + ZIP_FILE, DATA_DIR
  110.         + PACKAGE_NAME + "/")).unzip();
  111.     (new File(DATA_DIR + PACKAGE_NAME + "/"+ZIP_FILE)).delete();
  112.     File firstRun = new File(DATA_DIR + PACKAGE_NAME + "/tas");
  113.     try {
  114.         firstRun.createNewFile();
  115.     } catch (IOException e) {
  116.         e.printStackTrace();
  117.     }
  118.     }
  119.  
  120.     // 拷贝zip至/data/data
  121.     public void copyDataFromAsset() {
  122.     AssetManager am = getAssets();
  123.     try {
  124.         String fileName = ZIP_FILE; // destination
  125.         File destinationFile = new File(DATA_DIR + PACKAGE_NAME + "/"
  126.             + fileName);
  127.         InputStream in = am.open(ZIP_FILE); // source
  128.         FileOutputStream f = new FileOutputStream(destinationFile);
  129.         byte[] buffer = new byte[1024];
  130.         int len1 = 0;
  131.         while ((len1 = in.read(buffer)) > 0) {
  132.         f.write(buffer, 0, len1);
  133.         }
  134.         f.close();
  135.     } catch (Exception e) {
  136.         e.printStackTrace();
  137.     }
  138.     }
  139.  
  140.     // 通过路径构造的解压类
  141.     class Decompress {
  142.     private String zipFile;
  143.     private String targetDir;
  144.  
  145.     public Decompress(String zipFile, String targetDir) {
  146.         this.zipFile = zipFile;
  147.         this.targetDir = targetDir;
  148.         dirChecker("");
  149.  
  150.     }
  151.  
  152.     public void unzip() {
  153.         int BUFFER = 2048;
  154.         String strEntry;
  155.  
  156.         try {
  157.         BufferedOutputStream bos = null;
  158.         FileInputStream fis = new FileInputStream(zipFile);
  159.         ZipInputStream zis = new ZipInputStream(
  160.             new BufferedInputStream(fis));
  161.         ZipEntry entry;
  162.  
  163.         while ((entry = zis.getNextEntry()) != null) {
  164.             if (entry.isDirectory()) {
  165.             dirChecker(entry.getName());
  166.             }
  167.  
  168.             try {
  169.             int count;
  170.             byte data[] = new byte[BUFFER];
  171.             strEntry = entry.getName();
  172.             File entryFile = new File(targetDir + strEntry);
  173.             FileOutputStream fos = new FileOutputStream(entryFile);
  174.             bos = new BufferedOutputStream(fos, BUFFER);
  175.             while ((count = zis.read(data, 0, BUFFER)) != -1) {
  176.                 bos.write(data, 0, count);
  177.             }
  178.             bos.flush();
  179.             bos.close();
  180.             } catch (Exception e) {
  181.             e.printStackTrace();
  182.             }
  183.         }
  184.         zis.close();
  185.         } catch (Exception e) {
  186.         e.printStackTrace();
  187.         }
  188.     }
  189.  
  190.     //some baksmali hack
  191.     public void dirChecker(String dir) {
  192.         File f = new File(targetDir + dir);
  193.         if (!f.isDirectory()) {
  194.         f.mkdirs();
  195.         }
  196.     }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement