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 final File su = extractSu();
113 String.format("cat %s > /cache/superuser.zip\n", zip.getAbsolutePath()) +
114 String.format("cat %s > /cache/su\n", su.getAbsolutePath()) +
115 String.format("cat %s > /cache/Superuser.apk\n", getPackageCodePath()) +
116 "mkdir /cache/recovery\n" +
117 "echo '--update_package=CACHE:superuser.zip' > /cache/recovery/command\n" +
118 "chmod 644 /cache/superuser.zip\n" +
119 "chmod 644 /cache/recovery/command\n" +
122 Process p = Runtime.getRuntime().exec("su");
123 p.getOutputStream().write(command.getBytes());
124 p.getOutputStream().close();
125 if (p.waitFor() != 0)
126 throw new Exception("non zero result");
128 catch (Exception ex) {
129 ex.printStackTrace();
132 runOnUiThread(new Runnable() {
135 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
136 builder.setPositiveButton(android.R.string.ok, null);
137 builder.setTitle(R.string.install);
138 builder.setMessage(R.string.install_error);
139 builder.create().show();
147 void doSystemInstall() {
148 final ProgressDialog dlg = new ProgressDialog(this);
149 dlg.setTitle(R.string.installing);
150 dlg.setMessage(getString(R.string.installing_superuser));
151 dlg.setIndeterminate(true);
155 boolean _error = false;
157 final File su = extractSu();
158 final String command =
159 "mount -orw,remount /system\n" +
160 "rm /system/xbin/su\n" +
161 "rm /system/bin/su\n" +
162 "rm /system/app/Supersu.*\n" +
163 "rm /system/app/superuser.*\n" +
164 "rm /system/app/supersu.*\n" +
165 "rm /system/app/SuperUser.*\n" +
166 "rm /system/app/SuperSU.*\n" +
167 String.format("cat %s > /system/xbin/su\n", su.getAbsolutePath()) +
168 "chmod 6755 /system/xbin/su\n" +
169 "ln -s /system/xbin/su /system/bin/su\n" +
170 "mount -oro,remount /system\n" +
172 Process p = Runtime.getRuntime().exec("su");
173 p.getOutputStream().write(command.getBytes());
174 p.getOutputStream().close();
175 if (p.waitFor() != 0)
176 throw new Exception("non zero result");
177 SuHelper.checkSu(MainActivity.this);
179 catch (Exception ex) {
181 ex.printStackTrace();
184 final boolean error = _error;
185 runOnUiThread(new Runnable() {
188 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
189 builder.setPositiveButton(android.R.string.ok, null);
190 builder.setTitle(R.string.install);
193 builder.setMessage(R.string.install_error);
196 builder.setMessage(R.string.install_success);
198 builder.create().show();
206 AlertDialog.Builder builder = new AlertDialog.Builder(this);
207 builder.setTitle(R.string.install);
208 builder.setMessage(R.string.install_superuser_info);
209 builder.setPositiveButton(R.string.install, new OnClickListener() {
211 public void onClick(DialogInterface dialog, int which) {
215 builder.setNegativeButton(android.R.string.cancel, null);
216 builder.setNeutralButton(R.string.recovery_install, new OnClickListener() {
218 public void onClick(DialogInterface dialog, int which) {
222 builder.create().show();
225 private void saveWhatsNew() {
226 Settings.setString(this, "whats_new", WHATS_NEW);
229 // this is intentionally not localized as it will change constantly.
230 private static final String WHATS_NEW = "This update includes a security fix. See Google Play release notes for more information.";
231 protected void doWhatsNew() {
232 if (WHATS_NEW.equals(Settings.getString(this, "whats_new")))
235 AlertDialog.Builder builder = new AlertDialog.Builder(this);
236 builder.setTitle(R.string.whats_new);
237 builder.setIcon(R.drawable.ic_launcher);
238 builder.setMessage(WHATS_NEW);
239 builder.setPositiveButton(R.string.rate, new OnClickListener() {
241 public void onClick(DialogInterface dialog, int which) {
242 Intent i = new Intent();
243 i.setData(Uri.parse("market://details?id=com.koushikdutta.superuser"));
247 builder.setNegativeButton(android.R.string.cancel, null);
248 builder.create().show();
252 protected void onCreate(Bundle savedInstanceState) {
253 Settings.applyDarkThemeSetting(this, R.style.SuperuserDarkActivity);
254 super.onCreate(savedInstanceState);
256 if (Settings.getBoolean(this, "first_run", true)) {
258 Settings.setBoolean(this, "first_run", false);
261 final ProgressDialog dlg = new ProgressDialog(this);
262 dlg.setTitle(R.string.superuser);
263 dlg.setMessage(getString(R.string.checking_superuser));
264 dlg.setIndeterminate(true);
268 boolean _error = false;
270 SuHelper.checkSu(MainActivity.this);
272 catch (Exception e) {
276 final boolean error = _error;
278 runOnUiThread(new Runnable() {