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 com.android.settings.DefaultRingtonePreference;
21 import com.android.settings.Utils;
22 
23 import android.content.ContentResolver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.database.Cursor;
27 import android.database.sqlite.SQLiteException;
28 import android.media.Ringtone;
29 import android.media.RingtoneManager;
30 import android.os.AsyncTask;
31 import android.os.UserHandle;
32 import android.os.UserManager;
33 import android.net.Uri;
34 import android.provider.MediaStore;
35 import android.provider.OpenableColumns;
36 import android.util.AttributeSet;
37 
38 import static android.content.ContentProvider.getUriWithoutUserId;
39 
40 public class DefaultNotificationTonePreference extends DefaultRingtonePreference {
41     private Uri mRingtone;
42 
DefaultNotificationTonePreference(Context context, AttributeSet attrs)43     public DefaultNotificationTonePreference(Context context, AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
47     @Override
onRestoreRingtone()48     protected Uri onRestoreRingtone() {
49         return mRingtone;
50     }
51 
52     @Override
onPrepareRingtonePickerIntent(Intent ringtonePickerIntent)53     public void onPrepareRingtonePickerIntent(Intent ringtonePickerIntent) {
54         super.onPrepareRingtonePickerIntent(ringtonePickerIntent);
55         ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
56                 mRingtone);
57     }
58 
setRingtone(Uri ringtone)59     public void setRingtone(Uri ringtone) {
60         mRingtone = ringtone;
61         updateRingtoneName(mRingtone);
62     }
63 
updateRingtoneName(final Uri uri)64     private void updateRingtoneName(final Uri uri) {
65         AsyncTask ringtoneNameTask = new AsyncTask<Object, Void, CharSequence>() {
66             @Override
67             protected CharSequence doInBackground(Object... params) {
68                 return Ringtone.getTitle(mUserContext, uri, false /* followSettingsUri */,
69                         true /* allowRemote */);
70             }
71 
72             @Override
73             protected void onPostExecute(CharSequence name) {
74                 setSummary(name);
75             }
76         };
77         ringtoneNameTask.execute();
78     }
79 }