Move android package to xplat

Summary: That's the android package and all references to it.

Reviewed By: jknoxville

Differential Revision: D8875423

fbshipit-source-id: 478bb380012bc1327199d51527a7fe376824fce2
This commit is contained in:
Pascal Hartig
2018-07-18 07:51:32 -07:00
committed by Facebook Github Bot
parent 2987c32ed8
commit 818c5e9dc5
8 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.android;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import com.facebook.sonar.core.SonarClient;
public final class AndroidSonarClient {
private static boolean sIsInitialized = false;
private static SonarThread sSonarThread;
private static SonarThread sConnectionThread;
public static synchronized SonarClient getInstance(Context context) {
if (!sIsInitialized) {
sSonarThread = new SonarThread("SonarEventBaseThread");
sSonarThread.start();
sConnectionThread = new SonarThread("SonarConnectionThread");
sConnectionThread.start();
final Context app = context.getApplicationContext();
SonarClientImpl.init(
sSonarThread.getEventBase(),
sConnectionThread.getEventBase(),
getServerHost(app),
"Android",
getFriendlyDeviceName(),
getId(),
getRunningAppName(app),
getPackageName(app),
context.getFilesDir().getAbsolutePath());
sIsInitialized = true;
}
return SonarClientImpl.getInstance();
}
static boolean isRunningOnGenymotion() {
return Build.FINGERPRINT.contains("vbox");
}
static boolean isRunningOnStockEmulator() {
return Build.FINGERPRINT.contains("generic") && !Build.FINGERPRINT.contains("vbox");
}
static String getId() {
return Build.SERIAL;
}
static String getFriendlyDeviceName() {
if (isRunningOnGenymotion()) {
// Genymotion already has a friendly name by default
return Build.MODEL;
} else {
return Build.MODEL + " - " + Build.VERSION.RELEASE + " - API " + Build.VERSION.SDK_INT;
}
}
static String getServerHost(Context context) {
if (isRunningOnStockEmulator()) {
return "10.0.2.2";
} else if (isRunningOnGenymotion()) {
// This is hand-wavy but works on but ipv4 and ipv6 genymotion
final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo info = wifi.getConnectionInfo();
final int ip = info.getIpAddress();
return String.format("%d.%d.%d.2", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff));
} else {
// Running on physical device. Sonar desktop will run `adb reverse tcp:8088 tcp:8088`
return "localhost";
}
}
static String getRunningAppName(Context context) {
return context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
}
static String getPackageName(Context context) {
return context.getPackageName();
}
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright (c) 2004-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.android;
import com.facebook.jni.HybridClassBase;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.soloader.SoLoader;
import com.facebook.sonar.BuildConfig;
@DoNotStrip
class EventBase extends HybridClassBase {
static {
if (BuildConfig.IS_INTERNAL_BUILD) {
SoLoader.loadLibrary("sonar");
}
}
EventBase() {
initHybrid();
}
@DoNotStrip
native void loopForever();
@DoNotStrip
private native void initHybrid();
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.android;
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.soloader.SoLoader;
import com.facebook.sonar.BuildConfig;
import com.facebook.sonar.core.SonarClient;
import com.facebook.sonar.core.SonarPlugin;
@DoNotStrip
class SonarClientImpl implements SonarClient {
static {
if (BuildConfig.IS_INTERNAL_BUILD) {
SoLoader.loadLibrary("sonar");
}
}
private final HybridData mHybridData;
private SonarClientImpl(HybridData hd) {
mHybridData = hd;
}
public static native void init(
EventBase callbackWorker,
EventBase connectionWorker,
String host,
String os,
String device,
String deviceId,
String app,
String appId,
String privateAppDirectory);
public static native SonarClientImpl getInstance();
@Override
public native void addPlugin(SonarPlugin plugin);
@Override
public native <T extends SonarPlugin> T getPlugin(String id);
@Override
public native void removePlugin(SonarPlugin plugin);
@Override
public native void start();
@Override
public native void stop();
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.android;
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.soloader.SoLoader;
import com.facebook.sonar.BuildConfig;
import com.facebook.sonar.core.SonarArray;
import com.facebook.sonar.core.SonarConnection;
import com.facebook.sonar.core.SonarObject;
import com.facebook.sonar.core.SonarReceiver;
@DoNotStrip
class SonarConnectionImpl implements SonarConnection {
static {
if (BuildConfig.IS_INTERNAL_BUILD) {
SoLoader.loadLibrary("sonar");
}
}
private final HybridData mHybridData;
private SonarConnectionImpl(HybridData hd) {
mHybridData = hd;
}
@Override
public void send(String method, SonarObject params) {
sendObject(method, params);
}
@Override
public void send(String method, SonarArray params) {
sendArray(method, params);
}
public native void sendObject(String method, SonarObject params);
public native void sendArray(String method, SonarArray params);
@Override
public native void reportError(Throwable throwable);
@Override
public native void receive(String method, SonarReceiver receiver);
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.android;
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.soloader.SoLoader;
import com.facebook.sonar.BuildConfig;
import com.facebook.sonar.core.SonarArray;
import com.facebook.sonar.core.SonarObject;
import com.facebook.sonar.core.SonarResponder;
@DoNotStrip
class SonarResponderImpl implements SonarResponder {
static {
if (BuildConfig.IS_INTERNAL_BUILD) {
SoLoader.loadLibrary("sonar");
}
}
private final HybridData mHybridData;
private SonarResponderImpl(HybridData hd) {
mHybridData = hd;
}
@Override
public void success(SonarObject params) {
successObject(params);
}
@Override
public void success(SonarArray params) {
successArray(params);
}
@Override
public void success() {
successObject(new SonarObject.Builder().build());
}
public native void successObject(SonarObject response);
public native void successArray(SonarArray response);
@Override
public native void error(SonarObject response);
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2004-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.android;
import android.os.Process;
import javax.annotation.Nullable;
class SonarThread extends Thread {
private @Nullable EventBase mEventBase;
SonarThread(final String name) {
super(name);
}
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
synchronized (this) {
try {
mEventBase = new EventBase();
} finally {
notifyAll();
}
}
mEventBase.loopForever();
}
synchronized EventBase getEventBase() {
while (mEventBase == null) {
try {
wait();
} catch (InterruptedException e) {
// ignore
}
}
return mEventBase;
}
}

View File

@@ -0,0 +1,311 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
#include <memory>
#ifdef SONAR_OSS
#include <fbjni/fbjni.h>
#else
#include <fb/fbjni.h>
#endif
#include <folly/json.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/EventBaseManager.h>
#include <Sonar/SonarClient.h>
#include <Sonar/SonarWebSocket.h>
#include <Sonar/SonarConnection.h>
#include <Sonar/SonarResponder.h>
using namespace facebook;
using namespace facebook::sonar;
namespace {
class JEventBase : public jni::HybridClass<JEventBase> {
public:
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/android/EventBase;";
static void registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", JEventBase::initHybrid),
makeNativeMethod("loopForever", JEventBase::loopForever),
});
}
folly::EventBase* eventBase() {
return &eventBase_;
}
private:
friend HybridBase;
JEventBase() {}
void loopForever() {
folly::EventBaseManager::get()->setEventBase(&eventBase_, false);
eventBase_.loopForever();
}
static void initHybrid(jni::alias_ref<jhybridobject> o) {
return setCxxInstance(o);
}
folly::EventBase eventBase_;
};
class JSonarObject : public jni::JavaClass<JSonarObject> {
public:
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/core/SonarObject;";
static jni::local_ref<JSonarObject> create(const folly::dynamic& json) {
return newInstance(folly::toJson(json));
}
std::string toJsonString() {
static const auto method = javaClassStatic()->getMethod<std::string()>("toJsonString");
return method(self())->toStdString();
}
};
class JSonarArray : public jni::JavaClass<JSonarArray> {
public:
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/core/SonarArray;";
static jni::local_ref<JSonarArray> create(const folly::dynamic& json) {
return newInstance(folly::toJson(json));
}
std::string toJsonString() {
static const auto method = javaClassStatic()->getMethod<std::string()>("toJsonString");
return method(self())->toStdString();
}
};
class JSonarResponder : public jni::JavaClass<JSonarResponder> {
public:
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/core/SonarResponder;";
};
class JSonarResponderImpl : public jni::HybridClass<JSonarResponderImpl, JSonarResponder> {
public:
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/android/SonarResponderImpl;";
static void registerNatives() {
registerHybrid({
makeNativeMethod("successObject", JSonarResponderImpl::successObject),
makeNativeMethod("successArray", JSonarResponderImpl::successArray),
makeNativeMethod("error", JSonarResponderImpl::error),
});
}
void successObject(jni::alias_ref<JSonarObject> json) {
_responder->success(json ? folly::parseJson(json->toJsonString()) : folly::dynamic::object());
}
void successArray(jni::alias_ref<JSonarArray> json) {
_responder->success(json ? folly::parseJson(json->toJsonString()) : folly::dynamic::object());
}
void error(jni::alias_ref<JSonarObject> json) {
_responder->error(json ? folly::parseJson(json->toJsonString()) : folly::dynamic::object());
}
private:
friend HybridBase;
std::shared_ptr<SonarResponder> _responder;
JSonarResponderImpl(std::shared_ptr<SonarResponder> responder): _responder(std::move(responder)) {}
};
class JSonarReceiver : public jni::JavaClass<JSonarReceiver> {
public:
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/core/SonarReceiver;";
void receive(const folly::dynamic params, std::shared_ptr<SonarResponder> responder) const {
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<JSonarObject::javaobject>, jni::alias_ref<JSonarResponder::javaobject>)>("onReceive");
method(self(), JSonarObject::create(std::move(params)), JSonarResponderImpl::newObjectCxxArgs(responder));
}
};
class JSonarConnection : public jni::JavaClass<JSonarConnection> {
public:
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/core/SonarConnection;";
};
class JSonarConnectionImpl : public jni::HybridClass<JSonarConnectionImpl, JSonarConnection> {
public:
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/android/SonarConnectionImpl;";
static void registerNatives() {
registerHybrid({
makeNativeMethod("sendObject", JSonarConnectionImpl::sendObject),
makeNativeMethod("sendArray", JSonarConnectionImpl::sendArray),
makeNativeMethod("reportError", JSonarConnectionImpl::reportError),
makeNativeMethod("receive", JSonarConnectionImpl::receive),
});
}
void sendObject(const std::string method, jni::alias_ref<JSonarObject> json) {
_connection->send(std::move(method), json ? folly::parseJson(json->toJsonString()) : folly::dynamic::object());
}
void sendArray(const std::string method, jni::alias_ref<JSonarArray> json) {
_connection->send(std::move(method), json ? folly::parseJson(json->toJsonString()) : folly::dynamic::object());
}
void reportError(jni::alias_ref<jni::JThrowable> throwable) {
_connection->error(throwable->toString(), throwable->getStackTrace()->toString());
}
void receive(const std::string method, jni::alias_ref<JSonarReceiver> receiver) {
auto global = make_global(receiver);
_connection->receive(std::move(method), [global] (const folly::dynamic& params, std::unique_ptr<SonarResponder> responder) {
global->receive(params, std::move(responder));
});
}
private:
friend HybridBase;
std::shared_ptr<SonarConnection> _connection;
JSonarConnectionImpl(std::shared_ptr<SonarConnection> connection): _connection(std::move(connection)) {}
};
class JSonarPlugin : public jni::JavaClass<JSonarPlugin> {
public:
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/core/SonarPlugin;";
std::string identifier() const {
static const auto method = javaClassStatic()->getMethod<std::string()>("getId");
return method(self())->toStdString();
}
void didConnect(std::shared_ptr<SonarConnection> conn) {
static const auto method = javaClassStatic()->getMethod<void(jni::alias_ref<JSonarConnection::javaobject>)>("onConnect");
method(self(), JSonarConnectionImpl::newObjectCxxArgs(conn));
}
void didDisconnect() {
static const auto method = javaClassStatic()->getMethod<void()>("onDisconnect");
method(self());
}
};
class JSonarPluginWrapper : public SonarPlugin {
public:
jni::global_ref<JSonarPlugin> jplugin;
virtual std::string identifier() const override {
return jplugin->identifier();
}
virtual void didConnect(std::shared_ptr<SonarConnection> conn) override {
jplugin->didConnect(conn);
}
virtual void didDisconnect() override {
jplugin->didDisconnect();
}
JSonarPluginWrapper(jni::global_ref<JSonarPlugin> plugin): jplugin(plugin) {}
};
class JSonarClient : public jni::HybridClass<JSonarClient> {
public:
constexpr static auto kJavaDescriptor = "Lcom/facebook/sonar/android/SonarClientImpl;";
static void registerNatives() {
registerHybrid({
makeNativeMethod("init", JSonarClient::init),
makeNativeMethod("getInstance", JSonarClient::getInstance),
makeNativeMethod("start", JSonarClient::start),
makeNativeMethod("stop", JSonarClient::stop),
makeNativeMethod("addPlugin", JSonarClient::addPlugin),
makeNativeMethod("removePlugin", JSonarClient::removePlugin),
makeNativeMethod("getPlugin", JSonarClient::getPlugin),
});
}
static jni::alias_ref<JSonarClient::javaobject> getInstance(jni::alias_ref<jclass>) {
static auto client = make_global(newObjectCxxArgs());
return client;
}
void start() {
SonarClient::instance()->start();
}
void stop() {
SonarClient::instance()->stop();
}
void addPlugin(jni::alias_ref<JSonarPlugin> plugin) {
auto wrapper = std::make_shared<JSonarPluginWrapper>(make_global(plugin));
SonarClient::instance()->addPlugin(wrapper);
}
void removePlugin(jni::alias_ref<JSonarPlugin> plugin) {
auto client = SonarClient::instance();
client->removePlugin(client->getPlugin(plugin->identifier()));
}
jni::alias_ref<JSonarPlugin> getPlugin(const std::string& identifier) {
auto plugin = SonarClient::instance()->getPlugin(identifier);
if (plugin) {
auto wrapper = std::static_pointer_cast<JSonarPluginWrapper>(plugin);
return wrapper->jplugin;
} else {
return nullptr;
}
}
static void init(
jni::alias_ref<jclass>,
JEventBase* callbackWorker,
JEventBase* connectionWorker,
const std::string host,
const std::string os,
const std::string device,
const std::string deviceId,
const std::string app,
const std::string appId,
const std::string privateAppDirectory) {
SonarClient::init({
{
std::move(host),
std::move(os),
std::move(device),
std::move(deviceId),
std::move(app),
std::move(appId),
std::move(privateAppDirectory)
},
callbackWorker->eventBase(),
connectionWorker->eventBase()
});
}
private:
friend HybridBase;
JSonarClient() {}
};
} // namespace
jint JNI_OnLoad(JavaVM* vm, void*) {
return jni::initialize(vm, [] {
JSonarClient::registerNatives();
JSonarConnectionImpl::registerNatives();
JSonarResponderImpl::registerNatives();
JEventBase::registerNatives();
});
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.sonar.android.utils;
import android.app.ActivityManager;
import android.content.Context;
import com.facebook.sonar.BuildConfig;
import java.util.List;
public final class SonarUtils {
private SonarUtils() {}
public static boolean shouldEnableSonar(Context context) {
return BuildConfig.IS_INTERNAL_BUILD && !isEndToEndTest() && isMainProcess(context);
}
private static boolean isEndToEndTest() {
final String value = System.getenv("BUDDY_SONAR_DISABLED");
if (value == null || value.length() == 0) {
return false;
}
try {
return Boolean.parseBoolean(value);
} catch (NumberFormatException e) {
return false;
}
}
private static boolean isMainProcess(Context context) {
final int pid = android.os.Process.myPid();
final ActivityManager manager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningAppProcessInfo> infoList = manager.getRunningAppProcesses();
String processName = null;
if (infoList != null) {
for (ActivityManager.RunningAppProcessInfo info : infoList) {
if (info.pid == pid) {
processName = info.processName;
break;
}
}
}
return context.getPackageName().equals(processName);
}
}