Commit 972da543 authored by Sarah Zakarias's avatar Sarah Zakarias Committed by GitHub

Refactor platform_services sample (#8949)

* Refactor platform_services sample

* removed exception
parent 9adf1bf1
...@@ -8,12 +8,8 @@ import android.support.test.runner.AndroidJUnit4; ...@@ -8,12 +8,8 @@ import android.support.test.runner.AndroidJUnit4;
import io.flutter.view.FlutterView; import io.flutter.view.FlutterView;
import android.app.Instrumentation; import android.app.Instrumentation;
import android.support.test.InstrumentationRegistry;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; 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.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -23,8 +19,8 @@ import static org.junit.Assert.*; ...@@ -23,8 +19,8 @@ import static org.junit.Assert.*;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest { public class ExampleInstrumentedTest {
@Rule @Rule
public ActivityTestRule<ExampleActivity> activityRule = public ActivityTestRule<MainActivity> activityRule =
new ActivityTestRule<>(ExampleActivity.class); new ActivityTestRule<>(MainActivity.class);
@Test @Test
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
<application android:name="io.flutter.app.FlutterApplication" android:label="@string/app_name" > <application android:name="io.flutter.app.FlutterApplication" android:label="@string/app_name" >
<activity <activity
android:name=".ExampleActivity" android:name=".MainActivity"
android:launchMode="singleTop" android:launchMode="singleTop"
android:theme="@android:style/Theme.Black.NoTitleBar" android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
......
...@@ -18,7 +18,7 @@ import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler; ...@@ -18,7 +18,7 @@ import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
import io.flutter.plugin.common.FlutterMethodChannel.Response; import io.flutter.plugin.common.FlutterMethodChannel.Response;
import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodCall;
public class ExampleActivity extends FlutterActivity { public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "battery"; private static final String CHANNEL = "battery";
@Override @Override
...@@ -30,15 +30,19 @@ public class ExampleActivity extends FlutterActivity { ...@@ -30,15 +30,19 @@ public class ExampleActivity extends FlutterActivity {
@Override @Override
public void onMethodCall(MethodCall call, Response response) { public void onMethodCall(MethodCall call, Response response) {
if (call.method.equals("getBatteryLevel")) { if (call.method.equals("getBatteryLevel")) {
getBatteryLevel(response); int batteryLevel = getBatteryLevel();
} else {
throw new IllegalArgumentException("Unknown method " + call.method); 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; int batteryLevel = -1;
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE); BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);
...@@ -50,10 +54,6 @@ public class ExampleActivity extends FlutterActivity { ...@@ -50,10 +54,6 @@ public class ExampleActivity extends FlutterActivity {
intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
} }
if (batteryLevel != -1) { return batteryLevel;
response.success(batteryLevel);
} else {
response.error("UNAVAILABLE", "Battery level not available.", null);
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment