summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/activity/SettingsActivity.java
blob: b1754b87f77739b6d77329582389e910e0163c66 (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
package com.wireguard.android.activity;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.view.MenuItem;

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

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

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

public class SettingsActivity extends AppCompatActivity {
    @FunctionalInterface
    public interface PermissionRequestCallback {
        void done(String[] permissions, int[] grantResults);
    }

    private HashMap<Integer, PermissionRequestCallback> permissionRequestCallbacks = new HashMap<>();
    private int permissionRequestCounter = 0;

    public synchronized void ensurePermissions(String[] permissions, PermissionRequestCallback cb) {
        /* TODO(MSF): since when porting to AppCompat, you'll be replacing checkSelfPermission
         * and requestPermission with AppCompat.checkSelfPermission and AppCompat.requestPermission,
         * you can remove this SDK_INT block entirely here, and count on the compat lib to do
         * the right thing. */
        if (android.os.Build.VERSION.SDK_INT < 23) {
            int[] granted = new int[permissions.length];
            Arrays.fill(granted, PackageManager.PERMISSION_GRANTED);
            cb.done(permissions, granted);
        } else {
            List<String> needPermissions = new ArrayList<>(permissions.length);
            for (final String permission : permissions) {
                if (getApplicationContext().checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
                    needPermissions.add(permission);
            }
            if (needPermissions.isEmpty()) {
                int[] granted = new int[permissions.length];
                Arrays.fill(granted, PackageManager.PERMISSION_GRANTED);
                cb.done(permissions, granted);
                return;
            }
            int idx = permissionRequestCounter++;
            permissionRequestCallbacks.put(idx, cb);
            requestPermissions(needPermissions.toArray(new String[needPermissions.size()]), idx);
        }
    }

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

    @Override
    protected void onCreate(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(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(final Bundle savedInstanceState, final String key) {
            addPreferencesFromResource(R.xml.preferences);
            if (Application.getComponent().getBackendType() != WgQuickBackend.class) {
                final Preference toolsInstaller =
                        getPreferenceManager().findPreference("tools_installer");
                getPreferenceScreen().removePreference(toolsInstaller);
            }
        }
    }
}