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-04-29 02:04:28 +02:00
|
|
|
import android.content.pm.PackageManager;
|
2017-08-16 11:34:14 +02:00
|
|
|
import android.os.Bundle;
|
2018-02-07 19:19:20 +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-02-07 19:19:20 +01:00
|
|
|
import com.wireguard.android.Application;
|
2018-01-01 09:06:37 +01:00
|
|
|
import com.wireguard.android.R;
|
2018-02-07 19:19:20 +01:00
|
|
|
import com.wireguard.android.backend.WgQuickBackend;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
2018-04-29 02:04:28 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
/**
|
|
|
|
* 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-04-29 02:04:28 +02:00
|
|
|
@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, String[] permissions, int[] grantResults) {
|
|
|
|
final PermissionRequestCallback f = permissionRequestCallbacks.get(requestCode);
|
|
|
|
if (f != null) {
|
|
|
|
permissionRequestCallbacks.remove(requestCode);
|
|
|
|
f.done(permissions, grantResults);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
getFragmentManager().beginTransaction()
|
2018-01-08 04:45:11 +01:00
|
|
|
.add(android.R.id.content, new SettingsFragment())
|
2018-01-01 09:06:37 +01:00
|
|
|
.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);
|
2018-02-07 19:19:20 +01:00
|
|
|
if (Application.getComponent().getBackendType() != WgQuickBackend.class) {
|
|
|
|
final Preference toolsInstaller =
|
|
|
|
getPreferenceManager().findPreference("tools_installer");
|
|
|
|
getPreferenceScreen().removePreference(toolsInstaller);
|
|
|
|
}
|
2017-08-16 11:34:14 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-09 14:34:28 +02:00
|
|
|
}
|