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.settings.notification;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 import android.text.TextUtils;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.settings.R;
30 import com.android.settings.RingtonePreference;
31 import com.android.settings.core.OnActivityResultListener;
32 import com.android.settings.dashboard.DashboardFragment;
33 import com.android.settings.search.BaseSearchIndexProvider;
34 import com.android.settingslib.core.AbstractPreferenceController;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 import com.android.settingslib.search.SearchIndexable;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 /** Sounds settings for work profile. */
42 @SearchIndexable
43 public class SoundWorkSettings extends DashboardFragment implements OnActivityResultListener {
44 
45     private static final String TAG = "SoundWorkSettings";
46     private static final int REQUEST_CODE = 200;
47     private static final String SELECTED_PREFERENCE_KEY = "selected_preference";
48 
49     private RingtonePreference mRequestPreference;
50 
51     @Override
getMetricsCategory()52     public int getMetricsCategory() {
53         return SettingsEnums.WORK_PROFILE_SOUNDS;
54     }
55 
56     @Override
onCreate(Bundle savedInstanceState)57     public void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59 
60         if (savedInstanceState != null) {
61             String selectedPreference = savedInstanceState.getString(
62                     SELECTED_PREFERENCE_KEY, /* defaultValue= */ null);
63             if (!TextUtils.isEmpty(selectedPreference)) {
64                 mRequestPreference = findPreference(selectedPreference);
65             }
66         }
67     }
68 
69     @Override
onPreferenceTreeClick(Preference preference)70     public boolean onPreferenceTreeClick(Preference preference) {
71         if (preference instanceof RingtonePreference) {
72             writePreferenceClickMetric(preference);
73             mRequestPreference = (RingtonePreference) preference;
74             mRequestPreference.onPrepareRingtonePickerIntent(mRequestPreference.getIntent());
75             getActivity().startActivityForResultAsUser(
76                     mRequestPreference.getIntent(),
77                     REQUEST_CODE,
78                     /* options= */ null,
79                     UserHandle.of(mRequestPreference.getUserId()));
80             return true;
81         }
82         return super.onPreferenceTreeClick(preference);
83     }
84 
85     @Override
onActivityResult(int requestCode, int resultCode, Intent data)86     public void onActivityResult(int requestCode, int resultCode, Intent data) {
87         if (mRequestPreference != null) {
88             mRequestPreference.onActivityResult(requestCode, resultCode, data);
89             mRequestPreference = null;
90         }
91     }
92 
93     @Override
onSaveInstanceState(Bundle outState)94     public void onSaveInstanceState(Bundle outState) {
95         super.onSaveInstanceState(outState);
96         if (mRequestPreference != null) {
97             outState.putString(SELECTED_PREFERENCE_KEY, mRequestPreference.getKey());
98         }
99     }
100 
101     @Override
createPreferenceControllers(Context context)102     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
103         return buildPreferenceControllers(context, /* fragment= */ this, getSettingsLifecycle());
104     }
105 
106     @Override
getLogTag()107     protected String getLogTag() {
108         return TAG;
109     }
110 
111     @Override
getPreferenceScreenResId()112     protected int getPreferenceScreenResId() {
113         return R.xml.sound_work_settings;
114     }
115 
buildPreferenceControllers(Context context, SoundWorkSettings fragment, Lifecycle lifecycle)116     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
117             SoundWorkSettings fragment, Lifecycle lifecycle) {
118         final List<AbstractPreferenceController> controllers = new ArrayList<>();
119         controllers.add(new SoundWorkSettingsController(context, fragment, lifecycle));
120         return controllers;
121     }
122 
isSupportWorkProfileSound(Context context)123     static final boolean isSupportWorkProfileSound(Context context) {
124         final AudioHelper audioHelper = new AudioHelper(context);
125         final boolean hasWorkProfile = audioHelper.getManagedProfileId(
126                 UserManager.get(context)) != UserHandle.USER_NULL;
127         final boolean shouldShowRingtoneSettings = !audioHelper.isSingleVolume();
128 
129         return hasWorkProfile && shouldShowRingtoneSettings;
130     }
131 
enableWorkSync()132     void enableWorkSync() {
133         final SoundWorkSettingsController soundWorkSettingsController =
134                 use(SoundWorkSettingsController.class);
135         if (soundWorkSettingsController != null) {
136             soundWorkSettingsController.enableWorkSync();
137         }
138     }
139 
140     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
141             new BaseSearchIndexProvider(R.xml.sound_work_settings) {
142                 @Override
143                 protected boolean isPageSearchEnabled(Context context) {
144                     return isSupportWorkProfileSound(context);
145                 }
146             };
147 }
148