aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAng Li <angli@google.com>2019-02-12 19:03:32 -0800
committerGitHub <noreply@github.com>2019-02-12 19:03:32 -0800
commit244d747c29066e1caf9c99693e6ad356931b231d (patch)
treebe7c94ab94972ae4bf5f7ce393c7ca60a5cf5381
parenta727fbc5ee92c632fca4475d65dad2bdbf2d4833 (diff)
downloadmobly-bundled-snippets-244d747c29066e1caf9c99693e6ad356931b231d.tar.gz
Add api to remove account. (#108)
-rw-r--r--src/main/AndroidManifest.xml1
-rw-r--r--src/main/java/com/google/android/mobly/snippet/bundled/AccountSnippet.java42
2 files changed, 39 insertions, 4 deletions
diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index 3d4537d..c8c987c 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
+ <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
diff --git a/src/main/java/com/google/android/mobly/snippet/bundled/AccountSnippet.java b/src/main/java/com/google/android/mobly/snippet/bundled/AccountSnippet.java
index c7f576b..aac543a 100644
--- a/src/main/java/com/google/android/mobly/snippet/bundled/AccountSnippet.java
+++ b/src/main/java/com/google/android/mobly/snippet/bundled/AccountSnippet.java
@@ -23,7 +23,9 @@ import android.accounts.AccountsException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SyncAdapterType;
+import android.os.Build;
import android.os.Bundle;
+import androidx.annotation.RequiresApi;
import androidx.test.platform.app.InstrumentationRegistry;
import com.google.android.mobly.snippet.Snippet;
import com.google.android.mobly.snippet.rpc.Rpc;
@@ -78,10 +80,6 @@ public class AccountSnippet implements Snippet {
/**
* Adds a Google account to the device.
*
- * <p>TODO(adorokhine): Support adding accounts of other types with an optional 'type' kwarg.
- *
- * <p>TODO(adorokhine): Allow users to choose whether to enable/disable sync with a kwarg.
- *
* @param username Username of the account to add (including @gmail.com).
* @param password Password of the account to add.
*/
@@ -153,6 +151,42 @@ public class AccountSnippet implements Snippet {
}
/**
+ * Removes an account from the device.
+ *
+ * <p>The account has to be Google account.
+ *
+ * @param username the username of the account to remove.
+ * @throws AccountSnippetException if removing the account failed.
+ */
+ @RequiresApi(Build.VERSION_CODES.LOLLIPOP_MR1)
+ @Rpc(description = "Remove a Google account.")
+ public void removeAccount(String username) throws AccountSnippetException {
+ if (!mAccountManager.removeAccountExplicitly(getAccountByName(username))) {
+ throw new AccountSnippetException("Failed to remove account '" + username + "'.");
+ }
+ }
+
+ /**
+ * Get an existing account by its username.
+ *
+ * <p>Google account only.
+ *
+ * @param username the username of the account to remove.
+ * @return tHe account with the username.
+ * @throws AccountSnippetException if no account has the given username.
+ */
+ private Account getAccountByName(String username) throws AccountSnippetException {
+ Account[] accounts = mAccountManager.getAccountsByType(GOOGLE_ACCOUNT_TYPE);
+ for (Account account : accounts) {
+ if (account.name.equals(username)) {
+ return account;
+ }
+ }
+ throw new AccountSnippetException(
+ "Account '" + username + "' does not exist on the device.");
+ }
+
+ /**
* Checks to see if the SyncAdapter is whitelisted.
*
* <p>AccountSnippet disables syncing by default when adding an account, except for whitelisted