This commit is contained in:
Sergey Solovyev 2012-11-24 21:12:35 +04:00
parent cd41a35093
commit e54c5c97e6
24 changed files with 323 additions and 144 deletions

View File

@ -9,8 +9,9 @@
<!--TODO: REMOVE IN PRODUCTION--> <!--TODO: REMOVE IN PRODUCTION-->
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<!-- for overlay --> <!-- for onscreen -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="8"/> <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="8"/>
@ -78,6 +79,7 @@
<receiver android:name=".onscreen.CalculatorOnscreenBroadcastReceiver"> <receiver android:name=".onscreen.CalculatorOnscreenBroadcastReceiver">
<intent-filter> <intent-filter>
<action android:name="org.solovyev.android.calculator.INIT"/> <action android:name="org.solovyev.android.calculator.INIT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="org.solovyev.android.calculator.EDITOR_STATE_CHANGED"/> <action android:name="org.solovyev.android.calculator.EDITOR_STATE_CHANGED"/>
<action android:name="org.solovyev.android.calculator.DISPLAY_STATE_CHANGED"/> <action android:name="org.solovyev.android.calculator.DISPLAY_STATE_CHANGED"/>
</intent-filter> </intent-filter>

View File

@ -1,25 +1,58 @@
#!/bin/bash #!/bin/bash
declare -a skins=("WVGA854" "WQVGA400" "HVGA" "WQVGA432" "WVGA800" "QVGA") declare -a densities=("160" "213" "240" "320")
declare -a resolutions=("320x480" "480x640" "480x800" "480x854" "640x960" "1024x600" "1024x768" "1280x768" "1536x1152" "1920x1200")
declare -a targets=("android-16") declare -a targets=("android-16")
for target in ${targets[@]} for target in ${targets[@]}
do do
for skin in ${skins[@]} for density in ${densities[@]}
do do
$ANDROID_HOME/tools/android -s create avd -n AVD_$skin -t $target -b x86 -s $skin --force
for resolution in ${resolutions[@]}
do
name="AVD"
name="$name$density"
name="$name$resolution"
name="$name$target"
echo "Creating AVD $name"
echo "Density: $density"
echo "Resolution: $resolution"
echo "Target: $target"
$ANDROID_HOME/tools/android -s create avd -n $name -t $target -b x86 --force -s $resolution
# replace density in config.ini
sed -i "s/hw.lcd.density=240/hw.lcd.density=$density/g" $HOME/.android/avd/$name.avd/config.ini
arr=(${resolution//x/ })
echo "hw.lcd.width = ${arr[0]}" >> $HOME/.android/avd/$name.avd/config.ini
echo "hw.lcd.height = ${arr[1]}" >> $HOME/.android/avd/$name.avd/config.ini
done
done done
done done
for target in ${targets[@]} for target in ${targets[@]}
do do
for skin in ${skins[@]} for density in ${densities[@]}
do do
avdDeviceName=AVD_$skin
$ANDROID_HOME/tools/emulator -avd $avdDeviceName & for resolution in ${resolutions[@]}
$ANDROID_HOME/tools/monkeyrunner ./wait_device.py do
$ANDROID_HOME/platform-tools/adb -s emulator-5554 emu kill name="AVD"
name="$name$density"
name="$name$resolution"
name="$name$target"
$ANDROID_HOME/tools/emulator -avd $name &
$ANDROID_HOME/tools/monkeyrunner ./wait_device.py
$ANDROID_HOME/platform-tools/adb -s emulator-5554 emu kill
done
done done
done done

View File

@ -1,15 +1,24 @@
#!/bin/bash #!/bin/bash
declare -a skins=("WVGA854" "WQVGA400" "HVGA" "WQVGA432" "WVGA800" "QVGA") declare -a densities=("160" "213" "240" "320")
declare -a resolutions=("320x480" "480x640" "480x800" "480x854" "640x960" "1024x600" "1024x768" "1280x768" "1536x1152" "1920x1200")
declare -a targets=("android-16") declare -a targets=("android-16")
for target in ${targets[@]} for target in ${targets[@]}
do do
for skin in ${skins[@]} for density in ${densities[@]}
do do
$ANDROID_HOME/tools/android -s delete avd -n AVD_$skin
for resolution in ${resolutions[@]}
do
name="AVD"
name="$name$density"
name="$name$resolution"
name="$name$target"
$ANDROID_HOME/tools/android -s delete avd -n $name
done
done done
done done

View File

@ -9,7 +9,7 @@ outFilename = sys.argv[2]
print '' print ''
print 'Screenshot will be located in ' + outFolder + ' with name ' + outFilename; print 'Screenshot will be located in ' + outFolder + ' with name ' + outFilename;
apk = '/home/serso/projects/java/android/calculatorpp/calculatorpp/target/calculatorpp-1.5.2-SNAPSHOT.apk' apk = '/home/serso/projects/java/android/calculatorpp/calculatorpp/target/calculatorpp.apk'
package = 'org.solovyev.android.calculator' package = 'org.solovyev.android.calculator'
activity = 'org.solovyev.android.calculator.CalculatorActivity' activity = 'org.solovyev.android.calculator.CalculatorActivity'
deviceName = 'emulator-5554' deviceName = 'emulator-5554'
@ -20,7 +20,7 @@ def takeScreenshot (folder, filename):
return return
print 'Waiting for device ' + deviceName + '...' print 'Waiting for device ' + deviceName + '...'
device = MonkeyRunner.waitForConnection(30, deviceName) device = MonkeyRunner.waitForConnection(50, deviceName)
if device: if device:
# unlock device # unlock device
@ -38,11 +38,8 @@ if device:
print 'Starting activity ' + runComponent + '...' print 'Starting activity ' + runComponent + '...'
device.startActivity(component=runComponent) device.startActivity(component=runComponent)
# close all dialogs
device.shell('input keyevent 4')
# sleep while application will be loaded # sleep while application will be loaded
MonkeyRunner.sleep(2); MonkeyRunner.sleep(10);
print 'Taking screenshot...' print 'Taking screenshot...'
takeScreenshot(outFolder, outFilename + '_' + str(time.time()) ); takeScreenshot(outFolder, outFilename + '_' + str(time.time()) );

View File

@ -1,15 +1,25 @@
#!/bin/bash #!/bin/bash
declare -a skins=("WVGA854" "WQVGA400" "HVGA" "WQVGA432" "WVGA800" "QVGA") declare -a densities=("160" "213" "240" "320")
declare -a resolutions=("320x480" "480x640" "480x800" "480x854" "640x960" "1024x600" "1024x768" "1280x768" "1536x1152" "1920x1200")
declare -a targets=("android-16") declare -a targets=("android-16")
for target in ${targets[@]} for target in ${targets[@]}
do do
for skin in ${skins[@]} for density in ${densities[@]}
do do
avdDeviceName=AVD_$skin
$ANDROID_HOME/tools/emulator -avd $avdDeviceName & for resolution in ${resolutions[@]}
$ANDROID_HOME/tools/monkeyrunner ./take_screenshots.py ~/projects/java/android/calculatorpp/calculatorpp/misc/aux/tmp $avdDeviceName do
$ANDROID_HOME/platform-tools/adb -s emulator-5554 emu kill name="AVD"
name="$name$density"
name="$name$resolution"
name="$name$target"
$ANDROID_HOME/tools/emulator -avd $name &
$ANDROID_HOME/tools/monkeyrunner ./take_screenshots.py ~/projects/java/android/calculatorpp/calculatorpp/misc/other/tmp $name
$ANDROID_HOME/platform-tools/adb -s emulator-5554 emu kill
done
done done
done done

View File

@ -271,4 +271,8 @@
<string name="c_app_widget_4x5_name">Калькулятор++ Виджет (4x5)</string> <string name="c_app_widget_4x5_name">Калькулятор++ Виджет (4x5)</string>
<string name="open_onscreen_calculator">Нажмите чтобы открыть калькулятор поверх всех приложений</string> <string name="open_onscreen_calculator">Нажмите чтобы открыть калькулятор поверх всех приложений</string>
<string name="prefs_onscreen_title">Настройки плавающего калькулятора</string>
<string name="prefs_onscreen_start_on_boot_title">Запуск после загрузки устройства</string>
<string name="prefs_onscreen_start_on_boot_summary">Если включено кнопка плавающего калькулятора будет доступна в панели уведомлений полсе загрузки устройства</string>
</resources> </resources>

View File

@ -272,4 +272,8 @@
<string name="c_app_widget_4x5_name">Calculator++ Widget (4x5)</string> <string name="c_app_widget_4x5_name">Calculator++ Widget (4x5)</string>
<string name="open_onscreen_calculator">Click to open on-screen calculator</string> <string name="open_onscreen_calculator">Click to open on-screen calculator</string>
<string name="prefs_onscreen_title">On-screen calculator settings</string>
<string name="prefs_onscreen_start_on_boot_title">Start on boot</string>
<string name="prefs_onscreen_start_on_boot_summary">If checked on-screen calculator will appear in notification bar after device boot</string>
</resources> </resources>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:a="http://schemas.android.com/apk/res/android">
<PreferenceScreen a:title="@string/prefs_onscreen_title">
<org.solovyev.android.ads.AdViewPreference a:layout="@layout/admob_pref"/>
<android.preference.CheckBoxPreference
a:key="onscreen_start_on_boot"
a:title="@string/prefs_onscreen_start_on_boot_title"
a:summary="@string/prefs_onscreen_start_on_boot_summary"/>
</PreferenceScreen>
</PreferenceScreen>

View File

@ -1,12 +1,9 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.*;
import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.app.KeyguardManager; import android.app.KeyguardManager;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
@ -67,9 +64,7 @@ public abstract class AbstractCalculatorHelper implements SharedPreferences.OnSh
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
// let's disable locking of screen for monkeyrunner // let's disable locking of screen for monkeyrunner
// NOTE: this code is only for monkeyrunner if (CalculatorApplication.isMonkeyRunner(activity)) {
final String permission = Manifest.permission.DISABLE_KEYGUARD;
if (activity.checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
final KeyguardManager km = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE); final KeyguardManager km = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE);
km.newKeyguardLock(activity.getClass().getName()).disableKeyguard(); km.newKeyguardLock(activity.getClass().getName()).disableKeyguard();
} }

View File

@ -81,6 +81,12 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
Locator.getInstance().getPreferenceService().checkPreferredPreferences(false); Locator.getInstance().getPreferenceService().checkPreferredPreferences(false);
if ( CalculatorApplication.isMonkeyRunner(this) ) {
Locator.getInstance().getKeyboard().buttonPressed("123");
Locator.getInstance().getKeyboard().buttonPressed("+");
Locator.getInstance().getKeyboard().buttonPressed("321");
}
} }
@NotNull @NotNull
@ -100,35 +106,38 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
CalculatorPreferences.appVersion.putPreference(preferences, appVersion); CalculatorPreferences.appVersion.putPreference(preferences, appVersion);
boolean dialogShown = false; if (!CalculatorApplication.isMonkeyRunner(context)) {
if (EqualsTool.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) {
// new start boolean dialogShown = false;
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text); if (EqualsTool.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) {
builder.setPositiveButton(android.R.string.ok, null); // new start
builder.setTitle(R.string.c_first_start_text_title); final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text);
builder.create().show(); builder.setPositiveButton(android.R.string.ok, null);
dialogShown = true; builder.setTitle(R.string.c_first_start_text_title);
} else { builder.create().show();
if (savedVersion < appVersion) { dialogShown = true;
final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences); } else {
if (showReleaseNotes) { if (savedVersion < appVersion) {
final String releaseNotes = CalculatorReleaseNotesFragment.getReleaseNotes(context, savedVersion + 1); final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences);
if (!StringUtils.isEmpty(releaseNotes)) { if (showReleaseNotes) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(Html.fromHtml(releaseNotes)); final String releaseNotes = CalculatorReleaseNotesFragment.getReleaseNotes(context, savedVersion + 1);
builder.setPositiveButton(android.R.string.ok, null); if (!StringUtils.isEmpty(releaseNotes)) {
builder.setTitle(R.string.c_release_notes); final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(Html.fromHtml(releaseNotes));
builder.create().show(); builder.setPositiveButton(android.R.string.ok, null);
dialogShown = true; builder.setTitle(R.string.c_release_notes);
builder.create().show();
dialogShown = true;
}
} }
} }
} }
}
//Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!"); //Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!");
if (!dialogShown) { if (!dialogShown) {
if (appOpenedCounter != null && appOpenedCounter > 10) { if (appOpenedCounter != null && appOpenedCounter > 10) {
dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText, context); dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText, context);
}
} }
} }
} }

View File

@ -1,6 +1,8 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import net.robotmedia.billing.BillingController; import net.robotmedia.billing.BillingController;
import net.robotmedia.billing.helper.DefaultBillingObserver; import net.robotmedia.billing.helper.DefaultBillingObserver;
@ -13,7 +15,6 @@ import org.solovyev.android.ads.AdsController;
import org.solovyev.android.calculator.external.AndroidExternalListenersContainer; import org.solovyev.android.calculator.external.AndroidExternalListenersContainer;
import org.solovyev.android.calculator.history.AndroidCalculatorHistory; import org.solovyev.android.calculator.history.AndroidCalculatorHistory;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine; import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import org.solovyev.android.calculator.widget.CalculatorWidgetHelper;
/** /**
* User: serso * User: serso
@ -60,6 +61,7 @@ public class CalculatorApplication extends android.app.Application {
instance = this; instance = this;
} }
/* /*
********************************************************************** **********************************************************************
* *
@ -160,4 +162,8 @@ public class CalculatorApplication extends android.app.Application {
return instance; return instance;
} }
public static boolean isMonkeyRunner(@NotNull Context context) {
// NOTE: this code is only for monkeyrunner
return context.checkCallingOrSelfPermission(android.Manifest.permission.DISABLE_KEYGUARD) == PackageManager.PERMISSION_GRANTED;
}
} }

View File

@ -1,21 +0,0 @@
package org.solovyev.android.calculator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import org.solovyev.android.calculator.external.AndroidExternalListenersContainer;
import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService;
public class CalculatorOnscreenStartActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = new Intent(AndroidExternalListenersContainer.INIT_ACTION);
intent.setClass(this, CalculatorOnscreenService.class);
startService(intent);
this.finish();
}
}

View File

@ -1,9 +1,7 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import org.solovyev.android.calculator.external.AndroidExternalListenersContainer;
import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService; import org.solovyev.android.calculator.onscreen.CalculatorOnscreenService;
public class CalculatorOnscreenStartActivity extends Activity { public class CalculatorOnscreenStartActivity extends Activity {
@ -12,9 +10,7 @@ public class CalculatorOnscreenStartActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
final Intent intent = new Intent(AndroidExternalListenersContainer.INIT_ACTION); CalculatorOnscreenService.showOnscreenView(this);
intent.setClass(this, CalculatorOnscreenService.class);
startService(intent);
this.finish(); this.finish();
} }

View File

@ -28,6 +28,10 @@ public final class CalculatorPreferences {
public static final Preference<Integer> appVersion = new IntegerPreference("application.version", -1); public static final Preference<Integer> appVersion = new IntegerPreference("application.version", -1);
public static final Preference<Integer> appOpenedCounter = new IntegerPreference("app_opened_counter", 0); public static final Preference<Integer> appOpenedCounter = new IntegerPreference("app_opened_counter", 0);
public static class OnscreenCalculator {
public static final Preference<Boolean> startOnBoot = new BooleanPreference("onscreen_start_on_boot", false);
}
public static class Calculations { public static class Calculations {
public static final Preference<Boolean> calculateOnFly = new BooleanPreference("calculations_calculate_on_fly", true); public static final Preference<Boolean> calculateOnFly = new BooleanPreference("calculations_calculate_on_fly", true);

View File

@ -42,10 +42,11 @@ public class CalculatorPreferencesActivity extends SherlockPreferenceActivity im
//noinspection deprecation //noinspection deprecation
addPreferencesFromResource(R.xml.preferences); addPreferencesFromResource(R.xml.preferences);
//noinspection deprecation //noinspection deprecation
addPreferencesFromResource(R.xml.calculations_preferences); addPreferencesFromResource(R.xml.preferences_calculations);
addPreferencesFromResource(R.xml.appearance_preferences); addPreferencesFromResource(R.xml.preferences_appearance);
addPreferencesFromResource(R.xml.plot_preferences); addPreferencesFromResource(R.xml.preferences_plot);
addPreferencesFromResource(R.xml.other_preferences); addPreferencesFromResource(R.xml.preferences_other);
addPreferencesFromResource(R.xml.preferences_onscreen);
final Preference adFreePreference = findPreference(CalculatorApplication.AD_FREE_P_KEY); final Preference adFreePreference = findPreference(CalculatorApplication.AD_FREE_P_KEY);
adFreePreference.setEnabled(false); adFreePreference.setEnabled(false);

View File

@ -41,6 +41,7 @@ public class AndroidExternalListenersContainer implements CalculatorExternalList
public static final String EVENT_ID_EXTRA = "eventId"; public static final String EVENT_ID_EXTRA = "eventId";
public static final String INIT_ACTION = "org.solovyev.android.calculator.INIT"; public static final String INIT_ACTION = "org.solovyev.android.calculator.INIT";
public static final String INIT_ACTION_CREATE_VIEW_EXTRA = "createView";
public static final String EDITOR_STATE_CHANGED_ACTION = "org.solovyev.android.calculator.EDITOR_STATE_CHANGED"; public static final String EDITOR_STATE_CHANGED_ACTION = "org.solovyev.android.calculator.EDITOR_STATE_CHANGED";
public static final String EDITOR_STATE_EXTRA = "editorState"; public static final String EDITOR_STATE_EXTRA = "editorState";

View File

@ -3,7 +3,10 @@ package org.solovyev.android.calculator.onscreen;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.CalculatorPreferences;
/** /**
* User: serso * User: serso
@ -18,8 +21,15 @@ public final class CalculatorOnscreenBroadcastReceiver extends BroadcastReceiver
@Override @Override
public void onReceive(@NotNull Context context, public void onReceive(@NotNull Context context,
@NotNull Intent intent) { @NotNull Intent intent) {
final Intent newIntent = new Intent(intent); if ( intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) ) {
newIntent.setClass(context, CalculatorOnscreenService.class); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
context.startService(newIntent); if ( CalculatorPreferences.OnscreenCalculator.startOnBoot.getPreferenceNoError(preferences) ) {
CalculatorOnscreenService.showNotification(context);
}
} else {
final Intent newIntent = new Intent(intent);
newIntent.setClass(context, CalculatorOnscreenService.class);
context.startService(newIntent);
}
} }
} }

View File

@ -16,10 +16,7 @@ import org.solovyev.android.calculator.CalculatorDisplayViewState;
import org.solovyev.android.calculator.CalculatorEditorViewState; import org.solovyev.android.calculator.CalculatorEditorViewState;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.external.AndroidExternalListenersContainer; import org.solovyev.android.calculator.external.*;
import org.solovyev.android.calculator.external.DefaultExternalCalculatorIntentHandler;
import org.solovyev.android.calculator.external.ExternalCalculatorIntentHandler;
import org.solovyev.android.calculator.external.ExternalCalculatorStateUpdater;
/** /**
* User: serso * User: serso
@ -32,6 +29,7 @@ public class CalculatorOnscreenService extends Service implements ExternalCalcul
@NotNull @NotNull
private final ExternalCalculatorIntentHandler intentHandler = new DefaultExternalCalculatorIntentHandler(this); private final ExternalCalculatorIntentHandler intentHandler = new DefaultExternalCalculatorIntentHandler(this);
public static final Class<CalculatorOnscreenBroadcastReceiver> INTENT_LISTENER_CLASS = CalculatorOnscreenBroadcastReceiver.class;
@Nullable @Nullable
private static String cursorColor; private static String cursorColor;
@ -41,6 +39,8 @@ public class CalculatorOnscreenService extends Service implements ExternalCalcul
private boolean compatibilityStart = true; private boolean compatibilityStart = true;
private boolean viewCreated = false;
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
return null; return null;
@ -49,31 +49,37 @@ public class CalculatorOnscreenService extends Service implements ExternalCalcul
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
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();
} }
private int getHeight(int width) { 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; return 4 * width / 3;
} }
@ -82,8 +88,8 @@ public class CalculatorOnscreenService extends Service implements ExternalCalcul
} }
@NotNull @NotNull
private Class<?> getIntentListenerClass() { private static Class<?> getIntentListenerClass() {
return CalculatorOnscreenBroadcastReceiver.class; return INTENT_LISTENER_CLASS;
} }
private void stopCalculatorListening() { private void stopCalculatorListening() {
@ -137,12 +143,30 @@ public class CalculatorOnscreenService extends Service implements ExternalCalcul
private void handleStart(@Nullable Intent intent) { private void handleStart(@Nullable Intent intent) {
if ( intent != null ) { if ( intent != null ) {
intentHandler.onIntent(this, intent);
} if (isInitIntent(intent)) {
hideNotification();
boolean createView = intent.getBooleanExtra(AndroidExternalListenersContainer.INIT_ACTION_CREATE_VIEW_EXTRA, false);
if (createView) {
hideNotification();
createView();
} else {
showNotification();
}
}
if (viewCreated) {
intentHandler.onIntent(this, intent);
}
}
} }
private void hideNotification() { private boolean isInitIntent(@NotNull Intent intent) {
return intent.getAction().equals(AndroidExternalListenersContainer.INIT_ACTION);
}
private void hideNotification() {
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NOTIFICATION_ID); nm.cancel(NOTIFICATION_ID);
} }
@ -164,12 +188,30 @@ public class CalculatorOnscreenService extends Service implements ExternalCalcul
builder.setContentTitle(getText(R.string.c_app_name)); builder.setContentTitle(getText(R.string.c_app_name));
builder.setContentText(getString(R.string.open_onscreen_calculator)); builder.setContentText(getString(R.string.open_onscreen_calculator));
final Intent intent = new Intent(AndroidExternalListenersContainer.INIT_ACTION); final Intent intent = createShowOnscreenViewIntent(this);
intent.setClass(this, getIntentListenerClass());
builder.setContentIntent(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)); builder.setContentIntent(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID, builder.getNotification()); 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;
}
} }

View File

@ -8,11 +8,7 @@ import android.view.WindowManager;
import android.widget.ImageView; import android.widget.ImageView;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.AndroidCalculatorDisplayView; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.AndroidCalculatorEditorView;
import org.solovyev.android.calculator.CalculatorDisplayViewState;
import org.solovyev.android.calculator.CalculatorEditorViewState;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.widget.WidgetButton; import org.solovyev.android.calculator.widget.WidgetButton;
/** /**
@ -333,15 +329,46 @@ public class CalculatorOnscreenView {
private static class WindowDragTouchListener implements View.OnTouchListener { private static class WindowDragTouchListener implements View.OnTouchListener {
/*
**********************************************************************
*
* CONSTANTS
*
**********************************************************************
*/
private static final float DIST_EPS = 10f;
private static final float DIST_MAX = 100f;
private static final long TIME_EPS = 100L;
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
@NotNull
private final WindowManager wm; private final WindowManager wm;
private float x0; private float x0;
private float y0; private float y0;
private long time = 0;
@NotNull @NotNull
private final View view; private final View view;
/*
**********************************************************************
*
* CONSTRUCTORS
*
**********************************************************************
*/
public WindowDragTouchListener(@NotNull WindowManager wm, public WindowDragTouchListener(@NotNull WindowManager wm,
@NotNull View view) { @NotNull View view) {
this.wm = wm; this.wm = wm;
@ -364,25 +391,59 @@ public class CalculatorOnscreenView {
return true; return true;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
final float Δx = x1 - x0; final long currentTime = System.currentTimeMillis();
final float Δy = y1 - y0;
final WindowManager.LayoutParams params = (WindowManager.LayoutParams) view.getLayoutParams(); if ( currentTime - time >= TIME_EPS ) {
time = currentTime;
//Log.d(TAG, "0:" + toString(x0, y0) + ", 1: " + toString(x1, y1) + ", Δ: " + toString(Δx, Δy) + ", params: " + toString(params.x, params.y)); for (int i = 0; i < event.getHistorySize(); i++) {
final float xi = event.getHistoricalX(i);
params.x = (int) (params.x + Δx); final float yi = event.getHistoricalY(i);
params.y = (int) (params.y + Δy); processMove(xi, yi);
}
wm.updateViewLayout(view, params); processMove(x1, y1);
x0 = x1; }
y0 = y1;
return true; return true;
} }
return false; 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 @NotNull
private static String toString(float x, float y) { private static String toString(float x, float y) {
return "(" + formatFloat(x) + ", " + formatFloat(y) + ")"; return "(" + formatFloat(x) + ", " + formatFloat(y) + ")";

View File

@ -16,6 +16,6 @@ public class CalculatorPlotPreferenceActivity extends SherlockPreferenceActivity
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
//noinspection deprecation //noinspection deprecation
addPreferencesFromResource(R.xml.plot_preferences); addPreferencesFromResource(R.xml.preferences_plot);
} }
} }