Android Open Source - SensorDataCollector Audio Sensor Setup Dialog Fragment






From Project

Back to project page SensorDataCollector.

License

The source code is released under:

GNU General Public License

If you think the Android project SensorDataCollector listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2014, the SenSee authors.  Please see the AUTHORS file
 * for details. //  ww  w .j  av  a 2 s  .c om
 * 
 * Licensed under the GNU Public License, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.gnu.org/copyleft/gpl.html
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package sysnetlab.android.sdc.ui.fragments;

import java.util.ArrayList;
import java.util.List;

import sysnetlab.android.sdc.R;
import sysnetlab.android.sdc.sensor.audio.AudioChannelIn;
import sysnetlab.android.sdc.sensor.audio.AudioEncoding;
import sysnetlab.android.sdc.sensor.audio.AudioRecordParameter;
import sysnetlab.android.sdc.sensor.audio.AudioRecordSettingDataSource;
import sysnetlab.android.sdc.sensor.audio.AudioSource;
import sysnetlab.android.sdc.ui.UserInterfaceUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.ListView;

public class AudioSensorSetupDialogFragment extends DialogFragment {
    public static final int SELECT_SOURCE = 1;
    public static final int SELECT_CHANNEL_IN = 2;
    public static final int SELECT_ENCODING = 3;
    public static final int SELECT_SAMPLING_RATE = 4;
    public static final int SELECT_MIN_BUFFER_SIZE = 5;

    // assume that list is short. use sequential search
    private List<AudioRecordParameter> mListAudioRecordParameters;
    private AudioRecordParameter mAudioRecordParameter;
    private Activity mActivity;
    private ListView mListView;

    public static AudioSensorSetupDialogFragment newInstance(Activity activity,
            List<AudioRecordParameter> allParams,
            AudioRecordParameter param, ListView listView, int operation) {
        AudioSensorSetupDialogFragment d = new AudioSensorSetupDialogFragment();

        d.mActivity = activity;
        d.mAudioRecordParameter = param;
        d.mListAudioRecordParameters = allParams;
        d.mListView = listView;

        // Supply operation as an argument.
        Bundle args = new Bundle();
        args.putInt("operation", operation);
        d.setArguments(args);

        return d;
    }

    public AudioSensorSetupDialogFragment() {
        super();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int operation = getArguments().getInt("operation");

        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        CharSequence[] cs;
        int checkedItem;

        AudioRecordSettingDataSource.initializeInstance(getActivity());
        AudioRecordSettingDataSource dbSource = AudioRecordSettingDataSource.getInstance();
        dbSource.open();

        switch (operation) {
            case SELECT_SOURCE:

                final List<AudioSource> listSources = dbSource.getAllAudioSources();

                cs = new CharSequence[listSources.size()];
                checkedItem = 0;
                for (int i = 0; i < listSources.size(); i++) {
                    AudioSource source = listSources.get(i);
                    cs[i] = getActivity().getResources().getString(source.getSourceNameResId());
                    if (source.getSourceId() == mAudioRecordParameter.getSource().getSourceId()) {
                        checkedItem = i;
                    }
                }
                builder.setSingleChoiceItems(cs, checkedItem,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.d("SensorDataCollector",
                                        "AudioSensorSetupDialog::SOURCE_SLECT: which = " + which);
                                AudioSource source = listSources.get(which);
                                List<AudioRecordParameter> params = lookupAudioRecordParameter(source);
                                if (params == null || params.isEmpty()) {
                                    Log.d("SensorDataCollector",
                                            "AudioSensorSetupDialog::SOURCE_SELECT:params is empty");
                                    return;
                                }

                                AudioRecordParameter p = params.get(0);

                                Log.d("SensorDataCollector",
                                        "AudioSensorSetupDialog::SOURCE_SLECT:BEFORE:SourceId = "
                                                + mAudioRecordParameter.getSource().getSourceId());
                                mAudioRecordParameter.setSource(source);
                                Log.d("SensorDataCollector",
                                        "AudioSensorSetupDialog::SOURCE_SLECT:AFTER:SourceId = "
                                                + mAudioRecordParameter.getSource().getSourceId());

                                mAudioRecordParameter.setChannel(p.getChannel());
                                mAudioRecordParameter.setEncoding(p.getEncoding());
                                mAudioRecordParameter.setSamplingRate(p.getSamplingRate());
                                mAudioRecordParameter.setMinBufferSize(p.getBufferSize());
                            }
                        });

                builder.setPositiveButton(getActivity().getResources().getString(R.string.text_ok),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UserInterfaceUtils.updateAudioSensorPropertyListView(mActivity,
                                        mListView, mAudioRecordParameter);
                            }
                        });

                break;
            case SELECT_CHANNEL_IN:
                final List<AudioChannelIn> listChannels = dbSource
                        .getAllAudioChannelIns(mAudioRecordParameter.getSource());

                cs = new CharSequence[listChannels.size()];
                checkedItem = 0;
                for (int i = 0; i < listChannels.size(); i++) {
                    AudioChannelIn channel = listChannels.get(i);
                    cs[i] = getActivity().getResources().getString(channel.getChannelNameResId());
                    if (channel.getChannelId() == mAudioRecordParameter.getChannel().getChannelId()) {
                        checkedItem = i;
                    }
                }

                builder.setSingleChoiceItems(cs, checkedItem,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                AudioChannelIn c = listChannels.get(which);
                                AudioSource s = mAudioRecordParameter.getSource();
                                List<AudioRecordParameter> params = lookupAudioRecordParameter(s, c);
                                if (params == null || params.isEmpty()) {
                                    return;
                                }

                                AudioRecordParameter p = params.get(0);

                                mAudioRecordParameter.setSource(s);
                                mAudioRecordParameter.setChannel(c);
                                mAudioRecordParameter.setEncoding(p.getEncoding());
                                mAudioRecordParameter.setSamplingRate(p.getSamplingRate());
                                mAudioRecordParameter.setMinBufferSize(p.getBufferSize());
                            }
                        });

                builder.setPositiveButton(getActivity().getResources().getString(R.string.text_ok),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UserInterfaceUtils.updateAudioSensorPropertyListView(mActivity,
                                        mListView, mAudioRecordParameter);
                            }
                        });

                break;
            case SELECT_ENCODING:
                final List<AudioEncoding> listEncodings = dbSource.getAllAudioEncodings(
                        mAudioRecordParameter.getSource(),
                        mAudioRecordParameter.getChannel());

                cs = new CharSequence[listEncodings.size()];
                checkedItem = 0;
                for (int i = 0; i < listEncodings.size(); i++) {
                    AudioEncoding encoding = listEncodings.get(i);
                    cs[i] = getActivity().getResources().getString(encoding.getEncodingNameResId());
                    if (encoding.getEncodingId() == mAudioRecordParameter.getEncoding()
                            .getEncodingId()) {
                        checkedItem = i;
                    }
                }

                builder.setSingleChoiceItems(cs, checkedItem,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                AudioEncoding e = listEncodings.get(which);
                                AudioSource s = mAudioRecordParameter.getSource();
                                AudioChannelIn c = mAudioRecordParameter.getChannel();
                                List<AudioRecordParameter> params = lookupAudioRecordParameter(s, c);
                                if (params == null || params.isEmpty()) {
                                    return;
                                }

                                AudioRecordParameter p = params.get(0);

                                mAudioRecordParameter.setSource(s);
                                mAudioRecordParameter.setChannel(c);
                                mAudioRecordParameter.setEncoding(e);
                                mAudioRecordParameter.setSamplingRate(p.getSamplingRate());
                                mAudioRecordParameter.setMinBufferSize(p.getBufferSize());
                            }
                        });

                builder.setPositiveButton(getActivity().getResources().getString(R.string.text_ok),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UserInterfaceUtils.updateAudioSensorPropertyListView(mActivity,
                                        mListView, mAudioRecordParameter);
                            }
                        });

                break;
            case SELECT_SAMPLING_RATE:
                final List<Integer> listSamplingRates = dbSource.getAllSamplingRates(
                        mAudioRecordParameter.getSource(),
                        mAudioRecordParameter.getChannel(),
                        mAudioRecordParameter.getEncoding());

                cs = new CharSequence[listSamplingRates.size()];
                checkedItem = 0;
                for (int i = 0; i < listSamplingRates.size(); i++) {
                    int samplingRate = listSamplingRates.get(i);
                    cs[i] = Integer.toString(samplingRate);
                    if (samplingRate == mAudioRecordParameter.getSamplingRate()) {
                        checkedItem = i;
                    }
                }

                builder.setSingleChoiceItems(cs, checkedItem,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                int samplingRate = listSamplingRates.get(which);
                                AudioSource s = mAudioRecordParameter.getSource();
                                AudioChannelIn c = mAudioRecordParameter.getChannel();
                                AudioEncoding e = mAudioRecordParameter.getEncoding();
                                List<AudioRecordParameter> params = lookupAudioRecordParameter(s,
                                        c, e);
                                if (params == null || params.isEmpty()) {
                                    return;
                                }

                                AudioRecordParameter p = params.get(0);

                                mAudioRecordParameter.setSource(s);
                                mAudioRecordParameter.setChannel(c);
                                mAudioRecordParameter.setEncoding(e);
                                mAudioRecordParameter.setSamplingRate(samplingRate);
                                mAudioRecordParameter.setMinBufferSize(p.getBufferSize());
                            }
                        });

                builder.setPositiveButton(getActivity().getResources().getString(R.string.text_ok),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UserInterfaceUtils.updateAudioSensorPropertyListView(mActivity,
                                        mListView, mAudioRecordParameter);
                            }
                        });

                break;

            case SELECT_MIN_BUFFER_SIZE:

                // TODO change it to slider/seek bar instead of list
                final int minBufferSize = dbSource.getMinBufferSize(
                        mAudioRecordParameter.getSource(), mAudioRecordParameter.getChannel(),
                        mAudioRecordParameter.getEncoding(),
                        mAudioRecordParameter.getSamplingRate());

                int maxMultiplier = 20;
                cs = new CharSequence[maxMultiplier];
                checkedItem = 0;
                for (int i = 0; i < maxMultiplier; i++) {
                    int bufferSize = (i + 1) * minBufferSize;
                    cs[i] = Integer.toString(bufferSize);
                    if (mAudioRecordParameter.getBufferSize() == bufferSize) {
                        checkedItem = i;
                    }
                }

                builder.setSingleChoiceItems(cs, checkedItem,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                int bufferSize = minBufferSize * (which + 1);
                                AudioSource s = mAudioRecordParameter.getSource();
                                AudioChannelIn c = mAudioRecordParameter.getChannel();
                                AudioEncoding e = mAudioRecordParameter.getEncoding();
                                int r = mAudioRecordParameter.getSamplingRate();

                                mAudioRecordParameter.setSource(s);
                                mAudioRecordParameter.setChannel(c);
                                mAudioRecordParameter.setEncoding(e);
                                mAudioRecordParameter.setSamplingRate(r);
                                mAudioRecordParameter.setMinBufferSize(bufferSize);
                            }
                        });

                builder.setPositiveButton(getActivity().getResources().getString(R.string.text_ok),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UserInterfaceUtils.updateAudioSensorPropertyListView(mActivity,
                                        mListView, mAudioRecordParameter);
                            }
                        });

                break;
        }
        dbSource.close();
        Dialog dialog = builder.create();

        return dialog;
    }

    private List<AudioRecordParameter> lookupAudioRecordParameter(AudioSource source) {
        if (mListAudioRecordParameters == null) {
            return null;
        }

        List<AudioRecordParameter> params = new ArrayList<AudioRecordParameter>();
        for (AudioRecordParameter p : mListAudioRecordParameters) {
            if (p.getSource().getSourceId() == source.getSourceId()) {
                params.add(p);
            }
        }
        return params;
    }

    private List<AudioRecordParameter> lookupAudioRecordParameter(AudioSource source,
            AudioChannelIn channel) {
        if (mListAudioRecordParameters == null) {
            return null;
        }

        List<AudioRecordParameter> params = new ArrayList<AudioRecordParameter>();
        for (AudioRecordParameter p : mListAudioRecordParameters) {
            if (p.getSource().getSourceId() == source.getSourceId()
                    && p.getChannel().getChannelId() == channel.getChannelId()) {
                params.add(p);
            }
        }
        return params;
    }

    private List<AudioRecordParameter> lookupAudioRecordParameter(AudioSource source,
            AudioChannelIn channel, AudioEncoding encoding) {
        if (mListAudioRecordParameters == null) {
            return null;
        }

        List<AudioRecordParameter> params = new ArrayList<AudioRecordParameter>();
        for (AudioRecordParameter p : mListAudioRecordParameters) {
            if (p.getSource().getSourceId() == source.getSourceId()
                    && p.getChannel().getChannelId() == channel.getChannelId()
                    && p.getEncoding().getEncodingId() == encoding.getEncodingId()) {
                params.add(p);
            }
        }
        return params;
    }
}




Java Source Code List

sysnetlab.android.sdc.datacollector.AndroidSensorEventListener.java
sysnetlab.android.sdc.datacollector.DataCollectionState.java
sysnetlab.android.sdc.datacollector.DateUtils.java
sysnetlab.android.sdc.datacollector.DeviceInformation.java
sysnetlab.android.sdc.datacollector.DropboxHelper.java
sysnetlab.android.sdc.datacollector.ExperimentManagerSingleton.java
sysnetlab.android.sdc.datacollector.ExperimentManager.java
sysnetlab.android.sdc.datacollector.ExperimentTime.java
sysnetlab.android.sdc.datacollector.Experiment.java
sysnetlab.android.sdc.datacollector.Note.java
sysnetlab.android.sdc.datacollector.StateTag.java
sysnetlab.android.sdc.datacollector.Tag.java
sysnetlab.android.sdc.datacollector.TaggingAction.java
sysnetlab.android.sdc.datacollector.TaggingState.java
sysnetlab.android.sdc.datastore.AbstractStore.java
sysnetlab.android.sdc.datastore.SimpleFileStore.java
sysnetlab.android.sdc.datastore.SimpleXmlFileStore.java
sysnetlab.android.sdc.datastore.StoreSingleton.java
sysnetlab.android.sdc.loaders.ExperimentListLoader.java
sysnetlab.android.sdc.sensor.AbstractSensor.java
sysnetlab.android.sdc.sensor.AndroidSensor.java
sysnetlab.android.sdc.sensor.SensorDiscoverer.java
sysnetlab.android.sdc.sensor.SensorProperty.java
sysnetlab.android.sdc.sensor.audio.AudioChannelIn.java
sysnetlab.android.sdc.sensor.audio.AudioEncoding.java
sysnetlab.android.sdc.sensor.audio.AudioRecordParameter.java
sysnetlab.android.sdc.sensor.audio.AudioRecordSettingDBHelper.java
sysnetlab.android.sdc.sensor.audio.AudioRecordSettingDataSource.java
sysnetlab.android.sdc.sensor.audio.AudioSensorHelper.java
sysnetlab.android.sdc.sensor.audio.AudioSensor.java
sysnetlab.android.sdc.sensor.audio.AudioSource.java
sysnetlab.android.sdc.sensor.audio.WaveHeader.java
sysnetlab.android.sdc.sensor.camera.CameraHelper.java
sysnetlab.android.sdc.sensor.camera.CameraSensor.java
sysnetlab.android.sdc.services.RunExperimentService.java
sysnetlab.android.sdc.ui.AudioSensorProbingActivity.java
sysnetlab.android.sdc.ui.CreateExperimentActivity.java
sysnetlab.android.sdc.ui.FragmentActivityBase.java
sysnetlab.android.sdc.ui.GestureEventListener.java
sysnetlab.android.sdc.ui.SensorDataCollectorActivity.java
sysnetlab.android.sdc.ui.TaggingGridView.java
sysnetlab.android.sdc.ui.UserInterfaceUtils.java
sysnetlab.android.sdc.ui.ViewExperimentActivity.java
sysnetlab.android.sdc.ui.adapters.DataStoreListAdapter.java
sysnetlab.android.sdc.ui.adapters.ExperimentListAdapter.java
sysnetlab.android.sdc.ui.adapters.OperationAdapter.java
sysnetlab.android.sdc.ui.adapters.SensorListAdapter.java
sysnetlab.android.sdc.ui.adapters.SensorPropertyListAdapter.java
sysnetlab.android.sdc.ui.adapters.TagListAdapter.java
sysnetlab.android.sdc.ui.adapters.TaggingTagListAdapter.java
sysnetlab.android.sdc.ui.fragments.AudioSensorSetupDialogFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentDataStoreFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentEditNotesFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentEditTagsFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentListFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentRunFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentRunTaggingFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentSensorListFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentSensorSelectionFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentSensorSetupFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentSetupFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentViewFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentViewNotesFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentViewSensorDataFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentViewSensorListFragment.java
sysnetlab.android.sdc.ui.fragments.ExperimentViewTagsFragment.java
sysnetlab.android.sdc.ui.fragments.FragmentUtils.java