diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2017-12-19 02:42:00 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2017-12-19 02:42:00 +0100 |
commit | 9b92f4d3c5ed263533844d95031d497e3ba7a0ca (patch) | |
tree | 1869827cbad898087c5458a3d7fd9ef4bcce34c7 /app/src/main/java/com/wireguard/android/SettingsActivity.java | |
parent | ecc2a1c7591d1749c95d9fe687ec00753ea0eae2 (diff) |
SettingsActivity: allow for installing command line tools
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/SettingsActivity.java')
-rw-r--r-- | app/src/main/java/com/wireguard/android/SettingsActivity.java | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/app/src/main/java/com/wireguard/android/SettingsActivity.java b/app/src/main/java/com/wireguard/android/SettingsActivity.java index bb617b63..bc6d71f2 100644 --- a/app/src/main/java/com/wireguard/android/SettingsActivity.java +++ b/app/src/main/java/com/wireguard/android/SettingsActivity.java @@ -2,9 +2,14 @@ package com.wireguard.android; import android.app.Activity; import android.app.FragmentTransaction; +import android.content.Context; +import android.os.AsyncTask; import android.os.Bundle; +import android.preference.Preference; import android.preference.PreferenceFragment; +import com.wireguard.android.backends.RootShell; + public class SettingsActivity extends Activity { @Override protected void onCreate(final Bundle savedInstanceState) { @@ -22,6 +27,77 @@ public class SettingsActivity extends Activity { addPreferencesFromResource(R.xml.preferences); if (getArguments() != null && getArguments().getBoolean("showQuickTile")) ((ConfigListPreference) findPreference("primary_config")).show(); + + final Preference installTools = findPreference("install_cmd_line_tools"); + installTools.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + public boolean onPreferenceClick(Preference preference) { + new ToolsInstaller(installTools).execute(); + return true; + } + }); + } + } + + private static class ToolsInstaller extends AsyncTask<Void, Void, Integer> { + Preference installTools; + + public ToolsInstaller(Preference installTools) { + this.installTools = installTools; + installTools.setEnabled(false); + installTools.setSummary(installTools.getContext().getString(R.string.install_cmd_line_tools_progress)); + } + + private static final String[][] libraryNamedExecutables = { + { "libwg.so", "wg" }, + { "libwg-quick.so", "wg-quick" } + }; + + @Override + protected Integer doInBackground(final Void... voids) { + final Context context = installTools.getContext(); + final String libDir = context.getApplicationInfo().nativeLibraryDir; + final StringBuilder cmd = new StringBuilder(); + + cmd.append("set -ex;"); + + for (final String[] libraryNamedExecutable : libraryNamedExecutables) { + final String arg1 = "'" + libDir + "/" + libraryNamedExecutable[0] + "'"; + final String arg2 = "'/system/xbin/" + libraryNamedExecutable[1] + "'"; + + cmd.append(String.format("diff %s %s && ", arg1, arg2)); + } + cmd.append("exit 114;"); + + cmd.append("trap 'mount -o remount,ro /system' EXIT;"); + cmd.append("mount -o remount,rw /system;"); + + for (final String[] libraryNamedExecutable : libraryNamedExecutables) { + final String arg1 = "'" + libDir + "/" + libraryNamedExecutable[0] + "'"; + final String arg2 = "'/system/xbin/" + libraryNamedExecutable[1] + "'"; + cmd.append(String.format("cp %s %s; chmod 755 %s;", arg1, arg2, arg2)); + } + + return new RootShell(context).run(null, cmd.toString()); + } + + @Override + protected void onPostExecute(final Integer ret) { + final Context context = installTools.getContext(); + String status; + + switch (ret) { + case 0: + status = context.getString(R.string.install_cmd_line_tools_success); + break; + case 114 /* OsConstants.EALREADY */: + status = context.getString(R.string.install_cmd_line_tools_already); + break; + default: + status = context.getString(R.string.install_cmd_line_tools_failure); + break; + } + installTools.setSummary(status); + installTools.setEnabled(true); } } } |