2017-08-09 14:34:28 +02:00
|
|
|
package com.wireguard.android;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2017-08-16 11:34:14 +02:00
|
|
|
import android.app.FragmentTransaction;
|
2017-12-19 02:42:00 +01:00
|
|
|
import android.content.Context;
|
|
|
|
import android.os.AsyncTask;
|
2017-08-16 11:34:14 +02:00
|
|
|
import android.os.Bundle;
|
2017-12-19 02:42:00 +01:00
|
|
|
import android.preference.Preference;
|
2017-08-16 11:34:14 +02:00
|
|
|
import android.preference.PreferenceFragment;
|
2017-08-09 14:34:28 +02:00
|
|
|
|
2017-12-19 02:42:00 +01:00
|
|
|
import com.wireguard.android.backends.RootShell;
|
|
|
|
|
2017-08-09 14:34:28 +02:00
|
|
|
public class SettingsActivity extends Activity {
|
2017-08-16 11:34:14 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
final FragmentTransaction transaction = getFragmentManager().beginTransaction();
|
2017-11-27 03:13:34 +01:00
|
|
|
final SettingsFragment fragment = new SettingsFragment();
|
2017-11-27 05:22:54 +01:00
|
|
|
fragment.setArguments(getIntent().getExtras());
|
2017-11-27 03:13:34 +01:00
|
|
|
transaction.replace(android.R.id.content, fragment).commit();
|
2017-08-16 11:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class SettingsFragment extends PreferenceFragment {
|
|
|
|
@Override
|
|
|
|
public void onCreate(final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
addPreferencesFromResource(R.xml.preferences);
|
2017-11-27 05:22:54 +01:00
|
|
|
if (getArguments() != null && getArguments().getBoolean("showQuickTile"))
|
2017-11-27 03:13:34 +01:00
|
|
|
((ConfigListPreference) findPreference("primary_config")).show();
|
2017-12-19 02:42:00 +01:00
|
|
|
|
|
|
|
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] + "'";
|
|
|
|
|
2017-12-20 17:54:28 +01:00
|
|
|
cmd.append(String.format("cmp -s %s %s && ", arg1, arg2));
|
2017-12-19 02:42:00 +01:00
|
|
|
}
|
|
|
|
cmd.append("exit 114;");
|
|
|
|
|
2017-12-20 17:54:28 +01:00
|
|
|
cmd.append("trap 'mount -o ro,remount /system' EXIT;");
|
|
|
|
cmd.append("mount -o rw,remount /system;");
|
2017-12-19 02:42:00 +01:00
|
|
|
|
|
|
|
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);
|
2017-08-16 11:34:14 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-09 14:34:28 +02:00
|
|
|
}
|