parent
cbd9850b57
commit
616724a762
@ -1,12 +1,19 @@ |
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" |
||||||
package="com.arialyy.aria.publiccomponent" > |
xmlns:tools="http://schemas.android.com/tools" |
||||||
|
package="com.arialyy.aria.publiccomponent"> |
||||||
|
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
||||||
|
|
||||||
<application> |
<application> |
||||||
|
|
||||||
<provider |
<provider |
||||||
android:authorities="${applicationId}.com.arialyy.aria.provider" |
android:name="androidx.startup.InitializationProvider" |
||||||
android:name="com.arialyy.aria.orm.DbContentProvider" |
android:authorities="${applicationId}.androidx-startup" |
||||||
android:exported="false" /> |
android:exported="false" |
||||||
|
tools:node="merge"> |
||||||
|
<!-- This entry makes ExampleLoggerInitializer discoverable. --> |
||||||
|
<meta-data android:name="com.arialyy.aria.core.DuaStartupProvider" |
||||||
|
android:value="androidx.startup" /> |
||||||
|
</provider> |
||||||
</application> |
</application> |
||||||
</manifest> |
</manifest> |
||||||
|
@ -0,0 +1,58 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||||
|
* |
||||||
|
* 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 com.arialyy.aria.core |
||||||
|
|
||||||
|
import android.content.Context |
||||||
|
import androidx.room.Room |
||||||
|
import androidx.room.RoomDatabase |
||||||
|
import androidx.room.RoomDatabase.Builder |
||||||
|
import androidx.startup.Initializer |
||||||
|
import com.arialyy.aria.core.config.AutoGenerateConstance |
||||||
|
import com.arialyy.aria.orm.DefaultDbProvider |
||||||
|
import com.arialyy.aria.util.ReflectionUtil |
||||||
|
|
||||||
|
class DuaStartupProvider : Initializer<Unit> { |
||||||
|
/** |
||||||
|
* Find a user-defined database |
||||||
|
*/ |
||||||
|
private fun findCustomDatabase(context: Context): Builder<RoomDatabase>? { |
||||||
|
try { |
||||||
|
val clazz = javaClass.classLoader.loadClass(AutoGenerateConstance.GenerateClassName) |
||||||
|
?: return null |
||||||
|
|
||||||
|
val obj = clazz.newInstance() |
||||||
|
|
||||||
|
val method = ReflectionUtil.getMethod(clazz, "generateDb", Context::class.java) ?: return null |
||||||
|
|
||||||
|
return method.invoke(obj, context) as Builder<RoomDatabase>? |
||||||
|
} catch (e: java.lang.Exception) { |
||||||
|
return null |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
override fun create(context: Context) { |
||||||
|
var customDb = findCustomDatabase(context) |
||||||
|
if (customDb == null) { |
||||||
|
customDb = DefaultDbProvider().generateDb(context) |
||||||
|
} |
||||||
|
customDb.build() |
||||||
|
// .addMigrations(MIGRATION_2_3(), MIGRATION_3_4()) |
||||||
|
} |
||||||
|
|
||||||
|
override fun dependencies(): MutableList<Class<out Initializer<*>>> { |
||||||
|
return mutableListOf() |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||||
|
* |
||||||
|
* 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 com.arialyy.aria.core.config |
||||||
|
|
||||||
|
/** |
||||||
|
* Automatically created configurations |
||||||
|
*/ |
||||||
|
object AutoGenerateConstance { |
||||||
|
|
||||||
|
const val GenerateClassName = "com.arialyy.aria.custom.Constance" |
||||||
|
|
||||||
|
const val DbClassKeyName = "roomBuilder" |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||||
|
* |
||||||
|
* 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 com.arialyy.aria.core.provider |
||||||
|
|
||||||
|
import android.content.Context |
||||||
|
import androidx.room.RoomDatabase |
||||||
|
|
||||||
|
interface IDbProvider { |
||||||
|
|
||||||
|
fun getDbName() = "duaDb" |
||||||
|
|
||||||
|
fun <T : RoomDatabase> generateDb(context: Context): RoomDatabase.Builder<T> |
||||||
|
} |
@ -0,0 +1,32 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||||
|
* |
||||||
|
* 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 com.arialyy.aria.orm |
||||||
|
|
||||||
|
import android.content.Context |
||||||
|
import androidx.room.Room |
||||||
|
import androidx.room.RoomDatabase |
||||||
|
import androidx.room.RoomDatabase.Builder |
||||||
|
import com.arialyy.aria.core.provider.IDbProvider |
||||||
|
|
||||||
|
class DefaultDbProvider : IDbProvider { |
||||||
|
|
||||||
|
override fun <T : RoomDatabase> generateDb(context: Context): Builder<T> { |
||||||
|
return Room.databaseBuilder( |
||||||
|
context, |
||||||
|
DuaDb::class.java, getDbName() |
||||||
|
) as Builder<T> |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||||
|
* |
||||||
|
* 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 com.arialyy.aria.orm |
||||||
|
|
||||||
|
import androidx.room.Database |
||||||
|
import androidx.room.RoomDatabase |
||||||
|
import com.arialyy.aria.orm.entiry.UEntity |
||||||
|
|
||||||
|
@Database( |
||||||
|
entities = [DbEntity::class, UEntity::class], |
||||||
|
version = 1 |
||||||
|
) |
||||||
|
abstract class DuaDb : RoomDatabase() { |
||||||
|
companion object { |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,41 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||||
|
* |
||||||
|
* 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 com.arialyy.aria.orm.entiry |
||||||
|
|
||||||
|
import androidx.room.Entity |
||||||
|
import androidx.room.Index |
||||||
|
import androidx.room.PrimaryKey |
||||||
|
|
||||||
|
/** |
||||||
|
* Download Entity |
||||||
|
*/ |
||||||
|
@Entity(tableName = "d_entity", indices = [Index(value = ["sourceUrl", "savePath"])]) |
||||||
|
data class DEntity( |
||||||
|
@PrimaryKey(autoGenerate = true) val dId: Int = 0, |
||||||
|
|
||||||
|
/** |
||||||
|
* file source url |
||||||
|
*/ |
||||||
|
val sourceUrl: String, |
||||||
|
/** |
||||||
|
* file save path |
||||||
|
*/ |
||||||
|
val savePath: String, |
||||||
|
/** |
||||||
|
* extended Information |
||||||
|
*/ |
||||||
|
var ext: String? = null |
||||||
|
) |
@ -0,0 +1,39 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) |
||||||
|
* |
||||||
|
* 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 com.arialyy.aria.orm.entiry |
||||||
|
|
||||||
|
import androidx.room.Entity |
||||||
|
import androidx.room.Index |
||||||
|
import androidx.room.PrimaryKey |
||||||
|
|
||||||
|
@Entity(tableName = "u_entity", indices = [Index(value = ["serverUrl", "filePath"])]) |
||||||
|
data class UEntity( |
||||||
|
@PrimaryKey(autoGenerate = true) val uId: Int = 0, |
||||||
|
/** |
||||||
|
* uploader server url |
||||||
|
*/ |
||||||
|
val serverUrl: String, |
||||||
|
|
||||||
|
/** |
||||||
|
* file path |
||||||
|
*/ |
||||||
|
val filePath: String, |
||||||
|
|
||||||
|
/** |
||||||
|
* extended Information |
||||||
|
*/ |
||||||
|
var ext: String? = null |
||||||
|
) |
@ -0,0 +1,82 @@ |
|||||||
|
package com.arialyy.aria.util; |
||||||
|
|
||||||
|
import java.lang.reflect.Field; |
||||||
|
import java.lang.reflect.Method; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2015/7/30. |
||||||
|
* 反射工具类 |
||||||
|
*/ |
||||||
|
public class ReflectionUtil { |
||||||
|
private static final String TAG = "ReflectionUtil"; |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取类里面的所在字段 |
||||||
|
*/ |
||||||
|
public static Field[] getFields(Class clazz) { |
||||||
|
Field[] fields = null; |
||||||
|
fields = clazz.getDeclaredFields(); |
||||||
|
if (fields == null || fields.length == 0) { |
||||||
|
Class superClazz = clazz.getSuperclass(); |
||||||
|
if (superClazz != null) { |
||||||
|
fields = getFields(superClazz); |
||||||
|
} |
||||||
|
} |
||||||
|
return fields; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取类里面的指定对象,如果该类没有则从父类查询 |
||||||
|
*/ |
||||||
|
public static Field getField(Class clazz, String name) { |
||||||
|
Field field = null; |
||||||
|
try { |
||||||
|
field = clazz.getDeclaredField(name); |
||||||
|
} catch (NoSuchFieldException e) { |
||||||
|
try { |
||||||
|
field = clazz.getField(name); |
||||||
|
} catch (NoSuchFieldException e1) { |
||||||
|
if (clazz.getSuperclass() == null) { |
||||||
|
return field; |
||||||
|
} else { |
||||||
|
field = getField(clazz.getSuperclass(), name); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
if (field != null) { |
||||||
|
field.setAccessible(true); |
||||||
|
} |
||||||
|
return field; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 利用递归找一个类的指定方法,如果找不到,去父亲里面找直到最上层Object对象为止。 |
||||||
|
* |
||||||
|
* @param clazz 目标类 |
||||||
|
* @param methodName 方法名 |
||||||
|
* @param params 方法参数类型数组 |
||||||
|
* @return 方法对象 |
||||||
|
*/ |
||||||
|
public static Method getMethod(Class clazz, String methodName, final Class<?>... params) { |
||||||
|
Method method = null; |
||||||
|
try { |
||||||
|
method = clazz.getDeclaredMethod(methodName, params); |
||||||
|
} catch (NoSuchMethodException e) { |
||||||
|
try { |
||||||
|
method = clazz.getMethod(methodName, params); |
||||||
|
} catch (NoSuchMethodException ex) { |
||||||
|
if (clazz.getSuperclass() == null) { |
||||||
|
ALog.e(TAG, "无法找到" + methodName + "方法"); |
||||||
|
ALog.e(TAG, ALog.getExceptionString(e)); |
||||||
|
return method; |
||||||
|
} else { |
||||||
|
method = getMethod(clazz.getSuperclass(), methodName, params); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
if (method != null) { |
||||||
|
method.setAccessible(true); |
||||||
|
} |
||||||
|
return method; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue