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