Skip to content
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

Populate device.geo.country property #477

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@

package org.prebid.mobile.rendering.networking.parameters;

import static org.prebid.mobile.PrebidMobile.getApplicationContext;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.telephony.TelephonyManager;

import org.prebid.mobile.LogUtil;
import org.prebid.mobile.rendering.models.openrtb.bidRequests.geo.Geo;
import org.prebid.mobile.rendering.sdk.ManagersResolver;
import org.prebid.mobile.rendering.sdk.deviceData.managers.DeviceInfoManager;
import org.prebid.mobile.rendering.sdk.deviceData.managers.LocationInfoManager;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class GeoLocationParameterBuilder extends ParameterBuilder {

public static final int LOCATION_SOURCE_GPS = 1;
Expand Down Expand Up @@ -54,6 +66,40 @@ private void setLocation(AdRequestInput adRequestInput, LocationInfoManager loca
geo.lat = latitude.floatValue();
geo.lon = longitude.floatValue();
geo.type = LOCATION_SOURCE_GPS;
try {

geo.country = getTelephonyCountry(locationInfoManager.getContext());

if(geo.country.equals("")){
Locale locale = getApplicationContext().getResources().getConfiguration().locale;
geo.country = locale.getISO3Country();
}

if(geo.country.equals("")){
Geocoder geoCoder = new Geocoder(locationInfoManager.getContext());
List<Address> list = geoCoder.getFromLocation(locationInfoManager.getLatitude(), locationInfoManager.getLongitude(), 1);
geo.country = list.get(0).getCountryCode();
}

}catch(Throwable thr){
LogUtil.debug("Error getting country code");
}
}
}

private String getTelephonyCountry(Context ctx){
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

if(tm != null) {
String simCountry = tm.getSimCountryIso().toUpperCase();
String networkCountry = tm.getNetworkCountryIso().toUpperCase();

if (!simCountry.equals("")) {
return simCountry;
} else if (!networkCountry.equals("")) {
return networkCountry;
}
}
return "";
}
}