[MediaWiki-commits] [Gerrit] Add MCC-MNC once per app session on cellular. - change (apps...wikipedia)
Dr0ptp4kt has uploaded a new change for review. https://gerrit.wikimedia.org/r/131246 Change subject: Add MCC-MNC once per app session on cellular. .. Add MCC-MNC once per app session on cellular. * http://lists.wikimedia.org/pipermail/wikimedia-l/2014-April/071131.html Change-Id: If61f11f5fd6c04af2e42f58276b2c9cf888ec709 --- M wikipedia/src/main/java/org/wikipedia/WikipediaApp.java 1 file changed, 40 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia refs/changes/46/131246/1 diff --git a/wikipedia/src/main/java/org/wikipedia/WikipediaApp.java b/wikipedia/src/main/java/org/wikipedia/WikipediaApp.java index 54bf450..be32b6b 100644 --- a/wikipedia/src/main/java/org/wikipedia/WikipediaApp.java +++ b/wikipedia/src/main/java/org/wikipedia/WikipediaApp.java @@ -7,6 +7,7 @@ import android.net.*; import android.os.*; import android.preference.*; +import android.telephony.TelephonyManager; import android.webkit.*; import com.squareup.otto.*; import org.acra.*; @@ -120,10 +121,47 @@ private HashMap apis = new HashMap(); public Api getAPIForSite(Site site) { +if (!sentMccMnc) { +ConnectivityManager conn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); +NetworkInfo networkInfo = conn.getActiveNetworkInfo(); +if (networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED +&& (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE || networkInfo.getType() == ConnectivityManager.TYPE_WIMAX)) +{ +TelephonyManager t = (TelephonyManager)getSystemService(WikipediaApp.TELEPHONY_SERVICE); +if (t != null && t.getPhoneType() >= 0) { +String mccMnc = t.getNetworkOperator(); +if (mccMnc != null) { +mccMnc = mccMnc.substring(0,3) + "-" + mccMnc.substring(3); +if (t.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) { +String simMccMnc = t.getSimOperator(); +if (simMccMnc != null) { +simMccMnc = simMccMnc.substring(0,3) + "-" + simMccMnc.substring(3); +if (!simMccMnc.equals(mccMnc)) { +mccMnc = simMccMnc; +} +} +} +} +} + + +} + +} + + + if (!apis.containsKey(site.getDomain())) { apis.put(site.getDomain(), new Api(site.getApiDomain(), getUserAgent())); } return apis.get(site.getDomain()); +} + +public Api getAPIForSite(Site site, HashMap customHeaders) { +if (!apis.containsKey(site.getDomain() + customHeaders.hashCode())) { +apis.put(site.getDomain() + customHeaders.hashCode(), new Api(site.getApiDomain(), getUserAgent(), customHeaders)); +} +return apis.get(site.getDomain() + customHeaders.hashCode()); } private Site primarySite; @@ -312,4 +350,6 @@ public static boolean isWikipediaZeroDevmodeOn() { return WIKIPEDIA_ZERO_DEV_MODE_ON; } + +private static boolean sentMccMnc; } -- To view, visit https://gerrit.wikimedia.org/r/131246 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If61f11f5fd6c04af2e42f58276b2c9cf888ec709 Gerrit-PatchSet: 1 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: Dr0ptp4kt ___ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
[MediaWiki-commits] [Gerrit] Add MCC-MNC once per app session on cellular. - change (apps...wikipedia)
Brion VIBBER has submitted this change and it was merged. Change subject: Add MCC-MNC once per app session on cellular. .. Add MCC-MNC once per app session on cellular. * http://lists.wikimedia.org/pipermail/wikimedia-l/2014-April/071131.html Change-Id: I1c145a623abb582449152f0cf37ffaac3e8829ff --- M wikipedia/Categories/NSURLRequest+DictionaryRequest.m M wikipedia/Zero/ZeroConfigState.h 2 files changed, 43 insertions(+), 0 deletions(-) Approvals: Mhurd: Looks good to me, but someone else must approve Brion VIBBER: Verified; Looks good to me, approved diff --git a/wikipedia/Categories/NSURLRequest+DictionaryRequest.m b/wikipedia/Categories/NSURLRequest+DictionaryRequest.m index ae17b7e..cd285f4 100644 --- a/wikipedia/Categories/NSURLRequest+DictionaryRequest.m +++ b/wikipedia/Categories/NSURLRequest+DictionaryRequest.m @@ -3,7 +3,11 @@ #import "NSURLRequest+DictionaryRequest.h" #import "NSString+Extras.h" +#import "SessionSingleton.h" #import "WikipediaAppUtils.h" +#import +#import +#include @implementation NSURLRequest (DictionaryRequest) @@ -30,6 +34,7 @@ [request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"]; [request addValue:[WikipediaAppUtils versionedUserAgent] forHTTPHeaderField:@"User-Agent"]; // NSLog(@"%@", [WikipediaAppUtils versionedUserAgent]); +[self addMCCMNCToRequestIfAppropriate:request]; [request setHTTPBody:[[NSURLRequest constructEncodedURL:parameters] dataUsingEncoding:NSUTF8StringEncoding]]; return request; } @@ -45,6 +50,43 @@ [request addValue:[WikipediaAppUtils versionedUserAgent] forHTTPHeaderField:@"User-Agent"]; // NSLog(@"%@", [WikipediaAppUtils versionedUserAgent]); [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; +[self addMCCMNCToRequestIfAppropriate:request]; return request; } + +#pragma MCC-MNC Logging + +// Add the MCC-MNC code asn HTTP (protocol) header once per session when user using cellular data connection. +// Logging will be done in its own file with specific fields. See the following URL for details. +// http://lists.wikimedia.org/pipermail/wikimedia-l/2014-April/071131.html + ++(void) addMCCMNCToRequestIfAppropriate: (NSMutableURLRequest*) req +{ +if ([SessionSingleton sharedInstance].zeroConfigState.sentMCCMNC) { +return; +} else { +CTCarrier *mno = [[[CTTelephonyNetworkInfo alloc] init] subscriberCellularProvider]; +if (mno) { +SCNetworkReachabilityRef reachabilityRef = SCNetworkReachabilityCreateWithName(NULL, + [[[req URL] host] UTF8String]); +SCNetworkReachabilityFlags reachabilityFlags; +SCNetworkReachabilityGetFlags(reachabilityRef, &reachabilityFlags); + +// The following is a good functioning mask in practice for the case where +// cellular is being used, with wifi not on / there are no known wifi APs. +// When wifi is on with a known wifi AP connection, kSCNetworkReachabilityFlagsReachable +// is present, but kSCNetworkReachabilityFlagsIsWWAN is not present. +if (reachabilityFlags == (kSCNetworkReachabilityFlagsIsWWAN + | kSCNetworkReachabilityFlagsReachable + | kSCNetworkReachabilityFlagsTransientConnection)) { +NSString *mccMnc = [[NSString alloc] initWithFormat:@"%@-%@", [mno mobileCountryCode], [mno mobileNetworkCode]]; +[SessionSingleton sharedInstance].zeroConfigState.sentMCCMNC = true; +[req addValue:mccMnc forHTTPHeaderField:@"X-MCCMNC"]; +// NSLog(@"%@", mccMnc); +} +} +} +} + + @end diff --git a/wikipedia/Zero/ZeroConfigState.h b/wikipedia/Zero/ZeroConfigState.h index 3df69c4..44d5982 100644 --- a/wikipedia/Zero/ZeroConfigState.h +++ b/wikipedia/Zero/ZeroConfigState.h @@ -7,6 +7,7 @@ @property (strong, nonatomic) NSString *partnerXcs; @property (nonatomic) BOOL disposition; +@property (nonatomic) BOOL sentMCCMNC; @property (nonatomic, readonly) BOOL zeroOnDialogShownOnce; @property (nonatomic, readonly) BOOL zeroOffDialogShownOnce; @property (nonatomic, readonly) BOOL warnWhenLeaving; -- To view, visit https://gerrit.wikimedia.org/r/130674 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I1c145a623abb582449152f0cf37ffaac3e8829ff Gerrit-PatchSet: 5 Gerrit-Project: apps/ios/wikipedia Gerrit-Branch: master Gerrit-Owner: Dr0ptp4kt Gerrit-Reviewer: Brion VIBBER Gerrit-Reviewer: Dr0ptp4kt Gerrit-Reviewer: Mhurd ___ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-
[MediaWiki-commits] [Gerrit] Add MCC-MNC once per app session on cellular. - change (apps...wikipedia)
jenkins-bot has submitted this change and it was merged. Change subject: Add MCC-MNC once per app session on cellular. .. Add MCC-MNC once per app session on cellular. * http://lists.wikimedia.org/pipermail/wikimedia-l/2014-April/071131.html Change-Id: If61f11f5fd6c04af2e42f58276b2c9cf888ec709 --- M wikipedia/src/main/java/org/wikipedia/Site.java M wikipedia/src/main/java/org/wikipedia/Utils.java M wikipedia/src/main/java/org/wikipedia/WikipediaApp.java A wikipedia/src/main/java/org/wikipedia/networking/MccMncStateHandler.java 4 files changed, 95 insertions(+), 1 deletion(-) Approvals: Yuvipanda: Looks good to me, approved jenkins-bot: Verified diff --git a/wikipedia/src/main/java/org/wikipedia/Site.java b/wikipedia/src/main/java/org/wikipedia/Site.java index e8e614e..61c2b5a 100644 --- a/wikipedia/src/main/java/org/wikipedia/Site.java +++ b/wikipedia/src/main/java/org/wikipedia/Site.java @@ -12,7 +12,7 @@ this.domain = domain.replaceFirst("\\.m\\.", "."); } -protected String getApiDomain() { +public String getApiDomain() { return domain.replaceFirst("\\.", ".m."); } diff --git a/wikipedia/src/main/java/org/wikipedia/Utils.java b/wikipedia/src/main/java/org/wikipedia/Utils.java index 3e61bf7..c42 100644 --- a/wikipedia/src/main/java/org/wikipedia/Utils.java +++ b/wikipedia/src/main/java/org/wikipedia/Utils.java @@ -3,8 +3,11 @@ import android.app.*; import android.content.*; import android.content.pm.*; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.net.Uri; import android.os.*; +import android.telephony.TelephonyManager; import android.text.*; import android.text.format.*; import android.util.*; @@ -241,6 +244,48 @@ } /** + * Read the MCC-MNC (mobile operator code) if available and the cellular data connection is the active one. + * http://lists.wikimedia.org/pipermail/wikimedia-l/2014-April/071131.html + * @param ctx Application context. + * @return The MCC-MNC, typically as ###-##, or null if unable to ascertain (e.g., no actively used cellular) + */ +public static String getMccMnc(Context ctx) { +String mccMnc = null; +try { +ConnectivityManager conn = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); +NetworkInfo networkInfo = conn.getActiveNetworkInfo(); +if (networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED +&& (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE || networkInfo.getType() == ConnectivityManager.TYPE_WIMAX)) +{ +TelephonyManager t = (TelephonyManager)ctx.getSystemService(WikipediaApp.TELEPHONY_SERVICE); +if (t != null && t.getPhoneType() >= 0) { +mccMnc = t.getNetworkOperator(); +if (mccMnc != null) { +mccMnc = mccMnc.substring(0,3) + "-" + mccMnc.substring(3); +} + +// TelephonyManager documentation refers to MCC-MNC unreliability on CDMA, +// so we'll try to read the SIM and use the SIM MCC-MNC if there's a disagreement. +// There may be a counterargument to go the other way, although we'll go this route for now. +if (t.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) { +String simMccMnc = t.getSimOperator(); +if (simMccMnc != null) { +simMccMnc = simMccMnc.substring(0,3) + "-" + simMccMnc.substring(3); +if (!simMccMnc.equals(mccMnc)) { +mccMnc = simMccMnc; +} +} +} +} +} +return mccMnc; +} catch (Exception e) { +// Because, despite best efforts, things can go wrong and we don't want to crash the app: +return null; +} +} + +/** * Takes a language code (as returned by Android) and returns a wiki code, as used by wikipedia. * * @param langCode Language code (as returned by Android) diff --git a/wikipedia/src/main/java/org/wikipedia/WikipediaApp.java b/wikipedia/src/main/java/org/wikipedia/WikipediaApp.java index 71158dc..02aa611 100644 --- a/wikipedia/src/main/java/org/wikipedia/WikipediaApp.java +++ b/wikipedia/src/main/java/org/wikipedia/WikipediaApp.java @@ -162,7 +162,14 @@ } private HashMap apis = new HashMap(); +private MccMncStateHandler mccMncStateHandler = new MccMncStateHandler(); public Api getAPIForSite(Site site) { +// http://lists.wikimedia.org/pipermail/wikimedia-l/2014-April/071131.html +Api api = mccMncStateHandler.makeApiWithMccMncHeaderEnrichment(this, site, getUs