changes
This commit is contained in:
6
android-app-onscreen/AndroidManifest.xml
Normal file
6
android-app-onscreen/AndroidManifest.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.solovyev.android.calculator.onscreen"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0"/>
|
45
android-app-onscreen/pom.xml
Normal file
45
android-app-onscreen/pom.xml
Normal file
@@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.solovyev.android</groupId>
|
||||
<artifactId>calculatorpp-parent</artifactId>
|
||||
<version>1.5.3-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.solovyev.android</groupId>
|
||||
<artifactId>calculatorpp-android-app-onscreen</artifactId>
|
||||
<version>1.5.3-SNAPSHOT</version>
|
||||
<name>Calculator++ Application Onscreen</name>
|
||||
<packaging>apklib</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.solovyev.android</groupId>
|
||||
<artifactId>calculatorpp-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.solovyev.android</groupId>
|
||||
<artifactId>calculatorpp-android-app-core</artifactId>
|
||||
<type>apklib</type>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
<extensions>
|
||||
<extension>
|
||||
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
|
||||
<artifactId>android-maven-plugin</artifactId>
|
||||
</extension>
|
||||
</extensions>
|
||||
|
||||
</build>
|
||||
|
||||
</project>
|
17
android-app-onscreen/res/layout/onscreen_display.xml
Normal file
17
android-app-onscreen/res/layout/onscreen_display.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<!--
|
||||
~ Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
~ For more information, please, contact se.solovyev@gmail.com
|
||||
~ or visit http://se.solovyev.org
|
||||
-->
|
||||
|
||||
<org.solovyev.android.calculator.AndroidCalculatorDisplayView
|
||||
xmlns:a="http://schemas.android.com/apk/res/android"
|
||||
a:id="@+id/calculator_display"
|
||||
style="@style/cpp_onscreen_display_style"
|
||||
a:padding="@dimen/cpp_display_padding"
|
||||
a:inputType="textMultiLine"
|
||||
a:maxLines="3"
|
||||
a:scrollHorizontally="false"
|
||||
a:scrollbars="none"/>
|
22
android-app-onscreen/res/layout/onscreen_editor.xml
Normal file
22
android-app-onscreen/res/layout/onscreen_editor.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<!--
|
||||
~ Copyright (c) 2009-2011. Created by serso aka se.solovyev.
|
||||
~ For more information, please, contact se.solovyev@gmail.com
|
||||
~ or visit http://se.solovyev.org
|
||||
-->
|
||||
|
||||
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
|
||||
a:id="@+id/main_fragment_layout"
|
||||
style="?cpp_fragment_layout_style"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="match_parent"
|
||||
a:padding="@dimen/cpp_editor_padding">
|
||||
|
||||
<org.solovyev.android.calculator.AndroidCalculatorEditorView
|
||||
a:id="@+id/calculator_editor"
|
||||
style="@style/cpp_onscreen_editor_style"
|
||||
a:textIsSelectable="true"
|
||||
a:hint="@string/c_calc_editor_hint"/>
|
||||
|
||||
</LinearLayout>
|
8
android-app-onscreen/res/layout/onscreen_footer.xml
Normal file
8
android-app-onscreen/res/layout/onscreen_footer.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
</LinearLayout>
|
36
android-app-onscreen/res/layout/onscreen_header.xml
Normal file
36
android-app-onscreen/res/layout/onscreen_header.xml
Normal file
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<RelativeLayout xmlns:a="http://schemas.android.com/apk/res/android"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="30dp">
|
||||
|
||||
<LinearLayout a:id="@+id/onscreen_header_buttons"
|
||||
a:layout_width="wrap_content"
|
||||
a:layout_height="match_parent"
|
||||
a:layout_alignParentRight="true">
|
||||
|
||||
<ImageButton a:id="@+id/onscreen_fold_button"
|
||||
style="@style/onscreen_header_button_style"
|
||||
a:src="@drawable/cpp_onscreen_button_fold"/>
|
||||
|
||||
<ImageButton a:id="@+id/onscreen_minimize_button"
|
||||
style="@style/onscreen_header_button_style"
|
||||
a:src="@drawable/cpp_onscreen_button_minimize"/>
|
||||
|
||||
<ImageButton a:id="@+id/onscreen_close_button"
|
||||
style="@style/onscreen_header_button_style"
|
||||
a:src="@drawable/cpp_onscreen_button_close"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<ImageView a:id="@+id/onscreen_title"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="match_parent"
|
||||
a:src="@drawable/cpp_onscreen_header_logo"
|
||||
a:scaleType="fitStart"
|
||||
a:paddingLeft="6dp"
|
||||
a:paddingTop="2dp"
|
||||
a:paddingBottom="2dp"
|
||||
a:layout_toLeftOf="@id/onscreen_header_buttons"/>
|
||||
|
||||
</RelativeLayout>
|
69
android-app-onscreen/res/layout/onscreen_keyboard.xml
Normal file
69
android-app-onscreen/res/layout/onscreen_keyboard.xml
Normal file
@@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="match_parent"
|
||||
a:orientation="vertical">
|
||||
|
||||
<LinearLayout a:layout_weight="1"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="0dp">
|
||||
|
||||
<include layout="@layout/widget_seven_digit_button"/>
|
||||
<include layout="@layout/widget_eight_digit_button"/>
|
||||
<include layout="@layout/widget_nine_digit_button"/>
|
||||
<include layout="@layout/widget_multiplication_button"/>
|
||||
<include layout="@layout/widget_erase_button"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout a:layout_weight="1"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="0dp">
|
||||
|
||||
<include layout="@layout/widget_four_digit_button"/>
|
||||
<include layout="@layout/widget_five_digit_button"/>
|
||||
<include layout="@layout/widget_six_digit_button"/>
|
||||
<include layout="@layout/widget_division_button"/>
|
||||
<include layout="@layout/widget_percent_button"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout a:layout_weight="1"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="0dp">
|
||||
|
||||
<include layout="@layout/widget_one_digit_button"/>
|
||||
<include layout="@layout/widget_two_digit_button"/>
|
||||
<include layout="@layout/widget_three_digit_button"/>
|
||||
<include layout="@layout/widget_plus_button"/>
|
||||
<include layout="@layout/widget_power_button"/>
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout a:layout_weight="1"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="0dp">
|
||||
|
||||
<include layout="@layout/widget_round_brackets_button"/>
|
||||
<include layout="@layout/widget_zero_digit_button"/>
|
||||
<include layout="@layout/widget_dot_button"/>
|
||||
<include layout="@layout/widget_subtraction_button"/>
|
||||
<include layout="@layout/widget_app_button"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
<!-- <LinearLayout a:layout_weight="1"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="0dp">
|
||||
|
||||
<include layout="@layout/widget_left_button"/>
|
||||
<include layout="@layout/widget_right_button"/>
|
||||
<include layout="@layout/widget_vars_button"/>
|
||||
<include layout="@layout/widget_functions_button"/>
|
||||
<include layout="@layout/widget_app_button"/>
|
||||
<include layout="@layout/widget_history_button"/>
|
||||
|
||||
</LinearLayout>-->
|
||||
|
||||
</LinearLayout>
|
43
android-app-onscreen/res/layout/onscreen_layout.xml
Normal file
43
android-app-onscreen/res/layout/onscreen_layout.xml
Normal file
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
|
||||
a:orientation="vertical"
|
||||
style="@style/cpp_onscreen_main_layout_style">
|
||||
|
||||
<include a:id="@+id/onscreen_header" layout="@layout/onscreen_header"/>
|
||||
|
||||
<LinearLayout a:id="@+id/onscreen_content"
|
||||
a:orientation="vertical"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="match_parent">
|
||||
|
||||
<include layout="@layout/onscreen_editor"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="wrap_content"/>
|
||||
|
||||
<LinearLayout a:layout_weight="1"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="0dp">
|
||||
|
||||
<include layout="@layout/widget_equals_button"
|
||||
a:layout_margin="@dimen/cpp_button_margin"
|
||||
a:layout_weight="1"
|
||||
a:layout_width="0dp"
|
||||
a:layout_height="match_parent"/>
|
||||
|
||||
<include layout="@layout/onscreen_display"
|
||||
a:layout_weight="4"
|
||||
a:layout_width="0dp"
|
||||
a:layout_height="wrap_content"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<include layout="@layout/onscreen_keyboard"
|
||||
a:layout_weight="4"
|
||||
a:layout_width="match_parent"
|
||||
a:layout_height="0dp"/>
|
||||
</LinearLayout>
|
||||
|
||||
<include layout="@layout/onscreen_footer"/>
|
||||
|
||||
</LinearLayout>
|
4
android-app-onscreen/res/values/colors.xml
Normal file
4
android-app-onscreen/res/values/colors.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="cpp_onscreen_cursor_color">#ff707070</color>
|
||||
</resources>
|
8
android-app-onscreen/res/values/styles.xml
Normal file
8
android-app-onscreen/res/values/styles.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="cpp_onscreen_main_layout_style" parent="cpp_default_main_layout_style">
|
||||
<item name="android:padding">1dp</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
@@ -0,0 +1,35 @@
|
||||
package org.solovyev.android.calculator.onscreen;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.CalculatorPreferences;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/20/12
|
||||
* Time: 11:05 PM
|
||||
*/
|
||||
public final class CalculatorOnscreenBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
public CalculatorOnscreenBroadcastReceiver() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(@NotNull Context context,
|
||||
@NotNull Intent intent) {
|
||||
if ( intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) ) {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
if ( CalculatorPreferences.OnscreenCalculator.startOnBoot.getPreferenceNoError(preferences) ) {
|
||||
CalculatorOnscreenService.showNotification(context);
|
||||
}
|
||||
} else {
|
||||
final Intent newIntent = new Intent(intent);
|
||||
newIntent.setClass(context, CalculatorOnscreenService.class);
|
||||
context.startService(newIntent);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,216 @@
|
||||
package org.solovyev.android.calculator.onscreen;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.WindowManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.AndroidUtils;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewState;
|
||||
import org.solovyev.android.calculator.CalculatorEditorViewState;
|
||||
import org.solovyev.android.calculator.Locator;
|
||||
import org.solovyev.android.calculator.external.*;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/20/12
|
||||
* Time: 9:42 PM
|
||||
*/
|
||||
public class CalculatorOnscreenService extends Service implements ExternalCalculatorStateUpdater, OnscreenViewListener {
|
||||
|
||||
private static final int NOTIFICATION_ID = 9031988; // my birthday =)
|
||||
|
||||
@NotNull
|
||||
private final ExternalCalculatorIntentHandler intentHandler = new DefaultExternalCalculatorIntentHandler(this);
|
||||
public static final Class<CalculatorOnscreenBroadcastReceiver> INTENT_LISTENER_CLASS = CalculatorOnscreenBroadcastReceiver.class;
|
||||
|
||||
@Nullable
|
||||
private static String cursorColor;
|
||||
|
||||
@NotNull
|
||||
private CalculatorOnscreenView view;
|
||||
|
||||
private boolean compatibilityStart = true;
|
||||
|
||||
private boolean viewCreated = false;
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
private void createView() {
|
||||
if (!viewCreated) {
|
||||
final WindowManager wm = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE));
|
||||
|
||||
final DisplayMetrics dm = getResources().getDisplayMetrics();
|
||||
|
||||
int twoThirdWidth = 2 * wm.getDefaultDisplay().getWidth() / 3;
|
||||
int twoThirdHeight = 2 * wm.getDefaultDisplay().getHeight() / 3;
|
||||
|
||||
twoThirdWidth = Math.min(twoThirdWidth, twoThirdHeight);
|
||||
twoThirdHeight = Math.max(twoThirdWidth, getHeight(twoThirdWidth));
|
||||
|
||||
final int baseWidth = AndroidUtils.toPixels(dm, 300);
|
||||
final int width0 = Math.min(twoThirdWidth, baseWidth);
|
||||
final int height0 = Math.min(twoThirdHeight, getHeight(baseWidth));
|
||||
|
||||
final int width = Math.min(width0, height0);
|
||||
final int height = Math.max(width0, height0);
|
||||
|
||||
view = CalculatorOnscreenView.newInstance(this, CalculatorOnscreenViewDef.newInstance(width, height, -1, -1), getCursorColor(this), this);
|
||||
view.show();
|
||||
|
||||
startCalculatorListening();
|
||||
|
||||
viewCreated = true;
|
||||
}
|
||||
}
|
||||
|
||||
private int getHeight(int width) {
|
||||
return 4 * width / 3;
|
||||
}
|
||||
|
||||
private void startCalculatorListening() {
|
||||
Locator.getInstance().getExternalListenersContainer().addExternalListener(getIntentListenerClass());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Class<?> getIntentListenerClass() {
|
||||
return INTENT_LISTENER_CLASS;
|
||||
}
|
||||
|
||||
private void stopCalculatorListening() {
|
||||
Locator.getInstance().getExternalListenersContainer().removeExternalListener(getIntentListenerClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
stopCalculatorListening();
|
||||
this.view.hide();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(@NotNull Context context, @NotNull CalculatorEditorViewState editorState, @NotNull CalculatorDisplayViewState displayState) {
|
||||
view.updateDisplayState(displayState);
|
||||
view.updateEditorState(editorState);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getCursorColor(@NotNull Context context) {
|
||||
if (cursorColor == null) {
|
||||
cursorColor = Integer.toHexString(context.getResources().getColor(R.color.cpp_onscreen_cursor_color)).substring(2);
|
||||
}
|
||||
return cursorColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(Intent intent, int startId) {
|
||||
super.onStart(intent, startId);
|
||||
|
||||
if (this.compatibilityStart) {
|
||||
handleStart(intent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
|
||||
final int result;
|
||||
try {
|
||||
this.compatibilityStart = false;
|
||||
result = super.onStartCommand(intent, flags, startId);
|
||||
handleStart(intent);
|
||||
} finally {
|
||||
this.compatibilityStart = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void handleStart(@Nullable Intent intent) {
|
||||
if ( intent != null ) {
|
||||
|
||||
if (isInitIntent(intent)) {
|
||||
|
||||
boolean createView = intent.getBooleanExtra(AndroidExternalListenersContainer.INIT_ACTION_CREATE_VIEW_EXTRA, false);
|
||||
if (createView) {
|
||||
hideNotification();
|
||||
createView();
|
||||
} else {
|
||||
showNotification();
|
||||
}
|
||||
}
|
||||
|
||||
if (viewCreated) {
|
||||
intentHandler.onIntent(this, intent);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isInitIntent(@NotNull Intent intent) {
|
||||
return intent.getAction().equals(AndroidExternalListenersContainer.INIT_ACTION);
|
||||
}
|
||||
|
||||
private void hideNotification() {
|
||||
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.cancel(NOTIFICATION_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewMinimized() {
|
||||
showNotification();
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewHidden() {
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
private void showNotification() {
|
||||
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
|
||||
builder.setSmallIcon(R.drawable.kb_logo);
|
||||
builder.setContentTitle(getText(R.string.c_app_name));
|
||||
builder.setContentText(getString(R.string.open_onscreen_calculator));
|
||||
|
||||
final Intent intent = createShowOnscreenViewIntent(this);
|
||||
builder.setContentIntent(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
|
||||
|
||||
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.notify(NOTIFICATION_ID, builder.getNotification());
|
||||
}
|
||||
|
||||
public static void showNotification(@NotNull Context context) {
|
||||
final Intent intent = new Intent(AndroidExternalListenersContainer.INIT_ACTION);
|
||||
intent.setClass(context, getIntentListenerClass());
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
public static void showOnscreenView(@NotNull Context context) {
|
||||
final Intent intent = createShowOnscreenViewIntent(context);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Intent createShowOnscreenViewIntent(@NotNull Context context) {
|
||||
final Intent intent = new Intent(AndroidExternalListenersContainer.INIT_ACTION);
|
||||
intent.setClass(context, getIntentListenerClass());
|
||||
intent.putExtra(AndroidExternalListenersContainer.INIT_ACTION_CREATE_VIEW_EXTRA, true);
|
||||
return intent;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,461 @@
|
||||
package org.solovyev.android.calculator.onscreen;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.util.Log;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.ImageView;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.CalculatorButton;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/21/12
|
||||
* Time: 9:03 PM
|
||||
*/
|
||||
public class CalculatorOnscreenView {
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static final String TAG = CalculatorOnscreenView.class.getSimpleName();
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private View root;
|
||||
|
||||
@NotNull
|
||||
private View content;
|
||||
|
||||
@NotNull
|
||||
private View header;
|
||||
|
||||
@NotNull
|
||||
private AndroidCalculatorEditorView editorView;
|
||||
|
||||
@NotNull
|
||||
private AndroidCalculatorDisplayView displayView;
|
||||
|
||||
@NotNull
|
||||
private Context context;
|
||||
|
||||
private int width;
|
||||
|
||||
private int height;
|
||||
|
||||
private int unfoldedHeight;
|
||||
|
||||
@NotNull
|
||||
private String cursorColor;
|
||||
|
||||
@Nullable
|
||||
private OnscreenViewListener viewListener;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private boolean minimized = false;
|
||||
|
||||
private boolean attached = false;
|
||||
|
||||
private boolean folded = false;
|
||||
|
||||
private boolean initialized = false;
|
||||
|
||||
private boolean hidden = true;
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private CalculatorOnscreenView() {
|
||||
}
|
||||
|
||||
public static CalculatorOnscreenView newInstance(@NotNull Context context,
|
||||
@NotNull CalculatorOnscreenViewDef def,
|
||||
@NotNull String cursorColor,
|
||||
@Nullable OnscreenViewListener viewListener) {
|
||||
final CalculatorOnscreenView result = new CalculatorOnscreenView();
|
||||
|
||||
result.root = View.inflate(context, R.layout.onscreen_layout, null);
|
||||
result.context = context;
|
||||
result.width = def.getWidth();
|
||||
result.height = def.getHeight();
|
||||
result.cursorColor = cursorColor;
|
||||
result.viewListener = viewListener;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public void updateDisplayState(@NotNull CalculatorDisplayViewState displayState) {
|
||||
checkInit();
|
||||
displayView.setState(displayState);
|
||||
}
|
||||
|
||||
public void updateEditorState(@NotNull CalculatorEditorViewState editorState) {
|
||||
checkInit();
|
||||
editorView.setState(editorState);
|
||||
}
|
||||
|
||||
private void setHeight(int height) {
|
||||
checkInit();
|
||||
|
||||
this.height = height;
|
||||
|
||||
final WindowManager.LayoutParams params = (WindowManager.LayoutParams) root.getLayoutParams();
|
||||
|
||||
params.height = height;
|
||||
|
||||
getWindowManager().updateViewLayout(root, params);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* LIFECYCLE
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private void init() {
|
||||
|
||||
if (!initialized) {
|
||||
for (final CalculatorButton widgetButton : CalculatorButton.values()) {
|
||||
final View button = root.findViewById(widgetButton.getButtonId());
|
||||
if (button != null) {
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
widgetButton.onClick(context);
|
||||
if ( widgetButton == CalculatorButton.app ) {
|
||||
minimize();
|
||||
}
|
||||
}
|
||||
});
|
||||
button.setOnLongClickListener(new View.OnLongClickListener() {
|
||||
@Override
|
||||
public boolean onLongClick(View v) {
|
||||
widgetButton.onLongClick(context);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
|
||||
header = root.findViewById(R.id.onscreen_header);
|
||||
content = root.findViewById(R.id.onscreen_content);
|
||||
|
||||
displayView = (AndroidCalculatorDisplayView) root.findViewById(R.id.calculator_display);
|
||||
displayView.init(this.context, false);
|
||||
|
||||
editorView = (AndroidCalculatorEditorView) root.findViewById(R.id.calculator_editor);
|
||||
editorView.init(this.context);
|
||||
|
||||
final View onscreenFoldButton = root.findViewById(R.id.onscreen_fold_button);
|
||||
onscreenFoldButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (folded) {
|
||||
unfold();
|
||||
} else {
|
||||
fold();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
final View onscreenHideButton = root.findViewById(R.id.onscreen_minimize_button);
|
||||
onscreenHideButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
minimize();
|
||||
}
|
||||
});
|
||||
|
||||
root.findViewById(R.id.onscreen_close_button).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
hide();
|
||||
}
|
||||
});
|
||||
|
||||
final ImageView onscreenTitleImageView = (ImageView) root.findViewById(R.id.onscreen_title);
|
||||
onscreenTitleImageView.setOnTouchListener(new WindowDragTouchListener(wm, root));
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void checkInit() {
|
||||
if (!initialized) {
|
||||
throw new IllegalStateException("init() must be called!");
|
||||
}
|
||||
}
|
||||
|
||||
public void show() {
|
||||
if (hidden) {
|
||||
init();
|
||||
attach();
|
||||
|
||||
hidden = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void attach() {
|
||||
checkInit();
|
||||
|
||||
final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
if (!attached) {
|
||||
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
|
||||
width,
|
||||
height,
|
||||
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(root, params);
|
||||
attached = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void fold() {
|
||||
if (!folded) {
|
||||
final WindowManager.LayoutParams params = (WindowManager.LayoutParams) root.getLayoutParams();
|
||||
unfoldedHeight = params.height;
|
||||
int newHeight = header.getHeight();
|
||||
content.setVisibility(View.GONE);
|
||||
setHeight(newHeight);
|
||||
folded = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void unfold() {
|
||||
if (folded) {
|
||||
content.setVisibility(View.VISIBLE);
|
||||
setHeight(unfoldedHeight);
|
||||
folded = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void detach() {
|
||||
checkInit();
|
||||
|
||||
if (attached) {
|
||||
getWindowManager().removeView(root);
|
||||
attached = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void minimize() {
|
||||
checkInit();
|
||||
if (!minimized) {
|
||||
detach();
|
||||
|
||||
if (viewListener != null) {
|
||||
viewListener.onViewMinimized();
|
||||
}
|
||||
|
||||
minimized = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void hide() {
|
||||
checkInit();
|
||||
|
||||
if (!hidden) {
|
||||
|
||||
detach();
|
||||
|
||||
if (viewListener != null) {
|
||||
viewListener.onViewHidden();
|
||||
}
|
||||
|
||||
hidden = true;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private WindowManager getWindowManager() {
|
||||
return ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorOnscreenViewDef getCalculatorOnscreenViewDef() {
|
||||
final WindowManager.LayoutParams params = (WindowManager.LayoutParams) root.getLayoutParams();
|
||||
return CalculatorOnscreenViewDef.newInstance(width, height, params.x, params.y);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static class WindowDragTouchListener implements View.OnTouchListener {
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static final float DIST_EPS = 0f;
|
||||
private static final float DIST_MAX = 100000f;
|
||||
private static final long TIME_EPS = 0L;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private final WindowManager wm;
|
||||
|
||||
private float x0;
|
||||
|
||||
private float y0;
|
||||
|
||||
private long time = 0;
|
||||
|
||||
@NotNull
|
||||
private final View view;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public WindowDragTouchListener(@NotNull WindowManager wm,
|
||||
@NotNull View view) {
|
||||
this.wm = wm;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
|
||||
//Log.d(TAG, "Action: " + event.getAction());
|
||||
|
||||
final float x1 = event.getX();
|
||||
final float y1 = event.getY();
|
||||
|
||||
switch (event.getAction()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
Log.d(TAG, "0:" + toString(x0, y0) + ", 1: " + toString(x1, y1));
|
||||
x0 = x1;
|
||||
y0 = y1;
|
||||
return true;
|
||||
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
final long currentTime = System.currentTimeMillis();
|
||||
|
||||
if ( currentTime - time >= TIME_EPS ) {
|
||||
time = currentTime;
|
||||
for (int i = 0; i < event.getHistorySize(); i++) {
|
||||
final float xi = event.getHistoricalX(i);
|
||||
final float yi = event.getHistoricalY(i);
|
||||
processMove(xi, yi);
|
||||
}
|
||||
processMove(x1, y1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void processMove(float x1, float y1) {
|
||||
final float Δx = x1 - x0;
|
||||
final float Δy = y1 - y0;
|
||||
|
||||
final WindowManager.LayoutParams params = (WindowManager.LayoutParams) view.getLayoutParams();
|
||||
Log.d(TAG, "0:" + toString(x0, y0) + ", 1: " + toString(x1, y1) + ", Δ: " + toString(Δx, Δy) + ", params: " + toString(params.x, params.y));
|
||||
|
||||
boolean xInBounds = isDistanceInBounds(Δx);
|
||||
boolean yInBounds = isDistanceInBounds(Δy);
|
||||
if (xInBounds || yInBounds) {
|
||||
|
||||
if (xInBounds) {
|
||||
params.x = (int) (params.x + Δx);
|
||||
}
|
||||
|
||||
if (yInBounds) {
|
||||
params.y = (int) (params.y + Δy);
|
||||
}
|
||||
|
||||
wm.updateViewLayout(view, params);
|
||||
|
||||
if (xInBounds) {
|
||||
x0 = x1;
|
||||
}
|
||||
|
||||
if (yInBounds) {
|
||||
y0 = y1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isDistanceInBounds(float δx) {
|
||||
δx = Math.abs(δx);
|
||||
return δx >= DIST_EPS && δx < DIST_MAX;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String toString(float x, float y) {
|
||||
return "(" + formatFloat(x) + ", " + formatFloat(y) + ")";
|
||||
}
|
||||
|
||||
private static String formatFloat(float value) {
|
||||
if (value >= 0) {
|
||||
return "+" + String.format("%.2f", value);
|
||||
} else {
|
||||
return String.format("%.2f", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
package org.solovyev.android.calculator.onscreen;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/21/12
|
||||
* Time: 10:55 PM
|
||||
*/
|
||||
public class CalculatorOnscreenViewDef {
|
||||
|
||||
private int width;
|
||||
private int height;
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
private CalculatorOnscreenViewDef() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorOnscreenViewDef newInstance(int width, int height, int x, int y) {
|
||||
final CalculatorOnscreenViewDef result = new CalculatorOnscreenViewDef();
|
||||
result.width = width;
|
||||
result.height = height;
|
||||
result.x = x;
|
||||
result.y = y;
|
||||
return result;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setWidth(int width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package org.solovyev.android.calculator.onscreen;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/21/12
|
||||
* Time: 9:45 PM
|
||||
*/
|
||||
public interface OnscreenViewListener {
|
||||
|
||||
// view minimized == view is in the action bar
|
||||
void onViewMinimized();
|
||||
|
||||
// view hidden == view closed
|
||||
void onViewHidden();
|
||||
}
|
Reference in New Issue
Block a user