Esempio n. 1
0
    def load_lookup(self):
        self.lookup = {}  # prevents recursive calls (i guess) due to how things currently are
        sdk_dir = project.get_sdk_dir()
        platform = project.get_target_platform()

        self.load_widgets(sdk_dir, platform)

        els = ET.parse(os.path.join(sdk_dir, "platforms", platform, "data/res/values/attrs.xml")).getroot()
        self.lookup = {}

        for el in els:
            name = el.attrib.get("name", None)
            if name is None:
                continue
            attrs = {}

            for attr in el.getchildren():
                attr_name = attr.attrib.pop("name", None)
                if attr_name is None:
                    continue
                options = []
                for enum in attr.getchildren():
                    options.append(enum.attrib["name"])
                attrs[attr_name] = options

            self.lookup[name] = attrs
Esempio n. 2
0
    def run(self, device):
        adb = os.path.join(project.get_sdk_dir(), "platform-tools", "adb")
        activity = get_setting("sublimeandroid_default_activity", "")
        if not activity:
            activity = project.get_activity_main()

        opts = {"cmd": [adb, "-s", device, "shell", "am", "start", "-n", activity], "working_dir": project.get_path()}
        self.window.run_command("android_exec", opts)
Esempio n. 3
0
def get_devices():
    """Gets a list of devices currently attached.

    Querys `adb` from `get_sdk_dir()` for all emulator/device instances.

    Returns:
        A tuple of lists. The first value is a list of device ids suitable for
        use in selecting a device when calling adb. The second value is a list
        of strings suitable for displaying text more descriptive to the use to
        choose an appropriate device.
    """
    adb = os.path.join(project.get_sdk_dir(), "platform-tools", "adb")
    cmd = [adb, "devices"]
    try:
        proc = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE)
        out, err = proc.communicate()
    except:
        sublime.error_message("Error trying to launch ADB:\n\n{0}\n\n{1}".format(cmd, traceback.format_exc()))
        return
    # get list of device ids
    devices = []
    for line in out.split("\n"):
        line = line.strip()
        if line not in ["", "List of devices attached"]:
            devices.append(re.sub(r"[ \t]*device$", "", line))
    # build quick menu options displaying name, version, and device id
    options = []
    for device in devices:
        # dump build.prop
        cmd = [adb, "-s", device, "shell", "cat /system/build.prop"]
        proc = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE)
        build_prop = proc.stdout.read().strip()
        # get name
        product = "Unknown"  # should never actually see this
        if device.startswith("emulator"):
            port = device.rsplit("-")[-1]
            t = telnetlib.Telnet("localhost", port)
            t.read_until("OK", 1000)
            t.write("avd name\n")
            product = t.read_until("OK", 1000)
            t.close()
            product = product.replace("OK", "").strip()
        else:
            product = re.findall(r"^ro\.product\.model=(.*)$", build_prop, re.MULTILINE)
            if product:
                product = product[0]
        # get version
        version = re.findall(r"ro\.build\.version\.release=(.*)$", build_prop, re.MULTILINE)
        if version:
            version = version[0]
        else:
            version = "x.x.x"
        product = str(product).strip()
        version = str(version).strip()
        device = str(device).strip()
        options.append("%s %s - %s" % (product, version, device))

    return devices, options
Esempio n. 4
0
def exec_tool(cmd=[], panel=False):
    # TODO is panel necessary? need docs
    # TODO windows compat
    cmd[0] = os.path.join(project.get_sdk_dir(), "tools", cmd[0])
    if panel:
        sublime.active_window().run_command("exec", {"cmd": cmd})
    else:
        log.debug("executing sdk tool: %s", " ".join(cmd))
        subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Esempio n. 5
0
    def run(self, device=None, target="debug"):
        # TODO if device is None, run android_select_device and come back here.
        if device is None:
            self.window.run_command("android_select_device", {"target": target})
            return

        adb = os.path.join(project.get_sdk_dir(), "platform-tools", "adb")
        name = "{0}-{1}.apk".format(get_project_name(), target)
        apk = os.path.join(project.get_path(), "bin", name)

        opts = {"cmd": [adb, "-s", device, "install", "-r", apk], "working_dir": project.get_path()}
        self.window.run_command("android_exec", opts)
Esempio n. 6
0
    def run(self):
        support = os.path.join(project.get_sdk_dir(), "extras", "android", "support")
        if not os.path.exists(support):
            sublime.error_message("Support libraries are not installed.")
            return

        self.support_libs = []
        self.options = []

        for d in [d for d in os.listdir(support) if re.match(r"v[0-9]*", d) is not None]:
            path = os.path.join(support, d)
            print "checking:", path
            for root, dirs, files in os.walk(path):
                if self.match_files(root, files):
                    break

        self.window.show_quick_panel(self.options, self.on_done)
Esempio n. 7
0
    def get_targets(self, path, targets={}):
        """Gets list of ANT targets

        Recursively search given file and contained imports for ant targets.

        Returns:
            A dict containing keys of all ANT targets and values being the target's
            description.
        """
        log.debug("checking path: %s", path)
        # return in cases where path is not valid. this may occur when the build.xml
        # stubs imports for custom rules that may not have been implemented.
        if not os.path.isfile(path):
            return

        root = ET.parse(path).getroot()

        for target in root.getiterator("target"):
            name = target.attrib["name"]
            desc = target.attrib.get("description", "")[:100]

            # TODO skip targets with ant vars for now
            if re.search("\${.*}", name) is not None:
                continue

            if not name.startswith("-") and name not in targets:
                targets[name] = desc

        for imp in root.getiterator("import"):
            f = imp.attrib["file"]
            # check for paths with a reference to ${sdk.dir}
            #
            # TODO should load property files for more complex build.xml files
            # to determine appropriate paths with referenced ant vars.
            log.debug("found import with file attr: %s", f)
            if f.startswith("${sdk.dir"):
                f = f.replace("${sdk.dir}", project.get_sdk_dir())

            if not os.path.isabs(f):
                f = os.path.join(project.get_path(), f)

            self.get_targets(f, targets)

        return targets
Esempio n. 8
0
    def on_close(self, view):
        if view.name() == "Create Android Project":
            data = view.substr(sublime.Region(0, view.size()))
            args = ["create", "project"]
            for line in data.split("\n"):
                if not line.startswith("--"):
                    continue

                if line.startswith("--path"):
                    path = line.split(" ", 1)[1]
                    path = os.path.join(sublime.active_window().folders()[0], path)
                    line = "--path " + path
                args.append(line.rstrip())
            android = os.path.join(project.get_sdk_dir(), "tools", "android")
            cmd = " ".join([android] + args)
            log.info("running: %s", cmd)
            p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            stdout = p.stdout.read()
            log.info(stdout)
            stderr = p.stderr.read()
            log.info(stderr)
Esempio n. 9
0
    def run(self):
        view = self.window.new_file()
        view.set_name("Create Android Project")
        view.set_scratch(True)
        edit = view.begin_edit()
        buf = """
--target <target-id>

--name MyApp

--path ./

--activity MainActivity

--package com.example.app
"""
        android = os.path.join(project.get_sdk_dir(), "tools", "android")
        p = subprocess.Popen([android, "list", "targets"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout = p.stdout.readlines()
        targets = "".join([line.replace(" or", "") for line in stdout if line.startswith("id:")])
        buf = targets + buf
        view.insert(edit, 0, buf)
        view.end_edit(edit)
Esempio n. 10
0
def load_adbview(settings):
    settings.set("adb_command", os.path.join(project.get_sdk_dir(), "platform-tools", "adb"))