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  */
16 package com.android.messaging.ui.conversationsettings;
17 
18 import android.content.Context;
19 import android.database.Cursor;
20 import androidx.appcompat.widget.SwitchCompat;
21 import android.text.TextUtils;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.LinearLayout;
25 import android.widget.TextView;
26 
27 import com.android.messaging.R;
28 import com.android.messaging.datamodel.DataModel;
29 import com.android.messaging.datamodel.data.ParticipantData;
30 import com.android.messaging.datamodel.data.PeopleOptionsItemData;
31 import com.android.messaging.util.Assert;
32 
33 /**
34  * The view for a single entry in the options section of people & options activity.
35  */
36 public class PeopleOptionsItemView extends LinearLayout {
37     /**
38      * Implemented by the host of this view that handles options click event.
39      */
40     public interface HostInterface {
onOptionsItemViewClicked(PeopleOptionsItemData item, boolean isChecked)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 
PeopleOptionsItemView(final Context context, final AttributeSet attrs)50     public PeopleOptionsItemView(final Context context, final AttributeSet attrs) {
51         super(context, attrs);
52         mData = DataModel.get().createPeopleOptionsItemData(context);
53     }
54 
55     @Override
onFinishInflate()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 
bind(final Cursor cursor, final int columnIndex, ParticipantData otherParticipant, final HostInterface hostInterface)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