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 40 final LocalePickerWithRegion selector = LocalePickerWithRegion.createLanguagePicker( 41 this, LocalePickerWithRegionActivity.this, false /* translate only */); 42 getFragmentManager() 43 .beginTransaction() 44 .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) 45 .replace(R.id.content_frame, selector) 46 .addToBackStack(PARENT_FRAGMENT_NAME) 47 .commit(); 48 } 49 50 @Override onOptionsItemSelected(MenuItem item)51 public boolean onOptionsItemSelected(MenuItem item) { 52 if (item.getItemId() == android.R.id.home) { 53 handleBackPressed(); 54 return true; 55 } 56 return super.onOptionsItemSelected(item); 57 } 58 59 @Override onLocaleSelected(LocaleStore.LocaleInfo locale)60 public void onLocaleSelected(LocaleStore.LocaleInfo locale) { 61 final Intent intent = new Intent(); 62 intent.putExtra(LocaleListEditor.INTENT_LOCALE_KEY, locale); 63 setResult(RESULT_OK, intent); 64 finish(); 65 } 66 67 @Override onBackPressed()68 public void onBackPressed() { 69 handleBackPressed(); 70 } 71 handleBackPressed()72 private void handleBackPressed() { 73 if (getFragmentManager().getBackStackEntryCount() > 1) { 74 super.onBackPressed(); 75 } else { 76 setResult(RESULT_CANCELED); 77 finish(); 78 } 79 } 80 } 81 82