popup windows are slightly different than dialogs, so we have a separate path which polls for them to set up in RecordTest - Android User Interface

Android examples for User Interface:PopupWindow

Description

popup windows are slightly different than dialogs, so we have a separate path which polls for them to set up in RecordTest

Demo Code


import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.Window.Callback;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import com.androidApp.Utility.Constants.Classes;
import com.androidApp.Utility.Constants.Fields;

public class Main{
    /**/*from w  w  w .  j av a2 s  .  c  om*/
     * popup windows are slightly different than dialogs, so we have a separate path which polls for them
     * to set up in RecordTest
     * @param activity
     * @return Object: because it can be a window or a popup window, and they don't inherit from each other
     */
    public static WindowAndView findPopupWindow(Activity activity) {
        try {
            View[] views = ViewExtractor.getWindowDecorViews();
            if (views != null) {
                int numDecorViews = views.length;

                // iterate through the set of decor windows.  The dialog may already have been presented.
                for (int iView = 0; iView < numDecorViews; iView++) {
                    View v = views[iView];
                    if (isDialogOrPopup(activity, v)) {
                        // dialogs are handled in the other case.
                        Dialog dialog = getDialog(v);
                        if (dialog == null) {

                            // Toasts and other weird things get picked up as popup windows.
                            try {
                                Object window = ReflectionUtils
                                        .getFieldValue(v, v.getClass(),
                                                Constants.Classes.THIS);
                                WindowAndView windowAndView = new WindowAndView(
                                        window, v);
                                return windowAndView;
                            } catch (Exception ex) {
                                return null;
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    /** 
     * is this view in the same context as the activity, but has a different window? Then it is contained in a popup window
     * and may or may not be a dialog.
     * @param a the activity
     * @param v a PhoneWindow$DecorView or PopupContainerView or something like that
     * @return true, or maybe false.  probably true, though
     */
    public static boolean isDialogOrPopup(Activity a, View v) {
        if (v != null) {
            Context viewContext = v.getContext();
            // dialogs use a context theme wrapper, not a context, so we have to extract he context from the theme wrapper's
            // base context. Dialogs can also have nested Context Theme wrappers, so we have to loop to get the "true"
            // base context.
            while (viewContext instanceof ContextThemeWrapper) {
                ContextThemeWrapper ctw = (ContextThemeWrapper) viewContext;
                viewContext = ctw.getBaseContext();
            }
            Context activityContext = a;
            Context activityBaseContext = a.getBaseContext();
            return (activityContext.equals(viewContext) || activityBaseContext
                    .equals(viewContext))
                    && (v != a.getWindow().getDecorView());
        } else {
            return false;
        }
    }
    /**
     * given a PhoneWindow$DecorView, see if it has a WindowCallback which derives from Dialog.  If so, then return that
     * dialog, otherwise return null.
     * @param phoneWindowDecorView PhoneWindow$DecorView
     * @return Dialog or null;
     */
    public static Dialog getDialog(View phoneWindowDecorView) {
        try {
            Class<? extends View> phoneDecorViewClass = (Class<? extends View>) Class
                    .forName(Constants.Classes.PHONE_DECOR_VIEW);
            if (phoneWindowDecorView.getClass() == phoneDecorViewClass) {
                Window phoneWindow = (Window) ReflectionUtils
                        .getFieldValue(phoneWindowDecorView,
                                phoneDecorViewClass, Constants.Classes.THIS);
                Window.Callback callback = phoneWindow.getCallback();
                if (callback instanceof Dialog) {
                    Dialog dialog = (Dialog) callback;
                    return dialog;
                }
            }
        } catch (Exception ex) {
        }
        return null;
    }
}

Related Tutorials