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; 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 com.android.settings.R; 29 import com.android.settings.RingtonePreference; 30 31 public class NotificationSoundPreference extends RingtonePreference { 32 private Uri mRingtone; 33 NotificationSoundPreference(Context context, AttributeSet attrs)34 public NotificationSoundPreference(Context context, AttributeSet attrs) { 35 super(context, attrs); 36 } 37 38 @Override onRestoreRingtone()39 protected Uri onRestoreRingtone() { 40 return mRingtone; 41 } 42 setRingtone(Uri ringtone)43 public void setRingtone(Uri ringtone) { 44 mRingtone = ringtone; 45 setSummary("\u00A0"); 46 updateRingtoneName(mRingtone); 47 } 48 49 @Override onActivityResult(int requestCode, int resultCode, Intent data)50 public boolean onActivityResult(int requestCode, int resultCode, Intent data) { 51 if (data != null) { 52 Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); 53 setRingtone(uri); 54 callChangeListener(uri); 55 } 56 57 return true; 58 } 59 updateRingtoneName(final Uri uri)60 private void updateRingtoneName(final Uri uri) { 61 AsyncTask ringtoneNameTask = new AsyncTask<Object, Void, CharSequence>() { 62 @Override 63 protected CharSequence doInBackground(Object... params) { 64 if (uri == null) { 65 return getContext().getString(com.android.internal.R.string.ringtone_silent); 66 } else if (RingtoneManager.isDefault(uri)) { 67 return getContext().getString(R.string.notification_sound_default); 68 } else if(ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())) { 69 return getContext().getString(R.string.notification_unknown_sound_title); 70 } else { 71 return Ringtone.getTitle(getContext(), uri, false /* followSettingsUri */, 72 true /* allowRemote */); 73 } 74 } 75 76 @Override 77 protected void onPostExecute(CharSequence name) { 78 setSummary(name); 79 } 80 }; 81 ringtoneNameTask.execute(); 82 } 83 } 84