2018-01-01 09:06:37 +01:00
|
|
|
package com.wireguard.android.activity;
|
2017-08-09 14:34:28 +02:00
|
|
|
|
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-04-27 18:59:27 +02:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.support.v7.preference.Preference;
|
|
|
|
import android.support.v7.preference.PreferenceFragmentCompat;
|
2018-04-29 06:44:20 +02:00
|
|
|
import android.view.MenuItem;
|
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
|
|
|
|
2018-04-27 18:59:27 +02:00
|
|
|
public class SettingsActivity extends AppCompatActivity {
|
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
|
2018-04-27 18:59:27 +02:00
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
2018-04-29 02:04:28 +02:00
|
|
|
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-04-27 18:59:27 +02:00
|
|
|
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
|
|
|
|
getSupportFragmentManager().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
|
|
|
}
|
|
|
|
|
2018-04-29 06:44:20 +02:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 18:59:27 +02:00
|
|
|
public static class SettingsFragment extends PreferenceFragmentCompat {
|
2017-08-16 11:34:14 +02:00
|
|
|
@Override
|
2018-04-27 18:59:27 +02:00
|
|
|
public void onCreatePreferences(final Bundle savedInstanceState, final String key) {
|
2017-08-16 11:34:14 +02:00
|
|
|
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
|
|
|
}
|