Query Unread Texts for Specific Contact Android - java

From this question I can see how to get total unread: Get number of unread sms
I have searched for this on Google but unfortunately do not understand how I may go about this.
I am wondering if it is possible to get the number of texts (unread) for a specific number, or, to do more work on my end, get the number of unread texts for each phone number in the list, and then make my own comparisons. The end result would be that I am able to, for example, take the number 111 and see how many unread tests I have from them or to simply get an ArrayList or similar construct containing numbers and unread texts for each, through which to loop and compare against the number 111.

Use columns from android.provider.Telephony.TextBasedSmsColumns in your ContentResolver query to find SMS messages that are from a specific address and not read:
Use these column contants:
Telephony.TextBasedSmsColumns.ADDRESS
Telephony.TextBasedSmsColumns.READ
Something like this:
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(Telephony.Sms.CONTENT_URI,
null,
"WHERE " + Telephony.TextBasedSmsColumns.READ + " = ? and "
+ Telephony.TextBaseSmsColumns.ADDRESS + "= ?" ,
new String[]{"false", "+1 555 555 1212"},
Telephony.Sms.Conversations.DEFAULT_SORT_ORDER);
int unreadCount = cursor.getCount();

Related

Java: showing the output of a separate for loop as an extra column shown in a console

Bit of context...
In my project I have one embedded for loop that outputs data whereby for each category show the item and within each item show its property so in reality the output I generated is 3 columns of data in the console (headings: Category/Item/Property) The for loop to show this data looks like this (Variables are set earlier on in the method):
for... (picks up each category)
for...(picks up items in category)
for (String propertyName : item.getPropertyNames()) {
out.println(category.getName() + "\t"
+ itemDesc.getName() + "\tProperty:"
+ propertyName);
}
}
}
The purpose of the project is to provide a more dynamic documentation of the properties of set components in the system. (The /t making it possible to separate them in to individual columns on a console and even in a file in say an excel spreadsheet should I choose to set the file on the printstream (Also at the start of this method.))
The Problem
Now for the problem, after the for loops specified above I have generated another for loop separate from the data but shows the list of all the functions and operators involved in the components:
//Outside the previous for loops
for (Function function : Functions.allFunctions) {
out.println(function.getSignature());
}
What I want is to set this list as the 4th column but the positioning of the for loop and the way it is set leaves it fixed on the first column with the categories. I cant add it after property names as the functions are more generic to everything in the lists and there maybe repetitions of the functions which I am trying to avoid. Is there a way to set it as the forth column? Having trouble finding the sufficient research that specifies what I am looking for here. Hope this makes sense.
One solution, if the total amount of output is small enough to fit in memory, is to simply save all the data into an ArrayList of String, and output it all at the very end.
List<String> myList = new ArrayList<String>();
for... (picks up each category)
for...(picks up items in category)
for (String propertyName : item.getPropertyNames()) {
myList.add(category.getName() + "\t"
+ itemDesc.getName() + "\tProperty:"
+ propertyName);
}
}
}
int i = 0;
// Here we assume that the total lines output by the previous set of loops is
// equal to the total output by this loop.
for (Function function : Functions.allFunctions) {
out.println(myList.get(i) + "\t" + function.getSignature());
i++;
}

Validating the sorting functionality using Selenium (Java)

This is my first post for help. Please correct me if you see anything wrong with my post.
I am trying to validate the sorting functionality in a web page with Selenium script (using java). here are the details...
First I go to a User search results page with multiple pages.
It has users with the details: user name, number of miles.
There is a sort filter drop down with values: Values A-Z, Values Z-A, Miles Most, Miles Least, Newest Members, Oldest members . by default the sorting is newest members.
Initially I just want to validate: Values A-Z, Values Z-A, Miles Most and Miles Least Since I could see those values in the search page.
For someone who is looking to solve the same problem. Below code worked for me in validating the sorting of all the string values in a page
//Declare Variables
int eleCount;
List<String> customerNameA = new ArrayList();
List<String> customerNameB = new ArrayList();
// Check for our Customer elements and count them.... replace xxx with your xpath
assertTrue(isElementPresent(By.xpath("xxx")));
elements = driver.findElements(By.xpath("xxx']"));
eleCount = elements.size();
System.out.println("Element count: " + eleCount);
for(int i = 2; i < eleCount; i++){
//Capture the customer name values
//replace xxx with your xpath & replace the value increments for each element in xpath with + i +
customerNameA.add(driver.findElement(By.xpath("xxx")).getText());
System.out.println(driver.findElement(By.xpath("xxx")).getText());
customerNameB.add(driver.findElement(By.xpath("xxx")).getText());
}
Collections.sort(customerNameA);
for (int i=0;i<customerNameA.size();i++) {
System.out.println("Customer Name from input: " + customerNameB.get(i) + "--Customer Name from sorted input: " + customerNameA.get(i));
if (!(customerNameA.get(i).equals(customerNameB.get(i)))) {
System.out.println("Customer Names not sorted: " + i);
break;
}
}
}

Android Cursor - Two floats not adding if they are equal?

This is a strange problem, and I hope it has a simple solution. I have a database with encrypted values. I have created a cursor that will go through each of the entries in a table, decrypt the value from the column I need, and add the value to a variable, "total". I want the sum of all of the values in the column. Here is the code:
while (c.moveToNext())
{
strTotal = c.getString(c.getColumnIndexOrThrow(KEY_TOTAL));
strTotal = sc.decrypt(strTotal);
total = Float.valueOf(strTotal) + total;
}
Now, here's the strange part. Let's suppose I have two values in the database: 2 + 4. After each is decrypted, it will correctly add them: 6. Now, if the values are equal: 2 + 2, for instance, the method returns "2" instead of "4". This happens even if it is off by a decimal (2 + 2.01 = 4.01, but 2 + 2 still outputs 2 for example).
Is there something I am missing here? Thanks!
EDIT:
I've changed the code around just to see if the decryption was the problem and it is still giving me the same result:
float total = 0;
String strTotal = "10";
while (c.moveToNext())
{
try {
//strTotal = sc.decrypt(c.getString(c.getColumnIndex(KEY_TOTAL)));
total = Float.valueOf(strTotal) + total;
} catch (Exception e) {
Log.e(TAG, "exception", e);
}
}
This code is returning "10", even though there are 3 entries in the database! It looks like if two rows in the database have the same value in the KEY_TOTAL field, it is returning less results. Here is the query:
Cursor c = mDb.query(true, DATABASE_TABLE, new String[] {KEY_TOTAL}, KEY_TYPE + "=" + t, null, null, null, null, null);
If I pull the db and open it with a sqlite browser, and SELECT all of the rows, I am getting 3 still, however.
I just checked the SQLite documentation for Android (I'm not an Android developer) and I think I found your problem. The first argument to the query method is whether to select distinct rows. Since you're passing TRUE and you're only selecting one column, duplicates will be removed from the result, which is not what you want.
Changing your call to query to the following should fix your issue.
Cursor c = mDb.query(false, DATABASE_TABLE, new String[] {KEY_TOTAL},
KEY_TYPE + "=" + t, null, null, null, null, null);
Have you checked for thrown exceptions? Especially NumberFormatException? I'm guessing there is some other logic problem that is causing the loop to exit prematurely.

Neighbouring cell information is inaccurate

I'm trying to make use of a mobile information about neighbouring cells, available on Android via TelephonyManager class and its getNeighboringCellInfo method. Below I'm posting a part of code (mostly taken from publicly available sources) which does the job, and one example of output which this code produces (shown in attached screenshot). The code and the image are placed "as is" without any changes, so it should be relatively easy to associate one with the other and make sure it should work correctly (of course, there can be errors which I overlooked).
The problem is that the list of neighbouring cells does often contain elements with "incorrect" (to my understanding) data, such as:
a NeighboringCellInfo with all properties - lac, cid, psc - set to -1, and only rssi field seems meaningful;
a NeighboringCellInfo with lac equal to 0; does this mean that the lac is the same as current active cell?
a NeighboringCellInfo with rssi value outside the range [0, 31] and not UNKNOWN_RSSI; such values can be as positive (33, as shown in the screenshot), as negative (they look like a proper raw rssi value, that is without the need to convert from asu);
list elements obtained in the same geolocation do not demonstrate consistency as much as I'd expect, that is in two consecutive scans every one can have an element omitted in the other one, and the omitted elements' rssi levels are not of a most low level in the list (in fact their rssi's can be larger than for currently used cell); I admit this could be right behaviour if every cell signal tends to be very unstable, but I'm not sure if it's true in general for GSM and/or UMTS networks. Current cell always has all fields defined well, but its rssi can vary very quickly in a range of 30 dBm (say from -60 to -90).
The same as 4, but about consistency from one day to another. In a highly urbanized and matured environment I'd expect to see the same list of cells every day, yet they vary in such a way, that one day I don't even see a mention about a cell that was active cell in a previuos day.
Does all this mean a normal functioning of mobile technology, some sort of possibly powersaving optimizations, or a flaw in a specific device (LG Optimus One in my case)?
Please suggest how can one obtain consistent readings from cell environment on Android, if this is possible.
GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();
String networkOperator = telephonyManager.getNetworkOperator();
int type = telephonyManager.getNetworkType();
String mcc = networkOperator.substring(0, 3);
String mnc = networkOperator.substring(3);
textMCC.setText("mcc: " + mcc + " mnc: " + mnc);
textMNC.setText("operator: " + networkOperator);
int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
int psc = cellLocation.getPsc();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("lac: " + String.valueOf(lac) + " cid: " + String.valueOf(cid) + " psc: " + String.valueOf(psc) + " type: " + String.valueOf(type) + " rssi: " + String.valueOf(currentCellRSSI));
TextView Neighboring = (TextView)findViewById(R.id.neighboring);
List<NeighboringCellInfo> NeighboringList = telephonyManager.getNeighboringCellInfo();
String stringNeighboring = "Neighboring List - Lac : Cid : Psc : type : RSSI\n";
for(int i = 0; i < NeighboringList.size(); i++)
{
String dBm;
int rssi = NeighboringList.get(i).getRssi();
if(rssi == NeighboringCellInfo.UNKNOWN_RSSI)
{
dBm = "Unknown RSSI";
}
else
{
if(rssi >= 0 && rssi < 32)
{
dBm = String.valueOf(-113 + 2 * rssi) + " dBm";
}
else
{
dBm = "Unknown value:" + Integer.toString(rssi);
}
}
stringNeighboring = stringNeighboring
+ String.valueOf(NeighboringList.get(i).getLac()) + " : "
+ String.valueOf(NeighboringList.get(i).getCid()) + " : "
+ String.valueOf(NeighboringList.get(i).getPsc()) + " : "
+ String.valueOf(NeighboringList.get(i).getNetworkType()) + " : "
+ dBm + "\n";
}
Neighboring.setText(stringNeighboring);
Neighboring cells are reported in two different ways:
On GSM/GPRS (which seems to be the network you were on when you took your screenshot) you should get the MCC/MNC/LAC/CID tuple for the neighboring cells. I see you get valid CID values. The PSC will always be -1 if you're on a GSM (2.xG) network, as the PSC has no meaning on GSM (PSC is a CDMA parameter and GSM is TDMA-based).
On UMTS things are different: For neighboring cells only the PSC is reported, and you will not find out their other parameters unless you connect to them.
LTE is in principle similar to UMTS, but with slightly different names: instead of LAC and CID you have TAC (Tracking Area Code) and CI (Cell Identity); instead of a PSC you have a PCI (Physical Cell ID). However, they do essentially the same as their UMTS counterparts.
Note, however, that implementation varies greatly between devices: Some phones will not report PSC even on 3G networks, and some will never report neighboring cells. The Nexus S (as most Samsung-built devices) reports neither.
Not sure about the LAC=0, though. It might mean "same LAC as current cell", in which case it would be interesting to see the output from the boundary of a Location Area, where the phone can pick up cells with multiple LACs. (Would we be seeing cells from both LAs? Or just from "our" LA? What LAC would be reported for cells from a neighboring LA?)
The mobile device should be aware of the neighbouring cells, so that it can hand over to a better cell if necessary. In any case, you've told it to get information about neighbouring cells, so that's what it should do. Your results don't seem to match up to what is described in the Android documentation either.
I would report this problem to the device vendor as a firmware bug.
I can imagine a situation where you would see a neighbouring cell with a stronger signal if for example the cell was GSM, and the device preferred a UMTS cell with a weaker signal, but this should be obvious from the network type field.

Complex WHERE condition in Android cursor query

I want to query the Contacts data and retrieve a contact name and a phone number with the following condition: if a contact has a mobile number then pick that number, else pick any number/first number the contact has. Is it possible to formulate this condition in a cursor query or would I have to do it within a custom cursor adapter?
This is the code I have at the moment. It works fine but it retrieves all numbers for all contacts, therefore I get duplicate names if a person has more than one contact.
private String WHERE_CONDITION = ContactsContract.Data.MIMETYPE + " = '" +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'";
private String[] PROJECTION = {ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.DATA1, ContactsContract.Data._ID };
private String SORT_ORDER = ContactsContract.Data.DISPLAY_NAME;
cursor = this.getContentResolver().query(
ContactsContract.Data.CONTENT_URI, PROJECTION, WHERE_CONDITION, null, SORT_ORDER);
Any help is much appreciated!
It would be much easier and simpler to put this logic in Java. Just retrieve all numbers and select the one you need. Typically you will have at most 3 numbers, may be 5. In any case overhead will be very low.

Categories

Resources