예제 #1
0
 private boolean addSongsToPlaylist(MyLibraryItem libraryItem) {
   switch (libraryItem.getLevel()) {
     case MyLibrary.LVL_ARTIST:
       MyLibrary addArtist = new MyLibrary(getActivity());
       addArtist.addOnLibrarySelectFinishedListener(LibraryFragment.this);
       addArtist.getAllTitlesFromArtistAsync(libraryItem.getArtist());
       return true;
     case MyLibrary.LVL_ALBUM:
       MyLibrary addAlbums = new MyLibrary(getActivity());
       addAlbums.addOnLibrarySelectFinishedListener(LibraryFragment.this);
       addAlbums.getTitlesAsync(libraryItem.getArtist(), libraryItem.getAlbum());
       return true;
     case MyLibrary.LVL_TITLE:
       Message msg = Message.obtain();
       LinkedList<String> urls = new LinkedList<String>();
       urls.add(libraryItem.getUrl());
       msg.obj =
           ClementineMessageFactory.buildInsertUrl(
               App.mClementine.getPlaylistManager().getActivePlaylistId(), urls);
       App.mClementineConnection.mHandler.sendMessage(msg);
       return false;
     default:
       return false;
   }
 }
  @Override
  public void onReceive(Context context, Intent intent) {
    // Check if we have an media button intent
    if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {

      // Get the key event and obtain a new message
      KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
      Message msg = Message.obtain();

      // Only on KeyDown
      if (event.getAction() == KeyEvent.ACTION_UP) {
        // Check which key was pressed
        switch (event.getKeyCode()) {
          case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            msg.obj = ClementineMessage.getMessage(MsgType.PLAYPAUSE);
            break;
          case KeyEvent.KEYCODE_MEDIA_NEXT:
            msg.obj = ClementineMessage.getMessage(MsgType.NEXT);
            break;
          case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            msg.obj = ClementineMessage.getMessage(MsgType.PREVIOUS);
            break;
          case KeyEvent.KEYCODE_VOLUME_DOWN:
            msg.obj = ClementineMessageFactory.buildVolumeMessage(App.mClementine.getVolume() - 10);
            break;
          case KeyEvent.KEYCODE_VOLUME_UP:
            msg.obj = ClementineMessageFactory.buildVolumeMessage(App.mClementine.getVolume() + 10);
            break;
          default:
            msg = null;
            break;
        }

        // Now send the message
        if (msg != null && App.mClementineConnection != null) {
          App.mClementineConnection.mHandler.sendMessage(msg);
        }
      }
    }
  }
  /**
   * Connect to Clementine
   *
   * @return true if the connection was established, false if not
   */
  private boolean connect() {
    String ip = mSharedPref.getString(App.SP_KEY_IP, "");
    int port;
    try {
      port =
          Integer.valueOf(
              mSharedPref.getString(App.SP_KEY_PORT, String.valueOf(Clementine.DefaultPort)));
    } catch (NumberFormatException e) {
      port = Clementine.DefaultPort;
    }
    int authCode = mSharedPref.getInt(App.SP_LAST_AUTH_CODE, 0);

    return mClient.createConnection(
        ClementineMessageFactory.buildConnectMessage(ip, port, authCode, false, true));
  }
  /**
   * This method checks if the offered file exists and sends a response to Clementine. If the file
   * does not exist -> Download file otherwise The user wants to override existing files -> Download
   * file otherwise refuse file
   *
   * @param chunk The chunk with the metadata
   * @return a boolean indicating if the song will be sent or not
   */
  private boolean processSongOffer(ResponseSongFileChunk chunk) {
    File f = new File(BuildFilePath(chunk));
    boolean accept = true;

    if (f.exists() && !mOverrideExistingFiles) {
      accept = false;
    }

    mClient.sendRequest(ClementineMessageFactory.buildSongOfferResponse(accept));

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

    return accept;
  }
예제 #5
0
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          MyLibraryItem item = mAdapters.getLast().getItem(position);

          switch (item.getLevel()) {
            case MyLibrary.LVL_ARTIST:
              LibraryAdapter album =
                  new LibraryAdapter(
                      getActivity(),
                      mLibrary.getAlbums(item.getArtist()),
                      mLibrary,
                      MyLibrary.LVL_ALBUM);
              mAdapters.add(album);
              showList();
              break;
            case MyLibrary.LVL_ALBUM:
              LibraryAdapter title =
                  new LibraryAdapter(
                      getActivity(),
                      mLibrary.getTitles(item.getArtist(), item.getAlbum()),
                      mLibrary,
                      MyLibrary.LVL_TITLE);
              mAdapters.add(title);
              showList();
              break;
            case MyLibrary.LVL_TITLE:
              Message msg = Message.obtain();
              LinkedList<String> urls = new LinkedList<String>();
              urls.add(item.getUrl());
              msg.obj =
                  ClementineMessageFactory.buildInsertUrl(
                      App.mClementine.getPlaylistManager().getActivePlaylistId(), urls);
              App.mClementineConnection.mHandler.sendMessage(msg);

              Toast.makeText(
                      getActivity(),
                      String.format(getString(R.string.library_songs_added), 1),
                      Toast.LENGTH_SHORT)
                  .show();
              break;
            default:
              break;
          }
        }
예제 #6
0
  private void addSongsToPlaylist(LinkedList<MyLibraryItem> l) {
    Message msg = Message.obtain();
    LinkedList<String> urls = new LinkedList<String>();
    for (MyLibraryItem item : l) {
      urls.add(item.getUrl());
    }

    msg.obj =
        ClementineMessageFactory.buildInsertUrl(
            App.mClementine.getPlaylistManager().getActivePlaylistId(), urls);

    App.mClementineConnection.mHandler.sendMessage(msg);

    Toast.makeText(
            getActivity(),
            String.format(getString(R.string.library_songs_added), urls.size()),
            Toast.LENGTH_SHORT)
        .show();
  }
  /**
   * Try to connect to Clementine
   *
   * @param message The Request Object. Stores the ip to connect to.
   */
  @Override
  public boolean createConnection(ClementineMessage message) {
    boolean connected = false;
    // Reset the connected flag
    mLastKeepAlive = 0;

    // Now try to connect and set the input and output streams
    connected = super.createConnection(message);

    // Check if Clementine dropped the connection.
    // Is possible when we connect from a public ip and clementine rejects it
    if (connected && !mSocket.isClosed()) {
      // Enter the main loop in the thread
      Message msg = Message.obtain();
      msg.arg1 = CHECK_FOR_DATA_ARG;
      mHandler.sendMessage(msg);

      // Now we are connected
      mLastSong = null;
      mLastState = App.mClementine.getState();

      // Setup the MediaButtonReceiver and the RemoteControlClient
      registerRemoteControlClient();

      // Register MediaButtonReceiver
      IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
      App.mApp.registerReceiver(mMediaButtonBroadcastReceiver, filter);

      updateNotification();

      // The device shall be awake
      mWakeLock.acquire();

      // We can now reconnect MAX_RECONNECTS times when
      // we get a keep alive timeout
      mLeftReconnects = MAX_RECONNECTS;

      // Set the current time to last keep alive
      setLastKeepAlive(System.currentTimeMillis());

      // Until we get a new connection request from ui,
      // don't request the first data a second time
      mRequestConnect =
          ClementineMessageFactory.buildConnectMessage(
              message.getIp(),
              message.getPort(),
              message.getMessage().getRequestConnect().getAuthCode(),
              false,
              message.getMessage().getRequestConnect().getDownloader());

      // Save started transmitted bytes
      int uid = App.mApp.getApplicationInfo().uid;
      mStartTx = TrafficStats.getUidTxBytes(uid);
      mStartRx = TrafficStats.getUidRxBytes(uid);

      mStartTime = new Date().getTime();
    } else {
      sendUiMessage(new ClementineMessage(ErrorMessage.NO_CONNECTION));
    }

    return connected;
  }