1 /*
2  * Copyright (C) 2014 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.example.android.supportv7.app;
18 
19 import android.content.res.Configuration;
20 import android.os.Bundle;
21 import android.preference.PreferenceActivity;
22 import android.view.MenuInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 
26 import androidx.annotation.LayoutRes;
27 import androidx.annotation.Nullable;
28 import androidx.appcompat.app.ActionBar;
29 import androidx.appcompat.app.AppCompatDelegate;
30 import androidx.appcompat.widget.Toolbar;
31 
32 /**
33  * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls
34  * to be used with AppCompat.
35  *
36  * This technique can be used with an {@link android.app.Activity} class, not just
37  * {@link android.preference.PreferenceActivity}.
38  */
39 public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
40 
41     private AppCompatDelegate mDelegate;
42 
43     @Override
onCreate(Bundle savedInstanceState)44     protected void onCreate(Bundle savedInstanceState) {
45         getDelegate().installViewFactory();
46         getDelegate().onCreate(savedInstanceState);
47         super.onCreate(savedInstanceState);
48     }
49 
50     @Override
onPostCreate(Bundle savedInstanceState)51     protected void onPostCreate(Bundle savedInstanceState) {
52         super.onPostCreate(savedInstanceState);
53         getDelegate().onPostCreate(savedInstanceState);
54     }
55 
getSupportActionBar()56     public ActionBar getSupportActionBar() {
57         return getDelegate().getSupportActionBar();
58     }
59 
setSupportActionBar(@ullable Toolbar toolbar)60     public void setSupportActionBar(@Nullable Toolbar toolbar) {
61         getDelegate().setSupportActionBar(toolbar);
62     }
63 
64     @Override
getMenuInflater()65     public MenuInflater getMenuInflater() {
66         return getDelegate().getMenuInflater();
67     }
68 
69     @Override
setContentView(@ayoutRes int layoutResID)70     public void setContentView(@LayoutRes int layoutResID) {
71         getDelegate().setContentView(layoutResID);
72     }
73 
74     @Override
setContentView(View view)75     public void setContentView(View view) {
76         getDelegate().setContentView(view);
77     }
78 
79     @Override
setContentView(View view, ViewGroup.LayoutParams params)80     public void setContentView(View view, ViewGroup.LayoutParams params) {
81         getDelegate().setContentView(view, params);
82     }
83 
84     @Override
addContentView(View view, ViewGroup.LayoutParams params)85     public void addContentView(View view, ViewGroup.LayoutParams params) {
86         getDelegate().addContentView(view, params);
87     }
88 
89     @Override
onPostResume()90     protected void onPostResume() {
91         super.onPostResume();
92         getDelegate().onPostResume();
93     }
94 
95     @Override
onTitleChanged(CharSequence title, int color)96     protected void onTitleChanged(CharSequence title, int color) {
97         super.onTitleChanged(title, color);
98         getDelegate().setTitle(title);
99     }
100 
101     @Override
onConfigurationChanged(Configuration newConfig)102     public void onConfigurationChanged(Configuration newConfig) {
103         super.onConfigurationChanged(newConfig);
104         getDelegate().onConfigurationChanged(newConfig);
105     }
106 
107     @Override
onStop()108     protected void onStop() {
109         super.onStop();
110         getDelegate().onStop();
111     }
112 
113     @Override
onDestroy()114     protected void onDestroy() {
115         super.onDestroy();
116         getDelegate().onDestroy();
117     }
118 
119     @Override
invalidateOptionsMenu()120     public void invalidateOptionsMenu() {
121         getDelegate().invalidateOptionsMenu();
122     }
123 
getDelegate()124     private AppCompatDelegate getDelegate() {
125         if (mDelegate == null) {
126             mDelegate = AppCompatDelegate.create(this, null);
127         }
128         return mDelegate;
129     }
130 }