more bug fixing

moved rate limiting to another class
navigation header is close to being functional
This commit is contained in:
Giovanni Harting
2015-10-31 22:30:25 +01:00
parent ce198af0ef
commit 2211186d9f
12 changed files with 202 additions and 78 deletions

View File

@@ -41,6 +41,7 @@ import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;
import com.idlegandalf.ledd.callbacks.RecieveColorCallback;
import com.idlegandalf.ledd.callbacks.StripesCallback;
@@ -49,6 +50,7 @@ import com.idlegandalf.ledd.components.LedDDaemon;
import com.idlegandalf.ledd.components.LedStripe;
import com.idlegandalf.ledd.fragments.AddStripeDialog;
import com.idlegandalf.ledd.helper.LedDHelper;
import com.idlegandalf.ledd.utils.RateLimiter;
import com.larswerkman.holocolorpicker.ColorPicker;
import com.larswerkman.holocolorpicker.SaturationBar;
import com.larswerkman.holocolorpicker.ValueBar;
@@ -58,6 +60,7 @@ import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import hugo.weaving.DebugLog;
public class ColorActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
@@ -75,16 +78,18 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
ValueBar valueBar;
@Bind(R.id.switch_onoff)
Switch aSwitch;
TextView mStripeName;
TextView mStripeMapping;
TextView mStripeType;
TextView mStripeHex;
private ActionBarDrawerToggle mDrawerToggle;
private refreshDaemonsListener daemonsListener;
private List<LedStripe> ledStripes;
private LedStripe mCurrentStripe;
private boolean fromOnCreate = true;
private boolean autoColorSet = false;
private double rate = 5.0;
private double per = 100.0;
private double allowance = rate;
private long last_check = System.currentTimeMillis();
private RateLimiter limiter;
private View headerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -92,6 +97,17 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
setContentView(R.layout.activity_color);
ButterKnife.bind(this);
// workaround for issue #190226 in design libary
headerLayout = navigationView.inflateHeaderView(R.layout.navigation_header);
mStripeName = ButterKnife.findById(headerLayout, R.id.nvh_name);
System.out.println("name: " + mStripeName.getText().toString());
mStripeType = ButterKnife.findById(headerLayout, R.id.nvh_type);
System.out.println("type: " + mStripeType.getText().toString());
mStripeHex = ButterKnife.findById(headerLayout, R.id.nvh_hex_color);
System.out.println("hex: " + mStripeHex.getText().toString());
mStripeMapping = ButterKnife.findById(headerLayout, R.id.nvh_mapping);
System.out.println("mapping: " + mStripeMapping.getText().toString());
// check for connectivity
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
@@ -104,6 +120,8 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
}).show();
}
limiter = new RateLimiter(5.0, 100.0);
colorPicker.addSaturationBar(saturationBar);
colorPicker.addValueBar(valueBar);
colorPicker.setShowOldCenterColor(false);
@@ -111,8 +129,9 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
colorPicker.setOnColorChangedListener(new ColorPicker.OnColorChangedListener() {
@Override
public void onColorChanged(int i) {
if (mCurrentStripe != null && isRateAcceptable() && aSwitch.isChecked() && !autoColorSet) {
if (mCurrentStripe != null && limiter.check() && aSwitch.isChecked() && !autoColorSet) {
mCurrentStripe.setColor(i);
mStripeHex.setText(String.format("#%06X", (0xFFFFFF & i)));
}
}
});
@@ -186,8 +205,12 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
if (mCurrentStripe != null) {
if (!checked) {
HSV nHSV = mCurrentStripe.getColor();
nHSV.setValue(0.0);
mCurrentStripe.setColor(nHSV);
if (nHSV != null) {
nHSV.setValue(0.0);
mCurrentStripe.setColor(nHSV);
} else {
mCurrentStripe.setColor(Color.BLACK);
}
//colorPicker.setColor(Color.HSVToColor(new float[]{(float) nHSV.getHue(), (float) nHSV.getSaturation(), (float) nHSV.getValue()}));
} else {
mCurrentStripe.setColor(colorPicker.getColor());
@@ -339,8 +362,13 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
});
}
@DebugLog
private void selectStripe(LedStripe stripe) {
mCurrentStripe = stripe;
mStripeName.setText(stripe.getName());
System.out.println("name: " + mStripeName.getText().toString());
mStripeMapping.setText(String.format("R: %d - G: %d - B: %d", stripe.getChannelRed(), stripe.getChannelGreen(), stripe.getChannelBlue()));
headerLayout.invalidate();
mDrawerLayout.closeDrawer(Gravity.LEFT);
toolbar.setTitle(stripe.getName());
@@ -368,33 +396,16 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
@Override
public void onRecievFailed(int code, String msg) {
autoColorSet = false;
}
@Override
public void onConnectionFailed(String message) {
autoColorSet = false;
}
});
}
private boolean isRateAcceptable() {
long current = System.currentTimeMillis();
long time_passed = current - last_check;
last_check = current;
allowance += time_passed * (rate / per);
if (allowance > rate) {
allowance = rate; // throttle
}
if (allowance < 1.0) {
return false;
} else {
allowance -= 1.0;
return true;
}
}
protected class refreshDaemonsListener extends BroadcastReceiver {
@Override

View File

@@ -66,7 +66,7 @@ public class LedStripe {
@Override
public String toString() {
return String.format("%s<-%s@%s (r=%d;g=%d;b=%d)", name, controller.getAddress(), ledDDaemon, channelRed, channelGreen, channelBlue);
return String.format("%s->%s->%s (%d|%d|%d)", ledDDaemon, controller.getAddress(), name, channelRed, channelGreen, channelBlue);
}
@DebugLog

View File

@@ -48,6 +48,8 @@ public class Sendable {
}
public void onConnectionFailed(String message) {
onAnswer.onConnectionFailed(message);
if (onAnswer != null) {
onAnswer.onConnectionFailed(message);
}
}
}

View File

@@ -301,7 +301,8 @@ public class AddDaemonDialog extends DialogFragment implements DialogInterface.O
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Snackbar.make(((ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0), getActivity().getString(R.string.snackbar_added_daemon_version) + version, Snackbar.LENGTH_LONG).show();
Snackbar.make(((ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0),
getActivity().getString(R.string.snackbar_added_daemon_version, version), Snackbar.LENGTH_LONG).show();
}
});

View File

@@ -231,10 +231,16 @@ public class LedDHelper {
try {
JSONObject json = new JSONObject(response.getResult().toString());
JSONArray hsv = json.getJSONArray("color");
ledStripe.setColor(new HSV(hsv.getDouble(0), hsv.getDouble(1), hsv.getDouble(2)));
callback.onColorRecieved(ledStripe);
if (hsv.length() == 3) {
System.out.println(hsv);
ledStripe.setColor(new HSV(hsv.getDouble(0), hsv.getDouble(1), hsv.getDouble(2)));
callback.onColorRecieved(ledStripe);
} else {
callback.onRecievFailed(-1, "HSV was empty");
}
} catch (JSONException e) {
e.printStackTrace();
callback.onRecievFailed(-1, "Unhandeled JSON Exception");
}
} else {
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") JSONRPC2Error error = response.getError();

View File

@@ -86,7 +86,6 @@ public class ColorService extends Service {
try {
reqIn = JSONRPC2Response.parse(new String(bb.getAllByteArray()));
} catch (JSONRPC2ParseException e) {
e.printStackTrace();
}

View File

@@ -0,0 +1,51 @@
/*
* LEDD Project
* Copyright (C) 2015 LEDD Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.idlegandalf.ledd.utils;
public class RateLimiter {
private double rate;
private double per;
private double allowance;
private long last_check;
public RateLimiter(double rate, double per) {
this.rate = rate;
this.per = per;
this.allowance = rate;
this.last_check = System.currentTimeMillis();
}
public boolean check() {
long current = System.currentTimeMillis();
long time_passed = current - last_check;
last_check = current;
allowance += time_passed * (rate / per);
if (allowance > rate) {
allowance = rate; // throttle
}
if (allowance < 1.0) {
return false;
} else {
allowance -= 1.0;
return true;
}
}
}

View File

@@ -26,54 +26,66 @@
<!-- The main content view -->
<LinearLayout
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize">
android:orientation="vertical">
<Switch
android:id="@+id/switch_onoff"
android:layout_width="wrap_content"
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<Switch
android:id="@+id/switch_onoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
<com.larswerkman.holocolorpicker.ColorPicker
android:id="@+id/picker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
app:color_center_radius="140dp"
app:color_wheel_radius="180dp"
app:color_wheel_thickness="30dp"/>
</android.support.v7.widget.Toolbar>
<com.larswerkman.holocolorpicker.SaturationBar
android:id="@+id/saturationbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
app:bar_thickness="15dp"/>
<com.larswerkman.holocolorpicker.ColorPicker
android:id="@+id/picker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
app:color_center_radius="100dp"
app:color_pointer_halo_radius="13dp"
app:color_pointer_radius="12dp"
app:color_wheel_radius="180dp"
app:color_wheel_thickness="20dp"/>
<com.larswerkman.holocolorpicker.ValueBar
android:id="@+id/valuebar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:bar_thickness="15dp"/>
<com.larswerkman.holocolorpicker.SaturationBar
android:id="@+id/saturationbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
app:bar_pointer_halo_radius="11dp"
app:bar_pointer_radius="10dp"
app:bar_thickness="15dp"/>
</LinearLayout>
<com.larswerkman.holocolorpicker.ValueBar
android:id="@+id/valuebar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:bar_pointer_halo_radius="11dp"
app:bar_pointer_radius="10dp"
app:bar_thickness="15dp"/>
</LinearLayout>
</ScrollView>
<!-- The navigation drawer -->
@@ -82,7 +94,6 @@
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/navigation_drawer"/>
</android.support.v4.widget.DrawerLayout>

View File

@@ -46,7 +46,7 @@
android:layout_marginLeft="7dp"
android:layout_toRightOf="@id/img_host"
android:gravity="center"
android:text="Choose Daemon"
android:text="@string/text_choose_daemon"
android:textAppearance="?android:textAppearanceMedium"
/>

View File

@@ -26,18 +26,61 @@
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<TextView
android:id="@+id/nvh_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="-- stripes infos go here --"
android:text="Name"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textSize="19sp"/>
<TextView
android:id="@+id/nvh_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nvh_name"
android:layout_marginTop="15dp"
android:text="type"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
<TextView
android:id="@+id/nvh_mapping"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nvh_type"
android:layout_marginTop="5dp"
android:text="mapping"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
<TextView
android:id="@+id/nvh_hex_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nvh_mapping"
android:layout_marginTop="5dp"
android:text="#hex"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
<ImageButton
android:layout_width="15dp"
android:layout_height="15dp"
android:id="@+id/nhv_delete"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:scaleType="center"
android:src="@drawable/ic_clear_white_48dp"/>
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_toLeftOf="@id/nhv_delete"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:scaleType="center"
android:src="@drawable/ic_mode_edit_white_24dp"/>
</RelativeLayout>

View File

@@ -18,6 +18,6 @@
-->
<resources>
<color name="primaryColor">#3ae765</color>
<color name="primaryColorDark">#69cc59</color>
<color name="primaryColor">#3ae88d</color>
<color name="primaryColorDark">#327D26</color>
</resources>

View File

@@ -43,7 +43,7 @@
<string name="snachbar_added_controller">Added Controller (Id=%1$d)</string>
<string name="snackbar_error">Error: </string>
<string name="snackbar_daemon_connection_failed">Couldn\'t connect to daemon at %1$s: %2$s</string>
<string name="snackbar_added_daemon_version">Added LedD Daemon version: </string>
<string name="snackbar_added_daemon_version">Added LedD Daemon version: %1$s</string>
<string name="snackbar_added_stripe_id">Added Stripe (Id=%1$d)</string>
<string name="snackbar_failed_add_stripe">Failed to add Stripe: </string>