package com.nickming.view;

import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/4/24.
 */

public class ScreenShoddmlib {
    //private BufferedImage image = null;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        AndroidDebugBridge.init(false); //
        ScreenShoddmlib screenshot = new ScreenShoddmlib();
        IDevice device = screenshot.getDevice();

        for (int i = 0; i < 10; i++) {

            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("MM-dd-HH-mm-ss");
            String nowTime = df.format(date);
            //	screenshot.getScreenShot(device, "Robotium" + nowTime);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }




    /**
     * 获取得到device对象
     *
     * @return
     */
    private IDevice getDevice() {
        IDevice device;
        AndroidDebugBridge bridge = AndroidDebugBridge
                .createBridge("adb", true);// 如果代码有问题请查看API，修改此处的参数值试一下
        waitDevicesList(bridge);
        IDevice devices[] = bridge.getDevices();
        device = devices[0];
        return device;
    }

    /**
     * 等待查找device
     *
     * @param bridge
     */
    private void waitDevicesList(AndroidDebugBridge bridge) {
        int count = 0;
        while (bridge.hasInitialDeviceList() == false) {
            try {
                Thread.sleep(500);
                count++;
            } catch (InterruptedException e) {
            }
            if (count > 240) {
                System.err.print("等待获取设备超时");
                break;
            }
        }
    }
}
