1 /* 2 * Copyright (C) 2024 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.localepicker; 18 19 import android.content.Context; 20 import android.os.LocaleList; 21 22 import androidx.annotation.NonNull; 23 24 import com.android.internal.app.LocaleStore; 25 import com.android.settings.R; 26 import com.android.settings.core.BasePreferenceController; 27 import com.android.settings.flags.Flags; 28 29 import java.util.Arrays; 30 import java.util.List; 31 import java.util.Locale; 32 33 public class NewTermsOfAddressController extends BasePreferenceController { 34 NewTermsOfAddressController(@onNull Context context, @NonNull String preferenceKey)35 public NewTermsOfAddressController(@NonNull Context context, @NonNull String preferenceKey) { 36 super(context, preferenceKey); 37 } 38 39 @Override getAvailabilityStatus()40 public int getAvailabilityStatus() { 41 if (Flags.regionalPreferencesApiEnabled()) { 42 if (Flags.termsOfAddressEnabled()) { 43 return checkAvailabilityStatus(); 44 } 45 } 46 return CONDITIONALLY_UNAVAILABLE; 47 } 48 checkAvailabilityStatus()49 private int checkAvailabilityStatus() { 50 // If language is not available for system language, or if ToA does not apply to 51 // system language, we will hide it. 52 final Locale defaultLocale = Locale.getDefault(); 53 LocaleStore.LocaleInfo localeInfo = LocaleStore.getLocaleInfo(defaultLocale); 54 final List<String> supportedLanguageList = Arrays.asList( 55 mContext.getResources().getStringArray( 56 R.array.terms_of_address_supported_languages)); 57 final List<String> notSupportedLocaleList = Arrays.asList( 58 mContext.getResources().getStringArray( 59 R.array.terms_of_address_unsupported_locales)); 60 61 final Locale locale = localeInfo.getLocale().stripExtensions(); 62 final String language = locale.getLanguage(); 63 final String localeTag = locale.toLanguageTag(); 64 65 // Supported locales: 66 // 1. All French is supported except fr-CA. 67 // 2. QA language en-XA (LTR pseudo locale), ar_XB (RTL pseudo locale). 68 if ((supportedLanguageList.contains(language) 69 && !notSupportedLocaleList.contains(localeTag)) 70 || LocaleList.isPseudoLocale(locale)) { 71 return AVAILABLE; 72 } 73 74 return CONDITIONALLY_UNAVAILABLE; 75 } 76 } 77