<commit_before>package sunmi.com.sunmiauto;

import android.app.Instrumentation;
import android.os.Build;
import android.os.Environment;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.util.Log;

import java.io.File;

/**
 * Created by fengy on 2017/7/8.
 */

public class SunmiUtil {
    static Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    static UiDevice device = UiDevice.getInstance(instrumentation);
    static final int timeoutSeconds = 20000;

    //清除最近使用程序
    public static void clearAllRecentApps() throws RemoteException {
        try{
            device.pressHome();
            device.pressHome();
            device.pressRecentApps();
            device.wait(Until.hasObject(By.res("com.android.systemui:id/loading")),timeoutSeconds);
            sleep(2000);
            UiObject2 clearObj = device.findObject(By.res("com.android.systemui:id/loading"));
            clearObj.clickAndWait(Until.newWindow(),timeoutSeconds);
        }catch (NullPointerException e){
            device.pressHome();
            device.pressHome();
            device.pressRecentApps();
            device.wait(Until.hasObject(By.res("com.android.systemui:id/loading")),timeoutSeconds);
            sleep(2000);
            UiObject2 clearObj = device.findObject(By.res("com.android.systemui:id/loading"));
            clearObj.clickAndWait(Until.newWindow(),timeoutSeconds);
        }

    }

    //传递一个应用名称，找到该名称的应用，找到返回true，未找到返回false
    public static boolean findAppByText(String appName){
        device.pressHome();
        device.pressHome();
        UiObject2 appIcon = device.findObject(By.text(appName));
        if(null != appIcon){
            return true;
        }
        device.wait(Until.hasObject(By.res("com.woyou.launcher:id/page_indicator")),timeoutSeconds);
        UiObject2 indicatorObj = device.findObject(By.res("com.woyou.launcher:id/page_indicator"));
        int pages = indicatorObj.getChildCount();
        Log.v("sssss",Integer.toString(pages));
        device.swipe(5,device.getDisplayHeight()/2,device.getDisplayWidth()-5,device.getDisplayHeight()/2,20);
        sleep(2000);
        for(int i = 0;i < pages - 1;i++){
            Log.v("ssss",Integer.toString(i));
            UiObject2 appObj = device.findObject(By.text(appName));
            if(null != appObj){
                return true;
            }
            else {
                sleep(2000);
                device.swipe(device.getDisplayWidth()-5,device.getDisplayHeight()/2,5,device.getDisplayHeight()/2,10);
                sleep(2000);
            }
        }
        return false;
    }

    //传递一个应用名称，找到该名称的应用并打开，找到返回true，未找到返回false
    public static boolean findAppAndOpenByText(String appName){
        device.pressHome();
        device.pressHome();
        UiObject2 appIcon = device.findObject(By.text(appName));
        if(null != appIcon){
            appIcon.clickAndWait(Until.newWindow(),timeoutSeconds);
            return true;<commit_msg>fix findappbytext bug<commit_after>package sunmi.com.sunmiauto;

import android.app.Instrumentation;
import android.os.Build;
import android.os.Environment;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.util.Log;

import java.io.File;

/**
 * Created by fengy on 2017/7/8.
 */

public class SunmiUtil {
    static Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    static UiDevice device = UiDevice.getInstance(instrumentation);
    static final int timeoutSeconds = 20000;

    //清除最近使用程序
    public static void clearAllRecentApps() throws RemoteException {
        try{
            device.pressHome();
            device.pressHome();
            device.pressRecentApps();
            device.wait(Until.hasObject(By.res("com.android.systemui:id/loading")),timeoutSeconds);
            sleep(2000);
            UiObject2 clearObj = device.findObject(By.res("com.android.systemui:id/loading"));
            clearObj.clickAndWait(Until.newWindow(),timeoutSeconds);
        }catch (NullPointerException e){
            device.pressHome();
            device.pressHome();
            device.pressRecentApps();
            device.wait(Until.hasObject(By.res("com.android.systemui:id/loading")),timeoutSeconds);
            sleep(2000);
            UiObject2 clearObj = device.findObject(By.res("com.android.systemui:id/loading"));
            clearObj.clickAndWait(Until.newWindow(),timeoutSeconds);
        }

    }

    //传递一个应用名称，找到该名称的应用，找到返回true，未找到返回false
    public static boolean findAppByText(String appName){
        device.pressHome();
        device.pressHome();
        UiObject2 appIcon = device.findObject(By.text(appName));
        if(null != appIcon){
            return true;
        }
        device.wait(Until.hasObject(By.res("com.woyou.launcher:id/page_indicator")),timeoutSeconds);
        UiObject2 indicatorObj = device.findObject(By.res("com.woyou.launcher:id/page_indicator"));
        int pages = indicatorObj.getChildCount();
        Log.v("sssss",Integer.toString(pages));
        device.swipe(5,device.getDisplayHeight()/2,device.getDisplayWidth()-5,device.getDisplayHeight()/2,20);
        sleep(2000);
        for(int i = 0;i < pages;i++){
            Log.v("ssss",Integer.toString(i));
            UiObject2 appObj = device.findObject(By.text(appName));
            if(null != appObj){
                return true;
            }
            else {
                sleep(2000);
                device.swipe(device.getDisplayWidth()-5,device.getDisplayHeight()/2,5,device.getDisplayHeight()/2,10);
                sleep(2000);
            }
        }
        return false;
    }

    //传递一个应用名称，找到该名称的应用并打开，找到返回true，未找到返回false
    public static boolean findAppAndOpenByText(String appName){
        device.pressHome();
        device.pressHome();
        UiObject2 appIcon = device.findObject(By.text(appName));
        if(null != appIcon){
            appIcon.clickAndWait(Until.newWindow(),timeoutSeconds);
            return true;
{'fraction_non_alphanumeric': 0.12145061728395062, 'fraction_numerical': 0.01589506172839506, 'mean_word_length': 2.83491124260355, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3137414', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1972, 'n_words': 268}