AccountUtils.java revision a91561aa58db1c43092c1caecc051a11fa5391c7
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
17package com.android.inputmethod.latin.personalization;
18
19import android.accounts.Account;
20import android.accounts.AccountManager;
21import android.content.Context;
22import android.util.Patterns;
23
24import java.util.ArrayList;
25import java.util.List;
26import java.util.Locale;
27
28public 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