From 48c87e35efea1317e9bdd7dd543bc714b189531a Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Tue, 31 Mar 2020 09:43:21 +0800 Subject: [PATCH] =?UTF-8?q?Create=20Framework=20=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E5=B7=A7=E5=A6=99=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Framework 设计巧妙点.md | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 blogs/Android/Framework/Interview/综合性面试题/Framework 设计巧妙点.md diff --git a/blogs/Android/Framework/Interview/综合性面试题/Framework 设计巧妙点.md b/blogs/Android/Framework/Interview/综合性面试题/Framework 设计巧妙点.md new file mode 100644 index 0000000..00f327d --- /dev/null +++ b/blogs/Android/Framework/Interview/综合性面试题/Framework 设计巧妙点.md @@ -0,0 +1,92 @@ +--- +Framework 设计巧妙点 +--- + +1. 对 Framework 的涉猎有一定的广度和深度 +2. 对系统设计有自己的总结和思考 + +#### 例子 + +1. Binder 调用,模糊进程边界 +2. Bitmap 大图传输,高性能 +3. Zygote 创建进程,资源共享 +4. Intent 解耦,模糊进程 + +Binder 调用: + +1. 请求转发 +2. Binder 对象的传递 + +Bitmap 大图传输: + +```c++ +static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject, ...){ + int fd = androidBitmap->getAshmemFd(); + if(fd>=0&&!isMutable&&p->allowFds()){ + p->writeDupImmutableBlobFileDescriptor(fd); + return JNI_TRUE; + } +} +static jobject Bitmap_createFromParcel(JNIEnv* env, jobject parcel){ + if(blob.fd()>=0 && (blob.isMutable()||!isMutable) && (size>=ASHMEM_BITMAP_MIN_SIZE)){ + nativeBitmap = GraphicsJNI::mapAshmemPixelRef(env, bitmap.get(), ctable, dupFd, const_cast(blob.data()), !isMutable); + } +} +``` + +Zygote 创建进程: + +```c++ +int main(int argc, char* const argv[]){ + runtime.start("com.android.internal.os.ZygoteInit", args, zygote); +} +// ZygoteInit#main +public static void main(String argv[]){ + preload(); + startSystemServer(abList, socketName); + runSelectLoop(abiList); +} +``` + +Intent 解耦: + +```java +Intent registerReceiver(..., IIntentReceiver receiver, IntentFilter filter, ...){ + ReceiverList rl = mRegistedReceivers.get(receiver.asBinder()); + if(rl == null){ + rl = new ReceiverList(this, ..., yserId, receiver); + rl.app.receivers.add(rl); + mRegisteredReceivers.put(receiver.asBinder(), rl); + } + BroadcastFilter bf = new BroadcastFilter(filter, rl, callerOackage, ...); + rl.add(bf); + mReceiverResolver.addFilter(bf); +} +``` + +```java +int broadcastIntentLocked(ProcessRecord callerApp, ...){ + List receivers = null; + List registeredReceivers = null; + registeredReceivers = mReceiverResolver.queryIntent(intent, ...); + + int NR = registeredReceivers.size(); + final BroadcastQueue queue = broadcastQueueForIntent(intent); + BroadcastRecord r = new BroadcastRecord(queue, intent, ...); + queue.enqueueParallelBroadcastLocked(r); + queue.scheduleBroadcastLocked(); + return ActivityManager.BROADCAST_SUCCESS; +} +``` + +```java +while(mParallelBroadcasts.size()>0){ + r = mParallelBroadcasts.remove(0); + final int N = r.receivers.size(); + for(int i=0;i