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.datamodel.data;
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context;
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.database.Cursor;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.media.Ringtone;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.media.RingtoneManager;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.ConversationListItemData.ConversationListViewColumns;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.Assert;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.RingtoneUtil;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class PeopleOptionsItemData {
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final String[] PROJECTION = {
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        ConversationListViewColumns.NOTIFICATION_ENABLED,
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        ConversationListViewColumns.NOTIFICATION_SOUND_URI,
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        ConversationListViewColumns.NOTIFICATION_VIBRATION,
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    };
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // Column index for query projection.
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_NOTIFICATION_ENABLED = 0;
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_NOTIFICATION_SOUND_URI = 1;
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_NOTIFICATION_VIBRATION = 2;
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // Identification for each setting that's surfaced to the UI layer.
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int SETTING_NOTIFICATION_ENABLED = 0;
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int SETTING_NOTIFICATION_SOUND_URI = 1;
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int SETTING_NOTIFICATION_VIBRATION = 2;
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int SETTING_BLOCKED = 3;
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int SETTINGS_COUNT = 4;
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // Type of UI switch to show for the toggle button.
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int TOGGLE_TYPE_CHECKBOX = 0;
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int TOGGLE_TYPE_SWITCH = 1;
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private String mTitle;
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private String mSubtitle;
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private Uri mRingtoneUri;
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private boolean mCheckable;
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private boolean mChecked;
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private boolean mEnabled;
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private int mItemId;
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private ParticipantData mOtherParticipant;
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final Context mContext;
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public PeopleOptionsItemData(final Context context) {
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mContext = context;
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Bind to a specific setting column on conversation metadata cursor. (Note
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * that it binds to columns because it treats individual columns of the cursor as
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * separate options to display for the conversation, e.g. notification settings).
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void bind(
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final Cursor cursor, final ParticipantData otherParticipant, final int settingType) {
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSubtitle = null;
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRingtoneUri = null;
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mCheckable = true;
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mEnabled = true;
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mItemId = settingType;
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mOtherParticipant = otherParticipant;
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final boolean notificationEnabled = cursor.getInt(INDEX_NOTIFICATION_ENABLED) == 1;
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        switch (settingType) {
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case SETTING_NOTIFICATION_ENABLED:
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mTitle = mContext.getString(R.string.notifications_enabled_conversation_pref_title);
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mChecked = notificationEnabled;
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case SETTING_NOTIFICATION_SOUND_URI:
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mTitle = mContext.getString(R.string.notification_sound_pref_title);
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final String ringtoneString = cursor.getString(INDEX_NOTIFICATION_SOUND_URI);
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                Uri ringtoneUri = RingtoneUtil.getNotificationRingtoneUri(ringtoneString);
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mSubtitle = mContext.getString(R.string.silent_ringtone);
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                if (ringtoneUri != null) {
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    final Ringtone ringtone = RingtoneManager.getRingtone(mContext, ringtoneUri);
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    if (ringtone != null) {
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        mSubtitle = ringtone.getTitle(mContext);
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    }
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mCheckable = false;
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mRingtoneUri = ringtoneUri;
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mEnabled = notificationEnabled;
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case SETTING_NOTIFICATION_VIBRATION:
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mTitle = mContext.getString(R.string.notification_vibrate_pref_title);
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mChecked = cursor.getInt(INDEX_NOTIFICATION_VIBRATION) == 1;
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mEnabled = notificationEnabled;
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case SETTING_BLOCKED:
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                Assert.notNull(otherParticipant);
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final int resourceId = otherParticipant.isBlocked() ?
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        R.string.unblock_contact_title : R.string.block_contact_title;
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mTitle = mContext.getString(resourceId, otherParticipant.getDisplayDestination());
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mCheckable = false;
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd             default:
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                 Assert.fail("Unsupported conversation option type!");
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public String getTitle() {
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mTitle;
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public String getSubtitle() {
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mSubtitle;
130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public boolean getCheckable() {
133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mCheckable;
134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public boolean getChecked() {
137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mChecked;
138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public boolean getEnabled() {
141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mEnabled;
142d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
143d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
144d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public int getItemId() {
145d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mItemId;
146d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
147d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
148d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public Uri getRingtoneUri() {
149d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mRingtoneUri;
150d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
151d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
152d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public ParticipantData getOtherParticipant() {
153d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mOtherParticipant;
154d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
155d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
156