android.media.session.MediaSessionManager Java Examples

The following examples show how to use android.media.session.MediaSessionManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MediaControlLoader.java    From Easer with GNU General Public License v3.0 8 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private boolean handleOnApi21(@ValidData @NonNull MediaControlOperationData data) {
    int keyCode = toKeyCode(data.choice);
    KeyEvent keyEvent_down = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
    KeyEvent keyEvent_up = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
    ComponentName myNotificationListenerComponent = new ComponentName(context, MediaControlHelperNotificationListenerService.class);
    MediaSessionManager mediaSessionManager = ((MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE));
    if (mediaSessionManager == null) {
        Logger.e("MediaSessionManager is null.");
        return false;
    }
    List<MediaController> activeSessions = mediaSessionManager.getActiveSessions(myNotificationListenerComponent);
    if (activeSessions.size() > 0) {
        MediaController mediaController = activeSessions.get(0);
        mediaController.dispatchMediaButtonEvent(keyEvent_down);
        mediaController.dispatchMediaButtonEvent(keyEvent_up);
    }
    return true;
}
 
Example #2
Source File: LyricsViewFragment.java    From QuickLyric with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onDestroy() {
    unregisterUpdateBroadcastReceiver();
    threadCancelled = true;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && networkCallback != null)
        try {
            ((ConnectivityManager) getActivity().getApplicationContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE)).unregisterNetworkCallback(networkCallback);
        } catch (IllegalArgumentException ignored) {
        }
    if (Build.VERSION.SDK_INT >= 21 && sessionListener != null) {
        ((MediaSessionManager) getActivity().getSystemService(Context.MEDIA_SESSION_SERVICE))
                .removeOnActiveSessionsChangedListener((OnActiveSessionsChangedListener) sessionListener);
    }
    super.onDestroy();
    RefWatcher refWatcher = App.getRefWatcher(getActivity());
    refWatcher.watch(this);
}
 
Example #3
Source File: NotificationListener4.java    From PodEmu with GNU General Public License v3.0 6 votes vote down vote up
@Override public void onCreate()
    {


        PodEmuLog.debug(TAG + ": listener class created");

        mediaSessionManager = (MediaSessionManager)getSystemService(Context.MEDIA_SESSION_SERVICE);
        //mediaSessionManager.addOnActiveSessionsChangedListener(sessionsChangedListener,componentName);
        /*
        List<MediaController> mediaControllerList = mediaSessionManager.getActiveSessions(componentName);
        mediaController = pickMediaController(mediaControllerList);
        if(mediaController != null) {
            mediaController.registerCallback(mediaControllerCallback);
            mediaMetadata = mediaController.getMetadata();
        }
*/
        super.onCreate();
    }
 
Example #4
Source File: MediaPlayback.java    From PodEmu with GNU General Public License v3.0 6 votes vote down vote up
public static MediaController getActiveMediaController()
{
    ComponentName componentName = new ComponentName(context, NotificationListener4.class);

    MediaSessionManager mediaSessionManager = (MediaSessionManager)context.getSystemService(Context.MEDIA_SESSION_SERVICE);
    List<MediaController> mediaControllerList = null;

    try
    {
        mediaControllerList = mediaSessionManager.getActiveSessions(componentName);

        for(MediaController mediaController: mediaControllerList)
        {
            if(mediaController.getPackageName().equals(ctrlAppProcessName)) return mediaController;
        }
    }
    catch(Exception e)
    {
        PodEmuLog.error("MPlayback: Notification Listener permissions not granted");
    }

    return null;
}
 
Example #5
Source File: RemoteControlLollipop.java    From Noyze with Apache License 2.0 5 votes vote down vote up
protected RemoteControlLollipop(Context context, Class<? extends NotificationListenerService> clazz) {
    super(context);
    mControllerService = new ComponentName(context, clazz);
    mControllers = new ConcurrentHashMap<>();
    mMediaSessionManager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
    mMediaSessionManager.addOnActiveSessionsChangedListener(this, mControllerService);
    mRegistered = true;
}
 
Example #6
Source File: ScrobblerService.java    From QuickLyric with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onDestroy() {
    super.onDestroy();
    sRunning = false;
    this.mBinder = null;
    if (listener != null && listener.get() != null)
        ((MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE)).removeOnActiveSessionsChangedListener(listener.get());
    if (mediaControllerCallback != null) {
        mediaControllerCallback.removeControllerCallback();
        mediaControllerCallback = null;
    }
}
 
Example #7
Source File: ScrobblerService.java    From QuickLyric with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    sRunning = true;
    try {
        mediaControllerCallback = new MediaControllerCallback(this);
        MediaSessionManager.OnActiveSessionsChangedListener sessionsChangedListener =
                list -> mediaControllerCallback.registerActiveSessionCallback(ScrobblerService.this, list);
        listener = new WeakReference<>(sessionsChangedListener);
        MediaSessionManager manager = ((MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE));
        ComponentName className = new ComponentName(getApplicationContext(), NotificationListenerService.class);
        manager.addOnActiveSessionsChangedListener(sessionsChangedListener, className);
        List<MediaController> controllers = manager.getActiveSessions(className);
        mediaControllerCallback.registerActiveSessionCallback(ScrobblerService.this, controllers);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            SharedPreferences current = getSharedPreferences("current_music", Context.MODE_PRIVATE);
            String artist = current.getString("artist", "");
            String track = current.getString("track", "");
            startForeground(NOTIFICATION_ID, NotificationUtil.makeNotification(this, artist, track, 0L, false, false));
        }
        if (!controllers.isEmpty())
            mediaControllerCallback.broadcastControllerState(this, controllers.get(0), null);
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    Log.d("geecko", "mediaControllerCallback registered");
}
 
Example #8
Source File: MediaControllerCallback.java    From QuickLyric with GNU General Public License v3.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void registerFallbackControllerCallback(Context context, MediaControllerCallback controllerCallback) {
    MediaSessionManager mediaSessionManager = ((MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE));
    ComponentName className = new ComponentName(context.getApplicationContext(), NotificationListenerService.class);
    if (sessionListener != null)
        mediaSessionManager.removeOnActiveSessionsChangedListener(sessionListener);
    sessionListener = list -> controllerCallback.registerActiveSessionCallback(context, list);
    mediaSessionManager.addOnActiveSessionsChangedListener(sessionListener, className);
    controllerCallback.registerActiveSessionCallback(context, mediaSessionManager.getActiveSessions(className));
}
 
Example #9
Source File: RemoteControlLollipop.java    From Noyze with Apache License 2.0 5 votes vote down vote up
protected RemoteControlLollipop(Context context, Class<? extends NotificationListenerService> clazz) {
    super(context);
    mControllerService = new ComponentName(context, clazz);
    mControllers = new ConcurrentHashMap<>();
    mMediaSessionManager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
    mMediaSessionManager.addOnActiveSessionsChangedListener(this, mControllerService);
    mRegistered = true;
}
 
Example #10
Source File: MediaController2Lollipop.java    From AcDisplay with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void onStart(Object... objects) {
    super.onStart();

    // Init a new thread.
    mThread = new T(this);
    mThread.setPriority(Thread.MIN_PRIORITY);
    mThread.start();

    // Media session manager leaks/holds the context for too long.
    // Don't let it to leak the activity, better lak the whole app.
    final Context context = mContext.getApplicationContext();
    mMediaSessionManager = (MediaSessionManager) context
            .getSystemService(Context.MEDIA_SESSION_SERVICE);

    try {
        mMediaSessionManager.addOnActiveSessionsChangedListener(mSessionListener, mComponent);
        mSessionListener.setMediaController(this);
        mSessionListening = true;
    } catch (SecurityException exception) {
        Log.w(TAG, "Failed to start Lollipop media controller: " + exception.getMessage());
        // Try to unregister it, just it case.
        try {
            mMediaSessionManager.removeOnActiveSessionsChangedListener(mSessionListener);
        } catch (Exception e) { /* unused */ } finally {
            mMediaSessionManager = null;
            mSessionListening = false;
        }
        // Media controller needs notification listener service
        // permissions to be granted.
        return;
    }

    List<MediaController> controllers = mMediaSessionManager.getActiveSessions(mComponent);
    mSessionListener.onActiveSessionsChanged(controllers);
}
 
Example #11
Source File: ListenerService.java    From scroball with MIT License 5 votes vote down vote up
@Override
public void onCreate() {
  ScroballApplication application = (ScroballApplication) getApplication();
  sharedPreferences = application.getSharedPreferences();

  ConnectivityManager connectivityManager =
      (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
  ScroballDB scroballDB = application.getScroballDB();
  ScrobbleNotificationManager scrobbleNotificationManager =
      new ScrobbleNotificationManager(this, sharedPreferences, scroballDB);
  LastfmClient lastfmClient = application.getLastfmClient();
  TrackLover trackLover = new TrackLover(lastfmClient, scroballDB, connectivityManager);
  Scrobbler scrobbler =
      new Scrobbler(
          lastfmClient, scrobbleNotificationManager, scroballDB, connectivityManager, trackLover);

  playbackTracker = new PlaybackTracker(scrobbleNotificationManager, scrobbler);

  Log.d(TAG, "NotificationListenerService started");

  MediaSessionManager mediaSessionManager =
      (MediaSessionManager)
          getApplicationContext().getSystemService(Context.MEDIA_SESSION_SERVICE);

  ComponentName componentName = new ComponentName(this, this.getClass());
  mediaSessionManager.addOnActiveSessionsChangedListener(this, componentName);

  NetworkStateReceiver networkStateReceiver = new NetworkStateReceiver(scrobbler);
  IntentFilter filter = new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
  this.registerReceiver(networkStateReceiver, filter);

  // Trigger change event with existing set of sessions.
  List<MediaController> initialSessions = mediaSessionManager.getActiveSessions(componentName);
  onActiveSessionsChanged(initialSessions);

  sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
 
Example #12
Source File: MediaService.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
public void sync() {
    mDevice.enableNotify(mediaCommandsCharac, commandsListener);
    try {
        mMediaSessionManager = (MediaSessionManager) mCtx.getSystemService(Context.MEDIA_SESSION_SERVICE);
        List<MediaController> controllers = mMediaSessionManager.getActiveSessions(new ComponentName(mCtx, NLService.class));
        onActiveSessionsChanged(controllers);
        mMediaSessionManager.addOnActiveSessionsChangedListener(this, new ComponentName(mCtx, NLService.class));
    } catch (SecurityException e) {
        Log.w("MediaService", "No Notification Access");
    }
}
 
Example #13
Source File: VolumeKeyService.java    From skipTrackLongPressVolume with MIT License 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    preferences = new AppPreferences(this);
    preferences.registerOnTrayPreferenceChangeListener(this);

    audioManager = getSystemService(AudioManager.class);
    powerManager = getSystemService(PowerManager.class);

    mediaSessionManager = getSystemService(MediaSessionManager.class);
    mHandler = new Handler();
}
 
Example #14
Source File: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
    if (resultCode == MediaSessionManager.RESULT_MEDIA_KEY_HANDLED) {
        mHandled = true;
        mHandler.removeCallbacks(this);
        return;
    }
    dispatchMediaKeyEvent();
}
 
Example #15
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
public Object createService(ContextImpl ctx) {
    return new MediaSessionManager(ctx);
}
 
Example #16
Source File: ServiceUtil.java    From PowerFileExplorer with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(21)
public static MediaSessionManager getMediaSessionManager() {
    return (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
}
 
Example #17
Source File: SystemServiceRegistry.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public MediaSessionManager createService(ContextImpl ctx) {
    return new MediaSessionManager(ctx);
}
 
Example #18
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
public Object createService(ContextImpl ctx) {
    return new MediaSessionManager(ctx);
}