code reformat
This commit is contained in:
@@ -9,27 +9,27 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.solovyev.android.calculator.CalculatorPreferences;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/20/12
|
||||
* Time: 11:05 PM
|
||||
*/
|
||||
* User: serso
|
||||
* Date: 11/20/12
|
||||
* Time: 11:05 PM
|
||||
*/
|
||||
public final class CalculatorOnscreenBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
public CalculatorOnscreenBroadcastReceiver() {
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -27,111 +27,111 @@ import org.solovyev.android.calculator.external.ExternalCalculatorStateUpdater;
|
||||
*/
|
||||
public class CalculatorOnscreenService extends Service implements ExternalCalculatorStateUpdater, OnscreenViewListener {
|
||||
|
||||
private static final int NOTIFICATION_ID = 9031988; // my birthday =)
|
||||
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;
|
||||
@NotNull
|
||||
private final ExternalCalculatorIntentHandler intentHandler = new DefaultExternalCalculatorIntentHandler(this);
|
||||
public static final Class<CalculatorOnscreenBroadcastReceiver> INTENT_LISTENER_CLASS = CalculatorOnscreenBroadcastReceiver.class;
|
||||
|
||||
@Nullable
|
||||
private static String cursorColor;
|
||||
@Nullable
|
||||
private static String cursorColor;
|
||||
|
||||
@NotNull
|
||||
private CalculatorOnscreenView view;
|
||||
@NotNull
|
||||
private CalculatorOnscreenView view;
|
||||
|
||||
private boolean compatibilityStart = true;
|
||||
|
||||
private boolean viewCreated = false;
|
||||
private boolean viewCreated = false;
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
}
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
private void createView() {
|
||||
if (!viewCreated) {
|
||||
final WindowManager wm = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE));
|
||||
private void createView() {
|
||||
if (!viewCreated) {
|
||||
final WindowManager wm = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE));
|
||||
|
||||
final DisplayMetrics dm = getResources().getDisplayMetrics();
|
||||
final DisplayMetrics dm = getResources().getDisplayMetrics();
|
||||
|
||||
int twoThirdWidth = 2 * wm.getDefaultDisplay().getWidth() / 3;
|
||||
int twoThirdHeight = 2 * wm.getDefaultDisplay().getHeight() / 3;
|
||||
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));
|
||||
twoThirdWidth = Math.min(twoThirdWidth, twoThirdHeight);
|
||||
twoThirdHeight = Math.max(twoThirdWidth, getHeight(twoThirdWidth));
|
||||
|
||||
final int baseWidth = Views.toPixels(dm, 300);
|
||||
final int width0 = Math.min(twoThirdWidth, baseWidth);
|
||||
final int height0 = Math.min(twoThirdHeight, getHeight(baseWidth));
|
||||
final int baseWidth = Views.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);
|
||||
final int width = Math.min(width0, height0);
|
||||
final int height = Math.max(width0, height0);
|
||||
|
||||
view = CalculatorOnscreenView.newInstance(this, CalculatorOnscreenViewState.newInstance(width, height, -1, -1), getCursorColor(this), this);
|
||||
view.show();
|
||||
view = CalculatorOnscreenView.newInstance(this, CalculatorOnscreenViewState.newInstance(width, height, -1, -1), getCursorColor(this), this);
|
||||
view.show();
|
||||
|
||||
startCalculatorListening();
|
||||
startCalculatorListening();
|
||||
|
||||
viewCreated = true;
|
||||
}
|
||||
}
|
||||
viewCreated = true;
|
||||
}
|
||||
}
|
||||
|
||||
private int getHeight(int width) {
|
||||
private int getHeight(int width) {
|
||||
return 4 * width / 3;
|
||||
}
|
||||
|
||||
private void startCalculatorListening() {
|
||||
Locator.getInstance().getExternalListenersContainer().addExternalListener(getIntentListenerClass());
|
||||
}
|
||||
Locator.getInstance().getExternalListenersContainer().addExternalListener(getIntentListenerClass());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Class<?> getIntentListenerClass() {
|
||||
return INTENT_LISTENER_CLASS;
|
||||
}
|
||||
@NotNull
|
||||
private static Class<?> getIntentListenerClass() {
|
||||
return INTENT_LISTENER_CLASS;
|
||||
}
|
||||
|
||||
private void stopCalculatorListening() {
|
||||
private void stopCalculatorListening() {
|
||||
Locator.getInstance().getExternalListenersContainer().removeExternalListener(getIntentListenerClass());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
stopCalculatorListening();
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
stopCalculatorListening();
|
||||
if (viewCreated) {
|
||||
this.view.hide();
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(@NotNull Context context, @NotNull CalculatorEditorViewState editorState, @NotNull CalculatorDisplayViewState displayState) {
|
||||
view.updateDisplayState(displayState);
|
||||
view.updateEditorState(editorState);
|
||||
}
|
||||
@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;
|
||||
}
|
||||
@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);
|
||||
@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) {
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
|
||||
final int result;
|
||||
try {
|
||||
@@ -143,80 +143,80 @@ public class CalculatorOnscreenService extends Service implements ExternalCalcul
|
||||
}
|
||||
|
||||
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 handleStart(@Nullable Intent intent) {
|
||||
if (intent != null) {
|
||||
|
||||
private void hideNotification() {
|
||||
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.cancel(NOTIFICATION_ID);
|
||||
}
|
||||
if (isInitIntent(intent)) {
|
||||
|
||||
@Override
|
||||
public void onViewMinimized() {
|
||||
showNotification();
|
||||
stopSelf();
|
||||
}
|
||||
boolean createView = intent.getBooleanExtra(AndroidExternalListenersContainer.INIT_ACTION_CREATE_VIEW_EXTRA, false);
|
||||
if (createView) {
|
||||
hideNotification();
|
||||
createView();
|
||||
} else {
|
||||
showNotification();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewHidden() {
|
||||
stopSelf();
|
||||
}
|
||||
if (viewCreated) {
|
||||
intentHandler.onIntent(this, intent);
|
||||
}
|
||||
|
||||
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));
|
||||
builder.setOngoing(true);
|
||||
}
|
||||
}
|
||||
|
||||
final Intent intent = createShowOnscreenViewIntent(this);
|
||||
builder.setContentIntent(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
|
||||
private boolean isInitIntent(@NotNull Intent intent) {
|
||||
return intent.getAction().equals(AndroidExternalListenersContainer.INIT_ACTION);
|
||||
}
|
||||
|
||||
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.notify(NOTIFICATION_ID, builder.getNotification());
|
||||
}
|
||||
private void hideNotification() {
|
||||
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.cancel(NOTIFICATION_ID);
|
||||
}
|
||||
|
||||
public static void showNotification(@NotNull Context context) {
|
||||
final Intent intent = new Intent(AndroidExternalListenersContainer.INIT_ACTION);
|
||||
intent.setClass(context, getIntentListenerClass());
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
@Override
|
||||
public void onViewMinimized() {
|
||||
showNotification();
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
public static void showOnscreenView(@NotNull Context context) {
|
||||
final Intent intent = createShowOnscreenViewIntent(context);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
@Override
|
||||
public void onViewHidden() {
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
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));
|
||||
builder.setOngoing(true);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -22,16 +22,16 @@ public class CalculatorOnscreenStartActivity extends Activity {
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
if (Android.isComponentEnabled(this, CalculatorOnscreenStartActivity.class)) {
|
||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
if (Android.isComponentEnabled(this, CalculatorOnscreenStartActivity.class)) {
|
||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
if (!CalculatorPreferences.OnscreenCalculator.removeIconDialogShown.getPreference(prefs)) {
|
||||
FixableMessagesDialog.showDialog(Arrays.asList(new FixableMessage(getString(R.string.cpp_onscreen_remove_icon_message), MessageType.warning, new RemoveIconFixableError(this))), this, false);
|
||||
CalculatorPreferences.OnscreenCalculator.removeIconDialogShown.putPreference(prefs, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CalculatorOnscreenService.showOnscreenView(this);
|
||||
CalculatorOnscreenService.showOnscreenView(this);
|
||||
|
||||
this.finish();
|
||||
}
|
||||
|
@@ -13,11 +13,7 @@ import android.view.WindowManager;
|
||||
import android.widget.ImageView;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.solovyev.android.calculator.AndroidCalculatorDisplayView;
|
||||
import org.solovyev.android.calculator.AndroidCalculatorEditorView;
|
||||
import org.solovyev.android.calculator.CalculatorButton;
|
||||
import org.solovyev.android.calculator.CalculatorDisplayViewState;
|
||||
import org.solovyev.android.calculator.CalculatorEditorViewState;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.prefs.Preference;
|
||||
|
||||
/**
|
||||
@@ -26,39 +22,39 @@ import org.solovyev.android.prefs.Preference;
|
||||
* Time: 9:03 PM
|
||||
*/
|
||||
public class CalculatorOnscreenView {
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTANTS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static final String TAG = CalculatorOnscreenView.class.getSimpleName();
|
||||
private static final String TAG = CalculatorOnscreenView.class.getSimpleName();
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATIC
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private static final Preference<CalculatorOnscreenViewState> viewStatePreference = new CalculatorOnscreenViewState.Preference("onscreen_view_state", CalculatorOnscreenViewState.newDefaultState());
|
||||
private static final Preference<CalculatorOnscreenViewState> viewStatePreference = new CalculatorOnscreenViewState.Preference("onscreen_view_state", CalculatorOnscreenViewState.newDefaultState());
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@NotNull
|
||||
private View root;
|
||||
@NotNull
|
||||
private View root;
|
||||
|
||||
@NotNull
|
||||
private View content;
|
||||
@NotNull
|
||||
private View content;
|
||||
|
||||
@NotNull
|
||||
private View header;
|
||||
@@ -70,133 +66,133 @@ public class CalculatorOnscreenView {
|
||||
private AndroidCalculatorDisplayView displayView;
|
||||
|
||||
@NotNull
|
||||
private Context context;
|
||||
private Context context;
|
||||
|
||||
@NotNull
|
||||
private CalculatorOnscreenViewState state = CalculatorOnscreenViewState.newDefaultState();
|
||||
@NotNull
|
||||
private CalculatorOnscreenViewState state = CalculatorOnscreenViewState.newDefaultState();
|
||||
|
||||
@NotNull
|
||||
private String cursorColor;
|
||||
@NotNull
|
||||
private String cursorColor;
|
||||
|
||||
@Nullable
|
||||
private OnscreenViewListener viewListener;
|
||||
@Nullable
|
||||
private OnscreenViewListener viewListener;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* STATES
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private boolean minimized = false;
|
||||
private boolean minimized = false;
|
||||
|
||||
private boolean attached = false;
|
||||
private boolean attached = false;
|
||||
|
||||
private boolean folded = false;
|
||||
private boolean folded = false;
|
||||
|
||||
private boolean initialized = false;
|
||||
private boolean initialized = false;
|
||||
|
||||
private boolean hidden = true;
|
||||
private boolean hidden = true;
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private CalculatorOnscreenView() {
|
||||
}
|
||||
private CalculatorOnscreenView() {
|
||||
}
|
||||
|
||||
public static CalculatorOnscreenView newInstance(@NotNull Context context,
|
||||
@NotNull CalculatorOnscreenViewState state,
|
||||
@NotNull String cursorColor,
|
||||
@Nullable OnscreenViewListener viewListener) {
|
||||
final CalculatorOnscreenView result = new CalculatorOnscreenView();
|
||||
public static CalculatorOnscreenView newInstance(@NotNull Context context,
|
||||
@NotNull CalculatorOnscreenViewState state,
|
||||
@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.cursorColor = cursorColor;
|
||||
result.viewListener = viewListener;
|
||||
result.root = View.inflate(context, R.layout.onscreen_layout, null);
|
||||
result.context = context;
|
||||
result.cursorColor = cursorColor;
|
||||
result.viewListener = viewListener;
|
||||
|
||||
final CalculatorOnscreenViewState persistedState = readState(context);
|
||||
if (persistedState != null) {
|
||||
result.state = persistedState;
|
||||
} else {
|
||||
result.state = state;
|
||||
}
|
||||
final CalculatorOnscreenViewState persistedState = readState(context);
|
||||
if (persistedState != null) {
|
||||
result.state = persistedState;
|
||||
} else {
|
||||
result.state = state;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public void updateDisplayState(@NotNull CalculatorDisplayViewState displayState) {
|
||||
checkInit();
|
||||
public void updateDisplayState(@NotNull CalculatorDisplayViewState displayState) {
|
||||
checkInit();
|
||||
displayView.setState(displayState);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateEditorState(@NotNull CalculatorEditorViewState editorState) {
|
||||
checkInit();
|
||||
public void updateEditorState(@NotNull CalculatorEditorViewState editorState) {
|
||||
checkInit();
|
||||
editorView.setState(editorState);
|
||||
}
|
||||
}
|
||||
|
||||
private void setHeight(int height) {
|
||||
checkInit();
|
||||
private void setHeight(int height) {
|
||||
checkInit();
|
||||
|
||||
final WindowManager.LayoutParams params = (WindowManager.LayoutParams) root.getLayoutParams();
|
||||
final WindowManager.LayoutParams params = (WindowManager.LayoutParams) root.getLayoutParams();
|
||||
|
||||
params.height = height;
|
||||
params.height = height;
|
||||
|
||||
getWindowManager().updateViewLayout(root, params);
|
||||
}
|
||||
getWindowManager().updateViewLayout(root, params);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* LIFECYCLE
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* LIFECYCLE
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
private void init() {
|
||||
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) {
|
||||
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 ) {
|
||||
if (widgetButton == CalculatorButton.app) {
|
||||
minimize();
|
||||
}
|
||||
}
|
||||
});
|
||||
button.setOnLongClickListener(new View.OnLongClickListener() {
|
||||
}
|
||||
});
|
||||
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);
|
||||
final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
|
||||
header = root.findViewById(R.id.onscreen_header);
|
||||
content = root.findViewById(R.id.onscreen_content);
|
||||
content = root.findViewById(R.id.onscreen_content);
|
||||
|
||||
displayView = (AndroidCalculatorDisplayView) root.findViewById(R.id.calculator_display);
|
||||
displayView.init(this.context, false);
|
||||
@@ -204,320 +200,320 @@ public class CalculatorOnscreenView {
|
||||
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(
|
||||
state.getWidth(),
|
||||
state.getHeight(),
|
||||
state.getX(),
|
||||
state.getY(),
|
||||
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
|
||||
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
|
||||
PixelFormat.TRANSLUCENT);
|
||||
|
||||
params.gravity = Gravity.TOP | Gravity.LEFT;
|
||||
|
||||
wm.addView(root, params);
|
||||
attached = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void fold() {
|
||||
if (!folded) {
|
||||
int newHeight = header.getHeight();
|
||||
content.setVisibility(View.GONE);
|
||||
setHeight(newHeight);
|
||||
folded = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void unfold() {
|
||||
if (folded) {
|
||||
content.setVisibility(View.VISIBLE);
|
||||
setHeight(state.getHeight());
|
||||
folded = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void detach() {
|
||||
checkInit();
|
||||
|
||||
if (attached) {
|
||||
getWindowManager().removeView(root);
|
||||
attached = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void minimize() {
|
||||
checkInit();
|
||||
if (!minimized) {
|
||||
persistState(context, getCurrentState(!folded));
|
||||
|
||||
detach();
|
||||
|
||||
if (viewListener != null) {
|
||||
viewListener.onViewMinimized();
|
||||
}
|
||||
|
||||
minimized = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void persistState(@NotNull Context context, @NotNull CalculatorOnscreenViewState state) {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
viewStatePreference.putPreference(preferences, state);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CalculatorOnscreenViewState readState(@NotNull Context context) {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
if (viewStatePreference.isSet(preferences)) {
|
||||
return viewStatePreference.getPreference(preferences);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void hide() {
|
||||
checkInit();
|
||||
|
||||
if (!hidden) {
|
||||
|
||||
persistState(context, getCurrentState(!folded));
|
||||
|
||||
detach();
|
||||
|
||||
if (viewListener != null) {
|
||||
viewListener.onViewHidden();
|
||||
}
|
||||
|
||||
hidden = true;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private WindowManager getWindowManager() {
|
||||
return ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorOnscreenViewState getCurrentState(boolean useRealSize) {
|
||||
final WindowManager.LayoutParams params = (WindowManager.LayoutParams) root.getLayoutParams();
|
||||
if (useRealSize) {
|
||||
return CalculatorOnscreenViewState.newInstance(params.width, params.height, params.x, params.y);
|
||||
} else {
|
||||
return CalculatorOnscreenViewState.newInstance(state.getWidth(), state.getHeight(), 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 int orientation;
|
||||
|
||||
private float x0;
|
||||
|
||||
private float y0;
|
||||
|
||||
private long time = 0;
|
||||
|
||||
@NotNull
|
||||
private final View view;
|
||||
|
||||
private int displayWidth;
|
||||
|
||||
private int displayHeight;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public WindowDragTouchListener(@NotNull WindowManager wm,
|
||||
@NotNull View view) {
|
||||
this.wm = wm;
|
||||
this.view = view;
|
||||
initDisplayParams();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
if (orientation != this.wm.getDefaultDisplay().getOrientation()) {
|
||||
// orientation has changed => we need to check display width/height each time window moved
|
||||
initDisplayParams();
|
||||
}
|
||||
|
||||
//Log.d(TAG, "Action: " + event.getAction());
|
||||
|
||||
final float x1 = event.getRawX();
|
||||
final float y1 = event.getRawY();
|
||||
|
||||
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;
|
||||
processMove(x1, y1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void initDisplayParams() {
|
||||
this.orientation = this.wm.getDefaultDisplay().getOrientation();
|
||||
|
||||
final DisplayMetrics displayMetrics = new DisplayMetrics();
|
||||
wm.getDefaultDisplay().getMetrics(displayMetrics);
|
||||
|
||||
this.displayWidth = displayMetrics.widthPixels;
|
||||
this.displayHeight = displayMetrics.heightPixels;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
params.x = Math.min(Math.max(params.x, 0), displayWidth - params.width);
|
||||
params.y = Math.min(Math.max(params.y, 0), displayHeight - params.height);
|
||||
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(
|
||||
state.getWidth(),
|
||||
state.getHeight(),
|
||||
state.getX(),
|
||||
state.getY(),
|
||||
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
|
||||
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
|
||||
PixelFormat.TRANSLUCENT);
|
||||
|
||||
params.gravity = Gravity.TOP | Gravity.LEFT;
|
||||
|
||||
wm.addView(root, params);
|
||||
attached = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void fold() {
|
||||
if (!folded) {
|
||||
int newHeight = header.getHeight();
|
||||
content.setVisibility(View.GONE);
|
||||
setHeight(newHeight);
|
||||
folded = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void unfold() {
|
||||
if (folded) {
|
||||
content.setVisibility(View.VISIBLE);
|
||||
setHeight(state.getHeight());
|
||||
folded = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void detach() {
|
||||
checkInit();
|
||||
|
||||
if (attached) {
|
||||
getWindowManager().removeView(root);
|
||||
attached = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void minimize() {
|
||||
checkInit();
|
||||
if (!minimized) {
|
||||
persistState(context, getCurrentState(!folded));
|
||||
|
||||
detach();
|
||||
|
||||
if (viewListener != null) {
|
||||
viewListener.onViewMinimized();
|
||||
}
|
||||
|
||||
minimized = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void persistState(@NotNull Context context, @NotNull CalculatorOnscreenViewState state) {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
viewStatePreference.putPreference(preferences, state);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CalculatorOnscreenViewState readState(@NotNull Context context) {
|
||||
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
if (viewStatePreference.isSet(preferences)) {
|
||||
return viewStatePreference.getPreference(preferences);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void hide() {
|
||||
checkInit();
|
||||
|
||||
if (!hidden) {
|
||||
|
||||
persistState(context, getCurrentState(!folded));
|
||||
|
||||
detach();
|
||||
|
||||
if (viewListener != null) {
|
||||
viewListener.onViewHidden();
|
||||
}
|
||||
|
||||
hidden = true;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private WindowManager getWindowManager() {
|
||||
return ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CalculatorOnscreenViewState getCurrentState(boolean useRealSize) {
|
||||
final WindowManager.LayoutParams params = (WindowManager.LayoutParams) root.getLayoutParams();
|
||||
if (useRealSize) {
|
||||
return CalculatorOnscreenViewState.newInstance(params.width, params.height, params.x, params.y);
|
||||
} else {
|
||||
return CalculatorOnscreenViewState.newInstance(state.getWidth(), state.getHeight(), 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 int orientation;
|
||||
|
||||
private float x0;
|
||||
|
||||
private float y0;
|
||||
|
||||
private long time = 0;
|
||||
|
||||
@NotNull
|
||||
private final View view;
|
||||
|
||||
private int displayWidth;
|
||||
|
||||
private int displayHeight;
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
public WindowDragTouchListener(@NotNull WindowManager wm,
|
||||
@NotNull View view) {
|
||||
this.wm = wm;
|
||||
this.view = view;
|
||||
initDisplayParams();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
if (orientation != this.wm.getDefaultDisplay().getOrientation()) {
|
||||
// orientation has changed => we need to check display width/height each time window moved
|
||||
initDisplayParams();
|
||||
}
|
||||
|
||||
//Log.d(TAG, "Action: " + event.getAction());
|
||||
|
||||
final float x1 = event.getRawX();
|
||||
final float y1 = event.getRawY();
|
||||
|
||||
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;
|
||||
processMove(x1, y1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void initDisplayParams() {
|
||||
this.orientation = this.wm.getDefaultDisplay().getOrientation();
|
||||
|
||||
final DisplayMetrics displayMetrics = new DisplayMetrics();
|
||||
wm.getDefaultDisplay().getMetrics(displayMetrics);
|
||||
|
||||
this.displayWidth = displayMetrics.widthPixels;
|
||||
this.displayHeight = displayMetrics.heightPixels;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
params.x = Math.min(Math.max(params.x, 0), displayWidth - params.width);
|
||||
params.y = Math.min(Math.max(params.y, 0), displayHeight - params.height);
|
||||
|
||||
wm.updateViewLayout(view, params);
|
||||
wm.updateViewLayout(view, params);
|
||||
|
||||
if (xInBounds) {
|
||||
x0 = x1;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -20,147 +20,147 @@ import java.util.Map;
|
||||
*/
|
||||
public class CalculatorOnscreenViewState implements Parcelable {
|
||||
|
||||
private static final String TAG = CalculatorOnscreenViewState.class.getSimpleName();
|
||||
private static final String TAG = CalculatorOnscreenViewState.class.getSimpleName();
|
||||
|
||||
public static final Parcelable.Creator<CalculatorOnscreenViewState> CREATOR = new Parcelable.Creator<CalculatorOnscreenViewState>() {
|
||||
public CalculatorOnscreenViewState createFromParcel(@NotNull Parcel in) {
|
||||
return CalculatorOnscreenViewState.fromParcel(in);
|
||||
}
|
||||
public static final Parcelable.Creator<CalculatorOnscreenViewState> CREATOR = new Parcelable.Creator<CalculatorOnscreenViewState>() {
|
||||
public CalculatorOnscreenViewState createFromParcel(@NotNull Parcel in) {
|
||||
return CalculatorOnscreenViewState.fromParcel(in);
|
||||
}
|
||||
|
||||
public CalculatorOnscreenViewState[] newArray(int size) {
|
||||
return new CalculatorOnscreenViewState[size];
|
||||
}
|
||||
};
|
||||
public CalculatorOnscreenViewState[] newArray(int size) {
|
||||
return new CalculatorOnscreenViewState[size];
|
||||
}
|
||||
};
|
||||
|
||||
private int width;
|
||||
private int width;
|
||||
|
||||
private int height;
|
||||
private int height;
|
||||
|
||||
private int x;
|
||||
private int x;
|
||||
|
||||
private int y;
|
||||
private int y;
|
||||
|
||||
private CalculatorOnscreenViewState() {
|
||||
}
|
||||
private CalculatorOnscreenViewState() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static CalculatorOnscreenViewState fromParcel(@NotNull Parcel in) {
|
||||
final CalculatorOnscreenViewState result = new CalculatorOnscreenViewState();
|
||||
result.width = in.readInt();
|
||||
result.height = in.readInt();
|
||||
result.x = in.readInt();
|
||||
result.y = in.readInt();
|
||||
return result;
|
||||
}
|
||||
@NotNull
|
||||
private static CalculatorOnscreenViewState fromParcel(@NotNull Parcel in) {
|
||||
final CalculatorOnscreenViewState result = new CalculatorOnscreenViewState();
|
||||
result.width = in.readInt();
|
||||
result.height = in.readInt();
|
||||
result.x = in.readInt();
|
||||
result.y = in.readInt();
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorOnscreenViewState newDefaultState() {
|
||||
return newInstance(200, 400, 0, 0);
|
||||
}
|
||||
@NotNull
|
||||
public static CalculatorOnscreenViewState newDefaultState() {
|
||||
return newInstance(200, 400, 0, 0);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CalculatorOnscreenViewState newInstance(int width, int height, int x, int y) {
|
||||
final CalculatorOnscreenViewState result = new CalculatorOnscreenViewState();
|
||||
result.width = width;
|
||||
result.height = height;
|
||||
result.x = x;
|
||||
result.y = y;
|
||||
return result;
|
||||
}
|
||||
@NotNull
|
||||
public static CalculatorOnscreenViewState newInstance(int width, int height, int x, int y) {
|
||||
final CalculatorOnscreenViewState result = new CalculatorOnscreenViewState();
|
||||
result.width = width;
|
||||
result.height = height;
|
||||
result.x = x;
|
||||
result.y = y;
|
||||
return result;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setWidth(int width) {
|
||||
this.width = width;
|
||||
}
|
||||
public void setWidth(int width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(@NotNull Parcel out, int flags) {
|
||||
out.writeInt(width);
|
||||
out.writeInt(height);
|
||||
out.writeInt(x);
|
||||
out.writeInt(y);
|
||||
}
|
||||
@Override
|
||||
public void writeToParcel(@NotNull Parcel out, int flags) {
|
||||
out.writeInt(width);
|
||||
out.writeInt(height);
|
||||
out.writeInt(x);
|
||||
out.writeInt(y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CalculatorOnscreenViewState{" +
|
||||
"y=" + y +
|
||||
", x=" + x +
|
||||
", height=" + height +
|
||||
", width=" + width +
|
||||
'}';
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CalculatorOnscreenViewState{" +
|
||||
"y=" + y +
|
||||
", x=" + x +
|
||||
", height=" + height +
|
||||
", width=" + width +
|
||||
'}';
|
||||
}
|
||||
|
||||
public static class Preference extends AbstractPreference<CalculatorOnscreenViewState> {
|
||||
public static class Preference extends AbstractPreference<CalculatorOnscreenViewState> {
|
||||
|
||||
public Preference(@NotNull String key, @Nullable CalculatorOnscreenViewState defaultValue) {
|
||||
super(key, defaultValue);
|
||||
}
|
||||
public Preference(@NotNull String key, @Nullable CalculatorOnscreenViewState defaultValue) {
|
||||
super(key, defaultValue);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected CalculatorOnscreenViewState getPersistedValue(@NotNull SharedPreferences preferences) {
|
||||
try {
|
||||
final CalculatorOnscreenViewState result = new CalculatorOnscreenViewState();
|
||||
final JSONObject jsonObject = new JSONObject(preferences.getString(getKey(), "{}"));
|
||||
result.width = jsonObject.getInt("width");
|
||||
result.height = jsonObject.getInt("height");
|
||||
result.x = jsonObject.getInt("x");
|
||||
result.y = jsonObject.getInt("y");
|
||||
@Nullable
|
||||
@Override
|
||||
protected CalculatorOnscreenViewState getPersistedValue(@NotNull SharedPreferences preferences) {
|
||||
try {
|
||||
final CalculatorOnscreenViewState result = new CalculatorOnscreenViewState();
|
||||
final JSONObject jsonObject = new JSONObject(preferences.getString(getKey(), "{}"));
|
||||
result.width = jsonObject.getInt("width");
|
||||
result.height = jsonObject.getInt("height");
|
||||
result.x = jsonObject.getInt("x");
|
||||
result.y = jsonObject.getInt("y");
|
||||
|
||||
Log.d(TAG, "Reading onscreen view state: " + result);
|
||||
Log.d(TAG, "Reading onscreen view state: " + result);
|
||||
|
||||
return result;
|
||||
} catch (JSONException e) {
|
||||
return getDefaultValue();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} catch (JSONException e) {
|
||||
return getDefaultValue();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void putPersistedValue(@NotNull SharedPreferences.Editor editor, @NotNull CalculatorOnscreenViewState value) {
|
||||
final Map<String, Object> properties = new HashMap<String, Object>();
|
||||
properties.put("width", value.getWidth());
|
||||
properties.put("height", value.getHeight());
|
||||
properties.put("x", value.getX());
|
||||
properties.put("y", value.getY());
|
||||
@Override
|
||||
protected void putPersistedValue(@NotNull SharedPreferences.Editor editor, @NotNull CalculatorOnscreenViewState value) {
|
||||
final Map<String, Object> properties = new HashMap<String, Object>();
|
||||
properties.put("width", value.getWidth());
|
||||
properties.put("height", value.getHeight());
|
||||
properties.put("x", value.getX());
|
||||
properties.put("y", value.getY());
|
||||
|
||||
final JSONObject jsonObject = new JSONObject(properties);
|
||||
final JSONObject jsonObject = new JSONObject(properties);
|
||||
|
||||
final String json = jsonObject.toString();
|
||||
Log.d(TAG, "Persisting onscreen view state: " + json);
|
||||
editor.putString(getKey(), json);
|
||||
}
|
||||
}
|
||||
final String json = jsonObject.toString();
|
||||
Log.d(TAG, "Persisting onscreen view state: " + json);
|
||||
editor.putString(getKey(), json);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -7,9 +7,9 @@ package org.solovyev.android.calculator.onscreen;
|
||||
*/
|
||||
public interface OnscreenViewListener {
|
||||
|
||||
// view minimized == view is in the action bar
|
||||
void onViewMinimized();
|
||||
// view minimized == view is in the action bar
|
||||
void onViewMinimized();
|
||||
|
||||
// view hidden == view closed
|
||||
void onViewHidden();
|
||||
// view hidden == view closed
|
||||
void onViewHidden();
|
||||
}
|
||||
|
Reference in New Issue
Block a user