Detecting if device is from Samsung Galaxy family - android

Is there a reliable way of detecting if device is one from Samsung Galaxy phones? Currently, I do it in this way:
private static boolean isSamsungGalaxyN() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
PackageInfo info = getContext().getPackageManager().getPackageInfo("com.samsung.android.app.galaxyfinder", 0);
if (info != null) {
return true;
}
} catch (PackageManager.NameNotFoundException e) {
// ignored
}
}
return false;
}
So, I just check if there is such apk (which is S Finder, actually):
com.samsung.android.app.galaxyfinder
But is this method reliable and is there some better method?

Use Build.MANUFACTURER and Build.MODEL.
With Model you can get device family. For Galaxy S7 it will be SM-G903x. You need to have list of known Galaxy model names.
https://www.techwalls.com/samsung-galaxy-s7-edge-model-numbers-differences/

Related

Detect 5G NR (SA/NSA) in my Android Application

I am trying to detect 5G network. I use the telephony manager to get NETWORK_TYPE. Even if I am in 5G network coverage and my phone shows 5G, I do not get NETWORK_TYPE_NR. The NETWORK_TYPE is always 13 i.e. LTE.
The Phones Engineering service mode shows NR related data.
Is there any way to detect NR (Standalone or Non-Standalone) mode?
I also need to get the Cell Information for NR data. I use telephonyManager.getAllCellinfo(), but I never get an instance of cellinfonr.
Any help is appreciated. Thanks
I faced the same problem for a few weeks ago. In my case, I want to detect 5G network on Galaxy S20-5G but the getDataNetworkType() always return 13 NETWORK_TYPE_LTE.
Following by netmonster-core strategy, and here is the code that I extract from them to solve my problem.
public boolean isNRConnected(TelephonyManager telephonyManager) {
try {
Object obj = Class.forName(telephonyManager.getClass().getName())
.getDeclaredMethod("getServiceState", new Class[0]).invoke(telephonyManager, new Object[0]);
// try extracting from string
String serviceState = obj.toString();
boolean is5gActive = serviceState.contains("nrState=CONNECTED") ||
serviceState.contains("nsaState=5") ||
(serviceState.contains("EnDc=true") &&
serviceState.contains("5G Allocated=true"));
if (is5gActive) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Here is full detector class from netmonster-core:
(DetectorLteAdvancedNrServiceState.kt)

How to detect Samsung S10 5G is running on 5G network?

Android Q added a new network type, NETWORK_TYPE_NR for 5G which is
not available for Android Pie. Recently released Samsung S10 fully supports 5G. It can show 5G icon on the status bar when it is on the 5G network.
Is it possible for a third-party app to know if Android Pie device on a 5G network or not?
Any help will be appreciated.
The following link is the definition for the new network type. It is not available on the Android Pie branch.
Source code for Pie release
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/pie-release-2/telephony/java/android/telephony/TelephonyManager.java
The latest source code that has NETWORK_TYPE_NR
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/telephony/java/android/telephony/TelephonyManager.java#2375
I believe they backported the code from Q to Pie as the logic for 5G was implemented at the end of last year in Q (alpha).
So when using
TelephonyManager.getNetworkType()
you will likely get
20 (5G)
EDIT
As per comment below: The network type will be 13 so it doesn't solve the thing.
EDIT
Try using reflection
static boolean isNRConnected(TelephonyManager telephonyManager) {
try {
Object obj = Class.forName(telephonyManager.getClass().getName())
.getDeclaredMethod("getServiceState", new Class[0]).invoke(telephonyManager, new Object[0]);
Method[] methods = Class.forName(obj.getClass().getName()).getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("getNrStatus") || method.getName().equals("getNrState")) {
method.setAccessible(true);
return ((Integer) method.invoke(obj, new Object[0])).intValue() == 3;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
There is an official documentation to detect 5G on Android 11.
https://developer.android.com/about/versions/11/features/5g#detection
Call TelephonyManager.listen(), passing in LISTEN_DISPLAY_INFO_CHANGED, to determine if the user has a 5G network connection.
as #Hunter suggested you need to use "listen" from telephony manager but if it's api 30+ you need to have READ_PHONE permission granted and listen for LISTEN_DISPLAY_INFO_CHANGED and override onDisplayInfoChanged from PhoneStateListener
(context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).listen(customPhoneStateListener,PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)
The listener should be something like this:
private class CustomPhoneStateListener : PhoneStateListener() {
override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
super.onDisplayInfoChanged(telephonyDisplayInfo)
when (telephonyDisplayInfo.overrideNetworkType) {
//5G
OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO,
OVERRIDE_NETWORK_TYPE_NR_NSA,
OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE -> setNetworkChange(NETWORK_TYPE_NR)
OVERRIDE_NETWORK_TYPE_LTE_CA -> {
setNetworkChange(19) //LTE+
}
else -> setNetworkChange(telephonyDisplayInfo.networkType)
}
} else {
setNetworkChange(telephonyDisplayInfo.networkType)
}
}
}
Link: https://developer.android.com/about/versions/11/features/5g#detection
Couldn't add this as a comment, but as #Pavel Machala said, looking at the ServiceState class in the AOSP yields the following:
/**
* Get the NR 5G status of the mobile data network.
* #return the NR 5G status.
* #hide
*/
public #NRStatus int getNrStatus() {
final NetworkRegistrationState regState = getNetworkRegistrationState(
NetworkRegistrationState.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
if (regState == null) return NetworkRegistrationState.NR_STATUS_NONE;
return regState.getNrStatus();
}
I have extracted the ServiceState.java from SM-G977N firmware and it confirms that they have added
ServiceState.getNrStatus()
5G(NR) is active is if NetworkRegistrationState.NR_STATUS_CONNECTED = 3;

How to find out whether android device has cellular radio module?

How can I find out for sure that device really has gsm, cdma or other cellular network equipment (not just WiFi)?
I don't want to check current connected network state, because device can be offline in the moment.
And I don't want to check device id via ((TelephonyManager) act.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId() because some devices would just give you polymorphic or dummy device ID.
Actualy, I need to check cell equipment exactly for skipping TelephonyManager.getDeviceId and performing Settings.Secure.ANDROID_ID check on those devices that don't have cellular radio. I have at least one tablet (Storage Options Scroll Excel 7") which returns different IMEIs every time you ask it, although it should return null as it has no cell radio (the same situation here: Android: getDeviceId() returns an IMEI, adb shell dumpsys iphonesubinfo returns Device ID=NULL). But I need to have reliable device id that is the same every time I ask.
I'd be glad to hear your thoughts!
If you're publishing in the store, and you want to limit your application only being visible to actual phones, you could add a <uses-feature> into your manifest that asks for android.hardware.telephony. Check out if that works for you from the documentation.
Just in case somebody needs complete solution for this:
Reflection is used because some things may not exist on some firmware versions.
MainContext - main activity context.
static public int getSDKVersion()
{
Class<?> build_versionClass = null;
try
{
build_versionClass = android.os.Build.VERSION.class;
}
catch (Exception e)
{
}
int retval = -1;
try
{
retval = (Integer) build_versionClass.getField("SDK_INT").get(build_versionClass);
}
catch (Exception e)
{
}
if (retval == -1)
retval = 3; //default 1.5
return retval;
}
static public boolean hasTelephony()
{
TelephonyManager tm = (TelephonyManager) Hub.MainContext.getSystemService(Context.TELEPHONY_SERVICE);
if (tm == null)
return false;
//devices below are phones only
if (Utils.getSDKVersion() < 5)
return true;
PackageManager pm = MainContext.getPackageManager();
if (pm == null)
return false;
boolean retval = false;
try
{
Class<?> [] parameters = new Class[1];
parameters[0] = String.class;
Method method = pm.getClass().getMethod("hasSystemFeature", parameters);
Object [] parm = new Object[1];
parm[0] = "android.hardware.telephony";
Object retValue = method.invoke(pm, parm);
if (retValue instanceof Boolean)
retval = ((Boolean) retValue).booleanValue();
else
retval = false;
}
catch (Exception e)
{
retval = false;
}
return retval;
}

Phone nr access works in phone but crashes on tablet even with try/catch

I have an Android phone app that accesses the phone number for registration info like this:
String thisPhoneNumber = "0";
try {
TelephonyManager tMgr=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
thisPhoneNumber = tMgr.getLine1Number(); }
catch (Exception e) { }
It works fine on the phone. A friend tried it on the tablet (a recent one) and the app crashes. If I comment out the 4 lines of try/catch it works fine so it looks like the crash must be on the getSystemService call.
In a bit of searching stackoverflow, it sounds like code should run on either platform but that's not what I'm seeing.
Shouldn't the try/catch prevent the crash and allow the exception to be handled?
Is there some other way to allow the getSystemService to execute without crashing on the tablet? Or do I have to not make that call on the tablet?
check with if statement check if of the variables in those 2-3 lines are null, and stop executing the code if so, for example
try
{
TelephonyManager tMgr=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
if(tMgr!=null)
thisPhoneNumber = tMgr.getLine1Number(); }
cath(Exception o)
{}
or you can try to find if the device is a tablet or phone(phone or non phone device)
and then execute two different statements in if code
if(phone) { the code}
or you can have abstract class whit a function find phone number
and do some think like
if(device is phone)
{
apstractClass=new phoneImplementation();
} else
{
apstractClass= new nonPhoneImplementation();if (((TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number()
== null)
{
// no phone
} enter code here
}
thisPhoneNumber=apstarctClass.getPhoneNumber()
where phoneImplementation().getPhoneNumber() will have your code, the code from the question and nonPhoneImplementation().getPhoneNumber() will return 0
you can check with this if a device has phone capabilities
if (((TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number()
== null)
{
// no phone
}
My opinion is that this will solve your problem:
String thisPhoneNumber = "0";
try
{
if (((TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number()
!= null)
{
thisPhoneNumber = tMgr.getLine1Number();
} }
catch(Exception o)
{}

How to check telephony and camera availability for SDK version < 5

Standard way of checking camera and telephony hardware availability works only since SDK >= 5:
PackageManager pm = this.getPackageManager();
boolean hasTelephony=pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
boolean hasCamera=pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
My problem that I need to runtime define availability of telephony and camera in SDK 3 (Android 1.5)
Any ideas?
P.S. I understand that Android 1.5 is very outdated, but still I do have bunch of customers running these devices, so I have to keep compatibility with them.
Well, I have found solution - very odd but it's working.
Basically method tries to get telephony service if it's null - it returns false, if it's not null (e.g. for HTC Flyer TelephonyManager is not null) method tries to run PackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) using reflection, since this method is not available for old versions of SDK.
Here is a code:
private Boolean hasTelephony;
public boolean hasTelephony()
{
if(hasTelephony==null)
{
TelephonyManager tm=(TelephonyManager )this.getSystemService(Context.TELEPHONY_SERVICE);
if(tm==null)
{
hasTelephony=new Boolean(false);
return hasTelephony.booleanValue();
}
if(this.getSDKVersion() < 5)
{
hasTelephony=new Boolean(true);
return hasTelephony;
}
PackageManager pm = this.getPackageManager();
Method method=null;
if(pm==null)
return hasCamera=new Boolean(false);
else
{
try
{
Class[] parameters=new Class[1];
parameters[0]=String.class;
method=pm.getClass().getMethod("hasSystemFeature", parameters);
Object[] parm=new Object[1];
parm[0]=new String(PackageManager.FEATURE_TELEPHONY);
Object retValue=method.invoke(pm, parm);
if(retValue instanceof Boolean)
hasTelephony=new Boolean(((Boolean )retValue).booleanValue());
else
hasTelephony=new Boolean(false);
}
catch(Exception e)
{
hasTelephony=new Boolean(false);
}
}
}
return hasTelephony;
}
More or less the same approach is workable for checking of camera availability

Categories

Resources