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