1/*
2 * Copyright (C) 2011 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 com.android.chimpchat.core;
18
19import com.android.chimpchat.ChimpManager;
20
21import com.google.common.collect.Lists;
22
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.Collection;
26import java.util.Collections;
27import java.util.List;
28import java.util.logging.Level;
29import java.util.logging.Logger;
30
31/* A class for querying a view object by its id */
32public class ChimpView implements IChimpView {
33    private static final Logger LOG = Logger.getLogger(ChimpView.class.getName());
34    public static final String ACCESSIBILITY_IDS = "accessibilityids";
35    public static final String VIEW_ID = "viewid";
36
37    private String viewType;
38    private List<String> ids;
39    private ChimpManager manager;
40
41    public ChimpView(String viewType, List<String> ids) {
42        this.viewType = viewType;
43        this.ids = ids;
44    }
45
46    public void setManager(ChimpManager manager) {
47        this.manager = manager;
48    }
49
50    private String queryView(String query) {
51        try {
52            return manager.queryView(viewType, ids, query);
53        } catch(IOException e) {
54            LOG.log(Level.SEVERE, "Error querying view: " + e.getMessage());
55            return "";
56        }
57    }
58
59    /**
60     * Get the coordinates for the view with the given id.
61     * @return a ChimpRect object with the coordinates for the corners of the view
62     */
63    public ChimpRect getLocation() {
64        List<String> result = Lists.newArrayList(queryView("getlocation").split(" "));
65        if (result.size() == 4) {
66            try {
67                int left = Integer.parseInt(result.get(0));
68                int top = Integer.parseInt(result.get(1));
69                int width = Integer.parseInt(result.get(2));
70                int height = Integer.parseInt(result.get(3));
71                return new ChimpRect(left, top, left+width, top+height);
72            } catch (NumberFormatException e) {
73                return new ChimpRect();
74            }
75        }
76        return new ChimpRect();
77    }
78
79    /**
80     * Retrieve the text contained by the view
81     * @return the text contained by the view
82     */
83    public String getText() {
84        return queryView("gettext");
85    }
86
87    /**
88     * Get the class of the view
89     * @return the class name of the view
90     */
91    public String getViewClass(){
92        return queryView("getclass");
93    }
94
95    /**
96     * Get the checked status of the view.
97     * @return true if the view is checked, false otherwise
98     */
99    public boolean getChecked(){
100      return Boolean.valueOf(queryView("getchecked").trim());
101    }
102
103    /**
104     * Get whether the view is enabled or not.
105     * @return true if the view is enabled, false otherwise
106     */
107    public boolean getEnabled(){
108      return Boolean.valueOf(queryView("getenabled").trim());
109    }
110
111    /**
112     * Get the selected status of the view.
113     * @return true if the view is selected, false otherwise
114     */
115    public boolean getSelected(){
116      return Boolean.valueOf(queryView("getselected").trim());
117    }
118
119    /**
120     * Set the selected status of the view.
121     * @param selected the select status to set for the view
122     */
123    public void setSelected(boolean selected) {
124      queryView("setselected " + selected);
125    }
126
127    /**
128     * Get the focused status of the view.
129     * @return true if the view is focused, false otherwise
130     */
131    public boolean getFocused(){
132      return Boolean.valueOf(queryView("getselected").trim());
133    }
134
135    /**
136     * Set the focused status of the view.
137     * @param focused the focus status to set for the view
138     */
139    public void setFocused(boolean focused) {
140      queryView("setfocused " + focused);
141    }
142
143    /**
144     * Get the parent of the view.
145     * @return the parent of the view
146     */
147    public IChimpView getParent() {
148        List<String> results = Lists.newArrayList(queryView("getparent").split(" "));
149        if (results.size() == 2) {
150            ChimpView parent = new ChimpView(ChimpView.ACCESSIBILITY_IDS, results);
151            parent.setManager(manager);
152            return parent;
153        }
154        return null;
155    }
156
157    /**
158     * Gets the children of the view.
159     * @return the children of the view as a List of IChimpViews
160     */
161    public List<IChimpView> getChildren() {
162        List<String> results = Lists.newArrayList(queryView("getchildren").split(" "));
163        /* We make sure this has an even number of results because we don't necessarily know how
164         * many children there are, but we know all children will return a pair of accessibility ids
165         */
166        if (results.size() % 2 == 0) {
167            List<IChimpView> children = new ArrayList<IChimpView>();
168            for (int i = 0; i < results.size()/2; i++) {
169                List<String> ids = Lists.newArrayList(results.get(2 * i), results.get(2 * i + 1));
170                ChimpView child = new ChimpView(ChimpView.ACCESSIBILITY_IDS, ids);
171                child.setManager(manager);
172                children.add(child);
173            }
174            return children;
175        }
176        return new ArrayList<IChimpView>();
177    }
178
179
180    /**
181     * Gets the accessibility ids of the current view
182     * @return the accessibility ids of the current view. Its returned as a two-item array of ints
183     * with first int being the window id, and the second int being the accessibility view id.
184     */
185    public int[] getAccessibilityIds() {
186        List<String> results = Lists.newArrayList(queryView("getaccessibilityids").split(" "));
187        if (results.size() == 2) {
188            int[] accessibilityIds = new int[2];
189            try {
190                accessibilityIds[0] = Integer.parseInt(results.get(0));
191                accessibilityIds[1] = Integer.parseInt(results.get(1));
192                return accessibilityIds;
193            } catch (NumberFormatException e) {
194                LOG.log(Level.SEVERE, "Error retrieving accesibility ids: " + e.getMessage());
195            }
196        }
197        int[] empty = {0,0};
198        return empty;
199    }
200
201}
202