summaryrefslogtreecommitdiffhomepage
path: root/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.java
blob: b597a9b129fcc267922a212bdb9f3ed0ff20837c (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.android.activity;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import android.util.SparseArray;
import android.view.MenuItem;

import com.wireguard.android.Application;
import com.wireguard.android.R;
import com.wireguard.android.backend.WgQuickBackend;
import com.wireguard.android.util.ModuleLoader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Interface for changing application-global persistent settings.
 */

public class SettingsActivity extends ThemeChangeAwareActivity {
    private final SparseArray<PermissionRequestCallback> permissionRequestCallbacks = new SparseArray<>();
    private int permissionRequestCounter;

    public void ensurePermissions(final String[] permissions, final PermissionRequestCallback cb) {
        final List<String> needPermissions = new ArrayList<>(permissions.length);
        for (final String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission)
                    != PackageManager.PERMISSION_GRANTED)
                needPermissions.add(permission);
        }
        if (needPermissions.isEmpty()) {
            final int[] granted = new int[permissions.length];
            Arrays.fill(granted, PackageManager.PERMISSION_GRANTED);
            cb.done(permissions, granted);
            return;
        }
        final int idx = permissionRequestCounter++;
        permissionRequestCallbacks.put(idx, cb);
        ActivityCompat.requestPermissions(this,
                needPermissions.toArray(new String[needPermissions.size()]), idx);
    }

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, new SettingsFragment())
                    .commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onRequestPermissionsResult(final int requestCode,
                                           final String[] permissions,
                                           final int[] grantResults) {
        final PermissionRequestCallback f = permissionRequestCallbacks.get(requestCode);
        if (f != null) {
            permissionRequestCallbacks.remove(requestCode);
            f.done(permissions, grantResults);
        }
    }

    public interface PermissionRequestCallback {
        void done(String[] permissions, int[] grantResults);
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(final Bundle savedInstanceState, final String key) {
            addPreferencesFromResource(R.xml.preferences);
            final PreferenceScreen screen = getPreferenceScreen();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
                screen.removePreference(getPreferenceManager().findPreference("dark_theme"));

            final Preference wgQuickOnlyPrefs[] = {
                    getPreferenceManager().findPreference("tools_installer"),
                    getPreferenceManager().findPreference("restore_on_boot"),
                    getPreferenceManager().findPreference("multiple_tunnels")
            };
            for (final Preference pref : wgQuickOnlyPrefs)
                pref.setVisible(false);
            Application.getBackendAsync().thenAccept(backend -> {
                for (final Preference pref : wgQuickOnlyPrefs) {
                    if (backend instanceof WgQuickBackend)
                        pref.setVisible(true);
                    else
                        screen.removePreference(pref);
                }
            });

            final Preference moduleInstaller = getPreferenceManager().findPreference("module_downloader");
            moduleInstaller.setVisible(false);
            if (ModuleLoader.isModuleLoaded()) {
                screen.removePreference(moduleInstaller);
            } else {
                Application.getAsyncWorker().runAsync(Application.getRootShell()::start).whenComplete((v, e) -> {
                    if (e == null)
                        moduleInstaller.setVisible(true);
                    else
                        screen.removePreference(moduleInstaller);
                });
            }
        }
    }
}