Skip to content
This repository has been archived by the owner on Aug 4, 2024. It is now read-only.

Commit

Permalink
Added equalizer support.
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriSko committed Dec 8, 2016
1 parent 75fc725 commit f1774b4
Show file tree
Hide file tree
Showing 34 changed files with 764 additions and 100 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Android client for control ALSA volume on remote server using [ALSA Mixer WebUI]

## Usage

- Install from [latest APK](https://github.com/JiriSko/amixer-webui-android/releases/download/v0.2.2/amixer-webui-v0.2.2.apk), or
- Install from [latest APK](https://github.com/JiriSko/amixer-webui-android/releases/download/v0.3.0/amixer-webui-v0.3.0.apk), or
- Build from source code

## License
Expand Down
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "cz.jiriskorpil.amixerwebui"
minSdkVersion 15
targetSdkVersion 25
versionCode 5
versionName "0.2.2"
versionCode 6
versionName "0.3.0"
}
buildTypes {
release {
Expand All @@ -28,4 +28,5 @@ dependencies {
compile "com.android.support:support-v4:$supportLibraryVersion"
compile "com.android.support:cardview-v7:$supportLibraryVersion"
compile "com.android.support:recyclerview-v7:$supportLibraryVersion"
compile 'com.h6ah4i.android.widget.verticalseekbar:verticalseekbar:0.7.0'
}
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity" />
</activity>
<activity
android:name=".activity.EqualizerActivity"
android:label="@string/equalizer"
android:parentActivityName=".activity.MainActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.MainActivity" />
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatDelegate;
Expand Down Expand Up @@ -45,6 +46,7 @@ public void setSupportActionBar(@Nullable Toolbar toolbar)
getDelegate().setSupportActionBar(toolbar);
}

@NonNull
@Override
public MenuInflater getMenuInflater()
{
Expand Down
132 changes: 58 additions & 74 deletions app/src/main/java/cz/jiriskorpil/amixerwebui/activity/DataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@
import java.util.List;

import cz.jiriskorpil.amixerwebui.R;
import cz.jiriskorpil.amixerwebui.control.Control;
import cz.jiriskorpil.amixerwebui.control.ControlContainer;
import cz.jiriskorpil.amixerwebui.control.ControlContainerAdapter;
import cz.jiriskorpil.amixerwebui.control.ControlContainerType;
import cz.jiriskorpil.amixerwebui.control.ControlFactory;
import cz.jiriskorpil.amixerwebui.control.mixer.MixerControl;
import cz.jiriskorpil.amixerwebui.control.ControlParser;
import cz.jiriskorpil.amixerwebui.control.Equalizer;
import cz.jiriskorpil.amixerwebui.task.AsyncHttpRequestTask;
import cz.jiriskorpil.amixerwebui.task.ChangeCardHttpRequestTask;
import cz.jiriskorpil.amixerwebui.task.RetrieveCardHttpRequestTask;
import cz.jiriskorpil.amixerwebui.task.RetrieveCardsHttpRequestTask;
import cz.jiriskorpil.amixerwebui.task.RetrieveControlsHttpRequestTask;
import cz.jiriskorpil.amixerwebui.task.RetrieveEqualizerHttpRequestTask;
import cz.jiriskorpil.amixerwebui.task.RetrieveHostnameHttpRequestTask;

/**
Expand All @@ -48,18 +47,19 @@ public class DataHandler
/* GUI components */
private RecyclerView listView;
private SwipeRefreshLayout swipeRefreshLayout;
private List<ControlContainer> controls;

private boolean downloadEnabled = true;
private String lastUrl = "";
private String cardId = "";

private Equalizer equalizer;

/**
* @param context The context to use. Usually {@link android.app.Activity} object.
* @param resultListView ListView for results
* @param swipeRefreshLayout layout for results
*/
public DataHandler(Context context, RecyclerView resultListView, SwipeRefreshLayout swipeRefreshLayout)
DataHandler(Context context, RecyclerView resultListView, SwipeRefreshLayout swipeRefreshLayout)
{
this.context = context;

Expand All @@ -82,7 +82,7 @@ public void onRefresh()
* Creates request to download data
* @return self
*/
public DataHandler download()
DataHandler download()
{
if (!downloadEnabled) return this;
downloadEnabled = false;
Expand All @@ -93,48 +93,58 @@ public DataHandler download()
public void run()
{
swipeRefreshLayout.setRefreshing(true);
new RetrieveControlsHttpRequestTask(context, getBaseUrl(), new AsyncHttpRequestTask.OnFinishListener()
new RetrieveControlsHttpRequestTask(context, getBaseUrl(context), new AsyncHttpRequestTask.OnFinishListener()
{
@Override
public void onFinish(String result)
{
try {
try
{
downloadEnabled = true;
lastUrl = getBaseUrl();
lastUrl = getBaseUrl(context);
displayData(result.equals("") ? null : new JSONArray(result));
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
}
}
}).execute();

if (!lastUrl.equals(getBaseUrl())) {
if (!lastUrl.equals(getBaseUrl(context)))
{
cardId = "";
setupHostname();
setupSoundCards();
}
setupEqualizer();
}
});
return this;
}

private void setupSoundCards()
private void setupHostname()
{
final MainActivity activity = (MainActivity) context;
activity.getSupportActionBar().setSubtitle("");
if (activity.getSupportActionBar() != null) {
activity.getSupportActionBar().setSubtitle("");
}

new RetrieveHostnameHttpRequestTask(context, getBaseUrl(), new AsyncHttpRequestTask.OnFinishListener() {
new RetrieveHostnameHttpRequestTask(context, getBaseUrl(context), new AsyncHttpRequestTask.OnFinishListener() {
@Override
public void onFinish(String result) {
activity.getSupportActionBar().setSubtitle(result);
}
}).execute();
}

final MenuItem soundCard = activity.menu.findItem(R.id.action_card);
private void setupSoundCards()
{
final MenuItem soundCard = ((MainActivity) context).menu.findItem(R.id.action_card);
soundCard.setVisible(false);
new RetrieveCardsHttpRequestTask(context, getBaseUrl(), new AsyncHttpRequestTask.OnFinishListener() {
new RetrieveCardsHttpRequestTask(context, getBaseUrl(context), new AsyncHttpRequestTask.OnFinishListener() {
@Override
public void onFinish(String result) {
try {
try
{
JSONObject cards = result.equals("[]") ? new JSONObject() : (JSONObject) (new JSONArray("[" + result + "]")).get(0);

if (cards.length() > 1) {
Expand Down Expand Up @@ -162,7 +172,7 @@ public void onItemSelected(AdapterView<?> adapterView, View view, final int i, l
return;
}

(new ChangeCardHttpRequestTask(context, getBaseUrl(), new AsyncHttpRequestTask.OnFinishListener() {
(new ChangeCardHttpRequestTask(context, getBaseUrl(context), new AsyncHttpRequestTask.OnFinishListener() {
@Override
public void onFinish(String result) {
download();
Expand All @@ -176,7 +186,7 @@ public void onNothingSelected(AdapterView<?> adapterView) {
}
});

new RetrieveCardHttpRequestTask(context, getBaseUrl(), new AsyncHttpRequestTask.OnFinishListener() {
new RetrieveCardHttpRequestTask(context, getBaseUrl(context), new AsyncHttpRequestTask.OnFinishListener() {
@Override
public void onFinish(String result) {
cardId = result;
Expand All @@ -191,7 +201,28 @@ public void onFinish(String result) {
}).execute();
}

public DataHandler setOnFailListener(OnFailListener listener)
void setupEqualizer()
{
final MainActivity activity = (MainActivity) context;

new RetrieveEqualizerHttpRequestTask(context, getBaseUrl(context), new AsyncHttpRequestTask.OnFinishListener() {
@Override
public void onFinish(String result) {
try
{
JSONArray data = new JSONArray(result);
if (data.length() > 0) {
activity.menu.findItem(R.id.action_equalizer).setVisible(true);
equalizer = new Equalizer(result);
}
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
}
}
}).execute();
}

DataHandler setOnFailListener(OnFailListener listener)
{
mOnFailListener = listener;
return this;
Expand All @@ -202,7 +233,7 @@ public DataHandler setOnFailListener(OnFailListener listener)
*
* @param jsonArray JSON array representing controls retrieved from server
*/
public void displayData(JSONArray jsonArray)
private void displayData(JSONArray jsonArray)
{
List<ControlContainer> controls = new ArrayList<>();

Expand All @@ -213,78 +244,31 @@ public void displayData(JSONArray jsonArray)
cardId = "";
}
} else {
controls = parseControls(jsonArray);
controls = ControlParser.parse(jsonArray);
}

listView.setAdapter(new ControlContainerAdapter(context, controls));
swipeRefreshLayout.setRefreshing(false);
}

/**
* Parses controls out of JSON array.
*
* @param jsonArray JSON array representing controls retrieved from server
* @return list of control containers
*/
private List<ControlContainer> parseControls(JSONArray jsonArray)
{
Control control;
controls = new ArrayList<>();

try {
for (int i = 0; i < jsonArray.length(); i++)
{
control = ControlFactory.createControl(jsonArray.getJSONObject(i));
if (control instanceof MixerControl) {
getControlContainer(control.getName()).addControl(control);
}/* else {
// not supported (yet?)
}*/
}
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
}
return controls;
}

private ControlContainer getControlContainer(String controlName)
{
int index = findControlContainer(controlName);

if (index == -1) // if doesn't exists then create new container
{
controls.add(new ControlContainer(controlName));
index = controls.size() - 1;
}

return controls.get(index);
}

private int findControlContainer(String controlName)
{
for (int i = 0; i < controls.size(); i++) {
if (controls.get(i).getName().equals(ControlContainerType.getContainerName(controlName))) {
return i;
}
}
return -1;
}

/**
* Returns base url to server (http://<ipAddress>:<port>)
*
* @return server url
*/
public String getBaseUrl()
public static String getBaseUrl(Context context)
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String ipAddress = preferences.getString("ip_address", context.getResources().getString(R.string.pref_default_ip_address));
String port = preferences.getString("port", context.getResources().getString(R.string.pref_default_port));
return "http://" + ipAddress + ":" + port;
}

public Equalizer getEqualizer() {
return equalizer;
}

public interface OnFailListener
interface OnFailListener
{
/**
* Called when downloading failed.
Expand Down
Loading

0 comments on commit f1774b4

Please sign in to comment.