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 17package com.google.android.droiddriver.util; 18 19import android.app.Activity; 20 21import com.google.android.droiddriver.instrumentation.InstrumentationDriver; 22import com.google.common.base.Supplier; 23 24/** 25 * Static helper methods for retrieving activities. 26 */ 27public class ActivityUtils { 28 private static Supplier<Activity> runningActivitySupplier; 29 30 /** 31 * Sets the Supplier for the running (a.k.a. resumed or foreground) activity. 32 * Called from {@link com.google.android.droiddriver.runner.TestRunner}. If a 33 * custom runner is used, this method must be called appropriately, otherwise 34 * {@link #getRunningActivity} won't work. 35 */ 36 public static synchronized void setRunningActivitySupplier(Supplier<Activity> activitySupplier) { 37 runningActivitySupplier = activitySupplier; 38 } 39 40 /** 41 * Gets the running (a.k.a. resumed or foreground) activity. 42 * {@link InstrumentationDriver} depends on this. 43 * 44 * @return the currently running activity, or null if no activity has focus. 45 */ 46 public static synchronized Activity getRunningActivity() { 47 return runningActivitySupplier.get(); 48 } 49} 50