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 17package android.support.v7.app; 18 19import android.app.Dialog; 20import android.content.Context; 21import android.os.Bundle; 22import android.support.annotation.IdRes; 23import android.support.annotation.LayoutRes; 24import android.support.annotation.Nullable; 25import android.support.v7.appcompat.R; 26import android.support.v7.view.ActionMode; 27import android.util.TypedValue; 28import android.view.View; 29import android.view.ViewGroup; 30 31/** 32 * Base class for AppCompat themed {@link android.app.Dialog}s. 33 */ 34public class AppCompatDialog extends Dialog implements AppCompatCallback { 35 36 private AppCompatDelegate mDelegate; 37 38 public AppCompatDialog(Context context) { 39 this(context, 0); 40 } 41 42 public AppCompatDialog(Context context, int theme) { 43 super(context, getThemeResId(context, theme)); 44 45 // This is a bit weird, but Dialog's are typically created and setup before being shown, 46 // which means that we can't rely on onCreate() being called before a content view is set. 47 // To workaround this, we call onCreate(null) in the ctor, and then again as usual in 48 // onCreate(). 49 getDelegate().onCreate(null); 50 51 // Apply AppCompat's DayNight resources if needed 52 getDelegate().applyDayNight(); 53 } 54 55 protected AppCompatDialog(Context context, boolean cancelable, 56 OnCancelListener cancelListener) { 57 super(context, cancelable, cancelListener); 58 } 59 60 @Override 61 protected void onCreate(Bundle savedInstanceState) { 62 getDelegate().installViewFactory(); 63 super.onCreate(savedInstanceState); 64 getDelegate().onCreate(savedInstanceState); 65 } 66 67 /** 68 * Support library version of {@link android.app.Dialog#getActionBar}. 69 * 70 * <p>Retrieve a reference to this dialog's ActionBar. 71 * 72 * @return The Dialog's ActionBar, or null if it does not have one. 73 */ 74 public ActionBar getSupportActionBar() { 75 return getDelegate().getSupportActionBar(); 76 } 77 78 @Override 79 public void setContentView(@LayoutRes int layoutResID) { 80 getDelegate().setContentView(layoutResID); 81 } 82 83 @Override 84 public void setContentView(View view) { 85 getDelegate().setContentView(view); 86 } 87 88 @Override 89 public void setContentView(View view, ViewGroup.LayoutParams params) { 90 getDelegate().setContentView(view, params); 91 } 92 93 @Nullable 94 @Override 95 public View findViewById(@IdRes int id) { 96 return getDelegate().findViewById(id); 97 } 98 99 @Override 100 public void setTitle(CharSequence title) { 101 super.setTitle(title); 102 getDelegate().setTitle(title); 103 } 104 105 @Override 106 public void setTitle(int titleId) { 107 super.setTitle(titleId); 108 getDelegate().setTitle(getContext().getString(titleId)); 109 } 110 111 @Override 112 public void addContentView(View view, ViewGroup.LayoutParams params) { 113 getDelegate().addContentView(view, params); 114 } 115 116 @Override 117 protected void onStop() { 118 super.onStop(); 119 getDelegate().onStop(); 120 } 121 122 /** 123 * Enable extended support library window features. 124 * <p> 125 * This is a convenience for calling 126 * {@link android.view.Window#requestFeature getWindow().requestFeature()}. 127 * </p> 128 * 129 * @param featureId The desired feature as defined in {@link android.view.Window} or 130 * {@link android.support.v4.view.WindowCompat}. 131 * @return Returns true if the requested feature is supported and now enabled. 132 * 133 * @see android.app.Dialog#requestWindowFeature 134 * @see android.view.Window#requestFeature 135 */ 136 public boolean supportRequestWindowFeature(int featureId) { 137 return getDelegate().requestWindowFeature(featureId); 138 } 139 140 /** 141 * @hide 142 */ 143 public void invalidateOptionsMenu() { 144 getDelegate().invalidateOptionsMenu(); 145 } 146 147 /** 148 * @return The {@link AppCompatDelegate} being used by this Dialog. 149 */ 150 public AppCompatDelegate getDelegate() { 151 if (mDelegate == null) { 152 mDelegate = AppCompatDelegate.create(this, this); 153 } 154 return mDelegate; 155 } 156 157 private static int getThemeResId(Context context, int themeId) { 158 if (themeId == 0) { 159 // If the provided theme is 0, then retrieve the dialogTheme from our theme 160 TypedValue outValue = new TypedValue(); 161 context.getTheme().resolveAttribute(R.attr.dialogTheme, outValue, true); 162 themeId = outValue.resourceId; 163 } 164 return themeId; 165 } 166 167 @Override 168 public void onSupportActionModeStarted(ActionMode mode) { 169 } 170 171 @Override 172 public void onSupportActionModeFinished(ActionMode mode) { 173 } 174 175 @Nullable 176 @Override 177 public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) { 178 return null; 179 } 180} 181