1 /*
2  * Copyright (C) 2018 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 static android.window.OnBackInvokedDispatcher.PRIORITY_DEFAULT;
20 
21 import android.app.FragmentTransaction;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.os.LocaleList;
25 import android.provider.Settings;
26 import android.util.Log;
27 import android.view.MenuItem;
28 import android.window.OnBackInvokedCallback;
29 
30 import androidx.core.view.ViewCompat;
31 
32 import com.android.internal.app.LocalePickerWithRegion;
33 import com.android.internal.app.LocaleStore;
34 import com.android.settings.R;
35 import com.android.settings.core.SettingsBaseActivity;
36 
37 /** A activity to show the locale picker page. */
38 public class LocalePickerWithRegionActivity extends SettingsBaseActivity
39         implements LocalePickerWithRegion.LocaleSelectedListener, MenuItem.OnActionExpandListener {
40     private static final String TAG = LocalePickerWithRegionActivity.class.getSimpleName();
41     private static final String PARENT_FRAGMENT_NAME = "localeListEditor";
42 
43     private LocalePickerWithRegion mSelector;
44 
45     private final OnBackInvokedCallback mOnBackInvokedCallback = () -> {
46         handleBackPressed();
47     };
48 
49     @Override
onCreate(Bundle savedInstanceState)50     public void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         getActionBar().setDisplayHomeAsUpEnabled(true);
53         setTitle(R.string.add_a_language);
54         LocaleList explicitLocales = null;
55         if (isDeviceDemoMode()) {
56             Bundle bundle = getIntent().getExtras();
57             explicitLocales = bundle == null
58                     ? null
59                     : bundle.getParcelable(Settings.EXTRA_EXPLICIT_LOCALES, LocaleList.class);
60             Log.i(TAG, "Has explicit locales : " + explicitLocales);
61         }
62         getOnBackInvokedDispatcher()
63                 .registerOnBackInvokedCallback(PRIORITY_DEFAULT, mOnBackInvokedCallback);
64         mSelector = LocalePickerWithRegion.createLanguagePicker(
65                 this,
66                 LocalePickerWithRegionActivity.this,
67                 false /* translate only */,
68                 explicitLocales,
69                 null /* appPackageName */,
70                 this);
71         getFragmentManager()
72                 .beginTransaction()
73                 .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
74                 .replace(R.id.content_frame, mSelector)
75                 .addToBackStack(PARENT_FRAGMENT_NAME)
76                 .commit();
77     }
78 
79     @Override
onDestroy()80     public void onDestroy() {
81         super.onDestroy();
82         getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(mOnBackInvokedCallback);
83     }
84 
85     @Override
onOptionsItemSelected(MenuItem item)86     public boolean onOptionsItemSelected(MenuItem item) {
87         if (item.getItemId() == android.R.id.home) {
88             handleBackPressed();
89             return true;
90         }
91         return super.onOptionsItemSelected(item);
92     }
93 
94     @Override
onLocaleSelected(LocaleStore.LocaleInfo locale)95     public void onLocaleSelected(LocaleStore.LocaleInfo locale) {
96         final Intent intent = new Intent();
97         intent.putExtra(LocaleListEditor.INTENT_LOCALE_KEY, locale);
98         setResult(RESULT_OK, intent);
99         finish();
100     }
101 
handleBackPressed()102     private void handleBackPressed() {
103         if (getFragmentManager().getBackStackEntryCount() > 1) {
104             super.onBackPressed();
105         } else {
106             setResult(RESULT_CANCELED);
107             finish();
108         }
109     }
110 
isDeviceDemoMode()111     private boolean isDeviceDemoMode() {
112         return Settings.Global.getInt(
113                 getContentResolver(), Settings.Global.DEVICE_DEMO_MODE, 0) == 1;
114     }
115 
116     @Override
onMenuItemActionExpand(MenuItem item)117     public boolean onMenuItemActionExpand(MenuItem item) {
118         // To prevent a large space on tool bar.
119         mAppBarLayout.setExpanded(false /*expanded*/, false /*animate*/);
120         // To prevent user can expand the collpasing tool bar view.
121         ViewCompat.setNestedScrollingEnabled(mSelector.getListView(), false);
122         return true;
123     }
124 
125     @Override
onMenuItemActionCollapse(MenuItem item)126     public boolean onMenuItemActionCollapse(MenuItem item) {
127         mAppBarLayout.setExpanded(false /*expanded*/, false /*animate*/);
128         ViewCompat.setNestedScrollingEnabled(mSelector.getListView(), true);
129         return true;
130     }
131 }
132 
133