Android Calendar Provider: How can I delete my own local calendars? - android

I am just learning how to work with the Android Calendars. So far, I am able to display the info about existing calendars. I can also create my own local calendars -- the test code like:
private void createCalendarTest()
{
Uri.Builder builder = Calendars.CONTENT_URI.buildUpon();
builder.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, "private")
.appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
Uri uri = builder.build();
ContentValues values = new ContentValues();
values.put(Calendars.NAME, "TEST");
values.put(Calendars.CALENDAR_DISPLAY_NAME, "Calendar named TEST");
values.put(Calendars.SYNC_EVENTS, false);
values.put(Calendars.VISIBLE, true);
getContentResolver().insert(uri, values);
}
Actually, I can create many calendars that differ only in _ID. I have read elsewhere that I can create a calendar only when using the sync adapter. Now, how can I delete the calendar? I expect the URI must also contain the sync adapter info, and the _ID of the deleted calendar. I tried the following code, but I was unsuccessful:
private void deleteCalendarTest()
{
Uri.Builder builder = Calendars.CONTENT_URI.buildUpon();
builder.appendPath("6") // here for testing; I know the calender has this ID
.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, "private")
.appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
Uri uri = builder.build();
getContentResolver().delete(uri, null, null);
Toast.makeText(this, "??? deleteCalendarTest() not working", Toast.LENGTH_SHORT).show();
}
How can I fix it?

After reading with more attention the documentation, i found out you should add to the content values the following fields too:
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
values.put(CalendarContract.Calendars.ACCOUNT_NAME, "private");
values.put(CalendarContract.Calendars.ACCOUNT_TYPE,CalendarContract.ACCOUNT_TYPE_LOCAL);
Then everything else should be fine and you should be able to delete the inserted calendar! ;)

"andrea-rinaldi" was right. The below code snippet worked for me. The "calendarHandler" is an instance of the helper class that extends AsyncQueryHandler, the one you used to create the calendar.
Uri calUri = CalendarContract.Calendars.CONTENT_URI;
calUri = calUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, BuildConfig.APPLICATION_ID)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
.build();
calendarHandler.startDelete(0,-1,calUri,null,null);

This Kotlin solution worked for me:
val uri = ContentUris.withAppendedId(CalendarContract.Calendars.CONTENT_URI, calendarId.toLong())
contentResolver.delete(uri, null, null)
I'm removing the calendar with the application (not a SyncAdapter) so there's no need to append any query parameters. Just append the calender's id to the content uri and use a ContentResolver to delete the calendar.

Related

How to update metadata of audio file in Android Q media store?

Updating metadata of audio file in media store is not working in Android Q OS, it works in all other OS.
I am using content provider with uri specified as MediaStore.Audio.Media.EXTERNAL_CONTENT_URI. It is working fine in all below Android Q device. Below is the code that I am using to update track metadata.
ContentValues cv = new ContentValues();
ContentResolver resolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
cv.put(MediaStore.Audio.Media.TITLE, newTitle);
cv.put(MediaStore.Audio.Media.ALBUM, newAlbumName);
cv.put(MediaStore.Audio.Media.ARTIST, newArtistName);
int rowsUpdated = resolver.update(uri, cv,
MediaStore.Audio.Media._ID + " = ? ", new String[]{audioId});
For Android Q device, rowsUpdated is always 0 with no exception.
How are other music player updating tracks metadata in Android Q ?
Finally, it took some time but I figured that out.
First, you need to obtain access to file. Here you can read about that
Next, I found out that to update title or artist fields (maybe others to, I didn't test them) you need to set column MediaStore.Audio.Media.IS_PENDING value to 1. Like that:
val id = //Your audio file id
val values = ContentValues()
values.put(MediaStore.Audio.Media.IS_PENDING, 1)
val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id)
contentResolver.update(uri, values, null, null)
And then you can edit fields that you need. Also to end the update process set MediaStore.Audio.Media.IS_PENDING to 0 again:
val id = //Your audio file id
val title = //New title
val artist = //New artist
val values = ContentValues()
values.put(MediaStore.Audio.Media.IS_PENDING, 0)
values.put(MediaStore.Audio.Media.TITLE, title)
values.put(MediaStore.Audio.Media.ARTIST, artist)
val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id)
contentResolver.update(uri, values, null, null)
So in one function, it would look like this:
#RequiresApi(value = android.os.Build.VERSION_CODES.Q)
fun updateMetadata(contentResolver: ContentResolver, id: Long, title: String, artist: String) {
val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id)
val values = ContentValues()
values.put(MediaStore.Audio.Media.IS_PENDING, 1)
contentResolver.update(uri, values, null, null)
values.clear()
values.put(MediaStore.Audio.Media.IS_PENDING, 0)
values.put(MediaStore.Audio.Media.TITLE, title)
values.put(MediaStore.Audio.Media.ARTIST, artist)
contentResolver.update(uri, values, null, null)
}
It's written in Kotlin but I think you will figure out how to do that in java.
UPDATE
By updating MediaStore you don't updating real file at any android version. That means, if a file would be updated (for example: renamed) and/or scanned by MediaScannerConnection your changes will be lost. This answer is right.
Using Android Q and beyond you have to first get the file
i.e
resolver.openInputStream(uri)?.use { stream -> outputFile.copyInputStreamToFile(stream) }
return outputFile.absolutePath
Helper Function
private fun File.copyInputStreamToFile(inputStream: InputStream?) {
this.outputStream().use { fileOut ->
inputStream?.copyTo(fileOut)
}
}
Then alter the metadata via a third party, I use J Audio Tagger
Then over write the old file
// From https://developer.android.com/reference/android/content/ContentProvider
// String: Access mode for the file. May be
// "r" for read-only access,
// "w" for write-only access (erasing whatever data is currently in the file),
// "wa" for write-only access to append to any existing data,
// "rw" for read and write access on any existing data, and
// "rwt" for read and write access that truncates any existing file. This value must never be null.
mContext.application.contentResolver.openOutputStream(uri, "w")?.use { stream ->
stream.write(file.readBytes())
}
This works fine when the file was created by your app
I've been updating meta data in the MediaStore through a ContentResolver, but this no longer works with Android Q (API 29). The following code gives me a warning, and the description is not updated:
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DESCRIPTION, "Some text");
res = getContext().getContentResolver().update(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values,
MediaStore.Images.Media._ID + "= ?", new String[]{sImageId});
android.process.media W/MediaProvider: Ignoring mutation of
description from com.example.android.someapp.app
This Medium post describes how Google has changed the API for accessing and updating files, but what about updating just the meta data? The warning seems to tell me Google no longer wants to allow third party apps to use the MediaStore, and I also found where the warning comes from: 
https://android.googlesource.com/platform/packages/providers/MediaProvider/+/master/src/com/android/providers/media/MediaProvider.java#2960

Content values are assigned null even after putting values in it

As a part of Unit testing I wanted to insert values to DB using content resolver. But when I tried this,
#Before
public void runBeforeTestSignOut() {
ContentValues values = new ContentValues();
String userId="123";
String userInfo="test";
values.put(UserProvider.USER_ID, userId);
values.put(UserProvider.USER_INFO, userInfo);
System.out.print("Values are "+values);
Uri uri = contentResolver.insert(UserProvider.CONTENT_USER_URI, values);
}
Values are still assigned null. I can’t figure out why this is happening. I found some related questions but couldn’t find a solution. Please help me with this.

How to add SMS with specific date in android

Hi stackoverflow I'm trying to develop an application to add SMS to prrogrammatically, I'm using the following code to add SMS
private void addSMS()
{
Uri uri = Uri.parse("content://sms/");
ContentValues cv2 = new ContentValues();
cv2.put("address", "+91956322222");
cv2.put("date", "1309632433677");
cv2.put("read", 1);
cv2.put("type", 2);
cv2.put("body", "Hey");
getContentResolver().insert(uri, cv2);
cv2.clear();
}
Permissions :
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
Problem is the Time of the message, it's displaying the time we added the message, but not the date we have passed in the list of messages, but when I open the message we added then the time will be correct as our input, please help me to solve this riddle.
Thanks.
This is a known problem I think. Try to add this line in the end:
getContentResolver().delete(Uri.parse("content://sms/conversations/-1"), null, null);
If you want more explanation you should check this out!
Here is the working code, Thanks to #Amulya Khare
private void addSMS()
{
Uri uri = Uri.parse("content://sms/");
ContentValues cv2 = new ContentValues();
cv2.put("address", "+91956322222");
cv2.put("date", "1309632433677");
cv2.put("read", 1);
cv2.put("type", 2);
cv2.put("body", "Hey");
getContentResolver().insert(uri, cv2);
/** This is very important line to solve the problem */
getContentResolver().delete(Uri.parse("content://sms/conversations/-1"), null, null);
cv2.clear();
}
Please follow the link to get more information Android programatically inserted SMS have incorrect timestamp in Messaging apps

Create new synced calendar with android api

i'm trying to create a calendar on my account to fill with events that i get from some websites. I've searched and found some new android 4.0 calendar example that i've modified to obtain what i need. The problem is that the calendar is created, filled with events but not synced with google calendar, so in the next sync it is erased. The funcion i use are these:
This is the one for add the new calendar if don't alreay exist:
public static Uri createCalendarWithName(Context ctx, String name,String accountName) {
Uri target = Uri.parse(CalendarContract.Calendars.CONTENT_URI.toString());
target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountName)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google").build();
ContentValues values = new ContentValues();
values.put(Calendars.ACCOUNT_NAME, accountName);
values.put(Calendars.ACCOUNT_TYPE, "com.google");
values.put(Calendars.NAME, name);
values.put(Calendars.CALENDAR_DISPLAY_NAME, name);
values.put(Calendars.CALENDAR_COLOR, 0x00FF00);
values.put(Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_ROOT);
values.put(Calendars.OWNER_ACCOUNT, accountName);
values.put(Calendars.VISIBLE, 1);
values.put(Calendars.SYNC_EVENTS, 1);
values.put(Calendars.CALENDAR_TIME_ZONE, "Europe/Rome");
values.put(Calendars.CAN_PARTIALLY_UPDATE, 1);
values.put(Calendars.CAL_SYNC1, "https://www.google.com/calendar/feeds/" + accountName + "/private/full");
values.put(Calendars.CAL_SYNC2, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName);
values.put(Calendars.CAL_SYNC3, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName);
values.put(Calendars.CAL_SYNC4, 1);
values.put(Calendars.CAL_SYNC5, 0);
values.put(Calendars.CAL_SYNC8, System.currentTimeMillis());
Uri newCalendar = ctx.getContentResolver().insert(target, values);
return newCalendar;
}
and that one create the new event without interaction:
public static Uri createEventWithName(Context ctx, long id, String name, String data) {
long startMillis = 0;
long endMillis = 0;
int id2=(int)id;
String[] divisi = data.split("/");
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012,Integer.parseInt(divisi[0])-1, Integer.parseInt(divisi[1]));
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012,Integer.parseInt(divisi[0])-1, Integer.parseInt(divisi[1]));
endMillis = endTime.getTimeInMillis();
ContentValues cv = new ContentValues();
cv.put(Events.TITLE, name);
cv.put(Events.DTSTART, startMillis);
cv.put(Events.DTEND, endMillis);
cv.put(Events.CALENDAR_ID, id2);
Log.d("aggiungo a calendario",Integer.toString(id2));
cv.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());
//cv.put(Events.RRULE, "FREQ=DAILY;INTERVAL=2");
Uri newEvent = ctx.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, cv);
return newEvent;
}
I'm not so experienced in Android programming so i think it's a stupid question =) i've read that accountName and Account Type must be the same that the one stored on android device, else the event is cancelled. I get accountName from android api and i think they are correct. The account type seems to work for other....
Thanks to anybody that help me!
Not possible for now.
After a lot of googling I could not find a way to create new syncable calendar in Gmail account (account type "com.google"). I've tried these scenarios:
1. without SyncAdapter and I get the same behaviour - calendar appears in external Google Calendar app with all events I added but shortly after (e.g. after couple of seconds) all events and calendar disappear.
2. with SyncAdapter and com.google authenticator - I get an error during the process
3. with SyncAdapter and com.example authenticator - possible to create calendar but not in Gmail account
Note you can create new account and create new calendar for that account (using SyncAdapter, scenario 3) but it's not a Gmail account so calendar is not syncable with Gmail Calendar (e.g. if you login to Gmail account in web browser and open Google Calendar that calendar you've just created will not show).
from http://developer.android.com/reference/android/provider/CalendarContract.html
public static final String ACCOUNT_TYPE_LOCAL
Added in API level 14 A special account type for calendars not
associated with any account. Normally calendars that do not match an
account on the device will be removed. Setting the account_type on a
calendar to this will prevent it from being wiped if it does not match
an existing account.
See Also ACCOUNT_TYPE Constant Value: "LOCAL"
Same issue i was facing. After making the below change now i am seeing the events permanently sticking to my calendar.
In the addEvent method add the below line
Uri uri = getContentResolver().insert(asSyncAdapter(Events.CONTENT_URI,"abc#gmail.com","com.gmail"), cv);
Code for asSyncAdapter is as below
static Uri asSyncAdapter(Uri uri, String account, String accountType) {
return uri.buildUpon()
.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
}
According to the Javadoc for CalendarContract.Calendars:
"Calendars are designed to be primarily managed by a sync adapter and inserting new calendars should be done as a sync adapter."
If you are not a sync adapter (if you are just an arbitrary app, you are not a sync adapter), that may explain why your new calendar is not getting synchronized like you would expect.

Android's Media Scanner: How do I remove files?

I'm writing an app that removes files that may or may not be listed in any one of the types of media libraries such as music or pictures. While I can use the MediaScannerConnection.scanFile method to add files to the media library there doesn't seem to be any call to notify the service that the file has been removed. Sending it the path of the file that no longer exists doesn't result in the desired behavior either. How should I go about removing items from the library that no longer exist on the Android storage?
I was able to put a method together using bits and pieces from these two questions
What is the String 'volumeName' argument of MediaStore.Audio.Playlists.Members.getContentUri referring to?
How can I refresh MediaStore on Android?
Basically I just run a query on each one of the MediaStore types (Audio, Video and Images) selecting by path and deleting any records I find.
public static void RemoveAllForPaths(String[] paths, Context context)
{
private static final String[] FIELDS = { MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.TITLE };
if(paths == null || paths.length == 0) return;
String select = "";
for(String path : paths)
{
if(!select.equals("")) select += " OR ";
select += MediaStore.MediaColumns.DATA + "=?";
}
Uri uri;
Cursor ca;
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ca = context.getContentResolver().query(uri, FIELDS, select, paths, null);
for(ca.moveToFirst(); !ca.isAfterLast(); ca.moveToNext()){
int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID));
uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
context.getContentResolver().delete(uri, null, null);
}
ca.close();
// More of the same just setting the URI to Video and Images
}
I'm not entirely sure how safe this is to do but it's the only solution I've found so far and some initial testing seems to be working. I invite others to submit other answers if anyone has any further information on this approach or a better method for performing this functionality.
Answer of Spencer Ruport is right, but you don't need to query and open a cursor in order to delete.
So for one file that is music file the code is simple like that:
public void DeleteMP3FromMediaStore( Context context, String path )
{
Uri rootUri = MediaStore.Audio.Media.getContentUriForPath( path );
context.getContentResolver().delete( rootUri,
MediaStore.MediaColumns.DATA + "=?", new String[]{ path } );
}
P.S. I wanted to comment answer of Spencer Ruport but don't have enough reputation yet.
Easy as pie: whenever you add a file, let MediaStore ContentProvider knows about it using
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(fileToAddInMediaStore)));
For deletion: just use
getContentResolver().delete(Uri.fromFile(fileToDeleteFromMediaStore), null, null)
The following works well for me. You can delete or add files using this.
MediaScannerConnection.scanFile(
context,
new String[]{fileToDelete, fileToAdd},
null, null);
The available method is to remove the item from library.
This post is detailed expressed how to add into or remove from the Media Library.
http://androidyue.github.io/blog/2014/01/19/scan-media-files-in-android/ Hopes this could help you.

Categories

Resources