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