mirror of
https://github.com/oxen-io/session-android.git
synced 2023-12-14 02:53:01 +01:00
1) Broke out the UI elements of the major Activites into stylable attributes. 2) Created a 'light' and 'dark' theme for the newly stylable attrs. 3) Touched up some of the UI spacing. 4) Implemented dynamic theme switching support.
125 lines
2.9 KiB
Java
125 lines
2.9 KiB
Java
package org.thoughtcrime.securesms.components;
|
|
|
|
import android.content.Context;
|
|
import android.content.res.Resources;
|
|
import android.content.res.TypedArray;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.util.AttributeSet;
|
|
import android.widget.ImageView;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
public class ImageDivet extends ImageView {
|
|
private static final float CORNER_OFFSET = 12F;
|
|
private static final String[] POSITIONS = new String[] {"left", "right"};
|
|
|
|
private Drawable drawable;
|
|
|
|
private int drawableIntrinsicWidth;
|
|
private int drawableIntrinsicHeight;
|
|
private int position;
|
|
private float density;
|
|
|
|
public ImageDivet(Context context, AttributeSet attrs, int defStyle) {
|
|
super(context, attrs, defStyle);
|
|
initialize(attrs);
|
|
}
|
|
|
|
public ImageDivet(Context context, AttributeSet attrs) {
|
|
super(context, attrs);
|
|
initialize(attrs);
|
|
}
|
|
|
|
public ImageDivet(Context context) {
|
|
super(context);
|
|
initialize(null);
|
|
}
|
|
|
|
private void initialize(AttributeSet attrs) {
|
|
if (attrs != null) {
|
|
position = attrs.getAttributeListValue(null, "position", POSITIONS, -1);
|
|
}
|
|
|
|
density = getContext().getResources().getDisplayMetrics().density;
|
|
setDrawable();
|
|
}
|
|
|
|
private void setDrawable() {
|
|
int attributes[] = new int[] {R.attr.conversation_avatar_divet_left,
|
|
R.attr.conversation_avatar_divet_right};
|
|
|
|
TypedArray drawables = getContext().obtainStyledAttributes(attributes);
|
|
|
|
switch (position) {
|
|
case 0:
|
|
drawable = drawables.getDrawable(1);
|
|
break;
|
|
case 1:
|
|
drawable = drawables.getDrawable(0);
|
|
break;
|
|
}
|
|
|
|
drawableIntrinsicWidth = drawable.getIntrinsicWidth();
|
|
drawableIntrinsicHeight = drawable.getIntrinsicHeight();
|
|
|
|
drawables.recycle();
|
|
}
|
|
|
|
@Override
|
|
public void onDraw(Canvas c) {
|
|
super.onDraw(c);
|
|
c.save();
|
|
computeBounds(c);
|
|
drawable.draw(c);
|
|
c.restore();
|
|
}
|
|
|
|
public void setPosition(int position) {
|
|
this.position = position;
|
|
setDrawable();
|
|
invalidate();
|
|
}
|
|
|
|
public int getPosition() {
|
|
return position;
|
|
}
|
|
|
|
public float getCloseOffset() {
|
|
return CORNER_OFFSET * density;
|
|
}
|
|
|
|
public ImageView asImageView() {
|
|
return this;
|
|
}
|
|
|
|
public float getFarOffset() {
|
|
return getCloseOffset() + drawableIntrinsicHeight;
|
|
}
|
|
|
|
private void computeBounds(Canvas c) {
|
|
final int left = 0;
|
|
final int top = 0;
|
|
final int right = getWidth();
|
|
|
|
final int cornerOffset = (int) getCloseOffset();
|
|
|
|
switch (position) {
|
|
case 1:
|
|
drawable.setBounds(
|
|
right - drawableIntrinsicWidth,
|
|
top + cornerOffset,
|
|
right,
|
|
top + cornerOffset + drawableIntrinsicHeight);
|
|
break;
|
|
case 0:
|
|
drawable.setBounds(
|
|
left,
|
|
top + cornerOffset,
|
|
left + drawableIntrinsicWidth,
|
|
top + cornerOffset + drawableIntrinsicHeight);
|
|
break;
|
|
}
|
|
}
|
|
}
|