Adding request for write storage permissions.

This commit is contained in:
Nedyalko Nikolov
2016-11-07 10:29:23 +02:00
parent b39b320a50
commit 303b609573
5 changed files with 55 additions and 7 deletions

1
.gitignore vendored
View File

@@ -11,6 +11,7 @@ dist/
package/
*.js
!tests/hooks/**/*.*
!gruntfile.js
!js-libs/**/*.*
!css/**/*.*

View File

@@ -2,6 +2,8 @@
import * as trace from "trace";
import tests = require("../testRunner");
import {Label} from "ui/label";
import * as application from "application";
import * as platform from "platform";
trace.enable();
trace.addCategories(trace.categories.Test + "," + trace.categories.Error);
@@ -21,14 +23,38 @@ page.id = "mainPage";
page.on(Page.navigatedToEvent, onNavigatedTo);
function runTests() {
setTimeout(function () {
tests.runAll();
}, 10);
}
function onNavigatedTo(args) {
let label = new Label();
label.text = "Running non-UI tests...";
page.content = label;
args.object.off(Page.navigatedToEvent, onNavigatedTo);
setTimeout(function () {
tests.runAll();
}, 10);
if (parseInt(platform.device.sdkVersion) >= 23) {
let handler = (args: application.AndroidActivityRequestPermissionsEventData) => {
application.android.off(application.AndroidApplication.activityRequestPermissionsEvent, handler);
if (args.requestCode === 1234 && args.grantResults.length > 0 && args.grantResults[0] === android.content.pm.PackageManager.PERMISSION_GRANTED) {
runTests();
} else {
trace.write("Permission for write to external storage not granted!", trace.categories.Error, trace.messageType.error);
}
};
application.android.on(application.AndroidApplication.activityRequestPermissionsEvent, handler);
if ((<any>android.support.v4.content.ContextCompat).checkSelfPermission(application.android.currentContext, (<any>android).Manifest.permission.WRITE_EXTERNAL_STORAGE) !== android.content.pm.PackageManager.PERMISSION_GRANTED) {
(<any>android.support.v4.app.ActivityCompat).requestPermissions(application.android.currentContext, [(<any>android).Manifest.permission.WRITE_EXTERNAL_STORAGE], 1234);
} else {
runTests();
}
} else {
runTests();
}
}
export function createPage() {

View File

@@ -176,7 +176,9 @@ function printRunTestStats() {
testFileContent.push("</testsuite>");
testFileContent.push("</testsuites>");
let testFilePath = fs.path.join(fs.knownFolders.documents().path, "test-results.xml");
let testFilePath = fs.path.join(android.os.Environment.getExternalStorageDirectory().getAbsolutePath(), "Documents", "test-results.xml");
console.log("testFilePath =====>>>>>>> " + testFilePath);
let testFile = fs.File.fromPath(testFilePath);
testFile.writeTextSync(testFileContent.join(""));

View File

@@ -0,0 +1,19 @@
var childProcess = require("child_process");
module.exports = function(logger, platformsData, projectData, hookArgs) {
if (/apk$/.test(hookArgs.packageFilePath)) {
var writeCommand = "adb shell pm grant " + projectData.projectId + " android.permission.WRITE_EXTERNAL_STORAGE";
var readCommand = "adb shell pm grant " + projectData.projectId + " android.permission.READ_EXTERNAL_STORAGE";
var callback = function(error, stdout, stderr) {
if (error) {
console.log(error);
}
};
childProcess.exec(writeCommand, {
cwd: projectData.projectDir
}, callback);
childProcess.exec(readCommand, {
cwd: projectData.projectDir
}, callback);
}
}

View File

@@ -300,12 +300,12 @@ declare module "application" {
/**
* The Permissions
*/
permissions: Array<String>,
permissions: Array<string>,
/**
* The Granted.
*/
grantResults: Array<Number>
grantResults: Array<number>
}
/**
@@ -480,7 +480,7 @@ declare module "application" {
/**
* This event is raised on the back button is pressed in an android application.
*/
on(event: "activityRequestPermissions", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any);
on(event: "activityRequestPermissions", callback: (args: AndroidActivityRequestPermissionsEventData) => void, thisArg?: any);
/**
* String value used when hooking to activityCreated event.