package com.gracecode.RainNoise.ui.fragment;

import android.app.ListFragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.gracecode.RainNoise.adapter.PresetsAdapter;
import com.gracecode.RainNoise.helper.MixerPresetsHelper;
import com.gracecode.RainNoise.player.PlayManager;
import com.gracecode.RainNoise.player.PlayerBinder;

public class PresetsFragment extends ListFragment implements PlayerBinder, MixerPresetsHelper {
    private PresetsAdapter mAdapter;
    private PlayManager mPlayManager;
    private SharedPreferences mSharedPreferences;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mAdapter = new PresetsAdapter(getActivity(), PRESET_TITLES);
        mSharedPreferences = getActivity().getSharedPreferences(PresetsFragment.class.getName(), Context.MODE_PRIVATE);
    }

    @Override
    public void onStart() {
        super.onStart();
        setListAdapter(mAdapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        setPresets(ALL_PRESETS[position]);

    }

    public void setPresets(float[] presets) {
        for (int i = 0; i < PlayManager.MAX_TRACKS_NUM; i++) {
            int volume = (int) (mPlayManager.getMaxVolume() * presets[i]);
            mPlayManager.setVolume(i, volume);
            mSharedPreferences.edit().putFloat("_" + i, presets[i]).commit();
        }
    }

    public float[] getPresets() {
        float[] result = new float[PlayManager.MAX_TRACKS_NUM];
        for (int i = 0; i < PlayManager.MAX_TRACKS_NUM; i++) {
            result[i] = mSharedPreferences.getFloat("_" + i, mPlayManager.getDefaultVolume());
        }

        return result;
    }

    @Override
    public void bindPlayerManager(PlayManager manager) {
        mPlayManager = manager;
    }

    @Override
    public void unbindPlayerManager() {
        mPlayManager = null;
    }

    @Override
    public void refresh() {
        setPresets(getPresets());
    }
}

@@ -669,16 +669,17 @@
 ences;%0A%0A
+%0A
     @Ove
@@ -800,17 +800,16 @@
 State);%0A
-%0A
         
@@ -911,16 +911,33 @@
 tivity()
+%0A                
 .getShar
@@ -1005,32 +1005,33 @@
 RIVATE);%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -1114,32 +1114,33 @@
 dapter);%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -1308,32 +1308,33 @@
 tion%5D);%0A%0A    %7D%0A%0A
+%0A
     public void 
@@ -1639,24 +1639,25 @@
    %7D%0A    %7D%0A%0A
+%0A
     public f
@@ -1933,32 +1933,33 @@
  result;%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -2044,32 +2044,33 @@
 manager;%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -2139,24 +2139,25 @@
 ull;%0A    %7D%0A%0A
+%0A
     @Overrid
@@ -2150,32 +2150,32 @@
 %0A%0A    @Override%0A
-
     public void 
@@ -2182,24 +2182,96 @@
 refresh() %7B%0A
+        if (mPlayManager == null %7C%7C mSharedPreferences == null) return;%0A
         setP

修复部分 Bug 和问题