1 /* 2 * Copyright (C) 2016 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 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.DefaultRingtonePreference; 29 30 public class DefaultNotificationTonePreference extends DefaultRingtonePreference { 31 private Uri mRingtone; 32 DefaultNotificationTonePreference(Context context, AttributeSet attrs)33 public DefaultNotificationTonePreference(Context context, AttributeSet attrs) { 34 super(context, attrs); 35 } 36 37 @Override onRestoreRingtone()38 protected Uri onRestoreRingtone() { 39 return mRingtone; 40 } 41 42 @Override onPrepareRingtonePickerIntent(Intent ringtonePickerIntent)43 public void onPrepareRingtonePickerIntent(Intent ringtonePickerIntent) { 44 super.onPrepareRingtonePickerIntent(ringtonePickerIntent); 45 ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, 46 mRingtone); 47 } 48 setRingtone(Uri ringtone)49 public void setRingtone(Uri ringtone) { 50 mRingtone = ringtone; 51 updateRingtoneName(mRingtone); 52 } 53 updateRingtoneName(final Uri uri)54 private void updateRingtoneName(final Uri uri) { 55 AsyncTask ringtoneNameTask = new AsyncTask<Object, Void, CharSequence>() { 56 @Override 57 protected CharSequence doInBackground(Object... params) { 58 return Ringtone.getTitle(mUserContext, uri, false /* followSettingsUri */, 59 true /* allowRemote */); 60 } 61 62 @Override 63 protected void onPostExecute(CharSequence name) { 64 setSummary(name); 65 } 66 }; 67 ringtoneNameTask.execute(); 68 } 69 }