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.SubMenu;
import android.view.View; import android.view.View;
import android.widget.Switch; import android.widget.Switch;
import android.widget.TextView;
import com.idlegandalf.ledd.callbacks.RecieveColorCallback; import com.idlegandalf.ledd.callbacks.RecieveColorCallback;
import com.idlegandalf.ledd.callbacks.StripesCallback; 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.components.LedStripe;
import com.idlegandalf.ledd.fragments.AddStripeDialog; import com.idlegandalf.ledd.fragments.AddStripeDialog;
import com.idlegandalf.ledd.helper.LedDHelper; import com.idlegandalf.ledd.helper.LedDHelper;
import com.idlegandalf.ledd.utils.RateLimiter;
import com.larswerkman.holocolorpicker.ColorPicker; import com.larswerkman.holocolorpicker.ColorPicker;
import com.larswerkman.holocolorpicker.SaturationBar; import com.larswerkman.holocolorpicker.SaturationBar;
import com.larswerkman.holocolorpicker.ValueBar; import com.larswerkman.holocolorpicker.ValueBar;
@@ -58,6 +60,7 @@ import java.util.List;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnCheckedChanged; import butterknife.OnCheckedChanged;
import hugo.weaving.DebugLog;
public class ColorActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { public class ColorActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
@@ -75,16 +78,18 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
ValueBar valueBar; ValueBar valueBar;
@Bind(R.id.switch_onoff) @Bind(R.id.switch_onoff)
Switch aSwitch; Switch aSwitch;
TextView mStripeName;
TextView mStripeMapping;
TextView mStripeType;
TextView mStripeHex;
private ActionBarDrawerToggle mDrawerToggle; private ActionBarDrawerToggle mDrawerToggle;
private refreshDaemonsListener daemonsListener; private refreshDaemonsListener daemonsListener;
private List<LedStripe> ledStripes; private List<LedStripe> ledStripes;
private LedStripe mCurrentStripe; private LedStripe mCurrentStripe;
private boolean fromOnCreate = true; private boolean fromOnCreate = true;
private boolean autoColorSet = false; private boolean autoColorSet = false;
private double rate = 5.0; private RateLimiter limiter;
private double per = 100.0; private View headerLayout;
private double allowance = rate;
private long last_check = System.currentTimeMillis();
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -92,6 +97,17 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
setContentView(R.layout.activity_color); setContentView(R.layout.activity_color);
ButterKnife.bind(this); 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 // check for connectivity
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
@@ -104,6 +120,8 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
}).show(); }).show();
} }
limiter = new RateLimiter(5.0, 100.0);
colorPicker.addSaturationBar(saturationBar); colorPicker.addSaturationBar(saturationBar);
colorPicker.addValueBar(valueBar); colorPicker.addValueBar(valueBar);
colorPicker.setShowOldCenterColor(false); colorPicker.setShowOldCenterColor(false);
@@ -111,8 +129,9 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
colorPicker.setOnColorChangedListener(new ColorPicker.OnColorChangedListener() { colorPicker.setOnColorChangedListener(new ColorPicker.OnColorChangedListener() {
@Override @Override
public void onColorChanged(int i) { public void onColorChanged(int i) {
if (mCurrentStripe != null && isRateAcceptable() && aSwitch.isChecked() && !autoColorSet) { if (mCurrentStripe != null && limiter.check() && aSwitch.isChecked() && !autoColorSet) {
mCurrentStripe.setColor(i); 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 (mCurrentStripe != null) {
if (!checked) { if (!checked) {
HSV nHSV = mCurrentStripe.getColor(); HSV nHSV = mCurrentStripe.getColor();
if (nHSV != null) {
nHSV.setValue(0.0); nHSV.setValue(0.0);
mCurrentStripe.setColor(nHSV); mCurrentStripe.setColor(nHSV);
} else {
mCurrentStripe.setColor(Color.BLACK);
}
//colorPicker.setColor(Color.HSVToColor(new float[]{(float) nHSV.getHue(), (float) nHSV.getSaturation(), (float) nHSV.getValue()})); //colorPicker.setColor(Color.HSVToColor(new float[]{(float) nHSV.getHue(), (float) nHSV.getSaturation(), (float) nHSV.getValue()}));
} else { } else {
mCurrentStripe.setColor(colorPicker.getColor()); mCurrentStripe.setColor(colorPicker.getColor());
@@ -339,8 +362,13 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
}); });
} }
@DebugLog
private void selectStripe(LedStripe stripe) { private void selectStripe(LedStripe stripe) {
mCurrentStripe = 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); mDrawerLayout.closeDrawer(Gravity.LEFT);
toolbar.setTitle(stripe.getName()); toolbar.setTitle(stripe.getName());
@@ -368,33 +396,16 @@ public class ColorActivity extends AppCompatActivity implements NavigationView.O
@Override @Override
public void onRecievFailed(int code, String msg) { public void onRecievFailed(int code, String msg) {
autoColorSet = false;
} }
@Override @Override
public void onConnectionFailed(String message) { 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 { protected class refreshDaemonsListener extends BroadcastReceiver {
@Override @Override

View File

@@ -66,7 +66,7 @@ public class LedStripe {
@Override @Override
public String toString() { 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 @DebugLog

View File

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

View File

@@ -301,7 +301,8 @@ public class AddDaemonDialog extends DialogFragment implements DialogInterface.O
getActivity().runOnUiThread(new Runnable() { getActivity().runOnUiThread(new Runnable() {
@Override @Override
public void run() { 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 { try {
JSONObject json = new JSONObject(response.getResult().toString()); JSONObject json = new JSONObject(response.getResult().toString());
JSONArray hsv = json.getJSONArray("color"); JSONArray hsv = json.getJSONArray("color");
if (hsv.length() == 3) {
System.out.println(hsv);
ledStripe.setColor(new HSV(hsv.getDouble(0), hsv.getDouble(1), hsv.getDouble(2))); ledStripe.setColor(new HSV(hsv.getDouble(0), hsv.getDouble(1), hsv.getDouble(2)));
callback.onColorRecieved(ledStripe); callback.onColorRecieved(ledStripe);
} else {
callback.onRecievFailed(-1, "HSV was empty");
}
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
callback.onRecievFailed(-1, "Unhandeled JSON Exception");
} }
} else { } else {
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") JSONRPC2Error error = response.getError(); @SuppressWarnings("ThrowableResultOfMethodCallIgnored") JSONRPC2Error error = response.getError();

View File

@@ -86,7 +86,6 @@ public class ColorService extends Service {
try { try {
reqIn = JSONRPC2Response.parse(new String(bb.getAllByteArray())); reqIn = JSONRPC2Response.parse(new String(bb.getAllByteArray()));
} catch (JSONRPC2ParseException e) { } catch (JSONRPC2ParseException e) {
e.printStackTrace(); 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,9 +26,13 @@
<!-- The main content view --> <!-- The main content view -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent">
<LinearLayout <LinearLayout
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<android.support.v7.widget.Toolbar <android.support.v7.widget.Toolbar
@@ -53,9 +57,11 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp" android:layout_marginTop="25dp"
app:color_center_radius="140dp" app:color_center_radius="100dp"
app:color_pointer_halo_radius="13dp"
app:color_pointer_radius="12dp"
app:color_wheel_radius="180dp" app:color_wheel_radius="180dp"
app:color_wheel_thickness="30dp"/> app:color_wheel_thickness="20dp"/>
<com.larswerkman.holocolorpicker.SaturationBar <com.larswerkman.holocolorpicker.SaturationBar
android:id="@+id/saturationbar" android:id="@+id/saturationbar"
@@ -64,6 +70,8 @@
android:layout_marginLeft="10dp" android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" android:layout_marginRight="10dp"
android:layout_marginTop="15dp" android:layout_marginTop="15dp"
app:bar_pointer_halo_radius="11dp"
app:bar_pointer_radius="10dp"
app:bar_thickness="15dp"/> app:bar_thickness="15dp"/>
<com.larswerkman.holocolorpicker.ValueBar <com.larswerkman.holocolorpicker.ValueBar
@@ -71,10 +79,14 @@
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="10dp" android:layout_margin="10dp"
app:bar_pointer_halo_radius="11dp"
app:bar_pointer_radius="10dp"
app:bar_thickness="15dp"/> app:bar_thickness="15dp"/>
</LinearLayout> </LinearLayout>
</ScrollView>
<!-- The navigation drawer --> <!-- The navigation drawer -->
<android.support.design.widget.NavigationView <android.support.design.widget.NavigationView
@@ -82,7 +94,6 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="start" android:layout_gravity="start"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/navigation_drawer"/> app:menu="@menu/navigation_drawer"/>
</android.support.v4.widget.DrawerLayout> </android.support.v4.widget.DrawerLayout>

View File

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

View File

@@ -26,18 +26,61 @@
android:theme="@style/ThemeOverlay.AppCompat.Dark"> android:theme="@style/ThemeOverlay.AppCompat.Dark">
<TextView <TextView
android:id="@+id/nvh_name"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="15dp" 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"/> android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
<ImageButton <ImageButton
android:layout_width="15dp" android:id="@+id/nhv_delete"
android:layout_height="15dp" android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:background="?android:attr/selectableItemBackground" android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:scaleType="center"
android:src="@drawable/ic_clear_white_48dp"/> 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> </RelativeLayout>

View File

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

View File

@@ -43,7 +43,7 @@
<string name="snachbar_added_controller">Added Controller (Id=%1$d)</string> <string name="snachbar_added_controller">Added Controller (Id=%1$d)</string>
<string name="snackbar_error">Error: </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_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_added_stripe_id">Added Stripe (Id=%1$d)</string>
<string name="snackbar_failed_add_stripe">Failed to add Stripe: </string> <string name="snackbar_failed_add_stripe">Failed to add Stripe: </string>