1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/* 2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project 3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License"); 5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License. 6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at 7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * http://www.apache.org/licenses/LICENSE-2.0 9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * 10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software 11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS, 12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and 14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License. 15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.ui.conversationsettings; 17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context; 19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.database.Cursor; 20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.v7.widget.SwitchCompat; 21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.text.TextUtils; 22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.util.AttributeSet; 23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.View; 24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.LinearLayout; 25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.TextView; 26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R; 28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DataModel; 29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.ParticipantData; 30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.PeopleOptionsItemData; 31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.Assert; 32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/** 34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * The view for a single entry in the options section of people & options activity. 35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class PeopleOptionsItemView extends LinearLayout { 37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** 38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Implemented by the host of this view that handles options click event. 39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public interface HostInterface { 41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd void onOptionsItemViewClicked(PeopleOptionsItemData item, boolean isChecked); 42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private TextView mTitle; 45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private TextView mSubtitle; 46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private SwitchCompat mSwitch; 47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private final PeopleOptionsItemData mData; 48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private HostInterface mHostInterface; 49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public PeopleOptionsItemView(final Context context, final AttributeSet attrs) { 51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd super(context, attrs); 52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mData = DataModel.get().createPeopleOptionsItemData(context); 53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd protected void onFinishInflate () { 57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mTitle = (TextView) findViewById(R.id.title); 58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSubtitle = (TextView) findViewById(R.id.subtitle); 59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSwitch = (SwitchCompat) findViewById(R.id.switch_button); 60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd setOnClickListener(new OnClickListener() { 61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public void onClick(final View v) { 63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mHostInterface.onOptionsItemViewClicked(mData, !mData.getChecked()); 64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd }); 66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public void bind(final Cursor cursor, final int columnIndex, ParticipantData otherParticipant, 69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final HostInterface hostInterface) { 70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd Assert.isTrue(columnIndex < PeopleOptionsItemData.SETTINGS_COUNT && columnIndex >= 0); 71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mData.bind(cursor, otherParticipant, columnIndex); 72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mHostInterface = hostInterface; 73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mTitle.setText(mData.getTitle()); 75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String subtitle = mData.getSubtitle(); 76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (TextUtils.isEmpty(subtitle)) { 77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSubtitle.setVisibility(GONE); 78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } else { 79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSubtitle.setVisibility(VISIBLE); 80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSubtitle.setText(subtitle); 81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (mData.getCheckable()) { 84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSwitch.setVisibility(VISIBLE); 85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSwitch.setChecked(mData.getChecked()); 86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } else { 87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSwitch.setVisibility(GONE); 88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final boolean enabled = mData.getEnabled(); 91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (enabled != isEnabled()) { 92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mTitle.setEnabled(enabled); 93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSubtitle.setEnabled(enabled); 94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mSwitch.setEnabled(enabled); 95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd setAlpha(enabled ? 1.0f : 0.5f); 96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd setEnabled(enabled); 97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd} 100