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