• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.car.settings.sound;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.media.AudioAttributes;
22 import android.media.Ringtone;
23 import android.media.RingtoneManager;
24 import android.net.Uri;
25 import android.os.Bundle;
26 
27 import androidx.fragment.app.Fragment;
28 
29 import com.android.car.settings.common.FragmentController;
30 import com.android.car.settings.common.PreferenceController;
31 
32 /** Business logic for changing the default ringtone. */
33 public class RingtonePreferenceController extends PreferenceController<RingtonePreference> {
34 
35     private RingtoneManager mRingtoneManager;
36 
RingtonePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)37     public RingtonePreferenceController(Context context, String preferenceKey,
38             FragmentController fragmentController,
39             CarUxRestrictions uxRestrictions) {
40         super(context, preferenceKey, fragmentController, uxRestrictions);
41         mRingtoneManager = new RingtoneManager(context);
42     }
43 
44     @Override
onCreateInternal()45     protected void onCreateInternal() {
46         mRingtoneManager.setType(getPreference().getRingtoneType());
47     }
48 
49     @Override
getPreferenceType()50     protected Class<RingtonePreference> getPreferenceType() {
51         return RingtonePreference.class;
52     }
53 
54     @Override
updateState(RingtonePreference preference)55     protected void updateState(RingtonePreference preference) {
56         Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(getContext(),
57                 getPreference().getRingtoneType());
58         // If this URI cannot be found by the ringtone manager, set the URI to be null.
59         if (mRingtoneManager.getRingtonePosition(ringtoneUri) < 0) {
60             ringtoneUri = null;
61         }
62         preference.setSummary(Ringtone.getTitle(getContext(), ringtoneUri,
63                 /* followSettingsUri= */ false, /* allowRemote= */ true));
64     }
65 
66     @Override
handlePreferenceClicked(RingtonePreference preference)67     protected boolean handlePreferenceClicked(RingtonePreference preference) {
68         getFragmentController().launchFragment(createRingtonePickerFragment(preference));
69         return true;
70     }
71 
72     /**
73      * Prepares the fragment to launch the ringtone picker. This can be modified
74      * to adjust the parameters of the ringtone picker.
75      */
createRingtonePickerFragment(RingtonePreference ringtonePreference)76     private Fragment createRingtonePickerFragment(RingtonePreference ringtonePreference) {
77         Fragment fragment = new RingtonePickerFragment();
78         Bundle args = fragment.getArguments();
79         if (args == null) {
80             args = new Bundle();
81             fragment.setArguments(args);
82         }
83         args.putCharSequence(RingtoneManager.EXTRA_RINGTONE_TITLE, ringtonePreference.getTitle());
84         args.putInt(RingtoneManager.EXTRA_RINGTONE_TYPE, ringtonePreference.getRingtoneType());
85         args.putBoolean(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT,
86                 ringtonePreference.getShowSilent());
87 
88         // Allow playback in external activity.
89         args.putInt(RingtoneManager.EXTRA_RINGTONE_AUDIO_ATTRIBUTES_FLAGS,
90                 AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY);
91 
92         return fragment;
93     }
94 }
95