1 /*
2  * Copyright (C) 2014 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.settings.accounts;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.res.Resources;
22 import android.support.v17.leanback.widget.ArrayObjectAdapter;
23 import android.support.v17.leanback.widget.HeaderItem;
24 
25 import com.android.tv.settings.BrowseInfoBase;
26 import com.android.tv.settings.MenuItem;
27 import com.android.tv.settings.R;
28 
29 /**
30  * Browse info for accounts.
31  */
32 public class AccountsBrowseInfo extends BrowseInfoBase {
33 
34     private static final int ID_SYNC = 0;
35     private static final int ID_REMOVE_ACCOUNT = 1;
36 
37     private static final String AUTHORITY_REMOVE = "remove_account";
38 
39     private final Context mContext;
40     private final String mAccountName;
41     private final AuthenticatorHelper mAuthenticatorHelper;
42     private int mNextItemId;
43 
AccountsBrowseInfo(Context context, String accountName)44     AccountsBrowseInfo(Context context, String accountName) {
45         mContext = context;
46         mAccountName = accountName;
47         mAuthenticatorHelper = new AuthenticatorHelper();
48         mAuthenticatorHelper.updateAuthDescriptions(context);
49         mAuthenticatorHelper.onAccountsUpdated(context, null);
50         Resources resources = context.getResources();
51         mRows.put(ID_SYNC, new ArrayObjectAdapter());
52         mRows.put(ID_REMOVE_ACCOUNT, new ArrayObjectAdapter());
53 
54         loadCacheItems();
55     }
56 
loadCacheItems()57     private void loadCacheItems() {
58         Resources resources = mContext.getResources();
59         mHeaderItems.clear();
60         addBrowseHeader(ID_SYNC, resources.getString(R.string.account_header_sync));
61         addBrowseHeader(ID_REMOVE_ACCOUNT,
62                 resources.getString(R.string.account_header_remove_account));
63 
64         updateMenuItems(mAccountName);
65     }
66 
addBrowseHeader(int id, String title)67     private void addBrowseHeader(int id, String title) {
68         mHeaderItems.add(new HeaderItem(id, title));
69     }
70 
updateMenuItems(String accountName)71     private void updateMenuItems(String accountName) {
72         Resources resources = mContext.getResources();
73 
74         // sync
75         Intent intent = new Intent(mContext, AccountSyncSettings.class)
76                 .putExtra(AccountSettingsActivity.EXTRA_ACCOUNT, accountName);
77         mRows.get(ID_SYNC).add(new MenuItem.Builder().id(mNextItemId++)
78                 .title(resources.getString(R.string.account_sync))
79                 .imageResourceId(mContext, R.drawable.ic_settings_sync)
80                 .intent(intent).build());
81 
82         // remove
83         Intent removeIntent = new Intent(mContext, RemoveAccountDialog.class).putExtra(
84                 AccountSettingsActivity.EXTRA_ACCOUNT, accountName);
85         mRows.get(ID_REMOVE_ACCOUNT).add(new MenuItem.Builder().id(mNextItemId++)
86                 .title(resources.getString(R.string.account_remove))
87                 .imageResourceId(mContext, R.drawable.ic_settings_remove)
88                 .intent(removeIntent)
89                 .build());
90     }
91 }
92