1 /*
2  * Copyright (C) 2023 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.localemanager.atom.overridelocaleconfig;
18 
19 import android.app.Activity;
20 import android.app.LocaleConfig;
21 import android.app.LocaleManager;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.os.LocaleList;
25 
26 /**
27  * Activity which tries to call setOverrideLocaleConfig.
28  */
29 public class ActivityForSettingOverrideLocaleConfig extends Activity {
30     private static final String OVERRIDE_LOCALE_CONFIG_KEY = "override_locale_config";
31     private static final String EXTRA_REMOVE_OVERRIDE = "remove_override";
32 
33     @Override
onCreate(Bundle icicle)34     public void onCreate(Bundle icicle) {
35         super.onCreate(icicle);
36         Intent intent = getIntent();
37         if (intent != null && intent.hasExtra(OVERRIDE_LOCALE_CONFIG_KEY)) {
38             String extra = intent.getStringExtra(OVERRIDE_LOCALE_CONFIG_KEY);
39             callSetOverrideLocaleConfig(extra);
40         }
41 
42         finish();
43     }
44 
callSetOverrideLocaleConfig(String extra)45     public void callSetOverrideLocaleConfig(String extra) {
46         LocaleConfig localeConfig = extra.equals(EXTRA_REMOVE_OVERRIDE) ? null : new LocaleConfig(
47                 LocaleList.forLanguageTags(extra));
48         LocaleManager localeManager = this.getSystemService(LocaleManager.class);
49         localeManager.setOverrideLocaleConfig(localeConfig);
50     }
51 }
52