Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko; |
michael@0 | 6 | |
michael@0 | 7 | import android.app.Activity; |
michael@0 | 8 | import android.view.View; |
michael@0 | 9 | import android.view.ViewGroup; |
michael@0 | 10 | import android.widget.EditText; |
michael@0 | 11 | import android.widget.TextSwitcher; |
michael@0 | 12 | import android.widget.TextView; |
michael@0 | 13 | |
michael@0 | 14 | public class FennecNativeElement implements Element { |
michael@0 | 15 | private final Activity mActivity; |
michael@0 | 16 | private Integer mId; |
michael@0 | 17 | |
michael@0 | 18 | public FennecNativeElement(Integer id, Activity activity) { |
michael@0 | 19 | mId = id; |
michael@0 | 20 | mActivity = activity; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | public Integer getId() { |
michael@0 | 24 | return mId; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | private boolean mClickSuccess; |
michael@0 | 28 | |
michael@0 | 29 | public boolean click() { |
michael@0 | 30 | mClickSuccess = false; |
michael@0 | 31 | RobocopUtils.runOnUiThreadSync(mActivity, |
michael@0 | 32 | new Runnable() { |
michael@0 | 33 | public void run() { |
michael@0 | 34 | View view = (View)mActivity.findViewById(mId); |
michael@0 | 35 | if (view != null) { |
michael@0 | 36 | if (view.performClick()) { |
michael@0 | 37 | mClickSuccess = true; |
michael@0 | 38 | } else { |
michael@0 | 39 | FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN, |
michael@0 | 40 | "Robocop called click on an element with no listener"); |
michael@0 | 41 | } |
michael@0 | 42 | } else { |
michael@0 | 43 | FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, |
michael@0 | 44 | "click: unable to find view "+mId); |
michael@0 | 45 | } |
michael@0 | 46 | } |
michael@0 | 47 | }); |
michael@0 | 48 | return mClickSuccess; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | private Object mText; |
michael@0 | 52 | |
michael@0 | 53 | public String getText() { |
michael@0 | 54 | mText = null; |
michael@0 | 55 | RobocopUtils.runOnUiThreadSync(mActivity, |
michael@0 | 56 | new Runnable() { |
michael@0 | 57 | public void run() { |
michael@0 | 58 | View v = mActivity.findViewById(mId); |
michael@0 | 59 | if (v instanceof EditText) { |
michael@0 | 60 | EditText et = (EditText)v; |
michael@0 | 61 | mText = et.getEditableText(); |
michael@0 | 62 | } else if (v instanceof TextSwitcher) { |
michael@0 | 63 | TextSwitcher ts = (TextSwitcher)v; |
michael@0 | 64 | ts.getNextView(); |
michael@0 | 65 | mText = ((TextView)ts.getCurrentView()).getText(); |
michael@0 | 66 | } else if (v instanceof ViewGroup) { |
michael@0 | 67 | ViewGroup vg = (ViewGroup)v; |
michael@0 | 68 | for (int i = 0; i < vg.getChildCount(); i++) { |
michael@0 | 69 | if (vg.getChildAt(i) instanceof TextView) { |
michael@0 | 70 | mText = ((TextView)vg.getChildAt(i)).getText(); |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | } else if (v instanceof TextView) { |
michael@0 | 74 | mText = ((TextView)v).getText(); |
michael@0 | 75 | } else if (v == null) { |
michael@0 | 76 | FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, |
michael@0 | 77 | "getText: unable to find view "+mId); |
michael@0 | 78 | } else { |
michael@0 | 79 | FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, |
michael@0 | 80 | "getText: unhandled type for view "+mId); |
michael@0 | 81 | } |
michael@0 | 82 | } // end of run() method definition |
michael@0 | 83 | } // end of anonymous Runnable object instantiation |
michael@0 | 84 | ); |
michael@0 | 85 | if (mText == null) { |
michael@0 | 86 | FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN, |
michael@0 | 87 | "getText: Text is null for view "+mId); |
michael@0 | 88 | return null; |
michael@0 | 89 | } |
michael@0 | 90 | return mText.toString(); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | private boolean mDisplayed; |
michael@0 | 94 | |
michael@0 | 95 | public boolean isDisplayed() { |
michael@0 | 96 | mDisplayed = false; |
michael@0 | 97 | RobocopUtils.runOnUiThreadSync(mActivity, |
michael@0 | 98 | new Runnable() { |
michael@0 | 99 | public void run() { |
michael@0 | 100 | View view = (View)mActivity.findViewById(mId); |
michael@0 | 101 | if (view != null) { |
michael@0 | 102 | mDisplayed = true; |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | }); |
michael@0 | 106 | return mDisplayed; |
michael@0 | 107 | } |
michael@0 | 108 | } |