1 /* 2 * Copyright (C) 2013 DroidDriver committers 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 17 package io.appium.droiddriver.base; 18 19 import android.app.Instrumentation; 20 21 import java.util.Map; 22 import java.util.WeakHashMap; 23 24 import io.appium.droiddriver.finders.ByXPath; 25 26 /** 27 * Internal helper for DroidDriver implementation. 28 */ 29 public class DroidDriverContext<R, E extends BaseUiElement<R, E>> { 30 private final Instrumentation instrumentation; 31 private final BaseDroidDriver<R, E> driver; 32 private final Map<R, E> map; 33 DroidDriverContext(Instrumentation instrumentation, BaseDroidDriver<R, E> driver)34 public DroidDriverContext(Instrumentation instrumentation, BaseDroidDriver<R, E> driver) { 35 this.instrumentation = instrumentation; 36 this.driver = driver; 37 map = new WeakHashMap<R, E>(); 38 } 39 getInstrumentation()40 public Instrumentation getInstrumentation() { 41 return instrumentation; 42 } 43 getDriver()44 public BaseDroidDriver<R, E> getDriver() { 45 return driver; 46 } 47 getElement(R rawElement, E parent)48 public E getElement(R rawElement, E parent) { 49 E element = map.get(rawElement); 50 if (element == null) { 51 element = driver.newUiElement(rawElement, parent); 52 map.put(rawElement, element); 53 } 54 return element; 55 } 56 newRootElement(R rawRoot)57 public E newRootElement(R rawRoot) { 58 clearData(); 59 return getElement(rawRoot, null /* parent */); 60 } 61 clearData()62 private void clearData() { 63 map.clear(); 64 ByXPath.clearData(); 65 } 66 } 67