Calculator Overlay

This commit is contained in:
Sergey Solovyev 2012-11-21 16:38:11 +04:00
parent 85feb7cfef
commit f085e9481f
2 changed files with 90 additions and 36 deletions

View File

@ -1,22 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="horizontal"
<RelativeLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="wrap_content"
a:gravity="right">
a:layout_height="30dp">
<LinearLayout a:id="@+id/overlay_header_buttons"
a:layout_width="wrap_content"
a:layout_height="match_parent"
a:layout_alignParentRight="true">
<Button a:id="@+id/overlay_hide_button"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
style="?cpp_operation_button_style"
a:layout_height="match_parent"
a:layout_weight="0"
a:padding="6dp"
style="@style/widget_metro_control_button_style"
a:text="_"/>
<Button a:id="@+id/overlay_close_button"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
style="?cpp_operation_button_style"
a:layout_height="match_parent"
a:layout_weight="0"
a:padding="6dp"
style="@style/widget_metro_control_button_style"
a:text="x"/>
</LinearLayout>
</LinearLayout>
<TextView a:id="@+id/overlay_title"
a:text="@string/c_app_name"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:layout_toLeftOf="@id/overlay_header_buttons"/>
</RelativeLayout>

View File

@ -7,6 +7,7 @@ import android.graphics.PixelFormat;
import android.os.IBinder;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
@ -29,7 +30,7 @@ import org.solovyev.android.calculator.widget.WidgetButton;
public class CalculatorOverlayService extends Service implements ExternalCalculatorStateUpdater {
@Nullable
private View view;
private View onscreenView;
@NotNull
private final ExternalCalculatorIntentHandler intentHandler = new DefaultExternalCalculatorIntentHandler(this);
@ -45,19 +46,13 @@ public class CalculatorOverlayService extends Service implements ExternalCalcula
@Override
public void onCreate() {
super.onCreate();
final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.overlay_layout, null);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
300,
450,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
onscreenView = layoutInflater.inflate(R.layout.overlay_layout, null);
for (final WidgetButton widgetButton : WidgetButton.values()) {
final View button = view.findViewById(widgetButton.getButtonId());
final View button = onscreenView.findViewById(widgetButton.getButtonId());
if (button != null) {
button.setOnClickListener(new View.OnClickListener() {
@Override
@ -68,15 +63,60 @@ public class CalculatorOverlayService extends Service implements ExternalCalcula
}
}
view.findViewById(R.id.overlay_close_button).setOnClickListener(new View.OnClickListener() {
onscreenView.findViewById(R.id.overlay_close_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(getApplicationContext(), CalculatorOverlayService.class));
}
});
final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(view, params);
final TextView overlayTitleTextView = (TextView) onscreenView.findViewById(R.id.overlay_title);
overlayTitleTextView.setOnTouchListener(new View.OnTouchListener() {
private volatile boolean move = false;
private volatile float startDragX;
private volatile float startDragY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if (move) {
int x = (int) (onscreenView.getX() - startDragX + event.getX());
int y = (int) (onscreenView.getY() - startDragY + event.getY());
overlayTitleTextView.setText("Calculator++: (" + x + ", " + y + ")");
final WindowManager.LayoutParams params = (WindowManager.LayoutParams) onscreenView.getLayoutParams();
params.x = x;
params.y = y;
// todo serso: add small delay
wm.updateViewLayout(onscreenView, params);
} else {
startDragX = event.getX();
startDragY = event.getY();
move = true;
}
return true;
}
move = false;
startDragX = 0;
startDragY = 0;
return false;
}
});
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
300,
450,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
wm.addView(onscreenView, params);
startCalculatorListening();
}
@ -98,9 +138,9 @@ public class CalculatorOverlayService extends Service implements ExternalCalcula
public void onDestroy() {
stopCalculatorListening();
if (view != null) {
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(view);
view = null;
if (onscreenView != null) {
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(onscreenView);
onscreenView = null;
}
super.onDestroy();
@ -108,7 +148,7 @@ public class CalculatorOverlayService extends Service implements ExternalCalcula
@Override
public void updateState(@NotNull Context context, @NotNull CalculatorEditorViewState editorState, @NotNull CalculatorDisplayViewState displayState) {
final View root = this.view;
final View root = this.onscreenView;
if (root != null) {
updateDisplayState(context, root, displayState);
updateEditorState(context, root, editorState);