• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 android.app;
18 
19 import android.annotation.SystemService;
20 import android.content.Context;
21 import android.content.res.Configuration;
22 import android.os.RemoteException;
23 
24 import java.util.Arrays;
25 import java.util.HashSet;
26 import java.util.Set;
27 
28 /**
29  * This class allow applications to control granular grammatical inflection settings (such as
30  * per-app grammatical gender).
31  */
32 @SystemService(Context.GRAMMATICAL_INFLECTION_SERVICE)
33 public class GrammaticalInflectionManager {
34     private static final Set<Integer> VALID_GENDER_VALUES = new HashSet<>(Arrays.asList(
35             Configuration.GRAMMATICAL_GENDER_NOT_SPECIFIED,
36             Configuration.GRAMMATICAL_GENDER_NEUTRAL,
37             Configuration.GRAMMATICAL_GENDER_FEMININE,
38             Configuration.GRAMMATICAL_GENDER_MASCULINE));
39 
40     private final Context mContext;
41     private final IGrammaticalInflectionManager mService;
42 
43     /** @hide Instantiated by ContextImpl */
GrammaticalInflectionManager(Context context, IGrammaticalInflectionManager service)44     public GrammaticalInflectionManager(Context context, IGrammaticalInflectionManager service) {
45         mContext = context;
46         mService = service;
47     }
48 
49     /**
50      * Returns the current grammatical gender for the calling app. A new value can be requested via
51      * {@link #setRequestedApplicationGrammaticalGender(int)} and will be updated with a new
52      * configuration change. The method always returns the value received with the last received
53      * configuration change.
54      *
55      * @return the value of grammatical gender
56      * @see Configuration#getGrammaticalGender
57      */
58     @Configuration.GrammaticalGender
getApplicationGrammaticalGender()59     public int getApplicationGrammaticalGender() {
60         return mContext.getApplicationContext()
61                 .getResources()
62                 .getConfiguration()
63                 .getGrammaticalGender();
64     }
65 
66     /**
67      * Sets the current grammatical gender for the calling app (keyed by package name and user ID
68      * retrieved from the calling pid).
69      *
70      * <p><b>Note:</b> Changes to app grammatical gender will result in a configuration change (and
71      * potentially an Activity re-creation) being applied to the specified application. For more
72      * information, see the <a
73      * href="https://developer.android.com/guide/topics/resources/runtime-changes">section on
74      * handling configuration changes</a>. The set grammatical gender are persisted across
75      * application restarts; they are backed up if the user has enabled Backup & Restore.`
76      *
77      * @param grammaticalGender the terms of address the user preferred in an application.
78      * @see Configuration#getGrammaticalGender
79      */
setRequestedApplicationGrammaticalGender( @onfiguration.GrammaticalGender int grammaticalGender)80     public void setRequestedApplicationGrammaticalGender(
81             @Configuration.GrammaticalGender int grammaticalGender) {
82         if (!VALID_GENDER_VALUES.contains(grammaticalGender)) {
83             throw new IllegalArgumentException("Unknown grammatical gender");
84         }
85 
86         try {
87             mService.setRequestedApplicationGrammaticalGender(
88                     mContext.getPackageName(), mContext.getUserId(), grammaticalGender);
89         } catch (RemoteException e) {
90             throw e.rethrowFromSystemServer();
91         }
92     }
93 }
94