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