2 * Copyright (C) 2013 Koushik Dutta (@koush)
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.koushikdutta.superuser;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.zip.ZipEntry;
25 import java.util.zip.ZipFile;
26 import java.util.zip.ZipOutputStream;
28 import android.app.AlertDialog;
29 import android.app.ProgressDialog;
30 import android.content.DialogInterface;
31 import android.content.DialogInterface.OnClickListener;
32 import android.os.Bundle;
33 import android.util.Log;
34 import android.view.Menu;
35 import android.view.MenuItem;
36 import android.view.MenuItem.OnMenuItemClickListener;
38 import com.koushikdutta.superuser.util.Settings;
39 import com.koushikdutta.superuser.util.StreamUtility;
40 import com.koushikdutta.widgets.BetterListActivity;
42 public class MainActivity extends BetterListActivity {
43 public MainActivity() {
44 super(PolicyFragment.class);
47 public PolicyFragmentInternal getFragment() {
48 return (PolicyFragmentInternal)super.getFragment();
52 public boolean onCreateOptionsMenu(Menu menu) {
53 MenuItem about = menu.add(R.string.about);
54 about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
56 public boolean onMenuItemClick(MenuItem item) {
57 getFragment().setContent(new AboutFragment(), true, getString(R.string.about));
62 return super.onCreateOptionsMenu(menu);
65 void doRecoveryInstall() {
66 final ProgressDialog dlg = new ProgressDialog(this);
67 dlg.setTitle(R.string.installing);
68 dlg.setMessage(getString(R.string.installing_superuser));
69 dlg.setIndeterminate(true);
72 void doEntry(ZipOutputStream zout, String entryName, String dest) throws IOException {
73 ZipFile zf = new ZipFile(getPackageCodePath());
74 ZipEntry ze = zf.getEntry(entryName);
75 zout.putNextEntry(new ZipEntry(dest));
77 StreamUtility.copyStream(in = zf.getInputStream(ze), zout);
85 File zip = getFileStreamPath("superuser.zip");
86 ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zip));
87 doEntry(zout, "assets/update-binary", "META-INF/com/google/android/update-binary");
90 final File libsu = new File(getApplicationInfo().dataDir, "lib/libsu.so");
92 throw new Exception(libsu.getAbsolutePath() + " not found");
95 String.format("cat %s > /cache/superuser.zip\n", zip.getAbsolutePath()) +
96 String.format("cat %s > /cache/su\n", libsu.getAbsolutePath()) +
97 String.format("cat %s > /cache/Superuser.apk\n", getPackageCodePath()) +
98 "mkdir /cache/recovery\n" +
99 "echo '--update_package=CACHE:superuser.zip' > /cache/recovery/command\n" +
100 "chmod 644 /cache/superuser.zip\n" +
101 "chmod 644 /cache/recovery/command\n" +
104 Process p = Runtime.getRuntime().exec("su");
105 p.getOutputStream().write(command.getBytes());
106 p.getOutputStream().close();
107 if (p.waitFor() != 0)
108 throw new Exception("non zero result");
110 catch (Exception ex) {
111 ex.printStackTrace();
114 runOnUiThread(new Runnable() {
117 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
118 builder.setPositiveButton(android.R.string.ok, null);
119 builder.setTitle(R.string.install);
120 builder.setMessage(R.string.install_error);
121 builder.create().show();
129 void doSystemInstall() {
130 final ProgressDialog dlg = new ProgressDialog(this);
131 dlg.setTitle(R.string.installing);
132 dlg.setMessage(getString(R.string.installing_superuser));
133 dlg.setIndeterminate(true);
135 final File libsu = new File(getApplicationInfo().dataDir, "lib/libsu.so");
136 final String command =
137 "mount -orw,remount /system\n" +
138 "rm /system/xbin/su\n" +
139 "rm /system/bin/su\n" +
140 "rm /system/app/Supersu.*\n" +
141 "rm /system/app/superuser.*\n" +
142 "rm /system/app/supersu.*\n" +
143 "rm /system/app/SuperUser.*\n" +
144 "rm /system/app/SuperSU.*\n" +
145 String.format("cat %s > /system/xbin/su\n", libsu.getAbsolutePath()) +
146 "chmod 6777 /system/xbin/su\n" +
147 "ln -s /system/xbin/su /system/bin/su\n" +
148 "mount -oro,remount /system\n" +
152 boolean _error = false;
155 throw new Exception(libsu.getAbsolutePath() + " not found");
156 Process p = Runtime.getRuntime().exec("su");
157 p.getOutputStream().write(command.getBytes());
158 p.getOutputStream().close();
159 if (p.waitFor() != 0)
160 throw new Exception("non zero result");
163 catch (Exception ex) {
165 ex.printStackTrace();
168 final boolean error = _error;
169 runOnUiThread(new Runnable() {
172 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
173 builder.setPositiveButton(android.R.string.ok, null);
174 builder.setTitle(R.string.install);
177 builder.setMessage(R.string.install_error);
180 builder.setMessage(R.string.install_success);
182 builder.create().show();
190 AlertDialog.Builder builder = new AlertDialog.Builder(this);
191 builder.setTitle(R.string.install);
192 builder.setMessage(R.string.install_superuser_info);
193 builder.setPositiveButton(R.string.install, new OnClickListener() {
195 public void onClick(DialogInterface dialog, int which) {
199 builder.setNegativeButton(android.R.string.cancel, null);
200 builder.setNeutralButton(R.string.recovery_install, new OnClickListener() {
202 public void onClick(DialogInterface dialog, int which) {
206 builder.create().show();
209 void checkSu() throws Exception {
210 Process p = Runtime.getRuntime().exec("su -v");
211 String result = Settings.readToEnd(p.getInputStream());
212 Log.i("Superuser", "Result: " + result);
213 if (0 != p.waitFor())
214 throw new Exception("non zero result");
216 throw new Exception("no data");
217 if (!result.contains(getPackageName()))
218 throw new Exception("unknown su");
219 // TODO: upgrades herp derp
223 protected void onCreate(Bundle savedInstanceState) {
224 super.onCreate(savedInstanceState);
226 final ProgressDialog dlg = new ProgressDialog(this);
227 dlg.setTitle(R.string.superuser);
228 dlg.setMessage(getString(R.string.checking_superuser));
229 dlg.setIndeterminate(true);
233 boolean error = false;
237 catch (Exception e) {
243 runOnUiThread(new Runnable() {