package me.wcy.music.activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.media.audiofx.AudioEffect; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceFragment; import android.text.TextUtils; import com.hwangjr.rxbus.RxBus; import me.wcy.music.R; import me.wcy.music.constants.RxBusTags; import me.wcy.music.service.AudioPlayer; import me.wcy.music.utils.MusicUtils; import me.wcy.music.storage.preference.Preferences; import me.wcy.music.utils.ToastUtils; public class SettingActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); } @Override protected void onServiceBound() { SettingFragment settingFragment = new SettingFragment(); getFragmentManager().beginTransaction().replace(R.id.ll_fragment_container, settingFragment).commit(); } public static class SettingFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener { private Preference mSoundEffect; private Preference mFilterSize; private Preference mFilterTime; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preference_setting); mSoundEffect = findPreference(getString(R.string.setting_key_sound_effect)); mFilterSize = findPreference(getString(R.string.setting_key_filter_size)); mFilterTime = findPreference(getString(R.string.setting_key_filter_time)); mSoundEffect.setOnPreferenceClickListener(this); mFilterSize.setOnPreferenceChangeListener(this); mFilterTime.setOnPreferenceChangeListener(this); mFilterSize.setSummary(getSummary(Preferences.getFilterSize(), R.array.filter_size_entries, R.array.filter_size_entry_values)); mFilterTime.setSummary(getSummary(Preferences.getFilterTime(), R.array.filter_time_entries, R.array.filter_time_entry_values)); } @Override public boolean onPreferenceClick(Preference preference) { if (preference == mSoundEffect) { startEqualizer(); return true; } return false; } private void startEqualizer() { if (MusicUtils.isAudioControlPanelAvailable(getActivity())) { Intent intent = new Intent(); String packageName = getActivity().getPackageName(); intent.setAction(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL); intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, packageName); intent.putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC); intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, AudioPlayer.get().getAudioSessionId()); try { startActivityForResult(intent, 1); } catch (ActivityNotFoundException e) { e.printStackTrace(); ToastUtils.show(R.string.device_not_support); } } else { ToastUtils.show(R.string.device_not_support); } } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if (preference == mFilterSize) { Preferences.saveFilterSize((String) newValue); mFilterSize.setSummary(getSummary(Preferences.getFilterSize(), R.array.filter_size_entries, R.array.filter_size_entry_values)); RxBus.get().post(RxBusTags.SCAN_MUSIC, 1); return true; } else if (preference == mFilterTime) { Preferences.saveFilterTime((String) newValue); mFilterTime.setSummary(getSummary(Preferences.getFilterTime(), R.array.filter_time_entries, R.array.filter_time_entry_values)); RxBus.get().post(RxBusTags.SCAN_MUSIC, 1); return true; } return false; } private String getSummary(String value, int entries, int entryValues) { String[] entryArray = getResources().getStringArray(entries); String[] entryValueArray = getResources().getStringArray(entryValues); for (int i = 0; i < entryValueArray.length; i++) { String v = entryValueArray[i]; if (TextUtils.equals(v, value)) { return entryArray[i]; } } return entryArray[0]; } } }
package me.wcy.music.adapter; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.ArrayList; import java.util.List; /** * Fragment适配器 * Created by wcy on 2015/11/26. */ public class FragmentAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragments = new ArrayList<>(); public FragmentAdapter(FragmentManager fm) { super(fm); } public void addFragment(Fragment fragment) { mFragments.add(fragment); } @Override public Fragment getItem(int position) { return mFragments.get(position); } @Override public int getCount() { return mFragments.size(); } }
package me.wcy.music.adapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.List; import me.wcy.music.R; import me.wcy.music.model.OnlineMusic; import me.wcy.music.utils.FileUtils; import me.wcy.music.utils.binding.Bind; import me.wcy.music.utils.binding.ViewBinder; /** * 在线音乐列表适配器 * Created by wcy on 2015/12/22. */ public class OnlineMusicAdapter extends BaseAdapter { private List<OnlineMusic> mData; private OnMoreClickListener mListener; public OnlineMusicAdapter(List<OnlineMusic> data) { this.mData = data; } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_holder_music, parent, false); holder = new ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } OnlineMusic onlineMusic = mData.get(position); Glide.with(parent.getContext()) .load(onlineMusic.getPic_small()) .placeholder(R.drawable.default_cover) .error(R.drawable.default_cover) .into(holder.ivCover); holder.tvTitle.setText(onlineMusic.getTitle()); String artist = FileUtils.getArtistAndAlbum(onlineMusic.getArtist_name(), onlineMusic.getAlbum_title()); holder.tvArtist.setText(artist); holder.ivMore.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mListener.onMoreClick(position); } }); holder.vDivider.setVisibility(isShowDivider(position) ? View.VISIBLE : View.GONE); return convertView; } private boolean isShowDivider(int position) { return position != mData.size() - 1; } public void setOnMoreClickListener(OnMoreClickListener listener) { mListener = listener; } private static class ViewHolder { @Bind(R.id.iv_cover) private ImageView ivCover; @Bind(R.id.tv_title) private TextView tvTitle; @Bind(R.id.tv_artist) private TextView tvArtist; @Bind(R.id.iv_more) private ImageView ivMore; @Bind(R.id.v_divider) private View vDivider; public ViewHolder(View view) { ViewBinder.bind(this, view); } } }
package me.wcy.music.adapter; /** * 音乐列表“更多”按钮监听器 * Created by hzwangchenyan on 2015/12/17. */ public interface OnMoreClickListener { void onMoreClick(int position); }
package me.wcy.music.adapter; import android.graphics.Bitmap; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; import me.wcy.music.R; import me.wcy.music.model.Music; import me.wcy.music.service.AudioPlayer; import me.wcy.music.utils.CoverLoader; import me.wcy.music.utils.FileUtils; import me.wcy.music.utils.binding.Bind; import me.wcy.music.utils.binding.ViewBinder; /** * 本地音乐列表适配器 * Created by wcy on 2015/11/27. */ public class PlaylistAdapter extends BaseAdapter { private List<Music> musicList; private OnMoreClickListener listener; private boolean isPlaylist; public PlaylistAdapter(List<Music> musicList) { this.musicList = musicList; } public void setIsPlaylist(boolean isPlaylist) { this.isPlaylist = isPlaylist; } public void setOnMoreClickListener(OnMoreClickListener listener) { this.listener = listener; } @Override public int getCount() { return musicList.size(); } @Override public Object getItem(int position) { return musicList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_holder_music, parent, false); holder = new ViewHolder(convertView); holder.ivCover=convertView.findViewById(R.id.iv_cover); holder.tvTitle=convertView.findViewById(R.id.tv_title); holder.tvArtist=convertView.findViewById(R.id.tv_artist); holder.ivMore=convertView.findViewById(R.id.iv_more); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.vPlaying.setVisibility((isPlaylist && position == AudioPlayer.get().getPlayPosition()) ? View.VISIBLE : View.INVISIBLE); Music music = musicList.get(position); Bitmap cover = CoverLoader.get().loadThumb(music); holder.ivCover.setImageBitmap(cover); holder.tvTitle.setText(music.getTitle()); String artist = FileUtils.getArtistAndAlbum(music.getArtist(), music.getAlbum()); holder.tvArtist.setText(artist); holder.ivMore.setOnClickListener(v -> { if (listener != null) { listener.onMoreClick(position); } }); holder.vDivider.setVisibility(isShowDivider(position) ? View.VISIBLE : View.GONE); return convertView; } private boolean isShowDivider(int position) { return position != musicList.size() - 1; } private static class ViewHolder { @Bind(R.id.v_playing) private View vPlaying; @Bind(R.id.iv_cover) private ImageView ivCover; @Bind(R.id.tv_title) private TextView tvTitle; @Bind(R.id.tv_artist) private TextView tvArtist; @Bind(R.id.iv_more) private ImageView ivMore; @Bind(R.id.v_divider) private View vDivider; public ViewHolder(View view) { ViewBinder.bind(this, view); } } }