Home | History | Annotate | Download | only in whitelistedaccount
      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 
     17 package com.example.android.app.admin.whitelistedaccount;
     18 
     19 import android.accounts.AbstractAccountAuthenticator;
     20 import android.accounts.Account;
     21 import android.accounts.AccountAuthenticatorResponse;
     22 import android.accounts.AccountManager;
     23 import android.accounts.NetworkErrorException;
     24 import android.app.Service;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.os.Bundle;
     28 import android.os.IBinder;
     29 
     30 public class MyAuthenticator extends Service {
     31     private static final String TAG = "TestAuthenticator";
     32 
     33     private static final String ACCOUNT_TYPE = "com.example.android.app.admin.whitelistedaccount";
     34 
     35     private static final String ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED =
     36             "android.account.DEVICE_OR_PROFILE_OWNER_ALLOWED";
     37 
     38     private static Authenticator sInstance;
     39 
     40     @Override
     41     public IBinder onBind(Intent intent) {
     42         if (sInstance == null) {
     43             sInstance = new Authenticator(getApplicationContext());
     44 
     45         }
     46         return sInstance.getIBinder();
     47     }
     48 
     49     public static boolean setUpAccount(Context context) {
     50         final AccountManager am = AccountManager.get(context);
     51         if (am.getAccountsByType(ACCOUNT_TYPE).length > 0) {
     52             return false; // Already set up.
     53         }
     54 
     55         // Add a new account.
     56         final Account account = new Account(
     57                 context.getResources().getString(R.string.account_name), ACCOUNT_TYPE);
     58         am.addAccountExplicitly(account, null, null);
     59         return true;
     60     }
     61 
     62     public static class Authenticator extends AbstractAccountAuthenticator {
     63 
     64         private final Context mContxet;
     65 
     66         public Authenticator(Context context) {
     67             super(context);
     68             mContxet = context;
     69         }
     70 
     71         @Override
     72         public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
     73                 String authTokenType, String[] requiredFeatures, Bundle options)
     74                 throws NetworkErrorException {
     75             return new Bundle();
     76         }
     77 
     78         @Override
     79         public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
     80             return new Bundle();
     81         }
     82 
     83         @Override
     84         public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
     85                 String authTokenType, Bundle options) throws NetworkErrorException {
     86             return new Bundle();
     87         }
     88 
     89         @Override
     90         public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
     91                 Bundle options) throws NetworkErrorException {
     92             return new Bundle();
     93         }
     94 
     95         @Override
     96         public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
     97                 String authTokenType, Bundle options) throws NetworkErrorException {
     98             return new Bundle();
     99         }
    100 
    101         @Override
    102         public String getAuthTokenLabel(String authTokenType) {
    103             return "token_label";
    104         }
    105 
    106         @Override
    107         public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
    108                 String[] features) throws NetworkErrorException {
    109 
    110             boolean hasAll = false;
    111 
    112             if ((features != null) && (features.length == 1)
    113                     && ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED.equals(features[0])) {
    114                 hasAll = true;
    115             }
    116 
    117             Bundle result = new Bundle();
    118             result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, hasAll);
    119             return result;
    120         }
    121     }
    122 }
    123