Created
March 30, 2018 11:37
-
-
Save cliffgr/f1d9b016d26327e3fc11e4064fd09fcc to your computer and use it in GitHub Desktop.
dsadasddasasd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wappier.wappierSDK.utils; | |
import android.content.Context; | |
import android.content.pm.PackageManager; | |
import android.location.Address; | |
import android.location.Geocoder; | |
import android.location.Location; | |
import android.location.LocationManager; | |
import android.os.Build; | |
import android.support.v4.content.ContextCompat; | |
import android.telephony.TelephonyManager; | |
import com.wappier.wappierSDK.Constants; | |
import com.wappier.wappierSDK.Utils; | |
import com.wappier.wappierSDK.Wappier; | |
import java.util.List; | |
import java.util.Locale; | |
/** | |
* Created by dioannou on 27/11/2017. | |
*/ | |
public class Country { | |
public LocationManager locationManager; | |
private TelephonyManager telephonyManager; | |
private Geocoder geocoder; | |
private String mCountry; | |
private Context mContext; | |
private Location mLocation; | |
public Country(Context context) { | |
this.mContext = context; | |
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); | |
this.telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); | |
this.geocoder = new Geocoder(context, Locale.getDefault()); | |
} | |
public LocationManager getLocationManager() { | |
return locationManager; | |
} | |
private Locale getLocale() { | |
mLocation = null; | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
if (Utils.isPermissionGranted(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION)) { | |
try { | |
mLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
if (mLocation == null) { | |
mLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); | |
} | |
} catch (SecurityException e) { | |
mLocation=null; | |
} | |
} else if (Utils.isPermissionGranted(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION)) { | |
try { | |
mLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); | |
} catch (SecurityException e) { | |
mLocation = null; | |
} | |
} | |
} else { | |
try { | |
mLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
if (mLocation == null) { | |
mLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); | |
} | |
} catch (SecurityException e) { | |
try { | |
mLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); | |
}catch (SecurityException e1) { | |
mLocation = null; | |
} | |
} | |
} | |
if (mLocation != null) { | |
List<Address> addresses; | |
try { | |
addresses = geocoder.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(), 1); | |
if (addresses != null && !addresses.isEmpty()) { | |
mCountry = addresses.get(0).getCountryCode(); | |
if (mCountry != null) { | |
return new Locale("", mCountry); | |
} | |
} | |
} catch (Exception e) { | |
//e.printStackTrace(); | |
} | |
} | |
mCountry = getCountryBasedOnSimCardOrNetwork(telephonyManager); | |
if (mCountry != null) { | |
return new Locale("",mCountry); | |
} | |
return null; | |
} | |
public String getTwoDigitCountry(){ | |
Locale locale = getLocale(); | |
if (locale!=null) { | |
return getLocale().getCountry(); | |
} | |
return Constants.COUNTRY_CODE; | |
} | |
public Location getLocation() { | |
Location location = new Location(""); | |
double longitude = (mLocation == null) ? 0 : mLocation.getLongitude(); | |
double latitude = (mLocation == null) ? 0 : mLocation.getLatitude(); | |
location.setLongitude(longitude); | |
location.setLatitude(latitude); | |
return location; | |
} | |
private static String getCountryBasedOnSimCardOrNetwork(TelephonyManager telephonyManager) { | |
if (telephonyManager != null) { | |
try { | |
final String simCountry = telephonyManager.getSimCountryIso(); | |
if (simCountry != null && simCountry.length() == 2) { // SIM country code is available | |
return new Locale("", simCountry).getCountry(); | |
} else if (telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable) | |
String networkCountry = telephonyManager.getNetworkCountryIso(); | |
if (networkCountry != null && networkCountry.length() == 2) { // network country code is available | |
return new Locale("", networkCountry).getCountry(); | |
} | |
} | |
} catch (Exception e) { | |
} | |
return null; | |
} | |
return null; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment