android - How to add own VPN settings to system VPN settings page? -
there system setting vpn. going add additional vpn service based on vpnservice class. see there method setconfigureintent
looks similar need not see examples of usage.
public vpnservice.builder setconfigureintent (pendingintent intent)
added in api level 14 set
pendingintent
activity users configure vpn connection. if not set, button configure not shown in system-managed dialogs.
vpn settings pages here:
, .
actually need add button system vpn settings clicking on custom dialog vpn specific settings shown.
here starting point @shoe rat proposed, using java reflection:
package com.nikola.despotoski.whatever; import java.lang.reflect.constructor; import java.lang.reflect.field; import java.lang.reflect.invocationtargetexception; import java.lang.reflect.method; import java.util.hashmap; import java.util.iterator; import java.util.map; import java.util.set; public class vpnsetter { private static map<string , class<?>> getmappedfields(){ map<string , class<?>> fieldsandtypes = new hashmap<string, class<?>>(); fieldsandtypes.put("name", string.class); // 0 fieldsandtypes.put("type" , int.class); // 1 fieldsandtypes.put("server", string.class); // 2 fieldsandtypes.put("username", string.class); fieldsandtypes.put("password", string.class); fieldsandtypes.put("dnsservers", string.class); fieldsandtypes.put("searchdomains", string.class); fieldsandtypes.put("routes", string.class); fieldsandtypes.put("mppe", boolean.class); fieldsandtypes.put("l2tpsecret", string.class); fieldsandtypes.put("ipsecidentifier", string.class); fieldsandtypes.put("ipsecsecret", string.class); fieldsandtypes.put("ipsecusercert", string.class); fieldsandtypes.put("ipseccacert", string.class); fieldsandtypes.put("savelogin", boolean.class); return fieldsandtypes; } public static final set<string> vpn_profile_keys = getmappedfields().keyset(); // contains keys quicker generation of key-value map each public static void addvpnprofile(string vpnprofilekey, map<string, object> values) throws classnotfoundexception, nosuchfieldexception, illegalargumentexception, illegalaccessexception, invocationtargetexception, instantiationexception, nosuchmethodexception{ class<?> vpnsettings = class.forname("com.android.settings.vpn2.vpnsettings"); class<?>[] privatevpnsettingsclasses = vpnsettings.getdeclaredclasses(); class<?> vpnpreference = null; class<?> vpnprofileclass = class.forname("com.android.settings.vpn2.vpnprofile"); for(class<?> priv :privatevpnsettingsclasses ){ if(priv.getname().equals("vpnpreference")){ vpnpreference = priv; break; } } field vpnprofilefromvpnpreferencefield = vpnpreference.getdeclaredfield("mprofile"); vpnprofilefromvpnpreferencefield.setaccessible(true); object vpnprofile = vpnprofilefromvpnpreferencefield.get(vpnprofileclass); constructor<?> constructor = vpnprofilefromvpnpreferencefield.getclass().getconstructors()[0]; constructor.setaccessible(true); vpnprofile = constructor.newinstance(vpnprofilekey);//creating new instance of vpnprofile class map<string, class<?>> vpnprofilemap = getmappedfields(); iterator<string> profilekeysiterator = vpnprofilemap.keyset().iterator(); while(profilekeysiterator.hasnext()){ string key = profilekeysiterator.next(); field field = vpnprofile.getclass().getfield(key); field.setaccessible(true); if(vpnprofilemap.get(key).equals(string.class) && values.get(key)!=null){ string s = new string(); field.set(s, "value");//change }else if(vpnprofilemap.get(key).equals(boolean.class) && values.get(key)!=null){ int = 0; field.setint(i, 1111111);// change }else if(values.get(key)!=null){ boolean b = false; field.setboolean(b, true);// change } } vpnsettings = class.forname("com.android.settings.vpn.vpnsettings"); //time add settings method addprofilemethod = vpnsettings.getdeclaredmethod("addprofile", vpnprofile.getclass()); addprofilemethod.setaccessible(true); addprofilemethod.invoke(vpnsettings, vpnprofile); } }
i didn't have time test it, surely give starting point. i'm not sure root access needed accessing hidden api. please bare in mind might throw securityexceptions or illegalaccessexception.
Comments
Post a Comment