|
|
@ -16,93 +16,64 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package org.transdroid.core.gui.navigation; |
|
|
|
package org.transdroid.core.gui.navigation; |
|
|
|
|
|
|
|
|
|
|
|
import java.security.InvalidParameterException; |
|
|
|
import android.content.Context; |
|
|
|
|
|
|
|
import android.view.LayoutInflater; |
|
|
|
import org.transdroid.R; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.Dialog; |
|
|
|
|
|
|
|
import android.app.DialogFragment; |
|
|
|
|
|
|
|
import android.os.Bundle; |
|
|
|
|
|
|
|
import android.view.View; |
|
|
|
import android.view.View; |
|
|
|
import android.view.View.OnClickListener; |
|
|
|
import android.view.View.OnClickListener; |
|
|
|
import android.view.Window; |
|
|
|
|
|
|
|
import android.widget.Button; |
|
|
|
import android.widget.Button; |
|
|
|
import android.widget.TextView; |
|
|
|
import android.widget.TextView; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
import com.afollestad.materialdialogs.MaterialDialog; |
|
|
|
* A dialog fragment that allow picking of maximum download and upload transfer rates as well as the resetting of these |
|
|
|
|
|
|
|
* values. |
|
|
|
|
|
|
|
* @author Eric Kok |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public class SetTransferRatesDialog extends DialogFragment { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private OnRatesPickedListener onRatesPickedListener = null; |
|
|
|
import org.transdroid.R; |
|
|
|
private TextView maxSpeedDown, maxSpeedUp; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SetTransferRatesDialog() { |
|
|
|
public class SetTransferRatesDialog { |
|
|
|
setRetainInstance(true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Sets the callback for results in this dialog (with newly selected values or a reset). |
|
|
|
* A dialog fragment that allow picking of maximum download and upload transfer rates as well as the resetting of these values. |
|
|
|
* @param onRatesPickedListener The event listener to this dialog |
|
|
|
* @param context The activity context that opens (and owns) this dialog |
|
|
|
* @return This dialog, for method chaining |
|
|
|
* @param onRatesPickedListener The callback for results in this dialog (with newly selected values or a reset) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public SetTransferRatesDialog setOnRatesPickedListener(OnRatesPickedListener onRatesPickedListener) { |
|
|
|
public static void show(final Context context, final OnRatesPickedListener onRatesPickedListener) { |
|
|
|
this.onRatesPickedListener = onRatesPickedListener; |
|
|
|
|
|
|
|
return this; |
|
|
|
View transferRatesLayout = LayoutInflater.from(context).inflate(R.layout.dialog_transferrates, null); |
|
|
|
} |
|
|
|
final TextView maxSpeedDown = (TextView) transferRatesLayout.findViewById(R.id.maxspeeddown_text); |
|
|
|
|
|
|
|
final TextView maxSpeedUp = (TextView) transferRatesLayout.findViewById(R.id.maxspeedup_text); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MaterialDialog dialog = new MaterialDialog.Builder(context).customView(transferRatesLayout, false).positiveText(R.string.status_update) |
|
|
|
|
|
|
|
.neutralText(R.string.status_maxspeed_reset).negativeText(android.R.string.cancel).callback(new MaterialDialog.ButtonCallback() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onPositive(MaterialDialog dialog) { |
|
|
|
|
|
|
|
int maxDown = -1, maxUp = -1; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
maxDown = Integer.parseInt(maxSpeedDown.getText().toString()); |
|
|
|
|
|
|
|
maxUp = Integer.parseInt(maxSpeedUp.getText().toString()); |
|
|
|
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
|
|
|
// Impossible as we only input via the number buttons
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (maxDown <= 0 || maxUp <= 0) { |
|
|
|
|
|
|
|
onRatesPickedListener.onInvalidNumber(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
onRatesPickedListener.onRatesPicked(maxDown, maxUp); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onNeutral(MaterialDialog dialog) { |
|
|
|
|
|
|
|
onRatesPickedListener.resetRates(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}).build(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bindButtons(dialog.getCustomView(), maxSpeedDown, R.id.down1Button, R.id.down2Button, R.id.down3Button, R.id.down4Button, R.id.down5Button, |
|
|
|
|
|
|
|
R.id.down6Button, R.id.down7Button, R.id.down8Button, R.id.down9Button, R.id.down0Button); |
|
|
|
|
|
|
|
bindButtons(dialog.getCustomView(), maxSpeedUp, R.id.up1Button, R.id.up2Button, R.id.up3Button, R.id.up4Button, R.id.up5Button, |
|
|
|
|
|
|
|
R.id.up6Button, R.id.up7Button, R.id.up8Button, R.id.up9Button, R.id.up0Button); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dialog.show(); |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) { |
|
|
|
|
|
|
|
if (onRatesPickedListener == null) |
|
|
|
|
|
|
|
throw new InvalidParameterException( |
|
|
|
|
|
|
|
"Please first set the callback listener using setOnRatesPickedListener before opening the dialog."); |
|
|
|
|
|
|
|
final View transferRatesContent = getActivity().getLayoutInflater().inflate(R.layout.dialog_transferrates, |
|
|
|
|
|
|
|
null, false); |
|
|
|
|
|
|
|
maxSpeedDown = (TextView) transferRatesContent.findViewById(R.id.maxspeeddown_text); |
|
|
|
|
|
|
|
maxSpeedUp = (TextView) transferRatesContent.findViewById(R.id.maxspeedup_text); |
|
|
|
|
|
|
|
bindButtons(transferRatesContent, maxSpeedDown, R.id.down1Button, R.id.down2Button, R.id.down3Button, |
|
|
|
|
|
|
|
R.id.down4Button, R.id.down5Button, R.id.down6Button, R.id.down7Button, R.id.down8Button, |
|
|
|
|
|
|
|
R.id.down9Button, R.id.down0Button); |
|
|
|
|
|
|
|
bindButtons(transferRatesContent, maxSpeedUp, R.id.up1Button, R.id.up2Button, R.id.up3Button, R.id.up4Button, |
|
|
|
|
|
|
|
R.id.up5Button, R.id.up6Button, R.id.up7Button, R.id.up8Button, R.id.up9Button, R.id.up0Button); |
|
|
|
|
|
|
|
((Button) transferRatesContent.findViewById(R.id.ok_button)).setOnClickListener(new OnClickListener() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onClick(View v) { |
|
|
|
|
|
|
|
int maxDown = -1, maxUp = -1; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
maxDown = Integer.parseInt(maxSpeedDown.getText().toString()); |
|
|
|
|
|
|
|
maxUp = Integer.parseInt(maxSpeedUp.getText().toString()); |
|
|
|
|
|
|
|
} catch (NumberFormatException e) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (maxDown <= 0 || maxUp <= 0) { |
|
|
|
|
|
|
|
onRatesPickedListener.onInvalidNumber(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
onRatesPickedListener.onRatesPicked(maxDown, maxUp); |
|
|
|
|
|
|
|
dismiss(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
((Button) transferRatesContent.findViewById(R.id.reset_button)).setOnClickListener(new OnClickListener() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onClick(View v) { |
|
|
|
|
|
|
|
onRatesPickedListener.resetRates(); |
|
|
|
|
|
|
|
dismiss(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
((Button) transferRatesContent.findViewById(R.id.cancel_button)).setOnClickListener(new OnClickListener() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onClick(View v) { |
|
|
|
|
|
|
|
dismiss(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
Dialog dialog = new Dialog(getActivity()); |
|
|
|
|
|
|
|
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); |
|
|
|
|
|
|
|
dialog.setContentView(transferRatesContent); |
|
|
|
|
|
|
|
return dialog; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void bindButtons(View transferRatesContent, View numberView, int... buttonResource) { |
|
|
|
private static void bindButtons(View transferRatesContent, View numberView, int... buttonResource) { |
|
|
|
for (int i : buttonResource) { |
|
|
|
for (int i : buttonResource) { |
|
|
|
// Keep the relevant number as reference in the view tag and bind the click listerner
|
|
|
|
// Keep the relevant number as reference in the view tag and bind the click listerner
|
|
|
|
transferRatesContent.findViewById(i).setTag(numberView); |
|
|
|
transferRatesContent.findViewById(i).setTag(numberView); |
|
|
@ -110,14 +81,15 @@ public class SetTransferRatesDialog extends DialogFragment { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private android.view.View.OnClickListener onNumberClicked = new android.view.View.OnClickListener() { |
|
|
|
private static OnClickListener onNumberClicked = new OnClickListener() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onClick(View v) { |
|
|
|
public void onClick(View v) { |
|
|
|
// Append the text contents of the button itself as text to the current number (as reference in the view's
|
|
|
|
// Append the text contents of the button itself as text to the current number (as reference in the view's
|
|
|
|
// tag)
|
|
|
|
// tag)
|
|
|
|
TextView numberView = (TextView) v.getTag(); |
|
|
|
TextView numberView = (TextView) v.getTag(); |
|
|
|
if (numberView.getText().toString().equals(getString(R.string.status_maxspeed_novalue))) |
|
|
|
if (numberView.getText().toString().equals(v.getContext().getString(R.string.status_maxspeed_novalue))) { |
|
|
|
numberView.setText(""); |
|
|
|
numberView.setText(""); |
|
|
|
|
|
|
|
} |
|
|
|
numberView.setText(numberView.getText().toString() + ((Button) v).getText().toString()); |
|
|
|
numberView.setText(numberView.getText().toString() + ((Button) v).getText().toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|