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

Commit

Permalink
Updated layout for two columns in landscape.
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriSko committed Oct 20, 2016
1 parent c0f2750 commit bd71465
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 141 deletions.
20 changes: 11 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 25
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "cz.jiriskorpil.amixerwebui"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "0.1"
targetSdkVersion 25
versionCode 2
versionName "0.1.1"
}
buildTypes {
release {
Expand All @@ -19,11 +19,13 @@ android {
}
}

ext.supportLibraryVersion = '25.0.+'
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
compile 'com.android.support:cardview-v7:23.2.1'
compile "com.android.support:appcompat-v7:$supportLibraryVersion"
compile "com.android.support:design:$supportLibraryVersion"
compile "com.android.support:support-v4:$supportLibraryVersion"
compile "com.android.support:cardview-v7:$supportLibraryVersion"
compile "com.android.support:recyclerview-v7:$supportLibraryVersion"
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
android:theme="@style/AppTheme">
<activity
android:name=".activity.MainActivity"
android:configChanges="orientation"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -28,23 +30,27 @@
*/
public class DataHandler
{
Context context;
OnFailListener mOnFailListener;
private Context context;
private OnFailListener mOnFailListener;

/* GUI components */
ListView listView;
SwipeRefreshLayout swipeRefreshLayout;
List<ControlContainer> controls;
private RecyclerView listView;
private SwipeRefreshLayout swipeRefreshLayout;
private List<ControlContainer> controls;

/**
* @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, ListView resultListView, SwipeRefreshLayout swipeRefreshLayout)
public DataHandler(Context context, RecyclerView resultListView, SwipeRefreshLayout swipeRefreshLayout)
{
this.context = context;

this.listView = resultListView;
this.listView.setHasFixedSize(true);
this.listView.setLayoutManager(new GridLayoutManager(context, context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ? 2 : 1, GridLayoutManager.VERTICAL, false));

this.swipeRefreshLayout = swipeRefreshLayout;
this.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
{
Expand Down Expand Up @@ -108,8 +114,7 @@ public void displayData(JSONArray jsonArray)
controls = parseControls(jsonArray);
}

ControlContainerAdapter adapter = new ControlContainerAdapter(context, R.layout.control_row, controls);
listView.setAdapter(adapter);
listView.setAdapter(new ControlContainerAdapter(context, controls));
swipeRefreshLayout.setRefreshing(false);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package cz.jiriskorpil.amixerwebui.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import cz.jiriskorpil.amixerwebui.R;

Expand All @@ -31,7 +33,7 @@ protected void onCreate(Bundle savedInstanceState)
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

dataHandler = (new DataHandler(this,
(ListView) findViewById(R.id.controls_list),
(RecyclerView) findViewById(R.id.controls_list),
(SwipeRefreshLayout) findViewById(R.id.swipe_container)))
.setOnFailListener(new DataHandler.OnFailListener()
{
Expand All @@ -57,6 +59,18 @@ public void onClick(View v)
lastUrl = dataHandler.getBaseUrl();
}

/**
* {@inheritDoc}
*/
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);

GridLayoutManager manager = (GridLayoutManager) ((RecyclerView) findViewById(R.id.controls_list)).getLayoutManager();
manager.setSpanCount(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ? 2 : 1);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
Expand All @@ -26,71 +26,61 @@
/**
* List adapter for control container.
*/
public class ControlContainerAdapter extends ArrayAdapter<ControlContainer>
public class ControlContainerAdapter extends RecyclerView.Adapter<ControlContainerAdapter.ControlContainerHolder>
{
Context context;
int layoutResourceId;
List<ControlContainer> data = null;
private Context context;
private List<ControlContainer> data = null;

private static final float FULL_VISIBILITY = 1;
private static final float LOW_VISIBILITY = (float) 0.4;

/**
*
* @param context The context to use. Usually {@link android.app.Activity} object.
* @param layoutResourceId GUI element for rendering
* @param data list of controls
*/
public ControlContainerAdapter(Context context, int layoutResourceId, List<ControlContainer> data)
public ControlContainerAdapter(Context context, List<ControlContainer> data)
{
super(context, layoutResourceId, data);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
public int getItemCount()
{
View row = convertView;
ControlContainerHolder holder;

if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);

holder = new ControlContainerHolder();
holder.enabled = (Switch) row.findViewById(R.id.enabled);
holder.name = (TextView) row.findViewById(R.id.name);
holder.bind_sliders = (CheckBox) row.findViewById(R.id.bind_sliders);
holder.source_list = (RadioGroup) row.findViewById(R.id.source_list);
holder.channels_list = (LinearLayout) row.findViewById(R.id.channels_list);
return data.size();
}

row.setTag(holder);
@Override
public void onBindViewHolder(ControlContainerHolder holder, int i)
{
ControlContainer container = data.get(i);

} else {
holder = (ControlContainerHolder) row.getTag();
}
setupCard(container, holder);
}

setupCard(data.get(position), holder, row);
@Override
public ControlContainerHolder onCreateViewHolder(ViewGroup viewGroup, int i)
{
View itemView = LayoutInflater
.from(viewGroup.getContext())
.inflate(R.layout.control_row, viewGroup, false);

return row;
return new ControlContainerHolder(itemView);
}

/**
* Setups control card.
*
* @param container control container
* @param holder control container holder
* @param row GUI row View
*/
private void setupCard(ControlContainer container, ControlContainerHolder holder, View row)
private void setupCard(ControlContainer container, ControlContainerHolder holder)
{
setupCardHeader(container, holder);

setupCardBodySource(container, holder);
setupCardBodyChannels(container, holder, row);
setupCardBodyChannels(container, holder);

setCardBodyAlpha(container, holder);
}
Expand Down Expand Up @@ -127,7 +117,7 @@ private void toggleControl(final ControlContainer container, final ControlContai
{
container.getSwitch().setChecked(isChecked);
setCardBodyAlpha(container, holder);
new ToggleControlHttpRequestTask(getContext(), ((MainActivity) getContext()).getDataHandler().getBaseUrl())
new ToggleControlHttpRequestTask(holder.getView().getContext(), ((MainActivity) holder.getView().getContext()).getDataHandler().getBaseUrl())
.execute(String.valueOf(container.getSwitch().getId()), container.getSwitch().isChecked() ? "1" : "0");
}

Expand Down Expand Up @@ -174,8 +164,8 @@ public void onClick(View v)
@Override
public boolean onLongClick(View v)
{
Toast.makeText(getContext(),
getContext().getResources().getString(holder.bind_sliders.isChecked() ? R.string.bound_sliders : R.string.unbounded_sliders),
Toast.makeText(holder.getView().getContext(),
holder.getView().getContext().getResources().getString(holder.bind_sliders.isChecked() ? R.string.bound_sliders : R.string.unbounded_sliders),
Toast.LENGTH_LONG).show();
return true;
}
Expand All @@ -200,22 +190,22 @@ private void setupCardBodySource(final ControlContainer container, final Control
holder.source_list.check(0);
for (int i = 0; i < container.getSource().getItems().length; i++)
{
final RadioButton button = new RadioButton(getContext());
button.setId(i);
final RadioButton button = new RadioButton(holder.getView().getContext());
button.setId(i + 1);
button.setText(container.getSource().getItems()[i]);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
container.getSource().setValue(holder.source_list.getCheckedRadioButtonId());
new ChangeSourceHttpRequestTask(getContext(), ((MainActivity) getContext()).getDataHandler().getBaseUrl())
new ChangeSourceHttpRequestTask(holder.getView().getContext(), ((MainActivity) holder.getView().getContext()).getDataHandler().getBaseUrl())
.execute(String.valueOf(container.getSource().getId()), String.valueOf(button.getId()));
}
});
holder.source_list.addView(button);
}
holder.source_list.check(container.getSource().getValue());
holder.source_list.check(container.getSource().getValue() + 1);
} else {
holder.source_list.setVisibility(View.INVISIBLE);
}
Expand All @@ -226,16 +216,15 @@ public void onClick(View v)
*
* @param container control container
* @param holder control container holder
* @param row GUI row View
*/
private void setupCardBodyChannels(final ControlContainer container, final ControlContainerHolder holder, View row)
private void setupCardBodyChannels(final ControlContainer container, final ControlContainerHolder holder)
{
holder.channels_list.removeAllViews();
if (container.hasVolumeControl())
{
for (int i = 0; i < container.getVolume().getChannels().length; i++)
{
LinearLayout channelLayout = (LinearLayout) row.findViewById(R.id.control_channel);
LinearLayout channelLayout = (LinearLayout) holder.getView().findViewById(R.id.control_channel);
View channelView = ((Activity) context).getLayoutInflater().inflate(R.layout.control_channel, channelLayout, false);
holder.channels_list.addView(channelView, i);

Expand All @@ -248,14 +237,14 @@ private void setupCardBodyChannels(final ControlContainer container, final Contr
channel_name.setText(channel.getName());
channel_volume_seek_bar.setMax(channel.getControl().getMax());
channel_volume_seek_bar.setProgress(channel.getValue());
channel_volume.setText(getContext().getString(R.string.channel_value, Math.round(100 * (double) channel.getValue() / channel.getControl().getMax())));
channel_volume.setText(holder.getView().getContext().getString(R.string.channel_value, Math.round(100 * (double) channel.getValue() / channel.getControl().getMax())));

channel_volume_seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
String progressMsg = getContext().getString(R.string.channel_value, Math.round(100 * progress / seekBar.getMax()));
String progressMsg = holder.getView().getContext().getString(R.string.channel_value, Math.round(100 * progress / seekBar.getMax()));
if (fromUser && holder.bind_sliders.isChecked()) {
for (int i = 0; i < container.getVolume().getChannels().length; i++) {
if (!holder.channels_list.getChildAt(i).equals(seekBar))
Expand Down Expand Up @@ -292,7 +281,7 @@ public void onStopTrackingTouch(SeekBar seekBar)
}
volumes += String.valueOf(container.getVolume().getChannels()[i].getValue());
}
new ChangeVolumeHttpRequestTask(getContext(), ((MainActivity) getContext()).getDataHandler().getBaseUrl())
new ChangeVolumeHttpRequestTask(holder.getView().getContext(), ((MainActivity) holder.getView().getContext()).getDataHandler().getBaseUrl())
.execute(String.valueOf(container.getVolume().getId()), volumes);
}
});
Expand All @@ -303,12 +292,31 @@ public void onStopTrackingTouch(SeekBar seekBar)
/**
* Holds ControlContainer GUI controls.
*/
static class ControlContainerHolder
static class ControlContainerHolder extends RecyclerView.ViewHolder
{
View v;

Switch enabled;
TextView name;
CheckBox bind_sliders;
RadioGroup source_list;
LinearLayout channels_list;

ControlContainerHolder(View v)
{
super(v);
this.v = v;
enabled = (Switch) v.findViewById(R.id.enabled);
name = (TextView) v.findViewById(R.id.name);
bind_sliders = (CheckBox) v.findViewById(R.id.bind_sliders);
source_list = (RadioGroup) v.findViewById(R.id.source_list);
channels_list = (LinearLayout) v.findViewById(R.id.channels_list);
}


public View getView()
{
return v;
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
tools:context=".activity.MainActivity"
tools:showIn="@layout/activity_main">

<ListView
<android.support.v7.widget.RecyclerView
android:id="@+id/controls_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:padding="@dimen/controlCardMargin"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"
android:divider="@android:color/transparent"
Expand Down
Loading

0 comments on commit bd71465

Please sign in to comment.