Merge pull request #56 from iansf/android_stop
Add stop command and supporting Android support.
This commit is contained in:
@@ -29,10 +29,16 @@ abstract class ApplicationPackage {
|
||||
|
||||
class AndroidApk extends ApplicationPackage {
|
||||
static const String _apkName = 'SkyShell.apk';
|
||||
static const String _androidPackage = 'org.domokit.sky.shell';
|
||||
static const String _packageID = 'org.domokit.sky.shell';
|
||||
static const String _componentID = '$_packageID/$_packageID.SkyActivity';
|
||||
|
||||
/// The path to the activity that should be launched.
|
||||
/// Defaults to 'org.domokit.sky.shell/org.domokit.sky.shell.SkyActivity'
|
||||
String component;
|
||||
AndroidApk(String appDir,
|
||||
[String appPackageID = _androidPackage, String appFileName = _apkName])
|
||||
{String appPackageID: _packageID,
|
||||
String appFileName: _apkName,
|
||||
this.component: _componentID})
|
||||
: super(path.join(appDir, 'apks'), appPackageID, appFileName);
|
||||
}
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ abstract class _Device {
|
||||
|
||||
class AndroidDevice extends _Device {
|
||||
static const String _ADB_PATH = 'adb';
|
||||
static const String _serverPort = '9888';
|
||||
|
||||
static const String className = 'AndroidDevice';
|
||||
static final String defaultDeviceID = 'default';
|
||||
@@ -232,6 +233,31 @@ class AndroidDevice extends _Device {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool stop(AndroidApk apk) {
|
||||
// Turn off reverse port forwarding
|
||||
try {
|
||||
runCheckedSync([adbPath, 'reverse', '--remove', 'tcp:$_serverPort']);
|
||||
} catch (e) {}
|
||||
// Stop the app
|
||||
runCheckedSync([adbPath, 'shell', 'am', 'force-stop', apk.appPackageID]);
|
||||
// Kill the server
|
||||
try {
|
||||
if (Platform.isMacOS) {
|
||||
String pid = runCheckedSync(['lsof', '-i', ':$_serverPort', '-t']);
|
||||
// Killing a pid with a shell command from within dart is hard,
|
||||
// so use a library command, but it's still nice to give the
|
||||
// equivalent command when doing verbose logging.
|
||||
_logging.info('kill $pid');
|
||||
Process.killPid(int.parse(pid));
|
||||
} else {
|
||||
runCheckedSync(['fuser', '-k', '$_serverPort/tcp']);
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
bool isConnected() => _hasValidAndroid;
|
||||
}
|
||||
|
||||
47
packages/flutter_tools/lib/src/stop.dart
Normal file
47
packages/flutter_tools/lib/src/stop.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
library sky_tools.stop;
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:args/command_runner.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:sky_tools/src/application_package.dart';
|
||||
import 'package:sky_tools/src/device.dart';
|
||||
|
||||
final Logger _logging = new Logger('sky_tools.stop');
|
||||
|
||||
class StopCommand extends Command {
|
||||
final name = 'stop';
|
||||
final description = 'Stop your Flutter app on all attached devices.';
|
||||
AndroidDevice android = null;
|
||||
|
||||
StopCommand([this.android]) {
|
||||
if (android == null) {
|
||||
android = new AndroidDevice();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> run() async {
|
||||
if (stop()) {
|
||||
return 0;
|
||||
} else {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
bool stop() {
|
||||
bool stoppedSomething = false;
|
||||
if (android.isConnected()) {
|
||||
Map<BuildPlatform, ApplicationPackage> packages =
|
||||
ApplicationPackageFactory.getAvailableApplicationPackages();
|
||||
ApplicationPackage androidApp = packages[BuildPlatform.android];
|
||||
stoppedSomething = android.stop(androidApp) || stoppedSomething;
|
||||
}
|
||||
|
||||
return stoppedSomething;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user