forked from scummvm/scummvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ANDROID: Add a LED widget and use it to indicate IO activity in SAF
This will notify the user of IOs by blinking the LED. This indicates that ScummVM is busy and not hung.
- Loading branch information
1 parent
41e49d8
commit 50658e6
Showing
4 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
backends/platform/android/org/scummvm/scummvm/LedView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package org.scummvm.scummvm; | ||
|
||
import android.content.Context; | ||
import android.content.res.TypedArray; | ||
import android.graphics.Canvas; | ||
import android.graphics.Paint; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.annotation.RequiresApi; | ||
|
||
public class LedView extends View { | ||
public static final int DEFAULT_LED_COLOR = 0xffff0000; | ||
private static final int BLINK_TIME = 30; // ms | ||
|
||
private boolean _state; | ||
private Runnable _blink; | ||
private Paint _painter; | ||
private int _radius; | ||
private int _centerX; | ||
private int _centerY; | ||
|
||
public LedView(Context context) { | ||
this(context, true, DEFAULT_LED_COLOR); | ||
} | ||
|
||
public LedView(Context context, boolean state) { | ||
this(context, state, DEFAULT_LED_COLOR); | ||
} | ||
|
||
public LedView(Context context, boolean state, int color) { | ||
super(context); | ||
_state = state; | ||
init(color); | ||
} | ||
|
||
public LedView(Context context, @Nullable AttributeSet attrs) { | ||
this(context, attrs, 0); | ||
} | ||
|
||
public LedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
init(context, attrs, defStyleAttr, 0); | ||
} | ||
|
||
@RequiresApi(android.os.Build.VERSION_CODES.LOLLIPOP) | ||
public LedView( | ||
Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { | ||
super(context, attrs, defStyleAttr, defStyleRes); | ||
init(context, attrs, defStyleAttr, defStyleRes); | ||
} | ||
|
||
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | ||
TypedArray a = context.getTheme().obtainStyledAttributes( | ||
attrs, | ||
R.styleable.LedView, | ||
defStyleAttr, defStyleRes); | ||
|
||
try { | ||
_state = a.getBoolean(R.styleable.LedView_state, true); | ||
int color = a.getColor(R.styleable.LedView_color, DEFAULT_LED_COLOR); | ||
init(color); | ||
} finally { | ||
a.recycle(); | ||
} | ||
} | ||
|
||
private void init(int color) { | ||
_painter = new Paint(); | ||
_painter.setStyle(Paint.Style.FILL); | ||
if (isInEditMode()) { | ||
_painter.setStrokeWidth(2); | ||
_painter.setStyle(_state ? Paint.Style.FILL : Paint.Style.STROKE); | ||
} | ||
_painter.setColor(color); | ||
_painter.setAntiAlias(true); | ||
} | ||
|
||
@Override | ||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth(); | ||
int w = resolveSizeAndState(minw, widthMeasureSpec, 0); | ||
|
||
int minh = MeasureSpec.getSize(w) - getPaddingLeft() - getPaddingRight() + | ||
getPaddingBottom() + getPaddingTop(); | ||
int h = resolveSizeAndState(minh, heightMeasureSpec, 0); | ||
|
||
setMeasuredDimension(w, h); | ||
} | ||
|
||
@Override | ||
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | ||
super.onSizeChanged(w, h, oldw, oldh); | ||
|
||
int xpad = (getPaddingLeft() + getPaddingRight()); | ||
int ypad = (getPaddingTop() + getPaddingBottom()); | ||
|
||
int ww = w - xpad; | ||
int hh = h - ypad; | ||
|
||
_radius = Math.min(ww, hh) / 2 - 2; | ||
_centerX = w / 2; | ||
_centerY = h / 2; | ||
} | ||
|
||
@Override | ||
protected void onDraw(@NonNull Canvas canvas) { | ||
super.onDraw(canvas); | ||
|
||
if (!_state && !isInEditMode()) { | ||
return; | ||
} | ||
|
||
canvas.drawCircle(_centerX, _centerY, _radius, _painter); | ||
} | ||
|
||
public void on() { | ||
setState(true); | ||
} | ||
|
||
public void off() { | ||
setState(false); | ||
} | ||
|
||
public void setState(boolean state) { | ||
if (_blink != null) { | ||
removeCallbacks(_blink); | ||
_blink = null; | ||
} | ||
|
||
if (_state == state) { | ||
return; | ||
} | ||
_state = state; | ||
invalidate(); | ||
} | ||
|
||
public void blinkOnce() { | ||
if (_blink != null) { | ||
return; | ||
} | ||
|
||
boolean oldState = _state; | ||
_state = !oldState; | ||
invalidate(); | ||
|
||
_blink = new Runnable() { | ||
private boolean _ran; | ||
|
||
@Override | ||
public void run() { | ||
if (_ran) { | ||
_blink = null; | ||
return; | ||
} | ||
|
||
_ran = true; | ||
_state = oldState; | ||
invalidate(); | ||
|
||
postDelayed(this, BLINK_TIME); | ||
} | ||
}; | ||
postDelayed(_blink, BLINK_TIME); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<resources> | ||
<declare-styleable name="LedView"> | ||
<attr name="color" format="reference|color" /> | ||
<attr name="state" format="boolean" /> | ||
</declare-styleable> | ||
</resources> |