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