Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adb Install Apk #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
description="Uninstalls the current application">
</action>

<action id="com.developerphil.adbidea.action.InstallAction"
class="com.developerphil.adbidea.action.InstallAction"
text="ADB Install App"
description="Installs the current application">
</action>

<action id="com.developerphil.adbidea.action.KillAction"
class="com.developerphil.adbidea.action.KillAction"
text="ADB Kill App"
Expand All @@ -103,6 +109,7 @@
text="ADB Clear App Data and Restart"
description="Clears the private storage of the app and restarts it">
</action>

<add-to-group group-id="AndroidToolsGroup" anchor="first"/>
</group>
</actions>
Expand Down
14 changes: 14 additions & 0 deletions src/com/developerphil/adbidea/action/InstallAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.developerphil.adbidea.action;

import com.developerphil.adbidea.adb.AdbFacade;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;

public class InstallAction extends AdbAction {

public void actionPerformed(AnActionEvent e, Project project) {
AdbFacade.install(project);
}


}
1 change: 1 addition & 0 deletions src/com/developerphil/adbidea/action/QuickListAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected void fillActions(@Nullable final Project project,
addAction("com.developerphil.adbidea.action.RestartAction", group);
addAction("com.developerphil.adbidea.action.ClearDataAction", group);
addAction("com.developerphil.adbidea.action.ClearDataAndRestartAction", group);
addAction("com.developerphil.adbidea.action.InstallAction", group);
}

protected boolean isEnabled() {
Expand Down
51 changes: 51 additions & 0 deletions src/com/developerphil/adbidea/adb/AdbFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.developerphil.adbidea.adb.command.*;
import com.developerphil.adbidea.ui.ApkChooserDialog;
import com.developerphil.adbidea.ui.DeviceChooserDialog;
import com.developerphil.adbidea.ui.ModuleChooserDialogHelper;
import com.google.common.collect.Lists;
Expand All @@ -13,6 +14,8 @@
import org.jetbrains.android.sdk.AndroidSdkUtils;
import org.jetbrains.android.util.AndroidUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -27,6 +30,29 @@ public static void uninstall(Project project) {
executeOnDevice(project, new UninstallCommand());
}

public static void install(Project project) {

final DeviceResult result = getDevice(project);
if (result == null) {
error("No Device found");
} else {
final List<File> apk = getApk(result.facet);
if (apk != null && !apk.isEmpty()) {
for (final IDevice device : result.devices) {
EXECUTOR.submit(new Runnable() {
@Override
public void run() {
new InstallCommand().run(device, apk.get(0));
}
});
}
} else {
error("No Apk File found");
}
}

}

public static void kill(Project project) {
executeOnDevice(project, new KillCommand());
}
Expand Down Expand Up @@ -97,6 +123,31 @@ private static DeviceResult getDevice(Project project) {
return null;
}

private static List<File> getApk(AndroidFacet facet) {
List<File> items = new ArrayList<File>();
File file = new File(facet.getModule().getModuleFile().getParent().getPath() +
File.separator +
"build" +
File.separator +
"outputs" +
File.separator +
"apk"
);

if (file.exists()) {
items = Lists.newArrayList(file.listFiles());
}

if (items == null || items.isEmpty()) {
return new ArrayList<File>();
}

ApkChooserDialog apkChooserDialog = new ApkChooserDialog(facet.getModule().getProject(), items);
apkChooserDialog.show();

return apkChooserDialog.getChosenElements();
}

private static List<AndroidFacet> getApplicationFacets(Project project) {

List<AndroidFacet> facets = Lists.newArrayList();
Expand Down
26 changes: 26 additions & 0 deletions src/com/developerphil/adbidea/adb/command/InstallCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.developerphil.adbidea.adb.command;

import com.android.ddmlib.IDevice;
import com.android.ddmlib.InstallException;

import java.io.File;

import static com.developerphil.adbidea.ui.NotificationHelper.error;
import static com.developerphil.adbidea.ui.NotificationHelper.info;

public class InstallCommand{
public boolean run(IDevice device, File apk) {
try {

if (apk.exists()) {
device.installPackage(apk.getAbsolutePath(), true);
}

info(String.format("<b>%s</b> installed on %s", apk.getName(), device.getName()));
return true;
} catch (InstallException e1) {
error("Install fail... " + e1.getMessage());
}
return false;
}
}
31 changes: 31 additions & 0 deletions src/com/developerphil/adbidea/ui/ApkChooserDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.developerphil.adbidea.ui;

import com.intellij.ide.util.ChooseElementsDialog;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.project.Project;
import org.jetbrains.android.util.AndroidBundle;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.File;
import java.util.List;

public class ApkChooserDialog extends ChooseElementsDialog<File> {
public ApkChooserDialog(Project project, List<? extends File> items) {
super(project, items, "Destination Apk", AndroidBundle.message("android.extract.package.choose.dest.apk"));
this.myChooser.setSingleSelectionMode();
}

@Override
protected String getItemText(File file) {
return file.getName();
}

@Nullable
@Override
protected Icon getItemIcon(File file) {
return ModuleType.EMPTY.getIcon();
}


}