I want to show Contact name (if contact is saved in my phone) instead of showing phone no (address) inside recyclerview - sqlite

In my app I have listed all the SMS messages that are available inside my phone, I added it but
I wish to add contact name (whatever saved in my phone) at the place where contact number is showing.
I also added all the required permission to show SMS in Android Manifest file
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
if anyone know how to achieve this in android studio, it will be very helpfull for me
Thankyou in advance.
I have attached an image below
public void showSMSData() {
ContentResolver contentResolver = getContentResolver();
Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/"), null, null, null, null);
int indexBody = smsInboxCursor.getColumnIndex("body");
int indexAddress = smsInboxCursor.getColumnIndex("address");
if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
arrayList.clear();
addresses.clear();
do {
if (!addresses.contains(smsInboxCursor.getString(indexAddress))) {
String str = smsInboxCursor.getString(indexBody) + "\n";
SMSModel smsModel = new SMSModel(smsInboxCursor.getString(indexAddress), str);
addresses.add(smsInboxCursor.getString(indexAddress));
arrayList.add(smsModel);
}
} while (smsInboxCursor.moveToNext());
}
I wish to add contact name (whatever saved in my phone) at the place where contact number is showing
if anyone know how to achieve this in android studio

Related

Working way to transfer SQLite database to another device without external storage permissions (updates Android 11)

With new Shared Storage and new Android API 30 requirements I can't find a way to recover a database on new or another device.
Until this time, I used File API to create a copy of the database. Any time user could put this file to any cloud, download from cloud on another device and restore the database on it. Of course, I used WRITE_EXTERNAL_STORAGE.
Now, Google rejects the app and says that we should use MediaStore API which can solve that issues without any permissions. I tried it and it works only on same device. And until if created the database file not copied to/from cloud or moved to/from another folder. In that case cursor.getCount == 0:
String[] projections = new String[]{MediaStore.Downloads._ID};
String selection = MediaStore.Downloads.DISPLAY_NAME + "=?";
String[] selectionArgs = new String[]{"database.db"};
String sortOrder = MediaStore.Downloads.DISPLAY_NAME + " ASC";
Cursor cursor = getApplicationContext().getContentResolver().query(MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL), projections, selection, selectionArgs, sortOrder);
Uri uri = null;
if (cursor != null) {
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID);
cursor.moveToNext();
long id = cursor.getLong(idColumn);
uri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, id);
try {
ContentResolver contentResolver = getApplicationContext().getContentResolver();
ParcelFileDescriptor parcelFileDescriptor = contentResolver.openFileDescriptor(uri, "rw");
FileInputStream fileInputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
File dbFile = new File(Environment.getDataDirectory(), "/data/com.example.app/databases/database.db");
FileOutputStream fileOutputStream = new FileOutputStream(dbFile);
byte[] buf = new byte[1024];
int len;
while ((len = fileInputStream.read(buf)) > 0) {
fileOutputStream.write(buf, 0, len);
}
fileOutputStream.close();
fileInputStream.close();
parcelFileDescriptor.close();
restartApp();
} catch (IOException e) {
e.printStackTrace();
}
cursor.close();
}
Something wrong with code or MediaStore API does not provide any actions with created files?
E/DBError: exception
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Also I tried Google Drive API to copy the database file on cloud directly from app and download it. But it's not working way too, because new device don't know about file ID which used to download files.
Any thoughts on this?
Figured out.
Without any storage permissions your app have access to onw created files with MediaStore API.
But haven't if:
file was moved/copied inside device storage
file was uploaded to cloud and then downloaded from it
app was reinstalled
I tested it on versions 28 (emulation), 29 (device), 30 (emulation).
So in my case anyway I need:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="29"/>
Don't know why but for read own file on Android Q not enough only READ_EXTERNAL_STORAGE
I was lucky and Google approved app. Hope you too.

How to open specific page in application from URL Shared link Xamarin forms

I need to do two things : First , I want to share Model to social media For example I have product which has title , text, id(or specific url)
I use share.plugin to share this model , Second , I want when user click the link if app is installed it navigate to specific page
else it navigate to web site page I followed links which talks about deep linking and app indexing but i didn't find something like i want
Note : I Used prism for xamarin forms
Update : I Tried this
private void RegisterLink()
{
//var url = $"https://unelectronics.com/ar/product/{GenerateSEOTitle(ProductData.ID, ProductData.SKUName)}".Trim();
var url = "https://unelectronics.com/ar/product/888-lg-55c7v-55-4k-oled-smart-tv";
var entry = new AppLinkEntry
{
Title = ProductData.Title,
AppLinkUri = new Uri(url, UriKind.RelativeOrAbsolute),
IsLinkActive = true,
};
entry.KeyValues.Add("contentType", "product");
entry.KeyValues.Add("companyName", "UNElectronics");
entry.KeyValues.Add("appName", "UNElectronics");
Application.Current.AppLinks.RegisterLink(entry);
}
which is applink concept but it doesn't work for me
Any help ?

save image on file system instead of DB

I m developping an application where user can provide text informations and images. I want to save images on file system and in DB, I will add a link of each user to its images So I need to add the user ID to the image name to not get images with same name. I m using SPRING MVC.
in my Controller :
#RequestMapping(value="/save",method=RequestMethod.POST)
public String add ( #RequestParam("prix") Long prix,
RequestParam("adresse") String ville,
#RequestParam("categorie") String categorie,
#RequestParam("photos") MultipartFile file,
) throws FileNotFoundException
{
String chemin=null;
if (!file.isEmpty())
{
try {
String orgName = file.getOriginalFilename();
// this line to retreive just file name
String
name=orgName.substring(orgName.lastIndexOf("\\")+1,orgName.length());
chemin="e:\\images\\"+name; //here I want to add id (auto generated)
File file1=new File(chemin);
file.transferTo(file1);
} catch (IOException e) {
e.printStackTrace();
}
}
annonce.setImage(chemin);
annonce.setTitre(prix);
annonce.setCorps(ville);
annonce.setPrix(cetegorie)
annoncedao.save(annonce);
return "SuccessAddAnnonce";
}
but I can not get the ID witch is auto generated so I can not get it throw #RequestParam like adress or categorie because it is auto generated and I still don t have it until we call save method witch is last .
Any suggestions are welcome .
I got the Id using query with Select max (id)
#Query("SELECT MAX(id) FROM Annonce")
Long findAutoincrementAnnonce();
I wish help someone.
You can generate a random ID for each user and assign it to a particular user, and thus its image name also (what you intend to do here I guess).
I will illustrate it simply.
// Get a random ID coressponding to the user address or whatever, here i will just use address
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] hash = digest.digest(ville.getBytes("UTF-8"));
String randomId = DatatypeConverter.printHexBinary(hash);
// Here you have a uniqueID for an address.
// You could have just used ville.hashCode() also but it might give you collisions with some different Strings.
if Input: "64th Street, Downtown, StackOverFlow" then
randomId = 8AB126F8EBC0F7B5CCBBEB3E21582ADF

How to know if invitee is installing the app first time by clicking on app invite link

After getting app invite, invitee clicks on link.
Based on app already installed or not, he will be directed to app store and finally come to Actiity which handles AppInite link.
My deep link look like this:
http://example.com/app-invite/
Where user_id is id of registed user (in my backend server). I am able to get the correct id of the user.
This is the code for handling deep link.
private void processReferralIntent(Intent intent) {
String invitationId = AppInviteReferral.getInvitationId(intent);
String deepLink = AppInviteReferral.getDeepLink(intent);
String userId = deepLink.substring(deepLink.lastIndexOf("/") + 1);
Utility.displayToast("userid " + userId);
// Handle the deep link. For example, open the linked
// content, or apply promotional credit to the user's
// account.
Log.d(TAG, "Found Referral: " + invitationId + ":" + deepLink);
((TextView) findViewById(R.id.deep_link_text))
.setText(getString(R.string.deep_link_fmt, deepLink));
((TextView) findViewById(R.id.invitation_id_text))
.setText(getString(R.string.invitation_id_fmt, invitationId));
}
Now if this is the first time invitee is installing the app, on clicking on app invite link, i want to give some promotion credits to both invitee and inviter.
How can i know this is the first time? Beacuse even app is installed alredy processReferralIntent() will be called.

How to switch to a different window using selenium webdriver java?

I am trying to switch to a New window which gets displayed when I click on the Debt Pricing Template. But I am unable to do that as a result of which I am not able to proceed with further scripting... The problem is I am not able to know what should I pass in the switchTo.window() because Pricing Approval Gateway window displays and following is the HTML for the new window:
<*h1 class="pageType noSecondHeader">Pricing Approval Gateway<*/h1>
Following is the code:
LoginPage2.driver.findElement(By.linkText("TEST ORG")).click();
System.out.println("3.Select Dept pricing template button from the organization detail page.");
if(LoginPage2.driver.findElement(By.name("debt_pricing_template")).isDisplayed())
System.out.println("User should able to navigate to Dept pricing template and template display few question, user have answer these question for further navigation.");
LoginPage2.driver.findElement(By.name("debt_pricing_template")).click();
LoginPage2.driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
LoginPage2.driver.switchTo().window("bPageTitle");
Please advise what needs to be added?
I never used it because in my tests I am not using any new windows, but this should help:
Set<string> handlers = driver.getWindowHandles();
if (driver.getWindowHandles().size()>= 1){
for(String handler : handlers){
driver.switchTo().window(handler);
if (driver.getElement(By.tagName("h1")).contains("Pricing")){
System.out.println("Get focus on Popup window");
break;
}
}
}
else System.out.println("No windows founded!");
I am not quite sure with the h1 approach. So if it does not help, try before opening new window storing current window to String:
String mainWindow = driver.getWindowHandle();
Then click the link (or do something else as you are doing now) to open new window. Then to switch to the new window:
Set<string> handlers = driver.getWindowHandles();
for (String handler : handlers){
if (!handler.equals(mainWindow)){
driver.switchTo(handler);
break;
}
}
And then to switch back to original window just do:
driver.switchTo(mainWindow);
Ofcourse the driver variable is expected live instance of
WebDriver
driver.findElement(By.linkText("Go to Billing Summary")).click();
driver.findElement(By.linkText("01 Mar 2016")).click();
Thread.sleep(5000);
driver.findElement(By.linkText("AMS TAX")).click();
driver.findElement(By.linkText("00842")).click();
Set<String> instancewindow= driver.getWindowHandles();
Iterator<String> it = instancewindow.iterator();
String parent =it.next();
String child = it.next();
driver.switchTo().window(child);
driver.switchTo().frame("modalSubWindow");
driver.findElement(By.linkText("View More Vehicle Details>>")).click();
driver.switchTo().window(parent);

Resources