Skip to content

Commit

Permalink
fix network not supported issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilitate committed May 19, 2016
1 parent 7e61f3f commit 27f8683
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 194 deletions.
142 changes: 10 additions & 132 deletions app/src/main/java/com/vasilitate/vapp/sdk/Vapp.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.vasilitate.vapp.sdk.exceptions.InvalidVappNetworkException;
import com.vasilitate.vapp.sdk.exceptions.InvalidVappNumberException;
import com.vasilitate.vapp.sdk.exceptions.InvalidVappProductException;
import com.vasilitate.vapp.sdk.exceptions.NetworkNotSupportedException;
import com.vasilitate.vapp.sdk.exceptions.VappException;

import java.io.BufferedReader;
Expand All @@ -28,7 +27,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
Expand All @@ -47,7 +45,6 @@ public abstract class Vapp {

private static List<VappProduct> productList;
private static List<String> deliveryNumbers;
private static Map<String, String> billingRouteMap;

// All Vapp members are static so no need for a constructor.
private Vapp() {
Expand Down Expand Up @@ -93,7 +90,6 @@ public static synchronized void initialise(Context context,

VappConfiguration.setTestMode(context, testMode);
VappConfiguration.setCancellableProducts(context, cancellableProducts);
initialiseBillingRouteLookup(context);

if (products == null || products.isEmpty()) {
throw new InvalidVappProductException("No VAPP! products setup");
Expand Down Expand Up @@ -214,24 +210,6 @@ public static String getOriginatingNetworkCountry(Context context) throws VappEx
}


/**
* Checks if the Originating Network for the current device is declared within the network
* list used to initialise VAPP!
*
* @param context the current context
* @return true if the current device's network is supported.
* @throws VappException Vapp exception - see its message for details.
*/
public static boolean isOriginatingNetworkSupported(Context context) throws VappException {
checkIfInitialised();
try {
return !TextUtils.isEmpty(getBillingRoute(context));
}
catch (VappException e) {
return false;
}
}

/**
* Gets the number of SMS messages that have already been sent for an incomplete payment.
*
Expand Down Expand Up @@ -349,20 +327,6 @@ public static VappProduct getProductBeingPurchased(Context context) throws VappE
return null;
}


// /**
// * The mobile number to which the SMS's will be sent for the current Originating
// * Network.
// *
// * @param context the current context
// * @return The mobile number to which the SMS's will be sent
// *
// * @exception VappException Vapp exception - see its message for details.
// */
// public static String getBillingRouteNumber(Context context) throws VappException {
// return getBillingRoute(context);
// }

/**
* Displays the Vapp Payment screen
*
Expand All @@ -387,11 +351,16 @@ public static boolean showVappPaymentScreen(Context context,
return false;
}

if (!Vapp.isOriginatingNetworkSupported(context)) {
String originatingNetwork = Vapp.getOriginatingNetwork(context);
showErrorMessage(context, context.getString(R.string.vapp_network_not_supported, originatingNetwork));
return false;
}
// if (!Vapp.isOriginatingNetworkSupported(context)) { // TODO check if needed?
// String originatingNetwork = Vapp.getOriginatingNetwork(context);
//
// if (originatingNetwork == null) {
// originatingNetwork = DEFAULT_ERR_CODE; // default err code
// }
//
// showErrorMessage(context, context.getString(R.string.vapp_network_not_supported, originatingNetwork));
// return false;
// }

Intent intent = new Intent(context, VappProgressActivity.class);
intent.putExtra(VappActions.EXTRA_PRODUCT_ID, product.getProductId());
Expand Down Expand Up @@ -439,49 +408,6 @@ static String getOriginatingNetwork(Context context) throws VappException {
return getDeviceStateContract(context).getOriginatingNetwork();
}

/**
* Gets the billing route for a purchase
*
* @param context the current context
* @return the billing route as a string
* @throws VappException Vapp exception - see its message for details.
*/
static String getBillingRoute(Context context) throws VappException {

checkIfInitialised();

String originatingNetwork = getOriginatingNetwork(context);

if (TextUtils.isEmpty(originatingNetwork)) {
throw new NetworkNotSupportedException("No Originating Network!");
}

// e.g. 234001, 23401 & 2341 all should match against 234/1

String normalisedNetwork = originatingNetwork.substring(0, 3) + "/";
String mnc = originatingNetwork.substring(3);

try {

Pattern leadingZeros = Pattern.compile("^([0]+)[0-9]");
Matcher matcher = leadingZeros.matcher(mnc);

if (matcher.find()) {
if (matcher.groupCount() == 1) {

String mncLeadingZeros = matcher.group(1);
mnc = mnc.substring(mncLeadingZeros.length());
}
}
}
catch (Exception e) {
throw new InvalidVappNetworkException(originatingNetwork);
}
normalisedNetwork += mnc;

return billingRouteMap.get(normalisedNetwork);
}

static VappSms generateSmsForProduct(Context context,
int smsCount,
int smsIndex) throws VappException {
Expand Down Expand Up @@ -632,52 +558,4 @@ private static void checkIfInitialised() throws VappException {
}
}

private static void initialiseBillingRouteLookup(Context context) {

if (billingRouteMap == null) {

billingRouteMap = new HashMap<>();

// Note, future releases may need to support more than one 'billing route' for
// multiple destination telco's.
addBillingRouteToMap(context, R.array.billing_route_A, "A");
}
}

private static void addBillingRouteToMap(Context context, int mccMncArrayResId, String billingRoute) {

if (!TextUtils.isEmpty(billingRoute)) {

String[] mccMncs = context.getResources().getStringArray(mccMncArrayResId);

// format is mcc, mcc, ... / mnc, mnc,....

for (String mccMnc : mccMncs) {

String[] mccAndMnc = mccMnc.split("/");

if (mccAndMnc.length == 2) {

String[] mccs = mccAndMnc[0].split(",");
String[] mncs = mccAndMnc[1].split(",");

if (mccs.length > 0 && mncs.length > 0) {

for (String mcc : mccs) {
for (String mnc : mncs) {

String combination = mcc.trim() + "/" + mnc.trim();
if (billingRouteMap.containsKey(combination)) {
throw new VappException("MCC/MNC combination is assigned to more than one billing route: " + combination);
}
else {
billingRouteMap.put(combination, billingRoute);
}
}
}
}
}
}
}
}
}
116 changes: 59 additions & 57 deletions app/src/main/res/values/mcc_mnc_codes.xml
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="billing_route_A">
<item>603/02</item>
<item>363/01</item>
<item>505/07, 03</item>
<item>232/03, 04, 07, 15</item>
<item>426/02</item>
<item>470/03</item>
<item>470/01</item>
<item>342/600</item>
<item>724/01, 06, 10, 11, 19, 23</item>
<item>724/30, 31</item>
<item>625/01</item>
<item>630/86</item>
<item>368/01</item>
<item>230/01</item>
<item>740/02</item>
<item>602/03</item>
<item>208/01, 02, 29, 91</item>
<item>262/01, 06</item>
<item>611/05</item>
<item>738/02</item>
<item>404/34, 38, 51, 53, 54, 55, 57, 58, 59, 62, 64, 66, 71, 72, 73, 74, 75, 76, 77, 79, 80, 81</item>
<item>404/04, 07, 22, 24, 78</item>
<item>405/25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47</item>
<item>510/09, 28</item>
<item>432/35</item>
<item>416/77</item>
<item>639/02</item>
<item>247/01</item>
<item>618/03, 07</item>
<item>606/00, 03</item>
<item>278/21</item>
<item>334/03, 30</item>
<item>643/04</item>
<item>621/50</item>
<item>410/01</item>
<item>410/07</item>
<item>260/09</item>
<item>226/03</item>
<item>655/07</item>
<item>214/04</item>
<item>520/01</item>
<item>438/01</item>
<item>641/10</item>
<item>641/18</item>
<item>234/02, 10, 11</item>
<item>234/33, 34</item>
<item>234/20, 94</item>
<item>234/26</item>
<item>234/28</item>
<item>234/25</item>
<item>234/30, 31, 32</item>
<item>234/03, 15, 27, 91</item>
<item>424, 430, 431/02</item>
<item>310/31, 160, 200, 210, 220, 230, 240, 250, 260, 270, 280, 300, 310, 330, 660, 800</item>
<item>452/05</item>
<item>648/04</item>
<!--<item>603/02</item>-->
<!--<item>363/01</item>-->
<!--<item>505/07, 03</item>-->
<!--<item>232/03, 04, 07, 15</item>-->
<!--<item>426/02</item>-->
<!--<item>470/03</item>-->
<!--<item>470/01</item>-->
<!--<item>342/600</item>-->
<!--<item>724/01, 06, 10, 11, 19, 23</item>-->
<!--<item>724/30, 31</item>-->
<!--<item>625/01</item>-->
<!--<item>630/86</item>-->
<!--<item>368/01</item>-->
<!--<item>230/01</item>-->
<!--<item>740/02</item>-->
<!--<item>602/03</item>-->
<!--<item>208/01, 02, 29, 91</item>-->
<!--<item>262/01, 06</item>-->
<!--<item>611/05</item>-->
<!--<item>738/02</item>-->
<!--<item>404/34, 38, 51, 53, 54, 55, 57, 58, 59, 62, 64, 66, 71, 72, 73, 74, 75, 76, 77, 79, 80, 81</item>-->
<!--<item>404/04, 07, 22, 24, 78</item>-->
<!--<item>405/25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47</item>-->
<!--<item>510/09, 28</item>-->
<!--<item>432/35</item>-->
<!--<item>416/77</item>-->
<!--<item>639/02</item>-->
<!--<item>247/01</item>-->
<!--<item>618/03, 07</item>-->
<!--<item>606/00, 03</item>-->
<!--<item>278/21</item>-->
<!--<item>334/03, 30</item>-->
<!--<item>643/04</item>-->
<!--<item>621/50</item>-->
<!--<item>410/01</item>-->
<!--<item>410/07</item>-->
<!--<item>260/09</item>-->
<!--<item>226/03</item>-->
<!--<item>655/07</item>-->
<!--<item>214/04</item>-->
<!--<item>520/01</item>-->
<!--<item>438/01</item>-->
<!--<item>641/10</item>-->
<!--<item>641/18</item>-->
<!--<item>234/15</item>-->

<!--<item>234/02, 10, 11</item>-->
<!--<item>234/33, 34</item>-->
<!--<item>234/20, 94</item>-->
<!--<item>234/26</item>-->
<!--<item>234/28</item>-->
<!--<item>234/25</item>-->
<!--<item>234/30, 31, 32</item>-->
<!--<item>234/03, 15, 27, 91</item>-->
<!--<item>424, 430, 431/02</item>-->
<!--<item>310/31, 160, 200, 210, 220, 230, 240, 250, 260, 270, 280, 300, 310, 330, 660, 800</item>-->
<!--<item>452/05</item>-->
<!--<item>648/04</item>-->

<!--<item>412/40</item>-->
<!--<item>412/1</item>-->
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<string name="vapp_not_available_error_no_sim">Unable to use VAPP! as your SIM cannot be detected</string>
<string name="vapp_not_available_error_no_network">Unable to use VAPP! - no network found</string>
<string name="vapp_not_available_error_no_internet">Unable to use VAPP! - an internet connection is required</string>
<string name="vapp_network_not_supported">VAPP! is not currently supported on your network</string>
<string name="vapp_network_not_supported">VAPP! is not currently supported on your network: (code: %s)</string>

<string name="vapp_sms_sent_failure_generic">General SMS Send error. Please try again later.</string>
<string name="vapp_sms_sent_failure_no_service">No SMS Service. Please check your settings and try again.</string>
Expand Down
4 changes: 2 additions & 2 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.vasilitate.example"
minSdkVersion 9
targetSdkVersion 22
versionCode 4
versionName "0.4"
versionCode 5
versionName "0.5"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class TelephonyManagerTest extends AndroidTestCase {
private String TEST_OPERATOR = "23410";
private boolean IS_ROAMING = false;
private boolean HAS_SIM = false;
private boolean NETWORK_SUPPORTED = false;
// private boolean NETWORK_SUPPORTED = false;
private String TEST_IMEI = "35145120840121";
private static final String NUMBER_RANGE = "+447458830000";

Expand All @@ -38,6 +38,6 @@ public void testDeviceState() {

assertEquals(HAS_SIM, Vapp.isSIMPresent(getContext()));
assertEquals(IS_ROAMING, Vapp.isRoaming(getContext()));
assertEquals(NETWORK_SUPPORTED, Vapp.isOriginatingNetworkSupported(getContext()));
// assertEquals(NETWORK_SUPPORTED, Vapp.isOriginatingNetworkSupported(getContext()));
}
}

0 comments on commit 27f8683

Please sign in to comment.