Android Open Source - brutefir-remote File3






From Project

Back to project page brutefir-remote.

License

The source code is released under:

GNU General Public License

If you think the Android project brutefir-remote 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) 2011 Victor Su//from  w  ww  .ja  v  a  2  s.co  m
 *
 * This program is open source. For license terms, see the LICENSE file.
 *
 */

package com.vsu.bruteremote;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class File3 extends Activity {
    // Debugging
    private static final String TAG = "File3";
    private static final boolean D = false;

    // The maximum level in dB.
    private static final int LEVEL_MAX_DB = 20;

    // The number of adjustment steps per dB for level.
    private static final int LEVEL_STEPS_PER_DB = 10;

    // The seekbar value range for level.
    private static final int LEVEL_SEEKBAR_RANGE = 2 * LEVEL_MAX_DB * LEVEL_STEPS_PER_DB;

    // The most recently used directory path.
    private static final String KEY_PREF_DIRECTORY = "file3_directory";

    // A reference to the enable button.
    private ToggleButton mButtonEnable;

    // A reference to the level seekbar.
    private SeekBar mSeekBarLevel;

    // A reference to shared preferences.
    private SharedPreferences mPreferences;

    /**
     * Called when the activity is created.
     * @param savedInstanceState  If the activity is being re-created from a previous
     *                            saved state, this is the state.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (D) Log.e(TAG, "+ ON CREATE +");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.file3);
        mPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        mButtonEnable = (ToggleButton)findViewById(R.id.toggleButtonEnableFile3);
        mButtonEnable.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (BruteRemote.mIO.checkConnection()) {
                    BruteRemote.mIO.setEnable(IO.CMD_FILE3_ENABLE, mButtonEnable.isChecked());
                }
            }
        });

        final TextView textViewLevel = (TextView)findViewById(R.id.textViewLevelFile3);
        mSeekBarLevel = (SeekBar)findViewById(R.id.seekBarLevelFile3);

        mSeekBarLevel.setMax(LEVEL_SEEKBAR_RANGE);
        mSeekBarLevel.setProgress(toProgress(0));

        mSeekBarLevel.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                double level = (double)toLevel(progress) / (double)LEVEL_STEPS_PER_DB;
                textViewLevel.setText(Double.toString(level) + " dB");

                if (fromUser) {
                    if (BruteRemote.mIO.checkConnection()) {
                        BruteRemote.mIO.setLevel(IO.CMD_FILE3_LEVEL, toLevel(progress));
                    }
                }
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        final Button buttonBrowse = (Button)findViewById(R.id.buttonBrowseFile3);
        buttonBrowse.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String directory = mPreferences.getString(KEY_PREF_DIRECTORY, "");

                Intent intent = new Intent(v.getContext(), FileBrowser.class);
                intent.putExtra(FileBrowser.EXTRA_STARTPATH, directory);

                startActivityForResult(intent, FileBrowser.REQUEST_SELECT_FILE);
            }
        });

        final Button buttonDelete = (Button)findViewById(R.id.buttonDeleteFile3);
        buttonDelete.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                TextView textViewFilename =
                        (TextView)findViewById(R.id.textViewFilenameFile3);

                if (!textViewFilename.getText().equals("")) {
                    new AlertDialog.Builder(v.getContext())
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setTitle(R.string.title_file_confirm)
                    .setMessage(R.string.message_delete_file)
                    .setPositiveButton(R.string.button_ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                if (BruteRemote.mIO.checkConnection()) {
                                    BruteRemote.mIO.setFilename(IO.CMD_FILE3_FILENAME, IO.FILENAME_NONE);
                                refreshData();
                            }
                        }
                        }
                    )
                    .setNegativeButton(R.string.button_cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                            }
                        }
                    )
                    .show();
                }
            }
        });
    }

    /**
     * Called when the activity is resumed.
     */
    @Override
    public synchronized void onResume() {
        if (D) Log.e(TAG, "+ ON RESUME +");

        super.onResume();
        refreshData();
    }

    /**
     * Called when an activity returns with result.
     * @param requestCode  The original code passed to the activity.
     * @param resultCode   The result code returned from the activity.
     * @param data         The data returned from the activity.
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == FileBrowser.REQUEST_SELECT_FILE) {
            if (data.hasExtra(FileBrowser.EXTRA_FILENAME)) {
                final String filename = data.getExtras().getString(FileBrowser.EXTRA_FILENAME);
                final String parentPath = data.getExtras().getString(FileBrowser.EXTRA_PARENTPATH);
                final String fullPath = data.getExtras().getString(FileBrowser.EXTRA_FULLPATH);

                if ((filename != null) && !filename.equals("")) {
                    String message =
                        getResources().getString(R.string.message_open_file) + " " + filename +  "?";

                    new AlertDialog.Builder(this)
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setTitle(R.string.title_file_confirm)
                        .setMessage(message)
                        .setPositiveButton(R.string.button_ok,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    // Save directory to preferences
                                    SharedPreferences.Editor editor = mPreferences.edit();
                                    editor.putString(KEY_PREF_DIRECTORY, parentPath);
                                    editor.commit();

                                    // Set filename and refresh page
                                    if (BruteRemote.mIO.checkConnection()) {
                                        if (BruteRemote.mIO.setFilename(IO.CMD_FILE3_FILENAME, fullPath)) {
                                          refreshData();
                                        }
                                        else {
                                            Resources res = getResources();

                                            new AlertDialog.Builder(File3.this)
                                          .setIcon(android.R.drawable.ic_dialog_alert)
                                        .setTitle(res.getString(R.string.title_invalid_file))
                                        .setMessage(res.getString(R.string.message_invalid_file))
                                        .setNegativeButton(res.getString(R.string.button_close), null)
                                        .show();
                                        }
                                    }
                                }
                            }
                        )
                        .setNegativeButton(R.string.button_cancel,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                }
                            }
                        )
                        .show();
                }
                else {
                    if (BruteRemote.mIO.checkConnection()) {
                        BruteRemote.mIO.setFilename(IO.CMD_FILE3_FILENAME, IO.FILENAME_NONE);
                    refreshData();
                }
            }
        }
    }
    }

    /**
     * Translate the level value to seekbar progress value.
     * The zero level value is the seekbar range / 2.
     * @param level  The level value.
     * @returns      The seekbar progress value.
     */
    private int toProgress(int level) {
        return level + (LEVEL_SEEKBAR_RANGE / 2);
    }

    /**
     * Translate the seekbar progress value to level value.
     * The zero level value is the seekbar range / 2.
     * @param progress  The seekbar progress value.
     * @returns         The level value.
     */
    private int toLevel(int progress) {
        return progress - (LEVEL_SEEKBAR_RANGE / 2);
    }

    /**
     * Retrieves data and refreshes the view.
     */
    private void refreshData() {
        if (BruteRemote.mIO.checkConnection()) {
            mButtonEnable.setChecked(BruteRemote.mIO.getEnable(IO.CMD_FILE3_ENABLE));
            mSeekBarLevel.setProgress(toProgress(BruteRemote.mIO.getLevel(IO.CMD_FILE3_LEVEL)));

            TextView textViewFilename = (TextView)findViewById(R.id.textViewFilenameFile3);
            textViewFilename.setText(BruteRemote.mIO.getMetadata(IO.CMD_FILE3_FILENAME));

            TextView textViewMetadata = (TextView)findViewById(R.id.textViewMetadataFile3);
            textViewMetadata.setText(BruteRemote.mIO.getMetadata(IO.CMD_FILE3_METADATA));
    } else {
        Resources res = getResources();

          new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle(res.getString(R.string.title_connection_error))
        .setMessage(res.getString(R.string.message_connection_error))
        .setNegativeButton(res.getString(R.string.button_close), null)
        .show();    
    }
    }
}




Java Source Code List

com.vsu.bruteremote.BruteRemote.java
com.vsu.bruteremote.Equalizer.java
com.vsu.bruteremote.File1.java
com.vsu.bruteremote.File2.java
com.vsu.bruteremote.File3.java
com.vsu.bruteremote.FileBrowserItem.java
com.vsu.bruteremote.FileBrowser.java
com.vsu.bruteremote.IO.java
com.vsu.bruteremote.Preferences.java