TShopping

 找回密碼
 註冊
搜索
查看: 1392|回復: 1
打印 上一主題 下一主題

[教學] app2sd source code

[複製鏈接]
跳轉到指定樓層
1#
發表於 2013-12-7 00:59:07 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package G3.Mod;

  6. import android.app.Activity;
  7. import android.app.AlertDialog;
  8. import android.content.DialogInterface;
  9. import android.content.Intent;
  10. import android.os.Bundle;
  11. import android.view.Menu;
  12. import android.view.MenuInflater;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.Button;
  18. import android.widget.Spinner;
  19. import android.widget.TextView;
  20. import java.io.IOException;

  21. /**
  22. *
  23. * @author Dymion
  24. */
  25. public class app2sd extends Activity implements OnClickListener {

  26.     private String app2sdContent, app2sdFile;
  27.     private String app2sdV1 = "mkdir /data/misc\n"
  28.             + "mkdir /data/misc/app2sd\n"
  29.             + "LOG=\"/data/misc/app2sd/app2sd.log\"\n"
  30.             + "sleep 3\n"
  31.             + "test_ext4=`fdisk -l /dev/block/mmcblk0 | grep mmcblk0p2 | head -n 1 | cut -d\" \" -f1`\n"
  32.             + "if [ -z $test_ext4 ]; then\n"
  33.             + "\t\t\t\t\techo \"No partition found\" > $LOG\n" + "\t\t\telse\n"
  34.             + "\t\techo \"$(date) App2SD Partition Found\" > $LOG\n"
  35.             + "\t\tbusybox mount -o rw,remount /dev/stl6 /system\n"
  36.             + "\t\tmkdir /system/sd\n"
  37.             + "\t\tbusybox ln -s /proc/partitions /etc/mtab\n"
  38.             + "\t\tbusybox mount -o ro,remount /dev/stl6 /system\n"
  39.             + "\t\tbusybox mount -t ext4 /dev/block/mmcblk0p2 /system/sd 1>>$LOG 2>>$LOG\n"
  40.             + "\t\tmkdir /system/sd/log\n"
  41.             + "\t\tmkdir /system/sd/app\n"
  42.             + "\t\tbusybox mount -o bind /system/sd/app /data/app 1>>$LOG 2>>$LOG\n"
  43.             + "\t\tbusybox mount -o bind /system/sd/log /data/log 1>>$LOG 2>>$LOG\n"
  44.             + "\t\tbusybox mount >> $LOG\n"
  45.             + "\t\techo \"$(date) App2SD Ended\" >> $LOG\n"
  46.             + "\t\t\tfi\n"
  47.             + "sleep 1\n";
  48.     private String app2sdV2 = "mkdir /data/misc\n"
  49.             + "mkdir /data/misc/app2sd\n"
  50.             + "LOG=\"/data/misc/app2sd/app2sd.log\"\n"
  51.             + "sleep 3\n"
  52.             + "test_ext4=`fdisk -l /dev/block/mmcblk0 | grep mmcblk0p2 | head -n 1 | cut -d\" \" -f1`\n"
  53.             + "if [ -z $test_ext4 ]; then\n"
  54.             + "\t\t\t\techo \"No partition found\" > $LOG\n"
  55.             + "\t\t\telse\n"
  56.             + "\t\techo \"$(date) App2SD Partition Found\" > $LOG\n"
  57.             + "\t\tbusybox mount -o rw,remount /dev/stl6 /system\n"
  58.             + "\t\tmkdir /system/sd\n"
  59.             + "\t\tbusybox ln -s /proc/partitions /etc/mtab\n"
  60.             + "\t\tbusybox mount -o ro,remount /dev/stl6 /system\n"
  61.             + "\t\tbusybox mount -t ext4 /dev/block/mmcblk0p2 /system/sd 1>>$LOG 2>>$LOG\n"
  62.             + "\t\tmkdir /system/sd/log\n"
  63.             + "\t\tmkdir /system/sd/app\n"
  64.             + "\t\tmkdir /system/sd/dalvik-cache\n"
  65.             + "\t\tbusybox mount -o bind /system/sd/app /data/app 1>>$LOG 2>>$LOG\n"
  66.             + "\t\tbusybox mount -o bind /system/sd/log /data/log 1>>$LOG 2>>$LOG\n"
  67.             + "\t\tbusybox mount -o bind /system/sd/dalvik-cache /data/dalvik-cache 1>>$LOG 2>>$LOG\n"
  68.             + "\t\tbusybox mount >> $LOG\n"
  69.             + "\t\techo \"$(date) App2SD Ended\" >> $LOG\n"
  70.             + "\t\t\tfi\n"
  71.             + "sleep 1\n";
  72.     private String app2sdV3 = "mkdir /data/misc\n"
  73.             + "mkdir /data/misc/app2sd\n"
  74.             + "LOG=\"/data/misc/app2sd/app2sd.log\"\n"
  75.             + "sleep 3\n"
  76.             + "test_ext4=`fdisk -l /dev/block/mmcblk0 | grep mmcblk0p2 | head -n 1 | cut -d\" \" -f1`\n"
  77.             + "if [ -z $test_ext4 ]; then\n"
  78.             + "\t\t\t\techo \"No partition found\" > $LOG\n"
  79.             + "\t\t\telse\n"
  80.             + "\t\techo \"$(date) App2SD Partition Found\" > $LOG\n"
  81.             + "\t\tbusybox mount -o rw,remount /dev/stl6 /system\n"
  82.             + "\t\tmkdir /system/sd\n"
  83.             + "\t\tbusybox ln -s /proc/partitions /etc/mtab\n"
  84.             + "\t\tbusybox mount -o ro,remount /dev/stl6 /system\n"
  85.             + "\t\tbusybox mount -t ext4 /dev/block/mmcblk0p2 /system/sd 1>>$LOG 2>>$LOG\n"
  86.             + "\t\tmkdir /system/sd/log\n"
  87.             + "\t\tmkdir /system/sd/app\n"
  88.             + "\t\tmkdir /system/sd/dalvik-cache\n"
  89.             + "\t\tmkdir /system/sd/data\n"
  90.             + "\t\tbusybox mount -o bind /system/sd/app /data/app 1>>$LOG 2>>$LOG\n"
  91.             + "\t\tbusybox mount -o bind /system/sd/log /data/log 1>>$LOG 2>>$LOG\n"
  92.             + "\t\tbusybox mount -o bind /system/sd/dalvik-cache /data/dalvik-cache 1>>$LOG 2>>$LOG\n"
  93.             + "\t\tbusybox mount -o bind /system/sd/data /data/data 1>>$LOG 2>>$LOG\n"
  94.             + "\t\tbusybox mount >> $LOG\n"
  95.             + "\t\techo \"$(date) App2SD Ended\" >> $LOG\n"
  96.             + "\t\t\tfi\n"
  97.             + "sleep 1\n";

  98.     /** Called when the activity is first created. */
  99.     @Override
  100.     public void onCreate(Bundle icicle) {
  101.         super.onCreate(icicle);
  102.         setContentView(R.layout.app2sd);

  103.         DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

  104.             @Override
  105.             public void onClick(DialogInterface dialog, int which) {
  106.                 switch (which) {
  107.                     case DialogInterface.BUTTON_POSITIVE:
  108.                         break;
  109.                 }
  110.             }
  111.         };

  112.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  113.         builder.setMessage("Make sure your SD-card is partitioned properly.").setPositiveButton("Ok", dialogClickListener).show();
  114.         builder.setCancelable(false);

  115.         //initialize the spinner and set the right adapter
  116.         Spinner app2sdSpinner = (Spinner) findViewById(R.id.app2sd_spinner);
  117.         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
  118.                 this, R.array.app2sd_array, android.R.layout.simple_spinner_item);
  119.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  120.         app2sdSpinner.setAdapter(adapter);

  121.         //initialize textview and button, they need to be changed
  122.         TextView app2sdTxt = (TextView) findViewById(R.id.app2sdTxt);
  123.         Button app2sd = (Button) findViewById(R.id.app2sd);

  124.         //A check if any of the scripts exists
  125.         readfile readV1 = new readfile("/system/etc/init.d/", "S0_app2sd-V1", false);
  126.         readfile readV2 = new readfile("/system/etc/init.d/", "S0_app2sd-V2", false);
  127.         readfile readV3 = new readfile("/system/etc/init.d/", "S0_app2sd-V3", false);
  128.         if (readV1.fileExists()) {
  129.             app2sdTxt.append("Activated.\n Activated version is 1");
  130.             app2sd.setText("Deactivate App2Sd");
  131.         } else if (readV2.fileExists()) {
  132.             app2sdTxt.append("Activated.\n Activated version is 2");
  133.             app2sd.setText("Deactivate App2Sd");
  134.         } else if (readV3.fileExists()) {
  135.             app2sdTxt.append("Activated.\n Activated version is 3");
  136.             app2sd.setText("Deactivate App2Sd");
  137.         } else {
  138.             app2sdTxt.append("Deactivated.");
  139.             app2sd.setText("Activate App2Sd");
  140.         }

  141.         //set the onclicklistener for the button
  142.         View app2sdBtn = findViewById(R.id.app2sd);
  143.         app2sdBtn.setOnClickListener(this);
  144.     }

  145.     @Override
  146.     public boolean onCreateOptionsMenu(Menu menu) {
  147.         new MenuInflater(this).inflate(R.menu.menu, menu);
  148.         return (super.onCreateOptionsMenu(menu));
  149.     }

  150.     @Override
  151.     public boolean onOptionsItemSelected(MenuItem item) {
  152.         // Handle item selection
  153.         switch (item.getItemId()) {
  154.             case R.id.aboutMenu:
  155.                 startActivity(new Intent(this, About.class));
  156.                 //finish();
  157.                 return true;
  158.             default:
  159.                 return super.onOptionsItemSelected(item);
  160.         }
  161.     }

  162.     public void onClick(View v) {
  163.         switch (v.getId()) {
  164.             case R.id.app2sd:
  165.                 final TextView app2sdTxt = (TextView) findViewById(R.id.app2sdTxt);
  166.                 final Button app2sd = (Button) findViewById(R.id.app2sd);

  167.                 //make /system/ writable
  168.                 try {
  169.                     Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "mount -o remount,rw /dev/block/stl6 /system"});
  170.                 } catch (IOException ex) {
  171.                     //It doesn't work..
  172.                 }

  173.                 //initialize 3 readfiles to check for any of the scripts
  174.                 readfile readV1 = new readfile("/system/etc/init.d/", "S0_app2sd-V1", false);
  175.                 readfile readV2 = new readfile("/system/etc/init.d/", "S0_app2sd-V2", false);
  176.                 readfile readV3 = new readfile("/system/etc/init.d/", "S0_app2sd-V3", false);
  177.                 if (readV1.fileExists()) { //if app2sd V1 is located in the init.d folder, we delete it!
  178.                     try {
  179.                         Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "rm -r /system/etc/init.d/S0_app2sd-V1"});
  180.                     } catch (IOException ex) {
  181.                         //It doesn't work..
  182.                     }

  183.                     DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

  184.                         @Override
  185.                         public void onClick(DialogInterface dialog, int which) {
  186.                             switch (which) {
  187.                                 case DialogInterface.BUTTON_POSITIVE:
  188.                                     try {
  189.                                         Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system", "reboot now"});
  190.                                     } catch (IOException ex) {
  191.                                         //It doesn't work..
  192.                                     }
  193.                                     break;

  194.                                 case DialogInterface.BUTTON_NEGATIVE:
  195.                                     try {
  196.                                         Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system"});
  197.                                     } catch (IOException ex) {
  198.                                         //It doesn't work..
  199.                                     }
  200.                                     app2sdTxt.setText("App2Sd Deactivated.");
  201.                                     app2sd.setText("Activate App2Sd");
  202.                                     break;
  203.                             }
  204.                         }
  205.                     };

  206.                     AlertDialog.Builder builder = new AlertDialog.Builder(this);
  207.                     builder.setMessage("App2Sd deactivated. \n Reboot now?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
  208.                     builder.setCancelable(false);
  209.                 } else if (readV2.fileExists()) { //if app2sd V2 is located in the init.d folder, we delete it!
  210.                     try {
  211.                         Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "rm -r /system/etc/init.d/S0_app2sd-V2"});
  212.                     } catch (IOException ex) {
  213.                         //It doesn't work..
  214.                     }

  215.                     DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

  216.                         @Override
  217.                         public void onClick(DialogInterface dialog, int which) {
  218.                             switch (which) {
  219.                                 case DialogInterface.BUTTON_POSITIVE:
  220.                                     try {
  221.                                         Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system", "reboot now"});
  222.                                     } catch (IOException ex) {
  223.                                         //It doesn't work..
  224.                                     }
  225.                                     break;

  226.                                 case DialogInterface.BUTTON_NEGATIVE:
  227.                                     try {
  228.                                         Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system"});
  229.                                     } catch (IOException ex) {
  230.                                         //It doesn't work..
  231.                                     }
  232.                                     app2sdTxt.setText("App2Sd Deactivated.");
  233.                                     app2sd.setText("Activate App2Sd");
  234.                                     break;
  235.                             }
  236.                         }
  237.                     };

  238.                     AlertDialog.Builder builder = new AlertDialog.Builder(this);
  239.                     builder.setMessage("App2Sd deactivated. \n Reboot now?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
  240.                     builder.setCancelable(false);
  241.                 } else if (readV3.fileExists()) { //if app2sd V3 is located in the init.d folder, we delete it!
  242.                     try {
  243.                         Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "rm -r /system/etc/init.d/S0_app2sd-V3"});
  244.                     } catch (IOException ex) {
  245.                         //It doesn't work..
  246.                     }

  247.                     DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

  248.                         @Override
  249.                         public void onClick(DialogInterface dialog, int which) {
  250.                             switch (which) {
  251.                                 case DialogInterface.BUTTON_POSITIVE:
  252.                                     try {
  253.                                         Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system", "reboot now"});
  254.                                     } catch (IOException ex) {
  255.                                         //It doesn't work..
  256.                                     }
  257.                                     break;

  258.                                 case DialogInterface.BUTTON_NEGATIVE:
  259.                                     try {
  260.                                         Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system"});
  261.                                     } catch (IOException ex) {
  262.                                         //It doesn't work..
  263.                                     }
  264.                                     app2sdTxt.setText("App2Sd Deactivated.");
  265.                                     app2sd.setText("Activate App2Sd");
  266.                                     break;
  267.                             }
  268.                         }
  269.                     };

  270.                     AlertDialog.Builder builder = new AlertDialog.Builder(this);
  271.                     builder.setMessage("App2Sd deactivated. \n Reboot now?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
  272.                     builder.setCancelable(false);
  273.                 } else { // if there aren't any old app2sd scripts, we create one!
  274.                     //todo: change path and add extra check to see what version was selected in the spinner, if not selected anything -> prompt message
  275.                     Spinner app2sdSpinner = (Spinner) findViewById(R.id.app2sd_spinner);
  276.                     app2sdSpinner.getSelectedItem().toString();
  277.                     if ("App2SD V1".equals(app2sdSpinner.getSelectedItem().toString())) {
  278.                         app2sdContent = app2sdV1;
  279.                         app2sdFile = "S0_app2sd-V1";
  280.                     } else if ("App2SD V2".equals(app2sdSpinner.getSelectedItem().toString())) {
  281.                         app2sdContent = app2sdV2;
  282.                         app2sdFile = "S0_app2sd-V2";
  283.                     } else if ("App2SD V3".equals(app2sdSpinner.getSelectedItem().toString())) {
  284.                         app2sdContent = app2sdV3;
  285.                         app2sdFile = "S0_app2sd-V3";
  286.                     } else {
  287.                         app2sdFile = "Non existent.fail";
  288.                         break;
  289.                     }

  290.                     writefile write = new writefile(app2sdContent, "/system/etc/init.d/", app2sdFile, false);

  291.                     DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

  292.                         @Override
  293.                         public void onClick(DialogInterface dialog, int which) {
  294.                             switch (which) {
  295.                                 case DialogInterface.BUTTON_POSITIVE:
  296.                                     try {
  297.                                         Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "mount -o remount,ro /dev/block/stl6 /system", "reboot recovery"});
  298.                                     } catch (IOException ex) {
  299.                                         //It doesn't work..
  300.                                     }
  301.                                     try {
  302.                                         Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "reboot recovery"});
  303.                                     } catch (IOException ex) {
  304.                                         //It doesn't work..
  305.                                     }
  306.                                     break;

  307.                                 case DialogInterface.BUTTON_NEGATIVE:
  308.                                     try {
  309.                                         Runtime.getRuntime().exec(new String[]{"mount -o remount,ro /dev/block/stl6 /system"});
  310.                                     } catch (IOException ex) {
  311.                                         //It doesn't work..
  312.                                     }
  313.                                     app2sdTxt.setText("App2Sd Activated.");
  314.                                     app2sd.setText("Deactivate App2Sd");
  315.                                     break;
  316.                             }
  317.                         }
  318.                     };

  319.                     AlertDialog.Builder builder = new AlertDialog.Builder(this);
  320.                     builder.setMessage("App2Sd activated. \n You have to reboot in recovery and do a wipe data/factory reset. Reboot now?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
  321.                     builder.setCancelable(false);
  322.                 }
  323.                 break;
  324.         }
  325.     }
  326. }

複製代碼
轉帖於:http://code.google.com/p/g3mod-a ... /G3/Mod/app2sd.java



 

臉書網友討論
2#
發表於 2013-12-11 14:32:56 | 只看該作者
牛X海海!!!

版主招募中

*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-5-4 21:59 , Processed in 0.062940 second(s), 22 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表