Android get gmail email subject from notification - android

i would like to read the subject of a new received email (gmail) from an android notification.
I think i have to use a NotificationListenerService running in background, catch the notification of new email in real time, and read the subject from that.
But how can i read the subject or a part of body text? Is it possible?
Thank you very much.

Here is an example that catches notifications and puts them into a text file. I only made this for development purposes so I could see how each app populates the notifications but it should show you how to do it.
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
new Store().execute(sbn);
}
private class Store extends AsyncTask<StatusBarNotification, Integer, Long> {
#Override
protected Long doInBackground(StatusBarNotification... params) {
String temp = "";
StatusBarNotification sbn = params[0];
Notification not = sbn.getNotification();
String pack = sbn.getPackageName();
// dump all the android notifications
if (pack.startsWith("com.android")) return null;
if (pack.startsWith("com.estrongs")) return null;
if (pack.startsWith("com.motorola")) return null;
count++;
if (not.category != null)
temp += "Category: " + not.category.toString() + "\n";
else
temp += "Category = null \n";
if (not.tickerText != null)
temp += "TickerText: " + not.tickerText.toString() + "\n";
else
temp += "TickerText = null \n";
temp += "Key: " + sbn.getKey() + "\n" + "ID: " + sbn.getId() + "\n";
SimpleDateFormat format = new SimpleDateFormat("DD-kk:mm:ss:SSS");
Long ptime = sbn.getPostTime();
temp += "Post time: " + format.format(ptime) + "\n";
Long nottime = not.when;
temp += "When: " + format.format(nottime) + "\n";
temp += "Extras..." + "\n";
Bundle bun = not.extras;
if (bun.getString(Notification.EXTRA_BIG_TEXT) != null)
temp += "BigText: " + bun.getString(Notification.EXTRA_BIG_TEXT).toString() + "\n";
else
temp += "BigText = null \n";
if (bun.getString(Notification.EXTRA_SUMMARY_TEXT) != null)
temp += "SummaryText: " + bun.getString(Notification.EXTRA_SUMMARY_TEXT).toString() + "\n";
else
temp += "SummaryText = null \n";
if (bun.getString(Notification.EXTRA_INFO_TEXT) != null)
temp += "InfoText: " + bun.getString(Notification.EXTRA_INFO_TEXT).toString() + "\n";
else
temp += "InfoText = null \n";
if (bun.getString(Notification.EXTRA_TEXT) != null)
temp += "Text: " + bun.getString(Notification.EXTRA_TEXT).toString() + "\n";
else
temp += "Text = null \n";
if (bun.getString(Notification.EXTRA_SUB_TEXT) != null)
temp += "SubText: " + bun.getString(Notification.EXTRA_SUB_TEXT).toString() + "\n";
else
temp += "SubText = null \n";
if (bun.getString(Notification.EXTRA_TITLE) != null)
temp += "Title:" + bun.getString(Notification.EXTRA_TITLE).toString() + "\n";
else
temp += "Title = null \n";
if (bun.getString(Notification.EXTRA_TITLE_BIG) != null)
temp += "Big Title:" + bun.getString(Notification.EXTRA_TITLE_BIG).toString() + "\n";
else
temp += "Big Title = null \n";
temp += "Fields... \n";
CharSequence[] lines = bun.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
if (lines != null) {
for (CharSequence line : lines) {
temp += "line: " + line.toString() + " \n";
}
} else {
temp += " no lines... \n";
}
file = new File(save, pack + count + ".txt");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(temp.getBytes());
fos.close();
//Toast.makeText(this, "File written", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
//Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
//Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
return null;
}
}
Notice I send the SBN to a background thread. The listener does not like much of anything in its thread. Also, the listener can be tricky with the debugger. I found that you need to disable the app in Android settings -> Sound & Notification -> Notification Access before you update the app. Then you can re-enable it once a package update is installed.
Hope that helps.

Related

How to retrieve multi-line text from Edittext?

I want to get a text(Multi-line) from Edittext same as given Screenshot.
I want below output when getText() from Edittext.
Output:
Lorem Ipsum is simply dummy
text of the printing and
typesetting industry. Lorem
Ipsum has been the industry
standard dummy text.
I have tried below solution but, it doesn't work
etMessage.getText().toString().replaceAll("\\n", "<br />")
By default all the EditText widgets in Android are multi-lined. And you can configure the number of lines and the characters types. By setting the input type to multiline do the trick.
<EditText
...
android:inputType="textMultiLine" <!-- Multiline input -->
...
android:lines="8" <!-- Total Lines prior display -->
android:minLines="6" <!-- Minimum lines -->
android:gravity="top|left" <!-- Cursor Position -->
android:maxLines="10" <!-- Maximum Lines -->
android:layout_height="wrap_content" <!-- Height determined by content -->
android:layout_width="match_parent" <!-- Fill entire width -->
android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>
After too much searching and waiting for an answer to this question. I have resolved this issue.
Solution:
I have measured each and every line & words for preserve this as Multiline text, you can use below function for that.
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
float density = metrics.density;
String result = fitString(ipText, ipText.getText().toString());
private String fitString(EditText editText, String message) {
Log.i(TAG, "fitString: Default String : " + message);
StringBuilder finalMessage = new StringBuilder();
if (isTooLarge(editText, message)) {
Log.i(TAG, "fitString: isTooLarge 1 : " + true);
List<String> lineList = Arrays.asList(message.split("\n"));
Log.i(TAG, "fitString: stringList" + lineList);
if (lineList != null && lineList.size() > 0) {
for (int i = 0; i < lineList.size(); i++) {
if (lineList.get(i) != null && !lineList.get(i).isEmpty()) {
if (isTooLarge(editText, lineList.get(i))) {
Log.i(TAG, "fitString: isTooLarge 2 : " + lineList.get(i) + " == " + true);
List<String> wordList = Arrays.asList(lineList.get(i).split(" "));
Log.i(TAG, "fitString: wordList" + wordList);
if (wordList != null && wordList.size() > 0) {
Log.i(TAG, "fitString: wordList : " + wordList.size());
StringBuilder temp = new StringBuilder();
String lastWord = "";
for (int j = 0; j < wordList.size(); j++) {
if (wordList.get(j) != null && !wordList.get(j).isEmpty()) {
if (isTooLarge(editText, wordList.get(j))) {
Log.i(TAG, "fitString: isTooLarge 3 : " + wordList.get(j) + " == " + true);
String newString = fitCharacter(editText, wordList.get(j));
Log.i(TAG, "fitString: fitCharacter == " + newString);
if (j == (wordList.size() - 1) && i == (lineList.size() - 1)) {
finalMessage.append(newString);
} else {
finalMessage.append(newString + "\n");
}
} else {
if (j == 0) {
lastWord = wordList.get(j);
} else {
lastWord = " " + wordList.get(j);
}
temp.append(lastWord);
Log.i(TAG, "fitString: temp : " + temp);
Log.i(TAG, "fitString: lastWord : " + lastWord);
if (isTooLarge(editText, temp.toString())) {
temp.setLength(0); // clear String Builder, new StringBuilder()
temp.append(lastWord);
if (j == (wordList.size() - 1) && i != (lineList.size() - 1)) {
Log.i(TAG, "fitString: ###### 1");
finalMessage.append("\n" + lastWord.trim() + "\n");
} else {
Log.i(TAG, "fitString: ###### 2");
finalMessage.append("\n" + lastWord.trim());
}
} else {
if (j == (wordList.size() - 1) && i != (lineList.size() - 1)) {
Log.i(TAG, "fitString: ###### 3");
finalMessage.append(lastWord + "\n");
} else {
Log.i(TAG, "fitString: ###### 4");
finalMessage.append(lastWord);
}
}
Log.i(TAG, "fitString: finalMessage : " + finalMessage);
}
} else {
Log.e(TAG, "fitString: Word is Null or Empty.");
finalMessage.append(" ");
}
}
} else {
Log.e(TAG, "fitString: wordList is Null or Empty.");
}
} else {
Log.i(TAG, "fitString: isTooLarge 2 : " + lineList.get(i) + " == " + false);
if (i == (lineList.size() - 1)) {
finalMessage.append(lineList.get(i));
} else {
finalMessage.append(lineList.get(i) + "\n");
}
}
} else {
Log.e(TAG, "fitString: Line is Null or Empty.");
finalMessage.append(lineList.get(i) + "\n");
}
}
} else {
Log.e(TAG, "fitString: stringList is Null or Empty.");
finalMessage.append("");
}
return finalMessage.toString();
} else {
Log.i(TAG, "fitString: isTooLarge : " + false);
return message;
}
}
public String fitCharacter(EditText editText, String message) {
Log.i(TAG, "fitCharacter2: Default Word : " + message);
StringBuilder finalWord = new StringBuilder();
int startIndex = 0;
int endIndex = 1;
for (; ; ) {
String tempSplitWord = message.substring(startIndex, endIndex);
Log.i(TAG, "fitCharacter2: startIndex : " + startIndex + " endIndex : " + endIndex + " tempSplitWord : " + tempSplitWord);
if (!isTooLarge(editText, tempSplitWord)) { // isTooLarge
if (endIndex < message.length()) {
endIndex = endIndex + 1;
Log.i(TAG, "IF fitCharacter2: endIndex < message.length() " + endIndex + " < " + message.length());
} else {
String result = finalWord.append(tempSplitWord).toString();
Log.i(TAG, "IF RETURN RESULT : " + result);
return result;
}
} else {
endIndex = endIndex - 1;
String splitWord = message.substring(startIndex, endIndex);
Log.i(TAG, "ELSE fitCharacter2: startIndex : " + startIndex + " endIndex : " + endIndex + " splitWord : " + splitWord);
boolean isTooLarge = isTooLarge(editText, splitWord);
if (!isTooLarge) {
finalWord.append(splitWord + "\n");
}
startIndex = endIndex;
endIndex = endIndex + 1;
Log.i(TAG, "ELSE fitCharacter2: startIndex : " + startIndex + " endIndex : " + endIndex);
}
}
}
private boolean isTooLarge(EditText editText, String newText) {
if (editText != null && editText.getPaint() != null) {
float textWidth = editText.getPaint().measureText(newText);
return (textWidth >= (editText.getMeasuredWidth() - (12 * density))); // editText.getMeasuredWidth();
} else {
return false;
}
}
For next comers, I find the accepted answer overcomplicated for the task. Here is a nice extension code in Kotlin that uses Paint.breakText(). That said, it can probably be simplified further...
fun EditText.getMultilineText(): String {
val maxWidth = (width - paddingLeft - paddingRight).toFloat()
val original = text.toString().trim()
val len = original.length
val multiline = mutableListOf<String>()
var p = 0
var count = -1
while (count != 0) {
count = paint.breakText(original, p, len, true, maxWidth, null)
if (p + count < len) {
var tmp = count
while (tmp > 0 && original[p + tmp - 1] != ' ') {
tmp -= 1
}
if (tmp > 0) {
count = tmp
}
}
val tmp = original.substring(p, p + count).trim()
if (tmp.isNotBlank()) {
multiline.add(tmp)
}
p += count
}
return multiline.joinToString("\r\n")
}
did you try this one
message = etMessage.getText().toString().replaceAll("\\n", "<br />")
please see this also How can I preserve line breaks from EditText?

How to Extract Name,Phone Number and Email Address From Vision OCR result text in Android?

Here is my sample code where i want to get details...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PHOTO_REQUEST && resultCode == RESULT_OK) {
launchMediaScanIntent();
try {
Bitmap bitmap = decodeBitmapUri(this, imageUri);
if (detector.isOperational() && bitmap != null) {
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> textBlocks = detector.detect(frame);
String blocks = "";
String lines = "";
String words = "";
for (int index = 0; index < textBlocks.size(); index++) {
//extract scanned text blocks here
TextBlock tBlock = textBlocks.valueAt(index);
blocks = blocks + tBlock.getValue() + "\n" + "\n";
for (Text line : tBlock.getComponents()) {
//extract scanned text lines here
lines = lines + line.getValue() + "\n";
for (Text element : line.getComponents()) {
//extract scanned text words here
words = words + element.getValue() + ", ";
}
}
}
if (textBlocks.size() == 0) {
scanResults.setText("Scan Failed: Found nothing to scan");
} else {
scanResults.setText(scanResults.getText() + "Blocks: " + "\n");
scanResults.setText(scanResults.getText() + blocks + "\n");
scanResults.setText(scanResults.getText() + "---------" + "\n");
scanResults.setText(scanResults.getText() + "Lines: " + "\n");
scanResults.setText(scanResults.getText() + lines + "\n");
scanResults.setText(scanResults.getText() + "---------" + "\n");
scanResults.setText(scanResults.getText() + "Words: " + "\n");
scanResults.setText(scanResults.getText() + words + "\n");
scanResults.setText(scanResults.getText() + "---------" + "\n");
}
} else {
scanResults.setText("Could not set up the detector!");
}
} catch (Exception e) {
Toast.makeText(this, "Failed to load Image", Toast.LENGTH_SHORT).show();
Log.e(LOG_TAG, e.toString());
}
}
}
You have nice libraries to parse link (emails,websites etc) like org.nibor.autolink
Concerning numbers you can have a look to libphonenumber. It is proposed by google and used by android. If you provide the country it can parse for you any format of the number.
Concerning names it is difficult. If you are using your app only for a country you can create a database with the names (in france we have a file in opendata proposed by a public servive) but it won't be complete...

How do I get the string array from a Notification bundle?

I have an app that reads notifications to the driver while driving. I am trying to parse the various notifications. Those that use the old 'tickerText' work fine but several apps like Skype don't support that. Instead, the text in the notification after the first message just says something like "3 new messages". Not very helpful. I want the last message string instead. I wrote this:
public class Catcher extends NotificationListenerService {
File file;
String dir;
File save;
int count = 0;
#Override
public void onCreate() {
super.onCreate();
dir = Environment.getExternalStorageDirectory() + "/NotCatch";
save = new File(dir);
if(!save.exists()){
save.mkdirs();
}
//Toast.makeText(this, dir + " " + file.getName(), Toast.LENGTH_LONG).show();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
file = new File(save, "NotCatch" + count + ".txt");
count++;
String temp = "";
String[] lines;
Notification not = sbn.getNotification();
temp += "Category: " + not.category+ "\n";
temp+= "TickerText: " + not.tickerText + "\n";
temp += "Extras..."+ "\n";
Bundle bun = not.extras;
temp+= "BigText: " + bun.getString(Notification.EXTRA_BIG_TEXT)+ "\n";
temp+= "SummaryText: " + bun.getString(Notification.EXTRA_SUMMARY_TEXT)+ "\n";
temp+= "InfoText: " + bun.getString(Notification.EXTRA_INFO_TEXT)+ "\n";
temp+= "Text: " + bun.getString(Notification.EXTRA_TEXT)+ "\n";
temp+= "TextLines: " + bun.getString(Notification.EXTRA_TEXT_LINES)+ "\n";
temp+= "SubText: " + bun.getString(Notification.EXTRA_SUB_TEXT) + "\n";
temp+= "Title:" + bun.getString(Notification.EXTRA_TITLE) +"\n";
temp+= "Big Title:" + bun.getString(Notification.EXTRA_TITLE_BIG) +"\n";
/* lines = bun.getString(Notification.EXTRA_TEXT_LINES).split("\n");
temp += "Lines... \n" ;
int cnt = 0;
for (String line : lines) {
temp+= cnt + ": " + line + "\n";
cnt++;
}*/
Looper.prepare();
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(temp.getBytes());
fos.close();
Toast.makeText(this,"File written", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
This is just what I have so far for testing of course. I can get the bundle from the notification but it can't get the string array of the message lines. It looks like this in the debugger. 'bun' below is the 'Bundle' from the 'Notification'.
My question is this. How do I get the array of strings that start with "some message text - 4"? This is the bundle from an Android Notification.
I finally was able to get the strings this way
CharSequence[] lines = bun.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
for(CharSequence line : lines){
temp+= "line: " + line.toString() + "\n";
}
Since the strings you are trying to retrieve are contained in the ArrayMap mMap (according to the debugger image), and as implementations of Map are serializable, you'll want to do the following to first retrieve that map:
ArrayMap map = bun.getSerializableExtra("mMap");
Then, you should be able to get the array of strings you want (which according to the debugger image is an array of CharSequence objects) with the following:
CharSequence[] messages = (CharSequence[]) map.valueAt(1);
An interesting read on bundles and maps:
https://medium.com/the-wtf-files/the-mysterious-case-of-the-bundle-and-the-map-7b15279a794e#.kf3m2haa3

how to parse data from custom message extension in android asmak

<message id="64Vz5-10" to="210#localhost" type="chat"><body>idirkfkckckckcjdjdnx hdhdjcj</body><MediaType xmlns="jabber:client">1</MediaType><MediaPlaceHolder xmlns="jabber:client">AFGDSGYWY+VXXBNVNXVVX+J3VGSFSJFSSJSFJFS+FJS</MediaPlaceHolder><RemoteURL xmlns="jabber:client">http://www.google.com</RemoteURL></message>
How to parse values from this given response in android ?
I am trying to fetch values for MediaType, MediaPlaceHolder and RemoteURL.
This is what I am doing :
if (packet instanceof Message) {
Message message = (Message) packet;
if (message.getBody() != null) {
Log.e(" message.toXML()", message.toXML() + '\n' + " : " + packet.toXML());
//Get extension from message
try {
CustomExtension fileExt = (CustomExtension) message.getExtension(CustomExtension.NAMESPACE);
// PacketExtension ext = message.getExtension(CustomExtension.ELEMENT_MEDIA_HOLDER);
// Log.e("File Extension ", "description:----" + ext.toXML());
if (fileExt != null) {
//Get values from extension
String filePath = fileExt.getFileType();
System.out.println("Message --:" + message.getBody() + " File url:--" + filePath);
}
} catch (Exception e) {
e.printStackTrace();
}
String fromName = StringUtils.parseBareAddress(message.getFrom());
I am getting ClassCastException:
org.jivesoftware.smack.packet.DefaultPacketExtension

Get repeated Entries for Some Items in DataBase!

I am working on an android application. In which i have slideShows. I am parsing these through an xml and after parsing them, saving in the SQLite DB. Majority of the slideshows are saved properly but, sometimes this happens that the slides are saved two times that is, every slide in the slideShow is saved two times obviously with different PK but same content. which should be avoided.
Partial code is here, where i am getting the slides and trying to store them in DB.
ArrayList<SlideShowItem> slideItems = null;
slideItems=Utils.database.getSlideItemOfUrl(Constants.StoriesTable,tempSlideShow.getFullStoryUrl().substring(0, index - 1), type);
if (slideItems == null) {
Log.d("store in DB: ", " when SlideItems == null ");
Log.d("SlideShow Title: ", tempSlideShow.getTitle());
Log.d("SlideShow pub Date: ", tempSlideShow.getPubDate());
slideItems = tempSlideShow.getSlideShow();
Utils.database.storeSlideItem(Constants.StoriesTable, myUrl,slideItems, type);
Utils.topStorySlidesArrayList = slideItems;
slideItems = null ;
} else {
Log.d("SlideShow Title: ", tempSlideShow.getTitle());
Utils.topStorySlidesArrayList = slideItems;
slideItems = null ;
}
and code of function storeSlideItem in DataBase is:
public synchronized void storeSlideItem(String tableName, String, url,ArrayList<SlideShowItem> list, String type) {
System.out.println("size of the Array list: " + list.size());
String newType = null;
if (type == null) {
newType = "List";
}else{
newType = type;
}
ArrayList<SlideShowItem> newList = new ArrayList<SlideShowItem>();
//newList = null;
Iterator<SlideShowItem> iterator = list.iterator();
while (iterator.hasNext())
{
SlideShowItem sSItem = iterator.next();
if(!newList.contains(sSItem))
{
newList.add(sSItem);
}
}
try {
for (int i = 0; i < newList.size(); i++) {
SlideShowItem item = newList.get(i);
String itemUrl = url + i;// Unique URL for the DB;
String imgString = null;
Log.e("Loop Counter", " time " + i);
Drawable drawable = item.getImage();
if (item.getBody() != null) {
item.setBody(item.getBody().replace('\'', '`'));
// replace as it create syntax error for storing data
}
if (item.getSubTitle() != null) {
item.setSubTitle(item.getSubTitle().replace('\'', '`'));
}
if (drawable != null) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
imgString = Base64.encodeBytes(b);
}
if (isOpen()) {
myDB.execSQL("INSERT INTO " + tableName + "(" + column[1] + "," + column[2] + "," + column[3] + "," + column[4] + "," + column[6]
+ "," + column[7] + ",type) VALUES('" + itemUrl + "','" + item.getSubTitle() + "','" + item.getBody() + "','"
+ item.getImagePath() + "','" + item.getIndex() + "','" + imgString + "','" + newType + "Slide')");
if (item.getBody() != null) {
item.setBody(item.getBody().replace('`', '\''));// " ' "
// replace as it create syntax error for storing data
}
if (item.getSubTitle() != null) {
item.setSubTitle(item.getSubTitle().replace('`', '\''));
}
if (tableName.equals(Constants.StoriesTable)) {
item.setItemId(getItemID(tableName, itemUrl));
Utils.hashListStoriesIds.put(itemUrl, item.getItemId());
if (imgString != null) {
Utils.hashListImages.put(item.getItemId(), new Boolean(true));
} else {
Utils.hashListImages.put(item.getItemId(), new Boolean(false));
}
}
}
}
} catch (Exception e) {
Log.e("Error", "Exception: storeSlideItem type " + e.toString());
} finally {
closeConnection();
}
}
Please tell me anything that can get me out of this irritating problem. Any help is appreciated.
in DB for duplication of slides the view is somewhat like:
1 abc USA 111
2 abc USA 111
and so on this was for one slide of a slideShow. if i have 3 slides in a slideshow, i'll get 6 entries in DB each slide being saved for two times.
Use HashSet instead of ArrayList

Categories

Resources