Home | History | Annotate | Download | only in personalization
      1 /*
      2  * Copyright (C) 2013 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.android.inputmethod.latin.personalization;
     18 
     19 import android.accounts.Account;
     20 import android.accounts.AccountManager;
     21 import android.content.Context;
     22 import android.util.Patterns;
     23 
     24 import java.util.ArrayList;
     25 import java.util.List;
     26 import java.util.Locale;
     27 
     28 public class AccountUtils {
     29     private AccountUtils() {
     30         // This utility class is not publicly instantiable.
     31     }
     32 
     33     private static Account[] getAccounts(final Context context) {
     34         return AccountManager.get(context).getAccounts();
     35     }
     36 
     37     public static List<String> getDeviceAccountsEmailAddresses(final Context context) {
     38         final ArrayList<String> retval = new ArrayList<>();
     39         for (final Account account : getAccounts(context)) {
     40             final String name = account.name;
     41             if (Patterns.EMAIL_ADDRESS.matcher(name).matches()) {
     42                 retval.add(name);
     43                 retval.add(name.split("@")[0]);
     44             }
     45         }
     46         return retval;
     47     }
     48 
     49     /**
     50      * Get all device accounts having specified domain name.
     51      * @param context application context
     52      * @param domain domain name used for filtering
     53      * @return List of account names that contain the specified domain name
     54      */
     55     public static List<String> getDeviceAccountsWithDomain(
     56             final Context context, final String domain) {
     57         final ArrayList<String> retval = new ArrayList<>();
     58         final String atDomain = "@" + domain.toLowerCase(Locale.ROOT);
     59         for (final Account account : getAccounts(context)) {
     60             if (account.name.toLowerCase(Locale.ROOT).endsWith(atDomain)) {
     61                 retval.add(account.name);
     62             }
     63         }
     64         return retval;
     65     }
     66 }
     67