├── .gitignore ├── LICENSE ├── README.md ├── android-hide ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── aidl │ └── android │ │ ├── app │ │ ├── IActivityManager.aidl │ │ ├── IApplicationThread.aidl │ │ ├── INotificationManager.aidl │ │ └── IServiceConnection.aidl │ │ └── content │ │ ├── IIntentReceiver.aidl │ │ └── IIntentSender.aidl │ └── java │ └── android │ ├── app │ ├── ActivityManager.java │ └── ActivityManagerNative.java │ ├── content │ └── IContentProvider.java │ ├── os │ ├── FileUtils.java │ └── ServiceManager.java │ └── util │ └── Singleton.java ├── android-hook ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ ├── android │ ├── app │ │ ├── Activity.java │ │ ├── ActivityHook.java │ │ ├── Application.java │ │ ├── ApplicationHook.java │ │ ├── IActivityManager.java │ │ ├── Notification2.java │ │ ├── NotificationHook.java │ │ ├── NotificationIconHook.java │ │ ├── Service.java │ │ └── ServiceHook.java │ ├── content │ │ ├── BroadcastReceiver.java │ │ ├── BroadcastReceiverHook.java │ │ ├── ContentProvider.java │ │ └── ContentProviderHook.java │ ├── os │ │ ├── IBinder.java │ │ ├── ServiceManager.java │ │ └── ServiceManagerHook.java │ └── support │ │ └── v4 │ │ └── app │ │ └── NotificationCompat.java │ ├── androidx │ └── core │ │ └── app │ │ ├── NotificationBuilderWithBuilderAccessor.java │ │ ├── NotificationCompat.java │ │ └── NotificationCompatBuilder.java │ └── java │ └── io │ ├── File.java │ └── FileHook.java ├── build.gradle ├── docs ├── develop.md ├── jessie-ex.svg ├── jessie-logo.png ├── jessie-logo.svg ├── jessie-svg.svg └── jessie-web.png ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── index.html ├── jessie-runtime ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── thens │ │ └── jessie │ │ └── runtime │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ ├── android │ │ ├── app │ │ │ ├── ActivityHook.kt │ │ │ ├── ApplicationHook.kt │ │ │ ├── NotificationHook.kt │ │ │ ├── NotificationIconHook.kt │ │ │ └── ServiceHook.kt │ │ ├── content │ │ │ ├── BroadcastReceiverHook.kt │ │ │ └── ContentProviderHook.kt │ │ └── os │ │ │ └── ServiceManagerHook.kt │ │ ├── cn │ │ └── jessie │ │ │ └── runtime │ │ │ ├── JessieImpl.kt │ │ │ ├── app │ │ │ ├── IntentMatcher.kt │ │ │ ├── MyProgram.kt │ │ │ ├── PackageManagerWrapper.kt │ │ │ ├── ProgramContext.kt │ │ │ ├── ProgramLayoutInflater.kt │ │ │ ├── ProgramPackageManager.kt │ │ │ ├── SystemServicesHook.kt │ │ │ ├── activity │ │ │ │ ├── ActivityStubs.kt │ │ │ │ ├── JessieBaseActivity.kt │ │ │ │ └── JessieStubActivity.kt │ │ │ ├── application │ │ │ │ ├── JessieBaseApplication.kt │ │ │ │ └── MyProgramApp.kt │ │ │ ├── notification │ │ │ │ └── JessieBaseNotification.kt │ │ │ ├── provider │ │ │ │ ├── JessieBaseProvider.kt │ │ │ │ ├── JessieStubProvider.kt │ │ │ │ ├── MyProgramProviders.kt │ │ │ │ └── ProgramResolver.kt │ │ │ ├── receiver │ │ │ │ └── JessieBaseReceiver.kt │ │ │ └── service │ │ │ │ ├── IJessieServiceConnection.kt │ │ │ │ ├── IJessieServiceManager.kt │ │ │ │ ├── JessieBaseService.kt │ │ │ │ ├── JessieStubService.kt │ │ │ │ ├── MyProgramServices.kt │ │ │ │ └── ServiceExecutor.kt │ │ │ ├── etc │ │ │ ├── BinderCursor.kt │ │ │ ├── BinderParcelable.kt │ │ │ ├── Bitmaps.kt │ │ │ ├── BuildCompat.kt │ │ │ ├── Files.kt │ │ │ ├── Init.kt │ │ │ ├── JCLogger.kt │ │ │ ├── NumberPool.kt │ │ │ ├── Reflect.kt │ │ │ ├── Reflections.kt │ │ │ └── Threads.kt │ │ │ ├── main │ │ │ ├── IJessieProgramManager.kt │ │ │ ├── JessieDaemonProviderImpl.kt │ │ │ ├── JessieDaemonService.kt │ │ │ ├── JessieLauncherActivity.kt │ │ │ ├── JessieProgramManagerImpl.kt │ │ │ ├── JessieServices.kt │ │ │ ├── MainAppContext.kt │ │ │ ├── MainInitProviderImpl.kt │ │ │ ├── ProcessDispatcher.kt │ │ │ ├── Processes.kt │ │ │ └── RemoteProgram.kt │ │ │ ├── program │ │ │ ├── AbstractProgram.kt │ │ │ ├── AndroidHook.kt │ │ │ ├── AndroidManifestParser.kt │ │ │ ├── AndroidPackageComponentsParser.kt │ │ │ ├── AndroidPackageInfo.kt │ │ │ ├── AppProgram.kt │ │ │ ├── DexProgram.kt │ │ │ ├── MergedResources.kt │ │ │ ├── PluginManager.kt │ │ │ └── PluginProgram.kt │ │ │ └── test │ │ │ └── JessieTests.kt │ │ └── java │ │ └── io │ │ └── FileHook.kt │ └── test │ └── java │ └── cn │ └── thens │ └── jessie │ └── runtime │ └── ExampleUnitTest.kt ├── jessie ├── build.gradle ├── components.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ └── jessie │ │ │ ├── android-hook.jar │ │ │ └── jessie-runtime.jar │ ├── java │ │ ├── android │ │ │ ├── app │ │ │ │ ├── ActivityWrapper.kt │ │ │ │ └── ServiceWrapper.kt │ │ │ └── content │ │ │ │ └── ContentProviderWrapper.kt │ │ └── cn │ │ │ └── jessie │ │ │ ├── Jessie.kt │ │ │ ├── JessieRuntime.kt │ │ │ ├── etc │ │ │ ├── AndroidVM.kt │ │ │ ├── FilePermissions.kt │ │ │ ├── Files.kt │ │ │ ├── JCLogger.kt │ │ │ └── ReflectionUtils.kt │ │ │ ├── main │ │ │ ├── JessieDaemonProvider.kt │ │ │ ├── JessieDaemonService.kt │ │ │ └── MainInitProvider.kt │ │ │ ├── program │ │ │ ├── AndroidPackageComponents.kt │ │ │ ├── DexInfo.kt │ │ │ ├── DexInstaller.kt │ │ │ └── Program.kt │ │ │ └── stub │ │ │ ├── JessieLauncherActivity.kt │ │ │ ├── JessieStub.kt │ │ │ ├── JessieStubActivities.kt │ │ │ ├── JessieStubComponents.kt │ │ │ ├── JessieStubProviders.kt │ │ │ └── JessieStubServices.kt │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── cn │ └── jessie │ ├── etc │ ├── NumberPoolTest.kt │ ├── ReflectionTarget.java │ └── ReflectionsTest.kt │ └── main │ └── ProgramManagerNativeTest.kt ├── sample-host ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cn │ │ └── jessie │ │ └── sample │ │ └── host │ │ ├── GlobalBroadcast.kt │ │ ├── HostApp.kt │ │ ├── JavaTest.java │ │ └── ProgramListActivity.kt │ └── res │ ├── drawable-nodpi │ ├── ic_launcher.png │ ├── ic_launcher_round.png │ └── ic_startup.png │ ├── layout │ ├── activity_program_list.xml │ └── item_program.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ ├── ic_launcher_foreground.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ ├── ic_launcher_foreground.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ ├── ic_launcher_foreground.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ ├── ic_launcher_foreground.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ ├── ic_launcher_foreground.png │ └── ic_launcher_round.png │ ├── values │ ├── colors.xml │ ├── strings.xml │ ├── styles.xml │ └── themes.xml │ └── xml │ └── network_security_config.xml ├── sample-plugin ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cn │ │ └── jessie │ │ └── sample │ │ └── plugin │ │ ├── MainActivity.kt │ │ ├── PluginApp.kt │ │ ├── PluginLog.kt │ │ ├── activity │ │ ├── ActivityFragment.kt │ │ ├── LaunchModeActivity.kt │ │ ├── LifecycleActivity.kt │ │ ├── SplashActivity.kt │ │ └── StaticFragmentActivity.kt │ │ ├── etc │ │ ├── AndroidXNotification.kt │ │ ├── EtcFragment.kt │ │ ├── FloatingViewManager.kt │ │ └── SupportNotification.kt │ │ ├── provider │ │ ├── MyProvider.kt │ │ └── ProviderFragment.kt │ │ └── service │ │ ├── MyService.kt │ │ └── ServiceFragment.kt │ └── res │ ├── drawable-nodpi │ ├── ic_info.png │ ├── ic_launcher.png │ ├── ic_launcher_round.png │ ├── ic_right.png │ ├── ic_splash_bg.xml │ └── ic_startup.png │ ├── layout │ ├── activity_main.xml │ ├── activity_static_fragment.xml │ ├── fragment_main_activity.xml │ ├── fragment_main_etc.xml │ ├── fragment_main_provider.xml │ └── fragment_main_service.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ ├── ic_launcher_foreground.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ ├── ic_launcher_foreground.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ ├── ic_launcher_foreground.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ ├── ic_launcher_foreground.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ ├── ic_launcher_foreground.png │ └── ic_launcher_round.png │ ├── values │ ├── colors.xml │ ├── strings.xml │ ├── styles.xml │ └── themes.xml │ └── xml │ └── network_security_config.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .gradle/ 3 | .local/ 4 | .settings/ 5 | captures/ 6 | build/ 7 | #jessie/src/main/assets/jessie/ 8 | *.iml 9 | local.properties 10 | .project 11 | .DS_Store 12 | .externalNativeBuild 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Thens Wong 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ![Jessie](./docs/jessie-svg.svg) 3 | 4 | # Jessie 5 | 6 | [![jitpack](https://jitpack.io/v/7hens/jessie.svg)](https://jitpack.io/#7hens/jessie) 7 | [![license](https://img.shields.io/github/license/7hens/jessie.svg)](https://github.com/7hens/jessie/blob/master/LICENSE) 8 | 9 | Jessie 是一个插件化框架,可以让其他 APP 在免安装的情况下直接运行。 10 | 11 | Jessie 可以像一个普通的 library 一样直接添加到宿主程序的依赖中。在宿主启动之后,Jessie 会自动运行。 12 | 其实,Jessie 在这背后在了很多事情,但这些事情对开发者来说是无感知的。 13 | 14 | **Jessie 支持以下特性:** 15 | 16 | - 支持加载第三方 APK 17 | - 支持四大组件和 Fragment(动态和静态) 18 | - 支持 Android Support 和 Jetpack 19 | - 支持自定义 Theme 20 | - 纯 Kotlin 实现 21 | 22 | [开发中...](docs/develop.md) 23 | 24 | ## 开始使用 25 | 26 | ### 设置依赖 27 | 28 | 配置根目录的 build.gradle。 29 | 30 | ```groovy 31 | allprojects { 32 | repositories { 33 | // ... 34 | maven { url 'https://jitpack.io' } 35 | } 36 | } 37 | ``` 38 | 39 | 在宿主程序的 build.gradle 中添加下面的依赖,插件程序无需配置。 40 | 41 | ```groovy 42 | implementation "com.github.7hens:jessie:" 43 | ``` 44 | 45 | ### 初始化 46 | 47 | 在宿主程序启动之后,Jessie 会自动运行并初始化,不需要额外的配置。 48 | 49 | ### 安装插件 50 | 51 | ```kotlin 52 | val program = Jessie.install(apk) 53 | ``` 54 | 55 | ### 卸载插件 56 | 57 | ```kotlin 58 | Jessie.uninstall(programPackageName) 59 | ``` 60 | 61 | ### 获取已安装的所有插件 62 | 63 | ```kotlin 64 | Jessie.programs 65 | ``` 66 | 67 | ### 获取插件信息 68 | 69 | ```kotlin 70 | program.packageName 71 | program.packageInfo 72 | program.packageComponents 73 | program.resources 74 | program.classLoader 75 | program.dexInfo 76 | ``` 77 | 78 | ### 启动插件 79 | 80 | 宿主启动插件的 Launcher Activity 81 | 82 | ```kotlin 83 | program.start() 84 | ``` 85 | 86 | 宿主启动插件的特定 Activity 87 | 88 | ```kotlin 89 | val intent = Intent().setComponent(programActivityComponent) 90 | Jessie.startActivity(context, intent) 91 | Jessie.startActivityForResult(context, intent, requestCode) 92 | ``` 93 | -------------------------------------------------------------------------------- /android-hide/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 28 5 | 6 | defaultConfig { 7 | minSdkVersion 16 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | 12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 | consumerProguardFiles 'proguard-rules.pro' 14 | } 15 | testOptions { 16 | unitTests { 17 | includeAndroidResources = true 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /android-hide/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /android-hide/src/main/aidl/android/app/IActivityManager.aidl: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | import android.app.IApplicationThread; 4 | import android.app.IServiceConnection; 5 | import android.content.ComponentName; 6 | import android.content.Intent; 7 | import android.content.IIntentSender; 8 | import android.os.IBinder; 9 | import android.os.IInterface; 10 | 11 | interface IActivityManager { 12 | ComponentName startService(in IApplicationThread caller, in Intent service, in String resolvedType, in String callingPackage, in int userId); 13 | int stopService(in IApplicationThread caller, in Intent service, in String resolvedType, in int userId); 14 | boolean stopServiceToken(in ComponentName className, in IBinder token, in int startId); 15 | void setServiceForeground(in ComponentName className, in IBinder token, in int id, in Notification notification, in boolean keepNotification); 16 | int bindService(in IApplicationThread caller, in IBinder token, in Intent service, in String resolvedType, in IServiceConnection connection, in int flags, in String callingPackage, in int userId); 17 | boolean unbindService(in IServiceConnection connection); 18 | void publishService(in IBinder token, in Intent intent, in IBinder service); 19 | void unbindFinished(in IBinder token, in Intent service, in boolean doRebind); 20 | IIntentSender getIntentSender(in int type, in String packageName, in IBinder token, in String resultWho, int requestCode, in Intent[] intents, in String[] resolvedTypes, in int flags, in Bundle options, in int userId); 21 | void cancelIntentSender(in IIntentSender sender); 22 | String getPackageForIntentSender(in IIntentSender sender); 23 | int getUidForIntentSender(in IIntentSender sender); 24 | } -------------------------------------------------------------------------------- /android-hide/src/main/aidl/android/app/IApplicationThread.aidl: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | interface IApplicationThread { 4 | } 5 | -------------------------------------------------------------------------------- /android-hide/src/main/aidl/android/app/INotificationManager.aidl: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | import android.app.Notification; 4 | import android.content.ComponentName; 5 | import android.content.Intent; 6 | import android.net.Uri; 7 | import android.os.Bundle; 8 | 9 | interface INotificationManager { 10 | void cancelAllNotifications(String pkg, int userId); 11 | void enqueueNotificationWithTag(String pkg, String opPkg, String tag, int id, in Notification notification, inout int[] idReceived, int userId); 12 | void cancelNotificationWithTag(String pkg, String tag, int id, int userId); 13 | void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled); 14 | boolean areNotificationsEnabledForPackage(String pkg, int uid); 15 | boolean areNotificationsEnabled(String pkg); 16 | } -------------------------------------------------------------------------------- /android-hide/src/main/aidl/android/app/IServiceConnection.aidl: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | import android.content.ComponentName; 4 | import android.os.IBinder; 5 | 6 | oneway interface IServiceConnection { 7 | void connected(in ComponentName name, IBinder service, boolean dead); 8 | } 9 | -------------------------------------------------------------------------------- /android-hide/src/main/aidl/android/content/IIntentReceiver.aidl: -------------------------------------------------------------------------------- 1 | package android.content; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | 6 | oneway interface IIntentReceiver { 7 | void performReceive(in Intent intent, int resultCode, String data, in Bundle extras, boolean ordered, boolean sticky, int sendingUser); 8 | } -------------------------------------------------------------------------------- /android-hide/src/main/aidl/android/content/IIntentSender.aidl: -------------------------------------------------------------------------------- 1 | package android.content; 2 | 3 | import android.content.IIntentReceiver; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | 7 | oneway interface IIntentSender { 8 | void send(int code, in Intent intent, String resolvedType, in IBinder whitelistToken, 9 | IIntentReceiver finishedReceiver, String requiredPermission, in Bundle options); 10 | } -------------------------------------------------------------------------------- /android-hide/src/main/java/android/app/ActivityManager.java: -------------------------------------------------------------------------------- 1 | //package android.app; 2 | // 3 | // 4 | //import android.content.Context; 5 | //import android.graphics.Bitmap; 6 | //import android.os.Handler; 7 | // 8 | //import java.util.List; 9 | // 10 | //public class ActivityManager { 11 | // ActivityManager(Context context, Handler handler) { 12 | // } 13 | // 14 | // public List getRunningAppProcesses() { 15 | // return null; 16 | // } 17 | // 18 | // public static class RunningAppProcessInfo { 19 | // public int pid; 20 | // public String processName; 21 | // } 22 | // 23 | // public static class TaskDescription { 24 | // public TaskDescription(String label, Bitmap bitmap) { 25 | // } 26 | // } 27 | //} 28 | -------------------------------------------------------------------------------- /android-hide/src/main/java/android/app/ActivityManagerNative.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 The Android Open Source Project 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 android.app; 18 | 19 | import android.content.Intent; 20 | import android.os.IBinder; 21 | 22 | /** 23 | * @deprecated will be removed soon. See individual methods for alternatives. 24 | */ 25 | @Deprecated 26 | public abstract class ActivityManagerNative { 27 | /** 28 | * Cast a Binder object into an activity manager interface, generating 29 | * a proxy if needed. 30 | * 31 | * @deprecated use IActivityManager.Stub.asInterface instead. 32 | */ 33 | public static IActivityManager asInterface(IBinder obj) { 34 | return IActivityManager.Stub.asInterface(obj); 35 | } 36 | 37 | /** 38 | * Retrieve the system's default/global activity manager. 39 | * 40 | * @deprecated use ActivityManager.getService instead. 41 | */ 42 | public static IActivityManager getDefault() { 43 | throw new RuntimeException("Stub!"); 44 | } 45 | 46 | /** 47 | * Convenience for checking whether the system is ready. For internal use only. 48 | * 49 | * @deprecated use ActivityManagerInternal.isSystemReady instead. 50 | */ 51 | public static boolean isSystemReady() { 52 | throw new RuntimeException("Stub!"); 53 | } 54 | 55 | /** 56 | * @deprecated use ActivityManager.broadcastStickyIntent instead. 57 | */ 58 | public static void broadcastStickyIntent(Intent intent, String permission, int userId) { 59 | throw new RuntimeException("Stub!"); 60 | } 61 | 62 | /** 63 | * Convenience for sending a sticky broadcast. For internal use only. 64 | * If you don't care about permission, use null. 65 | * 66 | * @deprecated use ActivityManager.broadcastStickyIntent instead. 67 | */ 68 | public static void broadcastStickyIntent(Intent intent, String permission, int appOp, int userId) { 69 | throw new RuntimeException("Stub!"); 70 | } 71 | 72 | /** 73 | * @deprecated use ActivityManager.noteWakeupAlarm instead. 74 | */ 75 | public static void noteWakeupAlarm(PendingIntent ps, int sourceUid, String sourcePkg, String tag) { 76 | throw new RuntimeException("Stub!"); 77 | } 78 | 79 | /** 80 | * @deprecated use ActivityManager.noteAlarmStart instead. 81 | */ 82 | public static void noteAlarmStart(PendingIntent ps, int sourceUid, String tag) { 83 | throw new RuntimeException("Stub!"); 84 | } 85 | 86 | /** 87 | * @deprecated use ActivityManager.noteAlarmFinish instead. 88 | */ 89 | public static void noteAlarmFinish(PendingIntent ps, int sourceUid, String tag) { 90 | throw new RuntimeException("Stub!"); 91 | } 92 | } -------------------------------------------------------------------------------- /android-hide/src/main/java/android/content/IContentProvider.java: -------------------------------------------------------------------------------- 1 | package android.content; 2 | 3 | import android.os.IInterface; 4 | 5 | /** 6 | * @author 7hens 7 | */ 8 | public interface IContentProvider extends IInterface { 9 | } 10 | -------------------------------------------------------------------------------- /android-hide/src/main/java/android/os/FileUtils.java: -------------------------------------------------------------------------------- 1 | package android.os; 2 | 3 | import java.io.File; 4 | import java.io.FileDescriptor; 5 | import java.io.IOException; 6 | 7 | public class FileUtils { 8 | public static int setPermissions(File path, int mode, int uid, int gid) { 9 | return setPermissions(path.getAbsolutePath(), mode, uid, gid); 10 | } 11 | 12 | public static int setPermissions(String path, int mode, int uid, int gid) { 13 | return -1; 14 | } 15 | 16 | public static int setPermissions(FileDescriptor fd, int mode, int uid, int gid) { 17 | return -1; 18 | } 19 | 20 | public static void copyPermissions(File from, File to) throws IOException { 21 | } 22 | 23 | public static int getUid(String path) { 24 | return -1; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /android-hide/src/main/java/android/os/ServiceManager.java: -------------------------------------------------------------------------------- 1 | package android.os; 2 | 3 | import java.util.Map; 4 | 5 | public final class ServiceManager { 6 | public static IBinder getService(String name) { 7 | return null; 8 | } 9 | 10 | public static IBinder getServiceOrThrow(String name) { 11 | return null; 12 | } 13 | 14 | public static void addService(String name, IBinder service) { 15 | } 16 | 17 | public static void addService(String name, IBinder service, boolean allowIsolated) { 18 | } 19 | 20 | public static IBinder checkService(String name) { 21 | return null; 22 | } 23 | 24 | public static String[] listServices() { 25 | return null; 26 | } 27 | 28 | public static void initServiceCache(Map cache) { 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /android-hide/src/main/java/android/util/Singleton.java: -------------------------------------------------------------------------------- 1 | package android.util; 2 | 3 | public abstract class Singleton { 4 | private T mInstance; 5 | 6 | protected abstract T create(); 7 | 8 | public final T get() { 9 | synchronized (this) { 10 | if (mInstance == null) { 11 | mInstance = create(); 12 | } 13 | return mInstance; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /android-hook/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 28 5 | 6 | defaultConfig { 7 | minSdkVersion 16 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | 12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 | consumerProguardFiles 'proguard-rules.pro' 14 | } 15 | testOptions { 16 | unitTests { 17 | includeAndroidResources = true 18 | } 19 | } 20 | } 21 | 22 | 23 | dependencies { 24 | implementation fileTree(dir: 'libs', include: ['*.jar']) 25 | implementation 'androidx.appcompat:appcompat:1.1.0-rc01' 26 | } -------------------------------------------------------------------------------- /android-hook/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/app/Activity.java: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | public class Activity extends ActivityHook { 4 | } 5 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/app/ActivityHook.java: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | @SuppressWarnings("WeakerAccess") 4 | public class ActivityHook { 5 | } 6 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/app/Application.java: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | import android.os.Bundle; 4 | 5 | import java.lang.reflect.InvocationHandler; 6 | import java.lang.reflect.Method; 7 | import java.lang.reflect.Proxy; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | public class Application extends ApplicationHook { 12 | 13 | public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callbacks) { 14 | super.registerActivityLifecycleCallbacks(callbacks); 15 | } 16 | 17 | public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callbacks) { 18 | super.unregisterActivityLifecycleCallbacks(callbacks); 19 | } 20 | 21 | public interface ActivityLifecycleCallbacks extends ApplicationHook.ActivityLifecycleCallbacks { 22 | void onActivityCreated(Activity activity, Bundle savedInstanceState); 23 | 24 | void onActivityStarted(Activity activity); 25 | 26 | void onActivityResumed(Activity activity); 27 | 28 | void onActivityPaused(Activity activity); 29 | 30 | void onActivityStopped(Activity activity); 31 | 32 | void onActivitySaveInstanceState(Activity activity, Bundle outState); 33 | 34 | void onActivityDestroyed(Activity activity); 35 | } 36 | 37 | public interface OnProvideAssistDataListener extends ApplicationHook.OnProvideAssistDataListener { 38 | void onProvideAssistData(Activity activity, Bundle data); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/app/ApplicationHook.java: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | public class ApplicationHook { 4 | 5 | public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callbacks) { 6 | } 7 | 8 | public void unregisterActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callbacks) { 9 | } 10 | 11 | public interface ActivityLifecycleCallbacks { 12 | } 13 | 14 | public interface OnProvideAssistDataListener { 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/app/IActivityManager.java: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | public interface IActivityManager { 4 | } 5 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/app/Notification2.java: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | import android.content.Context; 4 | 5 | public class Notification2 extends NotificationHook { 6 | 7 | public static class Builder extends NotificationHook.Builder { 8 | 9 | public Builder(Context context) { 10 | super(context); 11 | } 12 | 13 | public Builder(Context context, String channelId) { 14 | super(context, channelId); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/app/NotificationHook.java: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | import android.content.Context; 4 | 5 | @SuppressWarnings("WeakerAccess") 6 | public class NotificationHook { 7 | public static class Builder { 8 | public Builder(Context context) { 9 | } 10 | 11 | public Builder(Context context, String channelId) { 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/app/NotificationIconHook.java: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | import android.annotation.TargetApi; 4 | import android.graphics.drawable.Icon; 5 | import android.os.Build; 6 | 7 | public final class NotificationIconHook { 8 | @TargetApi(Build.VERSION_CODES.M) 9 | public static Notification.Builder setSmallIcon(Notification.Builder builder, Icon icon) { 10 | return builder.setSmallIcon(icon); 11 | } 12 | 13 | public static Notification.Builder setSmallIcon(Notification.Builder builder, int icon) { 14 | return builder.setSmallIcon(icon); 15 | } 16 | 17 | @TargetApi(Build.VERSION_CODES.M) 18 | public static void setSmallIcon(Notification notification, Icon icon) { 19 | notification.icon = 0; 20 | } 21 | 22 | public static void setSmallIcon(Notification notification, int icon) { 23 | notification.icon = 0; 24 | notification.icon = icon; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/app/Service.java: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | public abstract class Service extends ServiceHook { 4 | } 5 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/app/ServiceHook.java: -------------------------------------------------------------------------------- 1 | package android.app; 2 | 3 | @SuppressWarnings("WeakerAccess") 4 | public abstract class ServiceHook { 5 | } 6 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/content/BroadcastReceiver.java: -------------------------------------------------------------------------------- 1 | package android.content; 2 | 3 | public abstract class BroadcastReceiver extends BroadcastReceiverHook { 4 | } 5 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/content/BroadcastReceiverHook.java: -------------------------------------------------------------------------------- 1 | package android.content; 2 | 3 | @SuppressWarnings("WeakerAccess") 4 | public abstract class BroadcastReceiverHook { 5 | } 6 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/content/ContentProvider.java: -------------------------------------------------------------------------------- 1 | package android.content; 2 | 3 | public abstract class ContentProvider extends ContentProviderHook { 4 | } 5 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/content/ContentProviderHook.java: -------------------------------------------------------------------------------- 1 | package android.content; 2 | 3 | @SuppressWarnings("WeakerAccess") 4 | public abstract class ContentProviderHook { 5 | } 6 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/os/IBinder.java: -------------------------------------------------------------------------------- 1 | package android.os; 2 | 3 | public interface IBinder { 4 | } 5 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/os/ServiceManager.java: -------------------------------------------------------------------------------- 1 | package android.os; 2 | 3 | import java.util.Map; 4 | 5 | public final class ServiceManager { 6 | 7 | public static IBinder getService(String name) { 8 | return ServiceManagerHook.getService(name); 9 | } 10 | 11 | public static IBinder getServiceOrThrow(String name) { 12 | return ServiceManagerHook.getServiceOrThrow(name); 13 | } 14 | 15 | public static void addService(String name, IBinder service) { 16 | ServiceManagerHook.addService(name, service); 17 | } 18 | 19 | public static void addService(String name, IBinder service, boolean allowIsolated) { 20 | ServiceManagerHook.addService(name, service, allowIsolated); 21 | } 22 | 23 | public static IBinder checkService(String name) { 24 | return ServiceManagerHook.checkService(name); 25 | } 26 | 27 | public static String[] listServices() { 28 | return ServiceManagerHook.listServices(); 29 | } 30 | 31 | public static void initServiceCache(Map cache) { 32 | ServiceManagerHook.initServiceCache(cache); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /android-hook/src/main/java/android/os/ServiceManagerHook.java: -------------------------------------------------------------------------------- 1 | package android.os; 2 | 3 | import java.util.Map; 4 | 5 | @SuppressWarnings("ALL") 6 | public final class ServiceManagerHook { 7 | 8 | public static IBinder getService(String name) { 9 | return null; 10 | } 11 | 12 | public static IBinder getServiceOrThrow(String name) { 13 | return null; 14 | } 15 | 16 | public static void addService(String name, IBinder service) { 17 | } 18 | 19 | public static void addService(String name, IBinder service, boolean allowIsolated) { 20 | } 21 | 22 | public static IBinder checkService(String name) { 23 | return null; 24 | } 25 | 26 | public static String[] listServices() { 27 | return null; 28 | } 29 | 30 | public static void initServiceCache(Map cache) { 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /android-hook/src/main/java/androidx/core/app/NotificationBuilderWithBuilderAccessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 The Android Open Source Project 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 androidx.core.app; 18 | 19 | import android.app.Notification; 20 | 21 | /** 22 | * Interface implemented by notification compat builders that support 23 | * an accessor for {@link Notification.Builder}. {@link Notification.Builder} 24 | * was introduced in HoneyComb. 25 | */ 26 | public interface NotificationBuilderWithBuilderAccessor { 27 | Notification.Builder getBuilder(); 28 | } 29 | -------------------------------------------------------------------------------- /android-hook/src/main/java/java/io/File.java: -------------------------------------------------------------------------------- 1 | package java.io; 2 | 3 | import java.net.URI; 4 | 5 | public class File extends FileHook { 6 | public File(String pathname) { 7 | super(pathname); 8 | } 9 | 10 | public File(String parent, String child) { 11 | super(parent, child); 12 | } 13 | 14 | public File(File parent, String child) { 15 | super(parent, child); 16 | } 17 | 18 | public File(URI uri) { 19 | super(uri); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /android-hook/src/main/java/java/io/FileHook.java: -------------------------------------------------------------------------------- 1 | package java.io; 2 | 3 | import java.net.URI; 4 | 5 | public class FileHook { 6 | public FileHook(String pathname) { 7 | } 8 | 9 | public FileHook(String parent, String child) { 10 | } 11 | 12 | public FileHook(File parent, String child) { 13 | } 14 | 15 | public FileHook(URI uri) { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | kotlin_version = '1.3.72' 4 | } 5 | repositories { 6 | google() 7 | jcenter() 8 | mavenLocal() 9 | maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' } 10 | maven { url 'https://jitpack.io' } 11 | } 12 | dependencies { 13 | classpath 'com.android.tools.build:gradle:3.6.3' 14 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 15 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' 16 | } 17 | } 18 | 19 | allprojects { 20 | repositories { 21 | google() 22 | jcenter() 23 | mavenLocal() 24 | maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' } 25 | maven { url 'https://jitpack.io' } 26 | } 27 | } 28 | 29 | task clean(type: Delete) { 30 | delete rootProject.buildDir 31 | } -------------------------------------------------------------------------------- /docs/develop.md: -------------------------------------------------------------------------------- 1 | # Jessie 开发文档 2 | 3 | ## 名词解释 4 | 5 | - 宿主:也叫主程序,指可以动态加载并运行插件的程序 6 | - 插件:运行在宿主上的程序,可以免安装运行 7 | - 插件化:将项目分为宿主和插件部分,插件按需加载 8 | - 热修复:动态更新类或者函数,一般用于修复 BUG 9 | 10 | ## Jessie 的启动流程 11 | 12 | Jessie 在 AndroidManifest 中预埋了一个 MainInitProvider,用来做一些初始化工作。 13 | MainInitProvider 会跟随着宿主 APP 的 Application 一起启动, 14 | 它会通过启动 JessieDaemonService 来主动唤醒一个名为 `:jcdm` 的守护进程(Daemon 进程)。 15 | 16 | JessieDaemonProvider 会伴随着 Daemon 进程启动, 17 | 它会自动扫描已安装的 APK,并自动将他们加载入内存。 18 | 以后的所有插件 APK 的安装都会在 Daemon 进程中进行。 19 | 20 | ## 插进是如何安装的 21 | 22 | 安装插件的入口是 `Jessie.install(apk)`, 23 | 它会通过 AIDL 获取与 Daemon 进程通信的 `IJessieProgramManager` 实例。 24 | 在 Daemon 进程中,与之通信的是 `JessieProgramManagerImpl`, 25 | 它会调用 `PluginManager.install(apk)` 来安装插件。 26 | `PluginManager` 内部维护了一张表,记录了所有安装过的插件程序(Program)。 27 | 28 | > 整个插件的安装过程都是在 Daemon 进程中。 29 | 30 | ## 插件是如何运行的 31 | 32 | 每个插件都拥有自己单独的进程,进程分配的任务也是在 Daemon 进程进行的。 33 | 负责进程分配任务的类为 `ProcessDispatcher`。 34 | 35 | ### ClassLoader 36 | 37 | ClassLoader 是类加载器,用来加载 Class。 38 | ClassLoader 是插件运行的核心,任何插件化方案和热修复方案都离不开 ClassLoader。 39 | 40 | > 在 Android 中,有 2 种最常见的 ClassLoader: 41 | PathClassLoader 用来加载当前运行的 App 的 Class, 42 | DexClassLoader 用来动态加载 APK 或 DEX 文件中的 Class。 43 | 要是实现插件化,必须要使用 DexClassLoader。 44 | 45 | Jessie 中的每个插件都有自己的 ClassLoader —— PluginDexClassLoader,它们都继承制至 DexClassLoader。 46 | PluginDexClassLoader 破坏了 ClassLoader 原有的[双亲委托机制](https://blog.csdn.net/xiangzhihong8/article/details/65446152), 47 | 也就是说它会优先加载自己的 Class,在没有找到的情况下才会去寻找父 ClassLoader 中的 Class。 48 | 49 | ### Application 50 | 51 | ### Activity 52 | 53 | 我们知道 Android 中的 Activity 等四大组件等都有自己的生命周期,并且都必须要在 Manifest 文件中申明之后才能使用。但是插件是动态加载进来的,除非在宿主的 Manifest 中预埋一些 Activity 的坑位,否则很难让插件的 Activity 正常运行。 54 | 55 | ### Service 56 | 57 | ### Provider 58 | 59 | ## 测试 60 | 61 | ### 安装示例插件 62 | 63 | ```shell 64 | ./gradlew installPlugin 65 | ``` 66 | 67 | ### 推荐测试 APP 68 | 69 | 为了在测试过程中更快的定位到问题,建议使用源码透明的 APP 来测试。 70 | 71 | 下面是一些建议的测试 APP。 72 | 73 | | 名称 | 源码 | APK | 74 | | ---- | ---- | ---- | 75 | | 开源中国 | [Gitee](https://gitee.com/oschina/android-app/tree/v4.1.7/) | [应用宝](https://android.myapp.com/myapp/detail.htm?apkName=net.oschina.app&ADTAG=mobile) | 76 | | Flutter-OSC | [Github](https://github.com/yubo725/flutter-osc) | [Github](https://github.com/yubo725/flutter-osc/blob/master/apk/app-release.apk) | 77 | | Termux | [Github](https://github.com/termux/termux-app) | [F-Droid](https://f-droid.org/packages/com.termux/) | 78 | | AndroidFire | [Github](https://github.com/jaydenxiao2016/AndroidFire) | [fir.im](https://fir.im/androidFire) | 79 | | ARouter | [Github](https://github.com/alibaba/ARouter) | [Github](https://github.com/alibaba/ARouter/blob/develop/demo/arouter-demo.apk) | 80 | | flutter-go | [Github](https://github.com/alibaba/flutter-go) | [Github](https://github.com/alibaba/flutter-go) | 81 | | WanAndroid | [Github](https://github.com/senonwx/WanAndroid) | [Github](https://github.com/senonwx/WanAndroid) | 82 | | GSYGithubApp | [Github](https://github.com/CarGuo/GSYGithubApp) | [蒲公英](https://www.pgyer.com/GSYGithubApp) | 83 | | GSYGithubAppKotlin | [Github](https://github.com/CarGuo/GSYGithubAppKotlin) | [蒲公英](https://www.pgyer.com/XGtw) | 84 | | GSYGithubAppWeex | [Github](https://github.com/CarGuo/GSYGithubAppWeex) | [蒲公英](https://www.pgyer.com/K5kU) | 85 | | GSYGithubAppFlutter | [Github](https://github.com/CarGuo/GSYGithubAppFlutter) | [蒲公英](https://www.pgyer.com/vj2B) | 86 | 87 | ## 参考 88 | 89 | - [Android P - veridex 工具扫描非 SDK 接口](https://blog.csdn.net/yi_master/article/details/80664674) -------------------------------------------------------------------------------- /docs/jessie-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/docs/jessie-logo.png -------------------------------------------------------------------------------- /docs/jessie-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/docs/jessie-web.png -------------------------------------------------------------------------------- /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 | android.enableJetifier=true 10 | android.useAndroidX=true 11 | org.gradle.jvmargs=-Xmx1536m 12 | # When configured, Gradle will run in incubating parallel mode. 13 | # This option should only be used with decoupled projects. More details, visit 14 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 15 | # org.gradle.parallel=true 16 | 17 | VERSION=0.1.121 18 | IS_RELEASE_VERSION=false 19 | GROUP_ID=cn.thens 20 | USER_ORG=7hens 21 | DESCRIPTION=Jessie provides a sandbox environment that allows other apps to run directly without installation. 22 | WEBSITE=https\://github.com/7hens/jessie 23 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri May 01 11:15:00 CST 2020 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-5.6.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 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Jessie - 又一个插件化框架 6 | 7 | 8 | 9 | 10 | 11 | 12 |
加载中...
13 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /jessie-runtime/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | 4 | android { 5 | compileSdkVersion 29 6 | 7 | defaultConfig { 8 | minSdkVersion 15 9 | targetSdkVersion 29 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 14 | consumerProguardFiles 'consumer-rules.pro' 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | } 25 | 26 | dependencies { 27 | implementation fileTree(dir: 'libs', include: ['*.jar']) 28 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 29 | compileOnly project(':android-hide') 30 | implementation project(':jessie') 31 | implementation 'com.github.7hens:okbinder:1.0' 32 | implementation 'cn.thens:okparcelable:0.1.0' 33 | implementation 'com.github.7hens:logdog:0.4' 34 | 35 | testImplementation 'junit:junit:4.12' 36 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 37 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 38 | } 39 | -------------------------------------------------------------------------------- /jessie-runtime/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/jessie-runtime/consumer-rules.pro -------------------------------------------------------------------------------- /jessie-runtime/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /jessie-runtime/src/androidTest/java/cn/thens/jessie/runtime/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package cn.thens.jessie.runtime 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("cn.thens.runtime.test", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /jessie-runtime/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/android/app/ActivityHook.kt: -------------------------------------------------------------------------------- 1 | package android.app 2 | 3 | import cn.jessie.runtime.app.activity.JessieBaseActivity 4 | 5 | /** 6 | * @author 7hens 7 | */ 8 | open class ActivityHook : JessieBaseActivity() -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/android/app/ApplicationHook.kt: -------------------------------------------------------------------------------- 1 | package android.app 2 | 3 | import android.annotation.TargetApi 4 | import android.os.Build 5 | import cn.jessie.runtime.app.application.JessieBaseApplication 6 | 7 | /** 8 | * @author 7hens 9 | */ 10 | @Suppress("RedundantOverride") 11 | open class ApplicationHook : JessieBaseApplication() { 12 | 13 | override fun registerActivityLifecycleCallbacks(callback: Application.ActivityLifecycleCallbacks) { 14 | super.registerActivityLifecycleCallbacks(callback) 15 | } 16 | 17 | override fun unregisterActivityLifecycleCallbacks(callback: Application.ActivityLifecycleCallbacks) { 18 | super.unregisterActivityLifecycleCallbacks(callback) 19 | } 20 | 21 | interface ActivityLifecycleCallbacks : Application.ActivityLifecycleCallbacks 22 | 23 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 24 | interface OnProvideAssistDataListener : Application.OnProvideAssistDataListener 25 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/android/app/NotificationHook.kt: -------------------------------------------------------------------------------- 1 | package android.app 2 | 3 | import android.annotation.TargetApi 4 | import android.content.Context 5 | import android.os.Build 6 | import cn.jessie.runtime.app.notification.JessieBaseNotification 7 | 8 | @Suppress("unused") 9 | open class NotificationHook : JessieBaseNotification() { 10 | 11 | open class Builder : JessieBaseNotification.Builder { 12 | @Suppress("DEPRECATION") 13 | constructor(context: Context) : super(context) 14 | 15 | @TargetApi(Build.VERSION_CODES.O) 16 | constructor(context: Context, channelId: String) : super(context, channelId) 17 | } 18 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/android/app/NotificationIconHook.kt: -------------------------------------------------------------------------------- 1 | package android.app 2 | 3 | import android.annotation.TargetApi 4 | import android.graphics.drawable.Drawable 5 | import android.graphics.drawable.Icon 6 | import android.os.Build 7 | import cn.jessie.runtime.app.MyProgram 8 | import cn.jessie.runtime.app.application.MyProgramApp 9 | import cn.jessie.runtime.etc.Bitmaps 10 | import cn.jessie.runtime.etc.JCLogger 11 | import cn.jessie.runtime.etc.Reflections 12 | import cn.jessie.runtime.main.MainAppContext 13 | 14 | object NotificationIconHook { 15 | private const val defaultIconRes = 0 16 | 17 | @JvmStatic 18 | fun setSmallIcon(builder: Notification.Builder, icon: Int): Notification.Builder { 19 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 20 | return builder.setSmallIcon(bitmapIcon(icon)) 21 | } 22 | return builder.setSmallIcon(defaultIconRes) 23 | } 24 | 25 | @JvmStatic 26 | @TargetApi(Build.VERSION_CODES.M) 27 | fun setSmallIcon(builder: Notification.Builder, icon: Icon?): Notification.Builder { 28 | return builder.setSmallIcon(wrapIcon(icon)) 29 | } 30 | 31 | @JvmStatic 32 | fun setSmallIcon(notification: Notification, icon: Int) { 33 | notification.icon = defaultIconRes 34 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 35 | setSmallIconInternal(notification, bitmapIcon(icon)) 36 | } 37 | } 38 | 39 | @JvmStatic 40 | @TargetApi(Build.VERSION_CODES.M) 41 | fun setSmallIcon(notification: Notification, icon: Icon?) { 42 | setSmallIconInternal(notification, wrapIcon(icon)) 43 | } 44 | 45 | @TargetApi(Build.VERSION_CODES.M) 46 | private fun setSmallIconInternal(notification: Notification, icon: Icon?) { 47 | try { 48 | Reflections.set(notification, "mSmallIcon", icon) 49 | } catch (e: Throwable) { 50 | JCLogger.error(e) 51 | } 52 | } 53 | 54 | @TargetApi(Build.VERSION_CODES.M) 55 | private fun bitmapIcon(resId: Int): Icon? { 56 | return try { 57 | JCLogger.debug("resId=$resId\npackageName=${MyProgram.packageName}") 58 | Icon.createWithBitmap(Bitmaps.from(getDrawable(resId))) 59 | } catch (e: Throwable) { 60 | JCLogger.error(e) 61 | null 62 | } 63 | } 64 | 65 | private fun getDrawable(resId: Int): Drawable { 66 | if (resId == 0) { 67 | return MainAppContext.get().resources.getDrawable(MainAppContext.get().applicationInfo.icon) 68 | } 69 | return MyProgram.resources.getDrawable(resId) 70 | } 71 | 72 | @TargetApi(Build.VERSION_CODES.M) 73 | private fun wrapIcon(icon: Icon?): Icon? { 74 | if (icon == null) return null 75 | val drawable = icon.loadDrawable(MyProgramApp.get()) 76 | return Icon.createWithBitmap(Bitmaps.from(drawable)) 77 | } 78 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/android/app/ServiceHook.kt: -------------------------------------------------------------------------------- 1 | package android.app 2 | 3 | import cn.jessie.runtime.app.service.JessieBaseService 4 | 5 | abstract class ServiceHook : JessieBaseService() -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/android/content/BroadcastReceiverHook.kt: -------------------------------------------------------------------------------- 1 | package android.content 2 | 3 | import cn.jessie.runtime.app.receiver.JessieBaseReceiver 4 | 5 | abstract class BroadcastReceiverHook : JessieBaseReceiver() -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/android/content/ContentProviderHook.kt: -------------------------------------------------------------------------------- 1 | package android.content 2 | 3 | import cn.jessie.runtime.app.provider.JessieBaseProvider 4 | 5 | abstract class ContentProviderHook : JessieBaseProvider() -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/android/os/ServiceManagerHook.kt: -------------------------------------------------------------------------------- 1 | package android.os 2 | 3 | import cn.jessie.etc.JCLogger 4 | 5 | object ServiceManagerHook { 6 | @JvmStatic 7 | fun getService(name: String): IBinder? { 8 | JCLogger.error("ServiceManagerHook.getService($name)") 9 | return ServiceManager.getService(name) 10 | } 11 | 12 | @JvmStatic 13 | fun getServiceOrThrow(name: String): IBinder? { 14 | return ServiceManager.getServiceOrThrow(name) 15 | } 16 | 17 | @JvmStatic 18 | fun addService(name: String, service: IBinder) { 19 | ServiceManager.addService(name, service) 20 | } 21 | 22 | @JvmStatic 23 | fun addService(name: String, service: IBinder, allowIsolated: Boolean) { 24 | ServiceManager.addService(name, service, allowIsolated) 25 | } 26 | 27 | @JvmStatic 28 | fun checkService(name: String): IBinder? { 29 | return ServiceManager.checkService(name) 30 | } 31 | 32 | @JvmStatic 33 | fun listServices(): Array? { 34 | return ServiceManager.listServices() 35 | } 36 | 37 | @JvmStatic 38 | fun initServiceCache(cache: Map) { 39 | ServiceManager.initServiceCache(cache) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/IntentMatcher.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app 2 | 3 | import android.content.Context 4 | import android.content.Intent 5 | import android.content.IntentFilter 6 | 7 | internal object IntentMatcher { 8 | private const val TAG = "IntentMatcher" 9 | 10 | fun match(context: Context, intent: Intent, intentFilter: IntentFilter): Boolean { 11 | val action = intent.action 12 | val type = intent.resolveTypeIfNeeded(context.contentResolver) 13 | val scheme = intent.scheme 14 | val data = intent.data 15 | val categories = intent.categories 16 | val result = intentFilter.match(action, type, scheme, data, categories, TAG) 17 | return result >= 0 18 | } 19 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/MyProgram.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app 2 | 3 | import android.content.pm.PackageInfo 4 | import android.content.res.Resources 5 | import cn.jessie.Jessie 6 | import cn.jessie.program.AndroidPackageComponents 7 | import cn.jessie.program.DexInfo 8 | import cn.jessie.runtime.program.AbstractProgram 9 | 10 | internal object MyProgram : AbstractProgram() { 11 | private val delegate by lazy { 12 | val currentProcessName = Jessie.currentProgramProcessName 13 | require(currentProcessName.isNotEmpty()) 14 | val colonIndex = currentProcessName.indexOf(":") 15 | val currentPackageName = if (colonIndex == -1) { 16 | currentProcessName 17 | } else { 18 | currentProcessName.substring(0, colonIndex) 19 | } 20 | // Logdog.warn(""" 21 | // currentPackageName = $currentPackageName 22 | // currentProcessName = $currentProcessName 23 | // """.trimIndent()) 24 | Jessie.getProgram(currentPackageName)!! 25 | } 26 | 27 | override val packageName: String get() = delegate.packageName 28 | override val classLoader: ClassLoader get() = delegate.classLoader 29 | override val resources: Resources get() = delegate.resources 30 | override val packageInfo: PackageInfo get() = delegate.packageInfo 31 | override val dexInfo: DexInfo get() = delegate.dexInfo 32 | override val packageComponents: AndroidPackageComponents get() = delegate.packageComponents 33 | 34 | override fun preload() = delegate.preload() 35 | override fun start() = delegate.start() 36 | override fun stop() = delegate.stop() 37 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/ProgramLayoutInflater.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app 2 | 3 | import android.content.Context 4 | import android.os.Build 5 | import android.util.AttributeSet 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewStub 9 | import cn.jessie.runtime.app.activity.JessieStubActivity 10 | import java.lang.reflect.Constructor 11 | 12 | internal class ProgramLayoutInflater(original: LayoutInflater, newContext: Context) : LayoutInflater(original, newContext) { 13 | 14 | override fun cloneInContext(newContext: Context): LayoutInflater { 15 | val programContext = if (newContext is JessieStubActivity) newContext.programActivity else newContext 16 | val layoutInflater = ProgramLayoutInflater(this, programContext) 17 | layoutInflater.factory2 = Factory2(layoutInflater) 18 | return ProgramLayoutInflater(layoutInflater, programContext) 19 | } 20 | 21 | class Factory2(private val layoutInflater: LayoutInflater) : LayoutInflater.Factory2 { 22 | private val classPrefixes = arrayOf("android.widget.", "android.webkit.", "android.app.") 23 | private val constructors = hashMapOf>() 24 | 25 | override fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet?): View? { 26 | if (!name.contains(".")) { 27 | for (classPrefix in classPrefixes) { 28 | val view = onCreateView(parent, classPrefix + name, context, attrs) 29 | if (view != null) return view 30 | } 31 | return null 32 | } 33 | return createCustomView(name, context, attrs) 34 | } 35 | 36 | override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? { 37 | return onCreateView(null, name, context, attrs) 38 | } 39 | 40 | private fun createCustomView(name: String, context: Context, attrs: AttributeSet?): View? { 41 | val cacheKey = "${layoutInflater.context.packageName}-$name" 42 | var constructor = constructors[cacheKey] 43 | if (constructor != null && !verifyClassLoader(context, constructor)) { 44 | constructor = null 45 | constructors.remove(cacheKey) 46 | } 47 | try { 48 | if (constructor == null) { 49 | val clazz = context.classLoader.loadClass(name).asSubclass(View::class.java) 50 | constructor = clazz.getConstructor(Context::class.java, AttributeSet::class.java) 51 | constructor.isAccessible = true 52 | constructors[cacheKey] = constructor 53 | } 54 | 55 | val view = constructor.newInstance(context, attrs) 56 | if (view is ViewStub && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 57 | view.layoutInflater = layoutInflater.cloneInContext(context) 58 | } 59 | return view 60 | } catch (e: Exception) { 61 | return null 62 | } 63 | } 64 | 65 | private fun verifyClassLoader(context: Context, constructor: Constructor): Boolean { 66 | val constructorClassLoader = constructor.declaringClass.classLoader 67 | if (constructorClassLoader === LayoutInflater::class.java.classLoader) { 68 | return true 69 | } 70 | var classLoader: ClassLoader? = context.classLoader 71 | while (classLoader != null) { 72 | if (constructorClassLoader === classLoader) return true 73 | classLoader = classLoader.parent 74 | } 75 | return false 76 | } 77 | } 78 | 79 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/ProgramPackageManager.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app 2 | 3 | import android.annotation.TargetApi 4 | import android.content.ComponentName 5 | import android.content.Intent 6 | import android.content.pm.* 7 | import android.content.res.XmlResourceParser 8 | import android.os.Build 9 | import cn.jessie.runtime.main.JessieServices 10 | import cn.jessie.runtime.main.MainAppContext 11 | import cn.jessie.program.Program 12 | 13 | internal class ProgramPackageManager(base: PackageManager) : PackageManagerWrapper(base) { 14 | private val program: Program = MyProgram 15 | private val programManager get() = JessieServices.programManager 16 | 17 | override fun getXml(packageName: String, resid: Int, appInfo: ApplicationInfo): XmlResourceParser { 18 | return try { 19 | program.resources.getXml(resid) 20 | } catch (e: Throwable) { 21 | super.getXml(packageName, resid, appInfo) 22 | } 23 | } 24 | 25 | override fun getPackageInfo(packageName: String?, flags: Int): PackageInfo { 26 | if (packageName == program.packageName) return program.packageInfo 27 | return super.getPackageInfo(packageName, flags) 28 | } 29 | 30 | override fun getApplicationLabel(info: ApplicationInfo?): CharSequence { 31 | return try { 32 | program.resources.getString(program.packageInfo.applicationInfo.labelRes) 33 | } catch (e: Throwable) { 34 | super.getApplicationLabel(info) 35 | } 36 | } 37 | 38 | override fun getApplicationInfo(packageName: String?, flags: Int): ApplicationInfo { 39 | if (packageName == program.packageName) return program.packageInfo.applicationInfo 40 | return super.getApplicationInfo(packageName, flags) 41 | } 42 | 43 | override fun getActivityInfo(component: ComponentName?, flags: Int): ActivityInfo { 44 | if (component != null && component.packageName == program.packageName) { 45 | val activity = program.packageComponents.activities[component.className] 46 | if (activity != null) return activity.component 47 | } 48 | return super.getActivityInfo(component, flags) 49 | } 50 | 51 | override fun queryIntentActivities(intent: Intent?, flags: Int): MutableList { 52 | return programManager.queryIntentActivities(intent!!, flags) 53 | } 54 | 55 | override fun queryIntentServices(intent: Intent?, flags: Int): MutableList { 56 | return programManager.queryIntentServices(intent!!, flags) 57 | } 58 | 59 | @TargetApi(Build.VERSION_CODES.KITKAT) 60 | override fun queryIntentContentProviders(intent: Intent?, flags: Int): MutableList { 61 | return programManager.queryIntentContentProviders(intent!!, flags) 62 | } 63 | 64 | override fun resolveActivity(intent: Intent?, flags: Int): ResolveInfo? { 65 | return programManager.resolveActivity(intent!!, flags) 66 | } 67 | 68 | override fun resolveService(intent: Intent?, flags: Int): ResolveInfo? { 69 | return programManager.resolveService(intent!!, flags) 70 | } 71 | 72 | override fun resolveContentProvider(name: String, flags: Int): ProviderInfo? { 73 | return programManager.resolveContentProvider(name, flags) 74 | } 75 | 76 | override fun getComponentEnabledSetting(componentName: ComponentName?): Int { 77 | return programManager.getComponentEnabledSetting(componentName!!) 78 | } 79 | 80 | override fun setComponentEnabledSetting(componentName: ComponentName, newState: Int, flags: Int) { 81 | programManager.setComponentEnabledSetting(componentName, newState, flags) 82 | } 83 | 84 | companion object { 85 | private val instance by lazy { ProgramPackageManager(MainAppContext.get().packageManager) } 86 | 87 | fun get() = instance 88 | } 89 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/application/JessieBaseApplication.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app.application 2 | 3 | import android.app.Activity 4 | import android.app.Application 5 | import android.content.ComponentCallbacks 6 | import cn.jessie.runtime.app.activity.JessieStubActivity 7 | import cn.jessie.runtime.etc.Reflections 8 | import java.util.concurrent.ConcurrentHashMap 9 | 10 | /** 11 | * @author 7hens 12 | */ 13 | @Suppress("RedundantOverride", "unused") 14 | abstract class JessieBaseApplication : Application() { 15 | 16 | private val activityLifecycleCallbacksMap = ConcurrentHashMap() 17 | 18 | override fun registerActivityLifecycleCallbacks(callback: ActivityLifecycleCallbacks) { 19 | val callbackWrapper = wrapActivityLifecycleCallbacks(callback) 20 | activityLifecycleCallbacksMap[callback] = callbackWrapper 21 | super.registerActivityLifecycleCallbacks(callbackWrapper) 22 | } 23 | 24 | override fun unregisterActivityLifecycleCallbacks(callback: ActivityLifecycleCallbacks) { 25 | activityLifecycleCallbacksMap[callback]?.let { 26 | super.unregisterActivityLifecycleCallbacks(it) 27 | } 28 | activityLifecycleCallbacksMap.remove(callback) 29 | } 30 | 31 | private fun wrapActivityLifecycleCallbacks(callback: ActivityLifecycleCallbacks): ActivityLifecycleCallbacks { 32 | return Reflections.proxy { _, method, args -> 33 | requireNotNull(args) 34 | val activity = args[0] as Activity 35 | val callbackClassLoader = callback.javaClass.classLoader 36 | val programActivity: Activity? = when { 37 | activity.javaClass.classLoader == callbackClassLoader -> activity 38 | activity is JessieStubActivity -> { 39 | activity.programActivity.takeIf { it.javaClass.classLoader == callbackClassLoader } 40 | } 41 | else -> null 42 | } 43 | val newArgs = Array(args.size) { index -> 44 | if (index == 0 && programActivity != null) programActivity else args[index] 45 | } 46 | method.invoke(callback, *newArgs) 47 | } 48 | } 49 | 50 | override fun registerComponentCallbacks(callback: ComponentCallbacks?) { 51 | // TODO 52 | super.registerComponentCallbacks(callback) 53 | } 54 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/application/MyProgramApp.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app.application 2 | 3 | import android.annotation.SuppressLint 4 | import android.app.Application 5 | import android.content.Context 6 | import cn.jessie.Jessie 7 | import cn.jessie.runtime.app.MyProgram 8 | import cn.jessie.runtime.app.ProgramContext 9 | import cn.jessie.runtime.app.provider.MyProgramProviders 10 | import cn.jessie.runtime.etc.Init 11 | import cn.jessie.runtime.etc.JCLogger 12 | import cn.jessie.runtime.etc.Reflections 13 | import cn.jessie.runtime.main.Processes 14 | import java.lang.reflect.Modifier 15 | 16 | internal object MyProgramApp { 17 | private val init = Init.create() 18 | private lateinit var app: Application 19 | 20 | fun get() = app 21 | 22 | @SuppressLint("WrongConstant") 23 | fun initialize(mainApp: Context) { 24 | if (init.getElseInitialize()) return 25 | try { 26 | val classLoader = MyProgram.classLoader 27 | val packageInfo = MyProgram.packageInfo 28 | val applicationInfo = packageInfo.applicationInfo 29 | val currentProcessName = Jessie.currentProgramProcessName 30 | val applicationName = applicationInfo.className ?: Application::class.java.name 31 | val cApplication = classLoader.loadClass(applicationName) 32 | val cContext = classLoader.loadClass(Context::class.java.name) 33 | 34 | // 1) 新建 Application 的实例 35 | app = cApplication.newInstance() as Application 36 | for (field in Application::class.java.declaredFields) { 37 | if (Modifier.isStatic(field.modifiers)) continue 38 | field.isAccessible = true 39 | field.set(app, field.get(mainApp)) 40 | } 41 | 42 | // 2) 调用 Application.attachBaseContext 43 | val mAttachBaseContext = Reflections.method(cApplication, "attachBaseContext", cContext) 44 | mAttachBaseContext.isAccessible = true 45 | mAttachBaseContext.invoke(app, ProgramContext(mainApp)) 46 | 47 | // 3) 启动同进程的 ContentProvider 48 | MyProgram.packageInfo.providers.asSequence() 49 | .filter { currentProcessName == Processes.processName(it) } 50 | .forEach { providerInfo -> 51 | try { 52 | MyProgramProviders.create(providerInfo) 53 | } catch (e: Throwable) { 54 | JCLogger.error(e) 55 | } 56 | } 57 | 58 | // 4) 执行 Application.onCreate 59 | app.onCreate() 60 | } catch (e: Throwable) { 61 | JCLogger.error(e) 62 | } 63 | } 64 | 65 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/notification/JessieBaseNotification.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app.notification 2 | 3 | import android.annotation.TargetApi 4 | import android.app.Notification 5 | import android.app.NotificationIconHook 6 | import android.content.Context 7 | import android.graphics.drawable.Icon 8 | import android.os.Build 9 | 10 | @Suppress("unused") 11 | open class JessieBaseNotification : Notification() { 12 | 13 | open class Builder : Notification.Builder { 14 | @Suppress("DEPRECATION") 15 | constructor(context: Context) : super(context) 16 | 17 | @TargetApi(Build.VERSION_CODES.O) 18 | constructor(context: Context, channelId: String) : super(context, channelId) 19 | 20 | override fun setSmallIcon(icon: Icon?): Notification.Builder { 21 | return NotificationIconHook.setSmallIcon(this, icon) 22 | } 23 | 24 | override fun setSmallIcon(icon: Int): Notification.Builder { 25 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && icon != 0) { 26 | return NotificationIconHook.setSmallIcon(this, icon) 27 | } 28 | return super.setSmallIcon(0) 29 | } 30 | 31 | override fun setSmallIcon(icon: Int, level: Int): Notification.Builder { 32 | super.setSmallIcon(0, level) 33 | return setSmallIcon(icon) 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/provider/JessieBaseProvider.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app.provider 2 | 3 | import android.content.ContentProvider 4 | 5 | abstract class JessieBaseProvider : ContentProvider() -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/provider/MyProgramProviders.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app.provider 2 | 3 | import android.content.ContentProvider 4 | import android.content.pm.ProviderInfo 5 | import android.net.Uri 6 | import cn.jessie.runtime.app.MyProgram 7 | import cn.jessie.runtime.app.application.MyProgramApp 8 | import cn.jessie.runtime.etc.JCLogger 9 | import cn.jessie.runtime.main.Processes 10 | 11 | internal object MyProgramProviders { 12 | private val cache = mutableMapOf() 13 | 14 | fun get(authority: String): ContentProvider? { 15 | return cache[authority] 16 | } 17 | 18 | fun require(uri: Uri): ContentProvider { 19 | val authority = uri.authority ?: "" 20 | if (cache.containsKey(authority)) { 21 | return get(authority)!! 22 | } 23 | val providerInfo = MyProgram.packageComponents.providers.getValue(authority).component 24 | return create(providerInfo) 25 | } 26 | 27 | fun create(providerInfo: ProviderInfo): ContentProvider { 28 | JCLogger.debug(""" 29 | provider = ${providerInfo.name} 30 | authority = ${providerInfo.authority} 31 | processName = ${providerInfo.processName} 32 | """.trimIndent()) 33 | Processes.checkProgram(providerInfo) 34 | val cContentProvider = MyProgram.classLoader.loadClass(providerInfo.name) 35 | val provider = cContentProvider.newInstance() as ContentProvider 36 | provider.attachInfo(MyProgramApp.get(), providerInfo) 37 | cache[providerInfo.authority] = provider 38 | return provider 39 | } 40 | 41 | fun contains(authority: String): Boolean { 42 | return cache.containsKey(authority) 43 | } 44 | 45 | fun each(fn: (ContentProvider) -> Unit) { 46 | cache.values.forEach { fn(it) } 47 | } 48 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/receiver/JessieBaseReceiver.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app.receiver 2 | 3 | import android.content.BroadcastReceiver 4 | 5 | abstract class JessieBaseReceiver : BroadcastReceiver() { 6 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/service/IJessieServiceConnection.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app.service 2 | 3 | import android.content.ComponentName 4 | import android.os.IBinder 5 | import cn.thens.okbinder.OkBinder 6 | 7 | @OkBinder.Interface 8 | interface IJessieServiceConnection { 9 | fun onConnect(component: ComponentName, binder: IBinder?) 10 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/service/IJessieServiceManager.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app.service 2 | 3 | import android.content.ComponentName 4 | import android.content.Intent 5 | import cn.thens.okbinder.OkBinder 6 | 7 | @OkBinder.Interface 8 | interface IJessieServiceManager { 9 | fun startService(component: ComponentName, intent: Intent): ComponentName 10 | fun stopService(component: ComponentName, intent: Intent): Boolean 11 | fun bindService(component: ComponentName, intent: Intent, conn: IJessieServiceConnection, flags: Int): Boolean 12 | fun unbindService(component: ComponentName, conn: IJessieServiceConnection): Boolean 13 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/service/JessieBaseService.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app.service 2 | 3 | import android.app.Service 4 | 5 | abstract class JessieBaseService : Service() -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/app/service/MyProgramServices.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.app.service 2 | 3 | import android.app.Service 4 | import android.content.ComponentName 5 | import cn.jessie.runtime.app.MyProgram 6 | import cn.jessie.runtime.app.ProgramContext 7 | import cn.jessie.runtime.etc.Reflections 8 | import cn.jessie.runtime.main.MainAppContext 9 | import java.lang.reflect.Modifier 10 | 11 | internal object MyProgramServices { 12 | private val cache = mutableMapOf() 13 | 14 | fun contains(component: ComponentName): Boolean { 15 | return cache.containsKey(component) 16 | } 17 | 18 | fun get(component: ComponentName): Service? { 19 | return cache[component] 20 | } 21 | 22 | /** 23 | * get or create Service 24 | */ 25 | fun require(component: ComponentName): Service? { 26 | return get(component) ?: run { 27 | val service = MyProgram.classLoader.loadClass(component.className).newInstance() as Service 28 | cache[component] = service 29 | val programContext = ProgramContext(MainAppContext.get()) 30 | Reflections.invoke(service, "attachBaseContext", programContext) 31 | service.onCreate() 32 | service 33 | } 34 | } 35 | 36 | fun require(stubService: Service, component: ComponentName): Service? { 37 | return get(component) ?: run { 38 | val service = MyProgram.classLoader.loadClass(component.className).newInstance() as Service 39 | cache[component] = service 40 | var debugText = "" 41 | for (field in Service::class.java.declaredFields) { 42 | if (Modifier.isStatic(field.modifiers)) continue 43 | field.isAccessible = true 44 | debugText += "\n${field.name}" 45 | field.set(service, field.get(stubService)) 46 | } 47 | // Logdog.debug(debugText) 48 | Reflections.invoke(service, "attachBaseContext", ProgramContext(stubService)) 49 | service.onCreate() 50 | service 51 | } 52 | } 53 | 54 | fun remove(component: ComponentName) { 55 | cache.remove(component) 56 | } 57 | 58 | fun each(fn: (Service) -> Unit) { 59 | cache.values.forEach { fn(it) } 60 | } 61 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/etc/BinderCursor.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.etc 2 | 3 | import android.database.Cursor 4 | import android.database.MatrixCursor 5 | import android.os.Bundle 6 | import android.os.IBinder 7 | 8 | /** 9 | * @author 7hens 10 | */ 11 | internal class BinderCursor(columnNames: Array, binder: IBinder) : MatrixCursor(columnNames) { 12 | private var bundle = Bundle() 13 | 14 | constructor(binder: IBinder) : this(emptyArray(), binder) 15 | 16 | init { 17 | val value = BinderParcelable(binder) 18 | bundle.putParcelable(KEY_BINDER, value) 19 | } 20 | 21 | override fun getExtras(): Bundle { 22 | return bundle 23 | } 24 | 25 | companion object { 26 | private const val KEY_BINDER = "BINDER" 27 | 28 | fun getBinder(cursor: Cursor): IBinder? { 29 | return try { 30 | val extras = cursor.extras 31 | extras.classLoader = BinderCursor::class.java.classLoader 32 | extras.getParcelable(KEY_BINDER)?.binder 33 | } catch (e: Throwable) { 34 | JCLogger.error(e) 35 | null 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/etc/BinderParcelable.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.etc 2 | 3 | import android.os.IBinder 4 | import android.os.Parcel 5 | import android.os.Parcelable 6 | 7 | internal class BinderParcelable(val binder: IBinder) : Parcelable { 8 | constructor(source: Parcel) : this(source.readStrongBinder()) 9 | 10 | override fun describeContents(): Int { 11 | return 0 12 | } 13 | 14 | override fun writeToParcel(dest: Parcel, flags: Int) { 15 | dest.writeStrongBinder(binder) 16 | } 17 | 18 | companion object CREATOR : Parcelable.Creator { 19 | override fun createFromParcel(source: Parcel): BinderParcelable { 20 | return BinderParcelable(source) 21 | } 22 | 23 | override fun newArray(size: Int): Array { 24 | return arrayOfNulls(size) 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/etc/Bitmaps.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.etc 2 | 3 | import android.graphics.Bitmap 4 | import android.graphics.Canvas 5 | import android.graphics.drawable.BitmapDrawable 6 | import android.graphics.drawable.Drawable 7 | 8 | internal object Bitmaps { 9 | fun from(drawable: Drawable): Bitmap { 10 | if (drawable is BitmapDrawable) { 11 | return drawable.bitmap 12 | } 13 | val w = drawable.intrinsicWidth 14 | val h = drawable.intrinsicHeight 15 | val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888) 16 | val canvas = Canvas(bitmap) 17 | drawable.setBounds(0, 0, w, h) 18 | drawable.draw(canvas) 19 | return bitmap 20 | } 21 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/etc/BuildCompat.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.etc 2 | 3 | import android.os.Build 4 | 5 | @Suppress("unused", "MemberVisibilityCanBePrivate", "SpellCheckingInspection", "DEPRECATION") 6 | internal object BuildCompat { 7 | 8 | const val ARM = "arm" 9 | const val ARM64 = "arm64" 10 | 11 | val ALL_SUPPORTED_ABIS = arrayOf(Build.CPU_ABI, Build.CPU_ABI2) 12 | 13 | val SUPPORTED_ABIS: Array by lazy { 14 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 15 | if (Build.SUPPORTED_ABIS != null) { 16 | Build.SUPPORTED_ABIS 17 | } else { 18 | ALL_SUPPORTED_ABIS 19 | } 20 | } else { 21 | ALL_SUPPORTED_ABIS 22 | } 23 | } 24 | 25 | val SUPPORTED_32_BIT_ABIS: Array by lazy { 26 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 27 | if (Build.SUPPORTED_32_BIT_ABIS != null) { 28 | Build.SUPPORTED_32_BIT_ABIS 29 | } else { 30 | ALL_SUPPORTED_ABIS 31 | } 32 | } else { 33 | ALL_SUPPORTED_ABIS 34 | } 35 | } 36 | 37 | val SUPPORTED_64_BIT_ABIS: Array by lazy { 38 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 39 | if (Build.SUPPORTED_64_BIT_ABIS != null) { 40 | Build.SUPPORTED_64_BIT_ABIS 41 | } else { 42 | ALL_SUPPORTED_ABIS 43 | } 44 | } else { 45 | ALL_SUPPORTED_ABIS 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/etc/Files.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.etc 2 | 3 | import java.io.File 4 | import java.io.FileInputStream 5 | 6 | internal object Files { 7 | fun dir(file: File): File { 8 | if (!file.exists()) { 9 | file.mkdirs() 10 | } 11 | return file 12 | } 13 | 14 | fun delete(file: File): Boolean { 15 | return file.deleteRecursively() 16 | } 17 | 18 | fun read(file: String): String { 19 | return try { 20 | FileInputStream(file).bufferedReader().use { it.readText() } 21 | } catch (e: Throwable) { 22 | JCLogger.error(e) 23 | "" 24 | } 25 | } 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/etc/Init.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.etc 2 | 3 | import java.util.concurrent.atomic.AtomicBoolean 4 | 5 | /** 6 | * @author 7hens 7 | */ 8 | class Init private constructor() { 9 | private val isInitialized = AtomicBoolean(false) 10 | 11 | fun getElseInitialize(): Boolean { 12 | return !isInitialized.compareAndSet(false, true) 13 | } 14 | 15 | fun isInitialized(): Boolean { 16 | return isInitialized.get() 17 | } 18 | 19 | companion object { 20 | fun create(): Init { 21 | return Init() 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/etc/JCLogger.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.etc 2 | 3 | import cn.thens.logdog.Logdog 4 | 5 | val JCLogger: Logdog = Logdog.get().tag("JC") 6 | -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/etc/NumberPool.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.etc 2 | 3 | import java.util.* 4 | 5 | internal class NumberPool(count: Int) { 6 | private var runningQueue = LinkedList() 7 | private var record = (0.inv() ushr 1) % (1 shl count) 8 | 9 | fun get(): Int { 10 | return if (record > 0) { 11 | val rest = record.and(record - 1) 12 | val result = Integer.numberOfTrailingZeros(record - rest) 13 | runningQueue.add(result) 14 | record = rest 15 | result 16 | } else { 17 | return -1 18 | } 19 | } 20 | 21 | fun force(): Int { 22 | val result = runningQueue.pop() 23 | runningQueue.add(result) 24 | return result 25 | } 26 | 27 | fun recycle(number: Int) { 28 | runningQueue.remove(number) 29 | record = record or (1 shl number) 30 | } 31 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/etc/Threads.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.etc 2 | 3 | import android.os.Handler 4 | import android.os.Looper 5 | 6 | @Suppress("HasPlatformType", "MemberVisibilityCanBePrivate") 7 | internal object Threads { 8 | val main get() = Looper.getMainLooper().thread 9 | val current get() = Thread.currentThread() 10 | 11 | inline fun runOnMainThread(crossinline fn: () -> Unit) { 12 | val mainLooper = Looper.getMainLooper() 13 | if (mainLooper.thread == Thread.currentThread()) { 14 | fn() 15 | } else { 16 | Handler(mainLooper).post { fn() } 17 | } 18 | } 19 | 20 | fun checkMain() { 21 | if (current == main) return 22 | throw RuntimeException("wrong thread, expected main but actual (${current.name})") 23 | } 24 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/main/IJessieProgramManager.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.main 2 | 3 | import android.content.ComponentName 4 | import android.content.Intent 5 | import android.content.pm.PackageInfo 6 | import android.content.pm.ProviderInfo 7 | import android.content.pm.ResolveInfo 8 | import cn.jessie.program.DexInfo 9 | import cn.thens.okbinder.OkBinder 10 | 11 | @OkBinder.Interface 12 | interface IJessieProgramManager { 13 | fun getProgramPackageNames(): Array 14 | fun containsProgram(packageName: String): Boolean 15 | fun install(dexPath: String): String 16 | fun uninstall(program: String): Boolean 17 | fun getDexInfo(packageName: String): DexInfo? 18 | fun getPackageInfo(packageName: String): PackageInfo? 19 | fun getProcesses(packageName: String): Array 20 | fun preload(packageName: String) 21 | fun start(packageName: String) 22 | fun stop(packageName: String) 23 | fun requireProcessIndex(processName: String): Int 24 | fun getProgramProcessNameByPid(pid: Int): String 25 | 26 | fun wrapActivityIntent(intent: Intent): Intent 27 | fun queryIntentActivities(intent: Intent, flags: Int): MutableList 28 | fun queryIntentServices(intent: Intent, flags: Int): MutableList 29 | fun queryIntentContentProviders(intent: Intent, flags: Int): MutableList 30 | fun resolveActivity(intent: Intent, flags: Int): ResolveInfo? 31 | fun resolveService(intent: Intent, flags: Int): ResolveInfo? 32 | fun resolveContentProvider(authority: String, flags: Int): ProviderInfo? 33 | fun getComponentEnabledSetting(componentName: ComponentName): Int 34 | fun setComponentEnabledSetting(componentName: ComponentName, newState: Int, flags: Int) 35 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/main/JessieDaemonProviderImpl.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.main 2 | 3 | import android.content.ContentProvider 4 | import android.content.ContentValues 5 | import android.content.Context 6 | import android.content.pm.ProviderInfo 7 | import android.database.Cursor 8 | import android.net.Uri 9 | import cn.jessie.runtime.etc.BinderCursor 10 | 11 | /** 12 | * 守护者 Provider 13 | * 14 | * 用来和其他进程交互,并传递 Binder。 15 | */ 16 | class JessieDaemonProviderImpl : ContentProvider() { 17 | override fun attachInfo(context: Context?, info: ProviderInfo?) { 18 | MainAppContext.initialize(context!!) 19 | JessieServices.Daemon.initialize() 20 | super.attachInfo(context, info) 21 | } 22 | 23 | override fun onCreate(): Boolean { 24 | return true 25 | } 26 | 27 | override fun query(uri: Uri, projection: Array?, selection: String?, selectionArgs: Array?, sortOrder: String?): Cursor? { 28 | if (selection == null) return null 29 | val service = JessieServices.Daemon.query(selection) ?: return null 30 | return BinderCursor(service) 31 | } 32 | 33 | override fun insert(uri: Uri, values: ContentValues?): Uri? { 34 | return null 35 | } 36 | 37 | override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array?): Int { 38 | return -1 39 | } 40 | 41 | override fun delete(uri: Uri, selection: String?, selectionArgs: Array?): Int { 42 | return -1 43 | } 44 | 45 | override fun getType(uri: Uri): String? { 46 | return null 47 | } 48 | 49 | companion object { 50 | val authority by lazy { MainAppContext.packageName + ".jc.daemon" } 51 | } 52 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/main/JessieDaemonService.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.main 2 | 3 | import android.app.Service 4 | import android.content.Intent 5 | import android.os.IBinder 6 | 7 | /** 8 | * 守护者进程 Service,do nothing。 9 | * 10 | * 该 Service 只用来负责启动守护者进程(:jc 进程)。在守护者进程启动之后,会自动启动 [JessieDaemonProvider]。 11 | */ 12 | class JessieDaemonService : Service() { 13 | override fun onBind(intent: Intent?): IBinder? { 14 | return null 15 | } 16 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/main/JessieLauncherActivity.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.main 2 | 3 | import android.app.Activity 4 | import android.os.Bundle 5 | 6 | class JessieLauncherActivity: Activity() { 7 | override fun onCreate(savedInstanceState: Bundle?) { 8 | super.onCreate(savedInstanceState) 9 | finish() 10 | } 11 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/main/MainAppContext.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.main 2 | 3 | import android.annotation.SuppressLint 4 | import android.app.Application 5 | import android.content.Context 6 | import android.util.Log 7 | import cn.jessie.runtime.etc.Files 8 | import cn.jessie.runtime.etc.Init 9 | import cn.jessie.runtime.etc.Reflections 10 | import java.io.File 11 | 12 | /** 13 | * @author 7hens 14 | */ 15 | @SuppressLint("WrongConstant", "StaticFieldLeak") 16 | internal object MainAppContext { 17 | const val JESSIE_HOST_APPLICATION_SERVICE = "jessie_host_application" 18 | private val init = Init.create() 19 | private lateinit var app: Context 20 | private var isPluginProgram: Boolean = false 21 | val packageName: String by lazy { app.packageName } 22 | 23 | val debug: Boolean by lazy { 24 | try { 25 | require(init.isInitialized()) 26 | Reflections.get(Class.forName("$packageName.BuildConfig"), "DEBUG") as Boolean 27 | } catch (e: Throwable) { 28 | Log.e("@JC", MainAppContext::class.java.name + "#debug", e) 29 | false 30 | } 31 | } 32 | 33 | fun initialize(context: Context) { 34 | if (init.getElseInitialize()) return 35 | app = context.applicationContext as Application 36 | // Logdog.debug("JessieHostApplication = " + context.getSystemService(JESSIE_HOST_APPLICATION_SERVICE)) 37 | val hostApp = context.getSystemService(JESSIE_HOST_APPLICATION_SERVICE) as? Context 38 | if (hostApp != null) { 39 | isPluginProgram = true 40 | app = hostApp 41 | return 42 | } 43 | Processes.initialize(app) 44 | // Logdog.debug("MainApp.onCreate: ${Processes.current.let { it.processName + "#" + it.pid }}") 45 | } 46 | 47 | fun get(): Context = app 48 | 49 | fun isHost(): Boolean { 50 | require(init.isInitialized()) 51 | return !isPluginProgram 52 | } 53 | 54 | fun dir(name: String): File { 55 | return Files.dir(get().getDir(name, Context.MODE_PRIVATE)) 56 | } 57 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/main/MainInitProviderImpl.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.main 2 | 3 | import android.content.ContentProvider 4 | import android.content.ContentValues 5 | import android.content.Context 6 | import android.content.pm.ProviderInfo 7 | import android.database.Cursor 8 | import android.net.Uri 9 | import cn.jessie.runtime.test.JessieTests 10 | 11 | class MainInitProviderImpl : ContentProvider() { 12 | 13 | override fun attachInfo(context: Context?, info: ProviderInfo?) { 14 | super.attachInfo(context, info) 15 | MainAppContext.initialize(context!!) 16 | JessieServices.initialize(context) 17 | } 18 | 19 | override fun onCreate(): Boolean { 20 | return true 21 | } 22 | 23 | override fun getType(uri: Uri): String? = null 24 | 25 | override fun insert(uri: Uri, values: ContentValues?): Uri? = null 26 | 27 | override fun query(uri: Uri, projection: Array?, selection: String?, selectionArgs: Array?, sortOrder: String?): Cursor? = null 28 | 29 | override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array?): Int = 0 30 | 31 | override fun delete(uri: Uri, selection: String?, selectionArgs: Array?): Int = 0 32 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/main/ProcessDispatcher.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.main 2 | 3 | import cn.jessie.runtime.etc.JCLogger 4 | import cn.jessie.runtime.etc.NumberPool 5 | import cn.jessie.stub.JessieStubComponents 6 | import java.util.concurrent.ConcurrentHashMap 7 | 8 | /** 9 | * 插进进程分发器。 10 | */ 11 | internal object ProcessDispatcher { 12 | private const val MAX_SIZE = JessieStubComponents.PROCESS_COUNT 13 | private val runningProcessMap = ConcurrentHashMap() 14 | 15 | /** 16 | * 获取插件进程对应的索引。 17 | * 18 | * 如果对应的插件进程没有在运行,则返回 -1。 19 | * 20 | * @param processName 插件进程名,如:com.sample.app:h5 21 | * @return 宿主中对应进程坑位的索引,取值 -1~99,-1 表示该进程没有运行 22 | */ 23 | fun getProcessIndex(processName: String): Int { 24 | Processes.checkDaemon() 25 | return runningProcessMap[processName] ?: -1 26 | } 27 | 28 | fun requireProcessIndex(processName: String): Int { 29 | val index = getProcessIndex(processName) 30 | if (index >= 0) return index 31 | val newIndex = nextProcessIndex() 32 | runningProcessMap[processName] = newIndex 33 | return newIndex 34 | } 35 | 36 | fun getProcessName(index: Int): String { 37 | return "${MainAppContext.packageName}:jc$index" 38 | } 39 | 40 | fun queryProgramRunningProcesses(packageName: String): List { 41 | Processes.checkDaemon() 42 | return runningProcessMap.keys.filter { it == packageName || it.startsWith("$packageName:") } 43 | } 44 | 45 | fun queryProgramRunningProcessIndices(packageName: String): List { 46 | Processes.checkDaemon() 47 | return runningProcessMap 48 | .filter { it.key == packageName || it.key.startsWith("$packageName:") } 49 | .map { it.value } 50 | } 51 | 52 | fun getProgramProcessNameByPid(pid: Int): String { 53 | Processes.checkDaemon() 54 | return try { 55 | val process = Processes.all.first { it.pid == pid } 56 | val index = Processes.getJessieProcessIndex(process.processName) 57 | JCLogger.warn(process.processName + ", index = $index\n$runningProcessMap") 58 | runningProcessMap.entries.first { it.value == index }.key 59 | } catch (e: Throwable) { 60 | JCLogger.error(e) 61 | "" 62 | } 63 | } 64 | 65 | private val processPool = NumberPool(MAX_SIZE) 66 | 67 | private fun nextProcessIndex(): Int { 68 | var index = processPool.get() 69 | if (index == -1) { 70 | // FIXME 当前进程正在被使用,需要强杀 71 | index = processPool.force() 72 | } 73 | return index 74 | } 75 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/main/RemoteProgram.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.main 2 | 3 | import android.content.pm.PackageInfo 4 | import cn.jessie.program.DexInfo 5 | import cn.jessie.runtime.etc.JCLogger 6 | import cn.jessie.runtime.program.DexProgram 7 | 8 | internal class RemoteProgram(override val packageName: String) : DexProgram() { 9 | 10 | private val programManager get() = JessieServices.programManager 11 | 12 | override val dexInfo: DexInfo by lazy { 13 | programManager.getDexInfo(packageName)!! 14 | } 15 | 16 | override val packageInfo: PackageInfo by lazy { 17 | programManager.getPackageInfo(packageName)!! 18 | } 19 | 20 | override fun preload() { 21 | programManager.preload(packageName) 22 | } 23 | 24 | override fun start() { 25 | programManager.start(packageName) 26 | } 27 | 28 | override fun stop() { 29 | try { 30 | programManager.stop(packageName) 31 | } catch (e: Throwable) { 32 | JCLogger.debug("packageName = $packageName") 33 | JCLogger.error(e) 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/program/AbstractProgram.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.program 2 | 3 | import cn.jessie.program.Program 4 | 5 | abstract class AbstractProgram : Program { 6 | override fun equals(other: Any?): Boolean { 7 | return if (other is Program) { 8 | packageName == other.packageName 9 | } else { 10 | super.equals(other) 11 | } 12 | } 13 | 14 | override fun hashCode(): Int { 15 | return packageName.hashCode() 16 | } 17 | 18 | override fun toString(): String { 19 | return "${super.toString()}($packageName)" 20 | } 21 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/program/AndroidHook.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.program 2 | 3 | import android.app.Application 4 | import android.app.Service 5 | import android.content.ContentProvider 6 | import cn.jessie.etc.JCLogger 7 | import cn.jessie.program.DexInfo 8 | import cn.jessie.program.DexInstaller 9 | import cn.jessie.runtime.main.MainAppContext 10 | import java.io.File 11 | 12 | internal object AndroidHook { 13 | private const val JESSIE_HOOK_JAR = "jessie/android-hook.jar" 14 | private const val HOOK_DIR = "jc_hook" 15 | private const val BASE_JAR = "base.jar" 16 | 17 | private val hookDir by lazy { MainAppContext.dir(HOOK_DIR) } 18 | 19 | val hookedClassNames = arrayOf( 20 | Application::class.java.name, 21 | // Activity::class.java.name, 22 | Service::class.java.name, 23 | // BroadcastReceiver::class.java.name, 24 | ContentProvider::class.java.name, 25 | "androidx.core.app.NotificationCompat\$Builder", 26 | "androidx.core.app.NotificationCompatBuilder", 27 | "androidx.core.app.NotificationBuilderWithBuilderAccessor", 28 | "android.support.v4.app.NotificationCompat\$Builder" 29 | // Notification::class.java.name, 30 | // Notification.Builder::class.java.name 31 | ) 32 | 33 | fun classLoader(parent: ClassLoader): ClassLoader { 34 | val dexFile = File(hookDir, BASE_JAR) 35 | val dexFileSize = if (dexFile.exists()) dexFile.length() else 0L 36 | val shouldCreateNewDexFile = dexFileSize == 0L || run { 37 | MainAppContext.get().assets.open(JESSIE_HOOK_JAR) 38 | .use { it.available().toLong() != dexFileSize } 39 | } 40 | if (shouldCreateNewDexFile) { 41 | if (dexFileSize != 0L) { 42 | dexFile.delete() 43 | dexFile.createNewFile() 44 | } 45 | JCLogger.debug("copy android_hook.jar...") 46 | MainAppContext.get().assets.open(JESSIE_HOOK_JAR) 47 | .buffered() 48 | .use { it.copyTo(dexFile.outputStream()) } 49 | } 50 | val dexInfo = DexInstaller.install(dexFile, shouldCreateNewDexFile) 51 | return HookClassLoader(parent, dexInfo, hookedClassNames) 52 | } 53 | 54 | private class HookClassLoader( 55 | parent: ClassLoader?, dexInfo: DexInfo, 56 | private val hookedClassNames: Array) : DexInfo.ClassLoader(parent, dexInfo) { 57 | 58 | override fun loadClass(name: String, resolve: Boolean): Class<*> { 59 | return findLoadedClass(name) ?: run { 60 | val shouldHook = name in hookedClassNames 61 | JCLogger.onlyIf(shouldHook).debug("hook class $name") 62 | return try { 63 | if (!shouldHook) throw RuntimeException() 64 | findClass(name)!! 65 | } catch (e: Throwable) { 66 | if (shouldHook) JCLogger.error(e) 67 | parent.loadClass(name) 68 | } 69 | } 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/program/AndroidPackageComponentsParser.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.program 2 | 3 | import android.content.IntentFilter 4 | import android.content.pm.ComponentInfo 5 | import android.content.pm.PackageInfo 6 | import android.content.res.AssetManager 7 | import cn.jessie.program.AndroidPackageComponents 8 | 9 | object AndroidPackageComponentsParser { 10 | private const val ACTION_MAIN = "android.intent.action.MAIN" 11 | private const val CATEGORY_LAUNCHER = "android.intent.category.LAUNCHER" 12 | 13 | val launcherFilter by lazy { 14 | IntentFilter().apply { 15 | addAction(ACTION_MAIN) 16 | addCategory(CATEGORY_LAUNCHER) 17 | } 18 | } 19 | 20 | fun parse(packageInfo: PackageInfo, assetManager: AssetManager): AndroidPackageComponents { 21 | val intentFilterMap = AndroidManifestParser.parse(assetManager) 22 | fun map(components: Array, key: T.() -> String) = run { 23 | components 24 | .map { component -> 25 | val name = key(component) 26 | val intentFilters = intentFilterMap[name] ?: emptyList() 27 | name to AndroidPackageComponents.Item(component, intentFilters) 28 | } 29 | .toMap() 30 | } 31 | 32 | return AndroidPackageComponents( 33 | packageInfo.applicationInfo, 34 | map(packageInfo.activities) { name }, 35 | map(packageInfo.services) { name }, 36 | map(packageInfo.receivers) { name }, 37 | map(packageInfo.providers) { authority } 38 | ) 39 | } 40 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/program/AndroidPackageInfo.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.program 2 | 3 | import android.content.Context 4 | import android.content.pm.PackageInfo 5 | import android.content.pm.PackageManager 6 | import android.os.Build 7 | import cn.jessie.program.DexInfo 8 | import cn.jessie.runtime.etc.Files 9 | import cn.jessie.runtime.main.MainAppContext 10 | import java.io.File 11 | 12 | object AndroidPackageInfo { 13 | 14 | private val FLAG_GET_SIGNING_CERTIFICATES = when { 15 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.P -> PackageManager.GET_SIGNING_CERTIFICATES 16 | else -> PackageManager.GET_SIGNATURES 17 | } 18 | 19 | private val FLAG_MATCH_UNINSTALLED_PACKAGES = when { 20 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> PackageManager.MATCH_UNINSTALLED_PACKAGES 21 | else -> PackageManager.GET_UNINSTALLED_PACKAGES 22 | } 23 | 24 | private val FLAG_MATCH_DISABLED_COMPONENTS = when { 25 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> PackageManager.MATCH_DISABLED_COMPONENTS 26 | else -> PackageManager.GET_DISABLED_COMPONENTS 27 | } 28 | 29 | private val FLAG_MATCH_DISABLED_UNTIL_USED_COMPONENTS = when { 30 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS 31 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 -> PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS 32 | else -> 0 33 | } 34 | 35 | private val packageInfoFlags = 0 or 36 | PackageManager.GET_ACTIVITIES or 37 | PackageManager.GET_SERVICES or 38 | PackageManager.GET_PROVIDERS or 39 | PackageManager.GET_RECEIVERS or 40 | PackageManager.GET_META_DATA or 41 | PackageManager.GET_INTENT_FILTERS or 42 | PackageManager.GET_URI_PERMISSION_PATTERNS or 43 | PackageManager.GET_SHARED_LIBRARY_FILES or 44 | PackageManager.GET_PERMISSIONS or 45 | PackageManager.GET_INSTRUMENTATION or 46 | FLAG_GET_SIGNING_CERTIFICATES or 47 | FLAG_MATCH_DISABLED_COMPONENTS or 48 | FLAG_MATCH_DISABLED_UNTIL_USED_COMPONENTS or 49 | FLAG_MATCH_UNINSTALLED_PACKAGES 50 | 51 | fun get(dexInfo: DexInfo): PackageInfo { 52 | val context = MainAppContext.get() 53 | val packageManager = context.packageManager 54 | val apkPath = dexInfo.dexPath 55 | val packageInfo = packageManager.getPackageArchiveInfo(apkPath, packageInfoFlags).check() 56 | val applicationInfo = packageInfo.applicationInfo 57 | applicationInfo.sourceDir = apkPath 58 | applicationInfo.publicSourceDir = apkPath 59 | applicationInfo.nativeLibraryDir = dexInfo.libraryDirectory 60 | 61 | val parentDir = File(apkPath).parentFile 62 | val dataDir = Files.dir(File(parentDir, "data")) 63 | applicationInfo.dataDir = dataDir.absolutePath 64 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 65 | applicationInfo.deviceProtectedDataDir = dataDir.absolutePath 66 | } 67 | return packageInfo 68 | } 69 | 70 | fun get(context: Context): PackageInfo { 71 | return context.packageManager.getPackageInfo(context.packageName, 72 | packageInfoFlags and FLAG_GET_SIGNING_CERTIFICATES.inv()) 73 | .check() 74 | } 75 | 76 | private fun PackageInfo.check(): PackageInfo { 77 | if (activities == null) activities = arrayOf() 78 | if (services == null) services = arrayOf() 79 | if (receivers == null) receivers = arrayOf() 80 | if (providers == null) providers = arrayOf() 81 | return this 82 | } 83 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/program/AppProgram.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.program 2 | 3 | import android.content.ComponentName 4 | import android.content.Context 5 | import android.content.Intent 6 | import android.content.pm.PackageInfo 7 | import android.content.res.Resources 8 | import cn.jessie.program.AndroidPackageComponents 9 | import cn.jessie.program.DexInfo 10 | import cn.jessie.runtime.main.MainAppContext 11 | 12 | /** 13 | * @author 7hens 14 | */ 15 | internal class AppProgram(private val app: Context) : AbstractProgram() { 16 | override val packageName: String by lazy { app.packageName } 17 | override val classLoader: ClassLoader = app.classLoader 18 | override val resources: Resources = app.resources 19 | override val dexInfo: DexInfo by lazy { 20 | app.applicationInfo.run { DexInfo(sourceDir, "", nativeLibraryDir) } 21 | } 22 | 23 | override val packageInfo: PackageInfo by lazy { AndroidPackageInfo.get(app) } 24 | 25 | override val packageComponents: AndroidPackageComponents by lazy { 26 | AndroidPackageComponentsParser.parse(packageInfo, resources.assets) 27 | } 28 | 29 | override fun preload() { 30 | packageComponents 31 | } 32 | 33 | override fun start() { 34 | val launcherActivity = packageComponents.launcherActivities 35 | .map { it.component } 36 | .filter { it.processName == packageInfo.applicationInfo.processName && it.isEnabled } 37 | .map { it.name } 38 | .getOrElse(0) { "" } 39 | val intent = Intent() 40 | .setComponent(ComponentName(packageName, launcherActivity)) 41 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) 42 | MainAppContext.get().startActivity(intent) 43 | } 44 | 45 | override fun stop() { 46 | throw IllegalAccessException("cannot stop host app") 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/program/DexProgram.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.program 2 | 3 | import android.content.Context 4 | import android.content.res.Resources 5 | import cn.jessie.program.DexInfo 6 | import cn.jessie.runtime.etc.JCLogger 7 | import cn.jessie.runtime.main.MainAppContext 8 | 9 | abstract class DexProgram : AbstractProgram() { 10 | 11 | override val packageComponents by lazy { 12 | AndroidPackageComponentsParser.parse(packageInfo, resources.assets) 13 | } 14 | 15 | override val packageInfo by lazy { 16 | AndroidPackageInfo.get(dexInfo) 17 | } 18 | 19 | override val classLoader: ClassLoader by lazy { 20 | DexProgram::class.java.classLoader!! 21 | .let { AndroidHook.classLoader(it) } 22 | .let { PluginClassLoader(it, dexInfo) } 23 | } 24 | 25 | override val resources: Resources by lazy { 26 | val context = MainAppContext.get() 27 | val packageManager = context.packageManager 28 | val resources = packageManager.getResourcesForApplication(packageInfo.applicationInfo) 29 | MergedResources(resources, context.resources) 30 | } 31 | 32 | private fun getResource(context: Context, resources: Resources, packageName: String): Resources { 33 | val configRes = resources 34 | return object : Resources(resources.assets, configRes.displayMetrics, configRes.configuration) { 35 | override fun getIdentifier(name: String?, defType: String?, defPackage: String?): Int { 36 | JCLogger.debug("name = $name\ndefType = $defType\ndefPackage = $defPackage") 37 | var id = super.getIdentifier(name, defType, packageName) 38 | if (id == 0) { 39 | id = super.getIdentifier(name, defType, defPackage) 40 | } 41 | return id 42 | } 43 | } 44 | } 45 | 46 | override fun preload() { 47 | try { 48 | classLoader 49 | packageComponents 50 | } catch (e: Throwable) { 51 | JCLogger.error(e).error(packageName) 52 | } 53 | } 54 | 55 | private class PluginClassLoader(parent: ClassLoader?, dexInfo: DexInfo) 56 | : DexInfo.ClassLoader(parent, dexInfo) { 57 | 58 | override fun loadClass(name: String, resolve: Boolean): Class<*> { 59 | if (name in AndroidHook.hookedClassNames) { 60 | return parent.loadClass(name) 61 | } 62 | return findLoadedClass(name) ?: run { 63 | try { 64 | findClass(name)!! 65 | } catch (e: Throwable) { 66 | parent.loadClass(name) 67 | } 68 | } 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/program/PluginManager.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.program 2 | 3 | import cn.jessie.program.DexInstaller 4 | import cn.jessie.program.Program 5 | import cn.jessie.runtime.etc.Files 6 | import cn.jessie.runtime.etc.JCLogger 7 | import cn.jessie.runtime.main.MainAppContext 8 | import cn.jessie.runtime.main.Processes 9 | import java.io.File 10 | import java.io.FileNotFoundException 11 | import java.util.concurrent.ConcurrentHashMap 12 | 13 | /** 14 | * @author 7hens 15 | */ 16 | class PluginManager(private val programRootDir: File) { 17 | 18 | private val programs: MutableMap = ConcurrentHashMap() 19 | 20 | fun all(): Map { 21 | Processes.checkDaemon() 22 | return programs 23 | } 24 | 25 | fun install(apk: File): Program { 26 | val apkPath = apk.absolutePath 27 | val context = MainAppContext.get() 28 | val packageManager = context.packageManager 29 | 30 | if (!apk.exists() || !apk.isFile) { 31 | throw FileNotFoundException("install error, file($apkPath) not exists") 32 | } 33 | 34 | val packageInfo = packageManager.getPackageArchiveInfo(apkPath, 0) 35 | ?: throw RuntimeException("install error, packageInfo is null, apkPath: $apkPath") 36 | val packageName = packageInfo.packageName 37 | val programDir = Files.dir(File(programRootDir, packageName)) 38 | val newApk = File(programDir, BASK_APK) 39 | if (!newApk.exists()) newApk.createNewFile() 40 | val newApkPath = newApk.absolutePath 41 | val isNewApk = apkPath != newApkPath 42 | val isSameApk = !isNewApk || apk.length() == newApk.length() 43 | if (programs.containsKey(packageName) && isSameApk) { 44 | return programs.getValue(packageName) 45 | } 46 | if (isNewApk) apk.copyTo(newApk, true) 47 | val program = PluginProgram(packageName, DexInstaller.install(newApk, isNewApk)) 48 | programs[packageName] = program 49 | return program 50 | } 51 | 52 | fun installAll() { 53 | for (programDir in programDirs) { 54 | try { 55 | val packageName = programDir.name 56 | val apkFile = File(programDir, BASK_APK) 57 | if (!(apkFile.exists() && apkFile.isFile && apkFile.length() > 0L)) { 58 | val result = Files.delete(programDir) 59 | JCLogger.error("program($packageName) is missing, removed $result") 60 | continue 61 | } 62 | val dexInfo = DexInstaller.install(apkFile, false) 63 | programs[packageName] = PluginProgram(packageName, dexInfo) 64 | // Logdog.debug("install inner program($packageName)") 65 | } catch (e: Throwable) { 66 | JCLogger.error(e) 67 | } 68 | } 69 | // Logdog.debug("install inner complete") 70 | } 71 | 72 | fun uninstall(packageName: String): Boolean { 73 | val programDir = File(programRootDir, packageName) 74 | if (!programDir.exists()) return true 75 | return Files.delete(programDir) 76 | } 77 | 78 | fun uninstallAll(): Boolean { 79 | var result = true 80 | for (programDir in programDirs) { 81 | result = result && Files.delete(programDir) 82 | } 83 | return result 84 | } 85 | 86 | private val programDirs get() = programRootDir.listFiles() ?: emptyArray() 87 | 88 | companion object { 89 | private const val BASK_APK = "base.jar" 90 | } 91 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/program/PluginProgram.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.program 2 | 3 | import android.content.ComponentName 4 | import android.content.Intent 5 | import cn.jessie.program.DexInfo 6 | import cn.jessie.runtime.app.provider.JessieStubProvider 7 | import cn.jessie.runtime.etc.JCLogger 8 | import cn.jessie.runtime.main.JessieProgramManagerImpl 9 | import cn.jessie.runtime.main.MainAppContext 10 | import cn.jessie.runtime.main.ProcessDispatcher 11 | 12 | internal class PluginProgram( 13 | override val packageName: String, 14 | override val dexInfo: DexInfo) : DexProgram() { 15 | 16 | override fun start() { 17 | val launcherActivity = packageComponents.launcherActivities 18 | .map { it.component } 19 | .filter { it.processName == packageInfo.applicationInfo.processName && it.isEnabled } 20 | .map { it.name } 21 | .getOrElse(0) { "" } 22 | if (launcherActivity.isNullOrEmpty()) { 23 | JCLogger.warn("There is not a launcher activity in program $packageName") 24 | return 25 | } 26 | var intent = Intent().setComponent(ComponentName(packageName, launcherActivity)) 27 | intent = JessieProgramManagerImpl.get().wrapActivityIntent(intent) 28 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) 29 | MainAppContext.get().startActivity(intent) 30 | } 31 | 32 | override fun stop() { 33 | val contentResolver = MainAppContext.get().contentResolver 34 | ProcessDispatcher.queryProgramRunningProcessIndices(packageName).forEach { index -> 35 | try { 36 | // TODO 需要回收进程和 Activity 37 | val uri = JessieStubProvider.uri(index, JessieStubProvider.ACTION_EXIT) 38 | contentResolver.query(uri, null, null, null, null)?.close() 39 | } catch (e: Throwable) { 40 | JCLogger.error(e) 41 | } 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/cn/jessie/runtime/test/JessieTests.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.runtime.test 2 | 3 | import android.view.LayoutInflater 4 | import cn.jessie.etc.JCLogger 5 | import cn.jessie.runtime.etc.Reflections 6 | import cn.jessie.runtime.main.MainAppContext 7 | import cn.jessie.runtime.program.AndroidHook 8 | import java.lang.reflect.Constructor 9 | import java.util.* 10 | 11 | @Suppress("UNCHECKED_CAST", "MemberVisibilityCanBePrivate") 12 | internal object JessieTests { 13 | fun inflater(layoutInflater: LayoutInflater) { 14 | val constructorMap = Reflections.get(layoutInflater, "sConstructorMap") as HashMap> 15 | var debugText = "" 16 | constructorMap.values.forEach { 17 | debugText += it.declaringClass.name + ": " + it.declaringClass.classLoader + "\n" 18 | } 19 | JCLogger.debug(debugText) 20 | } 21 | 22 | fun printClass(clazz: Class<*>) { 23 | var cls: Class<*>? = clazz 24 | var result = "" 25 | while (cls != null) { 26 | result += cls.canonicalName!! + " (${cls.classLoader})\n" 27 | cls = cls.superclass 28 | } 29 | JCLogger.debug(result) 30 | } 31 | 32 | fun printClass(className: String) { 33 | val classLoader = AndroidHook.classLoader(MainAppContext.get().classLoader) 34 | printClass(classLoader.loadClass(className)) 35 | } 36 | } -------------------------------------------------------------------------------- /jessie-runtime/src/main/java/java/io/FileHook.kt: -------------------------------------------------------------------------------- 1 | package java.io 2 | 3 | import android.annotation.SuppressLint 4 | import cn.jessie.runtime.app.MyProgram 5 | import cn.jessie.runtime.main.MainAppContext 6 | import java.net.URI 7 | 8 | open class FileHook : File { 9 | 10 | constructor(pathname: String) : super(wrapPath(pathname)) 11 | 12 | constructor(parent: String, child: String) : super(wrapPath(parent + pathSeparator + child)) 13 | 14 | constructor(parent: File, child: String) : super(parent.absolutePath, child) 15 | 16 | constructor(uri: URI) : super(uri) 17 | 18 | companion object { 19 | @SuppressLint("SdCardPath") 20 | private fun wrapPath(path: String): String { 21 | val host = MainAppContext.packageName 22 | val program = MyProgram.packageName 23 | return path.replace("/data/data/$program", "/data/data/$host/app_jc_plugins/$program/data") 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /jessie-runtime/src/test/java/cn/thens/jessie/runtime/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package cn.thens.runtime 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jessie/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'com.github.dcendents.android-maven' 4 | apply plugin: 'maven-publish' 5 | apply from: 'components.gradle' 6 | 7 | group = "com.github.7hens" 8 | version = "-SNAPSHOT" 9 | 10 | android { 11 | compileSdkVersion 29 12 | 13 | defaultConfig { 14 | minSdkVersion 15 15 | targetSdkVersion 29 16 | versionCode 1 17 | versionName "1.0" 18 | 19 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 20 | consumerProguardFiles 'proguard-rules.pro' 21 | } 22 | testOptions { 23 | unitTests { 24 | includeAndroidResources = true 25 | } 26 | } 27 | lintOptions { 28 | abortOnError false 29 | disable 'WrongConstant', "Deprecation" 30 | } 31 | } 32 | 33 | dependencies { 34 | implementation fileTree(dir: 'libs', include: ['*.jar']) 35 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 36 | compileOnly project(':android-hide') 37 | implementation 'com.github.7hens:okbinder:1.0' 38 | implementation 'cn.thens:okparcelable:0.1.0' 39 | implementation 'com.github.7hens:logdog:0.4' 40 | 41 | testImplementation 'junit:junit:4.12' 42 | testImplementation 'org.mockito:mockito-core:2.22.0' 43 | testImplementation 'org.robolectric:robolectric:4.2.1' 44 | androidTestImplementation 'androidx.test:runner:1.2.0' 45 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 46 | } 47 | 48 | def createUpgradeTask = { String name, String project -> 49 | def dependency = rootProject.tasks.findByPath(":$project:dexDebug") 50 | def outputDir = file("src/main/assets/jessie") 51 | task(type: Jar, dependsOn: dependency.path, group: 'jessie', name) { 52 | from dependency.outputs.files.singleFile 53 | archiveFileName = "${project}.jar" 54 | destinationDirectory = outputDir 55 | } 56 | } 57 | 58 | createUpgradeTask('upgradeHook', 'android-hook') 59 | createUpgradeTask('upgradeRuntime', 'jessie-runtime') 60 | -------------------------------------------------------------------------------- /jessie/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -keep public class android.** { 23 | public *; 24 | } 25 | -keep public class java.** { 26 | public *; 27 | } 28 | -------------------------------------------------------------------------------- /jessie/src/main/assets/jessie/android-hook.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/jessie/src/main/assets/jessie/android-hook.jar -------------------------------------------------------------------------------- /jessie/src/main/assets/jessie/jessie-runtime.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/jessie/src/main/assets/jessie/jessie-runtime.jar -------------------------------------------------------------------------------- /jessie/src/main/java/android/app/ServiceWrapper.kt: -------------------------------------------------------------------------------- 1 | package android.app 2 | 3 | import android.content.Context 4 | import android.content.ContextWrapper 5 | import android.content.Intent 6 | import android.content.res.Configuration 7 | import android.os.IBinder 8 | import java.io.FileDescriptor 9 | import java.io.PrintWriter 10 | 11 | abstract class ServiceWrapper : Service() { 12 | abstract val baseService: Service 13 | 14 | override fun attachBaseContext(newBase: Context?) { 15 | super.attachBaseContext(newBase) 16 | val cContextWrapper = ContextWrapper::class.java 17 | val cContext = Context::class.java 18 | val attachBaseContext = cContextWrapper.getDeclaredMethod("attachBaseContext", cContext) 19 | attachBaseContext.isAccessible = true 20 | attachBaseContext.invoke(baseService, this) 21 | } 22 | 23 | override fun onConfigurationChanged(newConfig: Configuration) { 24 | baseService.onConfigurationChanged(newConfig) 25 | } 26 | 27 | override fun onRebind(intent: Intent?) { 28 | baseService.onRebind(intent) 29 | } 30 | 31 | override fun dump(fd: FileDescriptor?, writer: PrintWriter?, args: Array?) { 32 | baseService.dump(fd, writer, args) 33 | } 34 | 35 | override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { 36 | return baseService.onStartCommand(intent, flags, startId) 37 | } 38 | 39 | override fun onCreate() { 40 | baseService.onCreate() 41 | } 42 | 43 | override fun onLowMemory() { 44 | baseService.onLowMemory() 45 | } 46 | 47 | override fun onStart(intent: Intent?, startId: Int) { 48 | baseService.onStart(intent, startId) 49 | } 50 | 51 | override fun onTaskRemoved(rootIntent: Intent?) { 52 | baseService.onTaskRemoved(rootIntent) 53 | } 54 | 55 | override fun onBind(intent: Intent?): IBinder? { 56 | return baseService.onBind(intent) 57 | } 58 | 59 | override fun onTrimMemory(level: Int) { 60 | baseService.onTrimMemory(level) 61 | } 62 | 63 | override fun onUnbind(intent: Intent?): Boolean { 64 | return baseService.onUnbind(intent) 65 | } 66 | 67 | override fun onDestroy() { 68 | baseService.onDestroy() 69 | } 70 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/Jessie.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie 2 | 3 | import android.annotation.TargetApi 4 | import android.app.Activity 5 | import android.content.* 6 | import android.os.Build 7 | import android.os.Bundle 8 | import cn.jessie.program.Program 9 | import java.io.File 10 | 11 | interface Jessie { 12 | val hostProgram: Program 13 | val currentProgram: Program 14 | val programs: Map 15 | val currentProgramProcessName: String 16 | fun getProgram(packageName: String): Program? 17 | fun containsProgram(packageName: String): Boolean 18 | fun install(apk: File): Program 19 | fun uninstall(program: String): Boolean 20 | fun startActivity(context: Context, intent: Intent) 21 | fun startActivityForResult(context: Activity, intent: Intent, requestCode: Int) 22 | 23 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 24 | fun startActivity(context: Context, intent: Intent, options: Bundle?) 25 | 26 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 27 | fun startActivityForResult(context: Activity, intent: Intent, requestCode: Int, options: Bundle?) 28 | 29 | fun startService(context: Context, intent: Intent): ComponentName? 30 | fun stopService(context: Context, intent: Intent): Boolean 31 | fun bindService(context: Context, intent: Intent, conn: ServiceConnection, flags: Int): Boolean 32 | fun unbindService(context: Context, conn: ServiceConnection) 33 | fun getContentResolver(context: Context): ContentResolver 34 | 35 | companion object : Jessie by JessieRuntime.jessie 36 | } 37 | -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/JessieRuntime.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie 2 | 3 | import android.content.ContentProvider 4 | import android.content.Context 5 | import cn.jessie.program.DexInfo 6 | import cn.jessie.program.DexInstaller 7 | import java.io.File 8 | import java.util.concurrent.atomic.AtomicBoolean 9 | 10 | @Suppress("SameParameterValue") 11 | internal object JessieRuntime { 12 | private const val JESSIE_IMPL = "cn.jessie.runtime.JessieImpl" 13 | private const val MAIN_PROVIDER = "cn.jessie.runtime.main.MainInitProviderImpl" 14 | private const val DAEMON_PROVIDER = "cn.jessie.runtime.main.JessieDaemonProviderImpl" 15 | 16 | private const val RUNTIME_JAR = "jessie/jessie-runtime.jar" 17 | private const val RUNTIME_DIR = "runtime" 18 | private const val BASE_JAR = "base.jar" 19 | 20 | lateinit var context: Context 21 | 22 | private fun classLoader(context: Context): ClassLoader { 23 | val runtimeDir = context.getDir(RUNTIME_DIR, Context.MODE_PRIVATE) 24 | val dexFile = File(runtimeDir, BASE_JAR) 25 | val dexFileSize = if (dexFile.exists()) dexFile.length() else 0L 26 | val shouldCreateNewDexFile = dexFileSize == 0L || run { 27 | context.assets.open(RUNTIME_JAR) 28 | .use { it.available().toLong() != dexFileSize } 29 | } 30 | if (shouldCreateNewDexFile) { 31 | if (dexFileSize != 0L) { 32 | dexFile.delete() 33 | dexFile.createNewFile() 34 | } 35 | context.assets.open(RUNTIME_JAR) 36 | .buffered() 37 | .use { it.copyTo(dexFile.outputStream()) } 38 | } 39 | val dexInfo = DexInstaller.install(dexFile, shouldCreateNewDexFile) 40 | return DexInfo.ClassLoader(context.classLoader, dexInfo) 41 | } 42 | 43 | private val isInitialized = AtomicBoolean(false) 44 | private lateinit var classLoader: ClassLoader 45 | 46 | fun initialize(context: Context) { 47 | if (isInitialized.compareAndSet(false, true)) { 48 | this.context = context 49 | classLoader = classLoader(context) 50 | } 51 | } 52 | 53 | @Suppress("UNCHECKED_CAST") 54 | private fun get(name: String): T { 55 | return classLoader.loadClass(name).getField("INSTANCE").get(null) as T 56 | } 57 | 58 | @Suppress("UNCHECKED_CAST") 59 | fun create(name: String): T { 60 | return classLoader.loadClass(name).newInstance() as T 61 | } 62 | 63 | val mainProvider by lazy { create(MAIN_PROVIDER) } 64 | 65 | val daemonProvider by lazy { create(DAEMON_PROVIDER) } 66 | 67 | val jessie by lazy { get(JESSIE_IMPL) } 68 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/etc/AndroidVM.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.etc 2 | 3 | import android.annotation.SuppressLint 4 | import android.os.Build 5 | 6 | /** 7 | * @author 7hens 8 | */ 9 | @SuppressLint("PrivateApi") 10 | internal object AndroidVM { 11 | /** 12 | * 是否运行 ART 虚拟机 13 | */ 14 | val isArt: Boolean by lazy { 15 | System.getProperty("java.vm.version", "")?.startsWith("2") ?: false 16 | } 17 | 18 | @Suppress("LocalVariableName") 19 | val is64Bit: Boolean by lazy { 20 | try { 21 | // Android API 21之前不支持64位CPU 22 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 23 | false 24 | } else { 25 | val cVMRuntime = Class.forName("dalvik.system.VMRuntime") 26 | val runtime = cVMRuntime.getDeclaredMethod("getRuntime").invoke(null) 27 | cVMRuntime.getDeclaredMethod("is64Bit").invoke(runtime) as Boolean 28 | } 29 | } catch (e: Throwable) { 30 | JCLogger.error(e) 31 | false 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/etc/FilePermissions.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.etc 2 | 3 | import android.annotation.SuppressLint 4 | import android.os.FileUtils 5 | 6 | @Suppress("MemberVisibilityCanBePrivate", "unused") 7 | @SuppressLint("PrivateApi") 8 | internal object FilePermissions { 9 | const val UR = 0b100000000 10 | const val UW = 0b10000000 11 | const val UX = 0b1000000 12 | const val GR = 0b100000 13 | const val GW = 0b10000 14 | const val GX = 0b1000 15 | const val OR = 0b100 16 | const val OW = 0b10 17 | const val OX = 0b1 18 | 19 | const val U7 = UR or UW or UX 20 | const val G7 = GR or GW or GX 21 | const val O7 = OR or OW or OX 22 | 23 | fun set(filePath: String, mode: Int, uid: Int = -1, gid: Int = -1): Int { 24 | return try { 25 | FileUtils.setPermissions(filePath, mode, uid, gid) 26 | } catch (e: Throwable) { 27 | JCLogger.error(e) 28 | -1 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/etc/Files.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.etc 2 | 3 | import java.io.File 4 | import java.io.FileInputStream 5 | 6 | internal object Files { 7 | fun dir(file: File): File { 8 | if (!file.exists()) { 9 | file.mkdirs() 10 | } 11 | return file 12 | } 13 | 14 | fun delete(file: File): Boolean { 15 | return file.deleteRecursively() 16 | } 17 | 18 | fun read(file: String): String { 19 | return try { 20 | FileInputStream(file).bufferedReader().use { it.readText() } 21 | } catch (e: Throwable) { 22 | JCLogger.error(e) 23 | "" 24 | } 25 | } 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/etc/JCLogger.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.etc 2 | 3 | import cn.thens.logdog.Logdog 4 | 5 | val JCLogger: Logdog = Logdog.get().tag("JC") 6 | -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/etc/ReflectionUtils.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.etc 2 | 3 | import java.lang.reflect.Modifier 4 | 5 | internal object ReflectionUtils { 6 | fun copyFields(cls: Class, from: T, to: T) { 7 | for (field in cls.declaredFields) { 8 | if (Modifier.isStatic(field.modifiers)) continue 9 | field.isAccessible = true 10 | field.set(to, field.get(from)) 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/main/JessieDaemonProvider.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.main 2 | 3 | import android.content.ContentProvider 4 | import android.content.ContentProviderWrapper 5 | import android.content.Context 6 | import android.content.pm.ProviderInfo 7 | import cn.jessie.JessieRuntime 8 | 9 | /** 10 | * 守护者 Provider 11 | * 12 | * 用来和其他进程交互,并传递 Binder。 13 | */ 14 | class JessieDaemonProvider : ContentProviderWrapper() { 15 | override val baseProvider: ContentProvider by lazy { JessieRuntime.daemonProvider } 16 | 17 | override fun attachInfo(context: Context?, info: ProviderInfo?) { 18 | JessieRuntime.initialize(context!!) 19 | super.attachInfo(context, info) 20 | } 21 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/main/JessieDaemonService.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.main 2 | 3 | import android.app.Service 4 | import android.content.Intent 5 | import android.os.IBinder 6 | 7 | /** 8 | * 守护者进程 Service,do nothing。 9 | * 10 | * 该 Service 只用来负责启动守护者进程(:jc 进程)。在守护者进程启动之后,会自动启动 [JessieDaemonProvider]。 11 | */ 12 | class JessieDaemonService : Service() { 13 | override fun onBind(intent: Intent?): IBinder? { 14 | return null 15 | } 16 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/main/MainInitProvider.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.main 2 | 3 | import android.content.ContentProvider 4 | import android.content.ContentProviderWrapper 5 | import android.content.Context 6 | import android.content.pm.ProviderInfo 7 | import cn.jessie.JessieRuntime 8 | 9 | class MainInitProvider : ContentProviderWrapper() { 10 | override val baseProvider: ContentProvider by lazy { JessieRuntime.mainProvider } 11 | 12 | override fun attachInfo(context: Context?, info: ProviderInfo?) { 13 | JessieRuntime.initialize(context!!) 14 | super.attachInfo(context, info) 15 | } 16 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/program/AndroidPackageComponents.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.program 2 | 3 | import android.content.IntentFilter 4 | import android.content.pm.* 5 | 6 | class AndroidPackageComponents( 7 | val application: ApplicationInfo, 8 | val activities: Map>, 9 | val services: Map>, 10 | val receivers: Map>, 11 | val providers: Map>) { 12 | 13 | val launcherActivities by lazy { 14 | activities.values.filter { item -> 15 | item.intentFilters.find { it.hasAction(ACTION_MAIN) && it.hasCategory(CATEGORY_LAUNCHER) } != null 16 | } 17 | } 18 | 19 | companion object { 20 | private const val ACTION_MAIN = "android.intent.action.MAIN" 21 | private const val CATEGORY_LAUNCHER = "android.intent.category.LAUNCHER" 22 | } 23 | 24 | data class Item( 25 | val component: T, 26 | val intentFilters: List 27 | ) 28 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/program/DexInfo.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.program 2 | 3 | import cn.thens.okparcelable.OkParcelable 4 | import dalvik.system.DexClassLoader 5 | 6 | class DexInfo( 7 | val dexPath: String, 8 | val optimizedDirectory: String, 9 | val libraryDirectory: String) : OkParcelable { 10 | 11 | open class ClassLoader(parent: java.lang.ClassLoader?, dexInfo: DexInfo) 12 | : DexClassLoader( 13 | dexInfo.dexPath, 14 | dexInfo.optimizedDirectory, 15 | dexInfo.libraryDirectory, 16 | parent) 17 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/program/Program.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.program 2 | 3 | import android.content.pm.PackageInfo 4 | import android.content.res.Resources 5 | 6 | interface Program { 7 | val packageName: String 8 | 9 | val packageInfo: PackageInfo 10 | 11 | val packageComponents: AndroidPackageComponents 12 | 13 | val classLoader: ClassLoader 14 | 15 | val resources: Resources 16 | 17 | val dexInfo: DexInfo 18 | 19 | fun preload() 20 | 21 | fun start() 22 | 23 | fun stop() 24 | } 25 | -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/stub/JessieLauncherActivity.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.stub 2 | 3 | import android.app.Activity 4 | import android.os.Bundle 5 | 6 | class JessieLauncherActivity: Activity() { 7 | override fun onCreate(savedInstanceState: Bundle?) { 8 | super.onCreate(savedInstanceState) 9 | finish() 10 | } 11 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/stub/JessieStub.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.stub 2 | 3 | import android.app.Activity 4 | import android.app.ActivityWrapper 5 | import android.app.Service 6 | import android.app.ServiceWrapper 7 | import android.content.ContentProvider 8 | import android.content.ContentProviderWrapper 9 | import cn.jessie.JessieRuntime 10 | 11 | object JessieStub { 12 | private const val STUB_ACTIVITY = "cn.jessie.runtime.app.activity.JessieStubActivity" 13 | private const val STUB_SERVICE = "cn.jessie.runtime.app.service.JessieStubService" 14 | private const val STUB_PROVIDER = "cn.jessie.runtime.app.provider.JessieStubProvider" 15 | 16 | abstract class BaseActivity : ActivityWrapper() { 17 | override val baseActivity by lazy { JessieRuntime.create(STUB_ACTIVITY) } 18 | } 19 | 20 | abstract class BaseService : ServiceWrapper() { 21 | override val baseService by lazy { JessieRuntime.create(STUB_SERVICE) } 22 | } 23 | 24 | abstract class BaseProvider : ContentProviderWrapper() { 25 | override val baseProvider by lazy { JessieRuntime.create(STUB_PROVIDER) } 26 | } 27 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/stub/JessieStubComponents.kt: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * generated by `gradlew genComponents`. DO NOT MODIFY 4 | */ 5 | package cn.jessie.stub 6 | 7 | import cn.jessie.JessieRuntime 8 | 9 | object JessieStubComponents { 10 | const val PROCESS_COUNT = 20 11 | const val SERVICE_COUNT = 10 12 | val ACTIVITY_LAUNCH_MODE_COUNTS = intArrayOf(10, 5, 3, 4) 13 | 14 | fun getActivityClassName(processIndex: Int, launchMode: Int, number: Int): String { 15 | return "cn.jessie.stub.JessieStubActivities\$P${processIndex}LM${launchMode}N$number" 16 | } 17 | 18 | fun getServiceClassName(processIndex: Int, number: Int): String { 19 | return "cn.jessie.stub.JessieStubServices\$P$processIndex" 20 | } 21 | 22 | fun getProviderAuthority(processIndex: Int): String { 23 | return JessieRuntime.context.packageName + ".jc" + processIndex 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/stub/JessieStubProviders.kt: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * generated by `gradlew genComponents`. DO NOT MODIFY 4 | */ 5 | package cn.jessie.stub 6 | 7 | object JessieStubProviders { 8 | class P0 : JessieStub.BaseProvider() 9 | class P1 : JessieStub.BaseProvider() 10 | class P2 : JessieStub.BaseProvider() 11 | class P3 : JessieStub.BaseProvider() 12 | class P4 : JessieStub.BaseProvider() 13 | class P5 : JessieStub.BaseProvider() 14 | class P6 : JessieStub.BaseProvider() 15 | class P7 : JessieStub.BaseProvider() 16 | class P8 : JessieStub.BaseProvider() 17 | class P9 : JessieStub.BaseProvider() 18 | class P10 : JessieStub.BaseProvider() 19 | class P11 : JessieStub.BaseProvider() 20 | class P12 : JessieStub.BaseProvider() 21 | class P13 : JessieStub.BaseProvider() 22 | class P14 : JessieStub.BaseProvider() 23 | class P15 : JessieStub.BaseProvider() 24 | class P16 : JessieStub.BaseProvider() 25 | class P17 : JessieStub.BaseProvider() 26 | class P18 : JessieStub.BaseProvider() 27 | class P19 : JessieStub.BaseProvider() 28 | } -------------------------------------------------------------------------------- /jessie/src/main/java/cn/jessie/stub/JessieStubServices.kt: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * generated by `gradlew genComponents`. DO NOT MODIFY 4 | */ 5 | package cn.jessie.stub 6 | 7 | object JessieStubServices { 8 | class P0 : JessieStub.BaseService() 9 | class P1 : JessieStub.BaseService() 10 | class P2 : JessieStub.BaseService() 11 | class P3 : JessieStub.BaseService() 12 | class P4 : JessieStub.BaseService() 13 | class P5 : JessieStub.BaseService() 14 | class P6 : JessieStub.BaseService() 15 | class P7 : JessieStub.BaseService() 16 | class P8 : JessieStub.BaseService() 17 | class P9 : JessieStub.BaseService() 18 | class P10 : JessieStub.BaseService() 19 | class P11 : JessieStub.BaseService() 20 | class P12 : JessieStub.BaseService() 21 | class P13 : JessieStub.BaseService() 22 | class P14 : JessieStub.BaseService() 23 | class P15 : JessieStub.BaseService() 24 | class P16 : JessieStub.BaseService() 25 | class P17 : JessieStub.BaseService() 26 | class P18 : JessieStub.BaseService() 27 | class P19 : JessieStub.BaseService() 28 | } -------------------------------------------------------------------------------- /jessie/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | jessie 3 | 4 | -------------------------------------------------------------------------------- /jessie/src/test/java/cn/jessie/etc/NumberPoolTest.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.etc 2 | 3 | import org.junit.Assert.* 4 | import org.junit.Test 5 | 6 | class NumberPoolTest { 7 | @Test 8 | fun test() { 9 | val pool = NumberPool(5) 10 | assertEquals(pool.get(), 0) 11 | assertEquals(pool.get(), 1) 12 | assertEquals(pool.get(), 2) 13 | assertEquals(pool.get(), 3) 14 | assertEquals(pool.get(), 4) 15 | assertEquals(pool.get(), -1) 16 | assertEquals(pool.force(), 0) 17 | assertEquals(pool.force(), 1) 18 | assertEquals(pool.force(), 2) 19 | assertEquals(pool.force(), 3) 20 | assertEquals(pool.force(), 4) 21 | } 22 | } -------------------------------------------------------------------------------- /jessie/src/test/java/cn/jessie/etc/ReflectionTarget.java: -------------------------------------------------------------------------------- 1 | package cn.jessie.etc; 2 | 3 | /** 4 | * @author 7hens 5 | */ 6 | @SuppressWarnings("unused") 7 | public class ReflectionTarget { 8 | public static void invoke(int i) { 9 | System.out.println("hello: " + i); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jessie/src/test/java/cn/jessie/etc/ReflectionsTest.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.etc 2 | 3 | import org.junit.Assert.assertTrue 4 | import org.junit.Test 5 | import kotlin.properties.ReadOnlyProperty 6 | import kotlin.reflect.KProperty 7 | 8 | /** 9 | * @author 7hens 10 | */ 11 | class ReflectionsTest { 12 | 13 | @Test 14 | operator fun invoke() { 15 | Reflections.invoke(ReflectionTarget::class.java, "invoke", 1) 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /jessie/src/test/java/cn/jessie/main/ProgramManagerNativeTest.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.main 2 | 3 | import cn.jessie.app.activity.ActivityStubs 4 | import org.junit.Assert.assertEquals 5 | import org.junit.Test 6 | 7 | class ProgramManagerNativeTest { 8 | @Test 9 | fun test() { 10 | val stubId = ActivityStubs.ID.of(12, 1, 56) 11 | println(stubId) 12 | assertEquals(ActivityStubs.ID.processIndex(stubId), 12) 13 | assertEquals(ActivityStubs.ID.launchMode(stubId), 1) 14 | assertEquals(ActivityStubs.ID.number(stubId), 56) 15 | } 16 | } -------------------------------------------------------------------------------- /sample-host/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 29 7 | defaultConfig { 8 | applicationId "cn.jessie.sample.host" 9 | minSdkVersion 15 10 | targetSdkVersion 29 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 14 | ndk { 15 | abiFilters "armeabi", "x86" 16 | } 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | sourceSets { 25 | main.jniLibs.srcDirs = ['libs'] 26 | } 27 | lintOptions { 28 | abortOnError false 29 | } 30 | } 31 | 32 | dependencies { 33 | implementation fileTree(dir: 'libs', include: ['*.jar']) 34 | implementation project(':jessie') 35 | // implementation 'com.github.7hens:jessie:0.2' 36 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 37 | implementation 'androidx.appcompat:appcompat:1.1.0-rc01' 38 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 39 | implementation 'androidx.recyclerview:recyclerview:1.0.0' 40 | implementation "io.reactivex.rxjava2:rxjava:2.2.10" 41 | implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' 42 | implementation 'com.github.tbruyelle:rxpermissions:0.10.2' 43 | implementation 'com.github.7hens:logdog:0.4' 44 | 45 | testImplementation 'junit:junit:4.12' 46 | androidTestImplementation 'androidx.test:runner:1.2.0' 47 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 48 | debugImplementation 'com.amitshekhar.android:debug-db:1.0.4' 49 | } 50 | 51 | def shell = { String cmd -> 52 | println "\$ $cmd" 53 | def process = cmd.execute() 54 | def thread = { runnable -> new Thread(runnable).start() } 55 | thread { process.inputStream.eachLine { println ">> $it" } } 56 | thread { process.errorStream.eachLine { System.err.println ">> $it" } } 57 | process.waitForOrKill(10000) 58 | } 59 | 60 | task installPlugin { 61 | def pluginApkPath = rootProject.file("sample-plugin/build/outputs/apk/debug/sample-plugin-debug.apk") 62 | 63 | group = 'jessie' 64 | dependsOn ":sample-plugin:assembleDebug" 65 | doLast { 66 | shell "adb push $pluginApkPath /sdcard/jc_plugins/cn.jessie.sample.plugin.apk" 67 | shell "adb shell am force-stop cn.jessie.sample.host" 68 | shell "adb shell am start -n cn.jessie.sample.host/.ProgramListActivity" 69 | println '>> ** install success ** <<' 70 | } 71 | } 72 | 73 | afterEvaluate { 74 | tasks.preBuild.dependsOn rootProject.tasks.findByPath(":jessie:upgradeHook") 75 | tasks.preBuild.dependsOn rootProject.tasks.findByPath(":jessie:upgradeRuntime") 76 | } -------------------------------------------------------------------------------- /sample-host/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample-host/src/main/java/cn/jessie/sample/host/HostApp.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.host 2 | 3 | import android.annotation.SuppressLint 4 | import android.app.Activity 5 | import android.app.ActivityManager 6 | import android.app.Application 7 | import android.content.Context 8 | import android.os.Bundle 9 | import cn.jessie.Jessie 10 | import cn.jessie.etc.JCLogger 11 | 12 | class HostApp : Application() { 13 | override fun onCreate() { 14 | super.onCreate() 15 | GlobalBroadcast.initialize(this) 16 | GlobalBroadcast.get().apply { 17 | receive("stop").subscribe { programPackageName -> 18 | if (programPackageName.isNotEmpty()) { 19 | val program = Jessie.getProgram(programPackageName) 20 | ?: throw RuntimeException("program($programPackageName) not exists") 21 | program.stop() 22 | } else { 23 | Jessie.programs.values.forEach { it.stop() } 24 | } 25 | } 26 | receive("start").subscribe { programPackageName -> 27 | val program = Jessie.getProgram(programPackageName) 28 | ?: throw RuntimeException("program($programPackageName) not exists") 29 | program.start() 30 | } 31 | } 32 | } 33 | 34 | @SuppressLint("WrongConstant") 35 | private fun isOnMainProcess(): Boolean { 36 | val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager 37 | val myPid = android.os.Process.myPid() 38 | for (runningAppProcess in activityManager.runningAppProcesses) { 39 | if (runningAppProcess.pid == myPid) { 40 | if (runningAppProcess.processName == packageName) { 41 | return true 42 | } 43 | } 44 | } 45 | return false 46 | } 47 | 48 | private fun manageActivityLifecycleCallbacks() { 49 | registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks { 50 | override fun onActivityPaused(activity: Activity) { 51 | JCLogger.debug("${activity.javaClass.name}#onActivityPaused") 52 | } 53 | 54 | override fun onActivityResumed(activity: Activity) { 55 | JCLogger.debug("${activity.javaClass.name}#onActivityResumed") 56 | } 57 | 58 | override fun onActivityStarted(activity: Activity) { 59 | JCLogger.debug("${activity.javaClass.name}#onActivityStarted") 60 | } 61 | 62 | override fun onActivityDestroyed(activity: Activity) { 63 | JCLogger.debug("${activity.javaClass.name}#onActivityDestroyed") 64 | } 65 | 66 | override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) { 67 | JCLogger.debug("${activity.javaClass.name}#onActivitySaveInstanceState") 68 | } 69 | 70 | override fun onActivityStopped(activity: Activity) { 71 | JCLogger.debug("${activity.javaClass.name}#onActivityStopped") 72 | } 73 | 74 | override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { 75 | JCLogger.debug("${activity.javaClass.name}#onActivityCreated") 76 | } 77 | }) 78 | } 79 | } -------------------------------------------------------------------------------- /sample-host/src/main/java/cn/jessie/sample/host/JavaTest.java: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.host; 2 | 3 | import cn.jessie.Jessie; 4 | 5 | public class JavaTest { 6 | public static void test() { 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /sample-host/src/main/res/drawable-nodpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/drawable-nodpi/ic_launcher.png -------------------------------------------------------------------------------- /sample-host/src/main/res/drawable-nodpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/drawable-nodpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample-host/src/main/res/drawable-nodpi/ic_startup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/drawable-nodpi/ic_startup.png -------------------------------------------------------------------------------- /sample-host/src/main/res/layout/activity_program_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 19 | 20 | 21 | 22 | 32 | 33 | -------------------------------------------------------------------------------- /sample-host/src/main/res/layout/item_program.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 22 | 23 | 32 | 33 | 40 | 41 | 52 | 53 | 61 | -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-hdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-hdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-mdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-mdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /sample-host/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-host/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample-host/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #2196f3 4 | #303F9F 5 | #FF4081 6 | #333333 7 | #DDDDDD 8 | @android:color/darker_gray 9 | #FFFFFF 10 | #CCFF4081 11 | #FFFFFF 12 | 13 | 14 | -------------------------------------------------------------------------------- /sample-host/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Jessie 3 | 请将 APK 放入 /sdcard/jc_plugins/ 目录下后下拉刷新 4 | 刷新 5 | 正在刷新… 6 | 7 | -------------------------------------------------------------------------------- /sample-host/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 12 | 13 | 18 | 19 | 24 | 25 | 34 | 35 | 42 | 43 | 50 | 51 | 59 | 60 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /sample-host/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | -------------------------------------------------------------------------------- /sample-host/src/main/res/xml/network_security_config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /sample-plugin/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 28 7 | defaultConfig { 8 | applicationId "cn.jessie.sample.plugin" 9 | minSdkVersion 16 10 | targetSdkVersion 28 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 14 | ndk { 15 | abiFilters "armeabi" 16 | } 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | sourceSets { 25 | main.jniLibs.srcDirs = ['libs'] 26 | } 27 | lintOptions { 28 | abortOnError false 29 | disable 'HardcodedText' 30 | } 31 | } 32 | 33 | dependencies { 34 | implementation fileTree(dir: 'libs', include: ['*.jar']) 35 | // implementation project(":jessie") 36 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 37 | implementation 'androidx.appcompat:appcompat:1.1.0-rc01' 38 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 39 | implementation 'androidx.recyclerview:recyclerview:1.0.0' 40 | implementation 'com.google.android.material:material:1.1.0-alpha07' 41 | implementation "io.reactivex.rxjava2:rxjava:2.2.10" 42 | implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' 43 | implementation 'com.github.tbruyelle:rxpermissions:0.10.2' 44 | implementation 'com.github.7hens:logdog:0.4' 45 | 46 | testImplementation 'junit:junit:4.12' 47 | androidTestImplementation 'androidx.test:runner:1.2.0' 48 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 49 | } 50 | -------------------------------------------------------------------------------- /sample-plugin/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample-plugin/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 19 | 20 | 23 | 24 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 38 | 41 | 44 | 47 | 50 | 51 | 52 | 53 | 56 | 57 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin 2 | 3 | import android.os.Bundle 4 | import androidx.appcompat.app.AppCompatActivity 5 | import androidx.fragment.app.Fragment 6 | import androidx.fragment.app.FragmentManager 7 | import androidx.fragment.app.FragmentPagerAdapter 8 | import cn.jessie.sample.plugin.activity.ActivityFragment 9 | import cn.jessie.sample.plugin.etc.EtcFragment 10 | import cn.jessie.sample.plugin.provider.ProviderFragment 11 | import cn.jessie.sample.plugin.service.ServiceFragment 12 | import kotlinx.android.synthetic.main.activity_main.* 13 | 14 | class MainActivity : AppCompatActivity() { 15 | private val fragmentAdapter by lazy { MainFragmentAdapter(supportFragmentManager) } 16 | 17 | override fun onCreate(savedInstanceState: Bundle?) { 18 | super.onCreate(savedInstanceState) 19 | setContentView(R.layout.activity_main) 20 | 21 | vPages.adapter = fragmentAdapter 22 | vTabs.setupWithViewPager(vPages) 23 | 24 | // PluginLog.debug(""" 25 | // |host = ${Jessie.host.packageName} 26 | // |current = ${Jessie.current.packageName} 27 | // """.trimMargin()) 28 | } 29 | 30 | private class MainFragmentAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) { 31 | private val pageTitles = arrayOf("Activity", "Service", "Provider", "Etc") 32 | 33 | private val fragClasses = arrayOf( 34 | ActivityFragment::class, 35 | ServiceFragment::class, 36 | ProviderFragment::class, 37 | EtcFragment::class) 38 | 39 | override fun getItem(position: Int): Fragment { 40 | return fragClasses[position].java.newInstance() 41 | } 42 | 43 | override fun getCount(): Int { 44 | return fragClasses.size 45 | } 46 | 47 | override fun getPageTitle(position: Int): CharSequence? { 48 | return pageTitles[position] 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/PluginApp.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin 2 | 3 | import android.annotation.SuppressLint 4 | import android.app.Activity 5 | import android.app.ActivityManager 6 | import android.app.Application 7 | import android.content.Context 8 | import android.os.Bundle 9 | 10 | class PluginApp : Application() { 11 | override fun attachBaseContext(base: Context?) { 12 | super.attachBaseContext(base) 13 | PluginLog.debug("Application.attachBaseContext") 14 | } 15 | 16 | @SuppressLint("WrongConstant") 17 | override fun onCreate() { 18 | super.onCreate() 19 | manageActivityLifecycleCallbacks() 20 | val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager 21 | PluginLog.debug(activityManager.runningAppProcesses) 22 | PluginLog.debug("PluginApp.onCreate") 23 | } 24 | 25 | private fun manageActivityLifecycleCallbacks() { 26 | registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks { 27 | private fun log(activity: Activity, lifecycle: String) { 28 | PluginLog.debug("$lifecycle(${activity.javaClass.name})") 29 | } 30 | 31 | override fun onActivityPaused(activity: Activity) { 32 | log(activity, "onActivityPaused") 33 | } 34 | 35 | override fun onActivityResumed(activity: Activity) { 36 | log(activity, "onActivityResumed") 37 | } 38 | 39 | override fun onActivityStarted(activity: Activity) { 40 | log(activity, "onActivityStarted") 41 | } 42 | 43 | override fun onActivityDestroyed(activity: Activity) { 44 | log(activity, "onActivityDestroyed") 45 | } 46 | 47 | override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) { 48 | log(activity, "onActivitySaveInstanceState") 49 | } 50 | 51 | override fun onActivityStopped(activity: Activity) { 52 | log(activity, "onActivityStopped") 53 | } 54 | 55 | override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { 56 | log(activity, "onActivityCreated") 57 | } 58 | }) 59 | } 60 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/PluginLog.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin 2 | 3 | /** 4 | * @author 7hens 5 | */ 6 | 7 | import cn.thens.logdog.Logdog 8 | 9 | val PluginLog: Logdog = Logdog.get().tag("JC_PLUGIN") 10 | -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/activity/ActivityFragment.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.activity 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import android.view.LayoutInflater 6 | import android.view.View 7 | import android.view.ViewGroup 8 | import android.widget.Toast 9 | import androidx.fragment.app.Fragment 10 | import cn.jessie.sample.plugin.R 11 | import kotlinx.android.synthetic.main.fragment_main_activity.* 12 | 13 | class ActivityFragment : Fragment() { 14 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 15 | return inflater.inflate(R.layout.fragment_main_activity, container, false) 16 | } 17 | 18 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 19 | super.onViewCreated(view, savedInstanceState) 20 | vStartActivity.setOnClickListener { 21 | startActivityForResult(Intent(requireContext(), LaunchModeActivity.Standard::class.java), 0) 22 | } 23 | vStaticFragment.setOnClickListener { 24 | startActivity(Intent(requireContext(), StaticFragmentActivity::class.java)) 25 | } 26 | } 27 | 28 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 29 | super.onActivityResult(requestCode, resultCode, data) 30 | if (data != null) { 31 | val extra = data.getStringExtra("hello") 32 | Toast.makeText(requireContext(), "onActivityResult($extra)", Toast.LENGTH_SHORT).show() 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/activity/LaunchModeActivity.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.activity 2 | 3 | import android.annotation.SuppressLint 4 | import android.app.Activity 5 | import android.content.Context 6 | import android.content.Intent 7 | import android.os.Bundle 8 | import android.view.View 9 | import android.widget.Button 10 | import android.widget.LinearLayout 11 | import android.widget.TextView 12 | 13 | abstract class LaunchModeActivity : LifecycleActivity() { 14 | private val trace by lazy { 15 | (intent.getStringExtra(EXTRA_TRACE) ?: "") + " >> " + javaClass.simpleName 16 | } 17 | 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | setContentView(createContentView(this)) 21 | } 22 | 23 | @SuppressLint("SetTextI18n") 24 | private fun createContentView(context: Context): View { 25 | return LinearLayout(context).apply { 26 | orientation = LinearLayout.VERTICAL 27 | setPadding(32, 32, 32, 32) 28 | 29 | TextView(context).apply { 30 | text = trace 31 | }.let { addView(it) } 32 | 33 | Button(context).apply { 34 | text = "back" 35 | setOnClickListener { 36 | setResult(RESULT_OK, Intent().putExtra("hello", "jessie")) 37 | finish() 38 | } 39 | }.let { addView(it) } 40 | 41 | Button(context).apply { 42 | text = "Standard" 43 | isAllCaps = false 44 | setOnClickListener { start() } 45 | }.let { addView(it) } 46 | 47 | Button(context).apply { 48 | text = "SingleTop" 49 | isAllCaps = false 50 | setOnClickListener { start() } 51 | }.let { addView(it) } 52 | 53 | Button(context).apply { 54 | text = "SingleTask" 55 | isAllCaps = false 56 | setOnClickListener { start() } 57 | }.let { addView(it) } 58 | 59 | Button(context).apply { 60 | text = "SingleInstance" 61 | isAllCaps = false 62 | setOnClickListener { start() } 63 | }.let { addView(it) } 64 | } 65 | } 66 | 67 | private inline fun start() { 68 | startActivity(Intent(this, A::class.java) 69 | .putExtra(EXTRA_TRACE, trace)) 70 | } 71 | 72 | companion object { 73 | private const val EXTRA_TRACE = "TRACE" 74 | } 75 | 76 | class Standard : LaunchModeActivity() 77 | class SingleTop : LaunchModeActivity() 78 | class SingleTask : LaunchModeActivity() 79 | class SingleInstance : LaunchModeActivity() 80 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/activity/LifecycleActivity.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.activity 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import androidx.appcompat.app.AppCompatActivity 6 | import cn.jessie.sample.plugin.PluginLog 7 | 8 | abstract class LifecycleActivity: AppCompatActivity() { 9 | 10 | override fun onCreate(savedInstanceState: Bundle?) { 11 | super.onCreate(savedInstanceState) 12 | PluginLog.debug(javaClass.simpleName + ".onCreate()") 13 | } 14 | 15 | override fun onPostCreate(savedInstanceState: Bundle?) { 16 | super.onPostCreate(savedInstanceState) 17 | PluginLog.debug(javaClass.simpleName + ".onPostCreate()") 18 | } 19 | 20 | override fun onStart() { 21 | super.onStart() 22 | PluginLog.debug(javaClass.simpleName + ".onStart()") 23 | } 24 | 25 | override fun onResume() { 26 | super.onResume() 27 | PluginLog.debug(javaClass.simpleName + ".onResume()") 28 | } 29 | 30 | override fun onPostResume() { 31 | super.onPostResume() 32 | PluginLog.debug(javaClass.simpleName + ".onPostResume()") 33 | } 34 | 35 | override fun onPause() { 36 | super.onPause() 37 | PluginLog.debug(javaClass.simpleName + ".onPause()") 38 | } 39 | 40 | override fun onStop() { 41 | super.onStop() 42 | PluginLog.debug(javaClass.simpleName + ".onStop()") 43 | } 44 | 45 | override fun onDestroy() { 46 | super.onDestroy() 47 | PluginLog.debug(javaClass.simpleName + ".onDestroy()") 48 | } 49 | 50 | override fun onRestart() { 51 | super.onRestart() 52 | PluginLog.debug(javaClass.simpleName + ".onRestart()") 53 | } 54 | 55 | override fun onSaveInstanceState(outState: Bundle) { 56 | super.onSaveInstanceState(outState) 57 | PluginLog.debug(javaClass.simpleName + ".onSaveInstanceState()") 58 | } 59 | 60 | override fun onRestoreInstanceState(savedInstanceState: Bundle?) { 61 | super.onRestoreInstanceState(savedInstanceState) 62 | PluginLog.debug(javaClass.simpleName + ".onRestoreInstanceState()") 63 | } 64 | 65 | override fun onNewIntent(intent: Intent?) { 66 | super.onNewIntent(intent) 67 | PluginLog.debug(javaClass.simpleName + ".onNewIntent()") 68 | } 69 | 70 | override fun onBackPressed() { 71 | super.onBackPressed() 72 | PluginLog.debug(javaClass.simpleName + ".onBackPressed()") 73 | } 74 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/activity/SplashActivity.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.activity 2 | 3 | import android.content.Intent 4 | import androidx.appcompat.app.AppCompatActivity 5 | import cn.jessie.sample.plugin.MainActivity 6 | import io.reactivex.Observable 7 | import io.reactivex.android.schedulers.AndroidSchedulers 8 | import java.util.concurrent.TimeUnit 9 | 10 | class SplashActivity : AppCompatActivity() { 11 | override fun onResume() { 12 | super.onResume() 13 | Observable.timer(2, TimeUnit.SECONDS) 14 | .observeOn(AndroidSchedulers.mainThread()) 15 | .doFinally { startActivity(Intent(this, MainActivity::class.java)) } 16 | .subscribe() 17 | } 18 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/activity/StaticFragmentActivity.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.activity 2 | 3 | import android.os.Bundle 4 | import androidx.appcompat.app.AppCompatActivity 5 | import cn.jessie.sample.plugin.R 6 | import kotlinx.android.synthetic.main.activity_static_fragment.* 7 | 8 | class StaticFragmentActivity : AppCompatActivity() { 9 | override fun onCreate(savedInstanceState: Bundle?) { 10 | super.onCreate(savedInstanceState) 11 | setContentView(R.layout.activity_static_fragment) 12 | vGoBack.setOnClickListener { finish() } 13 | } 14 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/etc/AndroidXNotification.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.etc 2 | 3 | import android.app.Notification 4 | import android.app.NotificationChannel 5 | import android.app.NotificationManager 6 | import android.app.PendingIntent 7 | import android.content.Context 8 | import android.content.Intent 9 | import android.graphics.BitmapFactory 10 | import android.graphics.Color 11 | import android.os.Build 12 | import androidx.core.app.NotificationCompat 13 | import cn.jessie.sample.plugin.R 14 | import cn.jessie.sample.plugin.activity.StaticFragmentActivity 15 | 16 | class AndroidXNotification(private val context: Context) { 17 | fun show() { 18 | val intent = Intent(context, StaticFragmentActivity::class.java) 19 | val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0) 20 | val notification = getNotificationBuilder() 21 | .setContentTitle("contentTitle") 22 | .setContentText("contentText") 23 | .setSubText("subText") 24 | .setTicker("ticker") 25 | .setSmallIcon(R.drawable.ic_info) 26 | .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_launcher_round)) 27 | .setAutoCancel(true) 28 | .setContentIntent(pendingIntent) 29 | .build() 30 | getNotificationManager().notify(NOTIFICATION_ID, notification) 31 | } 32 | 33 | fun dismiss() { 34 | getNotificationManager().cancel(NOTIFICATION_ID) 35 | } 36 | 37 | private fun getNotificationBuilder(): NotificationCompat.Builder { 38 | return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 39 | val channel = NotificationChannel("channelId", "Primary Channel", NotificationManager.IMPORTANCE_DEFAULT) 40 | channel.lightColor = Color.GREEN 41 | channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE 42 | getNotificationManager().createNotificationChannel(channel) 43 | NotificationCompat.Builder(context, "channelId") 44 | } else { 45 | NotificationCompat.Builder(context) 46 | } 47 | } 48 | 49 | private fun getNotificationManager(): NotificationManager { 50 | return context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 51 | } 52 | 53 | companion object { 54 | private const val NOTIFICATION_ID = 1 55 | } 56 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/etc/EtcFragment.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.etc 2 | 3 | import android.annotation.SuppressLint 4 | import android.app.AlertDialog 5 | import android.app.Notification 6 | import android.app.NotificationChannel 7 | import android.app.NotificationManager 8 | import android.content.Context 9 | import android.content.Intent 10 | import android.graphics.Color 11 | import android.os.Build 12 | import android.os.Bundle 13 | import android.provider.Settings 14 | import android.view.LayoutInflater 15 | import android.view.View 16 | import android.view.ViewGroup 17 | import android.widget.Toast 18 | import androidx.core.app.NotificationCompat 19 | import androidx.fragment.app.Fragment 20 | import cn.jessie.sample.plugin.R 21 | import kotlinx.android.synthetic.main.fragment_main_etc.* 22 | 23 | class EtcFragment : Fragment() { 24 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 25 | return inflater.inflate(R.layout.fragment_main_etc, container, false) 26 | } 27 | 28 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 29 | super.onViewCreated(view, savedInstanceState) 30 | 31 | val androidXNotification = AndroidXNotification(requireContext()) 32 | vShowNotification.setOnClickListener { androidXNotification.show() } 33 | vHideNotification.setOnClickListener { androidXNotification.dismiss() } 34 | 35 | vShowDialog.setOnClickListener { 36 | AlertDialog.Builder(requireActivity()) 37 | .setIcon(R.drawable.ic_launcher_round) 38 | .setTitle("Jessie") 39 | .setMessage("Hello, Jessie") 40 | .setNegativeButton("取消") { dialog, _ -> dialog.dismiss() } 41 | .setPositiveButton("确定") { dialog, _ -> 42 | dialog.dismiss() 43 | Toast.makeText(requireActivity(), "OK", Toast.LENGTH_SHORT).show() 44 | } 45 | .show() 46 | } 47 | 48 | vShowToast.setOnClickListener { 49 | Toast.makeText(requireActivity(), "Hello, Jessie", Toast.LENGTH_SHORT).show() 50 | } 51 | 52 | vShowFloatingView.setOnClickListener { 53 | whenOverlayPermitted { 54 | FloatingViewManager.get(requireActivity()).show() 55 | } 56 | // RxPermissions(requireActivity()) 57 | // .request(android.Manifest.permission.SYSTEM_ALERT_WINDOW) 58 | // .doOnNext { isGranted -> 59 | // if (isGranted) { 60 | // FloatingViewManager.get(requireActivity()).show() 61 | // } else { 62 | // Toast.makeText(requireActivity(), "请先授予悬浮窗权限", Toast.LENGTH_SHORT).show() 63 | // } 64 | // } 65 | // .subscribe() 66 | } 67 | } 68 | 69 | private fun whenOverlayPermitted(fn: () -> Unit) { 70 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 71 | if (Settings.canDrawOverlays(requireContext())) { 72 | fn.invoke() 73 | } else { 74 | val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION) 75 | Toast.makeText(requireContext(), "请先授予悬浮窗权限", Toast.LENGTH_SHORT).show() 76 | startActivity(intent) 77 | } 78 | } else { 79 | fn.invoke() 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/etc/SupportNotification.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.etc 2 | 3 | import android.app.Notification 4 | import android.app.NotificationChannel 5 | import android.app.NotificationManager 6 | import android.app.PendingIntent 7 | import android.content.Context 8 | import android.content.Intent 9 | import android.graphics.BitmapFactory 10 | import android.graphics.Color 11 | import android.os.Build 12 | import androidx.core.app.NotificationCompat 13 | import cn.jessie.sample.plugin.R 14 | import cn.jessie.sample.plugin.activity.StaticFragmentActivity 15 | 16 | class SupportNotification(private val context: Context) { 17 | fun show() { 18 | val intent = Intent(context, StaticFragmentActivity::class.java) 19 | val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0) 20 | val notification = getNotificationBuilder() 21 | .setContentTitle("contentTitle") 22 | .setContentText("contentText") 23 | .setSubText("subText") 24 | .setTicker("ticker") 25 | .setSmallIcon(R.drawable.ic_info) 26 | .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_launcher_round)) 27 | .setAutoCancel(true) 28 | .setContentIntent(pendingIntent) 29 | .build() 30 | getNotificationManager().notify(NOTIFICATION_ID, notification) 31 | } 32 | 33 | fun dismiss() { 34 | getNotificationManager().cancel(NOTIFICATION_ID) 35 | } 36 | 37 | private fun getNotificationBuilder(): NotificationCompat.Builder { 38 | return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 39 | val channel = NotificationChannel("channelId", "Primary Channel", NotificationManager.IMPORTANCE_DEFAULT) 40 | channel.lightColor = Color.GREEN 41 | channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE 42 | getNotificationManager().createNotificationChannel(channel) 43 | NotificationCompat.Builder(context, "channelId") 44 | } else { 45 | NotificationCompat.Builder(context) 46 | } 47 | } 48 | 49 | private fun getNotificationManager(): NotificationManager { 50 | return context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 51 | } 52 | 53 | companion object { 54 | private const val NOTIFICATION_ID = 1 55 | } 56 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/provider/MyProvider.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.provider 2 | 3 | import android.content.ContentProvider 4 | import android.content.ContentValues 5 | import android.database.Cursor 6 | import android.net.Uri 7 | import android.widget.Toast 8 | import cn.jessie.sample.plugin.PluginLog 9 | 10 | 11 | class MyProvider : ContentProvider() { 12 | override fun onCreate(): Boolean { 13 | showToast("Provider.onCreate") 14 | PluginLog.debug("Provider.onCreate") 15 | return true 16 | } 17 | 18 | override fun insert(uri: Uri, values: ContentValues?): Uri? { 19 | showToast("Provider.insert") 20 | return null 21 | } 22 | 23 | override fun query(uri: Uri, projection: Array?, selection: String?, selectionArgs: Array?, sortOrder: String?): Cursor? { 24 | showToast("Provider.query") 25 | return null 26 | } 27 | 28 | override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array?): Int { 29 | showToast("Provider.update") 30 | return -1 31 | } 32 | 33 | override fun delete(uri: Uri, selection: String?, selectionArgs: Array?): Int { 34 | showToast("Provider.delete") 35 | return -1 36 | } 37 | 38 | override fun getType(uri: Uri): String? { 39 | showToast("Provider.onCreate") 40 | return null 41 | } 42 | 43 | private fun showToast(message: String) { 44 | Toast.makeText(context, message, Toast.LENGTH_SHORT).show() 45 | } 46 | 47 | companion object { 48 | const val AUTHORITY = "jessie.sample.plugin.provider" 49 | } 50 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/provider/ProviderFragment.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.provider 2 | 3 | import android.content.ContentValues 4 | import android.net.Uri 5 | import android.os.Bundle 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import androidx.fragment.app.Fragment 10 | import cn.jessie.sample.plugin.R 11 | import kotlinx.android.synthetic.main.fragment_main_provider.* 12 | 13 | class ProviderFragment : Fragment() { 14 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 15 | return inflater.inflate(R.layout.fragment_main_provider, container, false) 16 | } 17 | 18 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 19 | super.onViewCreated(view, savedInstanceState) 20 | vInsert.setOnClickListener { contentResolver.insert(uri, ContentValues()) } 21 | vUpdate.setOnClickListener { contentResolver.update(uri, ContentValues(), null, null) } 22 | vDelete.setOnClickListener { contentResolver.delete(uri, null, null) } 23 | vQuery.setOnClickListener { contentResolver.query(uri, null, null, null, null)?.close() } 24 | } 25 | 26 | private val uri by lazy { Uri.parse("content://${MyProvider.AUTHORITY}") } 27 | 28 | private val contentResolver by lazy { requireContext().contentResolver } 29 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/service/MyService.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.service 2 | 3 | import android.app.Service 4 | import android.content.Intent 5 | import android.os.IBinder 6 | import android.widget.Toast 7 | import cn.jessie.sample.plugin.PluginLog 8 | 9 | abstract class MyService : Service() { 10 | override fun onCreate() { 11 | super.onCreate() 12 | print("Service.onCreate") 13 | } 14 | 15 | override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { 16 | print("Service.onStartCommand") 17 | return super.onStartCommand(intent, flags, startId) 18 | } 19 | 20 | override fun onDestroy() { 21 | print("Service.onDestroy") 22 | super.onDestroy() 23 | } 24 | 25 | override fun onBind(intent: Intent?): IBinder? { 26 | print("Service.onBind") 27 | return null 28 | } 29 | 30 | override fun onUnbind(intent: Intent?): Boolean { 31 | print("Service.onUnbind") 32 | return true 33 | } 34 | 35 | private fun print(text: String) { 36 | Toast.makeText(this, text, Toast.LENGTH_SHORT).show() 37 | PluginLog.debug(text) 38 | } 39 | 40 | class Local : MyService() 41 | 42 | class Remote : MyService() 43 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/java/cn/jessie/sample/plugin/service/ServiceFragment.kt: -------------------------------------------------------------------------------- 1 | package cn.jessie.sample.plugin.service 2 | 3 | import android.content.ComponentName 4 | import android.content.Intent 5 | import android.content.ServiceConnection 6 | import android.os.Bundle 7 | import android.os.IBinder 8 | import android.view.LayoutInflater 9 | import android.view.View 10 | import android.view.ViewGroup 11 | import androidx.fragment.app.Fragment 12 | import cn.jessie.sample.plugin.PluginLog 13 | import cn.jessie.sample.plugin.R 14 | import kotlinx.android.synthetic.main.fragment_main_service.* 15 | 16 | class ServiceFragment : Fragment() { 17 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 18 | return inflater.inflate(R.layout.fragment_main_service, container, false) 19 | } 20 | 21 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 22 | super.onViewCreated(view, savedInstanceState) 23 | vStartLocalService.setOnClickListener { start(localIntent()) } 24 | vStopLocalService.setOnClickListener { stop(localIntent()) } 25 | vBindLocalService.setOnClickListener { bind(localIntent()) } 26 | vUnbindLocalService.setOnClickListener { unbind(localIntent()) } 27 | vStartRemoteService.setOnClickListener { start(removeIntent()) } 28 | vStopRemoteService.setOnClickListener { stop(removeIntent()) } 29 | vBindRemoteService.setOnClickListener { bind(removeIntent()) } 30 | vUnbindRemoteService.setOnClickListener { unbind(removeIntent()) } 31 | } 32 | 33 | private val connection by lazy { 34 | object : ServiceConnection { 35 | override fun onServiceDisconnected(name: ComponentName?) { 36 | PluginLog.debug("onServiceDisconnected") 37 | } 38 | 39 | override fun onServiceConnected(name: ComponentName?, service: IBinder?) { 40 | PluginLog.debug("onServiceConnected") 41 | } 42 | } 43 | } 44 | 45 | private fun start(intent: Intent) { 46 | requireContext().startService(intent) 47 | } 48 | 49 | private fun stop(intent: Intent) { 50 | requireContext().stopService(intent) 51 | } 52 | 53 | private fun bind(intent: Intent) { 54 | requireContext().bindService(intent, connection, 0) 55 | } 56 | 57 | private fun unbind(intent: Intent) { 58 | requireContext().unbindService(connection) 59 | } 60 | 61 | private fun localIntent(): Intent { 62 | return Intent(requireContext(), MyService.Local::class.java) 63 | } 64 | 65 | private fun removeIntent(): Intent { 66 | return Intent(requireContext(), MyService.Remote::class.java) 67 | } 68 | } -------------------------------------------------------------------------------- /sample-plugin/src/main/res/drawable-nodpi/ic_info.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-plugin/src/main/res/drawable-nodpi/ic_info.png -------------------------------------------------------------------------------- /sample-plugin/src/main/res/drawable-nodpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-plugin/src/main/res/drawable-nodpi/ic_launcher.png -------------------------------------------------------------------------------- /sample-plugin/src/main/res/drawable-nodpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-plugin/src/main/res/drawable-nodpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample-plugin/src/main/res/drawable-nodpi/ic_right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-plugin/src/main/res/drawable-nodpi/ic_right.png -------------------------------------------------------------------------------- /sample-plugin/src/main/res/drawable-nodpi/ic_splash_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 12 | 13 | -------------------------------------------------------------------------------- /sample-plugin/src/main/res/drawable-nodpi/ic_startup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7hens/jessie/c650b6abb29e8324f52d94f9142834db8d7b01e5/sample-plugin/src/main/res/drawable-nodpi/ic_startup.png -------------------------------------------------------------------------------- /sample-plugin/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 16 | 17 | 25 | 26 | 33 | 34 | -------------------------------------------------------------------------------- /sample-plugin/src/main/res/layout/activity_static_fragment.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 |