1 /* 2 * Copyright 2019, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.managedprovisioning.common; 17 18 import android.accounts.Account; 19 import android.annotation.Nullable; 20 import android.content.Context; 21 import android.os.AsyncTask; 22 23 import java.lang.ref.WeakReference; 24 25 import static com.android.internal.util.Preconditions.checkNotNull; 26 27 /** 28 * Asynchronous task to remove the specified account. 29 */ 30 class RemoveAccountAsyncTask extends AsyncTask<Void, Void, Void> { 31 32 private final WeakReference<Context> mContextRef; 33 private final Account mAccountToRemove; 34 private final Utils mUtils; 35 private RemoveAccountListener mCallback; 36 RemoveAccountAsyncTask( Context context, Account accountToRemove, Utils utils, @Nullable RemoveAccountListener callback)37 RemoveAccountAsyncTask( 38 Context context, Account accountToRemove, Utils utils, 39 @Nullable RemoveAccountListener callback) { 40 this.mContextRef = new WeakReference<>(checkNotNull(context)); 41 this.mAccountToRemove = checkNotNull(accountToRemove); 42 this.mUtils = checkNotNull(utils); 43 this.mCallback = callback; 44 } 45 46 @Override doInBackground(Void... params)47 protected Void doInBackground(Void... params) { 48 final Context context = mContextRef.get(); 49 if (context != null) { 50 mUtils.removeAccount(context, mAccountToRemove); 51 } 52 return null; 53 } 54 55 @Override onPostExecute(Void result)56 protected void onPostExecute(Void result) { 57 if (mCallback != null) { 58 mCallback.onAccountRemoved(); 59 } 60 } 61 } 62