-
Notifications
You must be signed in to change notification settings - Fork 73
feat: Add split tunneling #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sonalys
wants to merge
2
commits into
netbirdio:main
Choose a base branch
from
sonalys:ar/split-tunneling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/src/main/java/io/netbird/client/ui/splittunneling/AppInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package io.netbird.client.ui.splittunneling; | ||
|
|
||
| import android.graphics.drawable.Drawable; | ||
|
|
||
| public class AppInfo { | ||
| public final String name; | ||
| public final String packageName; | ||
| public final Drawable icon; | ||
| public boolean isSelected; | ||
|
|
||
| public AppInfo(String name, String packageName, Drawable icon, boolean isSelected) { | ||
| this.name = name; | ||
| this.packageName = packageName; | ||
| this.icon = icon; | ||
| this.isSelected = isSelected; | ||
| } | ||
| } |
87 changes: 87 additions & 0 deletions
87
app/src/main/java/io/netbird/client/ui/splittunneling/SplitTunnelingAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package io.netbird.client.ui.splittunneling; | ||
|
|
||
| import android.view.LayoutInflater; | ||
| import android.view.View; | ||
| import android.view.ViewGroup; | ||
| import android.widget.CheckBox; | ||
| import android.widget.ImageView; | ||
| import android.widget.TextView; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.recyclerview.widget.RecyclerView; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import io.netbird.client.R; | ||
|
|
||
| public class SplitTunnelingAdapter extends RecyclerView.Adapter<SplitTunnelingAdapter.ViewHolder> { | ||
|
|
||
| private List<AppInfo> allApps = new ArrayList<>(); | ||
| private List<AppInfo> filteredApps = new ArrayList<>(); | ||
| private final OnAppClickListener listener; | ||
|
|
||
| public interface OnAppClickListener { | ||
| void onAppClick(AppInfo app); | ||
| } | ||
|
|
||
| public SplitTunnelingAdapter(OnAppClickListener listener) { | ||
| this.listener = listener; | ||
| } | ||
|
|
||
| public void setApps(List<AppInfo> apps) { | ||
| this.allApps = apps; | ||
| this.filteredApps = new ArrayList<>(apps); | ||
| notifyDataSetChanged(); | ||
| } | ||
|
|
||
| public void filter(String query) { | ||
| if (query.isEmpty()) { | ||
| filteredApps = new ArrayList<>(allApps); | ||
| } else { | ||
| String lowerQuery = query.toLowerCase(); | ||
| filteredApps = allApps.stream() | ||
| .filter(app -> app.name.toLowerCase().contains(lowerQuery) || app.packageName.toLowerCase().contains(lowerQuery)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| notifyDataSetChanged(); | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||
| View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_app, parent, false); | ||
| return new ViewHolder(view); | ||
| } | ||
|
|
||
| @Override | ||
| public void onBindViewHolder(@NonNull ViewHolder holder, int position) { | ||
| AppInfo app = filteredApps.get(position); | ||
| holder.textAppName.setText(app.name); | ||
| holder.textPackageName.setText(app.packageName); | ||
| holder.imgAppIcon.setImageDrawable(app.icon); | ||
| holder.checkApp.setChecked(app.isSelected); | ||
| holder.itemView.setOnClickListener(v -> listener.onAppClick(app)); | ||
| } | ||
|
|
||
| @Override | ||
| public int getItemCount() { | ||
| return filteredApps.size(); | ||
| } | ||
|
|
||
| public static class ViewHolder extends RecyclerView.ViewHolder { | ||
| ImageView imgAppIcon; | ||
| TextView textAppName; | ||
| TextView textPackageName; | ||
| CheckBox checkApp; | ||
|
|
||
| public ViewHolder(@NonNull View itemView) { | ||
| super(itemView); | ||
| imgAppIcon = itemView.findViewById(R.id.img_app_icon); | ||
| textAppName = itemView.findViewById(R.id.text_app_name); | ||
| textPackageName = itemView.findViewById(R.id.text_package_name); | ||
| checkApp = itemView.findViewById(R.id.check_app); | ||
| } | ||
| } | ||
| } |
159 changes: 159 additions & 0 deletions
159
app/src/main/java/io/netbird/client/ui/splittunneling/SplitTunnelingFragment.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| package io.netbird.client.ui.splittunneling; | ||
|
|
||
| import android.content.Context; | ||
| import android.content.pm.ApplicationInfo; | ||
| import android.content.pm.PackageManager; | ||
| import android.graphics.drawable.Drawable; | ||
| import android.os.Bundle; | ||
| import android.text.Editable; | ||
| import android.text.TextWatcher; | ||
| import android.view.LayoutInflater; | ||
| import android.view.View; | ||
| import android.view.ViewGroup; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
| import androidx.appcompat.app.AlertDialog; | ||
| import androidx.fragment.app.Fragment; | ||
| import androidx.recyclerview.widget.LinearLayoutManager; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import io.netbird.client.R; | ||
| import io.netbird.client.databinding.FragmentSplitTunnelingBinding; | ||
| import io.netbird.client.tool.Preferences; | ||
| import io.netbird.client.tool.Preferences.SplitTunnelingMode; | ||
|
|
||
| public class SplitTunnelingFragment extends Fragment { | ||
|
|
||
| private FragmentSplitTunnelingBinding binding; | ||
| private SplitTunnelingAdapter adapter; | ||
| private Preferences preferences; | ||
| private List<AppInfo> allApps = new ArrayList<>(); | ||
| private ExecutorService backgroundExecutor; | ||
|
|
||
| @Nullable | ||
| @Override | ||
| public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||
| binding = FragmentSplitTunnelingBinding.inflate(inflater, container, false); | ||
| preferences = new Preferences(requireContext()); | ||
| backgroundExecutor = Executors.newSingleThreadExecutor(); | ||
| setupUI(); | ||
| loadApps(); | ||
| return binding.getRoot(); | ||
| } | ||
|
|
||
| private void setupUI() { | ||
| adapter = new SplitTunnelingAdapter(app -> { | ||
| app.isSelected = !app.isSelected; | ||
| saveApps(); | ||
| adapter.notifyDataSetChanged(); | ||
| }); | ||
|
|
||
| binding.recyclerApps.setLayoutManager(new LinearLayoutManager(requireContext())); | ||
| binding.recyclerApps.setAdapter(adapter); | ||
|
|
||
| Preferences.SplitTunnelingMode mode = preferences.getSplitTunnelingMode(); | ||
| switch (mode) { | ||
| case NONE: | ||
| binding.radioGroupMode.check(R.id.radio_mode_none); | ||
| break; | ||
| case EXCLUDE: | ||
| binding.radioGroupMode.check(R.id.radio_mode_exclude); | ||
| break; | ||
| case INCLUDE: | ||
| binding.radioGroupMode.check(R.id.radio_mode_include); | ||
| break; | ||
| } | ||
|
|
||
| binding.radioGroupMode.setOnCheckedChangeListener((group, checkedId) -> { | ||
| Preferences.SplitTunnelingMode newMode; | ||
| if (checkedId == R.id.radio_mode_none) { | ||
| newMode = Preferences.SplitTunnelingMode.NONE; | ||
| } else if (checkedId == R.id.radio_mode_exclude) { | ||
| newMode = Preferences.SplitTunnelingMode.EXCLUDE; | ||
| } else if (checkedId == R.id.radio_mode_include) { | ||
| newMode = Preferences.SplitTunnelingMode.INCLUDE; | ||
| } else { | ||
| newMode = Preferences.SplitTunnelingMode.NONE; | ||
| } | ||
| preferences.setSplitTunnelingMode(newMode); | ||
| }); | ||
|
|
||
| binding.editSearch.addTextChangedListener(new TextWatcher() { | ||
| @Override | ||
| public void beforeTextChanged(CharSequence s, int start, int count, int after) {} | ||
|
|
||
| @Override | ||
| public void onTextChanged(CharSequence s, int start, int before, int count) { | ||
| adapter.filter(s.toString()); | ||
| } | ||
|
|
||
| @Override | ||
| public void afterTextChanged(Editable s) {} | ||
| }); | ||
| } | ||
|
|
||
| private void loadApps() { | ||
| if (backgroundExecutor == null) return; | ||
| Context context = getContext(); | ||
| if (context == null) return; | ||
|
|
||
| PackageManager pm = context.getPackageManager(); | ||
| String myPackageName = context.getPackageName(); | ||
| binding.progressBar.setVisibility(View.VISIBLE); | ||
|
|
||
| backgroundExecutor.execute(() -> { | ||
| List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA); | ||
| Set<String> selectedApps = preferences.getSplitTunnelingApps(); | ||
|
|
||
| List<AppInfo> loadedApps = apps.stream() | ||
| .filter(app -> !app.packageName.equals(myPackageName)) | ||
| .map(app -> { | ||
| String name = pm.getApplicationLabel(app).toString(); | ||
| Drawable icon = pm.getApplicationIcon(app); | ||
| return new AppInfo(name, app.packageName, icon, selectedApps.contains(app.packageName)); | ||
| }) | ||
| .sorted((a, b) -> a.name.compareToIgnoreCase(b.name)) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| if (binding != null) { | ||
| binding.getRoot().post(() -> { | ||
| if (binding != null) { | ||
| allApps = loadedApps; | ||
| adapter.setApps(allApps); | ||
| binding.progressBar.setVisibility(View.GONE); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void saveApps() { | ||
| if (backgroundExecutor == null) return; | ||
| List<AppInfo> appsSnapshot = new ArrayList<>(allApps); | ||
| backgroundExecutor.execute(() -> { | ||
| Set<String> selectedApps = appsSnapshot.stream() | ||
| .filter(app -> app.isSelected) | ||
| .map(app -> app.packageName) | ||
| .collect(Collectors.toSet()); | ||
| preferences.setSplitTunnelingApps(selectedApps); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void onDestroyView() { | ||
| super.onDestroyView(); | ||
| if (backgroundExecutor != null) { | ||
| backgroundExecutor.shutdown(); | ||
| backgroundExecutor = null; | ||
| } | ||
| binding = null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:viewportWidth="24" | ||
| android:viewportHeight="24" | ||
| android:tint="?attr/colorControlNormal"> | ||
| <path | ||
| android:fillColor="#FF000000" | ||
| android:pathData="M8.59,16.59L13.17,12L8.59,7.41L10,6L16,12L10,18L8.59,16.59Z" /> | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| </vector> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.