1

While trying to remove calendars from the Android AccountManager's database using Tasker, I've been exploring the Android calendar provider api but I have not been able to find a command to delete a calendar once it is identified.

Based on https://developer.android.com/guide/topics/providers/calendar-provider#calendar, I tried to use the tasker intent commands to first create a calendar with:

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setData(CalendarContract.Calendars._ID);
startActivity(intent);

And I have found an event deleting command here: https://developer.android.com/guide/topics/providers/calendar-provider#delete-event. It think it deletes eventId 201.

private static final String DEBUG_TAG = "MyActivity";
...
long eventID = 201;
...
ContentResolver cr = getContentResolver();
Uri deleteUri = null;
deleteUri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
int rows = cr.delete(deleteUri, null, null);
Log.i(DEBUG_TAG, "Rows deleted: " + rows);

My question is: does anyone have a suggestion on a java command/intent that can be used to delete a calendar account once I found one?


Edit:

I was looking for the accountmanager. After finding the correct String value for parameter "Type" I can get a list of calendars using the getAccountsByType() method. Afterwards I should be able to loop through that list and apply either the [removeAccount(Account account, Activity activity, AccountManagerCallback<Bundle> callback, Handler handler)][4], or the removeAccountExplicitly(Account account) method.

This is an example script that retrieves the list of android Accounts. Part A1-A11 has been tested and verified with export constraints:

  • Package name consists of <1 letter>.<1 letter>.com
  • In advanced app export settings in tasker set the additional permission to:android.permission.GET_ACCOUNTS.

This is the tested tasker .xml file of the example script, which I am currently working on:

    <TaskerData sr="" dvi="1" tv="5.2.bf1">
    <Task sr="task27">
        <cdate>1545421710313</cdate>
        <edate>1545513273938</edate>
        <id>27</id>
        <nme>removeDavAccountsAD</nme>
        <pri>100</pri>
        <Kid sr="Kid">
            <eperm0>android.permission.WRITE_SYNC_SETTINGS</eperm0>
            <eperm1>android.permission.MANAGE_ACCOUNTS</eperm1>
            <eperm2>android.permission.AUTHENTICATE_ACCOUNTS</eperm2>
            <eperm3>android.permission.GET_ACCOUNTS</eperm3>
            <eperm4>android.permission.ACCOUNT_MANAGER</eperm4>
            <flgs>1</flgs>
            <launchID>27</launchID>
            <pkg>a.be.com</pkg>
            <vMin>25</vMin>
            <vTarg>25</vTarg>
            <vnme>1.0</vnme>
            <vnum>27</vnum>
        </Kid>
        <Action sr="act0" ve="7">
            <code>664</code>
            <Str sr="arg0" ve="3">am</Str>
            <Str sr="arg1" ve="3">AccountManager</Str>
            <Str sr="arg2" ve="3">get
{AccountManager} (Context)</Str>
            <Str sr="arg3" ve="3">CONTEXT</Str>
            <Str sr="arg4" ve="3"/>
            <Str sr="arg5" ve="3"/>
            <Str sr="arg6" ve="3"/>
            <Str sr="arg7" ve="3"/>
            <Str sr="arg8" ve="3"/>
            <Str sr="arg9" ve="3"/>
        </Action>
        <Action sr="act1" ve="7">
            <code>664</code>
            <Str sr="arg0" ve="3">accounts</Str>
            <Str sr="arg1" ve="3">am</Str>
            <Str sr="arg2" ve="3">getAccounts
{Account[]} ()</Str>
            <Str sr="arg3" ve="3"/>
            <Str sr="arg4" ve="3"/>
            <Str sr="arg5" ve="3"/>
            <Str sr="arg6" ve="3"/>
            <Str sr="arg7" ve="3"/>
            <Str sr="arg8" ve="3"/>
            <Str sr="arg9" ve="3"/>
        </Action>
        <Action sr="act10" ve="7">
            <code>664</code>
            <Str sr="arg0" ve="3"/>
            <Str sr="arg1" ve="3">am</Str>
            <Str sr="arg2" ve="3">removeAccountExplicitly
{boolean} (Account)</Str>
            <Str sr="arg3" ve="3">accounts[%item]</Str>
            <Str sr="arg4" ve="3"/>
            <Str sr="arg5" ve="3"/>
            <Str sr="arg6" ve="3"/>
            <Str sr="arg7" ve="3"/>
            <Str sr="arg8" ve="3"/>
            <Str sr="arg9" ve="3"/>
            <ConditionList sr="if">
                <Condition sr="c0" ve="3">
                    <lhs>%a_type</lhs>
                    <op>2</op>
                    <rhs>*at.bitfire.davdroid*</rhs>
                </Condition>
            </ConditionList>
        </Action>
        <Action sr="act11" ve="7">
            <code>548</code>
            <Str sr="arg0" ve="3">After: %a_name</Str>
            <Int sr="arg1" val="0"/>
            <ConditionList sr="if">
                <Condition sr="c0" ve="3">
                    <lhs>%a_type</lhs>
                    <op>2</op>
                    <rhs>*at.bitfire.davdroid*</rhs>
                </Condition>
            </ConditionList>
        </Action>
        <Action sr="act12" ve="7">
            <code>664</code>
            <Str sr="arg0" ve="3"/>
            <Str sr="arg1" ve="3">am</Str>
            <Str sr="arg2" ve="3">removeAccount
{AccountManagerFuture} (Account, Activity, AccountManagerCallback, android.os.Handler)</Str>
            <Str sr="arg3" ve="3">accounts[%item]</Str>
            <Str sr="arg4" ve="3"/>
            <Str sr="arg5" ve="3"/>
            <Str sr="arg6" ve="3"/>
            <Str sr="arg7" ve="3"/>
            <Str sr="arg8" ve="3"/>
            <Str sr="arg9" ve="3"/>
            <ConditionList sr="if">
                <Condition sr="c0" ve="3">
                    <lhs>%a_type</lhs>
                    <op>2</op>
                    <rhs>*at.bitfire.davdroid*</rhs>
                </Condition>
            </ConditionList>
        </Action>
        <Action sr="act13" ve="7">
            <code>664</code>
            <Str sr="arg0" ve="3"/>
            <Str sr="arg1" ve="3">am</Str>
<Str sr="arg2" ve="3">removeAccount
{AccountManagerFuture} (Account, AccountManagerCallback, android.os.Handler)</Str>
            <Str sr="arg3" ve="3">accounts[%item]</Str>
            <Str sr="arg4" ve="3"/>
            <Str sr="arg5" ve="3"/>
            <Str sr="arg6" ve="3"/>
            <Str sr="arg7" ve="3"/>
            <Str sr="arg8" ve="3"/>
            <Str sr="arg9" ve="3"/>
            <ConditionList sr="if">
                <Condition sr="c0" ve="3">
                    <lhs>%a_type</lhs>
                    <op>2</op>
                    <rhs>*at.bitfire.davdroid*</rhs>
                </Condition>
            </ConditionList>
        </Action>
        <Action sr="act14" ve="7">
            <code>40</code>
        </Action>
        <Action sr="act15" ve="7">
            <code>665</code>
            <Int sr="arg0" val="0"/>
            <Str sr="arg1" ve="3">am</Str>
        </Action>
        <Action sr="act2" ve="7">
            <code>664</code>
            <Str sr="arg0" ve="3">%accounts_length</Str>
            <Str sr="arg1" ve="3">accounts.length</Str>
            <Str sr="arg2" ve="3">assign
{int} ()</Str>
            <Str sr="arg3" ve="3"/>
            <Str sr="arg4" ve="3"/>
            <Str sr="arg5" ve="3"/>
            <Str sr="arg6" ve="3"/>
            <Str sr="arg7" ve="3"/>
            <Str sr="arg8" ve="3"/>
            <Str sr="arg9" ve="3"/>
        </Action>
        <Action sr="act3" ve="7">
            <code>548</code>
            <Str sr="arg0" ve="3">Number of accounts: %accounts_length</Str>
            <Int sr="arg1" val="1"/>
        </Action>
        <Action sr="act4" ve="7">
            <code>890</code>
            <Str sr="arg0" ve="3">%accounts_length</Str>
            <Int sr="arg1" val="1"/>
            <Int sr="arg2" val="0"/>
        </Action>
        <Action sr="act5" ve="7">
            <code>39</code>
            <Str sr="arg0" ve="3">%item</Str>
            <Str sr="arg1" ve="3">0:%accounts_length</Str>
        </Action>
        <Action sr="act6" ve="7">
            <code>664</code>
            <se>false</se>
            <Str sr="arg0" ve="3">%a_name</Str>
            <Str sr="arg1" ve="3">accounts[%item].name</Str>
            <Str sr="arg2" ve="3">toString
{String} ()</Str>
            <Str sr="arg3" ve="3"/>
            <Str sr="arg4" ve="3"/>
            <Str sr="arg5" ve="3"/>
            <Str sr="arg6" ve="3"/>
            <Str sr="arg7" ve="3"/>
            <Str sr="arg8" ve="3"/>
            <Str sr="arg9" ve="3"/>
        </Action>
        <Action sr="act7" ve="7">
            <code>664</code>
            <se>false</se>
            <Str sr="arg0" ve="3">%a_type</Str>
            <Str sr="arg1" ve="3">accounts[%item].type</Str>
            <Str sr="arg2" ve="3">toString
{String} ()</Str>
            <Str sr="arg3" ve="3"/>
            <Str sr="arg4" ve="3"/>
            <Str sr="arg5" ve="3"/>
            <Str sr="arg6" ve="3"/>
            <Str sr="arg7" ve="3"/>
            <Str sr="arg8" ve="3"/>
            <Str sr="arg9" ve="3"/>
        </Action>
        <Action sr="act8" ve="7">
            <code>410</code>
            <se>false</se>
            <Str sr="arg0" ve="3">/storage/17EE-2356/titwatest/AccountManager_Accounts1.txt</Str>
            <Str sr="arg1" ve="3">%a_name;%a_type</Str>
            <Int sr="arg2" val="1"/>
            <Int sr="arg3" val="1"/>
            <ConditionList sr="if">
                <Condition sr="c0" ve="3">
                    <lhs>%a_type</lhs>
                    <op>2</op>
                    <rhs>*bitfire.at.davdroid*/*at.bitfire.davdroid*</rhs>
                </Condition>
            </ConditionList>
        </Action>
        <Action sr="act9" ve="7">
            <code>548</code>
            <Str sr="arg0" ve="3">%a_name</Str>
            <Int sr="arg1" val="0"/>
            <ConditionList sr="if">
                <Condition sr="c0" ve="3">
                    <lhs>%a_type</lhs>
                    <op>2</op>
                    <rhs>*at.bitfire.davdroid*</rhs>
                </Condition>
</ConditionList>
        </Action>
        <Img sr="icn" ve="2">
            <nme>hl_collections_go_to_today</nme>
        </Img>
    </Task>
</TaskerData>

As soon as I have been able to modify/expand it to delete the accounts, I shall post the solution here as answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.