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