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: components:
- tools - tools
- platform-tools - platform-tools
- build-tools-27.0.3 - build-tools-28.0.3
- android-27 - android-28
- extra-android-m2repository - 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,6 +15,7 @@
*/ */
buildscript { buildscript {
ext.kotlin_version = '1.3.21'
repositories { repositories {
jcenter() jcenter()
google() google()
@ -22,19 +23,20 @@ buildscript {
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.1.3' classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
} }
} }
allprojects { allprojects {
apply plugin: 'idea' 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 { subprojects {
repositories { repositories {

View File

@ -15,6 +15,8 @@
*/ */
apply plugin: 'com.android.library' apply plugin: 'com.android.library'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
String getMyVersionName() { String getMyVersionName() {
def stdout = new ByteArrayOutputStream() def stdout = new ByteArrayOutputStream()
@ -43,6 +45,10 @@ android {
targetSdkVersion androidTargetSdk() targetSdkVersion androidTargetSdk()
} }
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
compileOptions { compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8
@ -52,4 +58,5 @@ android {
dependencies { dependencies {
api 'com.android.support:support-v4:25.3.1' api 'com.android.support:support-v4:25.3.1'
api 'org.microg:safe-parcel:1.5.0' 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; package com.google.android.gms.dynamic;
import android.os.IBinder; import android.os.IBinder;
import android.support.annotation.Nullable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -27,6 +28,7 @@ public class ObjectWrapper<T> extends IObjectWrapper.Stub {
this.t = t; this.t = t;
} }
@Nullable
public static Object unwrap(IObjectWrapper obj) { public static Object unwrap(IObjectWrapper obj) {
if (obj == null) { if (obj == null) {
return null; return null;
@ -43,8 +45,7 @@ public class ObjectWrapper<T> extends IObjectWrapper.Stub {
if (!field.isAccessible()) { if (!field.isAccessible()) {
field.setAccessible(true); field.setAccessible(true);
try { try {
Object wrapped = field.get(binder); return field.get(binder);
return wrapped;
} catch (NullPointerException localNullPointerException) { } catch (NullPointerException localNullPointerException) {
throw new IllegalArgumentException("Binder object is null.", throw new IllegalArgumentException("Binder object is null.",
localNullPointerException); 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) { public static <T> ObjectWrapper<T> wrap(T t) {
return new ObjectWrapper<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)