1/*
2 * Copyright (C) 2015 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 */
16package com.android.messaging.ui.conversationsettings;
17
18import android.content.Context;
19import android.database.Cursor;
20import android.support.v7.widget.SwitchCompat;
21import android.text.TextUtils;
22import android.util.AttributeSet;
23import android.view.View;
24import android.widget.LinearLayout;
25import android.widget.TextView;
26
27import com.android.messaging.R;
28import com.android.messaging.datamodel.DataModel;
29import com.android.messaging.datamodel.data.ParticipantData;
30import com.android.messaging.datamodel.data.PeopleOptionsItemData;
31import com.android.messaging.util.Assert;
32
33/**
34 * The view for a single entry in the options section of people & options activity.
35 */
36public class PeopleOptionsItemView extends LinearLayout {
37    /**
38     * Implemented by the host of this view that handles options click event.
39     */
40    public interface HostInterface {
41        void onOptionsItemViewClicked(PeopleOptionsItemData item, boolean isChecked);
42    }
43
44    private TextView mTitle;
45    private TextView mSubtitle;
46    private SwitchCompat mSwitch;
47    private final PeopleOptionsItemData mData;
48    private HostInterface mHostInterface;
49
50    public PeopleOptionsItemView(final Context context, final AttributeSet attrs) {
51        super(context, attrs);
52        mData = DataModel.get().createPeopleOptionsItemData(context);
53    }
54
55    @Override
56    protected void onFinishInflate () {
57        mTitle = (TextView) findViewById(R.id.title);
58        mSubtitle = (TextView) findViewById(R.id.subtitle);
59        mSwitch = (SwitchCompat) findViewById(R.id.switch_button);
60        setOnClickListener(new OnClickListener() {
61            @Override
62            public void onClick(final View v) {
63                mHostInterface.onOptionsItemViewClicked(mData, !mData.getChecked());
64            }
65        });
66    }
67
68    public void bind(final Cursor cursor, final int columnIndex, ParticipantData otherParticipant,
69            final HostInterface hostInterface) {
70        Assert.isTrue(columnIndex < PeopleOptionsItemData.SETTINGS_COUNT && columnIndex >= 0);
71        mData.bind(cursor, otherParticipant, columnIndex);
72        mHostInterface = hostInterface;
73
74        mTitle.setText(mData.getTitle());
75        final String subtitle = mData.getSubtitle();
76        if (TextUtils.isEmpty(subtitle)) {
77            mSubtitle.setVisibility(GONE);
78        } else {
79            mSubtitle.setVisibility(VISIBLE);
80            mSubtitle.setText(subtitle);
81        }
82
83        if (mData.getCheckable()) {
84            mSwitch.setVisibility(VISIBLE);
85            mSwitch.setChecked(mData.getChecked());
86        } else {
87            mSwitch.setVisibility(GONE);
88        }
89
90        final boolean enabled = mData.getEnabled();
91        if (enabled != isEnabled()) {
92            mTitle.setEnabled(enabled);
93            mSubtitle.setEnabled(enabled);
94            mSwitch.setEnabled(enabled);
95            setAlpha(enabled ? 1.0f : 0.5f);
96            setEnabled(enabled);
97        }
98    }
99}
100