1 /* 2 * Copyright (C) 2015 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 package com.android.dialer.app.settings; 17 18 import android.content.res.Configuration; 19 import android.os.Bundle; 20 import android.preference.PreferenceActivity; 21 import android.support.v7.app.AppCompatDelegate; 22 import android.support.v7.widget.Toolbar; 23 import android.view.MenuInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 27 /** 28 * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls 29 * to be used with AppCompat. 30 */ 31 public class AppCompatPreferenceActivity extends PreferenceActivity { 32 33 private AppCompatDelegate delegate; 34 35 private boolean isSafeToCommitTransactions; 36 37 @Override onCreate(Bundle savedInstanceState)38 protected void onCreate(Bundle savedInstanceState) { 39 getDelegate().installViewFactory(); 40 getDelegate().onCreate(savedInstanceState); 41 super.onCreate(savedInstanceState); 42 isSafeToCommitTransactions = true; 43 } 44 45 @Override onPostCreate(Bundle savedInstanceState)46 protected void onPostCreate(Bundle savedInstanceState) { 47 super.onPostCreate(savedInstanceState); 48 getDelegate().onPostCreate(savedInstanceState); 49 } 50 setSupportActionBar(Toolbar toolbar)51 public void setSupportActionBar(Toolbar toolbar) { 52 getDelegate().setSupportActionBar(toolbar); 53 } 54 55 @Override getMenuInflater()56 public MenuInflater getMenuInflater() { 57 return getDelegate().getMenuInflater(); 58 } 59 60 @Override setContentView(int layoutResID)61 public void setContentView(int layoutResID) { 62 getDelegate().setContentView(layoutResID); 63 } 64 65 @Override setContentView(View view)66 public void setContentView(View view) { 67 getDelegate().setContentView(view); 68 } 69 70 @Override setContentView(View view, ViewGroup.LayoutParams params)71 public void setContentView(View view, ViewGroup.LayoutParams params) { 72 getDelegate().setContentView(view, params); 73 } 74 75 @Override addContentView(View view, ViewGroup.LayoutParams params)76 public void addContentView(View view, ViewGroup.LayoutParams params) { 77 getDelegate().addContentView(view, params); 78 } 79 80 @Override onPostResume()81 protected void onPostResume() { 82 super.onPostResume(); 83 getDelegate().onPostResume(); 84 } 85 86 @Override onTitleChanged(CharSequence title, int color)87 protected void onTitleChanged(CharSequence title, int color) { 88 super.onTitleChanged(title, color); 89 getDelegate().setTitle(title); 90 } 91 92 @Override onConfigurationChanged(Configuration newConfig)93 public void onConfigurationChanged(Configuration newConfig) { 94 super.onConfigurationChanged(newConfig); 95 getDelegate().onConfigurationChanged(newConfig); 96 } 97 98 @Override onStop()99 protected void onStop() { 100 super.onStop(); 101 getDelegate().onStop(); 102 } 103 104 @Override onDestroy()105 protected void onDestroy() { 106 super.onDestroy(); 107 getDelegate().onDestroy(); 108 } 109 110 @Override invalidateOptionsMenu()111 public void invalidateOptionsMenu() { 112 getDelegate().invalidateOptionsMenu(); 113 } 114 getDelegate()115 private AppCompatDelegate getDelegate() { 116 if (delegate == null) { 117 delegate = AppCompatDelegate.create(this, null); 118 } 119 return delegate; 120 } 121 122 @Override onStart()123 protected void onStart() { 124 super.onStart(); 125 isSafeToCommitTransactions = true; 126 } 127 128 @Override onResume()129 protected void onResume() { 130 super.onResume(); 131 isSafeToCommitTransactions = true; 132 } 133 134 @Override onSaveInstanceState(Bundle outState)135 protected void onSaveInstanceState(Bundle outState) { 136 super.onSaveInstanceState(outState); 137 isSafeToCommitTransactions = false; 138 } 139 140 /** 141 * Returns true if it is safe to commit {@link FragmentTransaction}s at this time, based on 142 * whether {@link Activity#onSaveInstanceState} has been called or not. 143 * 144 * <p>Make sure that the current activity calls into {@link super.onSaveInstanceState(Bundle 145 * outState)} (if that method is overridden), so the flag is properly set. 146 */ isSafeToCommitTransactions()147 public boolean isSafeToCommitTransactions() { 148 return isSafeToCommitTransactions; 149 } 150 } 151