1 /* 2 * Copyright (C) 2017 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.settings.notification.app; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.media.Ringtone; 23 import android.media.RingtoneManager; 24 import android.net.Uri; 25 import android.os.AsyncTask; 26 import android.util.AttributeSet; 27 28 import android.util.Log; 29 import com.android.settings.R; 30 import com.android.settings.RingtonePreference; 31 32 public class NotificationSoundPreference extends RingtonePreference { 33 private static final String TAG = "NotificationSoundPreference"; 34 35 private Uri mRingtone; 36 NotificationSoundPreference(Context context, AttributeSet attrs)37 public NotificationSoundPreference(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 } 40 41 @Override onRestoreRingtone()42 protected Uri onRestoreRingtone() { 43 return mRingtone; 44 } 45 setRingtone(Uri ringtone)46 public void setRingtone(Uri ringtone) { 47 mRingtone = ringtone; 48 setSummary("\u00A0"); 49 updateRingtoneName(mRingtone); 50 } 51 52 @Override onActivityResult(int requestCode, int resultCode, Intent data)53 public boolean onActivityResult(int requestCode, int resultCode, Intent data) { 54 if (data != null) { 55 Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); 56 if (isValidRingtoneUri(uri)) { 57 setRingtone(uri); 58 callChangeListener(uri); 59 } else { 60 Log.e(TAG, "onActivityResult for URI:" + uri 61 + " ignored: invalid ringtone Uri"); 62 } 63 } 64 65 return true; 66 } 67 updateRingtoneName(final Uri uri)68 private void updateRingtoneName(final Uri uri) { 69 AsyncTask ringtoneNameTask = new AsyncTask<Object, Void, CharSequence>() { 70 @Override 71 protected CharSequence doInBackground(Object... params) { 72 if (uri == null) { 73 return getContext().getString(com.android.internal.R.string.ringtone_silent); 74 } else if (RingtoneManager.isDefault(uri)) { 75 return getContext().getString(R.string.notification_sound_default); 76 } else if(ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())) { 77 return getContext().getString(R.string.notification_unknown_sound_title); 78 } else { 79 return Ringtone.getTitle(getContext(), uri, false /* followSettingsUri */, 80 true /* allowRemote */); 81 } 82 } 83 84 @Override 85 protected void onPostExecute(CharSequence name) { 86 setSummary(name); 87 } 88 }; 89 ringtoneNameTask.execute(); 90 } 91 } 92