Home | History | Annotate | Download | only in accounts
      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 
     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 
     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 
     67     @Override
     68     public void refreshContent() {
     69         loadCacheItems();
     70     }
     71 
     72     private void addBrowseHeader(int id, String title) {
     73         mHeaderItems.add(new HeaderItem(id, title, null));
     74     }
     75 
     76     private void updateMenuItems(String accountName) {
     77         Resources resources = mContext.getResources();
     78 
     79         // sync
     80         Intent intent = new Intent(mContext, AccountSyncSettings.class)
     81                 .putExtra(AccountSettingsActivity.EXTRA_ACCOUNT, accountName);
     82         mRows.get(ID_SYNC).add(new MenuItem.Builder().id(mNextItemId++)
     83                 .title(resources.getString(R.string.account_sync))
     84                 .imageResourceId(mContext, R.drawable.ic_settings_sync)
     85                 .intent(intent).build());
     86 
     87         // remove
     88         Intent removeIntent = new Intent(mContext, RemoveAccountDialog.class).putExtra(
     89                 AccountSettingsActivity.EXTRA_ACCOUNT, accountName);
     90         mRows.get(ID_REMOVE_ACCOUNT).add(new MenuItem.Builder().id(mNextItemId++)
     91                 .title(resources.getString(R.string.account_remove))
     92                 .imageResourceId(mContext, R.drawable.ic_settings_remove)
     93                 .intent(removeIntent)
     94                 .build());
     95     }
     96 }
     97