• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
onCreate(Bundle savedInstanceState)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.setOnClickListener(this);
67         TextView label = (TextView)findViewById(R.id.label);
68         label.setText(getString(R.string.label_ringtone));
69 
70         mRingtoneTitle = (TextView)ringtoneLayout.findViewById(R.id.data);
71 
72         View sendToVoicemailLayout = findViewById(R.id.voicemail);
73         sendToVoicemailLayout.setOnClickListener(this);
74         label = (TextView)sendToVoicemailLayout.findViewById(R.id.label);
75         label.setText(getString(R.string.actionIncomingCall));
76 
77         mSendToVoicemailCheckbox = (CheckBox)sendToVoicemailLayout.findViewById(R.id.checkbox);
78     }
79 
80     @Override
onResume()81     protected void onResume() {
82         super.onResume();
83 
84         if (!loadData()) {
85             finish();
86         }
87 
88         updateView();
89     }
90 
updateView()91     private void updateView() {
92         if (mCustomRingtone == null) {
93             mRingtoneTitle.setText(getString(R.string.default_ringtone));
94         } else {
95             Uri ringtoneUri = Uri.parse(mCustomRingtone);
96             Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
97             if (ringtone == null) {
98                 Log.w(TAG, "ringtone's URI doesn't resolve to a Ringtone");
99                 return;
100             }
101             mRingtoneTitle.setText(ringtone.getTitle(this));
102         }
103 
104         mSendToVoicemailCheckbox.setChecked(mSendToVoicemail);
105     }
106 
onClick(View v)107     public void onClick(View v) {
108         switch (v.getId()) {
109             case R.id.ringtone: {
110                 doPickRingtone();
111                 break;
112             }
113             case R.id.voicemail: {
114                 doToggleSendToVoicemail();
115                 break;
116             }
117         }
118     }
119 
doPickRingtone()120     private void doPickRingtone() {
121 
122         Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
123         // Allow user to pick 'Default'
124         intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
125         // Show only ringtones
126         intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
127         // Don't show 'Silent'
128         intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
129 
130         Uri ringtoneUri;
131         if (mCustomRingtone != null) {
132             ringtoneUri = Uri.parse(mCustomRingtone);
133         } else {
134             // Otherwise pick default ringtone Uri so that something is selected.
135             ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
136         }
137 
138         // Put checkmark next to the current ringtone for this contact
139         intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtoneUri);
140 
141         // Launch!
142         startActivityForResult(intent, RINGTONE_PICKED);
143     }
144 
145     @Override
onActivityResult(int requestCode, int resultCode, Intent data)146     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
147         if (resultCode != RESULT_OK) {
148             return;
149         }
150 
151         switch (requestCode) {
152             case RINGTONE_PICKED: {
153                 Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
154                 handleRingtonePicked(pickedUri);
155                 break;
156             }
157         }
158     }
159 
handleRingtonePicked(Uri pickedUri)160     private void handleRingtonePicked(Uri pickedUri) {
161         if (pickedUri == null || RingtoneManager.isDefault(pickedUri)) {
162             mCustomRingtone = null;
163         } else {
164             mCustomRingtone = pickedUri.toString();
165         }
166         saveData();
167         updateView();
168     }
169 
doToggleSendToVoicemail()170     private void doToggleSendToVoicemail() {
171         mSendToVoicemailCheckbox.toggle();
172         mSendToVoicemail = mSendToVoicemailCheckbox.isChecked();
173         saveData();
174         updateView();
175     }
176 
loadData()177     private boolean loadData() {
178         Cursor c =
179                 getContentResolver().query(mLookupUri, AGGREGATES_PROJECTION, null, null, null);
180         try {
181             if (!c.moveToFirst()) {
182                 return false;
183             }
184 
185             mCustomRingtone = c.getString(COL_CUSTOM_RINGTONE);
186             mSendToVoicemail = c.getInt(COL_SEND_TO_VOICEMAIL) != 0;
187 
188         } finally {
189             c.close();
190         }
191         return true;
192     }
193 
saveData()194     private void saveData() {
195         ContentValues values = new ContentValues(2);
196         values.put(Contacts.CUSTOM_RINGTONE, mCustomRingtone);
197         values.put(Contacts.SEND_TO_VOICEMAIL, mSendToVoicemail);
198         getContentResolver().update(mLookupUri, values, null, null);
199     }
200 }
201 
202 
203