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