blob: 9ae8fe9c5679647b742181e820accf8a7d7577f1 [file] [log] [blame]
Dmitri Plotnikovef038722009-06-24 18:51:47 -07001/*
2 * Copyright (C) 2009 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
17package com.android.contacts;
18
Isaac Katzenelson4ec19872011-07-06 13:50:27 -070019import android.app.ActionBar;
Dmitri Plotnikovef038722009-06-24 18:51:47 -070020import android.app.Activity;
21import android.content.ContentValues;
22import android.content.Intent;
23import android.database.Cursor;
24import android.media.Ringtone;
25import android.media.RingtoneManager;
26import android.net.Uri;
27import android.os.Bundle;
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070028import android.provider.ContactsContract.Contacts;
Dmitri Plotnikovef038722009-06-24 18:51:47 -070029import android.util.Log;
Isaac Katzenelson4ec19872011-07-06 13:50:27 -070030import android.view.MenuItem;
Dmitri Plotnikovef038722009-06-24 18:51:47 -070031import android.view.View;
32import android.widget.CheckBox;
33import android.widget.TextView;
34
35/**
36 * An activity for selecting options for a given contact: custom ringtone and send-to-voicemail.
37 */
38public class ContactOptionsActivity extends Activity implements View.OnClickListener {
39
40 private static final String TAG = "ContactOptionsActivity";
41
42 private static final String[] AGGREGATES_PROJECTION = new String[] {
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070043 Contacts.CUSTOM_RINGTONE, Contacts.SEND_TO_VOICEMAIL
Dmitri Plotnikovef038722009-06-24 18:51:47 -070044 };
45
46 private static final int COL_CUSTOM_RINGTONE = 0;
47 private static final int COL_SEND_TO_VOICEMAIL = 1;
48
49 /** The launch code when picking a ringtone */
50 private static final int RINGTONE_PICKED = 3023;
51
52 private String mCustomRingtone;
53 private boolean mSendToVoicemail;
54 private TextView mRingtoneTitle;
55 private CheckBox mSendToVoicemailCheckbox;
56
Evan Millar6a61a1a2009-09-29 14:00:43 -070057 private Uri mLookupUri;
Dmitri Plotnikovef038722009-06-24 18:51:47 -070058
59 @Override
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62
Evan Millar6a61a1a2009-09-29 14:00:43 -070063 mLookupUri = getIntent().getData();
Dmitri Plotnikovef038722009-06-24 18:51:47 -070064
65 setContentView(R.layout.contact_options);
66
67 View ringtoneLayout = findViewById(R.id.ringtone);
Daniel Lehmann861eff42010-02-22 16:17:29 -080068 ringtoneLayout.setFocusable(true);
Dmitri Plotnikovef038722009-06-24 18:51:47 -070069 ringtoneLayout.setOnClickListener(this);
70 TextView label = (TextView)findViewById(R.id.label);
71 label.setText(getString(R.string.label_ringtone));
72
73 mRingtoneTitle = (TextView)ringtoneLayout.findViewById(R.id.data);
74
75 View sendToVoicemailLayout = findViewById(R.id.voicemail);
76 sendToVoicemailLayout.setOnClickListener(this);
77 label = (TextView)sendToVoicemailLayout.findViewById(R.id.label);
78 label.setText(getString(R.string.actionIncomingCall));
79
Isaac Katzenelson4ec19872011-07-06 13:50:27 -070080 ActionBar actionBar = getActionBar();
81 if (actionBar != null) {
82 actionBar.setDisplayHomeAsUpEnabled(true);
83 }
84
Dmitri Plotnikovef038722009-06-24 18:51:47 -070085 mSendToVoicemailCheckbox = (CheckBox)sendToVoicemailLayout.findViewById(R.id.checkbox);
86 }
87
88 @Override
89 protected void onResume() {
90 super.onResume();
91
92 if (!loadData()) {
93 finish();
94 }
95
96 updateView();
97 }
98
99 private void updateView() {
100 if (mCustomRingtone == null) {
101 mRingtoneTitle.setText(getString(R.string.default_ringtone));
102 } else {
103 Uri ringtoneUri = Uri.parse(mCustomRingtone);
104 Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
105 if (ringtone == null) {
106 Log.w(TAG, "ringtone's URI doesn't resolve to a Ringtone");
107 return;
108 }
109 mRingtoneTitle.setText(ringtone.getTitle(this));
110 }
111
112 mSendToVoicemailCheckbox.setChecked(mSendToVoicemail);
113 }
114
115 public void onClick(View v) {
116 switch (v.getId()) {
117 case R.id.ringtone: {
118 doPickRingtone();
119 break;
120 }
121 case R.id.voicemail: {
122 doToggleSendToVoicemail();
123 break;
124 }
125 }
126 }
127
128 private void doPickRingtone() {
129
130 Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
131 // Allow user to pick 'Default'
132 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
133 // Show only ringtones
134 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
135 // Don't show 'Silent'
136 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
137
138 Uri ringtoneUri;
139 if (mCustomRingtone != null) {
140 ringtoneUri = Uri.parse(mCustomRingtone);
141 } else {
142 // Otherwise pick default ringtone Uri so that something is selected.
143 ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
144 }
145
146 // Put checkmark next to the current ringtone for this contact
147 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtoneUri);
148
149 // Launch!
150 startActivityForResult(intent, RINGTONE_PICKED);
151 }
152
153 @Override
154 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
155 if (resultCode != RESULT_OK) {
156 return;
157 }
158
159 switch (requestCode) {
160 case RINGTONE_PICKED: {
161 Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
162 handleRingtonePicked(pickedUri);
163 break;
164 }
165 }
166 }
167
168 private void handleRingtonePicked(Uri pickedUri) {
169 if (pickedUri == null || RingtoneManager.isDefault(pickedUri)) {
170 mCustomRingtone = null;
171 } else {
172 mCustomRingtone = pickedUri.toString();
173 }
174 saveData();
175 updateView();
176 }
177
178 private void doToggleSendToVoicemail() {
179 mSendToVoicemailCheckbox.toggle();
180 mSendToVoicemail = mSendToVoicemailCheckbox.isChecked();
181 saveData();
182 updateView();
183 }
184
185 private boolean loadData() {
186 Cursor c =
Evan Millar6a61a1a2009-09-29 14:00:43 -0700187 getContentResolver().query(mLookupUri, AGGREGATES_PROJECTION, null, null, null);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700188 try {
189 if (!c.moveToFirst()) {
190 return false;
191 }
192
193 mCustomRingtone = c.getString(COL_CUSTOM_RINGTONE);
194 mSendToVoicemail = c.getInt(COL_SEND_TO_VOICEMAIL) != 0;
195
196 } finally {
197 c.close();
198 }
199 return true;
200 }
201
202 private void saveData() {
203 ContentValues values = new ContentValues(2);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700204 values.put(Contacts.CUSTOM_RINGTONE, mCustomRingtone);
205 values.put(Contacts.SEND_TO_VOICEMAIL, mSendToVoicemail);
Evan Millar6a61a1a2009-09-29 14:00:43 -0700206 getContentResolver().update(mLookupUri, values, null, null);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700207 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800208
209 @Override
210 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
211 boolean globalSearch) {
212 if (globalSearch) {
213 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
214 } else {
215 ContactsSearchManager.startSearch(this, initialQuery);
216 }
217 }
Isaac Katzenelson4ec19872011-07-06 13:50:27 -0700218
219 @Override
220 public boolean onOptionsItemSelected(MenuItem item) {
221
222 switch (item.getItemId()) {
223 case android.R.id.home:
224 finish();
225 return true;
226 default:
227 break;
228 }
229 return super.onOptionsItemSelected(item);
230 }
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700231}
232
233