Android Application Errors

(未完待续)

在Android中,对于App error有一个集中管理类:com.android.server.am.AppErrors

AppErrors.java

 // 处理ANR
 void handleShowAnrUi(Message msg)
 // 处理Crash
 void handleShowAppErrorUi(Message msg)
 boolean handleAppCrashLocked(ProcessRecord app, String reason, String shortMsg, String longMsg, String stackTrace, AppErrorDialog.Data data)

ANR调用顺序

InputManager.cpp

  status_t InputManager::start() {
         status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY);
         // .......
  }

InputDispatcher.cpp

  bool InputDispatcherThread::threadLoop() {
      mDispatcher->dispatchOnce();
      return true;
  }

InputDispatcher.cpp

  void InputDispatcher::dispatchOnce() {
         // .......
         dispatchOnceInnerLocked(&nextWakeupTime);
         // .......
  }

InputDispatcher.cpp

  void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) {
         // .......
         done = dispatchMotionLocked(currentTime, typedEntry,
            &dropReason, nextWakeupTime);
         // .......
  }

InputDispatcher.cpp

  bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, 
                         DropReason* dropReason, nsecs_t* nextWakeupTime) {
         // .......
         if (isPointerEvent) {
               // Pointer event.  (eg. touchscreen)
               injectionResult = findTouchedWindowTargetsLocked(currentTime, entry, inputTargets, 
                                 nextWakeupTime, &conflictingPointerActions);
         } else {
               // Non touch event.  (eg. trackball)
               injectionResult = findFocusedWindowTargetsLocked(currentTime, entry, inputTargets, 
                                 nextWakeupTime);
         }
         // .......
  }

InputDispatcher.cpp

  int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
                     Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) {
         // .......
         injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
                mFocusedApplicationHandle, NULL, nextWakeupTime,
                "Waiting because no window has focus but there is a "
                "focused application that may eventually add a window "
                "when it finishes starting up.");
         // .......
         injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
            mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime, reason.string());
         // ......
  }

  int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, 
                const MotionEntry* entry, Vector<InputTarget>& inputTargets, 
                nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) {
        // .......
        injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
                    NULL, touchedWindow.windowHandle, nextWakeupTime, reason.string());
        // .......
  }

InputDispatcher.cpp

  int32_t InputDispatcher::handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
                                const sp<InputApplicationHandle>& applicationHandle,
                                const sp<InputWindowHandle>& windowHandle,
                                nsecs_t* nextWakeupTime, const char* reason) {
        // ......
        onANRLocked(currentTime, applicationHandle, windowHandle, entry->eventTime, mInputTargetWaitStartTime, reason);
        // ......
  }

InputDispatcher.cpp

  void InputDispatcher::onANRLocked(nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
                const sp<InputWindowHandle>& windowHandle, nsecs_t eventTime, 
                nsecs_t waitStartTime, const char* reason) {
        // ......
        CommandEntry* commandEntry = postCommandLocked(& InputDispatcher::doNotifyANRLockedInterruptible);
        // ......
  }

InputDispatcher.cpp

  void InputDispatcher::doNotifyANRLockedInterruptible(CommandEntry* commandEntry) {
        // ......
        nsecs_t newTimeout = mPolicy->notifyANR(commandEntry->inputApplicationHandle,
                       commandEntry->inputWindowHandle, commandEntry->reason);
        // ......
  }

com_android_server_input_InputManagerService.cpp

  nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
    const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
        // ......
        jlong newTimeout = env->CallLongMethod(mServiceObj,
            gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
            reasonObj);
        // ......
  }

InputManagerService.java

  private long notifyANR(InputApplicationHandle inputApplicationHandle,
        InputWindowHandle inputWindowHandle, String reason) {
        return mWindowManagerCallbacks.notifyANR(
            inputApplicationHandle, inputWindowHandle, reason);
  }

InputMonitor.java

  @Override
  public long notifyANR(InputApplicationHandle inputApplicationHandle,
        InputWindowHandle inputWindowHandle, String reason) {
        // ......
        long timeout = ActivityManagerNative.getDefault().inputDispatchingTimedOut(
                    windowState.mSession.mPid, aboveSystem, reason);
        // ......
  }

ActivityManagerService.java

  @Override
  public long inputDispatchingTimedOut(int pid, final boolean aboveSystem, String reason) {
        // ......
        if (!inputDispatchingTimedOut(proc, null, null, aboveSystem, reason)) {
              return -1;
        }
        // ......
  }

ActivityManagerService.java

  public boolean inputDispatchingTimedOut(final ProcessRecord proc,
        final ActivityRecord activity, final ActivityRecord parent,
        final boolean aboveSystem, String reason) {
        // ......
        mAppErrors.appNotResponding(proc, activity, parent, aboveSystem, annotation);
        // ......
  }

AppErrors.java

  final void appNotResponding(ProcessRecord app, ActivityRecord activity, 
        ActivityRecord parent, boolean aboveSystem, final String annotation) {
        // ......
        msg.what = ActivityManagerService.SHOW_NOT_RESPONDING_UI_MSG;
        msg.obj = map;
        msg.arg1 = aboveSystem ? 1 : 0;
        // ......
        mService.mUiHandler.sendMessage(msg);
  }

ActivityManagerService$UiHandler.java

  @Override
  public void handleMessage(Message msg) {
        switch (msg.what) {
        // .......
        case SHOW_NOT_RESPONDING_UI_MSG: {
            mAppErrors.handleShowAnrUi(msg);
            ensureBootCompleted();
        } break;
        // .......
        }
  }

AppErrors.java

  void handleShowAnrUi(Message msg) {
        // .......
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,302评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,556评论 2 45
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,933评论 18 139
  • 这是风靡全球的一系列影片,我居然一部都没看,之前也没有一点想去看的意思。因为周末去看了《神奇动物在哪里》,这部...
    笨小孩_1981阅读 2,230评论 1 3
  • 2016.10.27发现一棵叶插小苗有根粉蚧壳虫,马上连同盆的另一棵小苗浸水清理,然后栽到另外一个盆里。原盆土扔掉...
    猫与多肉阅读 305评论 0 0