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