sendKeys - Send special characters - android
addressBar = (AutoCompleteTextView) mActivity.findViewById(package.R.id.addressBar);
TouchUtils.tapView(this, addressBar);
sendKeys("1"); //Works
sendKeys("G M A I L"); // Works - Result would be "gmail"
sendKeys("G M A I L . C O M"); // Doesn't work
sendKeys("{.}"); // Doesn't work
sendKeys("gmail") // Doesn't work
sendKeys("G M A I L {.} C O M") //Doesn't work
I am writing android test scripts using "InstrumentationTestCase2". I actually want to sendkeys - "gmail.com" but, unable to send special character "."(Dot)
For '.' (period or dot) you can try the int equivalent values of it.
Like,
sendKeys(56);
From Android-Docs
public static final int KEYCODE_PERIOD
Key code constant: '.' key.
Constant Value: 56 (0x00000038)
"The sequence of keys is a string containing the key names as specified in KeyEvent, without the KEYCODE_ prefix." (sendKeys documentation)
So you can use NUMPAD_DOT in the sendKeys string.
e.g.
sendKeys("G M A I L NUMPAD_DOT C O M");
For Further information see :
(http://developer.android.com/reference/android/test/InstrumentationTestCase.html#sendKeys(java.lang.String))
sendKeys(56); // for special character "." (Dot)
Have you tried the following:
getInstrumentation().sendStringSync("Do.You#Love.IT???");
works like magic and makes life a lot simpler!
Related
Convert country code 2 letter to 3 letter in Objective-C
I'm working on a function that gets the country code from the phone, but when I get the country code it consists of 2 letters, but I want it to return three letters. For example US -> USA In Android, java supports converting from 2 characters to 3 characters with the following code: Locale locale = new Locale("en", countryCode); return locale.getISO3Country(); But in iOS with Objective-C I don't know how to convert it, so can anyone help me to solve this problem?
for the sake of standardisation there is no ISO 3166-1 alpha-3 code on apple platforms to convert to. More the other way around, you could use a 3 letter code and still find the 2 letter code. and if you want to keep at least some consistency to your android code then you need to implement some LUT table supporting this off-standard feature yourself. The available list is not very long anyway (256 codes). NSArray *isoCountrys = [NSLocale ISOCountryCodes]; for (NSString *code in isoCountrys) { NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:code]; // country name in native language NSString *country = [locale localizedStringForCountryCode:code]; NSString *iso3 = LUTisoA3counterpartCodes[code]; NSLog(#"%# %# %# %#",code, iso3, country, locale.localeIdentifier); } Docu NSLocale -localizedStringForCountryCode: Docu NSLocale -countryCode the LUT could look like.. and is much better stored in a plist then the following runtime allocated dictionary. NSDictionary *LUTisoA3counterpartCodes = #{ #"AC":#"SHN",#"AW":#"ABW",#"AF":#"AFG",#"AO":#"AGO",#"AI":#"AIA",#"AX":#"ALA", #"AL":#"ALB",#"AD":#"AND",#"AE":#"ARE",#"AR":#"ARG",#"AM":#"ARM",#"AS":#"ASM", #"AQ":#"ATA",#"TF":#"ATF",#"AG":#"ATG",#"AU":#"AUS",#"AT":#"AUT",#"AZ":#"AZE", #"BI":#"BDI",#"BE":#"BEL",#"BJ":#"BEN",#"BQ":#"BES",#"BF":#"BFA",#"BD":#"BGD", #"BG":#"BGR",#"BH":#"BHR",#"BS":#"BHS",#"BA":#"BIH",#"BL":#"BLM",#"BY":#"BLR", #"BZ":#"BLZ",#"BM":#"BMU",#"BO":#"BOL",#"BR":#"BRA",#"BB":#"BRB",#"BN":#"BRN", #"BT":#"BTN",#"BV":#"BVT",#"BW":#"BWA",#"CF":#"CAF",#"CA":#"CAN",#"CC":#"CCK", #"CH":#"CHE",#"CL":#"CHL",#"CN":#"CHN",#"CI":#"CIV",#"CM":#"CMR",#"CD":#"COD", #"CG":#"COG",#"CK":#"COK",#"CO":#"COL",#"KM":#"COM",#"CV":#"CPV",#"CR":#"CRI", #"CU":#"CUB",#"CW":#"CUW",#"CX":#"CXR",#"KY":#"CYM",#"CY":#"CYP",#"CZ":#"CZE", #"DE":#"DEU",#"DJ":#"DJI",#"DM":#"DMA",#"DK":#"DNK",#"DO":#"DOM",#"DZ":#"DZA", #"EC":#"ECU",#"EG":#"EGY",#"ER":#"ERI",#"EH":#"ESH",#"ES":#"ESP",#"EE":#"EST", #"ET":#"ETH",#"FI":#"FIN",#"FJ":#"FJI",#"FK":#"FLK",#"FR":#"FRA",#"FO":#"FRO", #"FM":#"FSM",#"GA":#"GAB",#"GB":#"GBR",#"GE":#"GEO",#"GG":#"GGY",#"GH":#"GHA", #"GI":#"GIB",#"GN":#"GIN",#"GP":#"GLP",#"GM":#"GMB",#"GW":#"GNB",#"GQ":#"GNQ", #"GR":#"GRC",#"GD":#"GRD",#"GL":#"GRL",#"GT":#"GTM",#"GF":#"GUF",#"GU":#"GUM", #"GY":#"GUY",#"HK":#"HKG",#"HM":#"HMD",#"HN":#"HND",#"HR":#"HRV",#"HT":#"HTI", #"HU":#"HUN",#"ID":#"IDN",#"IM":#"IMN",#"IN":#"IND",#"IO":#"IOT",#"IE":#"IRL", #"IR":#"IRN",#"IQ":#"IRQ",#"IS":#"ISL",#"IL":#"ISR",#"IT":#"ITA",#"JM":#"JAM", #"JE":#"JEY",#"JO":#"JOR",#"JP":#"JPN",#"KZ":#"KAZ",#"KE":#"KEN",#"KG":#"KGZ", #"KH":#"KHM",#"KI":#"KIR",#"KN":#"KNA",#"KR":#"KOR",#"KW":#"KWT",#"LA":#"LAO", #"LB":#"LBN",#"LR":#"LBR",#"LY":#"LBY",#"LC":#"LCA",#"LI":#"LIE",#"LK":#"LKA", #"LS":#"LSO",#"LT":#"LTU",#"LU":#"LUX",#"LV":#"LVA",#"MO":#"MAC",#"MF":#"MAF", #"MA":#"MAR",#"MC":#"MCO",#"MD":#"MDA",#"MG":#"MDG",#"MV":#"MDV",#"MX":#"MEX", #"MH":#"MHL",#"MK":#"MKD",#"ML":#"MLI",#"MT":#"MLT",#"MM":#"MMR",#"ME":#"MNE", #"MN":#"MNG",#"MP":#"MNP",#"MZ":#"MOZ",#"MR":#"MRT",#"MS":#"MSR",#"MQ":#"MTQ", #"MU":#"MUS",#"MW":#"MWI",#"MY":#"MYS",#"YT":#"MYT",#"NA":#"NAM",#"NC":#"NCL", #"NE":#"NER",#"NF":#"NFK",#"NG":#"NGA",#"NI":#"NIC",#"NU":#"NIU",#"NL":#"NLD", #"NO":#"NOR",#"NP":#"NPL",#"NR":#"NRU",#"NZ":#"NZL",#"OM":#"OMN",#"PK":#"PAK", #"PA":#"PAN",#"PN":#"PCN",#"PE":#"PER",#"PH":#"PHL",#"PW":#"PLW",#"PG":#"PNG", #"PL":#"POL",#"PR":#"PRI",#"KP":#"PRK",#"PT":#"PRT",#"PY":#"PRY",#"PS":#"PSE", #"PF":#"PYF",#"QA":#"QAT",#"RE":#"REU",#"RO":#"ROU",#"RU":#"RUS",#"RW":#"RWA", #"SA":#"SAU",#"SD":#"SDN",#"SN":#"SEN",#"SG":#"SGP",#"GS":#"SGS",#"SH":#"SHN", #"SJ":#"SJM",#"SB":#"SLB",#"SL":#"SLE",#"SV":#"SLV",#"SM":#"SMR",#"SO":#"SOM", #"PM":#"SPM",#"RS":#"SRB",#"SS":#"SSD",#"ST":#"STP",#"SR":#"SUR",#"SK":#"SVK", #"SI":#"SVN",#"SE":#"SWE",#"SZ":#"SWZ",#"SX":#"SXM",#"SC":#"SYC",#"SY":#"SYR", #"TC":#"TCA",#"TD":#"TCD",#"TG":#"TGO",#"TH":#"THA",#"TJ":#"TJK",#"TK":#"TKL", #"TM":#"TKM",#"TL":#"TLS",#"TO":#"TON",#"TT":#"TTO",#"TN":#"TUN",#"TR":#"TUR", #"TV":#"TUV",#"TW":#"TWN",#"TZ":#"TZA",#"UG":#"UGA",#"UA":#"UKR",#"UM":#"UMI", #"UY":#"URY",#"US":#"USA",#"UZ":#"UZB",#"VA":#"VAT",#"VC":#"VCT",#"VE":#"VEN", #"VG":#"VGB",#"VI":#"VIR",#"VN":#"VNM",#"VU":#"VUT",#"WF":#"WLF",#"WS":#"WSM", #"XK":#"XKV",#"YE":#"YEM",#"ZA":#"ZAF",#"ZM":#"ZMB",#"ZW":#"ZWE", //unknown status or codes, to be changed soon #"DG":#"DGA" , //Diego Garcia #"EA":#"EA_" , //Ceuta and Melilla #"CP":#"CPT" , //Clipperton Island -> French Polynesia #"IC":#"IC_" , //Kanarian Island #"TA":#"TAA" , //டிரிஸ்டன் டா குன்ஹா , Tristan da Cunha -> St.Helena }; this LUT makes it easy to lookup by 2 letter code and get the 3 letter codes. And in reality the list is much longer and matter of permanent changes. and if you trust the sorting of Apples API you could just use a static NSArray instead of a plist or NSDictionary. The following prints it for use.. int i=1; fprintf(stderr,"\nstatic NSString *isoA3accordingToAppleSorting[256] = {\n"); for (NSString *code in isoCountrys) { if (i%20 == 19) fprintf(stderr,"\n"); NSString *iso3 = LUTisoA3counterpartCodes[code]; fprintf(stderr,"#\"%s\",",iso3.UTF8String); i++; } fprintf(stderr,"};\n"); which looks like.. static NSString *countryCodeAsA3accordingToAppleSorting[256] = { #"SHN",#"AND",#"ARE",#"AFG",#"ATG",#"AIA",#"ALB",#"ARM",#"AGO",#"ATA",#"ARG",#"ASM",#"AUT",#"AUS",#"ABW",#"ALA",#"AZE",#"BIH", #"BRB",#"BGD",#"BEL",#"BFA",#"BGR",#"BHR",#"BDI",#"BEN",#"BLM",#"BMU",#"BRN",#"BOL",#"BES",#"BRA",#"BHS",#"BTN",#"BVT",#"BWA",#"BLR",#"BLZ", #"CAN",#"CCK",#"COD",#"CAF",#"COG",#"CHE",#"CIV",#"COK",#"CHL",#"CMR",#"CHN",#"COL",#"CPT",#"CRI",#"CUB",#"CPV",#"CUW",#"CXR",#"CYP",#"CZE", #"DEU",#"DGA",#"DJI",#"DNK",#"DMA",#"DOM",#"DZA",#"EA_",#"ECU",#"EST",#"EGY",#"ESH",#"ERI",#"ESP",#"ETH",#"FIN",#"FJI",#"FLK",#"FSM",#"FRO", #"FRA",#"GAB",#"GBR",#"GRD",#"GEO",#"GUF",#"GGY",#"GHA",#"GIB",#"GRL",#"GMB",#"GIN",#"GLP",#"GNQ",#"GRC",#"SGS",#"GTM",#"GUM",#"GNB",#"GUY", #"HKG",#"HMD",#"HND",#"HRV",#"HTI",#"HUN",#"IC_",#"IDN",#"IRL",#"ISR",#"IMN",#"IND",#"IOT",#"IRQ",#"IRN",#"ISL",#"ITA",#"JEY",#"JAM",#"JOR", #"JPN",#"KEN",#"KGZ",#"KHM",#"KIR",#"COM",#"KNA",#"PRK",#"KOR",#"KWT",#"CYM",#"KAZ",#"LAO",#"LBN",#"LCA",#"LIE",#"LKA",#"LBR",#"LSO",#"LTU", #"LUX",#"LVA",#"LBY",#"MAR",#"MCO",#"MDA",#"MNE",#"MAF",#"MDG",#"MHL",#"MKD",#"MLI",#"MMR",#"MNG",#"MAC",#"MNP",#"MTQ",#"MRT",#"MSR",#"MLT", #"MUS",#"MDV",#"MWI",#"MEX",#"MYS",#"MOZ",#"NAM",#"NCL",#"NER",#"NFK",#"NGA",#"NIC",#"NLD",#"NOR",#"NPL",#"NRU",#"NIU",#"NZL",#"OMN",#"PAN", #"PER",#"PYF",#"PNG",#"PHL",#"PAK",#"POL",#"SPM",#"PCN",#"PRI",#"PSE",#"PRT",#"PLW",#"PRY",#"QAT",#"REU",#"ROU",#"SRB",#"RUS",#"RWA",#"SAU", #"SLB",#"SYC",#"SDN",#"SWE",#"SGP",#"SHN",#"SVN",#"SJM",#"SVK",#"SLE",#"SMR",#"SEN",#"SOM",#"SUR",#"SSD",#"STP",#"SLV",#"SXM",#"SYR",#"SWZ", #"TAA",#"TCA",#"TCD",#"ATF",#"TGO",#"THA",#"TJK",#"TKL",#"TLS",#"TKM",#"TUN",#"TON",#"TUR",#"TTO",#"TUV",#"TWN",#"TZA",#"UKR",#"UGA",#"UMI", #"USA",#"URY",#"UZB",#"VAT",#"VCT",#"VEN",#"VGB",#"VIR",#"VNM",#"VUT",#"WLF",#"WSM",#"XKV",#"YEM",#"MYT",#"ZAF",#"ZMB",#"ZWE",}; but then you have to find the index of your 2 letter code in apples ISOCountryCodes to look them up accordingly. Reminder. The ISO 3166-1 alpha-3 explains only that it should have 3 letters, not which letter exactly
Log.d can not log total message in Android Studio
Using Log.d() on Android Studio, I print a relatively long json, only a part of it is displayed, and I want to view the complete one, what should I do? Ofcourse, separating the content is a way we can think of directly, but isn't it not so cool?
You can break the answer into chunks and display them one by one: var str = jsonObj.toString() var k = 1000; //or a smaller value, idk.. for (i in 0..str.length step k) { Log.d(yourTag, str.substring(i, minOf(i + k, str.length))) }
KeyCharacterMap to use from code only
I need to insert a special character into my Android app that is not included in any the existing KeyCharacterMap files. Generating key events is the only way I see to get any active control without knowing which one. To load this map in order to generate the KeyEvent from I have to specify the map's id but this ID comes from an InputDevice and there is no such device for my newly generated kcm file. What is the best way to either use this keymap without an existing inputdevice or fake the inputdevice?
I believe that you can generate special key with this solution : char[] szRes = szStringText.toCharArray(); // Convert String to Char arra KeyCharacterMap CharMap; if(Build.VERSION.SDK_INT >= 11) // My soft runs until API 5 CharMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); else CharMap = KeyCharacterMap.load(KeyCharacterMap.ALPHA); KeyEvent[] events = CharMap.getEvents(szRes); for(int i=0; i<events.length; i++) MainWebView.dispatchKeyEvent(events[i]); // MainWebView is webview You can build the string[] szStringText with "!/§)". Normally, the code convert charactere. Source : https://www.codota.com/code/java/classes/android.view.KeyCharacterMap
Android Passing Variables to another activity with # not working as expected
Ok I'm trying to create a link to an activity and it works. However passint it a variable isn't working with # charters: String hashTagString = tempValues.getDescription().replaceAll("[#]+[A-Za-z0-9-_]+\\b", "$0"); Log.i(Utils.TAG, "hashTagString: " + hashTagString); The launched activity: String tag = getIntent().getData().getQueryParameter("tag"); Log.i(Utils.TAG, "tag: " + tag); Log Cat: 02-24 13:12:04.293: I/PROJECTCARUSO(29591): hashTagString: Everyone loves #hashtags ! Take advantage of them by using them and clicking to search by them. The activity it launches show this: 02-24 13:13:48.885: I/PROJECTCARUSO(29591): tag: However if I set it with hard coded values it shows: 02-24 13:14:41.176: I/PROJECTCARUSO(29883): hashTagString: Everyone loves #hashtags ! Take advantage of them by using them and clicking to search by them. 02-24 13:14:41.637: I/PROJECTCARUSO(29883): tag: test I'd like to pass the whole value, but if i cannot how can I remove all special charaters?
I don't think your regex is being evaluated correctlyTry using the following regex \\#[A-Za-z0-9\\-_]+ You may need to double escape (\). The \b (word-boundary) at the end of your sequence isn't needed because it will break when it reaches a " anyway and the - in your character class needs escaping otherwise the regex is trying to evaluate a nonsense range instead of treating it as a char Edit Now you've clarified your issue the issue with getting a blank tagis this: String tag = getIntent().getData().getQueryParameter("tag"); It should be String tag = getIntent().getExtras().getString("tag"); and should be sent as new Intent(...).putExtra("tag", hashTagString);
Try (\s|\A)#(\w+) that as a Java string is "(\\s|\\A)#(\\w+)". You can test it in the following site: http://www.regexplanet.com/advanced/java/index.html
Jelly Bean Issue - wifiManager.getConnectionInfo().getSSID() - extra ""
Hi all bug reporting for your information. link Problem details: The Code - wifiManager.getConnectionInfo().getSSID() The above code to returns the current SSID, it is returning the current SSID with extra quotations around it. For eg. the SSID internet is returned as "internet". This is only seen on Jelly bean 4.2 using device Nexus 7. This bug is causing errors in our app as we compare the current SSID with the SSID that we are trying to connect too. The code wifiManager.getScanResults(); however still returns all SSID's without extra quotation marks.
this is not a bug and behavior is correct as per documentation at http://developer.android.com/reference/android/net/wifi/WifiInfo.html#getSSID() The so-called bug apparently was in pre 4.2 devices, because they didn't return it with "" enclosure. Aiden's method looks good to me in the current state of confusion left by Android. However, being theoritically correct would just require if (ssid.startsWith("\"") && ssid.endsWith("\"")){ ssid = ssid.substring(1, ssid.length()-1); }
This regular expression is quite neat: String ssid = wi.getSSID().replaceAll("^\"(.*)\"$", "$1"); Just for the notes Edit °1 (as per question in the comment): The issue that OP describes is, that on some devices the SSID returned by getSSID() is enclosed in "" whereas it is not on other devices. E.g. on some devices the SSID is "MY_WIFI" and on others it is MY_WIFI - or spoken in Java code: "\"MY_WIFI\"" and "MY_WIFI". In order to to unify both results I proposed to remove the " at start and end - only there, because " is a legal character inside the SSID. In the regular expression above ^ means from start $ means at end \" means " (escaped) .* means any number of characters (...) means a capturing group, that can be referred by $1 So the whole expression means: replace "<something>" by <something> where $1 = <something>. If there is no " at end/start, the regular expression doesn't match and nothing is replaced. See Java Pattern class for more details.
For the mean time this is how I am getting around it, although its not great it will fix the issue. public String removeQuotationsInCurrentSSIDForJellyBean(String ssid){ int deviceVersion= Build.VERSION.SDK_INT; if (deviceVersion >= 17){ if (ssid.startsWith("\"") && ssid.endsWith("\"")){ ssid = ssid.substring(1, ssid.length()-1); } } return ssid; }
Two very simple variants: string = string.replaceAll("^\" | \"$", ""); and string = string.substring(1, string.length() - 1);
Faced the same problem! Used this technique which is backwards compatible: if (suppliedSSID.equals(connectionInfo.getSSID()) || ("\"" + suppliedSSID + "\"").equals(connectionInfo.getSSID()) { DO SOMETHING }