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
17package com.android.tv.settings.name;
18
19import android.os.Build;
20import com.android.tv.settings.R;
21import com.android.tv.settings.widget.ScrollAdapterView;
22import com.android.tv.settings.widget.ScrollArrayAdapter;
23
24import android.app.Fragment;
25import android.app.Activity;
26import android.os.Bundle;
27import android.text.TextUtils;
28import android.util.Log;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.AdapterView;
33import android.widget.FrameLayout;
34import android.widget.TextView;
35
36import java.util.ArrayList;
37
38/**
39 * Loads the device's current name and displays it. Gives the user the action
40 * options that correspond to the ArrayList<String> passed in the
41 * {@link #ARG_OPTION_LIST} extra.
42 */
43public class DeviceNameSummaryFragment extends Fragment implements AdapterView.OnItemClickListener {
44
45    private static final String TAG = "DeviceNameSummaryFragment";
46    private static final String ARG_DESCRIPTION = "text_desc";
47    private static final String ARG_TITLE = "text_title";
48    private static final String ARG_OPTION_LIST = "options_list";
49
50    /**
51     * Creates a DeviceNameSummaryFragment with the correct arguments.
52     * @param title The title to be displayed to the user.
53     * @param description The description to be displayed to the user.
54     * @param options The options to present to the user.
55     */
56    public static DeviceNameSummaryFragment createInstance(String title, String description,
57                                                           ArrayList<String> options) {
58        DeviceNameSummaryFragment fragment = new DeviceNameSummaryFragment();
59        Bundle args = new Bundle();
60        args.putString(ARG_TITLE, title);
61        args.putString(ARG_DESCRIPTION, description);
62        args.putStringArrayList(ARG_OPTION_LIST, options);
63        fragment.setArguments(args);
64        return fragment;
65    }
66
67    private ArrayList<String> mOptions;
68    private AdapterView.OnItemClickListener mListener;
69
70    @Override
71    public void onCreate(Bundle savedInstanceState) {
72        super.onCreate(savedInstanceState);
73        mOptions = getArguments().getStringArrayList(ARG_OPTION_LIST);
74    }
75
76    @Override
77    public void onAttach(Activity activity) {
78        super.onAttach(activity);
79    }
80
81    @Override
82    public View onCreateView(LayoutInflater inflater, ViewGroup container,
83            Bundle savedInstanceState) {
84        View content = inflater.inflate(R.layout.setup_content_area, null);
85        FrameLayout actionArea = (FrameLayout) content.findViewById(R.id.action);
86        FrameLayout descArea = (FrameLayout) content.findViewById(R.id.description);
87
88        View body = inflater.inflate(R.layout.setup_text_and_description, null);
89
90        // Title
91        String title = getArguments().getString(ARG_TITLE);
92        ((TextView) body.findViewById(R.id.title_text))
93                .setText(TextUtils.expandTemplate(title, Build.MODEL));
94
95        // Description
96        String descriptionText = getArguments().getString(ARG_DESCRIPTION);
97        String deviceName = DeviceManager.getDeviceName(getActivity());
98        ((TextView) body.findViewById(R.id.description_text))
99                .setText(TextUtils.expandTemplate(descriptionText, deviceName, Build.MODEL));
100
101        descArea.addView(body);
102
103        // Options
104        ScrollAdapterView actionList = (ScrollAdapterView) inflater.inflate(
105                R.layout.setup_scroll_adapter_view, null);
106        ScrollArrayAdapter<String> adapter = new ScrollArrayAdapter<String>(getActivity(),
107                R.layout.setup_list_item_text_only, R.id.list_item_text, mOptions);
108        actionList.setAdapter(adapter);
109        actionList.setOnItemClickListener(this);
110        actionArea.addView(actionList);
111
112        return content;
113    }
114
115    @Override
116    public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
117        // forward the result on
118        if (mListener != null) {
119            mListener.onItemClick(adapter, view, position, id);
120        } else {
121            Log.w(TAG, "Action selected, but no listener available.");
122        }
123    }
124
125    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
126        mListener = listener;
127    }
128}
129