Advertisement
motafoca

Untitled

Feb 3rd, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. package com.provectus.colectaservlaunch;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.OutputStreamWriter;
  9.  
  10. import android.app.Activity;
  11. import android.content.Intent;
  12. import android.content.res.AssetManager;
  13. import android.net.Uri;
  14. import android.os.Bundle;
  15. import android.util.Log;
  16.  
  17. public class ColectaservlaunchActivity extends Activity {
  18. /** Called when the activity is first created. */
  19.  
  20. // File Copy
  21. private void copyFileOrDir(String path) {
  22. AssetManager assetManager = this.getAssets();
  23. String assets[] = null;
  24. try {
  25. assets = assetManager.list(path);
  26. if (assets.length == 0) {
  27. copyFile(path);
  28. } else {
  29. String fullPath = "/data/data/" + this.getPackageName() + "/" + path;
  30. File dir = new File(fullPath);
  31. if (!dir.exists())
  32. dir.mkdir();
  33. for (int i = 0; i < assets.length; ++i) {
  34. copyFileOrDir(path + "/" + assets[i]);
  35. }
  36. }
  37. } catch (IOException ex) {
  38. Log.e("tag", "I/O Exception", ex);
  39. }
  40. }
  41.  
  42. private void copyFile(String filename) {
  43. AssetManager assetManager = this.getAssets();
  44.  
  45. InputStream in = null;
  46. OutputStream out = null;
  47. try {
  48. in = assetManager.open(filename);
  49. String newFileName = "/data/data/" + this.getPackageName() + "/" + filename;
  50. out = new FileOutputStream(newFileName);
  51.  
  52. byte[] buffer = new byte[1024];
  53. int read;
  54. while ((read = in.read(buffer)) != -1) {
  55. out.write(buffer, 0, read);
  56. }
  57. in.close();
  58. in = null;
  59. out.flush();
  60. out.close();
  61. out = null;
  62. } catch (Exception e) {
  63. Log.e("tag", e.getMessage());
  64. }
  65.  
  66. }
  67.  
  68. void execCommandLine(String command)
  69. {
  70. Runtime runtime = Runtime.getRuntime();
  71. Process proc = null;
  72. OutputStreamWriter osw = null;
  73.  
  74. try
  75. {
  76. proc = runtime.exec("su");
  77. osw = new OutputStreamWriter(proc.getOutputStream());
  78. osw.write(command);
  79. osw.flush();
  80. osw.close();
  81. }
  82. catch (IOException ex)
  83. {
  84. Log.e("execCommandLine()", "Command resulted in an IO Exception: " + command);
  85. return;
  86. }
  87. finally
  88. {
  89. if (osw != null)
  90. {
  91. try
  92. {
  93. osw.close();
  94. }
  95. catch (IOException e){}
  96. }
  97. }
  98.  
  99. try
  100. {
  101. proc.waitFor();
  102. }
  103. catch (InterruptedException e){}
  104.  
  105. if (proc.exitValue() != 0)
  106. {
  107. Log.e("execCommandLine()", "Command returned error: " + command + "\n Exit code: " + proc.exitValue());
  108. }
  109. }
  110.  
  111. @Override
  112. public void onCreate(Bundle savedInstanceState) {
  113. super.onCreate(savedInstanceState);
  114. setContentView(R.layout.main);
  115. copyFileOrDir("scripts");
  116. execCommandLine("chmod 755 /data/data/com.provectus.colectaservlaunch/scripts/apache_start");
  117. execCommandLine("chmod 755 /data/data/com.provectus.colectaservlaunch/scripts/apache_stop");
  118. execCommandLine("chmod 755 /data/data/com.provectus.colectaservlaunch/scripts/ssh_start");
  119. execCommandLine("chmod 755 /data/data/com.provectus.colectaservlaunch/scripts/ssh_stop");
  120. execCommandLine("/data/data/com.provectus.colectaservlaunch/scripts/apache_start");
  121. execCommandLine("/data/data/com.provectus.colectaservlaunch/scripts/ssh_start");
  122. Uri uriUrl = Uri.parse("http://127.0.0.1/collectaservicos/login.php");
  123. Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
  124. startActivity(launchBrowser);
  125. finish();
  126.  
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement