summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/SettingsActivity.java
blob: bc6d71f2e6647640e7d2e6a0c742c77a5f02cc94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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) {
        super.onCreate(savedInstanceState);
        final FragmentTransaction transaction = getFragmentManager().beginTransaction();
        final SettingsFragment fragment = new SettingsFragment();
        fragment.setArguments(getIntent().getExtras());
        transaction.replace(android.R.id.content, fragment).commit();
    }

    public static class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            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);
        }
    }
}