Re-implement subset of android.system package for API 19

This commit is contained in:
Eugen Pechanec 2022-06-20 00:54:14 +02:00
commit 2460552bb8
No known key found for this signature in database
GPG key ID: 1E110C960179440E
13 changed files with 307 additions and 0 deletions

View file

@ -152,7 +152,12 @@ page at http://checkstyle.sourceforge.net/config.html -->
<property name="severity" value="info" />
</module>
<module name="UpperEll" />
</module>
<!-- Excludes android.system polyfill. -->
<!-- See https://checkstyle.org/config_filefilters.html -->
<module name="BeforeExecutionExclusionFileFilter">
<property name="fileNamePattern" value=".*[\\/]java[\\/]android[\\/]system[\\/].*$" />
</module>
</module>

1
libcore/build.gradle Normal file
View file

@ -0,0 +1 @@
apply plugin: 'java-library'

View file

@ -0,0 +1,30 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libcore.io;
/**
* A checked exception thrown when {@link Os} methods fail. This exception contains the native
* errno value, for comparison against the constants in {@link OsConstants}, should sophisticated
* callers need to adjust their behavior based on the exact failure.
*/
public final class ErrnoException extends Exception {
private final String functionName;
public final int errno;
public ErrnoException(String functionName, int errno) {
throw new AssertionError();
}
}

View file

@ -0,0 +1,23 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libcore.io;
public final class Libcore {
private Libcore() {
}
public static Os os = null;
}

View file

@ -0,0 +1,25 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libcore.io;
import java.io.FileDescriptor;
import java.nio.ByteBuffer;
public interface Os {
public String strerror(int errno);
public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException;
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libcore.io;
public final class OsConstants {
private OsConstants() {
}
public static final int EINTR = placeholder();
public static String errnoName(int errno) {
return null;
}
// A hack to avoid these constants being inlined by javac...
private static int placeholder() { return 0; }
}

View file

@ -22,6 +22,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.13.1'
compileOnly rootProject.fileTree("thirdparty/androidx/annotation/1.3.0/annotation-1.3.0.jar")
compileOnly project(':libcore')
}
apply from: "$project.rootDir/config/android-checkstyle.gradle"

View file

@ -54,6 +54,8 @@ classpath="$CLASSES_DIR"
classpath="$classpath:$ROOT_PROJECT_DIR/thirdparty/androidx/annotation/1.3.0/annotation-1.3.0.jar"
javac -bootclasspath "$ANDROID_JAR" -cp "$classpath" -d "$CLASSES_DIR" \
-source 1.8 -target 1.8 \
android/system/*.java \
$ROOT_PROJECT_DIR/libcore/src/main/java/libcore/io/*.java \
com/genymobile/scrcpy/*.java \
com/genymobile/scrcpy/wrappers/*.java
@ -65,6 +67,7 @@ then
# use dx
"$ANDROID_HOME/build-tools/$BUILD_TOOLS/dx" --dex \
--output "$BUILD_DIR/classes.dex" \
android/system/*.class \
android/view/*.class \
android/content/*.class \
com/genymobile/scrcpy/*.class \
@ -78,6 +81,7 @@ else
# use d8
"$ANDROID_HOME/build-tools/$BUILD_TOOLS/d8" --classpath "$ANDROID_JAR" \
--output "$BUILD_DIR/classes.zip" \
android/system/*.class \
android/view/*.class \
android/content/*.class \
com/genymobile/scrcpy/*.class \

View file

@ -0,0 +1,97 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.system;
import android.annotation.TargetApi;
import java.io.IOException;
import java.net.SocketException;
import libcore.io.Libcore;
/**
* A checked exception thrown when {@link Os} methods fail. This exception contains the native
* errno value, for comparison against the constants in {@link OsConstants}, should sophisticated
* callers need to adjust their behavior based on the exact failure.
*/
@SuppressWarnings("unused")
@TargetApi(21)
public final class ErrnoException extends Exception {
private final String functionName;
/**
* The errno value, for comparison with the {@code E} constants in {@link OsConstants}.
*/
public final int errno;
/**
* Constructs an instance with the given function name and errno value.
*/
public ErrnoException(String functionName, int errno) {
this.functionName = functionName;
this.errno = errno;
}
/**
* Constructs an instance with the given function name, errno value, and cause.
*/
public ErrnoException(String functionName, int errno, Throwable cause) {
super(cause);
this.functionName = functionName;
this.errno = errno;
}
/**
* Converts the stashed function name and errno value to a human-readable string.
* We do this here rather than in the constructor so that callers only pay for
* this if they need it.
*/
@Override public String getMessage() {
String errnoName = OsConstants.errnoName(errno);
if (errnoName == null) {
errnoName = "errno " + errno;
}
String description = Libcore.os.strerror(errno);
return functionName + " failed: " + errnoName + " (" + description + ")";
}
/**
* Throws an {@link IOException} with a message based on {@link #getMessage()} and with this
* instance as the cause.
*
* <p>This method always terminates by throwing the exception. Callers can write
* {@code throw e.rethrowAsIOException()} to make that clear to the compiler.
*/
public IOException rethrowAsIOException() throws IOException {
IOException newException = new IOException(getMessage());
newException.initCause(this);
throw newException;
}
/**
* Throws a {@link SocketException} with a message based on {@link #getMessage()} and with this
* instance as the cause.
*
* <p>This method always terminates by throwing the exception. Callers can write
* {@code throw e.rethrowAsIOException()} to make that clear to the compiler.
*/
public SocketException rethrowAsSocketException() throws SocketException {
final SocketException newException = new SocketException(getMessage());
newException.initCause(this);
throw newException;
}
}

View file

@ -0,0 +1,53 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.system;
import android.annotation.TargetApi;
import java.io.FileDescriptor;
import java.io.InterruptedIOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;
import libcore.io.Libcore;
/**
* Access to low-level system functionality. Most of these are system calls. Most users will want
* to use higher-level APIs where available, but this class provides access to the underlying
* primitives used to implement the higher-level APIs.
*
* <p>The corresponding constants can be found in {@link OsConstants}.
*/
@SuppressWarnings("unused")
@TargetApi(21)
public final class Os {
private Os() {}
/**
* See <a href="http://man7.org/linux/man-pages/man2/write.2.html">write(2)</a>.
*/
public static int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException, InterruptedIOException {
try {
return Libcore.os.write(fd, buffer);
} catch (libcore.io.ErrnoException e) {
throw new ErrnoException("write", e.errno);
}
}
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.system;
import android.annotation.TargetApi;
@SuppressWarnings("unused")
@TargetApi(21)
public final class OsConstants {
private OsConstants() { }
public static final int EINTR = libcore.io.OsConstants.EINTR;
/**
* Returns the string name of an errno value.
* For example, "EACCES". See {@link Os#strerror} for human-readable errno descriptions.
*/
public static String errnoName(int errno) {
return libcore.io.OsConstants.errnoName(errno);
}
}

View file

@ -1,5 +1,6 @@
package com.genymobile.scrcpy;
import android.annotation.TargetApi;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
@ -13,6 +14,7 @@ public final class IO {
// not instantiable
}
@TargetApi(21) // Backported subset of android.system.
public static void writeFully(FileDescriptor fd, ByteBuffer from) throws IOException {
// ByteBuffer position is not updated as expected by Os.write() on old Android versions, so
// count the remaining bytes manually.

View file

@ -1 +1,2 @@
include ':server'
include ':libcore'