From 97f6e24903dce1c4f5282f5040420ba3016fde85 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Tue, 13 Mar 2012 23:23:44 +0400 Subject: [PATCH] Service implementation --- ...rvice.java => CalculationServiceImpl.java} | 9 +++-- .../calculator/ICalculationService.java | 9 +++++ .../android/calculator/LocalBinder.java | 33 +++++++++++++++++++ calculatorpp/AndroidManifest.xml | 2 +- calculatorpp/project.properties | 1 + .../calculator/CalculatorActivity.java | 23 +++++++++++-- 6 files changed, 70 insertions(+), 7 deletions(-) rename calculatorpp-service/src/main/java/org/solovyev/android/calculator/{CalculationService.java => CalculationServiceImpl.java} (87%) create mode 100644 calculatorpp-service/src/main/java/org/solovyev/android/calculator/ICalculationService.java create mode 100644 calculatorpp-service/src/main/java/org/solovyev/android/calculator/LocalBinder.java diff --git a/calculatorpp-service/src/main/java/org/solovyev/android/calculator/CalculationService.java b/calculatorpp-service/src/main/java/org/solovyev/android/calculator/CalculationServiceImpl.java similarity index 87% rename from calculatorpp-service/src/main/java/org/solovyev/android/calculator/CalculationService.java rename to calculatorpp-service/src/main/java/org/solovyev/android/calculator/CalculationServiceImpl.java index 6a40cd6d..5ab3d7ca 100644 --- a/calculatorpp-service/src/main/java/org/solovyev/android/calculator/CalculationService.java +++ b/calculatorpp-service/src/main/java/org/solovyev/android/calculator/CalculationServiceImpl.java @@ -12,11 +12,14 @@ import org.jetbrains.annotations.NotNull; * Date: 3/5/12 * Time: 10:23 PM */ -public class CalculationService extends Service { +public class CalculationServiceImpl extends Service implements ICalculationService { @NotNull private ServiceHandler handler; + public CalculationServiceImpl() { + } + @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this, ".CalculationService.onStartCommand", Toast.LENGTH_SHORT).show(); @@ -56,7 +59,7 @@ public class CalculationService extends Service { @Override public IBinder onBind(Intent intent) { - return null; + return new LocalBinder(this); } private final class ServiceHandler extends Handler { @@ -67,7 +70,7 @@ public class CalculationService extends Service { @Override public void handleMessage(@NotNull Message msg) { - Toast.makeText(CalculationService.this, "Doing job!", Toast.LENGTH_SHORT).show(); + Toast.makeText(CalculationServiceImpl.this, "Doing job!", Toast.LENGTH_SHORT).show(); stopSelf(msg.arg1); } } diff --git a/calculatorpp-service/src/main/java/org/solovyev/android/calculator/ICalculationService.java b/calculatorpp-service/src/main/java/org/solovyev/android/calculator/ICalculationService.java new file mode 100644 index 00000000..0e79e485 --- /dev/null +++ b/calculatorpp-service/src/main/java/org/solovyev/android/calculator/ICalculationService.java @@ -0,0 +1,9 @@ +package org.solovyev.android.calculator; + +/** + * User: serso + * Date: 3/5/12 + * Time: 10:23 PM + */ +public interface ICalculationService { +} diff --git a/calculatorpp-service/src/main/java/org/solovyev/android/calculator/LocalBinder.java b/calculatorpp-service/src/main/java/org/solovyev/android/calculator/LocalBinder.java new file mode 100644 index 00000000..26d39a41 --- /dev/null +++ b/calculatorpp-service/src/main/java/org/solovyev/android/calculator/LocalBinder.java @@ -0,0 +1,33 @@ +package org.solovyev.android.calculator; + +import android.os.Binder; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.ref.WeakReference; + +/** + * A generic implementation of Binder to be used for local services + * + * @param The type of the service being bound + * @author Geoff Bruckner 12th December 2009 + */ + +public class LocalBinder extends Binder { + + @NotNull + private static String TAG = "LocalBinder"; + + @NotNull + private WeakReference serviceReference; + + + public LocalBinder(@NotNull S service) { + serviceReference = new WeakReference(service); + } + + @Nullable + public S getService() { + return serviceReference.get(); + } +} \ No newline at end of file diff --git a/calculatorpp/AndroidManifest.xml b/calculatorpp/AndroidManifest.xml index 60c5c126..e9bc5ce6 100644 --- a/calculatorpp/AndroidManifest.xml +++ b/calculatorpp/AndroidManifest.xml @@ -19,7 +19,7 @@ - + diff --git a/calculatorpp/project.properties b/calculatorpp/project.properties index fcd97491..359c56a1 100644 --- a/calculatorpp/project.properties +++ b/calculatorpp/project.properties @@ -11,3 +11,4 @@ target=android-15 android.library.reference.1=../calculatorpp-service android.library.reference.2=gen-external-apklibs/org.solovyev.android_common_0.1 + diff --git a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index b40fe1d4..b9b896bc 100644 --- a/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/calculatorpp/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -7,9 +7,10 @@ package org.solovyev.android.calculator; import android.app.Activity; import android.app.AlertDialog; -import android.content.SharedPreferences; +import android.content.*; import android.content.res.Configuration; import android.os.Bundle; +import android.os.IBinder; import android.os.Vibrator; import android.preference.PreferenceManager; import android.text.ClipboardManager; @@ -53,14 +54,28 @@ import org.solovyev.common.utils.history.HistoryAction; import java.text.DecimalFormatSymbols; import java.util.Locale; -public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener { +public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener, ServiceConnection { private static final int HVGA_WIDTH_PIXELS = 320; @Nullable private IBillingObserver billingObserver; - public static enum Theme { + @Nullable + private ICalculationService calculationService; + + @Override + public void onServiceConnected(ComponentName componentName, IBinder binder) { + if (binder instanceof LocalBinder) { + calculationService = (ICalculationService)((LocalBinder) binder).getService(); + } + } + + @Override + public void onServiceDisconnected(ComponentName componentName) { + } + + public static enum Theme { default_theme(ThemeType.other, R.style.default_theme), violet_theme(ThemeType.other, R.style.violet_theme), @@ -188,6 +203,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh super.onCreate(savedInstanceState); setLayout(preferences); + bindService(new Intent(this, CalculationServiceImpl.class), this, Context.BIND_AUTO_CREATE); + if (customTitleSupported) { try { getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title);