-
Notifications
You must be signed in to change notification settings - Fork 64
Add Quick Settings Tile service for VPN control #136
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d6fe872
Add Quick Settings Tile service for VPN control
Dan-Kingsley 4a73d0d
Fix binding state management in NetbirdTileService
Dan-Kingsley aee6419
Handle bindService failure in NetbirdTileService and reset pendingCli…
Dan-Kingsley e067bf2
Enhance VPN service intent handling in NetbirdTileService
Dan-Kingsley dcb60cb
fix: Ensure VPN service starts as foreground and adapt activity launc…
Dan-Kingsley c965ed7
Fix: Ensure `updateTile` calls are executed on the main thread using …
Dan-Kingsley 74470aa
feat: Explicitly start VPN service as foreground before running the e…
Dan-Kingsley 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
153 changes: 153 additions & 0 deletions
153
app/src/main/java/io/netbird/client/NetbirdTileService.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,153 @@ | ||
| package io.netbird.client; | ||
|
|
||
| import android.content.ComponentName; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.content.ServiceConnection; | ||
| import android.net.VpnService; | ||
| import android.os.IBinder; | ||
| import android.service.quicksettings.Tile; | ||
| import android.service.quicksettings.TileService; | ||
| import android.util.Log; | ||
|
|
||
| import io.netbird.client.tool.ServiceStateListener; | ||
| import io.netbird.client.tool.VPNService; | ||
|
|
||
| public class NetbirdTileService extends TileService { | ||
|
|
||
| private static final String TAG = "NetbirdTileService"; | ||
| private VPNService.MyLocalBinder mBinder; | ||
| private boolean isBound = false; | ||
| private boolean isBinding = false; | ||
| private boolean pendingClick = false; | ||
|
|
||
| private final ServiceConnection serviceConnection = new ServiceConnection() { | ||
| @Override | ||
| public void onServiceConnected(ComponentName name, IBinder binder) { | ||
| mBinder = (VPNService.MyLocalBinder) binder; | ||
| isBound = true; | ||
| isBinding = false; | ||
| mBinder.addServiceStateListener(serviceStateListener); | ||
| updateTile(); | ||
|
|
||
| if (pendingClick) { | ||
| pendingClick = false; | ||
| handleToggle(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onServiceDisconnected(ComponentName name) { | ||
| mBinder = null; | ||
| isBound = false; | ||
| isBinding = false; | ||
| updateTile(); | ||
| } | ||
| }; | ||
|
|
||
| private final ServiceStateListener serviceStateListener = new ServiceStateListener() { | ||
| @Override | ||
| public void onStarted() { | ||
| updateTile(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onStopped() { | ||
| updateTile(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(String msg) { | ||
| updateTile(); | ||
| } | ||
| }; | ||
|
|
||
| @Override | ||
| public void onStartListening() { | ||
| super.onStartListening(); | ||
| Log.d(TAG, "onStartListening"); | ||
| bindToVpnService(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onStopListening() { | ||
| super.onStopListening(); | ||
| Log.d(TAG, "onStopListening"); | ||
| unbindFromVpnService(); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| @Override | ||
| public void onClick() { | ||
| super.onClick(); | ||
| Log.d(TAG, "onClick"); | ||
|
|
||
| if (VpnService.prepare(this) != null) { | ||
| Intent intent = new Intent(this, MainActivity.class); | ||
| intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
| startActivityAndCollapse(intent); | ||
| return; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| handleToggle(); | ||
| } | ||
|
|
||
| private void handleToggle() { | ||
| if (mBinder != null) { | ||
| if (mBinder.isRunning()) { | ||
| mBinder.stopEngine(); | ||
| } else { | ||
| mBinder.runEngine(null, false); | ||
| } | ||
| } else { | ||
| pendingClick = true; | ||
| startAndRunVpnService(); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| private void bindToVpnService() { | ||
| Intent intent = new Intent(this, VPNService.class); | ||
| isBinding = bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); | ||
| if (!isBinding) { | ||
| pendingClick = false; | ||
| Log.w(TAG, "bindService failed"); | ||
| } | ||
| } | ||
|
|
||
| private void unbindFromVpnService() { | ||
| if (isBound || isBinding) { | ||
| if (mBinder != null) { | ||
| mBinder.removeServiceStateListener(serviceStateListener); | ||
| } | ||
| try { | ||
| unbindService(serviceConnection); | ||
| } catch (IllegalArgumentException e) { | ||
| Log.w(TAG, "Service not bound", e); | ||
| } | ||
| isBound = false; | ||
| isBinding = false; | ||
| mBinder = null; | ||
| } | ||
| pendingClick = false; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| private void startAndRunVpnService() { | ||
| Intent intent = new Intent(this, VPNService.class); | ||
| intent.setAction(VPNService.INTENT_ACTION_START); | ||
| startService(intent); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. startService() can throw IllegalStateException. Please use startForegroundService() and handle the INTENT_ACTION_START action in VPNService.onStartCommand() to call startForeground() immediately. |
||
| bindToVpnService(); | ||
| } | ||
|
|
||
| private boolean isVpnRunning() { | ||
| return mBinder != null && isBound && mBinder.isRunning(); | ||
| } | ||
|
|
||
| private void updateTile() { | ||
| Tile tile = getQsTile(); | ||
| if (tile == null) return; | ||
|
|
||
| boolean running = isVpnRunning(); | ||
|
|
||
| tile.setState(running ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE); | ||
| tile.updateTile(); | ||
| } | ||
| } | ||
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
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.