• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.app.FragmentTransaction;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.view.MenuItem;
23 
24 import com.android.internal.app.LocalePickerWithRegion;
25 import com.android.internal.app.LocaleStore;
26 import com.android.settings.R;
27 import com.android.settings.core.SettingsBaseActivity;
28 
29 /** A activity to show the locale picker page. */
30 public class LocalePickerWithRegionActivity extends SettingsBaseActivity
31         implements LocalePickerWithRegion.LocaleSelectedListener {
32 
33     private static final String PARENT_FRAGMENT_NAME = "localeListEditor";
34 
35     @Override
onCreate(Bundle savedInstanceState)36     public void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38         getActionBar().setDisplayHomeAsUpEnabled(true);
39         setTitle(R.string.add_a_language);
40 
41         final LocalePickerWithRegion selector = LocalePickerWithRegion.createLanguagePicker(
42                 this, LocalePickerWithRegionActivity.this, false /* translate only */);
43         getFragmentManager()
44                 .beginTransaction()
45                 .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
46                 .replace(R.id.content_frame, selector)
47                 .addToBackStack(PARENT_FRAGMENT_NAME)
48                 .commit();
49     }
50 
51     @Override
onOptionsItemSelected(MenuItem item)52     public boolean onOptionsItemSelected(MenuItem item) {
53         if (item.getItemId() == android.R.id.home) {
54             handleBackPressed();
55             return true;
56         }
57         return super.onOptionsItemSelected(item);
58     }
59 
60     @Override
onLocaleSelected(LocaleStore.LocaleInfo locale)61     public void onLocaleSelected(LocaleStore.LocaleInfo locale) {
62         final Intent intent = new Intent();
63         intent.putExtra(LocaleListEditor.INTENT_LOCALE_KEY, locale);
64         setResult(RESULT_OK, intent);
65         finish();
66     }
67 
68     @Override
onBackPressed()69     public void onBackPressed() {
70         handleBackPressed();
71     }
72 
handleBackPressed()73     private void handleBackPressed() {
74         if (getFragmentManager().getBackStackEntryCount() > 1) {
75             super.onBackPressed();
76         } else {
77             setResult(RESULT_CANCELED);
78             finish();
79         }
80     }
81 }
82 
83