Refactor platform_services sample (#8949) * Refactor platform_services sample * removed exception
diff --git a/examples/platform_services/android/app/src/androidTest/java/com/example/flutter/ExampleInstrumentedTest.java b/examples/platform_services/android/app/src/androidTest/java/com/example/flutter/ExampleInstrumentedTest.java index 2fc8eeb..c8dd7a9 100644 --- a/examples/platform_services/android/app/src/androidTest/java/com/example/flutter/ExampleInstrumentedTest.java +++ b/examples/platform_services/android/app/src/androidTest/java/com/example/flutter/ExampleInstrumentedTest.java
@@ -8,12 +8,8 @@ import io.flutter.view.FlutterView; import android.app.Instrumentation; -import android.support.test.InstrumentationRegistry; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; -import org.json.JSONException; -import org.json.JSONObject; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -23,8 +19,8 @@ @RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest { @Rule - public ActivityTestRule<ExampleActivity> activityRule = - new ActivityTestRule<>(ExampleActivity.class); + public ActivityTestRule<MainActivity> activityRule = + new ActivityTestRule<>(MainActivity.class); @Test
diff --git a/examples/platform_services/android/app/src/main/AndroidManifest.xml b/examples/platform_services/android/app/src/main/AndroidManifest.xml index b068bdc..d18170b 100644 --- a/examples/platform_services/android/app/src/main/AndroidManifest.xml +++ b/examples/platform_services/android/app/src/main/AndroidManifest.xml
@@ -10,7 +10,7 @@ <application android:name="io.flutter.app.FlutterApplication" android:label="@string/app_name" > <activity - android:name=".ExampleActivity" + android:name=".MainActivity" android:launchMode="singleTop" android:theme="@android:style/Theme.Black.NoTitleBar" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
diff --git a/examples/platform_services/android/app/src/main/java/com/example/flutter/ExampleActivity.java b/examples/platform_services/android/app/src/main/java/com/example/flutter/MainActivity.java similarity index 80% rename from examples/platform_services/android/app/src/main/java/com/example/flutter/ExampleActivity.java rename to examples/platform_services/android/app/src/main/java/com/example/flutter/MainActivity.java index 7f2f288..3ce1f03 100644 --- a/examples/platform_services/android/app/src/main/java/com/example/flutter/ExampleActivity.java +++ b/examples/platform_services/android/app/src/main/java/com/example/flutter/MainActivity.java
@@ -18,7 +18,7 @@ import io.flutter.plugin.common.FlutterMethodChannel.Response; import io.flutter.plugin.common.MethodCall; -public class ExampleActivity extends FlutterActivity { +public class MainActivity extends FlutterActivity { private static final String CHANNEL = "battery"; @Override @@ -30,15 +30,19 @@ @Override public void onMethodCall(MethodCall call, Response response) { if (call.method.equals("getBatteryLevel")) { - getBatteryLevel(response); - } else { - throw new IllegalArgumentException("Unknown method " + call.method); + int batteryLevel = getBatteryLevel(); + + if (batteryLevel != -1) { + response.success(batteryLevel); + } else { + response.error("UNAVAILABLE", "Battery level not available.", null); + } } } }); } - private void getBatteryLevel(Response response) { + private int getBatteryLevel() { int batteryLevel = -1; if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE); @@ -50,10 +54,6 @@ intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); } - if (batteryLevel != -1) { - response.success(batteryLevel); - } else { - response.error("UNAVAILABLE", "Battery level not available.", null); - } + return batteryLevel; } }