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