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.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.zip.ZipEntry;
24 import java.util.zip.ZipFile;
25 import java.util.zip.ZipOutputStream;
27 import android.app.AlertDialog;
28 import android.app.ProgressDialog;
29 import android.content.DialogInterface;
30 import android.content.Intent;
31 import android.content.DialogInterface.OnClickListener;
32 import android.net.Uri;
33 import android.os.Bundle;
34 import android.view.Menu;
35 import android.view.MenuInflater;
36 import android.view.MenuItem;
37 import android.view.MenuItem.OnMenuItemClickListener;
39 import com.koushikdutta.superuser.util.Settings;
40 import com.koushikdutta.superuser.util.StreamUtility;
41 import com.koushikdutta.superuser.util.SuHelper;
42 import com.koushikdutta.widgets.BetterListActivity;
44 public class MainActivity extends BetterListActivity {
45 public MainActivity() {
46 super(PolicyFragment.class);
49 public PolicyFragmentInternal getFragment() {
50 return (PolicyFragmentInternal)super.getFragment();
54 public boolean onCreateOptionsMenu(Menu menu) {
55 MenuInflater mi = new MenuInflater(this);
56 mi.inflate(R.menu.app, menu);
57 MenuItem about = menu.findItem(R.id.about);
58 about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
60 public boolean onMenuItemClick(MenuItem item) {
61 getFragment().setContent(new AboutFragment(), true, getString(R.string.about));
66 return super.onCreateOptionsMenu(menu);
69 File extractSu() throws IOException, InterruptedException {
70 String arch = "armeabi";
71 if (System.getProperty("os.arch").contains("x86") || System.getProperty("os.arch").contains("i686") || System.getProperty("os.arch").contains("i386"))
73 ZipFile zf = new ZipFile(getPackageCodePath());
74 ZipEntry su = zf.getEntry("assets/" + arch + "/su");
75 InputStream zin = zf.getInputStream(su);
76 File ret = getFileStreamPath("su");
77 FileOutputStream fout = new FileOutputStream(ret);
78 StreamUtility.copyStream(zin, fout);
85 void doRecoveryInstall() {
86 final ProgressDialog dlg = new ProgressDialog(this);
87 dlg.setTitle(R.string.installing);
88 dlg.setMessage(getString(R.string.installing_superuser));
89 dlg.setIndeterminate(true);
92 void doEntry(ZipOutputStream zout, String entryName, String dest) throws IOException {
93 ZipFile zf = new ZipFile(getPackageCodePath());
94 ZipEntry ze = zf.getEntry(entryName);
95 zout.putNextEntry(new ZipEntry(dest));
97 StreamUtility.copyStream(in = zf.getInputStream(ze), zout);
105 File zip = getFileStreamPath("superuser.zip");
106 ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zip));
107 doEntry(zout, "assets/update-binary", "META-INF/com/google/android/update-binary");
110 ZipFile zf = new ZipFile(getPackageCodePath());
111 ZipEntry ze = zf.getEntry("assets/reboot");
113 FileOutputStream reboot;
114 StreamUtility.copyStream(in = zf.getInputStream(ze), reboot = openFileOutput("reboot", MODE_PRIVATE));
118 final File su = extractSu();
121 String.format("cat %s > /cache/superuser.zip\n", zip.getAbsolutePath()) +
122 String.format("cat %s > /cache/su\n", su.getAbsolutePath()) +
123 String.format("cat %s > /cache/Superuser.apk\n", getPackageCodePath()) +
124 "mkdir /cache/recovery\n" +
125 "echo '--update_package=CACHE:superuser.zip' > /cache/recovery/command\n" +
126 "chmod 644 /cache/superuser.zip\n" +
127 "chmod 644 /cache/recovery/command\n" +
129 String.format("chmod 755 %s\n", getFileStreamPath("reboot").getAbsolutePath()) +
131 Process p = Runtime.getRuntime().exec("su");
132 p.getOutputStream().write(command.getBytes());
133 p.getOutputStream().close();
134 File rebootScript = getFileStreamPath("reboot.sh");
135 StreamUtility.writeFile(rebootScript, "reboot recovery ; " + getFileStreamPath("reboot").getAbsolutePath() + " recovery ;");
137 Runtime.getRuntime().exec(new String[] { "su", "-c", ". " + rebootScript.getAbsolutePath() });
138 if (p.waitFor() != 0)
139 throw new Exception("non zero result");
141 catch (Exception ex) {
142 ex.printStackTrace();
145 runOnUiThread(new Runnable() {
148 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
149 builder.setPositiveButton(android.R.string.ok, null);
150 builder.setTitle(R.string.install);
151 builder.setMessage(R.string.install_error);
152 builder.create().show();
160 void doSystemInstall() {
161 final ProgressDialog dlg = new ProgressDialog(this);
162 dlg.setTitle(R.string.installing);
163 dlg.setMessage(getString(R.string.installing_superuser));
164 dlg.setIndeterminate(true);
168 boolean _error = false;
170 final File su = extractSu();
171 final String command =
172 "mount -orw,remount /system\n" +
173 "rm /system/xbin/su\n" +
174 "rm /system/bin/su\n" +
175 "rm /system/app/Supersu.*\n" +
176 "rm /system/app/superuser.*\n" +
177 "rm /system/app/supersu.*\n" +
178 "rm /system/app/SuperUser.*\n" +
179 "rm /system/app/SuperSU.*\n" +
180 String.format("cat %s > /system/xbin/su\n", su.getAbsolutePath()) +
181 "chmod 6755 /system/xbin/su\n" +
182 "ln -s /system/xbin/su /system/bin/su\n" +
183 "mount -oro,remount /system\n" +
185 Process p = Runtime.getRuntime().exec("su");
186 p.getOutputStream().write(command.getBytes());
187 p.getOutputStream().close();
188 if (p.waitFor() != 0)
189 throw new Exception("non zero result");
190 SuHelper.checkSu(MainActivity.this);
192 catch (Exception ex) {
194 ex.printStackTrace();
197 final boolean error = _error;
198 runOnUiThread(new Runnable() {
201 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
202 builder.setPositiveButton(android.R.string.ok, null);
203 builder.setTitle(R.string.install);
206 builder.setMessage(R.string.install_error);
209 builder.setMessage(R.string.install_success);
211 builder.create().show();
219 AlertDialog.Builder builder = new AlertDialog.Builder(this);
220 builder.setTitle(R.string.install);
221 builder.setMessage(R.string.install_superuser_info);
222 builder.setPositiveButton(R.string.install, new OnClickListener() {
224 public void onClick(DialogInterface dialog, int which) {
228 builder.setNegativeButton(android.R.string.cancel, null);
229 builder.setNeutralButton(R.string.recovery_install, new OnClickListener() {
231 public void onClick(DialogInterface dialog, int which) {
235 builder.create().show();
238 private void saveWhatsNew() {
239 Settings.setString(this, "whats_new", WHATS_NEW);
242 // this is intentionally not localized as it will change constantly.
243 private static final String WHATS_NEW = "This update includes a security fix. See Google Play release notes for more information.";
244 protected void doWhatsNew() {
245 if (WHATS_NEW.equals(Settings.getString(this, "whats_new")))
248 AlertDialog.Builder builder = new AlertDialog.Builder(this);
249 builder.setTitle(R.string.whats_new);
250 builder.setIcon(R.drawable.ic_launcher);
251 builder.setMessage(WHATS_NEW);
252 builder.setPositiveButton(R.string.rate, new OnClickListener() {
254 public void onClick(DialogInterface dialog, int which) {
255 Intent i = new Intent();
256 i.setData(Uri.parse("market://details?id=com.koushikdutta.superuser"));
260 builder.setNegativeButton(android.R.string.cancel, null);
261 builder.create().show();
265 protected void onCreate(Bundle savedInstanceState) {
266 Settings.applyDarkThemeSetting(this, R.style.SuperuserDarkActivity);
267 super.onCreate(savedInstanceState);
269 if (Settings.getBoolean(this, "first_run", true)) {
271 Settings.setBoolean(this, "first_run", false);
274 final ProgressDialog dlg = new ProgressDialog(this);
275 dlg.setTitle(R.string.superuser);
276 dlg.setMessage(getString(R.string.checking_superuser));
277 dlg.setIndeterminate(true);
281 boolean _error = false;
283 SuHelper.checkSu(MainActivity.this);
285 catch (Exception e) {
289 final boolean error = _error;
291 runOnUiThread(new Runnable() {