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 */
16package com.android.dialer.app.settings;
17
18import android.content.res.Configuration;
19import android.os.Bundle;
20import android.preference.PreferenceActivity;
21import android.support.v7.app.ActionBar;
22import android.support.v7.app.AppCompatDelegate;
23import android.support.v7.widget.Toolbar;
24import android.view.MenuInflater;
25import android.view.View;
26import 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 */
32public class AppCompatPreferenceActivity extends PreferenceActivity {
33
34  private AppCompatDelegate mDelegate;
35
36  private boolean mIsSafeToCommitTransactions;
37
38  @Override
39  protected void onCreate(Bundle savedInstanceState) {
40    getDelegate().installViewFactory();
41    getDelegate().onCreate(savedInstanceState);
42    super.onCreate(savedInstanceState);
43    mIsSafeToCommitTransactions = true;
44  }
45
46  @Override
47  protected void onPostCreate(Bundle savedInstanceState) {
48    super.onPostCreate(savedInstanceState);
49    getDelegate().onPostCreate(savedInstanceState);
50  }
51
52  public ActionBar getSupportActionBar() {
53    return getDelegate().getSupportActionBar();
54  }
55
56  public void setSupportActionBar(Toolbar toolbar) {
57    getDelegate().setSupportActionBar(toolbar);
58  }
59
60  @Override
61  public MenuInflater getMenuInflater() {
62    return getDelegate().getMenuInflater();
63  }
64
65  @Override
66  public void setContentView(int layoutResID) {
67    getDelegate().setContentView(layoutResID);
68  }
69
70  @Override
71  public void setContentView(View view) {
72    getDelegate().setContentView(view);
73  }
74
75  @Override
76  public void setContentView(View view, ViewGroup.LayoutParams params) {
77    getDelegate().setContentView(view, params);
78  }
79
80  @Override
81  public void addContentView(View view, ViewGroup.LayoutParams params) {
82    getDelegate().addContentView(view, params);
83  }
84
85  @Override
86  protected void onPostResume() {
87    super.onPostResume();
88    getDelegate().onPostResume();
89  }
90
91  @Override
92  protected void onTitleChanged(CharSequence title, int color) {
93    super.onTitleChanged(title, color);
94    getDelegate().setTitle(title);
95  }
96
97  @Override
98  public void onConfigurationChanged(Configuration newConfig) {
99    super.onConfigurationChanged(newConfig);
100    getDelegate().onConfigurationChanged(newConfig);
101  }
102
103  @Override
104  protected void onStop() {
105    super.onStop();
106    getDelegate().onStop();
107  }
108
109  @Override
110  protected void onDestroy() {
111    super.onDestroy();
112    getDelegate().onDestroy();
113  }
114
115  @Override
116  public void invalidateOptionsMenu() {
117    getDelegate().invalidateOptionsMenu();
118  }
119
120  private AppCompatDelegate getDelegate() {
121    if (mDelegate == null) {
122      mDelegate = AppCompatDelegate.create(this, null);
123    }
124    return mDelegate;
125  }
126
127  @Override
128  protected void onStart() {
129    super.onStart();
130    mIsSafeToCommitTransactions = true;
131  }
132
133  @Override
134  protected void onResume() {
135    super.onResume();
136    mIsSafeToCommitTransactions = true;
137  }
138
139  @Override
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   */
152  public boolean isSafeToCommitTransactions() {
153    return mIsSafeToCommitTransactions;
154  }
155}
156