Programmatically setting up a vpn on Android - android

I have found the following code to establish a new vpn programmatically but I do not know how to use it to create my app
VpnService service = context.getSystemService(VPN_SERVICE);
VpnProfile profile = VpnProfile.create(L2TP_PROFILE);
profile.setName(myServerName);
profile.setServerName(myServerAddress);
profile.setRouteList("192.168.1.0/255.255.255.0,192.168.10.0/255.255.255.0");
service.connect(profile, "myUserName", "myPassword");
service.setNotificationIntent(myIntent);
can anyone please help me with a sample code?
Is it even possible to to achieve dis?

Note: this answer is dated and may now be inaccurate.
Take a look at this question: How to configure VPN programmatically?
While yours isn't necessarily a duplicate, the answer is likely the same, in that you're going to need to expose the hidden API (if it's available) or worse, be dependent on the device being rooted.
Understandably, programmatically creating a VPN connection poses an inherent security risk for the end user, and shouldn't be implemented without consideration.

Related

How to get the physical channel configuration of the cellphone via programming

I want to get the PCI of both primary serving cell and secondary serving cell in 5G NSA, but it seems that I cannot get what I want using getAllCellInfo(). It seems that I need to parse the physical channel configuration as below:
{{mConnectionStatus=PrimaryServing,...,mRat=LTE,...,mPhysicalCellId=123},
{mConnectionStatus=SecondaryServing,...,mRat=NR,...}
Does anyone know how to get the (real-time) configuration? Or can I get the PCIs in another way? Thanks very much!
About PhysicalChannelConfig
The only possible way how to obtain PhysicalChannelConfig is via TelephonyManager.registerTelephonyCallback method. You can for example pass an instance of TelephonyCallback.PhysicalChannelConfigListener and you'll start obtaining what you need.
Please note that permission Manifest.permission.READ_PRECISE_PHONE_STATE is required, so your app needs to be a system app of carrier-privileged app.
There were some attempts to adjust this protection level, current status can be seen here. But as of Android 12 there's no way how to get PhysicalChannelConfig if you are a regular developer.
About PCIs
You can sometimes get PCIs of serving LTE and NR NSA cells via getAllCellInfo() as you mentioned. Sometimes there's one instance of CellInfoNr with PCI is present. This behaviour is device-specific.
Generally speaking - Android does not provide any official API you request.

Read Preferred Network Type Setting

I am trying to read the Preferred network type setting from the device. But nowhere android API's are available.
Use case:
Trying to read the Preferred network type and connected network type so that if the device has LTE enabled and the user is forcefully switched back to the lower network(3G,2G); then there should be a notification sent to the user.
I have checked the system setting code, But it's deprecated.
Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.System.NETWORK_PREFERENCE);
Is there any alternate way to read the system secured settings(By reflection?).
And Also is it possible to write back the setting with the user permission?
Help is much appreciated.
I think the right code is:
Settings.Global.getString(context.getContentResolver(),Settings.Global.NETWORK_PREFERENCE)

How to block ads in a 3rd party app: Android

I am building an operating system for children; the app is a launcher. From this app, children are allowed to use 3rd party apps, except requirements are that the ads should be blocked because some ads are inappropriate for our 3-5 year old targeted audience. Without actually owning these 3rd party apps, I have not thought of too many options for accomplishing this. The only thing I could think of is to turn off network connection while using these apps. Without a network connection, these 3rd party apps will not be able to display their ads. However, I see a huge drawback to this solution. Many (dare I say most) apps are meant to work with an active network connection, and without one these apps do not function.
Can you think of a better way of doing this? Is there a library that exists that I do not know of, or method for doing this that you can enlighten me on?
Thank you in advance.
I have found possible solution to this for other developers building operating systems for children. Adblockplusandroid is pretty useful, there is no root required.
https://github.com/adblockplus/adblockplusandroid
Additionally, there are some ways of changing/setting Proxy using Wificonfiguration like
WifiConfiguration config = new WifiConfiguration(configuration);
config.ipAssignment = WifiConfiguration.IpAssignment.UNASSIGNED;
config.proxySettings = WifiConfiguration.ProxySettings.STATIC;
config.linkProperties.clear();
config.linkProperties.setHttpProxy(new ProxyProperties("127.0.0.1", port, ""));
127.0.0.1 targets ads.

Enable 3G and WiFi Simultaneously

Does anyone know if its possible (programatically) to enable both 3G and WiFi to be used simultaneously, both receiving and sending packets?
I have seen various other questions on here, but with the Tethering ability inside Android now, I was wondering if this is a possibility? This has to be on a standard/stock device, and no modifications via root to the OS.
Thanks
Adam
I don't believe it is, as soon as wifi is turned on, 3g will automatically disconnect. As far as I know there is no way round it.
The only way you can achieve this is by using a APN name with the HttpConnection. This is possible in Java ME, Please visit this answer, however i have never tried it.
You need to check for the active connection, if not active first one the switch to the next connection.
public void shutup(){
SuDroid cmd = new SuDroid();
cmd.sh.runWaitFor("svc wifi enable");
cmd.sh.runWaitFor("svc data enable");
cmd.sh.runWaitFor("svc data prefer");
}
Using SuDroido

Change network selection mode programmatically

I'm trying (wondering if it's even possible) to write an app, that would change the network selection mode automatically, based on some criteria. E.g. change the network operator from Vodafone to T-Mobile (assuming that the SIM card registration will succeed, but I'm not worried about it atm)
Unfortunately, I can't seem to find any way in the API to do it. Anyone has any idea?
I assume, since it's not in the public APIs, there might still be a way to do it, if the phone is rooted. Is that true? If so, where should I look?
Thanks in advance
Sorry but you can't.
You can have a look into the TelephonyManager .
You can know the current operator: getSimOperator(Name) / getNetworkOperator(Name).
You can also check this thread saying "I learn that for the sake of security there aren't any public APIs to manage this so the only option is to send the user to the system PreferenceScreen within my app."
How about using android.telephony.CarrierConfigManager? I read about it on https://developer.android.com/reference/android/telephony/CarrierConfigManager.html and it seems to allow you to change alot of carrier-specific parameters, although the app must be signed with the certificate that has a matching signature to one on the SIM, so it can usually only be implemented by the carrier issuing the SIM. See also https://source.android.com/devices/tech/config/carrier.
I havent found an actual method to actively switch carrier, but if anywhere, I'd expect it to be there.

Categories

Resources