Home | History | Annotate | Download | only in name
      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.name;
     18 
     19 import android.app.Fragment;
     20 import android.content.Context;
     21 import android.os.Build;
     22 import android.os.Bundle;
     23 import android.text.TextUtils;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.AdapterView;
     28 import android.widget.FrameLayout;
     29 import android.widget.ImageView;
     30 import android.widget.TextView;
     31 
     32 import java.util.ArrayList;
     33 
     34 import com.android.tv.settings.R;
     35 import com.android.tv.settings.widget.ScrollAdapterView;
     36 import com.android.tv.settings.widget.ScrollArrayAdapter;
     37 
     38 public class SetDeviceNameFragment extends Fragment implements AdapterView.OnItemClickListener {
     39 
     40     private static final String ARG_NAME_LIST = "suggested_names";
     41     private static final String ARG_SHOW_CUSTOM_OPTION = "allow_custom";
     42     private static final int LAYOUT_MAIN = R.layout.setup_content_area;
     43     private static final int LAYOUT_DESCRIPTION = R.layout.setup_text_and_description;
     44     private static final int LAYOUT_ACTION = R.layout.setup_scroll_adapter_view;
     45     private static final int LAYOUT_LIST_ITEM = R.layout.setup_list_item_text_only;
     46     private static final int LAYOUT_ITEM_TEXT = R.id.list_item_text;
     47 
     48     public static SetDeviceNameFragment createInstance(ArrayList<String> names,
     49                                                     boolean allowCustom) {
     50         SetDeviceNameFragment frag = new SetDeviceNameFragment();
     51         Bundle args = new Bundle();
     52         args.putStringArrayList(SetDeviceNameFragment.ARG_NAME_LIST, names);
     53         args.putBoolean(SetDeviceNameFragment.ARG_SHOW_CUSTOM_OPTION, allowCustom);
     54         frag.setArguments(args);
     55         return frag;
     56     }
     57 
     58     private SetDeviceNameListener mEventListener;
     59     private ArrayList<String> mOptions;
     60     private boolean mShowCustom;
     61     private View mContent;
     62     private FrameLayout mDescription;
     63     private FrameLayout mAction;
     64     private ScrollAdapterView mList;
     65     private ScrollArrayAdapter<String> mListAdapter;
     66     private String mCustomRoomString = "";
     67 
     68     @Override
     69     public void onCreate(Bundle savedInstanceState) {
     70         super.onCreate(savedInstanceState);
     71         Bundle args = getArguments();
     72         if (args.containsKey(ARG_NAME_LIST)) {
     73             mOptions = args.getStringArrayList(ARG_NAME_LIST);
     74         } else {
     75             mOptions = new ArrayList<String>();
     76         }
     77 
     78         mShowCustom = args.getBoolean(ARG_SHOW_CUSTOM_OPTION, false);
     79 
     80         if (mShowCustom) {
     81             mCustomRoomString = getResources().getString(R.string.custom_room);
     82             mOptions.add(mCustomRoomString);
     83         }
     84     }
     85 
     86     @Override
     87     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     88                              Bundle savedInstanceState) {
     89         mContent = inflater.inflate(LAYOUT_MAIN, null);
     90         mAction = (FrameLayout) mContent.findViewById(R.id.action);
     91         mDescription = (FrameLayout) mContent.findViewById(R.id.description);
     92         mDescription.addView(inflater.inflate(LAYOUT_DESCRIPTION, null));
     93         String title = getResources().getString(R.string.select_title);
     94         ((TextView) mDescription.findViewById(R.id.title_text))
     95                 .setText(TextUtils.expandTemplate(title, Build.MODEL));
     96         String description = getResources().getString(R.string.select_description);
     97         ((TextView) mDescription.findViewById(R.id.description_text))
     98                 .setText(TextUtils.expandTemplate(description, Build.MODEL));
     99         mList = (ScrollAdapterView) inflater.inflate(LAYOUT_ACTION, null);
    100         mAction.addView(mList);
    101         setupList();
    102         return mContent;
    103     }
    104 
    105     public void setListener(SetDeviceNameListener listener) {
    106         mEventListener = listener;
    107     }
    108 
    109     @Override
    110     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    111         if (isCustomListItem(position)) {
    112             mEventListener.onCustomNameRequested();
    113         } else {
    114             mEventListener.onDeviceNameSelected(mOptions.get(position));
    115         }
    116     }
    117 
    118     private void setupList() {
    119         mListAdapter = new ScrollArrayAdapter<String>(getActivity(), LAYOUT_LIST_ITEM,
    120                 LAYOUT_ITEM_TEXT, mOptions) {
    121             private static final int VIEW_TYPE_TEXT = 0;
    122             private static final int VIEW_TYPE_TEXT_AND_ICON = 1;
    123             @Override
    124             public View getView(int position, View convertView, ViewGroup parent) {
    125                 // for a "standard" item, return standard view
    126                 if (!isCustomListItem(position)) {
    127                     return super.getView(position, convertView, parent);
    128                 }
    129 
    130                 // for the "other option" draw a custom view that includes an icon
    131                 if (convertView == null) {
    132                     LayoutInflater helium = (LayoutInflater) getActivity().getSystemService(
    133                             Context.LAYOUT_INFLATER_SERVICE);
    134                     convertView = helium.inflate(R.layout.setup_list_item, null);
    135 
    136                     // our image view is always going to be the same, so set that here
    137                     ImageView plusIcon = new ImageView(getActivity());
    138                     plusIcon.setImageResource(R.drawable.ic_menu_add);
    139 
    140                     ((FrameLayout) convertView.findViewById(R.id.list_item_icon)).addView(plusIcon);
    141                 }
    142 
    143                 TextView itemLabel = (TextView) convertView.findViewById(R.id.list_item_text);
    144                 itemLabel.setText(mOptions.get(position));
    145                 return convertView;
    146             }
    147 
    148             @Override
    149             public int getViewTypeCount() {
    150                 return 2;
    151             }
    152 
    153             @Override
    154             public int getItemViewType(int position) {
    155                 return mOptions.get(position).equals(mCustomRoomString) ?
    156                         VIEW_TYPE_TEXT_AND_ICON : VIEW_TYPE_TEXT;
    157             }
    158         };
    159         mList.setAdapter(mListAdapter);
    160         mList.setOnItemClickListener(this);
    161     }
    162 
    163     private boolean isCustomListItem(int position) {
    164         return mOptions.get(position).equals(mCustomRoomString);
    165     }
    166 }
    167