global: format code

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld
2020-03-09 12:36:47 -06:00
parent 40ebf8006e
commit 2e55e5fd05
19 changed files with 305 additions and 292 deletions
@@ -37,17 +37,16 @@ import java9.util.concurrent.CompletableFuture;
@NonNullForAll
public class Application extends android.app.Application implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "WireGuard/" + Application.class.getSimpleName();
public static final String USER_AGENT = String.format(Locale.ENGLISH, "WireGuard/%s (Android %d; %s; %s; %s %s; %s)", BuildConfig.VERSION_NAME, Build.VERSION.SDK_INT, Build.SUPPORTED_ABIS.length > 0 ? Build.SUPPORTED_ABIS[0] : "unknown ABI", Build.BOARD, Build.MANUFACTURER, Build.MODEL, Build.FINGERPRINT);
private static final String TAG = "WireGuard/" + Application.class.getSimpleName();
@SuppressWarnings("NullableProblems") private static WeakReference<Application> weakSelf;
private final CompletableFuture<Backend> futureBackend = new CompletableFuture<>();
@SuppressWarnings("NullableProblems") private AsyncWorker asyncWorker;
@Nullable private Backend backend;
@SuppressWarnings("NullableProblems") private ModuleLoader moduleLoader;
@SuppressWarnings("NullableProblems") private RootShell rootShell;
@SuppressWarnings("NullableProblems") private SharedPreferences sharedPreferences;
@SuppressWarnings("NullableProblems") private ToolsInstaller toolsInstaller;
@SuppressWarnings("NullableProblems") private ModuleLoader moduleLoader;
@SuppressWarnings("NullableProblems") private TunnelManager tunnelManager;
public Application() {
@@ -102,6 +101,10 @@ public class Application extends android.app.Application implements SharedPrefer
return get().futureBackend;
}
public static ModuleLoader getModuleLoader() {
return get().moduleLoader;
}
public static RootShell getRootShell() {
return get().rootShell;
}
@@ -114,10 +117,6 @@ public class Application extends android.app.Application implements SharedPrefer
return get().toolsInstaller;
}
public static ModuleLoader getModuleLoader() {
return get().moduleLoader;
}
public static TunnelManager getTunnelManager() {
return get().tunnelManager;
}
@@ -167,15 +166,15 @@ public class Application extends android.app.Application implements SharedPrefer
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
if ("multiple_tunnels".equals(key) && backend != null && backend instanceof WgQuickBackend)
((WgQuickBackend) backend).setMultipleTunnels(sharedPreferences.getBoolean(key, false));
}
@Override
public void onTerminate() {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
super.onTerminate();
}
@Override
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
if ("multiple_tunnels".equals(key) && backend != null && backend instanceof WgQuickBackend)
((WgQuickBackend)backend).setMultipleTunnels(sharedPreferences.getBoolean(key, false));
}
}
@@ -34,8 +34,20 @@ import androidx.databinding.DataBindingUtil;
@NonNullForAll
public class TunnelDetailFragment extends BaseFragment {
@Nullable private TunnelDetailFragmentBinding binding;
@Nullable private Timer timer;
@Nullable private State lastState = State.TOGGLE;
@Nullable private Timer timer;
private String formatBytes(final long bytes) {
if (bytes < 1024)
return requireContext().getString(R.string.transfer_bytes, bytes);
else if (bytes < 1024 * 1024)
return requireContext().getString(R.string.transfer_kibibytes, bytes / 1024.0);
else if (bytes < 1024 * 1024 * 1024)
return requireContext().getString(R.string.transfer_mibibytes, bytes / (1024.0 * 1024.0));
else if (bytes < 1024 * 1024 * 1024 * 1024)
return requireContext().getString(R.string.transfer_gibibytes, bytes / (1024.0 * 1024.0 * 1024.0));
return requireContext().getString(R.string.transfer_tibibytes, bytes / (1024.0 * 1024.0 * 1024.0) / 1024.0);
}
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
@@ -48,27 +60,6 @@ public class TunnelDetailFragment extends BaseFragment {
inflater.inflate(R.menu.tunnel_detail, menu);
}
@Override
public void onStop() {
super.onStop();
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Override
public void onResume() {
super.onResume();
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
updateStats();
}
}, 0, 1000);
}
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container,
@Nullable final Bundle savedInstanceState) {
@@ -86,6 +77,18 @@ public class TunnelDetailFragment extends BaseFragment {
super.onDestroyView();
}
@Override
public void onResume() {
super.onResume();
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
updateStats();
}
}, 0, 1000);
}
@Override
public void onSelectedTunnelChanged(@Nullable final ObservableTunnel oldTunnel, @Nullable final ObservableTunnel newTunnel) {
if (binding == null)
@@ -99,6 +102,15 @@ public class TunnelDetailFragment extends BaseFragment {
updateStats();
}
@Override
public void onStop() {
super.onStop();
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Override
public void onViewStateRestored(@Nullable final Bundle savedInstanceState) {
if (binding == null) {
@@ -110,18 +122,6 @@ public class TunnelDetailFragment extends BaseFragment {
super.onViewStateRestored(savedInstanceState);
}
private String formatBytes(final long bytes) {
if (bytes < 1024)
return requireContext().getString(R.string.transfer_bytes, bytes);
else if (bytes < 1024*1024)
return requireContext().getString(R.string.transfer_kibibytes, bytes/1024.0);
else if (bytes < 1024*1024*1024)
return requireContext().getString(R.string.transfer_mibibytes, bytes/(1024.0*1024.0));
else if (bytes < 1024*1024*1024*1024)
return requireContext().getString(R.string.transfer_gibibytes, bytes/(1024.0*1024.0*1024.0));
return requireContext().getString(R.string.transfer_tibibytes, bytes/(1024.0*1024.0*1024.0)/1024.0);
}
private void updateStats() {
if (binding == null || !isResumed())
return;
@@ -262,14 +262,6 @@ public class TunnelListFragment extends BaseFragment {
});
}
private void showSnackbar(final CharSequence message) {
if (binding != null) {
final Snackbar snackbar = Snackbar.make(binding.mainContainer, message, Snackbar.LENGTH_LONG);
snackbar.setAnchorView(binding.createFab);
snackbar.show();
}
}
private void onTunnelDeletionFinished(final Integer count, @Nullable final Throwable throwable) {
final String message;
if (throwable == null) {
@@ -337,6 +329,14 @@ public class TunnelListFragment extends BaseFragment {
});
}
private void showSnackbar(final CharSequence message) {
if (binding != null) {
final Snackbar snackbar = Snackbar.make(binding.mainContainer, message, Snackbar.LENGTH_LONG);
snackbar.setAnchorView(binding.createFab);
snackbar.show();
}
}
private MultiselectableRelativeLayout viewForTunnel(final ObservableTunnel tunnel, final List tunnels) {
return (MultiselectableRelativeLayout) binding.tunnelList.findViewHolderForAdapterPosition(tunnels.indexOf(tunnel)).itemView;
}
@@ -27,12 +27,12 @@ import java9.util.concurrent.CompletionStage;
public class ObservableTunnel extends BaseObservable implements Keyed<String>, Tunnel {
private final TunnelManager manager;
@Nullable private Config config;
private State state;
private String name;
private State state;
@Nullable private Statistics statistics;
ObservableTunnel(final TunnelManager manager, final String name,
@Nullable final Config config, final State state) {
@Nullable final Config config, final State state) {
this.name = name;
this.manager = manager;
this.config = config;
@@ -103,6 +103,11 @@ public class ObservableTunnel extends BaseObservable implements Keyed<String>, T
return name;
}
@Override
public void onStateChange(final State newState) {
onStateChanged(newState);
}
State onStateChanged(final State state) {
if (state != State.UP)
onStatisticsChanged(null);
@@ -111,11 +116,6 @@ public class ObservableTunnel extends BaseObservable implements Keyed<String>, T
return state;
}
@Override
public void onStateChange(final State newState) {
onStateChanged(newState);
}
@Nullable
Statistics onStatisticsChanged(@Nullable final Statistics statistics) {
this.statistics = statistics;
@@ -47,8 +47,7 @@ public class LogExporterPreference extends Preference {
final Process process = Runtime.getRuntime().exec(new String[]{
"logcat", "-b", "all", "-d", "-v", "threadtime", "*:V"});
try (final BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
final BufferedReader stderr = new BufferedReader(new InputStreamReader(process.getErrorStream())))
{
final BufferedReader stderr = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
String line;
while ((line = stdout.readLine()) != null) {
outputFile.getOutputStream().write(line.getBytes());
@@ -28,14 +28,6 @@ import androidx.preference.Preference;
public class VersionPreference extends Preference {
@Nullable private String versionSummary;
private String getBackendPrettyName(final Context context, final Backend backend) {
if (backend instanceof GoBackend)
return context.getString(R.string.type_name_kernel_module);
if (backend instanceof WgQuickBackend)
return context.getString(R.string.type_name_go_userspace);
return "";
}
public VersionPreference(final Context context, final AttributeSet attrs) {
super(context, attrs);
@@ -50,6 +42,14 @@ public class VersionPreference extends Preference {
});
}
private String getBackendPrettyName(final Context context, final Backend backend) {
if (backend instanceof GoBackend)
return context.getString(R.string.type_name_kernel_module);
if (backend instanceof WgQuickBackend)
return context.getString(R.string.type_name_go_userspace);
return "";
}
@Nullable
@Override
public CharSequence getSummary() {
@@ -26,30 +26,6 @@ import java.io.OutputStream;
@NonNullForAll
public class DownloadsFileSaver {
public static class DownloadsFile {
private Context context;
private OutputStream outputStream;
private String fileName;
private Uri uri;
private DownloadsFile(final Context context, final OutputStream outputStream, final String fileName, final Uri uri) {
this.context = context;
this.outputStream = outputStream;
this.fileName = fileName;
this.uri = uri;
}
public OutputStream getOutputStream() { return outputStream; }
public String getFileName() { return fileName; }
public void delete() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
context.getContentResolver().delete(uri, null, null);
else
new File(fileName).delete();
}
}
public static DownloadsFile save(final Context context, final String name, final String mimeType, final boolean overwriteExisting) throws Exception {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
final ContentResolver contentResolver = context.getContentResolver();
@@ -89,12 +65,40 @@ public class DownloadsFileSaver {
}
return new DownloadsFile(context, contentStream, path, contentUri);
} else {
@SuppressWarnings("deprecation")
final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
@SuppressWarnings("deprecation") final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
final File file = new File(path, name);
if (!path.isDirectory() && !path.mkdirs())
throw new IOException(context.getString(R.string.create_output_dir_error));
return new DownloadsFile(context, new FileOutputStream(file), file.getAbsolutePath(), null);
}
}
public static class DownloadsFile {
private Context context;
private String fileName;
private OutputStream outputStream;
private Uri uri;
private DownloadsFile(final Context context, final OutputStream outputStream, final String fileName, final Uri uri) {
this.context = context;
this.outputStream = outputStream;
this.fileName = fileName;
this.uri = uri;
}
public void delete() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
context.getContentResolver().delete(uri, null, null);
else
new File(fileName).delete();
}
public String getFileName() {
return fileName;
}
public OutputStream getOutputStream() {
return outputStream;
}
}
}
@@ -51,14 +51,6 @@ public final class ErrorMessages {
BackendException.Reason.TUN_CREATION_ERROR, R.string.tun_create_error,
BackendException.Reason.GO_ACTIVATION_ERROR_CODE, R.string.tunnel_on_error
));
private static final Map<RootShellException.Reason, Integer> RSE_REASON_MAP = new EnumMap<>(Maps.of(
RootShellException.Reason.NO_ROOT_ACCESS, R.string.error_root,
RootShellException.Reason.SHELL_MARKER_COUNT_ERROR, R.string.shell_marker_count_error,
RootShellException.Reason.SHELL_EXIT_STATUS_READ_ERROR, R.string.shell_exit_status_read_error,
RootShellException.Reason.SHELL_START_ERROR, R.string.shell_start_error,
RootShellException.Reason.CREATE_BIN_DIR_ERROR, R.string.create_bin_dir_error,
RootShellException.Reason.CREATE_TEMP_DIR_ERROR, R.string.create_temp_dir_error
));
private static final Map<Format, Integer> KFE_FORMAT_MAP = new EnumMap<>(Maps.of(
Format.BASE64, R.string.key_length_explanation_base64,
Format.BINARY, R.string.key_length_explanation_binary,
@@ -74,6 +66,14 @@ public final class ErrorMessages {
InetNetwork.class, R.string.parse_error_inet_network,
Integer.class, R.string.parse_error_integer
);
private static final Map<RootShellException.Reason, Integer> RSE_REASON_MAP = new EnumMap<>(Maps.of(
RootShellException.Reason.NO_ROOT_ACCESS, R.string.error_root,
RootShellException.Reason.SHELL_MARKER_COUNT_ERROR, R.string.shell_marker_count_error,
RootShellException.Reason.SHELL_EXIT_STATUS_READ_ERROR, R.string.shell_exit_status_read_error,
RootShellException.Reason.SHELL_START_ERROR, R.string.shell_start_error,
RootShellException.Reason.CREATE_BIN_DIR_ERROR, R.string.create_bin_dir_error,
RootShellException.Reason.CREATE_TEMP_DIR_ERROR, R.string.create_temp_dir_error
));
private ErrorMessages() {
// Prevent instantiation