-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add getDefaultProfile and setDefaultProfile methods to API #1858
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
7c071ba
83ef0f1
c1dedd0
da38b01
0a7247c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| import android.content.Intent; | ||
| import android.content.IntentFilter; | ||
| import android.content.ServiceConnection; | ||
| import android.content.SharedPreferences; | ||
| import android.net.VpnService; | ||
| import android.os.Binder; | ||
| import android.os.Build; | ||
|
|
@@ -37,6 +38,7 @@ | |
| import de.blinkt.openvpn.core.ConnectionStatus; | ||
| import de.blinkt.openvpn.core.IOpenVPNServiceInternal; | ||
| import de.blinkt.openvpn.core.OpenVPNService; | ||
| import de.blinkt.openvpn.core.Preferences; | ||
| import de.blinkt.openvpn.core.ProfileManager; | ||
| import de.blinkt.openvpn.core.VPNLaunchHelper; | ||
| import de.blinkt.openvpn.core.VpnStatus; | ||
|
|
@@ -109,6 +111,10 @@ public void onCreate() { | |
|
|
||
| } | ||
|
|
||
| private APIVpnProfile newAPIVpnProfile(VpnProfile vp) { | ||
| return new APIVpnProfile(vp.getUUIDString(), vp.mName, vp.mUserEditable, vp.mProfileCreator); | ||
| } | ||
|
|
||
| private final IOpenVPNAPIService.Stub mBinder = new IOpenVPNAPIService.Stub() { | ||
|
|
||
| @Override | ||
|
|
@@ -121,7 +127,7 @@ public List<APIVpnProfile> getProfiles() throws RemoteException { | |
|
|
||
| for (VpnProfile vp : pm.getProfiles()) { | ||
| if (!vp.profileDeleted) | ||
| profiles.add(new APIVpnProfile(vp.getUUIDString(), vp.mName, vp.mUserEditable, vp.mProfileCreator)); | ||
| profiles.add(newAPIVpnProfile(vp)); | ||
| } | ||
|
|
||
| return profiles; | ||
|
|
@@ -232,7 +238,7 @@ public APIVpnProfile addNewVPNProfileWithExtras(String name, boolean userEditabl | |
| vp.addChangeLogEntry("AIDL API created profile"); | ||
| pm.saveProfile(ExternalOpenVPNService.this, vp); | ||
| pm.saveProfileList(ExternalOpenVPNService.this); | ||
| return new APIVpnProfile(vp.getUUIDString(), vp.mName, vp.mUserEditable, vp.mProfileCreator); | ||
| return newAPIVpnProfile(vp); | ||
| } catch (IOException e) { | ||
| VpnStatus.logException(e); | ||
| return null; | ||
|
|
@@ -330,6 +336,36 @@ public void resume() throws RemoteException { | |
| mService.userPause(false); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public APIVpnProfile getDefaultProfile() throws RemoteException { | ||
| ProfileManager pm = ProfileManager.getInstance(getBaseContext()); | ||
|
Owner
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. Missing permission check.
Author
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. Done |
||
| SharedPreferences prefs = Preferences.getDefaultSharedPreferences(getBaseContext()); | ||
| String profileUUID = prefs.getString("alwaysOnVpn", null); | ||
| if (profileUUID == null) { | ||
| return null; | ||
| } | ||
| VpnProfile vp = ProfileManager.get(getBaseContext(), profileUUID); | ||
| if (vp.checkProfile(getApplicationContext()) != R.string.no_error_found) { | ||
| VpnStatus.logInfo("Default profile is currently set to unknown UUID " + profileUUID); | ||
| return null; | ||
| } | ||
| APIVpnProfile result = newAPIVpnProfile(vp); | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public void setDefaultProfile(String profileUUID) throws RemoteException { | ||
| mExtAppDb.checkOpenVPNPermission(getPackageManager()); | ||
| ProfileManager pm = ProfileManager.getInstance(getBaseContext()); | ||
| VpnProfile vp = ProfileManager.get(getBaseContext(), profileUUID); | ||
| if (vp.checkProfile(getApplicationContext()) != R.string.no_error_found) | ||
| throw new RemoteException(getString(vp.checkProfile(getApplicationContext()))); | ||
| SharedPreferences prefs = Preferences.getDefaultSharedPreferences(getBaseContext()); | ||
| SharedPreferences.Editor editor = prefs.edit(); | ||
| editor.putString("alwaysOnVpn", vp.getUUIDString()); | ||
| editor.apply(); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.content.ServiceConnection; | ||
| import android.content.SharedPreferences; | ||
| import android.os.Bundle; | ||
| import android.os.IBinder; | ||
| import android.os.RemoteException; | ||
|
|
@@ -19,7 +20,9 @@ | |
| import de.blinkt.openvpn.VpnProfile; | ||
| import de.blinkt.openvpn.core.IOpenVPNServiceInternal; | ||
| import de.blinkt.openvpn.core.OpenVPNService; | ||
| import de.blinkt.openvpn.core.Preferences; | ||
| import de.blinkt.openvpn.core.ProfileManager; | ||
| import de.blinkt.openvpn.core.VpnStatus; | ||
|
|
||
| public class RemoteAction extends Activity { | ||
|
|
||
|
|
@@ -98,6 +101,18 @@ private void performAction() throws RemoteException { | |
| startActivity(startVPN); | ||
| } | ||
| break; | ||
| case ".api.SetDefaultVPN": | ||
| String defaultVpnName = intent.getStringExtra(EXTRA_NAME); | ||
|
||
| VpnProfile defaultProfile = ProfileManager.getInstance(this).getProfileByName(defaultVpnName); | ||
| if (defaultProfile == null) { | ||
| Toast.makeText(this, String.format("Vpn profile %s from API call not found", defaultVpnName), Toast.LENGTH_LONG).show(); | ||
| } else { | ||
| SharedPreferences prefs = Preferences.getDefaultSharedPreferences(this); | ||
| SharedPreferences.Editor editor = prefs.edit(); | ||
| editor.putString("alwaysOnVpn", defaultProfile.getUUIDString()); | ||
| editor.apply(); | ||
| } | ||
| break; | ||
| } | ||
| finish(); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be named rather something like APIVPNProfileFromVPNProfile or even better have a static method in APIVpnProfile like
constructFromVPNProfileUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The static method would be ideal except then there would have to be a dependency in APIVPNProfile on the implementation class VPNProfile, though. So I renamed to apiVPNProfileFromVPNProfile, in order to have the method name start with a lower case letter. (Google style guide would have us write use "Vpn" and "vpn" everywhere but we're not following that in any case). Not attached to the name.
I did notice that the two copies of APIVpnProfile.java were out of sync so I updated those. Notably, the copyright notices were different.