Skip to content

Commit

Permalink
Populate Device.Geo.Country property (#477)
Browse files Browse the repository at this point in the history
Country from telephonyManager-locale-reverseGeocoding
  • Loading branch information
GenarC authored Aug 15, 2022
1 parent 342ad3c commit bf8d878
Showing 1 changed file with 46 additions and 0 deletions.
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 "";
}
}

0 comments on commit bf8d878

Please sign in to comment.