├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── copyright │ ├── profiles_settings.xml │ └── xuexiang.xml ├── gradle.xml ├── markdown-navigator.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml └── runConfigurations.xml ├── JitPackUpload.gradle ├── README.md ├── apk ├── remote_demo_1.0.apk └── xipc_demo_1.0.apk ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── xuexiang │ │ └── xipcdemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── aidl │ │ └── com │ │ │ └── xuexiang │ │ │ └── aidl │ │ │ └── ICompute.aidl │ ├── java │ │ └── com │ │ │ └── xuexiang │ │ │ ├── aidl │ │ │ └── AIDLService.java │ │ │ └── xipcdemo │ │ │ ├── App.java │ │ │ ├── activity │ │ │ ├── MainActivity.java │ │ │ └── SecondProcessActivity.java │ │ │ ├── fragment │ │ │ ├── AIDLProcessFragment.java │ │ │ ├── DifferentProcessFragment.java │ │ │ ├── MainFragment.java │ │ │ └── SameProcessFragment.java │ │ │ └── service │ │ │ ├── IComputeService.java │ │ │ ├── IFileUtils.java │ │ │ ├── ILoadingTask.java │ │ │ ├── IUserManager.java │ │ │ ├── LoadingCallback.java │ │ │ └── impl │ │ │ ├── ComputeService.java │ │ │ ├── FileUtils.java │ │ │ ├── LoadingTask.java │ │ │ └── UserManager.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── layout_aidl_test.xml │ │ └── layout_ipc_test.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── xuexiang │ └── xipcdemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── img ├── 1.png └── demo.gif ├── principle.md ├── remote_demo ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── xuexiang │ │ └── remotedemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── aidl │ │ └── com │ │ │ └── xuexiang │ │ │ └── aidl │ │ │ └── ICompute.aidl │ ├── java │ │ └── com │ │ │ └── xuexiang │ │ │ └── remotedemo │ │ │ ├── App.java │ │ │ ├── activity │ │ │ └── MainActivity.java │ │ │ ├── fragment │ │ │ ├── AIDLProcessFragment.java │ │ │ ├── CrossApplicationProcessFragment.java │ │ │ └── MainFragment.java │ │ │ └── service │ │ │ ├── IComputeService.java │ │ │ ├── IFileUtils.java │ │ │ ├── ILoadingTask.java │ │ │ ├── IUserManager.java │ │ │ └── LoadingCallback.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── layout_aidl_test.xml │ │ └── layout_ipc_test.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── xuexiang │ └── remotedemo │ └── ExampleUnitTest.java ├── settings.gradle ├── versions.gradle └── xipc-runtime ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src └── main ├── AndroidManifest.xml └── java └── com └── xuexiang └── xipc ├── XIPC.java ├── annotation ├── Background.java ├── ClassName.java ├── MethodName.java ├── Singleton.java ├── WeakRef.java └── WithinProcess.java ├── core ├── channel │ ├── Channel.java │ ├── IIPCService.java │ ├── IIPCServiceCallback.java │ ├── IPCCallbackInvocationHandler.java │ ├── IPCInvocationHandler.java │ ├── IPCListener.java │ └── IPCService.java ├── entity │ ├── CallbackMail.java │ ├── Mail.java │ └── Reply.java ├── receiver │ ├── IReceiver.java │ ├── Receiver.java │ ├── ReceiverDesignator.java │ └── impl │ │ ├── InstanceCreatingReceiver.java │ │ ├── InstanceGettingReceiver.java │ │ ├── ObjectReceiver.java │ │ ├── UtilityGettingReceiver.java │ │ └── UtilityReceiver.java ├── sender │ ├── ISender.java │ ├── Sender.java │ └── impl │ │ ├── InstanceCreatingSender.java │ │ ├── InstanceGettingSender.java │ │ ├── ObjectSender.java │ │ ├── SenderDesignator.java │ │ └── UtilityGettingSender.java └── wrapper │ ├── BaseWrapper.java │ ├── MethodWrapper.java │ ├── ObjectWrapper.java │ ├── ParameterWrapper.java │ └── TypeWrapper.java ├── exception ├── ErrorCodes.java └── IPCException.java ├── logs ├── ILogger.java ├── IPCLog.java └── LogcatLogger.java └── util ├── CallbackManager.java ├── ClassUtils.java ├── IPCCallbackGc.java ├── IPCGc.java ├── IPCPoolExecutor.java ├── ObjectCenter.java ├── SerializeUtils.java ├── TimeStampGenerator.java ├── Triple.java ├── TypeCenter.java └── TypeUtils.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /keystores 4 | /local.properties 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | .DS_Store 9 | /build 10 | /captures 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/copyright/xuexiang.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /JitPackUpload.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.github.dcendents.android-maven' 2 | 3 | // 指定group,com.github.<用户名>,这里我默认填写的是我的github账号,请换成你自己的。 4 | group='com.github.xuexiangjys' 5 | 6 | //--------------------------------------------- 7 | 8 | 9 | // 指定编码 10 | tasks.withType(JavaCompile) { 11 | options.encoding = "UTF-8" 12 | } 13 | 14 | tasks.withType(Javadoc) { 15 | options.encoding = 'UTF-8' 16 | } 17 | 18 | if (project.hasProperty("android")) { // Android libraries 19 | task sourcesJar(type: Jar) { 20 | classifier = 'sources' 21 | from android.sourceSets.main.java.srcDirs 22 | } 23 | 24 | task javadoc(type: Javadoc) { 25 | failOnError false 26 | source = android.sourceSets.main.java.srcDirs 27 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 28 | } 29 | } else { // Java libraries 30 | task sourcesJar(type: Jar, dependsOn: classes) { 31 | classifier = 'sources' 32 | from sourceSets.main.allSource 33 | } 34 | } 35 | 36 | javadoc { 37 | options { 38 | encoding "UTF-8" 39 | charSet 'UTF-8' 40 | author true 41 | version true 42 | links "http://docs.oracle.com/javase/7/docs/api" 43 | } 44 | } 45 | 46 | // 制作文档(Javadoc) 47 | task javadocJar(type: Jar, dependsOn: javadoc) { 48 | classifier = 'javadoc' 49 | from javadoc.destinationDir 50 | } 51 | 52 | artifacts { 53 | archives javadocJar 54 | archives sourcesJar 55 | } -------------------------------------------------------------------------------- /apk/remote_demo_1.0.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/apk/remote_demo_1.0.apk -------------------------------------------------------------------------------- /apk/xipc_demo_1.0.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/apk/xipc_demo_1.0.apk -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.xuexiang.xaop' //引用xaop插件 3 | 4 | android { 5 | compileSdkVersion build_versions.target_sdk 6 | buildToolsVersion build_versions.build_tools 7 | 8 | defaultConfig { 9 | applicationId "com.xuexiang.xipcdemo" 10 | minSdkVersion 19 11 | targetSdkVersion build_versions.target_sdk 12 | versionCode 1 13 | versionName "1.0" 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | } 16 | 17 | if (isNeedPackage.toBoolean()) { 18 | signingConfigs { 19 | release { 20 | storeFile file(app_release.storeFile) 21 | storePassword app_release.storePassword 22 | keyAlias app_release.keyAlias 23 | keyPassword app_release.keyPassword 24 | } 25 | } 26 | } 27 | 28 | buildTypes { 29 | release { 30 | minifyEnabled true 31 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 32 | if (isNeedPackage.toBoolean()) { 33 | signingConfig signingConfigs.release 34 | } 35 | } 36 | } 37 | 38 | if (isNeedPackage.toBoolean()) { 39 | applicationVariants.all { variant -> 40 | variant.outputs.all { 41 | if (variant.buildType.name.equals('release')) { 42 | outputFileName = "xipc_demo_${defaultConfig.versionName}.apk" 43 | } 44 | } 45 | } 46 | } 47 | 48 | lintOptions { 49 | abortOnError false 50 | } 51 | } 52 | 53 | dependencies { 54 | implementation fileTree(include: ['*.jar'], dir: 'libs') 55 | implementation deps.support.app_compat 56 | testImplementation deps.junit 57 | androidTestImplementation deps.runner 58 | androidTestImplementation deps.espresso.core 59 | implementation deps.support.recyclerview 60 | implementation 'com.github.xuexiangjys.XUtil:xutil-core:1.1.4' 61 | implementation 'com.github.xuexiangjys.XAOP:xaop-runtime:1.0.1' 62 | //添加依赖 63 | //XPage 64 | implementation 'com.github.xuexiangjys.XPage:xpage-lib:2.2.2' 65 | annotationProcessor 'com.github.xuexiangjys.XPage:xpage-compiler:2.2.2' 66 | //butterKnife的sdk 67 | implementation 'com.jakewharton:butterknife:8.8.1' 68 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' 69 | //如果开启了内存泄漏监测leak,就需要加上这个依赖 70 | debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4' 71 | releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' 72 | testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' 73 | 74 | 75 | implementation 'com.github.xuexiangjys:XIPC:1.0.0' 76 | // implementation project(':xipc-runtime') 77 | } 78 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/xuexiang/xipcdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.xuexiang.xipcdemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.xuexiang.xipcdemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 25 | 26 | 27 | 30 | 31 | 32 | 33 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /app/src/main/aidl/com/xuexiang/aidl/ICompute.aidl: -------------------------------------------------------------------------------- 1 | // ICompute.aidl 2 | package com.xuexiang.aidl; 3 | 4 | // Declare any non-default types here with import statements 5 | 6 | interface ICompute { 7 | /** 8 | * Demonstrates some basic types that you can use as parameters 9 | * and return values in AIDL. 10 | */ 11 | float calculate(float value1, String symbol, float value2); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/aidl/AIDLService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.aidl; 18 | 19 | import android.app.Service; 20 | import android.content.Intent; 21 | import android.os.IBinder; 22 | import android.os.RemoteException; 23 | import android.support.annotation.Nullable; 24 | 25 | import com.xuexiang.xipcdemo.service.impl.ComputeService; 26 | 27 | /** 28 | * aidl服务 29 | * 30 | * @author xuexiang 31 | * @since 2018/9/24 下午1:49 32 | */ 33 | public class AIDLService extends Service { 34 | 35 | @Nullable 36 | @Override 37 | public IBinder onBind(Intent intent) { 38 | return new RemoteBinder(); 39 | } 40 | 41 | @Override 42 | public boolean onUnbind(Intent intent) { 43 | return super.onUnbind(intent); 44 | } 45 | 46 | 47 | /** 48 | * 远程绑定服务 49 | */ 50 | public class RemoteBinder extends ICompute.Stub { 51 | 52 | private ComputeService mComputeService; 53 | 54 | public RemoteBinder() { 55 | mComputeService = new ComputeService(); 56 | } 57 | 58 | @Override 59 | public float calculate(float value1, String symbol, float value2) throws RemoteException { 60 | return mComputeService.calculate(value1, symbol, value2); 61 | } 62 | 63 | 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/App.java: -------------------------------------------------------------------------------- 1 | package com.xuexiang.xipcdemo; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | import android.graphics.Bitmap; 6 | 7 | import com.xuexiang.xipc.XIPC; 8 | import com.xuexiang.xipcdemo.service.LoadingCallback; 9 | import com.xuexiang.xipcdemo.service.impl.ComputeService; 10 | import com.xuexiang.xipcdemo.service.impl.FileUtils; 11 | import com.xuexiang.xipcdemo.service.impl.LoadingTask; 12 | import com.xuexiang.xipcdemo.service.impl.UserManager; 13 | import com.xuexiang.xpage.AppPageConfig; 14 | import com.xuexiang.xpage.PageConfig; 15 | import com.xuexiang.xpage.PageConfiguration; 16 | import com.xuexiang.xpage.model.PageInfo; 17 | import com.xuexiang.xutil.XUtil; 18 | 19 | import java.util.List; 20 | 21 | /** 22 | * @author xuexiang 23 | * @since 2018/9/14 下午2:47 24 | */ 25 | public class App extends Application { 26 | 27 | @Override 28 | public void onCreate() { 29 | super.onCreate(); 30 | 31 | XUtil.init(this); 32 | XUtil.debug(true); 33 | 34 | initXIPC(); 35 | 36 | PageConfig.getInstance().setPageConfiguration(new PageConfiguration() { //页面注册 37 | @Override 38 | public List registerPages(Context context) { 39 | return AppPageConfig.getInstance().getPages(); //自动注册页面 40 | } 41 | }).debug("PageLog").enableWatcher(true).init(this); 42 | } 43 | 44 | private void initXIPC() { 45 | XIPC.init(this); 46 | XIPC.debug(BuildConfig.DEBUG); 47 | 48 | XIPC.register(UserManager.class); 49 | XIPC.register(LoadingTask.class); 50 | XIPC.register(FileUtils.class); 51 | XIPC.register(LoadingCallback.class); 52 | XIPC.register(ComputeService.class); 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.xuexiang.xipcdemo.activity; 2 | 3 | import android.os.Bundle; 4 | 5 | import com.xuexiang.xipcdemo.fragment.MainFragment; 6 | import com.xuexiang.xpage.base.XPageActivity; 7 | 8 | public class MainActivity extends XPageActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | 14 | openPage(MainFragment.class); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/activity/SecondProcessActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.activity; 18 | 19 | import android.os.Bundle; 20 | 21 | import com.xuexiang.xipcdemo.fragment.DifferentProcessFragment; 22 | import com.xuexiang.xpage.base.XPageActivity; 23 | 24 | /** 25 | * @author xuexiang 26 | * @since 2018/9/18 下午12:05 27 | */ 28 | public class SecondProcessActivity extends XPageActivity { 29 | 30 | @Override 31 | protected void onCreate(Bundle savedInstanceState) { 32 | super.onCreate(savedInstanceState); 33 | 34 | openPage(DifferentProcessFragment.class); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/fragment/AIDLProcessFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.fragment; 18 | 19 | import android.content.ComponentName; 20 | import android.content.Context; 21 | import android.content.Intent; 22 | import android.content.ServiceConnection; 23 | import android.os.IBinder; 24 | import android.os.RemoteException; 25 | 26 | import com.xuexiang.aidl.AIDLService; 27 | import com.xuexiang.aidl.ICompute; 28 | import com.xuexiang.xipcdemo.R; 29 | import com.xuexiang.xpage.annotation.Page; 30 | import com.xuexiang.xpage.base.XPageFragment; 31 | import com.xuexiang.xutil.tip.ToastUtils; 32 | 33 | import butterknife.OnClick; 34 | 35 | /** 36 | * @author xuexiang 37 | * @since 2018/9/24 下午1:39 38 | */ 39 | @Page(name = "原生AIDL进程间通信") 40 | public class AIDLProcessFragment extends XPageFragment { 41 | 42 | private ICompute mICompute; 43 | 44 | @Override 45 | protected int getLayoutId() { 46 | return R.layout.layout_aidl_test; 47 | } 48 | 49 | @Override 50 | protected void initArgs() { 51 | super.initArgs(); 52 | getContext().bindService(new Intent(getContext(), AIDLService.class), mServiceConnection, Context.BIND_AUTO_CREATE); 53 | } 54 | 55 | @Override 56 | protected void initViews() { 57 | 58 | } 59 | 60 | @Override 61 | protected void initListeners() { 62 | 63 | } 64 | 65 | @OnClick(R.id.btn_compute) 66 | public void onViewClicked() { 67 | try { 68 | ToastUtils.toast("3+4=" + mICompute.calculate(3 , "+", 4)); 69 | } catch (RemoteException e) { 70 | e.printStackTrace(); 71 | } 72 | } 73 | 74 | @Override 75 | public void onDestroyView() { 76 | if (mServiceConnection != null) { 77 | getContext().unbindService(mServiceConnection); 78 | } 79 | super.onDestroyView(); 80 | } 81 | 82 | private ServiceConnection mServiceConnection = new ServiceConnection() { 83 | 84 | @Override 85 | public void onServiceDisconnected(ComponentName name) { 86 | mICompute = null; 87 | } 88 | @Override 89 | public void onServiceConnected(ComponentName name, IBinder service) { 90 | mICompute = ICompute.Stub.asInterface(service); 91 | } 92 | }; 93 | } 94 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/fragment/DifferentProcessFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.fragment; 18 | 19 | import android.view.View; 20 | import android.widget.ProgressBar; 21 | 22 | import com.xuexiang.xaop.annotation.SingleClick; 23 | import com.xuexiang.xipc.XIPC; 24 | import com.xuexiang.xipcdemo.R; 25 | import com.xuexiang.xipcdemo.service.IComputeService; 26 | import com.xuexiang.xipcdemo.service.IFileUtils; 27 | import com.xuexiang.xipcdemo.service.ILoadingTask; 28 | import com.xuexiang.xipcdemo.service.IUserManager; 29 | import com.xuexiang.xipcdemo.service.LoadingCallback; 30 | import com.xuexiang.xipcdemo.service.impl.UserManager; 31 | import com.xuexiang.xpage.annotation.Page; 32 | import com.xuexiang.xpage.base.XPageFragment; 33 | import com.xuexiang.xutil.tip.ToastUtils; 34 | 35 | import butterknife.BindView; 36 | import butterknife.OnClick; 37 | 38 | /** 39 | * @author xuexiang 40 | * @since 2018/9/18 上午10:11 41 | */ 42 | @Page(name = "不同进程间的通信") 43 | public class DifferentProcessFragment extends XPageFragment { 44 | 45 | @BindView(R.id.progress_bar) 46 | ProgressBar progressBar; 47 | 48 | @Override 49 | protected void initArgs() { 50 | super.initArgs(); 51 | XIPC.connect(getContext()); 52 | } 53 | 54 | @Override 55 | protected int getLayoutId() { 56 | return R.layout.layout_ipc_test; 57 | } 58 | 59 | @Override 60 | protected void initViews() { 61 | 62 | } 63 | 64 | @Override 65 | protected void initListeners() { 66 | 67 | } 68 | 69 | @SingleClick 70 | @OnClick({R.id.btn_compute, R.id.btn_download, R.id.btn_get_user, R.id.btn_util_test}) 71 | public void onViewClicked(View view) { 72 | switch (view.getId()) { 73 | case R.id.btn_compute: 74 | IComputeService computeService = XIPC.getService(IComputeService.class); 75 | ToastUtils.toast("3*4=" + computeService.calculate(3 , "*", 4)); 76 | break; 77 | case R.id.btn_download: 78 | ILoadingTask loadingTask = XIPC.getService(ILoadingTask.class, "pic.png"); 79 | loadingTask.start(new LoadingCallback() { 80 | @Override 81 | public void callback(int progress) { 82 | progressBar.setProgress(progress); 83 | } 84 | }); 85 | break; 86 | case R.id.btn_get_user: 87 | IUserManager userManager = XIPC.getInstance(IUserManager.class); 88 | ToastUtils.toast("不使用XIPC获取单例的内容:" + UserManager.getInstance().getUser() + "\r\n" + 89 | "使用XIPC获取单例的内容:" + userManager.getUser()); 90 | break; 91 | case R.id.btn_util_test: 92 | IFileUtils fileUtils = XIPC.getUtilityClass(IFileUtils.class); 93 | ToastUtils.toast("应用目录:" + fileUtils.getExternalCacheDir(getContext())); 94 | break; 95 | } 96 | } 97 | 98 | @Override 99 | public void onDestroyView() { 100 | XIPC.disconnect(getContext()); 101 | super.onDestroyView(); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/fragment/MainFragment.java: -------------------------------------------------------------------------------- 1 | package com.xuexiang.xipcdemo.fragment; 2 | 3 | import android.view.KeyEvent; 4 | import android.view.View; 5 | 6 | import com.xuexiang.xipcdemo.activity.SecondProcessActivity; 7 | import com.xuexiang.xipcdemo.service.impl.UserManager; 8 | import com.xuexiang.xpage.annotation.Page; 9 | import com.xuexiang.xpage.base.XPageContainerListFragment; 10 | import com.xuexiang.xpage.base.XPageSimpleListFragment; 11 | import com.xuexiang.xpage.utils.TitleBar; 12 | import com.xuexiang.xutil.app.ActivityUtils; 13 | import com.xuexiang.xutil.common.ClickUtils; 14 | 15 | import java.util.List; 16 | 17 | /** 18 | * @author xuexiang 19 | * @since 2018/9/14 下午2:50 20 | */ 21 | @Page(name = "XIPC 进程间通信框架") 22 | public class MainFragment extends XPageSimpleListFragment { 23 | 24 | @Override 25 | protected List initSimpleData(List lists) { 26 | lists.add("同进程通信"); 27 | lists.add("不同进程间的通信"); 28 | lists.add("原生AIDL进程间通信"); 29 | return lists; 30 | } 31 | 32 | @Override 33 | protected void initArgs() { 34 | super.initArgs(); 35 | UserManager.getInstance().setUser("我的名字改了!");//修改一下单例的内容 36 | } 37 | 38 | @Override 39 | protected void onItemClick(int position) { 40 | switch(position) { 41 | case 0: 42 | openPage(SameProcessFragment.class); 43 | break; 44 | case 1: 45 | ActivityUtils.startActivity(SecondProcessActivity.class); 46 | break; 47 | case 2: 48 | openPage(AIDLProcessFragment.class); 49 | break; 50 | default: 51 | break; 52 | } 53 | } 54 | 55 | @Override 56 | protected TitleBar initTitleBar() { 57 | return super.initTitleBar().setLeftClickListener(new View.OnClickListener() { 58 | @Override 59 | public void onClick(View view) { 60 | ClickUtils.exitBy2Click(); 61 | } 62 | }); 63 | } 64 | 65 | 66 | /** 67 | * 菜单、返回键响应 68 | */ 69 | @Override 70 | public boolean onKeyDown(int keyCode, KeyEvent event) { 71 | if (keyCode == KeyEvent.KEYCODE_BACK) { 72 | ClickUtils.exitBy2Click(); 73 | } 74 | return true; 75 | } 76 | 77 | 78 | 79 | } 80 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/fragment/SameProcessFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.fragment; 18 | 19 | import android.view.View; 20 | import android.widget.ProgressBar; 21 | 22 | import com.xuexiang.xaop.annotation.SingleClick; 23 | import com.xuexiang.xipc.XIPC; 24 | import com.xuexiang.xipcdemo.R; 25 | import com.xuexiang.xipcdemo.service.IComputeService; 26 | import com.xuexiang.xipcdemo.service.IFileUtils; 27 | import com.xuexiang.xipcdemo.service.ILoadingTask; 28 | import com.xuexiang.xipcdemo.service.IUserManager; 29 | import com.xuexiang.xipcdemo.service.LoadingCallback; 30 | import com.xuexiang.xpage.annotation.Page; 31 | import com.xuexiang.xpage.base.XPageFragment; 32 | import com.xuexiang.xutil.tip.ToastUtils; 33 | 34 | import butterknife.BindView; 35 | import butterknife.OnClick; 36 | 37 | /** 38 | * @author xuexiang 39 | * @since 2018/9/18 上午10:03 40 | */ 41 | @Page(name = "同进程通信") 42 | public class SameProcessFragment extends XPageFragment { 43 | 44 | @BindView(R.id.progress_bar) 45 | ProgressBar progressBar; 46 | 47 | @Override 48 | protected void initArgs() { 49 | super.initArgs(); 50 | XIPC.connect(getContext()); 51 | } 52 | 53 | @Override 54 | protected int getLayoutId() { 55 | return R.layout.layout_ipc_test; 56 | } 57 | 58 | @Override 59 | protected void initViews() { 60 | 61 | } 62 | 63 | @Override 64 | protected void initListeners() { 65 | 66 | } 67 | 68 | @SingleClick 69 | @OnClick({R.id.btn_compute, R.id.btn_download, R.id.btn_get_user, R.id.btn_util_test}) 70 | public void onViewClicked(View view) { 71 | switch (view.getId()) { 72 | case R.id.btn_compute: 73 | IComputeService computeService = XIPC.getService(IComputeService.class); 74 | ToastUtils.toast("3*4=" + computeService.calculate(3 , "*", 4)); 75 | break; 76 | case R.id.btn_download: 77 | ILoadingTask loadingTask = XIPC.getService(ILoadingTask.class, "pic.png"); 78 | loadingTask.start(new LoadingCallback() { 79 | @Override 80 | public void callback(int progress) { 81 | progressBar.setProgress(progress); 82 | } 83 | }); 84 | break; 85 | case R.id.btn_get_user: 86 | IUserManager userManager = XIPC.getInstance(IUserManager.class); 87 | ToastUtils.toast(userManager.getUser()); 88 | break; 89 | case R.id.btn_util_test: 90 | IFileUtils fileUtils = XIPC.getUtilityClass(IFileUtils.class); 91 | ToastUtils.toast("应用目录:" + fileUtils.getExternalCacheDir(getContext())); 92 | break; 93 | } 94 | } 95 | 96 | @Override 97 | public void onDestroyView() { 98 | XIPC.disconnect(getContext()); 99 | super.onDestroyView(); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/service/IComputeService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.service; 18 | 19 | import com.xuexiang.xipc.annotation.ClassName; 20 | import com.xuexiang.xipc.annotation.MethodName; 21 | 22 | /** 23 | * 服务接口测试 24 | * 25 | * @author xuexiang 26 | * @since 2018/9/18 上午10:26 27 | */ 28 | @ClassName("ComputeService") 29 | public interface IComputeService { 30 | 31 | /** 32 | * 计算 33 | * 34 | * @param value1 值1 35 | * @param symbol 算数符号 36 | * @param value2 值2 37 | * @return 38 | */ 39 | @MethodName("calculate") 40 | float calculate(float value1, String symbol, float value2); 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/service/IFileUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.service; 18 | 19 | import android.content.Context; 20 | 21 | import com.xuexiang.xipc.annotation.ClassName; 22 | 23 | /** 24 | * 工具类测试 25 | * 26 | * @author xuexiang 27 | * @since 2018/9/18 上午9:59 28 | */ 29 | @ClassName("FileUtils") 30 | public interface IFileUtils { 31 | 32 | String getExternalCacheDir(Context context); 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/service/ILoadingTask.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.service; 18 | 19 | import com.xuexiang.xipc.annotation.ClassName; 20 | import com.xuexiang.xipc.annotation.MethodName; 21 | import com.xuexiang.xipc.annotation.WeakRef; 22 | 23 | /** 24 | * 服务接口测试 25 | * 26 | * @author xuexiang 27 | * @since 2018/9/18 上午9:58 28 | */ 29 | @ClassName("LoadingTask") 30 | public interface ILoadingTask { 31 | 32 | @MethodName("start") 33 | void start(@WeakRef LoadingCallback loadingCallback); 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/service/IUserManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.service; 18 | 19 | import com.xuexiang.xipc.annotation.ClassName; 20 | import com.xuexiang.xipc.annotation.MethodName; 21 | 22 | /** 23 | * 测试单例的获取 24 | * 25 | * @author xuexiang 26 | * @since 2018/9/18 上午9:56 27 | */ 28 | @ClassName("UserManager") 29 | public interface IUserManager { 30 | 31 | @MethodName("getUser") 32 | String getUser(); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/service/LoadingCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.service; 18 | 19 | import com.xuexiang.xipc.annotation.ClassName; 20 | 21 | /** 22 | * 定义的回调接口 23 | * 24 | * @author xuexiang 25 | * @since 2018/9/18 上午9:51 26 | */ 27 | @ClassName("LoadingCallback") 28 | public interface LoadingCallback { 29 | 30 | void callback(int progress); 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/service/impl/ComputeService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.service.impl; 18 | 19 | import com.xuexiang.xipc.annotation.ClassName; 20 | import com.xuexiang.xipc.annotation.MethodName; 21 | import com.xuexiang.xipcdemo.service.IComputeService; 22 | 23 | /** 24 | * 服务接口测试 25 | * @author xuexiang 26 | * @since 2018/9/18 上午10:33 27 | */ 28 | @ClassName("ComputeService") 29 | public class ComputeService implements IComputeService { 30 | 31 | @Override 32 | @MethodName("calculate") 33 | public float calculate(float value1, String symbol, float value2) { 34 | float result; 35 | switch(symbol) { 36 | case "+": 37 | result = value1 + value2; 38 | break; 39 | case "-": 40 | result = value1 - value2; 41 | break; 42 | case "*": 43 | result = value1 * value2; 44 | break; 45 | case "/": 46 | result = value1 / value2; 47 | break; 48 | default: 49 | result = value1 + value2; 50 | break; 51 | } 52 | return result; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/service/impl/FileUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.service.impl; 18 | 19 | import android.content.Context; 20 | 21 | import com.xuexiang.xipc.annotation.ClassName; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * 工具类测试 27 | * 28 | * @author xuexiang 29 | * @since 2018/9/18 上午10:00 30 | */ 31 | @ClassName("FileUtils") 32 | public class FileUtils { 33 | 34 | public static String getExternalCacheDir(Context context) { 35 | try { 36 | return context.getExternalCacheDir().getCanonicalPath(); 37 | } catch (IOException e) { 38 | return null; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/service/impl/LoadingTask.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.service.impl; 18 | 19 | import com.xuexiang.xipc.annotation.ClassName; 20 | import com.xuexiang.xipc.annotation.MethodName; 21 | import com.xuexiang.xipcdemo.service.ILoadingTask; 22 | import com.xuexiang.xipcdemo.service.LoadingCallback; 23 | 24 | import java.util.Timer; 25 | import java.util.TimerTask; 26 | 27 | /** 28 | * 测试接口回调、服务接口 29 | * 30 | * @author xuexiang 31 | * @since 2018/9/18 上午9:50 32 | */ 33 | @ClassName("LoadingTask") 34 | public class LoadingTask implements ILoadingTask { 35 | 36 | public LoadingTask(String url) { 37 | 38 | } 39 | 40 | @MethodName("start") 41 | @Override 42 | public void start(final LoadingCallback loadingCallback) { 43 | final Timer timer = new Timer(); 44 | timer.schedule(new TimerTask() { 45 | int time = 0; 46 | @Override 47 | public void run() { 48 | time += 10; 49 | if (time > 100) { 50 | time = 100; 51 | } 52 | loadingCallback.callback(time); 53 | if (time == 100) { 54 | timer.cancel(); 55 | } 56 | } 57 | }, 0, 100); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/java/com/xuexiang/xipcdemo/service/impl/UserManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipcdemo.service.impl; 18 | 19 | import com.xuexiang.xipc.annotation.ClassName; 20 | import com.xuexiang.xipc.annotation.MethodName; 21 | import com.xuexiang.xipc.annotation.Singleton; 22 | import com.xuexiang.xipcdemo.service.IUserManager; 23 | 24 | /** 25 | * 测试单例的获取 26 | * 27 | * @author xuexiang 28 | * @since 2018/9/18 上午9:56 29 | */ 30 | @ClassName("UserManager") 31 | public class UserManager implements IUserManager { 32 | 33 | private static volatile UserManager sInstance = null; 34 | 35 | private String mUser; 36 | 37 | private UserManager() { 38 | mUser = "我的名字叫:XIPC"; 39 | } 40 | 41 | @Singleton 42 | public static UserManager getInstance() { 43 | if (sInstance == null) { 44 | synchronized (UserManager.class) { 45 | if (sInstance == null) { 46 | sInstance = new UserManager(); 47 | } 48 | } 49 | } 50 | return sInstance; 51 | } 52 | 53 | @MethodName("getUser") 54 | @Override 55 | public String getUser() { 56 | return mUser; 57 | } 58 | 59 | public UserManager setUser(String user) { 60 | mUser = user; 61 | return this; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_aidl_test.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 21 | 22 | 27 | 28 | 33 | 34 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_ipc_test.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 21 | 22 | 27 | 28 | 33 | 34 | 40 | 41 | 47 | 48 | 54 | 55 | 61 | 62 | 68 | 69 | 75 | 76 | 82 | 83 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #299EE3 4 | #299EE3 5 | #299EE3 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | XIPC 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/xuexiang/xipcdemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.xuexiang.xipcdemo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | apply from: './versions.gradle' 5 | addRepos(repositories) //增加代码仓库 6 | dependencies { 7 | classpath deps.android_gradle_plugin 8 | classpath deps.android_maven_gradle_plugin 9 | 10 | classpath 'com.github.xuexiangjys.XAOP:xaop-plugin:1.0.1' 11 | } 12 | } 13 | 14 | allprojects { 15 | addRepos(repositories) 16 | } 17 | 18 | task clean(type: Delete) { 19 | delete rootProject.buildDir 20 | } 21 | 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | 15 | # 是否打包APK 16 | isNeedPackage=false -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Sep 14 11:46:29 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /img/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/img/1.png -------------------------------------------------------------------------------- /img/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/img/demo.gif -------------------------------------------------------------------------------- /remote_demo/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /remote_demo/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'com.android.application' 18 | apply plugin: 'com.xuexiang.xaop' //引用xaop插件 19 | 20 | android { 21 | compileSdkVersion build_versions.target_sdk 22 | buildToolsVersion build_versions.build_tools 23 | 24 | defaultConfig { 25 | applicationId "com.xuexiang.remotedemo" 26 | minSdkVersion 19 27 | targetSdkVersion build_versions.target_sdk 28 | versionCode 1 29 | versionName "1.0" 30 | 31 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 32 | 33 | } 34 | 35 | if (isNeedPackage.toBoolean()) { 36 | signingConfigs { 37 | release { 38 | storeFile file(app_release.storeFile) 39 | storePassword app_release.storePassword 40 | keyAlias app_release.keyAlias 41 | keyPassword app_release.keyPassword 42 | } 43 | } 44 | } 45 | 46 | buildTypes { 47 | release { 48 | minifyEnabled true 49 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 50 | if (isNeedPackage.toBoolean()) { 51 | signingConfig signingConfigs.release 52 | } 53 | } 54 | } 55 | 56 | if (isNeedPackage.toBoolean()) { 57 | applicationVariants.all { variant -> 58 | variant.outputs.all { 59 | if (variant.buildType.name.equals('release')) { 60 | outputFileName = "remote_demo_${defaultConfig.versionName}.apk" 61 | } 62 | } 63 | } 64 | } 65 | 66 | lintOptions { 67 | abortOnError false 68 | } 69 | 70 | } 71 | 72 | dependencies { 73 | implementation fileTree(include: ['*.jar'], dir: 'libs') 74 | implementation deps.support.app_compat 75 | testImplementation deps.junit 76 | androidTestImplementation deps.runner 77 | androidTestImplementation deps.espresso.core 78 | implementation deps.support.recyclerview 79 | implementation 'com.github.xuexiangjys.XUtil:xutil-core:1.1.4' 80 | implementation 'com.github.xuexiangjys.XAOP:xaop-runtime:1.0.1' 81 | //添加依赖 82 | //XPage 83 | implementation 'com.github.xuexiangjys.XPage:xpage-lib:2.2.2' 84 | annotationProcessor 'com.github.xuexiangjys.XPage:xpage-compiler:2.2.2' 85 | //butterKnife的sdk 86 | implementation 'com.jakewharton:butterknife:8.8.1' 87 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' 88 | //如果开启了内存泄漏监测leak,就需要加上这个依赖 89 | debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4' 90 | releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' 91 | testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' 92 | 93 | 94 | implementation project(':xipc-runtime') 95 | } 96 | -------------------------------------------------------------------------------- /remote_demo/src/androidTest/java/com/xuexiang/remotedemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo; 18 | 19 | import android.content.Context; 20 | import android.support.test.InstrumentationRegistry; 21 | import android.support.test.runner.AndroidJUnit4; 22 | 23 | import org.junit.Test; 24 | import org.junit.runner.RunWith; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | /** 29 | * Instrumented test, which will execute on an Android device. 30 | * 31 | * @see Testing documentation 32 | */ 33 | @RunWith(AndroidJUnit4.class) 34 | public class ExampleInstrumentedTest { 35 | @Test 36 | public void useAppContext() { 37 | // Context of the app under test. 38 | Context appContext = InstrumentationRegistry.getTargetContext(); 39 | 40 | assertEquals("com.xuexiang.remotedemo", appContext.getPackageName()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /remote_demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 19 | 20 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /remote_demo/src/main/aidl/com/xuexiang/aidl/ICompute.aidl: -------------------------------------------------------------------------------- 1 | // ICompute.aidl 2 | package com.xuexiang.aidl; 3 | 4 | // Declare any non-default types here with import statements 5 | 6 | interface ICompute { 7 | /** 8 | * Demonstrates some basic types that you can use as parameters 9 | * and return values in AIDL. 10 | */ 11 | float calculate(float value1, String symbol, float value2); 12 | } 13 | -------------------------------------------------------------------------------- /remote_demo/src/main/java/com/xuexiang/remotedemo/App.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo; 18 | 19 | import android.app.Application; 20 | import android.content.Context; 21 | 22 | import com.xuexiang.xipc.XIPC; 23 | import com.xuexiang.xpage.AppPageConfig; 24 | import com.xuexiang.xpage.PageConfig; 25 | import com.xuexiang.xpage.PageConfiguration; 26 | import com.xuexiang.xpage.model.PageInfo; 27 | import com.xuexiang.xutil.XUtil; 28 | 29 | import java.util.List; 30 | 31 | /** 32 | * @author xuexiang 33 | * @since 2018/9/14 下午2:47 34 | */ 35 | public class App extends Application { 36 | 37 | @Override 38 | public void onCreate() { 39 | super.onCreate(); 40 | 41 | XUtil.init(this); 42 | XUtil.debug(true); 43 | 44 | initXIPC(); 45 | 46 | PageConfig.getInstance().setPageConfiguration(new PageConfiguration() { //页面注册 47 | @Override 48 | public List registerPages(Context context) { 49 | return AppPageConfig.getInstance().getPages(); //自动注册页面 50 | } 51 | }).debug("PageLog").enableWatcher(true).init(this); 52 | } 53 | 54 | private void initXIPC() { 55 | XIPC.init(this); 56 | XIPC.debug(BuildConfig.DEBUG); 57 | 58 | //注册包名下的所有定义的服务接口 59 | XIPC.register("com.xuexiang.remotedemo.service"); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /remote_demo/src/main/java/com/xuexiang/remotedemo/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo.activity; 18 | 19 | import android.os.Bundle; 20 | 21 | import com.xuexiang.remotedemo.fragment.MainFragment; 22 | import com.xuexiang.xpage.base.XPageActivity; 23 | 24 | public class MainActivity extends XPageActivity { 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | 30 | openPage(MainFragment.class); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /remote_demo/src/main/java/com/xuexiang/remotedemo/fragment/AIDLProcessFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo.fragment; 18 | 19 | import android.content.ComponentName; 20 | import android.content.Context; 21 | import android.content.Intent; 22 | import android.content.ServiceConnection; 23 | import android.os.IBinder; 24 | import android.os.RemoteException; 25 | 26 | import com.xuexiang.aidl.ICompute; 27 | import com.xuexiang.remotedemo.R; 28 | import com.xuexiang.xpage.annotation.Page; 29 | import com.xuexiang.xpage.base.XPageFragment; 30 | import com.xuexiang.xutil.tip.ToastUtils; 31 | 32 | import butterknife.OnClick; 33 | 34 | /** 35 | * @author xuexiang 36 | * @since 2018/9/24 下午1:39 37 | */ 38 | @Page(name = "原生AIDL进程间通信") 39 | public class AIDLProcessFragment extends XPageFragment { 40 | 41 | private ICompute mICompute; 42 | 43 | @Override 44 | protected int getLayoutId() { 45 | return R.layout.layout_aidl_test; 46 | } 47 | 48 | @Override 49 | protected void initArgs() { 50 | super.initArgs(); 51 | Intent intent = new Intent(); 52 | intent.setClassName("com.xuexiang.xipcdemo", "com.xuexiang.aidl.AIDLService"); 53 | getContext().bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE); 54 | } 55 | 56 | @Override 57 | protected void initViews() { 58 | 59 | } 60 | 61 | @Override 62 | protected void initListeners() { 63 | 64 | } 65 | 66 | @OnClick(R.id.btn_compute) 67 | public void onViewClicked() { 68 | try { 69 | ToastUtils.toast("3+4=" + mICompute.calculate(3 , "+", 4)); 70 | } catch (RemoteException e) { 71 | e.printStackTrace(); 72 | } 73 | } 74 | 75 | @Override 76 | public void onDestroyView() { 77 | if (mServiceConnection != null) { 78 | getContext().unbindService(mServiceConnection); 79 | } 80 | super.onDestroyView(); 81 | } 82 | 83 | private ServiceConnection mServiceConnection = new ServiceConnection() { 84 | 85 | @Override 86 | public void onServiceDisconnected(ComponentName name) { 87 | mICompute = null; 88 | } 89 | @Override 90 | public void onServiceConnected(ComponentName name, IBinder service) { 91 | mICompute = ICompute.Stub.asInterface(service); 92 | } 93 | }; 94 | } 95 | -------------------------------------------------------------------------------- /remote_demo/src/main/java/com/xuexiang/remotedemo/fragment/CrossApplicationProcessFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo.fragment; 18 | 19 | import android.view.View; 20 | import android.widget.ProgressBar; 21 | 22 | import com.xuexiang.remotedemo.R; 23 | import com.xuexiang.remotedemo.service.IComputeService; 24 | import com.xuexiang.remotedemo.service.IFileUtils; 25 | import com.xuexiang.remotedemo.service.ILoadingTask; 26 | import com.xuexiang.remotedemo.service.IUserManager; 27 | import com.xuexiang.remotedemo.service.LoadingCallback; 28 | import com.xuexiang.xaop.annotation.SingleClick; 29 | import com.xuexiang.xipc.XIPC; 30 | import com.xuexiang.xipc.core.channel.IPCListener; 31 | import com.xuexiang.xipc.core.channel.IPCService; 32 | import com.xuexiang.xpage.annotation.Page; 33 | import com.xuexiang.xpage.base.XPageFragment; 34 | import com.xuexiang.xutil.tip.ToastUtils; 35 | 36 | import butterknife.BindView; 37 | import butterknife.OnClick; 38 | 39 | /** 40 | * @author xuexiang 41 | * @since 2018/9/18 上午10:03 42 | */ 43 | @Page(name = "跨应用间进程通信") 44 | public class CrossApplicationProcessFragment extends XPageFragment { 45 | 46 | @BindView(R.id.progress_bar) 47 | ProgressBar progressBar; 48 | 49 | @Override 50 | protected void initArgs() { 51 | super.initArgs(); 52 | XIPC.connectApp(getContext(), "com.xuexiang.xipcdemo"); 53 | XIPC.setIPCListener(new IPCListener() { 54 | @Override 55 | public void onIPCConnected(Class extends IPCService> service) { 56 | ToastUtils.toast("IPC服务已绑定!"); 57 | } 58 | }); 59 | } 60 | 61 | @Override 62 | protected int getLayoutId() { 63 | return R.layout.layout_ipc_test; 64 | } 65 | 66 | @Override 67 | protected void initViews() { 68 | 69 | } 70 | 71 | @Override 72 | protected void initListeners() { 73 | 74 | } 75 | 76 | @SingleClick 77 | @OnClick({R.id.btn_compute, R.id.btn_download, R.id.btn_get_user, R.id.btn_util_test}) 78 | public void onViewClicked(View view) { 79 | switch (view.getId()) { 80 | case R.id.btn_compute: 81 | IComputeService computeService = XIPC.getService(IComputeService.class); 82 | ToastUtils.toast("3*4=" + computeService.calculate(3 , "*", 4)); 83 | break; 84 | case R.id.btn_download: 85 | ILoadingTask loadingTask = XIPC.getService(ILoadingTask.class, "pic.png"); 86 | loadingTask.start(new LoadingCallback() { 87 | @Override 88 | public void callback(int progress) { 89 | progressBar.setProgress(progress); 90 | } 91 | }); 92 | break; 93 | case R.id.btn_get_user: 94 | 95 | IUserManager userManager = XIPC.getInstance(IUserManager.class); 96 | ToastUtils.toast(userManager.getUser()); 97 | break; 98 | case R.id.btn_util_test: 99 | IFileUtils fileUtils = XIPC.getUtilityClass(IFileUtils.class); 100 | ToastUtils.toast("应用目录:" + fileUtils.getExternalCacheDir(getContext())); 101 | break; 102 | } 103 | } 104 | 105 | @Override 106 | public void onDestroyView() { 107 | XIPC.disconnect(getContext()); 108 | super.onDestroyView(); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /remote_demo/src/main/java/com/xuexiang/remotedemo/fragment/MainFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo.fragment; 18 | 19 | import android.view.KeyEvent; 20 | import android.view.View; 21 | 22 | import com.xuexiang.xpage.annotation.Page; 23 | import com.xuexiang.xpage.base.XPageContainerListFragment; 24 | import com.xuexiang.xpage.utils.TitleBar; 25 | import com.xuexiang.xutil.common.ClickUtils; 26 | 27 | /** 28 | * @author xuexiang 29 | * @since 2018/9/14 下午2:50 30 | */ 31 | @Page(name = "XIPC 进程间通信框架") 32 | public class MainFragment extends XPageContainerListFragment { 33 | 34 | 35 | @Override 36 | protected Class[] getPagesClasses() { 37 | return new Class[]{ 38 | CrossApplicationProcessFragment.class, 39 | AIDLProcessFragment.class 40 | }; 41 | } 42 | 43 | @Override 44 | protected TitleBar initTitleBar() { 45 | return super.initTitleBar().setLeftClickListener(new View.OnClickListener() { 46 | @Override 47 | public void onClick(View view) { 48 | ClickUtils.exitBy2Click(); 49 | } 50 | }); 51 | } 52 | 53 | 54 | /** 55 | * 菜单、返回键响应 56 | */ 57 | @Override 58 | public boolean onKeyDown(int keyCode, KeyEvent event) { 59 | if (keyCode == KeyEvent.KEYCODE_BACK) { 60 | ClickUtils.exitBy2Click(); 61 | } 62 | return true; 63 | } 64 | 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /remote_demo/src/main/java/com/xuexiang/remotedemo/service/IComputeService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo.service; 18 | 19 | import com.xuexiang.xipc.annotation.ClassName; 20 | import com.xuexiang.xipc.annotation.MethodName; 21 | 22 | /** 23 | * 服务接口测试 24 | * 25 | * @author xuexiang 26 | * @since 2018/9/18 上午10:26 27 | */ 28 | @ClassName("ComputeService") 29 | public interface IComputeService { 30 | 31 | /** 32 | * 计算 33 | * 34 | * @param value1 值1 35 | * @param symbol 算数符号 36 | * @param value2 值2 37 | * @return 38 | */ 39 | @MethodName("calculate") 40 | float calculate(float value1, String symbol, float value2); 41 | } 42 | -------------------------------------------------------------------------------- /remote_demo/src/main/java/com/xuexiang/remotedemo/service/IFileUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo.service; 18 | 19 | import android.content.Context; 20 | 21 | import com.xuexiang.xipc.annotation.ClassName; 22 | 23 | /** 24 | * 工具类测试 25 | * 26 | * @author xuexiang 27 | * @since 2018/9/18 上午9:59 28 | */ 29 | @ClassName("FileUtils") 30 | public interface IFileUtils { 31 | 32 | String getExternalCacheDir(Context context); 33 | } 34 | -------------------------------------------------------------------------------- /remote_demo/src/main/java/com/xuexiang/remotedemo/service/ILoadingTask.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo.service; 18 | 19 | import com.xuexiang.xipc.annotation.ClassName; 20 | import com.xuexiang.xipc.annotation.MethodName; 21 | import com.xuexiang.xipc.annotation.WeakRef; 22 | 23 | /** 24 | * 服务接口测试 25 | * 26 | * @author xuexiang 27 | * @since 2018/9/18 上午9:58 28 | */ 29 | @ClassName("LoadingTask") 30 | public interface ILoadingTask { 31 | 32 | @MethodName("start") 33 | void start(@WeakRef LoadingCallback loadingCallback); 34 | } 35 | -------------------------------------------------------------------------------- /remote_demo/src/main/java/com/xuexiang/remotedemo/service/IUserManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo.service; 18 | 19 | import com.xuexiang.xipc.annotation.ClassName; 20 | import com.xuexiang.xipc.annotation.MethodName; 21 | 22 | /** 23 | * 测试单例的获取 24 | * 25 | * @author xuexiang 26 | * @since 2018/9/18 上午9:56 27 | */ 28 | @ClassName("UserManager") 29 | public interface IUserManager { 30 | 31 | @MethodName("getUser") 32 | String getUser(); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /remote_demo/src/main/java/com/xuexiang/remotedemo/service/LoadingCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo.service; 18 | 19 | import com.xuexiang.xipc.annotation.ClassName; 20 | 21 | /** 22 | * 定义的回调接口 23 | * 24 | * @author xuexiang 25 | * @since 2018/9/18 上午9:51 26 | */ 27 | @ClassName("LoadingCallback") 28 | public interface LoadingCallback { 29 | 30 | void callback(int progress); 31 | } 32 | -------------------------------------------------------------------------------- /remote_demo/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 23 | 28 | 29 | 35 | 38 | 41 | 42 | 43 | 44 | 50 | 51 | -------------------------------------------------------------------------------- /remote_demo/src/main/res/layout/layout_aidl_test.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 21 | 22 | 27 | 28 | 33 | 34 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /remote_demo/src/main/res/layout/layout_ipc_test.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 21 | 22 | 27 | 28 | 33 | 34 | 40 | 41 | 47 | 48 | 54 | 55 | 61 | 62 | 68 | 69 | 75 | 76 | 82 | 83 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/remote_demo/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/remote_demo/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/remote_demo/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/remote_demo/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/remote_demo/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/remote_demo/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/remote_demo/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/remote_demo/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/remote_demo/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /remote_demo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexiangjys/XIPC/1113736f9298b4acb6d4743b6c42a2ec4d90399c/remote_demo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /remote_demo/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | #299EE3 19 | #299EE3 20 | #299EE3 21 | 22 | -------------------------------------------------------------------------------- /remote_demo/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | RemoteDemo 19 | 20 | -------------------------------------------------------------------------------- /remote_demo/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /remote_demo/src/test/java/com/xuexiang/remotedemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.remotedemo; 18 | 19 | import org.junit.Test; 20 | 21 | import static org.junit.Assert.*; 22 | 23 | /** 24 | * Example local unit test, which will execute on the development machine (host). 25 | * 26 | * @see Testing documentation 27 | */ 28 | public class ExampleUnitTest { 29 | @Test 30 | public void addition_isCorrect() { 31 | assertEquals(4, 2 + 2); 32 | } 33 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':xipc-runtime', ':remote_demo' 2 | -------------------------------------------------------------------------------- /xipc-runtime/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /xipc-runtime/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion build_versions.target_sdk 5 | buildToolsVersion build_versions.build_tools 6 | 7 | defaultConfig { 8 | minSdkVersion 14 9 | targetSdkVersion build_versions.target_sdk 10 | } 11 | 12 | lintOptions { 13 | abortOnError false 14 | } 15 | } 16 | 17 | dependencies { 18 | compileOnly deps.support.app_compat 19 | compileOnly deps.gson 20 | } 21 | 22 | apply from: "../JitPackUpload.gradle" -------------------------------------------------------------------------------- /xipc-runtime/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # xipc 2 | -keep @com.xuexiang.xipc.annotation.* class * {*;} 3 | -keep class * { 4 | @com.xuexiang.xipc.annotation.* ; 5 | } 6 | -keepclassmembers class * { 7 | @com.xuexiang.xipc.annotation.* ; 8 | } -------------------------------------------------------------------------------- /xipc-runtime/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/annotation/Background.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.annotation; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * 标识接口回调的线程是子线程的注解 26 | * 27 | * @author xuexiang 28 | * @since 2018/9/14 下午3:27 29 | */ 30 | @Target(ElementType.PARAMETER) 31 | @Retention(RetentionPolicy.RUNTIME) 32 | public @interface Background { 33 | 34 | } 35 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/annotation/ClassName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.annotation; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * 类的标识、映射注解,用于查找指定的类 26 | * 27 | * @author xuexiang 28 | * @since 2018/9/14 下午3:29 29 | */ 30 | @Target(ElementType.TYPE) 31 | @Retention(RetentionPolicy.RUNTIME) 32 | public @interface ClassName { 33 | /** 34 | * @return 类名,必填 35 | */ 36 | String value(); 37 | } 38 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/annotation/MethodName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.annotation; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * 标识方法的注解,用于查找指定的方法 26 | */ 27 | @Target(ElementType.METHOD) 28 | @Retention(RetentionPolicy.RUNTIME) 29 | public @interface MethodName { 30 | /** 31 | * @return 方法名,必填 32 | */ 33 | String value(); 34 | } 35 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/annotation/Singleton.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.annotation; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * 标识获取单例方法的注解 26 | * 27 | * @author xuexiang 28 | * @since 2018/9/14 下午3:31 29 | */ 30 | @Target(ElementType.METHOD) 31 | @Retention(RetentionPolicy.RUNTIME) 32 | public @interface Singleton { 33 | 34 | } 35 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/annotation/WeakRef.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.annotation; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * 标识接口回调是弱引用的注解,防止内存泄漏 26 | * 27 | * @author xuexiang 28 | * @since 2018/9/14 下午3:39 29 | */ 30 | @Target(ElementType.PARAMETER) 31 | @Retention(RetentionPolicy.RUNTIME) 32 | public @interface WeakRef { 33 | } 34 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/annotation/WithinProcess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.annotation; 18 | 19 | import java.lang.annotation.ElementType; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.RetentionPolicy; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * 被注解的方法、类不能进行进程间通信 26 | * 27 | * @author xuexiang 28 | * @since 2018/9/14 下午3:40 29 | */ 30 | @Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.TYPE}) 31 | @Retention(RetentionPolicy.RUNTIME) 32 | public @interface WithinProcess { 33 | 34 | } 35 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/channel/IPCCallbackInvocationHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.channel; 18 | 19 | import android.os.RemoteException; 20 | 21 | import com.xuexiang.xipc.core.entity.CallbackMail; 22 | import com.xuexiang.xipc.core.entity.Reply; 23 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 24 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 25 | import com.xuexiang.xipc.exception.IPCException; 26 | import com.xuexiang.xipc.logs.IPCLog; 27 | import com.xuexiang.xipc.util.TypeUtils; 28 | 29 | import java.lang.reflect.InvocationHandler; 30 | import java.lang.reflect.Method; 31 | 32 | /** 33 | * IPC回调执行的动态代理 34 | * 35 | * @author xuexiang 36 | * @since 2018/9/17 下午5:26 37 | */ 38 | public class IPCCallbackInvocationHandler implements InvocationHandler { 39 | 40 | private long mTimeStamp; 41 | 42 | private int mIndex; 43 | 44 | private IIPCServiceCallback mCallback; 45 | 46 | public IPCCallbackInvocationHandler(long timeStamp, int index, IIPCServiceCallback callback) { 47 | mTimeStamp = timeStamp; 48 | mIndex = index; 49 | mCallback = callback; 50 | } 51 | 52 | @Override 53 | public Object invoke(Object proxy, Method method, Object[] objects) { 54 | try { 55 | MethodWrapper methodWrapper = new MethodWrapper(method); 56 | ParameterWrapper[] parameterWrappers = TypeUtils.objectToWrapper(objects); 57 | CallbackMail callbackMail = new CallbackMail(mTimeStamp, mIndex, methodWrapper, parameterWrappers); 58 | Reply reply = mCallback.callback(callbackMail); 59 | if (reply == null) { 60 | return null; 61 | } 62 | if (reply.success()) { 63 | /** 64 | * Note that the returned type should be registered in the remote process. 65 | */ 66 | return reply.getResult(); 67 | } else { 68 | IPCLog.e("Error occurs: " + reply.getMessage()); 69 | return null; 70 | } 71 | } catch (IPCException e) { 72 | IPCLog.e("Error occurs but does not crash the app.", e); 73 | } catch (RemoteException e) { 74 | IPCLog.e("Error occurs but does not crash the app.", e); 75 | } 76 | return null; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/channel/IPCInvocationHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.channel; 18 | 19 | import com.xuexiang.xipc.core.entity.Reply; 20 | import com.xuexiang.xipc.core.sender.Sender; 21 | import com.xuexiang.xipc.core.sender.impl.SenderDesignator; 22 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 23 | import com.xuexiang.xipc.exception.IPCException; 24 | import com.xuexiang.xipc.logs.IPCLog; 25 | 26 | import java.lang.reflect.InvocationHandler; 27 | import java.lang.reflect.Method; 28 | 29 | /** 30 | * IPC服务执行方法的动态代理 31 | * 32 | * @author xuexiang 33 | * @since 2018/9/17 下午5:59 34 | */ 35 | public class IPCInvocationHandler implements InvocationHandler { 36 | 37 | private Sender mSender; 38 | 39 | public IPCInvocationHandler(Class extends IPCService> service, ObjectWrapper object) { 40 | mSender = SenderDesignator.getPostOffice(service, SenderDesignator.TYPE_INVOKE_METHOD, object); 41 | } 42 | 43 | @Override 44 | public Object invoke(Object proxy, Method method, Object[] objects) { 45 | try { 46 | Reply reply = mSender.send(method, objects); 47 | if (reply == null) { 48 | return null; 49 | } 50 | if (reply.success()) { 51 | return reply.getResult(); 52 | } else { 53 | IPCLog.e("Error occurs. Error " + reply.getErrorCode() + ": " + reply.getMessage()); 54 | return null; 55 | } 56 | } catch (IPCException e) { 57 | e.printStackTrace(); 58 | IPCLog.e("Error occurs. Error " + e.getErrorCode() + ": " + e.getMessage()); 59 | return null; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/channel/IPCListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.channel; 18 | 19 | /** 20 | * IPC服务监听回调 21 | * 22 | * @author xuexiang 23 | * @since 2018/9/17 下午6:15 24 | */ 25 | public abstract class IPCListener { 26 | 27 | public abstract void onIPCConnected(Class extends IPCService> service); 28 | 29 | public void onIPCDisconnected(Class extends IPCService> service) { 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/channel/IPCService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.channel; 18 | 19 | import android.app.Service; 20 | import android.content.Intent; 21 | import android.os.IBinder; 22 | import android.os.RemoteException; 23 | 24 | import com.xuexiang.xipc.core.entity.Mail; 25 | import com.xuexiang.xipc.core.entity.Reply; 26 | import com.xuexiang.xipc.core.receiver.Receiver; 27 | import com.xuexiang.xipc.core.receiver.ReceiverDesignator; 28 | import com.xuexiang.xipc.exception.IPCException; 29 | import com.xuexiang.xipc.util.ObjectCenter; 30 | 31 | import java.util.List; 32 | import java.util.concurrent.ConcurrentHashMap; 33 | 34 | /** 35 | * IPC通信服务 36 | * 37 | * @author xuexiang 38 | * @since 2018/9/17 下午4:15 39 | */ 40 | public abstract class IPCService extends Service { 41 | 42 | private static final ObjectCenter OBJECT_CENTER = ObjectCenter.getInstance(); 43 | 44 | private ConcurrentHashMap mCallbacks = new ConcurrentHashMap(); 45 | 46 | private final IIPCService.Stub mBinder = new IIPCService.Stub() { 47 | @Override 48 | public Reply send(Mail mail) { 49 | try { 50 | Receiver receiver = ReceiverDesignator.getReceiver(mail.getObject()); 51 | int pid = mail.getPid(); 52 | IIPCServiceCallback callback = mCallbacks.get(pid); 53 | if (callback != null) { 54 | receiver.setIPCServiceCallback(callback); 55 | } 56 | return receiver.action(mail.getTimeStamp(), mail.getMethod(), mail.getParameters()); 57 | } catch (IPCException e) { 58 | e.printStackTrace(); 59 | return new Reply(e.getErrorCode(), e.getMessage()); 60 | } 61 | } 62 | 63 | @Override 64 | public void register(IIPCServiceCallback callback, int pid) throws RemoteException { 65 | mCallbacks.put(pid, callback); 66 | } 67 | 68 | @Override 69 | public void gc(List timeStamps) throws RemoteException { 70 | OBJECT_CENTER.deleteObjects(timeStamps); 71 | } 72 | }; 73 | 74 | public IPCService() { 75 | } 76 | 77 | @Override 78 | public IBinder onBind(Intent intent) { 79 | return mBinder; 80 | } 81 | 82 | public static class IPCService0 extends IPCService {} 83 | 84 | public static class IPCService1 extends IPCService {} 85 | 86 | public static class IPCService2 extends IPCService {} 87 | 88 | public static class IPCService3 extends IPCService {} 89 | 90 | public static class IPCService4 extends IPCService {} 91 | 92 | public static class IPCService5 extends IPCService {} 93 | 94 | public static class IPCService6 extends IPCService {} 95 | 96 | public static class IPCService7 extends IPCService {} 97 | 98 | public static class IPCService8 extends IPCService {} 99 | 100 | public static class IPCService9 extends IPCService {} 101 | 102 | } 103 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/entity/CallbackMail.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.entity; 18 | 19 | import android.os.Parcel; 20 | import android.os.Parcelable; 21 | 22 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 23 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 24 | 25 | /** 26 | * 请求接口回调的载体 27 | * 28 | * @author xuexiang 29 | * @since 2018/9/17 下午3:39 30 | */ 31 | public class CallbackMail implements Parcelable { 32 | 33 | private long mTimeStamp; 34 | 35 | private int mIndex; 36 | 37 | private MethodWrapper mMethod; 38 | 39 | private ParameterWrapper[] mParameters; 40 | 41 | public static final Creator CREATOR 42 | = new Creator() { 43 | public CallbackMail createFromParcel(Parcel in) { 44 | CallbackMail mail = new CallbackMail(); 45 | mail.readFromParcel(in); 46 | return mail; 47 | } 48 | public CallbackMail[] newArray(int size) { 49 | return new CallbackMail[size]; 50 | } 51 | }; 52 | 53 | @Override 54 | public int describeContents() { 55 | return 0; 56 | } 57 | 58 | @Override 59 | public void writeToParcel(Parcel parcel, int flags) { 60 | parcel.writeLong(mTimeStamp); 61 | parcel.writeInt(mIndex); 62 | parcel.writeParcelable(mMethod, flags); 63 | parcel.writeParcelableArray(mParameters, flags); 64 | } 65 | 66 | public void readFromParcel(Parcel in) { 67 | mTimeStamp = in.readLong(); 68 | mIndex = in.readInt(); 69 | ClassLoader classLoader = CallbackMail.class.getClassLoader(); 70 | mMethod = in.readParcelable(classLoader); 71 | Parcelable[] parcelables = in.readParcelableArray(classLoader); 72 | if (parcelables == null) { 73 | mParameters = null; 74 | } else { 75 | int length = parcelables.length; 76 | mParameters = new ParameterWrapper[length]; 77 | for (int i = 0; i < length; ++i) { 78 | mParameters[i] = (ParameterWrapper) parcelables[i]; 79 | } 80 | } 81 | 82 | } 83 | 84 | private CallbackMail() { 85 | 86 | } 87 | 88 | public CallbackMail(long timeStamp, int index, MethodWrapper method, ParameterWrapper[] parameters) { 89 | mTimeStamp = timeStamp; 90 | mIndex = index; 91 | mMethod = method; 92 | mParameters = parameters; 93 | } 94 | 95 | public ParameterWrapper[] getParameters() { 96 | return mParameters; 97 | } 98 | 99 | public int getIndex() { 100 | return mIndex; 101 | } 102 | 103 | public MethodWrapper getMethod() { 104 | return mMethod; 105 | } 106 | 107 | public long getTimeStamp() { 108 | return mTimeStamp; 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/entity/Mail.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.entity; 18 | 19 | import android.os.Parcel; 20 | import android.os.Parcelable; 21 | import android.os.Process; 22 | 23 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 24 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 25 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 26 | 27 | /** 28 | * 请求接口的方法、参数、数据的载体 29 | * 30 | * @author xuexiang 31 | * @since 2018/9/17 下午3:20 32 | */ 33 | public class Mail implements Parcelable { 34 | 35 | private long mTimeStamp; 36 | 37 | /** 38 | * 唯一号标识 39 | */ 40 | private int mPid; 41 | 42 | /** 43 | * 请求接口的类型 44 | */ 45 | private ObjectWrapper mObject; 46 | 47 | /** 48 | * 请求的方法 49 | */ 50 | private MethodWrapper mMethod; 51 | 52 | /** 53 | * 请求的参数 54 | */ 55 | private ParameterWrapper[] mParameters; 56 | 57 | public static final Creator CREATOR 58 | = new Creator() { 59 | public Mail createFromParcel(Parcel in) { 60 | Mail mail = new Mail(); 61 | mail.readFromParcel(in); 62 | return mail; 63 | } 64 | 65 | public Mail[] newArray(int size) { 66 | return new Mail[size]; 67 | } 68 | }; 69 | 70 | @Override 71 | public int describeContents() { 72 | return 0; 73 | } 74 | 75 | @Override 76 | public void writeToParcel(Parcel parcel, int flags) { 77 | parcel.writeLong(mTimeStamp); 78 | parcel.writeInt(mPid); 79 | parcel.writeParcelable(mObject, flags); 80 | parcel.writeParcelable(mMethod, flags); 81 | parcel.writeParcelableArray(mParameters, flags); 82 | } 83 | 84 | public void readFromParcel(Parcel in) { 85 | mTimeStamp = in.readLong(); 86 | mPid = in.readInt(); 87 | ClassLoader classLoader = Mail.class.getClassLoader(); 88 | mObject = in.readParcelable(classLoader); 89 | mMethod = in.readParcelable(classLoader); 90 | Parcelable[] parcelables = in.readParcelableArray(classLoader); 91 | if (parcelables == null) { 92 | mParameters = null; 93 | } else { 94 | int length = parcelables.length; 95 | mParameters = new ParameterWrapper[length]; 96 | for (int i = 0; i < length; ++i) { 97 | mParameters[i] = (ParameterWrapper) parcelables[i]; 98 | } 99 | } 100 | 101 | } 102 | 103 | private Mail() { 104 | 105 | } 106 | 107 | /** 108 | * 构造放啊 109 | * 110 | * @param timeStamp 自增序列 111 | * @param object 请求类型 112 | * @param method 请求方法 113 | * @param parameters 请求参数 114 | */ 115 | public Mail(long timeStamp, ObjectWrapper object, MethodWrapper method, ParameterWrapper[] parameters) { 116 | mTimeStamp = timeStamp; 117 | mPid = Process.myPid(); 118 | mObject = object; 119 | mMethod = method; 120 | mParameters = parameters; 121 | } 122 | 123 | public int getPid() { 124 | return mPid; 125 | } 126 | 127 | public ParameterWrapper[] getParameters() { 128 | return mParameters; 129 | } 130 | 131 | public ObjectWrapper getObject() { 132 | return mObject; 133 | } 134 | 135 | public MethodWrapper getMethod() { 136 | return mMethod; 137 | } 138 | 139 | public long getTimeStamp() { 140 | return mTimeStamp; 141 | } 142 | 143 | } 144 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/entity/Reply.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.entity; 18 | 19 | import android.os.Parcel; 20 | import android.os.Parcelable; 21 | 22 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 23 | import com.xuexiang.xipc.core.wrapper.TypeWrapper; 24 | import com.xuexiang.xipc.exception.ErrorCodes; 25 | import com.xuexiang.xipc.exception.IPCException; 26 | import com.xuexiang.xipc.util.SerializeUtils; 27 | import com.xuexiang.xipc.util.TypeCenter; 28 | 29 | /** 30 | * 请求结果返回的载体 31 | * 32 | * @author xuexiang 33 | * @since 2018/9/17 下午3:39 34 | */ 35 | public class Reply implements Parcelable { 36 | 37 | private final static TypeCenter TYPE_CENTER = TypeCenter.getInstance(); 38 | 39 | /** 40 | * 请求错误码 41 | */ 42 | private int mErrorCode; 43 | 44 | /** 45 | * 请求错误信息 46 | */ 47 | private String mErrorMessage; 48 | 49 | /** 50 | * 请求返回类型 51 | */ 52 | private TypeWrapper mClass; 53 | 54 | /** 55 | * 请求返回结果 56 | */ 57 | private Object mResult; 58 | 59 | public static final Creator CREATOR 60 | = new Creator() { 61 | public Reply createFromParcel(Parcel in) { 62 | Reply reply = new Reply(); 63 | reply.readFromParcel(in); 64 | return reply; 65 | } 66 | 67 | public Reply[] newArray(int size) { 68 | return new Reply[size]; 69 | } 70 | }; 71 | 72 | @Override 73 | public int describeContents() { 74 | return 0; 75 | } 76 | 77 | @Override 78 | public void writeToParcel(Parcel parcel, int flags) { 79 | parcel.writeInt(mErrorCode); 80 | parcel.writeString(mErrorMessage); 81 | parcel.writeParcelable(mClass, flags); 82 | try { 83 | parcel.writeString(SerializeUtils.encode(mResult)); 84 | } catch (IPCException e) { 85 | e.printStackTrace(); 86 | } 87 | } 88 | 89 | 90 | public void readFromParcel(Parcel in) { 91 | mErrorCode = in.readInt(); 92 | ClassLoader classLoader = Reply.class.getClassLoader(); 93 | mErrorMessage = in.readString(); 94 | mClass = in.readParcelable(classLoader); 95 | try { 96 | Class> clazz = TYPE_CENTER.getClassType(mClass); 97 | mResult = SerializeUtils.decode(in.readString(), clazz); 98 | } catch (IPCException e) { 99 | e.printStackTrace(); 100 | } 101 | } 102 | 103 | private Reply() { 104 | 105 | } 106 | 107 | /** 108 | * 请求成功的回复 109 | * 110 | * @param parameterWrapper 111 | */ 112 | public Reply(ParameterWrapper parameterWrapper) { 113 | try { 114 | Class> clazz = TYPE_CENTER.getClassType(parameterWrapper); 115 | mResult = SerializeUtils.decode(parameterWrapper.getData(), clazz); 116 | mErrorCode = ErrorCodes.SUCCESS; 117 | mErrorMessage = null; 118 | mClass = new TypeWrapper(clazz); 119 | } catch (IPCException e) { 120 | e.printStackTrace(); 121 | mErrorCode = e.getErrorCode(); 122 | mErrorMessage = e.getMessage(); 123 | mResult = null; 124 | mClass = null; 125 | } 126 | } 127 | 128 | /** 129 | * 出错的回复 130 | * 131 | * @param errorCode 132 | * @param message 133 | */ 134 | public Reply(int errorCode, String message) { 135 | mErrorCode = errorCode; 136 | mErrorMessage = message; 137 | mResult = null; 138 | mClass = null; 139 | } 140 | 141 | public int getErrorCode() { 142 | return mErrorCode; 143 | } 144 | 145 | public boolean success() { 146 | return mErrorCode == ErrorCodes.SUCCESS; 147 | } 148 | 149 | public String getMessage() { 150 | return mErrorMessage; 151 | } 152 | 153 | public Object getResult() { 154 | return mResult; 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/receiver/IReceiver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.receiver; 18 | 19 | import com.xuexiang.xipc.exception.IPCException; 20 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 21 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 22 | 23 | /** 24 | * 注册方法的接收 25 | * 26 | * @author xuexiang 27 | * @since 2018/9/17 下午2:54 28 | */ 29 | public interface IReceiver { 30 | 31 | void setMethod(MethodWrapper methodWrapper, ParameterWrapper[] parameterWrappers) 32 | throws IPCException; 33 | 34 | Object invokeMethod() throws IPCException; 35 | } 36 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/receiver/ReceiverDesignator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.receiver; 18 | 19 | import com.xuexiang.xipc.core.receiver.impl.InstanceCreatingReceiver; 20 | import com.xuexiang.xipc.core.receiver.impl.InstanceGettingReceiver; 21 | import com.xuexiang.xipc.core.receiver.impl.ObjectReceiver; 22 | import com.xuexiang.xipc.core.receiver.impl.UtilityGettingReceiver; 23 | import com.xuexiang.xipc.core.receiver.impl.UtilityReceiver; 24 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 25 | import com.xuexiang.xipc.exception.ErrorCodes; 26 | import com.xuexiang.xipc.exception.IPCException; 27 | 28 | /** 29 | * 接收器类型构建器,处理相应的请求动作 30 | * 31 | * @author xuexiang 32 | * @since 2018/9/18 下午3:30 33 | */ 34 | public class ReceiverDesignator { 35 | 36 | public static Receiver getReceiver(ObjectWrapper objectWrapper) throws IPCException { 37 | int type = objectWrapper.getType(); 38 | switch (type) { 39 | case ObjectWrapper.TYPE_OBJECT_TO_NEW: 40 | return new InstanceCreatingReceiver(objectWrapper); 41 | case ObjectWrapper.TYPE_OBJECT_TO_GET: 42 | return new InstanceGettingReceiver(objectWrapper); 43 | case ObjectWrapper.TYPE_CLASS: 44 | return new UtilityReceiver(objectWrapper); 45 | case ObjectWrapper.TYPE_OBJECT: 46 | return new ObjectReceiver(objectWrapper); 47 | case ObjectWrapper.TYPE_CLASS_TO_GET: 48 | return new UtilityGettingReceiver(objectWrapper); 49 | default: 50 | throw new IPCException(ErrorCodes.ILLEGAL_PARAMETER_EXCEPTION, 51 | "Type " + type + " is not supported."); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/receiver/impl/InstanceCreatingReceiver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.receiver.impl; 18 | 19 | import com.xuexiang.xipc.core.receiver.Receiver; 20 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 21 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 22 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 23 | import com.xuexiang.xipc.exception.ErrorCodes; 24 | import com.xuexiang.xipc.exception.IPCException; 25 | import com.xuexiang.xipc.util.TypeUtils; 26 | 27 | import java.lang.reflect.Constructor; 28 | import java.lang.reflect.InvocationTargetException; 29 | 30 | /** 31 | * 处理实例的创建 32 | * 33 | * @author xuexiang 34 | * @since 2018/9/18 下午3:22 35 | */ 36 | public class InstanceCreatingReceiver extends Receiver { 37 | 38 | private Class> mObjectClass; 39 | 40 | private Constructor> mConstructor; 41 | 42 | public InstanceCreatingReceiver(ObjectWrapper object) throws IPCException { 43 | super(object); 44 | Class> clazz = TYPE_CENTER.getClassType(object); 45 | TypeUtils.validateAccessible(clazz); 46 | mObjectClass = clazz; 47 | } 48 | 49 | @Override 50 | public void setMethod(MethodWrapper methodWrapper, ParameterWrapper[] parameterWrappers) 51 | throws IPCException { 52 | Constructor> constructor = TypeUtils.getConstructor(mObjectClass, TYPE_CENTER.getClassTypes(parameterWrappers)); 53 | TypeUtils.validateAccessible(constructor); 54 | mConstructor = constructor; 55 | } 56 | 57 | @Override 58 | public Object invokeMethod() throws IPCException { 59 | Exception exception; 60 | try { 61 | Object object; 62 | Object[] parameters = getParameters(); 63 | if (parameters == null) { 64 | object = mConstructor.newInstance(); 65 | } else { 66 | object = mConstructor.newInstance(parameters); 67 | } 68 | OBJECT_CENTER.putObject(getObjectTimeStamp(), object); 69 | return null; 70 | } catch (InstantiationException e) { 71 | exception = e; 72 | } catch (IllegalAccessException e) { 73 | exception = e; 74 | } catch (InvocationTargetException e) { 75 | exception = e; 76 | } 77 | exception.printStackTrace(); 78 | throw new IPCException(ErrorCodes.METHOD_INVOCATION_EXCEPTION, 79 | "Error occurs when invoking constructor to create an instance of " 80 | + mObjectClass.getName(), exception); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/receiver/impl/InstanceGettingReceiver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.receiver.impl; 18 | 19 | import com.xuexiang.xipc.core.receiver.Receiver; 20 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 21 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 22 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 23 | import com.xuexiang.xipc.exception.ErrorCodes; 24 | import com.xuexiang.xipc.exception.IPCException; 25 | import com.xuexiang.xipc.util.TypeUtils; 26 | 27 | import java.lang.reflect.InvocationTargetException; 28 | import java.lang.reflect.Method; 29 | import java.lang.reflect.Modifier; 30 | 31 | /** 32 | * 处理单例的获取 33 | * 34 | * @author xuexiang 35 | * @since 2018/9/18 下午3:25 36 | */ 37 | public class InstanceGettingReceiver extends Receiver { 38 | 39 | private Method mMethod; 40 | 41 | private Class> mObjectClass; 42 | 43 | public InstanceGettingReceiver(ObjectWrapper objectWrapper) throws IPCException { 44 | super(objectWrapper); 45 | Class> clazz = TYPE_CENTER.getClassType(objectWrapper); 46 | TypeUtils.validateAccessible(clazz); 47 | mObjectClass = clazz; 48 | } 49 | 50 | @Override 51 | public void setMethod(MethodWrapper methodWrapper, ParameterWrapper[] parameterWrappers) 52 | throws IPCException { 53 | int length = parameterWrappers.length; 54 | Class>[] parameterTypes = new Class>[length]; 55 | for (int i = 0; i < length; ++i) { 56 | parameterTypes[i] = TYPE_CENTER.getClassType(parameterWrappers[i]); 57 | } 58 | String methodName = methodWrapper.getName(); 59 | Method method = TypeUtils.getMethodForGettingInstance(mObjectClass, methodName, parameterTypes); 60 | if (!Modifier.isStatic(method.getModifiers())) { 61 | throw new IPCException(ErrorCodes.METHOD_GET_INSTANCE_NOT_STATIC, 62 | "Method " + method.getName() + " of class " + mObjectClass.getName() + " is not static. " 63 | + "Only the static method can be invoked to get an instance."); 64 | } 65 | TypeUtils.validateAccessible(method); 66 | mMethod = method; 67 | } 68 | 69 | @Override 70 | public Object invokeMethod() throws IPCException { 71 | Exception exception; 72 | try { 73 | Object object = mMethod.invoke(null, getParameters()); 74 | OBJECT_CENTER.putObject(getObjectTimeStamp(), object); 75 | return null; 76 | } catch (IllegalAccessException e) { 77 | exception = e; 78 | } catch (InvocationTargetException e) { 79 | exception = e; 80 | } 81 | exception.printStackTrace(); 82 | throw new IPCException(ErrorCodes.METHOD_INVOCATION_EXCEPTION, 83 | "Error occurs when invoking method " + mMethod + " to get an instance of " 84 | + mObjectClass.getName(), exception); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/receiver/impl/ObjectReceiver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.receiver.impl; 18 | 19 | import com.xuexiang.xipc.core.receiver.Receiver; 20 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 21 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 22 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 23 | import com.xuexiang.xipc.exception.ErrorCodes; 24 | import com.xuexiang.xipc.exception.IPCException; 25 | import com.xuexiang.xipc.util.TypeUtils; 26 | 27 | import java.lang.reflect.InvocationTargetException; 28 | import java.lang.reflect.Method; 29 | 30 | /** 31 | * 处理对象方法的执行 32 | * 33 | * @author xuexiang 34 | * @since 2018/9/18 下午3:25 35 | */ 36 | public class ObjectReceiver extends Receiver { 37 | 38 | private Method mMethod; 39 | 40 | private Object mObject; 41 | 42 | public ObjectReceiver(ObjectWrapper objectWrapper) { 43 | super(objectWrapper); 44 | mObject = OBJECT_CENTER.getObject(getObjectTimeStamp()); 45 | } 46 | 47 | @Override 48 | public void setMethod(MethodWrapper methodWrapper, ParameterWrapper[] parameterWrappers) 49 | throws IPCException { 50 | Method method = TYPE_CENTER.getMethod(mObject.getClass(), methodWrapper); 51 | TypeUtils.validateAccessible(method); 52 | mMethod = method; 53 | } 54 | 55 | @Override 56 | public Object invokeMethod() throws IPCException { 57 | Exception exception; 58 | try { 59 | return mMethod.invoke(mObject, getParameters()); 60 | } catch (IllegalAccessException e) { 61 | exception = e; 62 | } catch (InvocationTargetException e) { 63 | exception = e; 64 | } 65 | exception.printStackTrace(); 66 | throw new IPCException(ErrorCodes.METHOD_INVOCATION_EXCEPTION, 67 | "Error occurs when invoking method " + mMethod + " on " + mObject, exception); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/receiver/impl/UtilityGettingReceiver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.receiver.impl; 18 | 19 | import com.xuexiang.xipc.core.receiver.Receiver; 20 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 21 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 22 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 23 | import com.xuexiang.xipc.exception.IPCException; 24 | import com.xuexiang.xipc.util.TypeUtils; 25 | 26 | /** 27 | * 处理工具类的获取 28 | * 29 | * @author xuexiang 30 | * @since 2018/9/17 下午6:51 31 | */ 32 | public class UtilityGettingReceiver extends Receiver { 33 | 34 | public UtilityGettingReceiver(ObjectWrapper objectWrapper) throws IPCException { 35 | super(objectWrapper); 36 | Class> clazz = TYPE_CENTER.getClassType(objectWrapper); 37 | TypeUtils.validateAccessible(clazz); 38 | } 39 | 40 | @Override 41 | public void setMethod(MethodWrapper methodWrapper, ParameterWrapper[] parameterWrappers) { 42 | 43 | } 44 | 45 | @Override 46 | public Object invokeMethod() { 47 | return null; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/receiver/impl/UtilityReceiver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.receiver.impl; 18 | 19 | import com.xuexiang.xipc.core.receiver.Receiver; 20 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 21 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 22 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 23 | import com.xuexiang.xipc.exception.ErrorCodes; 24 | import com.xuexiang.xipc.exception.IPCException; 25 | import com.xuexiang.xipc.util.TypeUtils; 26 | 27 | import java.lang.reflect.InvocationTargetException; 28 | import java.lang.reflect.Method; 29 | import java.lang.reflect.Modifier; 30 | 31 | /** 32 | * 处理工具类方法的执行 33 | * 34 | * @author xuexiang 35 | * @since 2018/9/17 下午6:49 36 | */ 37 | public class UtilityReceiver extends Receiver { 38 | 39 | private Method mMethod; 40 | 41 | private Class> mClass; 42 | 43 | public UtilityReceiver(ObjectWrapper objectWrapper) throws IPCException { 44 | super(objectWrapper); 45 | Class> clazz = TYPE_CENTER.getClassType(objectWrapper); 46 | TypeUtils.validateAccessible(clazz); 47 | mClass = clazz; 48 | } 49 | 50 | @Override 51 | public void setMethod(MethodWrapper methodWrapper, ParameterWrapper[] parameterWrappers) 52 | throws IPCException { 53 | Method method = TYPE_CENTER.getMethod(mClass, methodWrapper); 54 | if (!Modifier.isStatic(method.getModifiers())) { 55 | throw new IPCException(ErrorCodes.ACCESS_DENIED, 56 | "Only static methods can be invoked on the utility class " + mClass.getName() 57 | + ". Please modify the method: " + mMethod); 58 | } 59 | TypeUtils.validateAccessible(method); 60 | mMethod = method; 61 | } 62 | 63 | @Override 64 | public Object invokeMethod() throws IPCException { 65 | Exception exception; 66 | try { 67 | return mMethod.invoke(null, getParameters()); 68 | } catch (IllegalAccessException e) { 69 | exception = e; 70 | } catch (InvocationTargetException e) { 71 | exception = e; 72 | } 73 | exception.printStackTrace(); 74 | throw new IPCException(ErrorCodes.METHOD_INVOCATION_EXCEPTION, 75 | "Error occurs when invoking method " + mMethod + ".", exception); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/sender/ISender.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.sender; 18 | 19 | import com.xuexiang.xipc.exception.IPCException; 20 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 21 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 22 | 23 | import java.lang.reflect.Method; 24 | 25 | /** 26 | * 注册方法的请求 27 | * 28 | * @author xuexiang 29 | * @since 2018/9/17 下午2:58 30 | */ 31 | public interface ISender { 32 | 33 | /** 34 | * 获取请求方法的包装器 35 | * 36 | * @param method 37 | * @param parameterWrappers 38 | * @return 39 | * @throws IPCException 40 | */ 41 | MethodWrapper getMethodWrapper(Method method, ParameterWrapper[] parameterWrappers) throws IPCException; 42 | } 43 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/sender/impl/InstanceCreatingSender.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.sender.impl; 18 | 19 | import com.xuexiang.xipc.core.channel.IPCService; 20 | import com.xuexiang.xipc.core.sender.Sender; 21 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 22 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 23 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 24 | 25 | import java.lang.reflect.Method; 26 | 27 | /** 28 | * 请求创建实例(服务发现) 29 | * 30 | * @author xuexiang 31 | * @since 2018/9/18 下午3:38 32 | */ 33 | public class InstanceCreatingSender extends Sender { 34 | 35 | private Class>[] mConstructorParameterTypes; 36 | 37 | public InstanceCreatingSender(Class extends IPCService> service, ObjectWrapper object) { 38 | super(service, object); 39 | } 40 | 41 | @Override 42 | public MethodWrapper getMethodWrapper(Method method, ParameterWrapper[] parameterWrappers) { 43 | int length = parameterWrappers == null ? 0 : parameterWrappers.length; 44 | mConstructorParameterTypes = new Class>[length]; 45 | for (int i = 0; i < length; ++i) { 46 | try { 47 | ParameterWrapper parameterWrapper = parameterWrappers[i]; 48 | mConstructorParameterTypes[i] = parameterWrapper == null ? null : parameterWrapper.getClassType(); 49 | } catch (Exception e) { 50 | e.printStackTrace(); 51 | } 52 | } 53 | return new MethodWrapper(mConstructorParameterTypes); 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/sender/impl/InstanceGettingSender.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.sender.impl; 18 | 19 | import com.xuexiang.xipc.core.channel.IPCService; 20 | import com.xuexiang.xipc.core.sender.Sender; 21 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 22 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 23 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 24 | import com.xuexiang.xipc.exception.ErrorCodes; 25 | import com.xuexiang.xipc.exception.IPCException; 26 | import com.xuexiang.xipc.util.SerializeUtils; 27 | 28 | import java.lang.reflect.Method; 29 | 30 | /** 31 | * 请求获取单例 32 | * 33 | * @author xuexiang 34 | * @since 2018/9/18 下午3:38 35 | */ 36 | public class InstanceGettingSender extends Sender { 37 | 38 | public InstanceGettingSender(Class extends IPCService> service, ObjectWrapper object) { 39 | super(service, object); 40 | } 41 | 42 | @Override 43 | protected void setParameterWrappers(ParameterWrapper[] parameterWrappers) { 44 | int length = parameterWrappers.length; 45 | ParameterWrapper[] tmp = new ParameterWrapper[length - 1]; 46 | System.arraycopy(parameterWrappers, 1, tmp, 0, length - 1); 47 | super.setParameterWrappers(tmp); 48 | } 49 | 50 | @Override 51 | public MethodWrapper getMethodWrapper(Method method, ParameterWrapper[] parameterWrappers) throws IPCException { 52 | ParameterWrapper parameterWrapper = parameterWrappers[0]; 53 | String methodName; 54 | try { 55 | methodName = SerializeUtils.decode(parameterWrapper.getData(), String.class); 56 | } catch (IPCException e) { 57 | e.printStackTrace(); 58 | throw new IPCException(ErrorCodes.GSON_DECODE_EXCEPTION, 59 | "Error occurs when decoding the method name."); 60 | } 61 | int length = parameterWrappers.length; 62 | Class>[] parameterTypes = new Class[length - 1]; 63 | for (int i = 1; i < length; ++i) { 64 | parameterWrapper = parameterWrappers[i]; 65 | parameterTypes[i - 1] = parameterWrapper == null ? null : parameterWrapper.getClassType(); 66 | } 67 | return new MethodWrapper(methodName, parameterTypes); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/sender/impl/ObjectSender.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.sender.impl; 18 | 19 | import com.xuexiang.xipc.core.channel.IPCService; 20 | import com.xuexiang.xipc.core.sender.Sender; 21 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 22 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 23 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 24 | 25 | import java.lang.reflect.Method; 26 | 27 | /** 28 | * 请求执行对象的方法 29 | * 30 | * @author xuexiang 31 | * @since 2018/9/18 下午3:33 32 | */ 33 | public class ObjectSender extends Sender { 34 | 35 | public ObjectSender(Class extends IPCService> service, ObjectWrapper object) { 36 | super(service, object); 37 | } 38 | 39 | @Override 40 | public MethodWrapper getMethodWrapper(Method method, ParameterWrapper[] parameterWrappers) { 41 | return new MethodWrapper(method); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/sender/impl/SenderDesignator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.sender.impl; 18 | 19 | import com.xuexiang.xipc.core.channel.IPCService; 20 | import com.xuexiang.xipc.core.sender.Sender; 21 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 22 | 23 | /** 24 | * 请求动作的构建器 25 | * 26 | * @author xuexiang 27 | * @since 2018/9/18 下午3:35 28 | */ 29 | public class SenderDesignator { 30 | 31 | /** 32 | * 新建实例,服务发现 33 | */ 34 | public static final int TYPE_NEW_INSTANCE = 0; 35 | 36 | /** 37 | * 获取单例 38 | */ 39 | public static final int TYPE_GET_INSTANCE = 1; 40 | 41 | /** 42 | * 获取工具类 43 | */ 44 | public static final int TYPE_GET_UTILITY_CLASS = 2; 45 | 46 | /** 47 | * 执行对象的方法 48 | */ 49 | public static final int TYPE_INVOKE_METHOD = 3; 50 | 51 | public static Sender getPostOffice(Class extends IPCService> service, int type, ObjectWrapper object) { 52 | switch (type) { 53 | case TYPE_NEW_INSTANCE: 54 | return new InstanceCreatingSender(service, object); 55 | case TYPE_GET_INSTANCE: 56 | return new InstanceGettingSender(service, object); 57 | case TYPE_GET_UTILITY_CLASS: 58 | return new UtilityGettingSender(service, object); 59 | case TYPE_INVOKE_METHOD: 60 | return new ObjectSender(service, object); 61 | default: 62 | throw new IllegalArgumentException("Type " + type + " is not supported."); 63 | } 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/sender/impl/UtilityGettingSender.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.sender.impl; 18 | 19 | import com.xuexiang.xipc.core.channel.IPCService; 20 | import com.xuexiang.xipc.core.sender.Sender; 21 | import com.xuexiang.xipc.core.wrapper.MethodWrapper; 22 | import com.xuexiang.xipc.core.wrapper.ObjectWrapper; 23 | import com.xuexiang.xipc.core.wrapper.ParameterWrapper; 24 | 25 | import java.lang.reflect.Method; 26 | 27 | /** 28 | * 请求获取工具类 29 | * 30 | * @author xuexiang 31 | * @since 2018/9/18 下午3:37 32 | */ 33 | public class UtilityGettingSender extends Sender { 34 | 35 | public UtilityGettingSender(Class extends IPCService> service, ObjectWrapper object) { 36 | super(service, object); 37 | } 38 | 39 | @Override 40 | public MethodWrapper getMethodWrapper(Method method, ParameterWrapper[] parameterWrappers) { 41 | return null; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/wrapper/BaseWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.wrapper; 18 | 19 | import android.os.Parcel; 20 | 21 | /** 22 | * 基础包装器,用于IPC信息传输 23 | * 24 | * @author xuexiang 25 | * @since 2018/9/14 下午4:14 26 | */ 27 | public class BaseWrapper { 28 | 29 | /** 30 | * 是不是类的名字【true: 类的名字, false:用注解定义的映射】 31 | */ 32 | private boolean mIsName; 33 | 34 | /** 35 | * 名字 36 | */ 37 | private String mName; 38 | 39 | /** 40 | * 构造方法 41 | * @param isName 42 | * @param name 43 | */ 44 | protected void setName(boolean isName, String name) { 45 | if (name == null) { 46 | throw new IllegalArgumentException("name can not be null"); 47 | } 48 | mIsName = isName; 49 | mName = name; 50 | } 51 | 52 | public void writeToParcel(Parcel parcel, int flags) { 53 | parcel.writeInt(mIsName ? 1 : 0); 54 | parcel.writeString(mName); 55 | } 56 | 57 | public void readFromParcel(Parcel in) { 58 | mIsName = in.readInt() == 1; 59 | mName = in.readString(); 60 | } 61 | 62 | public boolean isName() { 63 | return mIsName; 64 | } 65 | 66 | public String getName() { 67 | return mName; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/wrapper/ObjectWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.wrapper; 18 | 19 | import android.os.Parcel; 20 | import android.os.Parcelable; 21 | 22 | import com.xuexiang.xipc.annotation.ClassName; 23 | import com.xuexiang.xipc.util.TimeStampGenerator; 24 | import com.xuexiang.xipc.util.TypeUtils; 25 | 26 | /** 27 | * 对象的包装器(用于方法参数的包装) 28 | * 29 | * @author xuexiang 30 | * @since 2018/9/14 下午6:16 31 | */ 32 | public class ObjectWrapper extends BaseWrapper implements Parcelable { 33 | 34 | /** 35 | * 请求创建新的实例对象【注册服务】 36 | */ 37 | public static final int TYPE_OBJECT_TO_NEW = 0; 38 | 39 | /** 40 | * 请求获取单例 41 | */ 42 | public static final int TYPE_OBJECT_TO_GET = 1; 43 | 44 | /** 45 | * 请求获取工具类 46 | */ 47 | public static final int TYPE_CLASS_TO_GET = 2; 48 | 49 | /** 50 | * 处理对象方法的执行 51 | */ 52 | public static final int TYPE_OBJECT = 3; 53 | 54 | /** 55 | * 处理工具类(静态)方法的执行 56 | */ 57 | public static final int TYPE_CLASS = 4; 58 | 59 | /** 60 | * 自增long型,标记对象 61 | */ 62 | private long mTimeStamp; 63 | 64 | //only used here 65 | private Class> mClass; 66 | 67 | /** 68 | * 对象的包装类型 69 | */ 70 | private int mType; 71 | 72 | public static final Creator CREATOR 73 | = new Creator() { 74 | public ObjectWrapper createFromParcel(Parcel in) { 75 | ObjectWrapper objectWrapper = new ObjectWrapper(); 76 | objectWrapper.readFromParcel(in); 77 | return objectWrapper; 78 | } 79 | 80 | public ObjectWrapper[] newArray(int size) { 81 | return new ObjectWrapper[size]; 82 | } 83 | }; 84 | 85 | private ObjectWrapper() { 86 | } 87 | 88 | /** 89 | * 包装对象 90 | * 91 | * @param clazz 对象的类 92 | * @param type 对象的类型 93 | */ 94 | public ObjectWrapper(Class> clazz, int type) { 95 | setName(!clazz.isAnnotationPresent(ClassName.class), TypeUtils.getClassId(clazz)); 96 | mClass = clazz; 97 | mTimeStamp = TimeStampGenerator.getTimeStamp(); 98 | mType = type; 99 | } 100 | 101 | 102 | @Override 103 | public int describeContents() { 104 | return 0; 105 | } 106 | 107 | @Override 108 | public void writeToParcel(Parcel parcel, int flags) { 109 | super.writeToParcel(parcel, flags); 110 | parcel.writeLong(mTimeStamp); 111 | parcel.writeInt(mType); 112 | } 113 | 114 | public void readFromParcel(Parcel in) { 115 | super.readFromParcel(in); 116 | mTimeStamp = in.readLong(); 117 | mType = in.readInt(); 118 | } 119 | 120 | public long getTimeStamp() { 121 | return mTimeStamp; 122 | } 123 | 124 | public Class> getObjectClass() { 125 | return mClass; 126 | } 127 | 128 | public void setType(int type) { 129 | mType = type; 130 | } 131 | 132 | public int getType() { 133 | return mType; 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/wrapper/ParameterWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.wrapper; 18 | 19 | import android.os.Parcel; 20 | import android.os.Parcelable; 21 | 22 | import com.xuexiang.xipc.annotation.ClassName; 23 | import com.xuexiang.xipc.exception.IPCException; 24 | import com.xuexiang.xipc.util.SerializeUtils; 25 | import com.xuexiang.xipc.util.TypeUtils; 26 | 27 | /** 28 | * 参数的包装器 29 | * 30 | * @author xuexiang 31 | * @since 2018/9/14 下午6:14 32 | */ 33 | public class ParameterWrapper extends BaseWrapper implements Parcelable { 34 | 35 | /** 36 | * 参数的数据 37 | */ 38 | private String mData; 39 | 40 | //only used here. 41 | /** 42 | * 参数的类型 43 | */ 44 | private Class> mClass; 45 | 46 | public static final Creator CREATOR 47 | = new Creator() { 48 | public ParameterWrapper createFromParcel(Parcel in) { 49 | ParameterWrapper parameterWrapper = new ParameterWrapper(); 50 | parameterWrapper.readFromParcel(in); 51 | return parameterWrapper; 52 | } 53 | 54 | public ParameterWrapper[] newArray(int size) { 55 | return new ParameterWrapper[size]; 56 | } 57 | }; 58 | 59 | private ParameterWrapper() { 60 | 61 | } 62 | 63 | /** 64 | * 包装参数 65 | * 66 | * @param clazz 67 | * @param object 68 | * @throws IPCException 69 | */ 70 | public ParameterWrapper(Class> clazz, Object object) throws IPCException { 71 | mClass = clazz; 72 | setName(!clazz.isAnnotationPresent(ClassName.class), TypeUtils.getClassId(clazz)); 73 | mData = SerializeUtils.encode(object); 74 | } 75 | 76 | /** 77 | * 包装参数 78 | * 79 | * @param object 80 | * @throws IPCException 81 | */ 82 | public ParameterWrapper(Object object) throws IPCException { 83 | if (object == null) { 84 | setName(false, ""); 85 | mData = null; 86 | mClass = null; 87 | } else { 88 | Class> clazz = object.getClass(); 89 | mClass = clazz; 90 | setName(!clazz.isAnnotationPresent(ClassName.class), TypeUtils.getClassId(clazz)); 91 | mData = SerializeUtils.encode(object); 92 | } 93 | } 94 | 95 | @Override 96 | public int describeContents() { 97 | return 0; 98 | } 99 | 100 | @Override 101 | public void writeToParcel(Parcel parcel, int flags) { 102 | super.writeToParcel(parcel, flags); 103 | parcel.writeString(mData); 104 | } 105 | 106 | public void readFromParcel(Parcel in) { 107 | super.readFromParcel(in); 108 | mData = in.readString(); 109 | } 110 | 111 | public String getData() { 112 | return mData; 113 | } 114 | 115 | public boolean isNull() { 116 | return mData == null; 117 | } 118 | 119 | public Class> getClassType() { 120 | return mClass; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/core/wrapper/TypeWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.core.wrapper; 18 | 19 | import android.os.Parcel; 20 | import android.os.Parcelable; 21 | 22 | import com.xuexiang.xipc.annotation.ClassName; 23 | import com.xuexiang.xipc.util.TypeUtils; 24 | 25 | /** 26 | * 类型的包装器(用于方法的包装) 27 | * 28 | * @author xuexiang 29 | * @since 2018/9/14 下午4:18 30 | */ 31 | public class TypeWrapper extends BaseWrapper implements Parcelable { 32 | 33 | public static final Creator CREATOR 34 | = new Creator() { 35 | public TypeWrapper createFromParcel(Parcel in) { 36 | TypeWrapper typeWrapper = new TypeWrapper(); 37 | typeWrapper.readFromParcel(in); 38 | return typeWrapper; 39 | } 40 | public TypeWrapper[] newArray(int size) { 41 | return new TypeWrapper[size]; 42 | } 43 | }; 44 | 45 | private TypeWrapper() { 46 | 47 | } 48 | 49 | /** 50 | * 包装类型 51 | * @param clazz 52 | */ 53 | public TypeWrapper(Class> clazz) { 54 | setName(!clazz.isAnnotationPresent(ClassName.class), TypeUtils.getClassId(clazz)); 55 | } 56 | 57 | @Override 58 | public int describeContents() { 59 | return 0; 60 | } 61 | 62 | @Override 63 | public void writeToParcel(Parcel parcel, int flags) { 64 | super.writeToParcel(parcel, flags); 65 | } 66 | 67 | public void readFromParcel(Parcel in) { 68 | super.readFromParcel(in); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/exception/ErrorCodes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.exception; 18 | 19 | /** 20 | * 错误码 21 | * 22 | * @author xuexiang 23 | * @since 2018/9/14 下午4:07 24 | */ 25 | public class ErrorCodes { 26 | 27 | public static final int SUCCESS = 0; 28 | 29 | public static final int REMOTE_EXCEPTION = SUCCESS + 1; 30 | 31 | public static final int SERVICE_UNAVAILABLE = REMOTE_EXCEPTION + 1; 32 | 33 | public static final int NULL_POINTER_EXCEPTION = SERVICE_UNAVAILABLE + 1; 34 | 35 | public static final int ILLEGAL_PARAMETER_EXCEPTION = NULL_POINTER_EXCEPTION + 1; 36 | 37 | public static final int ACCESS_DENIED = ILLEGAL_PARAMETER_EXCEPTION + 1; 38 | 39 | public static final int GSON_ENCODE_EXCEPTION = ACCESS_DENIED + 1; 40 | 41 | public static final int GSON_DECODE_EXCEPTION = GSON_ENCODE_EXCEPTION + 1; 42 | 43 | /** 44 | * 方法匹配过多 45 | */ 46 | public static final int TOO_MANY_MATCHING_METHODS = GSON_DECODE_EXCEPTION + 1; 47 | 48 | /** 49 | * 方法参数不匹配 50 | */ 51 | public static final int METHOD_PARAMETER_NOT_MATCHING = TOO_MANY_MATCHING_METHODS + 1; 52 | 53 | /** 54 | * 方法的返回类型不匹配 55 | */ 56 | public static final int METHOD_RETURN_TYPE_NOT_MATCHING = METHOD_PARAMETER_NOT_MATCHING + 1; 57 | 58 | /** 59 | * 获取实例方法匹配过多 60 | */ 61 | public static final int TOO_MANY_MATCHING_METHODS_FOR_GETTING_INSTANCE = METHOD_RETURN_TYPE_NOT_MATCHING + 1; 62 | 63 | /** 64 | * 获取实例返回值的类型不对 65 | */ 66 | public static final int GETTING_INSTANCE_RETURN_TYPE_ERROR = TOO_MANY_MATCHING_METHODS_FOR_GETTING_INSTANCE + 1; 67 | 68 | /** 69 | * 获取实例的方法未找到 70 | */ 71 | public static final int GETTING_INSTANCE_METHOD_NOT_FOUND = GETTING_INSTANCE_RETURN_TYPE_ERROR + 1; 72 | 73 | /** 74 | * 创建实例的构造方法匹配过多 75 | */ 76 | public static final int TOO_MANY_MATCHING_CONSTRUCTORS_FOR_CREATING_INSTANCE = GETTING_INSTANCE_METHOD_NOT_FOUND + 1; 77 | 78 | /** 79 | * 构造方法未找到 80 | */ 81 | public static final int CONSTRUCTOR_NOT_FOUND = TOO_MANY_MATCHING_CONSTRUCTORS_FOR_CREATING_INSTANCE + 1; 82 | /** 83 | * 类未找到 84 | */ 85 | public static final int CLASS_NOT_FOUND = CONSTRUCTOR_NOT_FOUND + 1; 86 | /** 87 | * 方法未找到 88 | */ 89 | public static final int METHOD_NOT_FOUND = CLASS_NOT_FOUND + 1; 90 | /** 91 | * 方法执行错误 92 | */ 93 | public static final int METHOD_INVOCATION_EXCEPTION = METHOD_NOT_FOUND + 1; 94 | 95 | public static final int CLASS_WITH_PROCESS = METHOD_INVOCATION_EXCEPTION + 1; 96 | 97 | public static final int METHOD_WITH_PROCESS = CLASS_WITH_PROCESS + 1; 98 | 99 | public static final int METHOD_GET_INSTANCE_NOT_STATIC = METHOD_WITH_PROCESS + 1; 100 | 101 | public static final int CALLBACK_NOT_ALIVE = METHOD_GET_INSTANCE_NOT_STATIC + 1; 102 | 103 | private ErrorCodes() { 104 | 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/exception/IPCException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.exception; 18 | 19 | /** 20 | * IPC异常 21 | * 22 | * @author xuexiang 23 | * @since 2018/9/14 下午4:06 24 | */ 25 | public class IPCException extends Exception { 26 | 27 | /** 28 | * 错误码 29 | */ 30 | private int mErrorCode; 31 | 32 | public IPCException(int errorCode, String errorMessage) { 33 | super(errorMessage); 34 | mErrorCode = errorCode; 35 | } 36 | 37 | public IPCException(int errorCode, String errorMessage, Throwable t) { 38 | super(errorMessage, t); 39 | mErrorCode = errorCode; 40 | } 41 | 42 | public int getErrorCode() { 43 | return mErrorCode; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/logs/ILogger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.logs; 18 | 19 | /** 20 | * 简易的日志接口 21 | * 22 | * @author xuexiang 23 | * @since 2018/6/29 下午7:57 24 | */ 25 | public interface ILogger { 26 | 27 | /** 28 | * 打印信息 29 | * 30 | * @param priority 优先级 31 | * @param tag 标签 32 | * @param message 信息 33 | * @param t 出错信息 34 | */ 35 | void log(int priority, String tag, String message, Throwable t); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/logs/LogcatLogger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.logs; 18 | 19 | import android.support.annotation.NonNull; 20 | import android.util.Log; 21 | 22 | import java.io.PrintWriter; 23 | import java.io.StringWriter; 24 | 25 | /** 26 | * 默认Logcat日志记录 27 | * 28 | * @author xuexiang 29 | * @since 2018/6/29 下午7:57 30 | */ 31 | public class LogcatLogger implements ILogger { 32 | 33 | /** 34 | * logcat里日志的最大长度. 35 | */ 36 | private static final int MAX_LOG_LENGTH = 4000; 37 | 38 | /** 39 | * 打印信息 40 | * 41 | * @param priority 优先级 42 | * @param tag 标签 43 | * @param message 信息 44 | * @param t 出错信息 45 | */ 46 | @Override 47 | public void log(int priority, String tag, String message, Throwable t) { 48 | if (message != null && message.length() == 0) { 49 | message = null; 50 | } 51 | if (message == null) { 52 | if (t == null) { 53 | return; // Swallow message if it's null and there's no throwable. 54 | } 55 | message = getStackTraceString(t); 56 | } else { 57 | if (t != null) { 58 | message += "\n" + getStackTraceString(t); 59 | } 60 | } 61 | 62 | log(priority, tag, message); 63 | } 64 | 65 | private String getStackTraceString(Throwable t) { 66 | // Don't replace this with Log.getStackTraceString() - it hides 67 | // UnknownHostException, which is not what we want. 68 | StringWriter sw = new StringWriter(256); 69 | PrintWriter pw = new PrintWriter(sw, false); 70 | t.printStackTrace(pw); 71 | pw.flush(); 72 | return sw.toString(); 73 | } 74 | 75 | 76 | /** 77 | * 使用LogCat输出日志,字符长度超过4000则自动换行. 78 | * 79 | * @param priority 优先级 80 | * @param tag 标签 81 | * @param message 信息 82 | */ 83 | public void log(int priority, String tag, String message) { 84 | int subNum = message.length() / MAX_LOG_LENGTH; 85 | if (subNum > 0) { 86 | int index = 0; 87 | for (int i = 0; i < subNum; i++) { 88 | int lastIndex = index + MAX_LOG_LENGTH; 89 | String sub = message.substring(index, lastIndex); 90 | logSub(priority, tag, sub); 91 | index = lastIndex; 92 | } 93 | logSub(priority, tag, message.substring(index, message.length())); 94 | } else { 95 | logSub(priority, tag, message); 96 | } 97 | } 98 | 99 | 100 | /** 101 | * 使用LogCat输出日志. 102 | * 103 | * @param priority 优先级 104 | * @param tag 标签 105 | * @param sub 信息 106 | */ 107 | private void logSub(int priority, @NonNull String tag, @NonNull String sub) { 108 | switch (priority) { 109 | case Log.VERBOSE: 110 | Log.v(tag, sub); 111 | break; 112 | case Log.DEBUG: 113 | Log.d(tag, sub); 114 | break; 115 | case Log.INFO: 116 | Log.i(tag, sub); 117 | break; 118 | case Log.WARN: 119 | Log.w(tag, sub); 120 | break; 121 | case Log.ERROR: 122 | Log.e(tag, sub); 123 | break; 124 | case Log.ASSERT: 125 | Log.wtf(tag, sub); 126 | break; 127 | default: 128 | Log.v(tag, sub); 129 | break; 130 | } 131 | } 132 | 133 | } 134 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/CallbackManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import android.support.v4.util.Pair; 20 | import android.util.Log; 21 | 22 | import com.xuexiang.xipc.logs.IPCLog; 23 | 24 | import java.lang.ref.WeakReference; 25 | import java.util.concurrent.ConcurrentHashMap; 26 | 27 | import static android.content.ContentValues.TAG; 28 | 29 | /** 30 | * 回调管理 31 | * 32 | * @author xuexiang 33 | * @since 2018/9/17 下午5:03 34 | */ 35 | public class CallbackManager { 36 | 37 | private static final int MAX_INDEX = 10; 38 | 39 | private static volatile CallbackManager sInstance = null; 40 | 41 | private final ConcurrentHashMap mCallbackWrappers; 42 | 43 | private CallbackManager() { 44 | mCallbackWrappers = new ConcurrentHashMap<>(); 45 | } 46 | 47 | public static CallbackManager getInstance() { 48 | if (sInstance == null) { 49 | synchronized (CallbackManager.class) { 50 | if (sInstance == null) { 51 | sInstance = new CallbackManager(); 52 | } 53 | } 54 | } 55 | return sInstance; 56 | } 57 | 58 | private static long getKey(long timeStamp, int index) { 59 | if (index >= MAX_INDEX) { 60 | throw new IllegalArgumentException("Index should be less than " + MAX_INDEX); 61 | } 62 | return timeStamp * MAX_INDEX + index; 63 | } 64 | 65 | public void addCallback(long timeStamp, int index, Object callback, boolean isWeakRef, boolean uiThread) { 66 | long key = getKey(timeStamp, index); 67 | mCallbackWrappers.put(key, new CallbackWrapper(isWeakRef, callback, uiThread)); 68 | } 69 | 70 | public Pair getCallback(long timeStamp, int index) { 71 | long key = getKey(timeStamp, index); 72 | CallbackWrapper callbackWrapper = mCallbackWrappers.get(key); 73 | if (callbackWrapper == null) { 74 | return null; 75 | } 76 | Pair pair = callbackWrapper.get(); 77 | if (pair.second == null) { 78 | mCallbackWrappers.remove(key); 79 | } 80 | return pair; 81 | } 82 | 83 | public void removeCallback(long timeStamp, int index) { 84 | long key = getKey(timeStamp, index); 85 | if (mCallbackWrappers.remove(key) == null) { 86 | IPCLog.e("[CallbackManager] An error occurs in the callback GC."); 87 | } 88 | } 89 | 90 | private static class CallbackWrapper { 91 | 92 | private Object mCallback; 93 | 94 | private boolean mUiThread; 95 | 96 | CallbackWrapper(boolean isWeakRef, Object callback, boolean uiThread) { 97 | if (isWeakRef) { 98 | mCallback = new WeakReference<>(callback); 99 | } else { 100 | mCallback = callback; 101 | } 102 | mUiThread = uiThread; 103 | } 104 | 105 | public Pair get() { 106 | Object callback; 107 | if (mCallback instanceof WeakReference) { 108 | callback = ((WeakReference) mCallback).get(); 109 | } else { 110 | callback = mCallback; 111 | } 112 | return new Pair<>(mUiThread, callback); 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/IPCCallbackGc.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import android.os.RemoteException; 20 | import android.support.v4.util.Pair; 21 | 22 | import com.xuexiang.xipc.core.channel.IIPCServiceCallback; 23 | 24 | import java.lang.ref.PhantomReference; 25 | import java.lang.ref.ReferenceQueue; 26 | import java.util.ArrayList; 27 | import java.util.HashMap; 28 | import java.util.Map; 29 | import java.util.Set; 30 | import java.util.concurrent.ConcurrentHashMap; 31 | 32 | /** 33 | * IPC回调接口的资源回收 34 | * 35 | * This works in the main process. 36 | */ 37 | public class IPCCallbackGc { 38 | 39 | private static volatile IPCCallbackGc sInstance = null; 40 | /** 41 | * 引用队列 42 | */ 43 | private final ReferenceQueue mReferenceQueue; 44 | 45 | /** 46 | * 引用信息存储 47 | */ 48 | private final ConcurrentHashMap, Triple> mTimeStamps; 49 | 50 | private IPCCallbackGc() { 51 | mReferenceQueue = new ReferenceQueue<>(); 52 | mTimeStamps = new ConcurrentHashMap<>(); 53 | } 54 | 55 | public static IPCCallbackGc getInstance() { 56 | if (sInstance == null) { 57 | synchronized (IPCCallbackGc.class) { 58 | if (sInstance == null) { 59 | sInstance = new IPCCallbackGc(); 60 | } 61 | } 62 | } 63 | return sInstance; 64 | } 65 | 66 | /** 67 | * 资源回收 68 | */ 69 | private void gc() { 70 | synchronized (mReferenceQueue) { 71 | PhantomReference reference; 72 | Triple triple; 73 | HashMap, ArrayList>> timeStamps 74 | = new HashMap<>(); 75 | while ((reference = (PhantomReference) mReferenceQueue.poll()) != null) { 76 | triple = mTimeStamps.remove(reference); 77 | if (triple != null) { 78 | Pair, ArrayList> tmp = timeStamps.get(triple.first); 79 | if (tmp == null) { 80 | tmp = new Pair<>(new ArrayList(), new ArrayList()); 81 | timeStamps.put(triple.first, tmp); 82 | } 83 | tmp.first.add(triple.second); 84 | tmp.second.add(triple.third); 85 | } 86 | } 87 | Set, ArrayList>>> set = timeStamps.entrySet(); 88 | for (Map.Entry, ArrayList>> entry : set) { 89 | Pair, ArrayList> values = entry.getValue(); 90 | if (!values.first.isEmpty()) { 91 | try { 92 | entry.getKey().gc(values.first, values.second); 93 | } catch (RemoteException e) { 94 | e.printStackTrace(); 95 | } 96 | } 97 | } 98 | } 99 | } 100 | 101 | /** 102 | * 注册IPC接口回调,以方便资源回收 103 | * 104 | * @param callback 回调 105 | * @param object 回调接口的返回值 106 | * @param timeStamp 序号 107 | * @param index 索引 108 | */ 109 | public void register(IIPCServiceCallback callback, Object object, long timeStamp, int index) { 110 | gc(); 111 | //使用虚引用 112 | mTimeStamps.put(new PhantomReference<>(object, mReferenceQueue), Triple.create(callback, timeStamp, index)); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/IPCGc.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import com.xuexiang.xipc.core.channel.Channel; 20 | import com.xuexiang.xipc.core.channel.IPCService; 21 | 22 | import java.lang.ref.PhantomReference; 23 | import java.lang.ref.ReferenceQueue; 24 | import java.util.ArrayList; 25 | import java.util.HashMap; 26 | import java.util.Map; 27 | import java.util.Set; 28 | import java.util.concurrent.ConcurrentHashMap; 29 | 30 | /** 31 | * IPC资源回收 32 | * 33 | * This works in the remote process. 34 | */ 35 | public class IPCGc { 36 | 37 | private static volatile IPCGc sInstance = null; 38 | 39 | private final ReferenceQueue mReferenceQueue; 40 | 41 | private static final Channel CHANNEL = Channel.getInstance(); 42 | 43 | private final ConcurrentHashMap, Long> mTimeStamps; 44 | 45 | private final ConcurrentHashMap> mServices; 46 | 47 | private IPCGc() { 48 | mReferenceQueue = new ReferenceQueue<>(); 49 | mTimeStamps = new ConcurrentHashMap<>(); 50 | mServices = new ConcurrentHashMap<>(); 51 | } 52 | 53 | public static IPCGc getInstance() { 54 | if (sInstance == null) { 55 | synchronized (IPCGc.class) { 56 | if (sInstance == null) { 57 | sInstance = new IPCGc(); 58 | } 59 | } 60 | } 61 | return sInstance; 62 | } 63 | 64 | private void gc() { 65 | synchronized (mReferenceQueue) { 66 | PhantomReference reference; 67 | Long timeStamp; 68 | HashMap, ArrayList> timeStamps 69 | = new HashMap<>(); 70 | while ((reference = (PhantomReference) mReferenceQueue.poll()) != null) { 71 | //After a long time, the program can reach here. 72 | timeStamp = mTimeStamps.remove(reference); 73 | if (timeStamp != null) { 74 | Class extends IPCService> clazz = mServices.remove(timeStamp); 75 | if (clazz != null) { 76 | ArrayList tmp = timeStamps.get(clazz); 77 | if (tmp == null) { 78 | tmp = new ArrayList<>(); 79 | timeStamps.put(clazz, tmp); 80 | } 81 | tmp.add(timeStamp); 82 | } 83 | } 84 | } 85 | Set, ArrayList>> set = timeStamps.entrySet(); 86 | for (Map.Entry, ArrayList> entry : set) { 87 | ArrayList values = entry.getValue(); 88 | if (!values.isEmpty()) { 89 | CHANNEL.gc(entry.getKey(), values); 90 | } 91 | } 92 | } 93 | } 94 | 95 | /** 96 | * 注册IPC服务接口,以方便资源回收 97 | * @param service ipc服务 98 | * @param object 99 | * @param timeStamp 100 | */ 101 | public void register(Class extends IPCService> service, Object object, Long timeStamp) { 102 | gc(); 103 | mTimeStamps.put(new PhantomReference<>(object, mReferenceQueue), timeStamp); 104 | mServices.put(timeStamp, service); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/IPCPoolExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import java.util.concurrent.Callable; 20 | import java.util.concurrent.ExecutorService; 21 | import java.util.concurrent.Executors; 22 | import java.util.concurrent.Future; 23 | 24 | /** 25 | * 线程池管理 26 | * 27 | * @author xuexiang 28 | * @since 2018/8/15 上午2:42 29 | */ 30 | public class IPCPoolExecutor { 31 | 32 | private static volatile IPCPoolExecutor sInstance; 33 | /** 34 | * 线程池 35 | */ 36 | private ExecutorService mExecutorService; 37 | 38 | private IPCPoolExecutor() { 39 | mExecutorService = Executors.newFixedThreadPool(2); 40 | } 41 | 42 | /** 43 | * 获取线程池管理 44 | * 45 | * @return 46 | */ 47 | public static IPCPoolExecutor get() { 48 | if (sInstance == null) { 49 | synchronized (IPCPoolExecutor.class) { 50 | if (sInstance == null) { 51 | sInstance = new IPCPoolExecutor(); 52 | } 53 | } 54 | } 55 | return sInstance; 56 | } 57 | 58 | /** 59 | * 设置线程池 60 | * 61 | * @param service 线程池 62 | */ 63 | public void setExecutorService(final ExecutorService service) { 64 | if (!mExecutorService.isShutdown()) { 65 | mExecutorService.shutdownNow(); 66 | } 67 | mExecutorService = service; 68 | } 69 | 70 | /** 71 | * 提交一个Runnable任务用于执行 72 | * 73 | * @param task 任务 74 | * @return 表示任务等待完成的Future, 该Future的{@code get}方法在成功完成时将会返回null结果。 75 | */ 76 | public Future> addTask(final Runnable task) { 77 | return mExecutorService.submit(task); 78 | } 79 | 80 | /** 81 | * 提交一个Runnable任务用于执行 82 | * 83 | * @param task 任务 84 | * @param result 返回的结果 85 | * @param 泛型 86 | * @return 表示任务等待完成的Future, 该Future的{@code get}方法在成功完成时将会返回该任务的结果。 87 | */ 88 | public Future addTask(final Runnable task, final T result) { 89 | return mExecutorService.submit(task, result); 90 | } 91 | 92 | /** 93 | * 提交一个Callable任务用于执行 94 | * 如果想立即阻塞任务的等待,则可以使用{@code result = exec.submit(aCallable).get();}形式的构造。 95 | * 96 | * @param task 任务 97 | * @param 泛型 98 | * @return 表示任务等待完成的Future, 该Future的{@code get}方法在成功完成时将会返回该任务的结果。 99 | */ 100 | public Future addTask(final Callable task) { 101 | return mExecutorService.submit(task); 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/ObjectCenter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import com.xuexiang.xipc.logs.IPCLog; 20 | 21 | import java.util.List; 22 | import java.util.concurrent.ConcurrentHashMap; 23 | 24 | /** 25 | * 对象存储仓库,存放对应对象的映射 26 | * 27 | * @author xuexiang 28 | * @since 2018/9/17 下午4:06 29 | */ 30 | public class ObjectCenter { 31 | 32 | private static volatile ObjectCenter sInstance = null; 33 | 34 | private final ConcurrentHashMap mObjects; 35 | 36 | private ObjectCenter() { 37 | mObjects = new ConcurrentHashMap<>(); 38 | } 39 | 40 | public static ObjectCenter getInstance() { 41 | if (sInstance == null) { 42 | synchronized (ObjectCenter.class) { 43 | if (sInstance == null) { 44 | sInstance = new ObjectCenter(); 45 | } 46 | } 47 | } 48 | return sInstance; 49 | } 50 | 51 | /** 52 | * 取出对象 53 | * 54 | * @param timeStamp 55 | * @return 56 | */ 57 | public Object getObject(Long timeStamp) { 58 | return mObjects.get(timeStamp); 59 | } 60 | 61 | /** 62 | * 存放对象 63 | * 64 | * @param timeStamp 65 | * @param object 66 | */ 67 | public void putObject(long timeStamp, Object object) { 68 | mObjects.put(timeStamp, object); 69 | } 70 | 71 | /** 72 | * 删除对象 73 | * 74 | * @param timeStamps 75 | */ 76 | public void deleteObjects(List timeStamps) { 77 | for (Long timeStamp : timeStamps) { 78 | if (mObjects.remove(timeStamp) == null) { 79 | IPCLog.e("[ObjectCenter] An error occurs in the GC."); 80 | } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/SerializeUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import com.google.gson.Gson; 20 | import com.xuexiang.xipc.exception.ErrorCodes; 21 | import com.xuexiang.xipc.exception.IPCException; 22 | 23 | /** 24 | * 序列化工具 25 | * 26 | * @author xuexiang 27 | * @since 2018/9/14 下午6:02 28 | */ 29 | public final class SerializeUtils { 30 | 31 | private static final Gson GSON = new Gson(); 32 | 33 | private SerializeUtils() { 34 | throw new UnsupportedOperationException("u can't instantiate me..."); 35 | } 36 | 37 | /** 38 | * 序列化编码 39 | * 40 | * @param object 41 | * @return 42 | * @throws IPCException 43 | */ 44 | public static String encode(Object object) throws IPCException { 45 | if (object == null) { 46 | return null; 47 | } else { 48 | try { 49 | return GSON.toJson(object); 50 | } catch (RuntimeException e) { 51 | e.printStackTrace(); 52 | } catch (Exception e) { 53 | e.printStackTrace(); 54 | } 55 | throw new IPCException(ErrorCodes.GSON_ENCODE_EXCEPTION, 56 | "Error occurs when Gson encodes Object " 57 | + object + " to Json."); 58 | } 59 | } 60 | 61 | /** 62 | * 反序列化解码 63 | * 64 | * @param data 65 | * @param clazz 66 | * @param 67 | * @return 68 | * @throws IPCException 69 | */ 70 | public static T decode(String data, Class clazz) throws IPCException { 71 | try { 72 | return GSON.fromJson(data, clazz); 73 | } catch (RuntimeException e) { 74 | e.printStackTrace(); 75 | } catch (Exception e) { 76 | e.printStackTrace(); 77 | } 78 | throw new IPCException(ErrorCodes.GSON_DECODE_EXCEPTION, 79 | "Error occurs when Gson decodes data of the Class " 80 | + clazz.getName()); 81 | 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/TimeStampGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import java.util.concurrent.atomic.AtomicLong; 20 | 21 | /** 22 | * 时间戳自增器 23 | * 24 | * @author xuexiang 25 | * @since 2018/9/14 下午4:03 26 | */ 27 | public final class TimeStampGenerator { 28 | 29 | private TimeStampGenerator() { 30 | throw new UnsupportedOperationException("u can't instantiate me..."); 31 | } 32 | 33 | private static AtomicLong sTimeStamp = new AtomicLong(); 34 | 35 | public static long getTimeStamp() { 36 | return sTimeStamp.incrementAndGet(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/Triple.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | /** 20 | * 三泛型对象 21 | */ 22 | public class Triple { 23 | public final T1 first; 24 | public final T2 second; 25 | public final T3 third; 26 | 27 | private Triple(T1 first, T2 second, T3 third) { 28 | this.first = first; 29 | this.second = second; 30 | this.third = third; 31 | } 32 | 33 | public static Triple create(T1 first, T2 second, T3 third) { 34 | return new Triple<>(first, second, third); 35 | } 36 | } 37 | --------------------------------------------------------------------------------
35 | * This works in the main process. 36 | */ 37 | public class IPCCallbackGc { 38 | 39 | private static volatile IPCCallbackGc sInstance = null; 40 | /** 41 | * 引用队列 42 | */ 43 | private final ReferenceQueue mReferenceQueue; 44 | 45 | /** 46 | * 引用信息存储 47 | */ 48 | private final ConcurrentHashMap, Triple> mTimeStamps; 49 | 50 | private IPCCallbackGc() { 51 | mReferenceQueue = new ReferenceQueue<>(); 52 | mTimeStamps = new ConcurrentHashMap<>(); 53 | } 54 | 55 | public static IPCCallbackGc getInstance() { 56 | if (sInstance == null) { 57 | synchronized (IPCCallbackGc.class) { 58 | if (sInstance == null) { 59 | sInstance = new IPCCallbackGc(); 60 | } 61 | } 62 | } 63 | return sInstance; 64 | } 65 | 66 | /** 67 | * 资源回收 68 | */ 69 | private void gc() { 70 | synchronized (mReferenceQueue) { 71 | PhantomReference reference; 72 | Triple triple; 73 | HashMap, ArrayList>> timeStamps 74 | = new HashMap<>(); 75 | while ((reference = (PhantomReference) mReferenceQueue.poll()) != null) { 76 | triple = mTimeStamps.remove(reference); 77 | if (triple != null) { 78 | Pair, ArrayList> tmp = timeStamps.get(triple.first); 79 | if (tmp == null) { 80 | tmp = new Pair<>(new ArrayList(), new ArrayList()); 81 | timeStamps.put(triple.first, tmp); 82 | } 83 | tmp.first.add(triple.second); 84 | tmp.second.add(triple.third); 85 | } 86 | } 87 | Set, ArrayList>>> set = timeStamps.entrySet(); 88 | for (Map.Entry, ArrayList>> entry : set) { 89 | Pair, ArrayList> values = entry.getValue(); 90 | if (!values.first.isEmpty()) { 91 | try { 92 | entry.getKey().gc(values.first, values.second); 93 | } catch (RemoteException e) { 94 | e.printStackTrace(); 95 | } 96 | } 97 | } 98 | } 99 | } 100 | 101 | /** 102 | * 注册IPC接口回调,以方便资源回收 103 | * 104 | * @param callback 回调 105 | * @param object 回调接口的返回值 106 | * @param timeStamp 序号 107 | * @param index 索引 108 | */ 109 | public void register(IIPCServiceCallback callback, Object object, long timeStamp, int index) { 110 | gc(); 111 | //使用虚引用 112 | mTimeStamps.put(new PhantomReference<>(object, mReferenceQueue), Triple.create(callback, timeStamp, index)); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/IPCGc.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import com.xuexiang.xipc.core.channel.Channel; 20 | import com.xuexiang.xipc.core.channel.IPCService; 21 | 22 | import java.lang.ref.PhantomReference; 23 | import java.lang.ref.ReferenceQueue; 24 | import java.util.ArrayList; 25 | import java.util.HashMap; 26 | import java.util.Map; 27 | import java.util.Set; 28 | import java.util.concurrent.ConcurrentHashMap; 29 | 30 | /** 31 | * IPC资源回收 32 | * 33 | * This works in the remote process. 34 | */ 35 | public class IPCGc { 36 | 37 | private static volatile IPCGc sInstance = null; 38 | 39 | private final ReferenceQueue mReferenceQueue; 40 | 41 | private static final Channel CHANNEL = Channel.getInstance(); 42 | 43 | private final ConcurrentHashMap, Long> mTimeStamps; 44 | 45 | private final ConcurrentHashMap> mServices; 46 | 47 | private IPCGc() { 48 | mReferenceQueue = new ReferenceQueue<>(); 49 | mTimeStamps = new ConcurrentHashMap<>(); 50 | mServices = new ConcurrentHashMap<>(); 51 | } 52 | 53 | public static IPCGc getInstance() { 54 | if (sInstance == null) { 55 | synchronized (IPCGc.class) { 56 | if (sInstance == null) { 57 | sInstance = new IPCGc(); 58 | } 59 | } 60 | } 61 | return sInstance; 62 | } 63 | 64 | private void gc() { 65 | synchronized (mReferenceQueue) { 66 | PhantomReference reference; 67 | Long timeStamp; 68 | HashMap, ArrayList> timeStamps 69 | = new HashMap<>(); 70 | while ((reference = (PhantomReference) mReferenceQueue.poll()) != null) { 71 | //After a long time, the program can reach here. 72 | timeStamp = mTimeStamps.remove(reference); 73 | if (timeStamp != null) { 74 | Class extends IPCService> clazz = mServices.remove(timeStamp); 75 | if (clazz != null) { 76 | ArrayList tmp = timeStamps.get(clazz); 77 | if (tmp == null) { 78 | tmp = new ArrayList<>(); 79 | timeStamps.put(clazz, tmp); 80 | } 81 | tmp.add(timeStamp); 82 | } 83 | } 84 | } 85 | Set, ArrayList>> set = timeStamps.entrySet(); 86 | for (Map.Entry, ArrayList> entry : set) { 87 | ArrayList values = entry.getValue(); 88 | if (!values.isEmpty()) { 89 | CHANNEL.gc(entry.getKey(), values); 90 | } 91 | } 92 | } 93 | } 94 | 95 | /** 96 | * 注册IPC服务接口,以方便资源回收 97 | * @param service ipc服务 98 | * @param object 99 | * @param timeStamp 100 | */ 101 | public void register(Class extends IPCService> service, Object object, Long timeStamp) { 102 | gc(); 103 | mTimeStamps.put(new PhantomReference<>(object, mReferenceQueue), timeStamp); 104 | mServices.put(timeStamp, service); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/IPCPoolExecutor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import java.util.concurrent.Callable; 20 | import java.util.concurrent.ExecutorService; 21 | import java.util.concurrent.Executors; 22 | import java.util.concurrent.Future; 23 | 24 | /** 25 | * 线程池管理 26 | * 27 | * @author xuexiang 28 | * @since 2018/8/15 上午2:42 29 | */ 30 | public class IPCPoolExecutor { 31 | 32 | private static volatile IPCPoolExecutor sInstance; 33 | /** 34 | * 线程池 35 | */ 36 | private ExecutorService mExecutorService; 37 | 38 | private IPCPoolExecutor() { 39 | mExecutorService = Executors.newFixedThreadPool(2); 40 | } 41 | 42 | /** 43 | * 获取线程池管理 44 | * 45 | * @return 46 | */ 47 | public static IPCPoolExecutor get() { 48 | if (sInstance == null) { 49 | synchronized (IPCPoolExecutor.class) { 50 | if (sInstance == null) { 51 | sInstance = new IPCPoolExecutor(); 52 | } 53 | } 54 | } 55 | return sInstance; 56 | } 57 | 58 | /** 59 | * 设置线程池 60 | * 61 | * @param service 线程池 62 | */ 63 | public void setExecutorService(final ExecutorService service) { 64 | if (!mExecutorService.isShutdown()) { 65 | mExecutorService.shutdownNow(); 66 | } 67 | mExecutorService = service; 68 | } 69 | 70 | /** 71 | * 提交一个Runnable任务用于执行 72 | * 73 | * @param task 任务 74 | * @return 表示任务等待完成的Future, 该Future的{@code get}方法在成功完成时将会返回null结果。 75 | */ 76 | public Future> addTask(final Runnable task) { 77 | return mExecutorService.submit(task); 78 | } 79 | 80 | /** 81 | * 提交一个Runnable任务用于执行 82 | * 83 | * @param task 任务 84 | * @param result 返回的结果 85 | * @param 泛型 86 | * @return 表示任务等待完成的Future, 该Future的{@code get}方法在成功完成时将会返回该任务的结果。 87 | */ 88 | public Future addTask(final Runnable task, final T result) { 89 | return mExecutorService.submit(task, result); 90 | } 91 | 92 | /** 93 | * 提交一个Callable任务用于执行 94 | * 如果想立即阻塞任务的等待,则可以使用{@code result = exec.submit(aCallable).get();}形式的构造。 95 | * 96 | * @param task 任务 97 | * @param 泛型 98 | * @return 表示任务等待完成的Future, 该Future的{@code get}方法在成功完成时将会返回该任务的结果。 99 | */ 100 | public Future addTask(final Callable task) { 101 | return mExecutorService.submit(task); 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/ObjectCenter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import com.xuexiang.xipc.logs.IPCLog; 20 | 21 | import java.util.List; 22 | import java.util.concurrent.ConcurrentHashMap; 23 | 24 | /** 25 | * 对象存储仓库,存放对应对象的映射 26 | * 27 | * @author xuexiang 28 | * @since 2018/9/17 下午4:06 29 | */ 30 | public class ObjectCenter { 31 | 32 | private static volatile ObjectCenter sInstance = null; 33 | 34 | private final ConcurrentHashMap mObjects; 35 | 36 | private ObjectCenter() { 37 | mObjects = new ConcurrentHashMap<>(); 38 | } 39 | 40 | public static ObjectCenter getInstance() { 41 | if (sInstance == null) { 42 | synchronized (ObjectCenter.class) { 43 | if (sInstance == null) { 44 | sInstance = new ObjectCenter(); 45 | } 46 | } 47 | } 48 | return sInstance; 49 | } 50 | 51 | /** 52 | * 取出对象 53 | * 54 | * @param timeStamp 55 | * @return 56 | */ 57 | public Object getObject(Long timeStamp) { 58 | return mObjects.get(timeStamp); 59 | } 60 | 61 | /** 62 | * 存放对象 63 | * 64 | * @param timeStamp 65 | * @param object 66 | */ 67 | public void putObject(long timeStamp, Object object) { 68 | mObjects.put(timeStamp, object); 69 | } 70 | 71 | /** 72 | * 删除对象 73 | * 74 | * @param timeStamps 75 | */ 76 | public void deleteObjects(List timeStamps) { 77 | for (Long timeStamp : timeStamps) { 78 | if (mObjects.remove(timeStamp) == null) { 79 | IPCLog.e("[ObjectCenter] An error occurs in the GC."); 80 | } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/SerializeUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import com.google.gson.Gson; 20 | import com.xuexiang.xipc.exception.ErrorCodes; 21 | import com.xuexiang.xipc.exception.IPCException; 22 | 23 | /** 24 | * 序列化工具 25 | * 26 | * @author xuexiang 27 | * @since 2018/9/14 下午6:02 28 | */ 29 | public final class SerializeUtils { 30 | 31 | private static final Gson GSON = new Gson(); 32 | 33 | private SerializeUtils() { 34 | throw new UnsupportedOperationException("u can't instantiate me..."); 35 | } 36 | 37 | /** 38 | * 序列化编码 39 | * 40 | * @param object 41 | * @return 42 | * @throws IPCException 43 | */ 44 | public static String encode(Object object) throws IPCException { 45 | if (object == null) { 46 | return null; 47 | } else { 48 | try { 49 | return GSON.toJson(object); 50 | } catch (RuntimeException e) { 51 | e.printStackTrace(); 52 | } catch (Exception e) { 53 | e.printStackTrace(); 54 | } 55 | throw new IPCException(ErrorCodes.GSON_ENCODE_EXCEPTION, 56 | "Error occurs when Gson encodes Object " 57 | + object + " to Json."); 58 | } 59 | } 60 | 61 | /** 62 | * 反序列化解码 63 | * 64 | * @param data 65 | * @param clazz 66 | * @param 67 | * @return 68 | * @throws IPCException 69 | */ 70 | public static T decode(String data, Class clazz) throws IPCException { 71 | try { 72 | return GSON.fromJson(data, clazz); 73 | } catch (RuntimeException e) { 74 | e.printStackTrace(); 75 | } catch (Exception e) { 76 | e.printStackTrace(); 77 | } 78 | throw new IPCException(ErrorCodes.GSON_DECODE_EXCEPTION, 79 | "Error occurs when Gson decodes data of the Class " 80 | + clazz.getName()); 81 | 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/TimeStampGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | import java.util.concurrent.atomic.AtomicLong; 20 | 21 | /** 22 | * 时间戳自增器 23 | * 24 | * @author xuexiang 25 | * @since 2018/9/14 下午4:03 26 | */ 27 | public final class TimeStampGenerator { 28 | 29 | private TimeStampGenerator() { 30 | throw new UnsupportedOperationException("u can't instantiate me..."); 31 | } 32 | 33 | private static AtomicLong sTimeStamp = new AtomicLong(); 34 | 35 | public static long getTimeStamp() { 36 | return sTimeStamp.incrementAndGet(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /xipc-runtime/src/main/java/com/xuexiang/xipc/util/Triple.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.xuexiang.xipc.util; 18 | 19 | /** 20 | * 三泛型对象 21 | */ 22 | public class Triple { 23 | public final T1 first; 24 | public final T2 second; 25 | public final T3 third; 26 | 27 | private Triple(T1 first, T2 second, T3 third) { 28 | this.first = first; 29 | this.second = second; 30 | this.third = third; 31 | } 32 | 33 | public static Triple create(T1 first, T2 second, T3 third) { 34 | return new Triple<>(first, second, third); 35 | } 36 | } 37 | --------------------------------------------------------------------------------
如果想立即阻塞任务的等待,则可以使用{@code result = exec.submit(aCallable).get();}形式的构造。