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.tv.util.account;
18 
19 import android.accounts.Account;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.preference.PreferenceManager;
23 import android.support.annotation.Nullable;
24 import com.android.tv.common.dagger.annotations.ApplicationContext;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 
28 /** Helper methods for getting and selecting a user account. */
29 @Singleton
30 public class AccountHelperImpl implements com.android.tv.util.account.AccountHelper {
31     private static final String SELECTED_ACCOUNT = "android.tv.livechannels.selected_account";
32 
33     protected final Context mContext;
34     private final SharedPreferences mDefaultPreferences;
35 
36     @Nullable private Account mSelectedAccount;
37 
38     @Inject
AccountHelperImpl(@pplicationContext Context context)39     public AccountHelperImpl(@ApplicationContext Context context) {
40         mContext = context.getApplicationContext();
41         mDefaultPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
42     }
43 
44     /** Returns the currently selected account or {@code null} if none is selected. */
45     @Override
46     @Nullable
getSelectedAccount()47     public final Account getSelectedAccount() {
48         String accountId = mDefaultPreferences.getString(SELECTED_ACCOUNT, null);
49         if (accountId == null) {
50             return null;
51         }
52         if (mSelectedAccount == null || !mSelectedAccount.name.equals((accountId))) {
53             mSelectedAccount = null;
54             for (Account account : getEligibleAccounts()) {
55                 if (account.name.equals(accountId)) {
56                     mSelectedAccount = account;
57                     break;
58                 }
59             }
60         }
61         return mSelectedAccount;
62     }
63 
64     /**
65      * Returns all eligible accounts.
66      *
67      * <p>Override this method to return the accounts needed.
68      */
getEligibleAccounts()69     protected Account[] getEligibleAccounts() {
70         return new Account[0];
71     }
72 
73     /**
74      * Selects the first account available.
75      *
76      * @return selected account or {@code null} if none is selected.
77      */
78     @Override
79     @Nullable
selectFirstAccount()80     public final Account selectFirstAccount() {
81         Account account = getFirstEligibleAccount();
82         if (account != null) {
83             selectAccount(account);
84         }
85         return account;
86     }
87 
88     /**
89      * Gets the first account eligible.
90      *
91      * @return first account or {@code null} if none is eligible.
92      */
93     @Override
94     @Nullable
getFirstEligibleAccount()95     public final Account getFirstEligibleAccount() {
96         Account[] accounts = getEligibleAccounts();
97         return accounts.length == 0 ? null : accounts[0];
98     }
99 
100     /** Sets the given account as the selected account. */
selectAccount(Account account)101     private void selectAccount(Account account) {
102         SharedPreferences defaultPreferences =
103                 PreferenceManager.getDefaultSharedPreferences(mContext);
104         defaultPreferences.edit().putString(SELECTED_ACCOUNT, account.name).commit();
105     }
106 
107     @Override
init()108     public void init() {
109         // do nothing.
110     }
111 }
112