/
ContactOptionsActivity.java
executable file
·264 lines (229 loc) · 9.21 KB
/
ContactOptionsActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lewa.PIM.contacts;
import com.lewa.PIM.R;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.RingtonePreference;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
/**
* An activity for selecting options for a given contact: custom ringtone and send-to-voicemail.
*/
public class ContactOptionsActivity extends PreferenceActivity implements OnPreferenceChangeListener{
private static final String TAG = "ContactOptionsActivity";
private static final String PRE_RING_TONE = "pref_key_ringtone_contacts";
private static final String PRE_RING_SEND_TO_VOICEMAIL = "pref_key_send_to_voicemail";
private static final String[] AGGREGATES_PROJECTION = new String[] {
Contacts.CUSTOM_RINGTONE, Contacts.SEND_TO_VOICEMAIL
};
private static final int COL_CUSTOM_RINGTONE = 0;
private static final int COL_SEND_TO_VOICEMAIL = 1;
/** The launch code when picking a ringtone */
private static final int RINGTONE_PICKED = 3023;
private String mCustomRingtone;
private boolean mSendToVoicemail;
private TextView mRingtoneTitle;
private CheckBox mSendToVoicemailCheckbox;
private RingtonePreference mRingtonePreference;
private CheckBoxPreference mSendToVoicemailCheckPre;
private Uri mLookupUri;
private SharedPreferences mePrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLookupUri = getIntent().getData();
mePrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
// setContentView(R.layout.contact_options);
// View ringtoneLayout = findViewById(R.id.ringtone);
// ringtoneLayout.setFocusable(true);
// ringtoneLayout.setOnClickListener(this);
// TextView label = (TextView)findViewById(R.id.label);
// label.setText(getString(R.string.label_ringtone));
//
// mRingtoneTitle = (TextView)ringtoneLayout.findViewById(R.id.data);
//
// View sendToVoicemailLayout = findViewById(R.id.voicemail);
// sendToVoicemailLayout.setOnClickListener(this);
// label = (TextView)sendToVoicemailLayout.findViewById(R.id.label);
// label.setText(getString(R.string.actionIncomingCall));
// mSendToVoicemailCheckbox = (CheckBox)sendToVoicemailLayout.findViewById(R.id.checkbox);
addPreferencesFromResource(R.xml.contact_options);
mRingtonePreference = (RingtonePreference)findPreference(PRE_RING_TONE);
((Preference)mRingtonePreference).setOnPreferenceChangeListener(this);
mSendToVoicemailCheckPre = (CheckBoxPreference)findPreference(PRE_RING_SEND_TO_VOICEMAIL);
mSendToVoicemailCheckPre.setOnPreferenceChangeListener(this);
}
@Override
protected void onResume() {
super.onResume();
if (!loadData()) {
finish();
}
updateView();
}
private void updateView() {
// if (mCustomRingtone == null) {
// mRingtoneTitle.setText(getString(R.string.default_ringtone));
// } else {
// Uri ringtoneUri = Uri.parse(mCustomRingtone);
// Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
// if (ringtone == null) {
// Log.w(TAG, "ringtone's URI doesn't resolve to a Ringtone");
// return;
// }
// mRingtoneTitle.setText(ringtone.getTitle(this));
// }
//
// mSendToVoicemailCheckbox.setChecked(mSendToVoicemail);
Uri ringtoneUri;
if (mCustomRingtone != null) {
ringtoneUri = Uri.parse(mCustomRingtone);
} else {
// Otherwise pick default ringtone Uri so that something is selected.
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
if (ringtone != null) {
mRingtonePreference.setSummary(ringtone.getTitle(this));
SharedPreferences.Editor editor = mePrefs.edit();
editor.putString(PRE_RING_TONE, ringtoneUri.toString());
editor.commit();
}
mSendToVoicemailCheckPre.setChecked(mSendToVoicemail);
}
// public void onClick(View v) {
// switch (v.getId()) {
// case R.id.ringtone: {
// doPickRingtone();
// break;
// }
// case R.id.voicemail: {
// doToggleSendToVoicemail();
// break;
// }
// }
// }
// private void doPickRingtone() {
//
// Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
// // Allow user to pick 'Default'
// intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
// // Show only ringtones
// intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
// // Don't show 'Silent'
// intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
//
// Uri ringtoneUri;
// if (mCustomRingtone != null) {
// ringtoneUri = Uri.parse(mCustomRingtone);
// } else {
// // Otherwise pick default ringtone Uri so that something is selected.
// ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
// }
//
// // Put checkmark next to the current ringtone for this contact
// intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtoneUri);
//
// // Launch!
// startActivityForResult(intent, RINGTONE_PICKED);
// }
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if (resultCode != RESULT_OK) {
// return;
// }
//
// switch (requestCode) {
// case RINGTONE_PICKED: {
// Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
// handleRingtonePicked(pickedUri);
// break;
// }
// }
// }
private void handleRingtonePicked(Uri pickedUri) {
if (pickedUri == null || RingtoneManager.isDefault(pickedUri)) {
mCustomRingtone = null;
} else {
mCustomRingtone = pickedUri.toString();
}
saveData();
updateView();
}
private void doToggleSendToVoicemail() {
mSendToVoicemailCheckbox.toggle();
mSendToVoicemail = mSendToVoicemailCheckbox.isChecked();
saveData();
updateView();
}
private boolean loadData() {
Cursor c =
getContentResolver().query(mLookupUri, AGGREGATES_PROJECTION, null, null, null);
try {
if (!c.moveToFirst()) {
return false;
}
mCustomRingtone = c.getString(COL_CUSTOM_RINGTONE);
mSendToVoicemail = c.getInt(COL_SEND_TO_VOICEMAIL) != 0;
} finally {
c.close();
}
return true;
}
private void saveData() {
ContentValues values = new ContentValues(2);
values.put(Contacts.CUSTOM_RINGTONE, mCustomRingtone);
values.put(Contacts.SEND_TO_VOICEMAIL, mSendToVoicemail);
getContentResolver().update(mLookupUri, values, null, null);
}
@Override
public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
boolean globalSearch) {
if (globalSearch) {
super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
} else {
ContactsSearchManager.startSearch(this, initialQuery);
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// TODO Auto-generated method stub
if (preference == mRingtonePreference) {
mCustomRingtone = (String)newValue;
saveData();
} else if (preference == mSendToVoicemailCheckPre) {
mSendToVoicemail = (Boolean)newValue;
saveData();
}
return true;
}
}