Exemple #1
0
 private void setImage(RemoteControlClient remoteControl, Drawable drawable) {
   if (remoteControl != null && drawable != null) {
     Bitmap origBitmap = ((BitmapDrawable) drawable).getBitmap();
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
       origBitmap = origBitmap.copy(origBitmap.getConfig(), false);
     }
     if (origBitmap != null && !origBitmap.isRecycled()) {
       remoteControl
           .editMetadata(false)
           .putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, origBitmap)
           .apply();
     } else {
       Log.e(TAG, "Tried to load a recycled bitmap.");
       remoteControl
           .editMetadata(false)
           .putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, null)
           .apply();
     }
   }
 }
  public void updateMetadata(final Context context, final MusicDirectory.Entry currentSong) {
    if (mRemoteControl == null) {
      return;
    }

    if (imageLoader == null) {
      imageLoader = SubsonicActivity.getStaticImageLoader(context);
    }

    // Update the remote controls
    RemoteControlClient.MetadataEditor editor = mRemoteControl.editMetadata(true);
    updateMetadata(currentSong, editor);
    editor.apply();
    if (currentSong == null || imageLoader == null) {
      mRemoteControl
          .editMetadata(true)
          .putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, null)
          .apply();
    } else {
      imageLoader.loadImage(context, mRemoteControl, currentSong);
    }
  }
 @SuppressLint("NewApi")
 @Override
 public void onDestroy() {
   super.onDestroy();
   if (remoteControlClient != null) {
     RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
     metadataEditor.clear();
     metadataEditor.apply();
     audioManager.unregisterRemoteControlClient(remoteControlClient);
   }
   NotificationCenter.getInstance()
       .removeObserver(this, NotificationCenter.audioProgressDidChanged);
   NotificationCenter.getInstance().removeObserver(this, NotificationCenter.audioPlayStateChanged);
 }
Exemple #4
0
  @TargetApi(14)
  private void updateRemoteControlClientMetadata() {
    if (!Util.isICSOrLater()) // NOP check
    return;

    if (mRemoteControlClient != null) {
      MetadataEditor editor = mRemoteControlClient.editMetadata(true);
      editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, mCurrentMedia.getAlbum());
      editor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, mCurrentMedia.getArtist());
      editor.putString(MediaMetadataRetriever.METADATA_KEY_GENRE, mCurrentMedia.getGenre());
      editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mCurrentMedia.getTitle());
      editor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, mCurrentMedia.getLength());
      editor.putBitmap(MetadataEditor.BITMAP_KEY_ARTWORK, getCover());
      editor.apply();
    }
  }
  /** Update the RemoteControlClient */
  private void updateRemoteControlClient() {
    // Update playstate
    if (App.mClementine.getState() == Clementine.State.PLAY) {
      mRcClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    } else {
      mRcClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
    }

    // Get the metadata editor
    if (mLastSong != null && mLastSong.getArt() != null) {
      RemoteControlClient.MetadataEditor editor = mRcClient.editMetadata(false);
      editor.putBitmap(MetadataEditor.BITMAP_KEY_ARTWORK, mLastSong.getArt());

      // The RemoteControlClients displays the following info:
      // METADATA_KEY_TITLE (white) - METADATA_KEY_ALBUMARTIST (grey) - METADATA_KEY_ALBUM (grey)
      //
      // So i put the metadata not in the "correct" fields to display artist, track and album
      // TODO: Fix it when changed in newer android versions
      editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, mLastSong.getAlbum());
      editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mLastSong.getArtist());
      editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, mLastSong.getTitle());
      editor.apply();
    }
  }
  @SuppressLint("NewApi")
  private void createNotification(MessageObject messageObject) {
    String songName = messageObject.getMusicTitle();
    String authorName = messageObject.getMusicAuthor();
    AudioInfo audioInfo = MediaController.getInstance().getAudioInfo();

    RemoteViews simpleContentView =
        new RemoteViews(
            getApplicationContext().getPackageName(), R.layout.player_small_notification);
    RemoteViews expandedView = null;
    if (supportBigNotifications) {
      expandedView =
          new RemoteViews(
              getApplicationContext().getPackageName(), R.layout.player_big_notification);
    }

    Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
    intent.setAction("com.tmessages.openplayer");
    intent.setFlags(32768);
    PendingIntent contentIntent =
        PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent, 0);

    Notification notification =
        new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.player)
            .setContentIntent(contentIntent)
            .setContentTitle(songName)
            .build();

    notification.contentView = simpleContentView;
    if (supportBigNotifications) {
      notification.bigContentView = expandedView;
    }

    setListeners(simpleContentView);
    if (supportBigNotifications) {
      setListeners(expandedView);
    }

    Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover() : null;
    if (albumArt != null) {
      notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
      if (supportBigNotifications) {
        notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
      }
    } else {
      notification.contentView.setImageViewResource(
          R.id.player_album_art, R.drawable.nocover_small);
      if (supportBigNotifications) {
        notification.bigContentView.setImageViewResource(
            R.id.player_album_art, R.drawable.nocover_big);
      }
    }
    if (MediaController.getInstance().isDownloadingCurrentMessage()) {
      notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
      notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
      notification.contentView.setViewVisibility(R.id.player_next, View.GONE);
      notification.contentView.setViewVisibility(R.id.player_previous, View.GONE);
      notification.contentView.setViewVisibility(R.id.player_progress_bar, View.VISIBLE);
      if (supportBigNotifications) {
        notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
        notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
        notification.bigContentView.setViewVisibility(R.id.player_next, View.GONE);
        notification.bigContentView.setViewVisibility(R.id.player_previous, View.GONE);
        notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.VISIBLE);
      }
    } else {
      notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
      notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
      notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
      if (supportBigNotifications) {
        notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
        notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
        notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
      }

      if (MediaController.getInstance().isAudioPaused()) {
        notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
        if (supportBigNotifications) {
          notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
          notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
        }
      } else {
        notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
        notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
        if (supportBigNotifications) {
          notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
          notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
        }
      }
    }

    notification.contentView.setTextViewText(R.id.player_song_name, songName);
    notification.contentView.setTextViewText(R.id.player_author_name, authorName);
    if (supportBigNotifications) {
      notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
      notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
    }
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    startForeground(5, notification);

    if (remoteControlClient != null) {
      RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
      metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
      metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
      if (audioInfo != null && audioInfo.getCover() != null) {
        try {
          metadataEditor.putBitmap(
              RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, audioInfo.getCover());
        } catch (Throwable e) {
          FileLog.e("tmessages", e);
        }
      }
      metadataEditor.apply();
    }
  }
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  void updateRemoteControl() {
    updateRemoteControlState(RemoteControlClient.PLAYSTATE_PLAYING);
    remoteControlClient.setTransportControlFlags(
        RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_NEXT);

    // Update the remote controls
    Bitmap bitmap =
        MediaUtils.getArtworkQuick(this, currentTrack, screenWidth / 2, screenWidth / 2);
    int redTop = 0, greenTop = 0, blueTop = 0, pixelsTop = 0;
    int redBtm = 0, greenBtm = 0, blueBtm = 0, pixelsBtm = 0;
    int colorTop = 0, colorBtm = 0;

    if (bitmap != null) {
      int w = bitmap.getWidth();
      int h = bitmap.getHeight();
      for (int i = 0; i < w; i++) {
        try {
          colorTop = bitmap.getPixel(i, 0);
          redTop += Color.red(colorTop);
          greenTop += Color.green(colorTop);
          blueTop += Color.blue(colorTop);
          pixelsTop += 1;

          colorBtm = bitmap.getPixel(i, h - 1);
          redBtm += Color.red(colorBtm);
          greenBtm += Color.green(colorBtm);
          blueBtm += Color.blue(colorBtm);
          pixelsBtm += 1;
        } catch (Exception e) {
        }
      }
      if (pixelsTop > 0 && pixelsBtm > 0) {
        colorTop =
            Color.rgb(redTop / pixelsTop, greenTop / pixelsTop, blueTop / pixelsTop); // EDE7E9
        colorBtm = Color.rgb(redBtm / pixelsBtm, greenBtm / pixelsBtm, blueBtm / pixelsBtm);
        Shader shader =
            new LinearGradient(w / 2, 0, w / 2, h, colorTop, colorBtm, Shader.TileMode.CLAMP);
        Bitmap bitmapBgr = Bitmap.createBitmap(w, screenHeight / 2, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmapBgr);
        Paint paint = new Paint();
        paint.setShader(shader);
        canvas.drawRect(0, 0, w, screenHeight / 2, paint);
        canvas.drawBitmap(bitmap, 0, (screenHeight / 2 - screenWidth / 2) / 2, null);
        bitmap.recycle();
        bitmap = bitmapBgr;
      }
    } else {
      // create random color
      bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
      Random rnd = new Random();
      bitmap.eraseColor(Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
    }
    remoteControlClient
        .editMetadata(true)
        .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, currentTrack.getArtist())
        .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, currentTrack.getAlbum())
        .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, currentTrack.getTitle())
        .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, currentTrack.getDuration())
        .putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, bitmap)
        .apply();
  }