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.android.settings.intelligence.suggestions.eligibility;
18 
19 import android.accounts.Account;
20 import android.accounts.AccountManager;
21 import android.content.Context;
22 import android.content.pm.ResolveInfo;
23 import androidx.annotation.VisibleForTesting;
24 import android.util.Log;
25 
26 public class AccountEligibilityChecker {
27     /**
28      * If defined, only display this optional step if an account of that type exists.
29      */
30     @VisibleForTesting
31     static final String META_DATA_REQUIRE_ACCOUNT = "com.android.settings.require_account";
32     private static final String TAG = "AccountEligibility";
33 
isEligible(Context context, String id, ResolveInfo info)34     public static boolean isEligible(Context context, String id, ResolveInfo info) {
35         final String requiredAccountType = info.activityInfo.metaData.
36                 getString(META_DATA_REQUIRE_ACCOUNT);
37         if (requiredAccountType == null) {
38             return true;
39         }
40         AccountManager accountManager = AccountManager.get(context);
41         Account[] accounts = accountManager.getAccountsByType(requiredAccountType);
42         boolean satisfiesRequiredAccount = accounts.length > 0;
43         if (!satisfiesRequiredAccount) {
44             Log.i(TAG, id + " requires unavailable account type " + requiredAccountType);
45         }
46         return satisfiesRequiredAccount;
47     }
48 }
49