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
17package com.android.settings.notification;
18
19
20import com.android.settings.DefaultRingtonePreference;
21import com.android.settings.Utils;
22
23import android.content.ContentResolver;
24import android.content.Context;
25import android.content.Intent;
26import android.database.Cursor;
27import android.database.sqlite.SQLiteException;
28import android.media.Ringtone;
29import android.media.RingtoneManager;
30import android.os.AsyncTask;
31import android.os.UserHandle;
32import android.os.UserManager;
33import android.net.Uri;
34import android.provider.MediaStore;
35import android.provider.OpenableColumns;
36import android.util.AttributeSet;
37
38import static android.content.ContentProvider.getUriWithoutUserId;
39
40public class DefaultNotificationTonePreference extends DefaultRingtonePreference {
41    private Uri mRingtone;
42
43    public DefaultNotificationTonePreference(Context context, AttributeSet attrs) {
44        super(context, attrs);
45    }
46
47    @Override
48    protected Uri onRestoreRingtone() {
49        return mRingtone;
50    }
51
52    @Override
53    public void onPrepareRingtonePickerIntent(Intent ringtonePickerIntent) {
54        super.onPrepareRingtonePickerIntent(ringtonePickerIntent);
55        ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
56                mRingtone);
57    }
58
59    public void setRingtone(Uri ringtone) {
60        mRingtone = ringtone;
61        updateRingtoneName(mRingtone);
62    }
63
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}