Does SyncAdapter get notified when AccountManager removes account?

后端 未结 3 1581
心在旅途
心在旅途 2020-12-30 14:46

So, my question restated is when you go to Settings -> Accounts & Sync and select the an account that was created that your SyncAdapter is syncing with a c

3条回答
  •  情话喂你
    2020-12-30 15:06

    I discovered the solution is to make the app's ContentProvider implement OnAccountsUpdateListener. Attach the ContentProvider as a listener in its onCreate method with account_manager.addOnAccountsUpdatedListener(this, null, false) and then implement the interface method like

    @Override
    public void onAccountsUpdated(final Account[] accounts) {
        Ln.i("Accounts updated.");
        final Iterable account_list = new Iterable() {
            @Override
            public Iterator iterator() {
                return new Iterator() {
                    private final Iterator account_list = Arrays.asList(accounts).iterator();
    
                    @Override
                    public boolean hasNext() {
                        return account_list.hasNext();
                    }
    
                    /** Extracts the next account name and wraps it in single quotes. */
                    @Override
                    public String next() {
                        return "'" + account_list.next().name + "'";
                    }
    
                    @Override
                    public void remove() { throw new UnsupportedOperationException("Not implemented"); }
                };
            }
        };
        final String account_set = TextUtils.join(", ", account_list);
        Ln.i("Current accounts: %s", account_set);
    
        // Removes content that is associated with accounts that are not currently connected
        final SelectionBuilder builder = new SelectionBuilder();
        builder.table(Tables.CALENDARS)
               .where(Calendars.CALENDAR_USER + " NOT IN (?)", account_set);
    
        new SafeAsyncTask() {
            @Override
            public Void call() throws Exception {
                _model.openWritableDatabase();
                _model.delete(builder);
                return null;
            }
        }.execute();
    
    
        getContext().getContentResolver().notifyChange(Calendars.NO_SYNC_URI, null, false);
    }
    

    I construct a String of the currently connected accounts, then build a SQL query with that String. I perform a delete on the database in a background thread on that query to remove the data associated with accounts not currently connected. And I notify that content changed, but does not need to synchronized with the server.

提交回复
热议问题