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

Copy the device screenshot to clipboard #19

Open
wants to merge 6 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
95 changes: 95 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm

.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build/

# Accio dependency management
Dependencies/
.accio/

# fastlane
#
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/

.DS_Store
.idea

Carthage
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Download app from [Releases](https://github.com/naman14/adb-tools-mac/releases)

- Connect device over tcp
- Take screenshots and record screen
- Copy the screenshot in your clipboard
- Open deeplinks
- Capture logcat

Expand All @@ -31,4 +32,4 @@ If you are looking for something similar for Linux, check out [gnome-android-too
>This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
>You should have received a copy of the GNU General Public License along with this app. If not, see <https://www.gnu.org/licenses/>.
>You should have received a copy of the GNU General Public License along with this app. If not, see <https://www.gnu.org/licenses/>.
4 changes: 2 additions & 2 deletions adbconnect.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_ASSET_PATHS = "\"adbconnect/Preview Content\"";
DEVELOPMENT_TEAM = V3237NN4PA;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = adbconnect/Info.plist;
Expand All @@ -338,7 +338,7 @@
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_ASSET_PATHS = "\"adbconnect/Preview Content\"";
DEVELOPMENT_TEAM = V3237NN4PA;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = adbconnect/Info.plist;
Expand Down
72 changes: 47 additions & 25 deletions adbconnect/AdbHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,54 @@
// Created by Naman Dwivedi on 11/03/21.
//

import AppKit
import Foundation

class AdbHelper {

let adb = Bundle.main.url(forResource: "adb", withExtension: nil)

func getDevices() -> [Device] {
let command = "devices -l | awk 'NR>1 {print $1}'"
let devicesResult = runAdbCommand(command)
return devicesResult
.components(separatedBy: .newlines)
.filter({ (id) -> Bool in
.filter { id -> Bool in
!id.isEmpty
})
.map { (id) -> Device in
}
.map { id -> Device in
Device(id: id, name: getDeviceName(deviceId: id))
}
}

func getDeviceName(deviceId: String) -> String {
let command = "-s " + deviceId + " shell getprop ro.product.model"
return runAdbCommand(command)
}

func takeScreenshot(deviceId: String) {
let time = formattedTime()
_ = runAdbCommand("-s " + deviceId + " shell screencap -p /sdcard/screencap_adbtool.png")
_ = self.runAdbCommand("-s " + deviceId + " pull /sdcard/screencap_adbtool.png ~/Desktop/screen" + time + ".png")
_ = runAdbCommand("-s " + deviceId + " pull /sdcard/screencap_adbtool.png ~/Desktop/screen" + time + ".png")
}


func takeScreenshotAndCopyIt(deviceId: String) {
let time = formattedTime()
let path = "~/Desktop/screen" + time + ".png"
_ = runAdbCommand("-s " + deviceId + " shell screencap -p /sdcard/screencap_adbtool.png")
_ = runAdbCommand("-s " + deviceId + " pull /sdcard/screencap_adbtool.png \(path)")

let absolutePath = NSString(string: path).expandingTildeInPath
do {
try copyToPastboard(data: Data(contentsOf: URL(fileURLWithPath: "\(absolutePath)")))
try FileManager.default.removeItem(atPath: "\(absolutePath)")
} catch {
print("Error opening file: \(error)")
}
}

func recordScreen(deviceId: String) {
let command = "-s " + deviceId + " shell screenrecord /sdcard/screenrecord_adbtool.mp4"

// run record screen in background
DispatchQueue.global(qos: .background).async {
_ = self.runAdbCommand(command)
Expand All @@ -46,26 +61,28 @@ class AdbHelper {

func stopScreenRecording(deviceId: String) {
let time = formattedTime()

// kill already running screenrecord process to stop recording
_ = runAdbCommand("-s " + deviceId + " shell pkill -INT screenrecord")

// after killing the screenrecord process,we have to for some time before pulling the file else file stays corrupted

// after killing the screenrecord process,we have to for some time
// before pulling the file else file stays corrupted
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
_ = self.runAdbCommand("-s " + deviceId + " pull /sdcard/screenrecord_adbtool.mp4 ~/Desktop/record" + time + ".mp4")
_ = self.runAdbCommand("-s " + deviceId +
" pull /sdcard/screenrecord_adbtool.mp4 ~/Desktop/record" + time + ".mp4")
}
}

func makeTCPConnection(deviceId: String) {
DispatchQueue.global(qos: .background).async {
let deviceIp = self.getDeviceIp(deviceId: deviceId);
let deviceIp = self.getDeviceIp(deviceId: deviceId)
let tcpCommand = "-s " + deviceId + " tcpip 5555"
_ = self.runAdbCommand(tcpCommand)
let connectCommand = "-s " + deviceId + " connect " + deviceIp + ":5555"
_ = self.runAdbCommand(connectCommand)
}
}

func disconnectTCPConnection(deviceId: String) {
DispatchQueue.global(qos: .background).async {
_ = self.runAdbCommand("-s " + deviceId + " disconnect")
Expand All @@ -76,40 +93,45 @@ class AdbHelper {
let command = "-s " + deviceId + " shell ip route | awk '{print $9}'"
return runAdbCommand(command)
}

func openDeeplink(deviceId: String, deeplink: String) {
let command = "-s " + deviceId + " shell am start -a android.intent.action.VIEW -d '" + deeplink + "'"
_ = runAdbCommand(command)
}

func captureBugReport(deviceId: String) {
let time = formattedTime()
DispatchQueue.global(qos: .background).async {
_ = self.runAdbCommand("-s " + deviceId + " logcat -d > ~/Desktop/logcat" + time + ".txt")
}
}

private func formattedTime() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd-HH-mm"
let time = formatter.string(from: Date())
return time
}

private func runAdbCommand(_ command: String) -> String {
let task = Process()
let pipe = Pipe()

task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-c", adb!.path + " " + command]
task.launchPath = "/bin/sh"
task.launch()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines)
return output
}

}

func copyToPastboard(data: Data) {
let pastboard = NSPasteboard.general
pastboard.declareTypes([.png], owner: nil)

pastboard.setData(data, forType: NSPasteboard.PasteboardType.png)
}
}
36 changes: 16 additions & 20 deletions adbconnect/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,40 @@ import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

var popover: DevicesPopover!
var statusBarItem: NSStatusItem!
func applicationDidFinishLaunching(_ aNotification: Notification) {

func applicationDidFinishLaunching(_: Notification) {
// Create the popover
let popover = DevicesPopover()
popover.contentSize = NSSize(width: 300, height: 250)
popover.behavior = .transient
self.popover = popover

// Create the status item
self.statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(NSStatusItem.variableLength))
if let button = self.statusBarItem.button {
statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(NSStatusItem.variableLength))

if let button = statusBarItem.button {
button.image = NSImage(named: "Icon")
button.action = #selector(togglePopover(_:))
}

NSApp.activate(ignoringOtherApps: true)
}

@objc func togglePopover(_ sender: AnyObject?) {
if let button = self.statusBarItem.button {
if self.popover.isShown {
self.popover.performClose(sender)
if let button = statusBarItem.button {
if popover.isShown {
popover.performClose(sender)
} else {
NSApp.activate(ignoringOtherApps: true)
self.popover.update()
self.popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
popover.update()
popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
}
}
}
func applicationWillResignActive(_ notification: Notification) {
self.popover.performClose(nil)

func applicationWillResignActive(_: Notification) {
popover.performClose(nil)
}

}


15 changes: 15 additions & 0 deletions adbconnect/Assets.xcassets/PasteIcon.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"images" : [
{
"filename" : "PasteIcon.png",
"idiom" : "mac"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading