1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)package org.chromium.chromoting;
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.accounts.Account;
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.content.Context;
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.view.LayoutInflater;
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.view.View;
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.view.ViewGroup;
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.widget.ArrayAdapter;
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import android.widget.TextView;
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/** SpinnerAdapter class used for the ActionBar accounts spinner. */
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)public class AccountsAdapter extends ArrayAdapter<Account> {
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    private LayoutInflater mInflater;
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    public AccountsAdapter(Context context, Account[] accounts) {
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        // ArrayAdapter only uses the |resource| parameter to return a View from getView() and
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        // getDropDownView(). But these methods are overridden here to return custom Views, so it's
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        // OK to provide 0 as the resource for the base class.
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        super(context, 0, accounts);
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    @Override
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    public View getView(int position, View convertView, ViewGroup parent) {
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        View view = mInflater.inflate(R.layout.account_selected, parent, false);
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        Account account = getItem(position);
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        TextView target = (TextView)view.findViewById(R.id.account_name);
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        target.setText(account.name);
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        return view;
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    @Override
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    public View getDropDownView(int position, View convertView, ViewGroup parent) {
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        TextView view = (TextView)mInflater.inflate(R.layout.account_dropdown, parent, false);
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        Account account = getItem(position);
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        view.setText(account.name);
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        return view;
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
44