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