Complete PlatformPlugin.java (flutter/engine#3123)

After this patch, the PlatformPlugin has all the required functionality on Android.
This commit is contained in:
Adam Barth
2016-10-12 14:29:57 -07:00
committed by GitHub
parent 35e0fa1818
commit 1ccda4a4ca

View File

@@ -5,14 +5,16 @@
package io.flutter.plugin.platform;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.view.HapticFeedbackConstants;
import android.view.SoundEffectConstants;
import android.view.View;
import io.flutter.plugin.common.JSONMessageListener;
import io.flutter.view.FlutterView;
import org.chromium.base.PathUtils;
import org.domokit.common.ActivityLifecycleListener;
import org.json.JSONArray;
import org.json.JSONException;
@@ -40,6 +42,10 @@ public class PlatformPlugin extends JSONMessageListener implements ActivityLifec
JSONArray args = message.getJSONArray("args");
if (method.equals("SystemSound.play")) {
playSystemSound(args.getString(0));
} else if (method.equals("HapticFeedback.vibrate")) {
vibrateHapticFeedback();
} else if (method.equals("UrlLauncher.launch")) {
launchURL(args.getString(0));
} else if (method.equals("SystemChrome.setPreferredOrientations")) {
setSystemChromePreferredOrientatations(args.getJSONArray(0));
} else if (method.equals("SystemChrome.setApplicationSwitcherDescription")) {
@@ -48,6 +54,10 @@ public class PlatformPlugin extends JSONMessageListener implements ActivityLifec
setSystemChromeEnabledSystemUIOverlays(args.getJSONArray(0));
} else if (method.equals("SystemChrome.setSystemUIOverlayStyle")) {
setSystemChromeSystemUIOverlayStyle(args.getString(0));
} else if (method.equals("PathProvider.getTemporaryDirectory")) {
return getPathProviderTemporaryDirectory();
} else if (method.equals("PathProvider.getApplicationDocumentsDirectory")) {
return getPathProviderApplicationDocumentsDirectory();
} else {
// TODO(abarth): We should throw an exception here that gets
// transmitted back to Dart.
@@ -62,6 +72,21 @@ public class PlatformPlugin extends JSONMessageListener implements ActivityLifec
}
}
private void vibrateHapticFeedback() {
View view = mActivity.getWindow().getDecorView();
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
}
private void launchURL(String url) {
try {
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
launchIntent.setData(Uri.parse(url));
mActivity.startActivity(launchIntent);
} catch (java.lang.Exception exception) {
// Ignore parsing or ActivityNotFound errors
}
}
private void setSystemChromePreferredOrientatations(JSONArray orientatations) throws JSONException {
// Currently the Android implementation only supports masks with zero or one
// selected device orientations.
@@ -135,6 +160,18 @@ public class PlatformPlugin extends JSONMessageListener implements ActivityLifec
// so LIGHT vs DARK effectively isn't supported in Android.
}
private JSONObject getPathProviderTemporaryDirectory() throws JSONException {
JSONObject result = new JSONObject();
result.put("path", mActivity.getCacheDir().getPath());
return result;
}
private JSONObject getPathProviderApplicationDocumentsDirectory() throws JSONException {
JSONObject result = new JSONObject();
result.put("path", PathUtils.getDataDirectory(mActivity));
return result;
}
@Override
public void onPostResume() {
updateSystemUiOverlays();