Exemplo n.º 1
0
 @Override
 public void MessageFromClementine(ClementineMessage clementineMessage) {
   switch (clementineMessage.getMessageType()) {
     case CURRENT_METAINFO:
       setActionBarTitle();
       break;
     default:
       break;
   }
 }
  /**
   * Process the received protocol buffer
   *
   * @param clementineMessage The Message received from Clementine
   */
  private void processProtocolBuffer(ClementineMessage clementineMessage) {
    // Close the connection if we have an old proto verion
    if (clementineMessage.isErrorMessage()) {
      closeConnection(clementineMessage);
    } else if (clementineMessage.getTypeGroup() == MessageGroup.GUI_RELOAD) {
      sendUiMessage(clementineMessage);

      // Now update the notification and the remote control client
      if (App.mClementine.getCurrentSong() != mLastSong) {
        mLastSong = App.mClementine.getCurrentSong();
        updateNotification();
        updateRemoteControlClient();
        mPebble.sendMusicUpdateToPebble();
      }
      if (App.mClementine.getState() != mLastState) {
        mLastState = App.mClementine.getState();
        updateRemoteControlClient();
      }
    } else if (clementineMessage.getMessageType() == MsgType.DISCONNECT) {
      closeConnection(clementineMessage);
    } else {
      sendUiMessage(clementineMessage);
    }
  }
Exemplo n.º 3
0
  @Override
  public void MessageFromClementine(ClementineMessage clementineMessage) {
    switch (clementineMessage.getMessageType()) {
      case PLAY:
      case PAUSE:
      case STOP:
        stateChanged();
        break;
      case CURRENT_METAINFO:
        metadataChanged();
      default:
        break;
    }

    if (mPlayerPageFragment.isAdded()) {
      mPlayerPageFragment.MessageFromClementine(clementineMessage);
    }
    if (mSongDetailFragment.isAdded()) {
      mSongDetailFragment.MessageFromClementine(clementineMessage);
    }
    if (mConnectionFragment.isAdded()) {
      mConnectionFragment.MessageFromClementine(clementineMessage);
    }
  }
  /** Start the Download */
  private DownloaderResult startDownloading(ClementineMessage clementineMessage) {
    boolean downloadFinished = false;
    DownloaderResult result = new DownloaderResult(DownloadResult.SUCCESSFUL);
    File f = null;
    FileOutputStream fo = null;

    publishProgress(0);

    // Now request the songs
    mClient.sendRequest(clementineMessage);

    while (!downloadFinished) {
      // Check if the user canceled the process
      if (isCancelled()) {
        // Close the stream and delete the incomplete file
        try {
          if (fo != null) {
            fo.flush();
            fo.close();
          }
          if (f != null) {
            f.delete();
          }
        } catch (IOException e) {
        }
        Log.d(TAG, "isCancelled");
        break;
      }

      // Get the raw protocol buffer
      ClementineMessage message = mClient.getProtoc();

      if (message.isErrorMessage()) {
        result = new DownloaderResult(DownloadResult.CONNECTION_ERROR);
        break;
      }

      // Is the download forbidden?
      if (message.getMessageType() == MsgType.DISCONNECT) {
        result = new DownloaderResult(DownloadResult.FOBIDDEN);
        break;
      }

      // Ignore other elements!
      if (message.getMessageType() != MsgType.LIBRARY_CHUNK) {
        continue;
      }

      ResponseLibraryChunk chunk = message.getMessage().getResponseLibraryChunk();

      try {
        // Check if we need to create a new file
        if (f == null) {
          // Check if we have enougth free space
          // size times 2, because we optimise the table later and
          // need space for that too!
          if ((chunk.getSize() * 2) > Utilities.getFreeSpaceExternal()) {
            result = new DownloaderResult(DownloadResult.INSUFFIANT_SPACE);
            break;
          }
          f = mLibrary.getLibraryDb();

          // User wants to override files, so delete it here!
          // The check was already done in processSongOffer()
          if (f.exists()) {
            f.delete();
          }

          f.createNewFile();
          fo = new FileOutputStream(f);
        }

        // Write chunk to sdcard
        fo.write(chunk.getData().toByteArray());

        // Have we downloaded all chunks?
        if (chunk.getChunkCount() == chunk.getChunkNumber()) {
          fo.flush();
          fo.close();
          f = null;
          downloadFinished = true;
        }

        // Update notification
        updateProgress(chunk);
      } catch (IOException e) {
        result = new DownloaderResult(DownloaderResult.DownloadResult.NOT_MOUNTED);
        break;
      }
    }

    // Disconnect at the end
    mClient.disconnect(ClementineMessage.getMessage(MsgType.DISCONNECT));

    // Optimize library table
    if (mLibrary.getLibraryDb().exists()) {
      mLibrary.optimizeTable();
    }

    return result;
  }
  /** Start the Downlaod */
  private DownloaderResult startDownloading(ClementineMessage clementineMessage) {
    boolean downloadFinished = false;
    DownloaderResult result = new DownloaderResult(DownloadResult.SUCCESSFUL);
    File f = null;
    FileOutputStream fo = null;

    // Do we have a playlist?
    mIsPlaylist =
        (clementineMessage.getMessage().getRequestDownloadSongs().getDownloadItem()
            == DownloadItem.APlaylist);
    if (mIsPlaylist) {
      int id = clementineMessage.getMessage().getRequestDownloadSongs().getPlaylistId();
      mPlaylistName = App.mClementine.getPlaylistManager().getPlaylist(id).getName();
    }

    publishProgress(0);

    // Now request the songs
    mClient.sendRequest(clementineMessage);

    while (!downloadFinished) {
      // Check if the user canceled the process
      if (isCancelled()) {
        // Close the stream and delete the incomplete file
        try {
          if (fo != null) {
            fo.flush();
            fo.close();
          }
          if (f != null) {
            f.delete();
          }
        } catch (IOException e) {
        }

        break;
      }

      // Get the raw protocol buffer
      ClementineMessage message = mClient.getProtoc();

      // Check if an error occured
      if (message == null || message.isErrorMessage()) {
        result = new DownloaderResult(DownloadResult.CONNECTION_ERROR);
        break;
      }

      // Is the download forbidden?
      if (message.getMessageType() == MsgType.DISCONNECT) {
        result = new DownloaderResult(DownloadResult.FOBIDDEN);
        break;
      }

      // Download finished?
      if (message.getMessageType() == MsgType.DOWNLOAD_QUEUE_EMPTY) {
        break;
      }

      // Ignore other elements!
      if (message.getMessageType() != MsgType.SONG_FILE_CHUNK) {
        continue;
      }

      ResponseSongFileChunk chunk = message.getMessage().getResponseSongFileChunk();

      // If we received chunk no 0, then we have to decide wether to
      // accept the song offered or not
      if (chunk.getChunkNumber() == 0) {
        processSongOffer(chunk);

        // Update progress here to. If the first (and only) file exists and shall not be
        // overriten, the notification bar shows NULL.
        mCurrentSong = MySong.fromProtocolBuffer(chunk.getSongMetadata());
        publishProgress(mCurrentProgress);
        continue;
      }

      try {
        // Check if we need to create a new file
        if (f == null) {
          // Check if we have enougth free space
          if (chunk.getSize() > Utilities.getFreeSpaceExternal()) {
            result = new DownloaderResult(DownloadResult.INSUFFIANT_SPACE);
            break;
          }

          File dir = new File(BuildDirPath(chunk));
          f = new File(BuildFilePath(chunk));

          // User wants to override files, so delete it here!
          // The check was already done in processSongOffer()
          if (f.exists()) {
            f.delete();
          }

          dir.mkdirs();
          f.createNewFile();
          fo = new FileOutputStream(f);

          // File for download fragment
          mFileUri = Uri.fromFile(f);
        }

        // Write chunk to sdcard
        fo.write(chunk.getData().toByteArray());

        // Have we downloaded all chunks?
        if (chunk.getChunkCount() == chunk.getChunkNumber()) {
          // Index file
          MediaScannerConnection.scanFile(mContext, new String[] {f.getAbsolutePath()}, null, null);
          fo.flush();
          fo.close();
          f = null;
        }

        // Update notification
        updateProgress(chunk);
      } catch (IOException e) {
        result = new DownloaderResult(DownloaderResult.DownloadResult.NOT_MOUNTED);
        break;
      }
    }

    // Disconnect at the end
    mClient.disconnect(ClementineMessage.getMessage(MsgType.DISCONNECT));

    return result;
  }