Skip to content

Commit

Permalink
Added ImageButtonDivet, and used in transport button
Browse files Browse the repository at this point in the history
// FREEBIE
  • Loading branch information
s0 committed Feb 22, 2015
1 parent 61f394f commit f5dd0bb
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 2 deletions.
5 changes: 4 additions & 1 deletion res/layout/conversation_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@
android:padding="12dp"
android:src="@drawable/ic_transport_sms_insecure"
android:clickable="false"
android:enabled="false" />
android:enabled="false"
divet_position="bottom_right"
divet_margin_h="3"
divet_margin_v="3" />

<org.thoughtcrime.securesms.components.EmojiToggle
android:id="@+id/emoji_toggle"
Expand Down
2 changes: 2 additions & 0 deletions res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<attr name="fab_color" format="reference|color" />
<attr name="actionbar_icon" format="reference" />
<attr name="lower_right_divet" format="reference" />
<attr name="divet_margin_h" format="integer" />
<attr name="divet_margin_v" format="integer" />

<attr name="conversation_background" format="reference|color"/>
<attr name="conversation_editor_background" format="reference"/>
Expand Down
114 changes: 114 additions & 0 deletions src/org/thoughtcrime/securesms/components/ImageButtonDivet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package org.thoughtcrime.securesms.components;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageButton;
import android.widget.ImageView;

import org.thoughtcrime.securesms.R;

public class ImageButtonDivet extends ImageButton {
private static final float CORNER_OFFSET = 12F;
private static final String[] POSITIONS = new String[] {"none", "bottom_right"};

private Drawable drawable;

private int drawableIntrinsicWidth;
private int drawableIntrinsicHeight;
private int position;
private int divetMarginH;
private int divetMarginV;
private float density;

public ImageButtonDivet(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize(attrs);
}

public ImageButtonDivet(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(attrs);
}

public ImageButtonDivet(Context context) {
super(context);
initialize(null);
}

private void initialize(AttributeSet attrs) {
if (attrs != null) {
position = attrs.getAttributeListValue(null, "divet_position", POSITIONS, -1);
int[] attrsArr = new int[] {R.attr.divet_margin_h, R.attr.divet_margin_v};
TypedArray dimensions = getContext().obtainStyledAttributes(attrs, attrsArr);
divetMarginH = attrs.getAttributeIntValue(null, "divet_margin_h", 0);
divetMarginV = attrs.getAttributeIntValue(null, "divet_margin_v", 0);
}

density = getContext().getResources().getDisplayMetrics().density;
setDrawable();
}

private void setDrawable() {
int attributes[] = new int[] {R.attr.lower_right_divet};

TypedArray drawables = getContext().obtainStyledAttributes(attributes);

switch (position) {
case 0:
drawable = null;
break;
case 1:
drawable = drawables.getDrawable(0);
break;
}

if(drawable != null) {
drawableIntrinsicWidth = drawable.getIntrinsicWidth();
drawableIntrinsicHeight = drawable.getIntrinsicHeight();
}

drawables.recycle();
}

@Override
public void onDraw(Canvas c) {
super.onDraw(c);
if(drawable != null) {
c.save();
computeBounds(c);
drawable.draw(c);
c.restore();
}
}

public void setDivetPosition(int position) {
this.position = position;
setDrawable();
invalidate();
}

public int getPosition() {
return position;
}

private void computeBounds(Canvas c) {
final int right = getWidth();
final int bottom = getHeight();

int marginH = (int) (divetMarginH * density);
int marginV = (int) (divetMarginV * density);

switch (position) {
case 1:
drawable.setBounds(
right - marginH - drawableIntrinsicWidth,
bottom - marginV - drawableIntrinsicHeight,
right - marginH,
bottom - marginV);
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.thoughtcrime.securesms.TransportOptions;
import org.thoughtcrime.securesms.TransportOptions.OnTransportChangedListener;

public class TransportButton extends ImageButton {
public class TransportButton extends ImageButtonDivet {
private TransportOptions transportOptions;
private EditText composeText;

Expand Down Expand Up @@ -46,8 +46,10 @@ public void onChange(TransportOption newTransport) {
// Check the number of enabled transports
if(transportOptions.getEnabledTransports().size() > 1){
setClickable(true);
setDivetPosition(1);
} else {
setClickable(false);
setDivetPosition(0);
}
}
});
Expand Down

0 comments on commit f5dd0bb

Please sign in to comment.