Add Kotlin feature for ObjectWrapper

This commit is contained in:
Marvin W 2019-05-26 21:10:10 +02:00
parent 66285c73e7
commit 7197b8320b
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
5 changed files with 50 additions and 11 deletions

View File

@ -9,9 +9,7 @@ android:
components:
- tools
- platform-tools
- build-tools-27.0.3
- android-27
- build-tools-28.0.3
- android-28
- extra-android-m2repository
before_install:
- yes | sdkmanager "platforms;android-27"

View File

@ -1,5 +1,5 @@
/*
* Copyright 2013-2016 microG Project Team
* Copyright 2013-2019 microG Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,6 +15,7 @@
*/
buildscript {
ext.kotlin_version = '1.3.21'
repositories {
jcenter()
google()
@ -22,19 +23,20 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
apply plugin: 'idea'
ext.androidBuildVersionTools = "27.0.3"
ext.androidBuildVersionTools = "28.0.3"
}
def androidCompileSdk() { return 27 }
def androidCompileSdk() { return 28 }
def androidTargetSdk() { return 27 }
def androidTargetSdk() { return 28 }
def androidMinSdk() { return 9 }
def androidMinSdk() { return 14 }
subprojects {
repositories {

View File

@ -15,6 +15,8 @@
*/
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
String getMyVersionName() {
def stdout = new ByteArrayOutputStream()
@ -43,6 +45,10 @@ android {
targetSdkVersion androidTargetSdk()
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
@ -52,4 +58,5 @@ android {
dependencies {
api 'com.android.support:support-v4:25.3.1'
api 'org.microg:safe-parcel:1.5.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

View File

@ -17,6 +17,7 @@
package com.google.android.gms.dynamic;
import android.os.IBinder;
import android.support.annotation.Nullable;
import java.lang.reflect.Field;
@ -27,6 +28,7 @@ public class ObjectWrapper<T> extends IObjectWrapper.Stub {
this.t = t;
}
@Nullable
public static Object unwrap(IObjectWrapper obj) {
if (obj == null) {
return null;
@ -43,8 +45,7 @@ public class ObjectWrapper<T> extends IObjectWrapper.Stub {
if (!field.isAccessible()) {
field.setAccessible(true);
try {
Object wrapped = field.get(binder);
return wrapped;
return field.get(binder);
} catch (NullPointerException localNullPointerException) {
throw new IllegalArgumentException("Binder object is null.",
localNullPointerException);
@ -60,6 +61,15 @@ public class ObjectWrapper<T> extends IObjectWrapper.Stub {
}
}
@Nullable
public static <T> T unwrapTyped(IObjectWrapper obj, Class<T> clazz) {
try {
return clazz.cast(unwrap(obj));
} catch (ClassCastException e) {
return null;
}
}
public static <T> ObjectWrapper<T> wrap(T t) {
return new ObjectWrapper<T>(t);
}

View File

@ -0,0 +1,22 @@
/*
* Copyright (C) 2019 microG Project Team
*
* 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 org.microg.gms.kotlin
import com.google.android.gms.dynamic.IObjectWrapper
import com.google.android.gms.dynamic.ObjectWrapper
inline fun <reified T> IObjectWrapper?.unwrap(): T? = ObjectWrapper.unwrapTyped(this, T::class.java)