1 /* 2 * Copyright (C) 2018 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.server.cts.device.statsd; 17 18 import android.accounts.AbstractAccountAuthenticator; 19 import android.accounts.Account; 20 import android.accounts.AccountAuthenticatorResponse; 21 import android.accounts.AccountManager; 22 import android.accounts.NetworkErrorException; 23 import android.app.Service; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.os.Bundle; 27 import android.os.IBinder; 28 import android.util.Log; 29 30 import java.util.Arrays; 31 32 /** 33 * Authenticator for the sync test. 34 */ 35 public class StatsdAuthenticator extends Service { 36 private static final String TAG = "AtomTestsAuthenticator"; 37 38 private static final String ACCOUNT_NAME = "StatsdCts"; 39 private static final String ACCOUNT_TYPE = "com.android.cts.statsd"; 40 private static Authenticator sInstance; 41 42 @Override onBind(Intent intent)43 public IBinder onBind(Intent intent) { 44 if (sInstance == null) { 45 sInstance = new Authenticator(getApplicationContext()); 46 47 } 48 return sInstance.getIBinder(); 49 } 50 getTestAccount()51 public static Account getTestAccount() { 52 return new Account(ACCOUNT_NAME, ACCOUNT_TYPE); 53 } 54 55 /** 56 * Adds the test account, if it doesn't exist yet. 57 */ ensureTestAccount(Context context)58 public static void ensureTestAccount(Context context) { 59 final Account account = getTestAccount(); 60 61 Bundle result = new Bundle(); 62 result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); 63 result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); 64 65 final AccountManager am = context.getSystemService(AccountManager.class); 66 67 if (!Arrays.asList(am.getAccountsByType(account.type)).contains(account) ){ 68 am.addAccountExplicitly(account, "password", new Bundle()); 69 } 70 } 71 72 /** 73 * Remove the test account. 74 */ removeAllAccounts(Context context)75 public static void removeAllAccounts(Context context) { 76 final AccountManager am = context.getSystemService(AccountManager.class); 77 78 for (Account account : am.getAccountsByType(ACCOUNT_TYPE)) { 79 Log.i(TAG, "Removing " + account + "..."); 80 am.removeAccountExplicitly(account); 81 Log.i(TAG, "Removed"); 82 } 83 } 84 85 public static class Authenticator extends AbstractAccountAuthenticator { 86 87 private final Context mContxet; 88 Authenticator(Context context)89 public Authenticator(Context context) { 90 super(context); 91 mContxet = context; 92 } 93 94 @Override addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options)95 public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, 96 String authTokenType, String[] requiredFeatures, Bundle options) 97 throws NetworkErrorException { 98 return new Bundle(); 99 } 100 101 @Override editProperties(AccountAuthenticatorResponse response, String accountType)102 public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { 103 return new Bundle(); 104 } 105 106 @Override updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)107 public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, 108 String authTokenType, Bundle options) throws NetworkErrorException { 109 return new Bundle(); 110 } 111 112 @Override confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options)113 public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, 114 Bundle options) throws NetworkErrorException { 115 return new Bundle(); 116 } 117 118 @Override getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)119 public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, 120 String authTokenType, Bundle options) throws NetworkErrorException { 121 return new Bundle(); 122 } 123 124 @Override getAuthTokenLabel(String authTokenType)125 public String getAuthTokenLabel(String authTokenType) { 126 return "token_label"; 127 } 128 129 @Override hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features)130 public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, 131 String[] features) throws NetworkErrorException { 132 return new Bundle(); 133 } 134 } 135 } 136