• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.content.Context;
20 import android.media.RingtoneManager;
21 import android.os.Bundle;
22 import android.view.View;
23 import android.view.ViewTreeObserver;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.annotation.XmlRes;
28 import androidx.recyclerview.widget.LinearLayoutManager;
29 
30 import com.android.car.settings.R;
31 import com.android.car.settings.common.SettingsFragment;
32 import com.android.car.ui.toolbar.MenuItem;
33 import com.android.car.ui.toolbar.ToolbarController;
34 
35 import java.util.Collections;
36 import java.util.List;
37 
38 /** Ringtone picker fragment. */
39 public class RingtonePickerFragment extends SettingsFragment {
40 
41     private RingtonePickerPreferenceController mPreferenceController;
42     private MenuItem mSaveButton;
43 
44     @Override
45     @XmlRes
getPreferenceScreenResId()46     protected int getPreferenceScreenResId() {
47         return R.xml.ringtone_picker_fragment;
48     }
49 
50     @Override
onAttach(Context context)51     public void onAttach(Context context) {
52         super.onAttach(context);
53 
54         mPreferenceController = use(RingtonePickerPreferenceController.class,
55                 R.string.pk_ringtone_picker);
56         mPreferenceController.setArguments(getArguments());
57         mSaveButton = new MenuItem.Builder(getContext())
58                 .setTitle(R.string.ringtone_picker_save_title)
59                 .setOnClickListener(item -> {
60                     mPreferenceController.saveRingtone();
61                     goBack();
62                 })
63                 .build();
64     }
65 
66     @Override
getToolbarMenuItems()67     protected List<MenuItem> getToolbarMenuItems() {
68         return Collections.singletonList(mSaveButton);
69     }
70 
71     @Override
setupToolbar(@onNull ToolbarController toolbar)72     protected void setupToolbar(@NonNull ToolbarController toolbar) {
73         super.setupToolbar(toolbar);
74 
75         toolbar.setTitle(getArguments().getCharSequence(RingtoneManager.EXTRA_RINGTONE_TITLE));
76     }
77 
78     @Override
onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)79     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
80         super.onViewCreated(view, savedInstanceState);
81 
82         // Logic to scroll to the selected item. This needs to be done in a global layout listener
83         // so that it can be triggered after the sound items added dynamically in the
84         // PreferenceScreen.
85         getListView().getViewTreeObserver().addOnGlobalLayoutListener(
86                 new ViewTreeObserver.OnGlobalLayoutListener() {
87                     @Override
88                     public void onGlobalLayout() {
89                         // This should only be triggered once per onViewCreated.
90                         getListView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
91 
92                         // There are various methods on the PreferenceFragment and RecyclerView
93                         // that should be able to scroll to the desired preference. However this
94                         // approach is the most reliable with dynamically added preferences.
95                         LinearLayoutManager layoutManager =
96                                 (LinearLayoutManager) getListView().getLayoutManager();
97                         layoutManager.scrollToPositionWithOffset(
98                                 mPreferenceController.getCurrentlySelectedPreferencePos(),
99                                 /* offset= */ 0);
100 
101                         // This will only work after the scrolling has completed, since the item
102                         // may not be immediately visible. Setting this item to be selected to allow
103                         // this item to be rotary focused by default if in rotary mode.
104                         getListView().post(() -> {
105                             View itemView = getListView().findViewById(
106                                     R.id.ringtone_picker_selected_id);
107                             itemView = layoutManager.findContainingItemView(itemView);
108                             itemView.setSelected(true);
109                         });
110                     }
111                 });
112     }
113 }
114