Skip to content

Instantly share code, notes, and snippets.

@jkreiser
Last active January 25, 2018 16:34
Show Gist options
  • Save jkreiser/1c8a7c383cb0b73f7b570a1875686e19 to your computer and use it in GitHub Desktop.
Save jkreiser/1c8a7c383cb0b73f7b570a1875686e19 to your computer and use it in GitHub Desktop.
Utility method for calling the correct AccountManager.removeAccount() - Java
/*
Copyright 2018 Michigan Software Labs, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
* In API 22 (LOLLIPOP_MR1),
* AccountManager.removeAccount(Account, Activity, AccountManagerCallback<Bundle>, Handler) was added, and
* AccountManager.removeAccount(Account, AccountManagerCallback<Boolean>, Handler) was deprecated.
*
* To resolve this when running on a pre-22 device, call the old .removeAccount(), get the Boolean result,
* put it in a Bundle using key AccountManager.KEY_BOOLEAN_RESULT,
* then call AccountManagerCallback<Bundle> callback.run() with a new AccountManagerFuture<Bundle>
* that returns this Bundle.
*/
public static void removeAccount(
@NonNull Account account,
@NonNull Activity activity,
@Nullable final AccountManagerCallback<Bundle> callback,
@Nullable Handler handler
) {
AccountManager accountManager = AccountManager.get(activity);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
accountManager.removeAccount(account, activity, callback, handler);
} else {
//noinspection deprecation - we don't need our IDE to warn us that this is deprecated
accountManager.removeAccount(
account,
new AccountManagerCallback<Boolean>() {
@Override
public void run(final AccountManagerFuture<Boolean> future) {
if (callback == null) {
return;
}
callback.run(new AccountManagerFuture<Bundle>() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return future.cancel(mayInterruptIfRunning);
}
@Override
public boolean isCancelled() {
return future.isCancelled();
}
@Override
public boolean isDone() {
return future.isDone();
}
@Override
public Bundle getResult()
throws OperationCanceledException, IOException, AuthenticatorException
{
Bundle result = new Bundle();
result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, future.getResult());
return result;
}
@Override
public Bundle getResult(long timeout, TimeUnit unit)
throws OperationCanceledException, IOException, AuthenticatorException
{
Bundle result = new Bundle();
result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, future.getResult(timeout, unit));
return result;
}
});
}
},
handler
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment