getDeviceIds Fetches the serial numbers of all devices available if they are in allowed Ids. - Java Native OS

Java examples for Native OS:OS

Description

getDeviceIds Fetches the serial numbers of all devices available if they are in allowed Ids.

Demo Code

/*/*from  w  w w.  j  a v  a2 s.c om*/
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
//package com.java2s;
import java.io.*;

import java.net.*;
import java.util.*;

public class Main {
    /**
     * getDeviceIds
     * Fetches the serial numbers of all devices available if they are in allowedIds.
     */
    public static ArrayList getAndroidDeviceIds(String adb, int run_id,
            String[] allowedIds) {
        String line = null;
        String body = null;
        Process p = null;
        ArrayList ret = new ArrayList();
        int numOfflineDevices = 0;
        int numReadyDevices = 0;
        int i = 0;
        InetAddress ia = null;

        /**
        if(allowedIds != null){
           System.out.println("allowedIds=" + allowedIds);
           for(i = 0; i < allowedIds.length; ++i){
              System.out.println("\t" + allowedIds[i]);
           }
        }
         **/

        try {
            if (new File(adb).exists()) {
                String[] listDevices = { adb, "devices" };
                p = Runtime.getRuntime().exec(listDevices);

                BufferedReader br = new BufferedReader(
                        new InputStreamReader(p.getInputStream()));

                // Collect all of the device serial numbers.  Note that these serial numbers aren't the
                // same as the serial numbers found on the back of a device.
                while (true) {
                    line = br.readLine();

                    if ((line == null)
                            || (line.trim().compareToIgnoreCase("") == 0)) {
                        break;
                    }

                    if (line.indexOf("List of devices") == -1) {
                        if (line.indexOf("device") > -1) {
                            ++numReadyDevices;
                            line = line
                                    .substring(0, line.indexOf("device"));
                            line = line.trim();

                            //System.out.println("getAndroidDeviceIds: line=" + line);

                            // Make sure we're allowed to use this device.
                            if (allowedIds != null) {
                                for (i = 0; i < allowedIds.length; ++i) {
                                    if (line.compareToIgnoreCase(allowedIds[i]
                                            .trim()) == 0) {
                                        System.out
                                                .println("Adding device: '"
                                                        + line + "'");
                                        ret.add(line);
                                    } else {
                                        //System.out.println("Found device '" + line + "', but that does not match " + allowedIds[i].trim());
                                    }
                                }
                            } else {
                                // If no restricted list of IDs was given, return all of them.
                                ret.add(line);
                            }

                        } else if (line.indexOf("offline") > -1) {
                            ++numOfflineDevices;
                            line = line.substring(0,
                                    line.indexOf("offline"));
                            line = line.trim();
                            System.out.println("Found offline device: '"
                                    + line + "'");
                        }
                    }
                }

                if ((numReadyDevices == 0 || numOfflineDevices > 0)
                        && run_id > -1) {
                    try {
                        System.out
                                .println("There are no devices reporting at all.");

                        /*
                        ia = InetAddress.getLocalHost();
                        String hostname = ia.getHostName();

                        body = "(The following message is automatically generated by the Mustella framework.)  Device trouble is ocurring on " + hostname + ".  ";

                        if( numOfflineDevices > 0 ){
                           body = body + "We have " + Integer.toString(numOfflineDevices) + " offline device(s).  ";
                        }

                        if( numReadyDevices + numOfflineDevices == 0 ){
                           body = body + "There are no devices reporting at all.  ";
                        }

                        if( numReadyDevices > 0 ){
                           body = body + "On the bright side, at least we have " + Integer.toString(numReadyDevices) + " device(s) still able to respond, so all is not lost.  ";
                        }

                        body = body + "Can you please help?  Thanks!  ";

                        InternetAddress[] to = new InternetAddress[1];
                        to[0] = new InternetAddress("MustellaMobileResults@adobe.com");
                        HtmlNotify hn = new HtmlNotify();
                        hn.sendMessage("inner-relay-1.corp.adobe.com",
                                    "rvollmar@adobe.com",
                                    to,
                                    hostname + " mustella device offline",
                                    body);
                         */

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ret;
    }
}

Related Tutorials