2018-01-01 09:06:37 +01:00
|
|
|
package com.wireguard.android.activity;
|
2017-08-09 14:34:28 +02:00
|
|
|
|
|
|
|
import android.app.Activity;
|
2018-01-01 09:06:37 +01:00
|
|
|
import android.app.Fragment;
|
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
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
import com.wireguard.android.R;
|
|
|
|
import com.wireguard.android.preference.TunnelListPreference;
|
|
|
|
import com.wireguard.android.util.RootShell;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for changing application-global persistent settings.
|
|
|
|
*/
|
2017-12-19 02:42:00 +01:00
|
|
|
|
2017-08-09 14:34:28 +02:00
|
|
|
public class SettingsActivity extends Activity {
|
2018-01-01 09:06:37 +01:00
|
|
|
public static final String KEY_SHOW_QUICK_TILE_SETTINGS = "show_quick_tile_settings";
|
|
|
|
|
2017-08-16 11:34:14 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2018-01-01 09:06:37 +01:00
|
|
|
if (getFragmentManager().findFragmentById(android.R.id.content) == null) {
|
|
|
|
final Fragment fragment = new SettingsFragment();
|
|
|
|
fragment.setArguments(getIntent().getExtras());
|
|
|
|
getFragmentManager().beginTransaction()
|
|
|
|
.add(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-12-19 02:42:00 +01:00
|
|
|
final Preference installTools = findPreference("install_cmd_line_tools");
|
2018-01-01 09:06:37 +01:00
|
|
|
installTools.setOnPreferenceClickListener(preference -> {
|
|
|
|
new ToolsInstaller(preference).execute();
|
|
|
|
return true;
|
2017-12-19 02:42:00 +01:00
|
|
|
});
|
2018-01-01 09:06:37 +01:00
|
|
|
if (getArguments() != null && getArguments().getBoolean(KEY_SHOW_QUICK_TILE_SETTINGS))
|
|
|
|
((TunnelListPreference) findPreference("primary_config")).show();
|
2017-12-19 02:42:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
private static final class ToolsInstaller extends AsyncTask<Void, Void, Integer> {
|
|
|
|
private static final String[][] LIBRARY_NAMED_EXECUTABLES = {
|
|
|
|
{"libwg.so", "wg"},
|
|
|
|
{"libwg-quick.so", "wg-quick"}
|
|
|
|
};
|
2017-12-19 02:42:00 +01:00
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
private final Context context;
|
|
|
|
private final Preference preference;
|
2017-12-19 02:42:00 +01:00
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
private ToolsInstaller(final Preference preference) {
|
|
|
|
context = preference.getContext();
|
|
|
|
this.preference = preference;
|
|
|
|
preference.setEnabled(false);
|
|
|
|
preference.setSummary(context.getString(R.string.install_cmd_line_tools_progress));
|
|
|
|
}
|
2017-12-19 02:42:00 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Integer doInBackground(final Void... voids) {
|
|
|
|
final String libDir = context.getApplicationInfo().nativeLibraryDir;
|
|
|
|
final StringBuilder cmd = new StringBuilder();
|
|
|
|
|
|
|
|
cmd.append("set -ex;");
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
for (final String[] libraryNamedExecutable : LIBRARY_NAMED_EXECUTABLES) {
|
|
|
|
final String arg1 = '\'' + libDir + '/' + libraryNamedExecutable[0] + '\'';
|
|
|
|
final String arg2 = "'/system/xbin/" + libraryNamedExecutable[1] + '\'';
|
2017-12-19 02:42:00 +01:00
|
|
|
|
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
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
for (final String[] libraryNamedExecutable : LIBRARY_NAMED_EXECUTABLES) {
|
|
|
|
final String arg1 = '\'' + libDir + '/' + libraryNamedExecutable[0] + '\'';
|
|
|
|
final String arg2 = "'/system/xbin/" + libraryNamedExecutable[1] + '\'';
|
2017-12-19 02:42:00 +01:00
|
|
|
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) {
|
2018-01-01 09:06:37 +01:00
|
|
|
final String status;
|
2017-12-19 02:42:00 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-01-01 09:06:37 +01:00
|
|
|
preference.setSummary(status);
|
|
|
|
preference.setEnabled(true);
|
2017-08-16 11:34:14 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-09 14:34:28 +02:00
|
|
|
}
|