Exemplo n.º 1
0
  public void onDestroy() {

    // it is too late to call graphics.destroy - it needs live gl GLThread and gl context, otherwise
    // it will cause of deadlock
    // if (graphics != null) {
    //	graphics.clearManagedCaches();
    //	graphics.destroy();
    // }

    // so we do what we can..
    if (graphics != null) {
      // not necessary - already called in AndroidLiveWallpaperService.onDeepPauseApplication
      // app.graphics.clearManagedCaches();

      // kill the GLThread managed by GLSurfaceView (only for GLSurfaceView because
      // GLSurffaceViewCupcake stops thread in onPause events - which is not as easy and safe for
      // GLSurfaceView)
      if (graphics.view != null && (graphics.view instanceof GLSurfaceView)) {
        GLSurfaceView glSurfaceView = (GLSurfaceView) graphics.view;
        try {
          Method method = null;
          for (Method m : glSurfaceView.getClass().getMethods()) {
            if (m.getName()
                .equals("onDestroy")) // implemented in AndroidGraphicsLiveWallpaper, redirects to
            // onDetachedFromWindow - which stops GLThread by calling
            // mGLThread.requestExitAndWait()
            {
              method = m;
              break;
            }
          }

          if (method != null) {
            method.invoke(glSurfaceView);
            if (AndroidLiveWallpaperService.DEBUG)
              Log.d(
                  AndroidLiveWallpaperService.TAG,
                  " > AndroidLiveWallpaper - onDestroy() stopped GLThread managed by GLSurfaceView");
          } else throw new Exception("method not found!");
        } catch (Throwable t) {
          // error while scheduling exit of GLThread, GLThread will remain live and wallpaper
          // service wouldn't be able to shutdown completely
          Log.e(
              AndroidLiveWallpaperService.TAG,
              "failed to destroy GLSurfaceView's thread! GLSurfaceView.onDetachedFromWindow impl changed since API lvl 16!");
          t.printStackTrace();
        }
      }
    }

    if (audio != null) {
      // dispose audio and free native resources, mandatory since graphics.pause is never called in
      // live wallpaper
      audio.dispose();
    }
  }