/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.app;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import com.android.internal.app.ActionBarImpl;
import com.android.internal.policy.PolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.util.TypedValue;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;
import java.lang.ref.WeakReference;
/**
* Base class for Dialogs.
*
* <p>Note: Activities provide a facility to manage the creation, saving and
* restoring of dialogs. See {@link Activity#onCreateDialog(int)},
* {@link Activity#onPrepareDialog(int, Dialog)},
* {@link Activity#showDialog(int)}, and {@link Activity#dismissDialog(int)}. If
* these methods are used, {@link #getOwnerActivity()} will return the Activity
* that managed this dialog.
*
* <p>Often you will want to have a Dialog display on top of the current
* input method, because there is no reason for it to accept text. You can
* do this by setting the {@link WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM
* WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM} window flag (assuming
* your Dialog takes input focus, as it the default) with the following code:
*
* <pre>
* getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
* WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);</pre>
*
* <div class="special reference">
* <h3>Developer Guides</h3>
* <p>For more information about creating dialogs, read the
* <a href="{@docRoot}guide/topics/ui/dialogs.html">Dialogs</a> developer guide.</p>
* </div>
*/
public class Dialog implements DialogInterface, Window.Callback,
KeyEvent.Callback, OnCreateContextMenuListener {
private static final String TAG = "Dialog";
private Activity mOwnerActivity;
final Context mContext;
final WindowManager mWindowManager;
Window mWindow;
View mDecor;
private ActionBarImpl mActionBar;
/**
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected boolean mCancelable = true;
private String mCancelAndDismissTaken;
private Message mCancelMessage;
private Message mDismissMessage;
private Message mShowMessage;
private OnKeyListener mOnKeyListener;
private boolean mCreated = false;
private boolean mShowing = false;
private boolean mCanceled = false;
private final Handler mHandler = new Handler();
private static final int DISMISS = 0x43;
private static final int CANCEL = 0x44;
private static final int SHOW = 0x45;
private Handler mListenersHandler;
private ActionMode mActionMode;
private final Runnable mDismissAction = new Runnable() {
public void run() {
dismissDialog();
}
};
/**
* Create a Dialog window that uses the default dialog frame style.
*
* @param context The Context the Dialog is to run it. In particular, it
* uses the window manager and theme in this context to
* present its UI.
*/
public Dialog(Context context) {
this(context, 0, true);
}
/**
* Create a Dialog window that uses a custom dialog style.
*
* @param context The Context in which the Dialog should run. In particular, it
* uses the window manager and theme from this context to
* present its UI.
* @param theme A style resource describing the theme to use for the
* window. See <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">Style
* and Theme Resources</a> for more information about defining and using
* styles. This theme is applied on top of the current theme in
* <var>context</var>. If 0, the default dialog theme will be used.
*/
public Dialog(Context context, int theme) {
this(context, theme, true);
}
Dialog(Context context, int theme, boolean createContextThemeWrapper) {
if (createContextThemeWrapper) {
if (theme == 0) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(com.android.internal.R.attr.dialogTheme,
outValue, true);
theme = outValue.resourceId;
}
mContext = new ContextThemeWrapper(context, theme);
} else {
mContext = context;
}
mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Window w = PolicyManager.makeNewWindow(mContext);
mWindow = w;
w.setCallback(this);
w.setWindowManager(mWindowManager, null, null);
w.setGravity(Gravity.CENTER);
mListenersHandler = new ListenersHandler(this);
}
/**
* @deprecated
* @hide
*/
@Deprecated
protected Dialog(Context context, boolean cancelable,
Message cancelCallback) {
this(context);
mCancelable = cancelable;
mCancelMessage = cancelCallback;
}
protected Dialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
this(context);
mCancelable = cancelable;
setOnCancelListener(cancelListener);
}
/**
* Retrieve the Context this Dialog is running in.
*
* @return Context The Context used by the Dialog.
*/
public final Context getContext() {
return mContext;
}
/**
* Retrieve the {@link ActionBar} attached to this dialog, if present.
*
* @return The ActionBar attached to the dialog or null if no ActionBar is present.
*/
public ActionBar getActionBar() {
return mActionBar;
}
/**
* Sets the Activity that owns this dialog. An example use: This Dialog will
* use the suggested volume control stream of the Activity.
*
* @param activity The Activity that owns this dialog.
*/
public final void setOwnerActivity(Activity activity) {
mOwnerActivity = activity;
getWindow().setVolumeControlStream(mOwnerActivity.getVolumeControlStream());
}
/**
* Returns the Activity that owns this Dialog. For example, if
* {@link Activity#showDialog(int)} is used to show this Dia
评论1
最新资源