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