天天看点

android-code-handler

来自于vlc 0.9.9版本

让handler持有 activity的弱引用,可以及时释放activity。

public abstract class WeakHandler<T> extends Handler {
    private WeakReference<T> mOwner;

    public WeakHandler(T owner) {
        mOwner = new WeakReference<T>(owner);
    }

    public T getOwner() {
        return mOwner.get();
    }
}      

使用

/**
   * Handle libvlc asynchronous events
   */
  private final Handler eventHandler = new VideoPlayerEventHandler(this);

  private static class VideoPlayerEventHandler extends
      WeakHandler<VideoPlayerActivity> {
    public VideoPlayerEventHandler(VideoPlayerActivity owner) {
      super(owner);
    }

    @Override
    public void handleMessage(Message msg) {
      VideoPlayerActivity activity = getOwner();
      if (activity == null)
        return;
      // Do not handle events if we are leaving the VideoPlayerActivity
      if (activity.mSwitchingView)
        return;

      switch (msg.getData().getInt("event")) {
      case EventHandler.MediaParsedChanged:
        Log.i(TAG, "MediaParsedChanged");
        activity.updateNavStatus();
        if (!activity.mHasMenu
            && activity.mLibVLC.getVideoTracksCount() < 1) {
          Log.i(TAG, "No video track, open in audio mode");
          activity.switchToAudioMode();
        }
        break;
      case EventHandler.MediaPlayerPlaying:
        Log.i(TAG, "MediaPlayerPlaying");
        activity.stopLoadingAnimation();
        activity.showOverlay();
        /**
         * FIXME: update the track list when it changes during the
         * playback. (#7540)
         */
        activity.setESTrackLists(true);
        activity.setESTracks();
        activity.changeAudioFocus(true);
        activity.updateNavStatus();
        break;
      case EventHandler.MediaPlayerPaused:
        Log.i(TAG, "MediaPlayerPaused");
        break;
      case EventHandler.MediaPlayerStopped:
        Log.i(TAG, "MediaPlayerStopped");
        activity.changeAudioFocus(false);
        break;
      case EventHandler.MediaPlayerEndReached:
        Log.i(TAG, "MediaPlayerEndReached");
        activity.changeAudioFocus(false);
        activity.endReached();
        break;
      case EventHandler.MediaPlayerVout:
        activity.updateNavStatus();
        if (!activity.mHasMenu)
          activity.handleVout(msg);
        break;
      case EventHandler.MediaPlayerPositionChanged:
        if (!activity.mCanSeek)
          activity.mCanSeek = true;
        // don't spam the logs
        break;
      case EventHandler.MediaPlayerEncounteredError:
        Log.i(TAG, "MediaPlayerEncounteredError");
        activity.encounteredError();
        break;
      case EventHandler.HardwareAccelerationError:
        Log.i(TAG, "HardwareAccelerationError");
        activity.handleHardwareAccelerationError();
        break;
      case EventHandler.MediaPlayerTimeChanged:
        // avoid useless error logs
        break;
      default:
        Log.e(TAG, String.format("Event not handled (0x%x)", msg
            .getData().getInt("event")));
        break;
      }
      activity.updateOverlayPausePlay();
    }
  };