Reorganise sample into debug/release flavours

Summary:
This makes use of the new no-op package and also structures the app
into release and debug flavours that can be built with and without
Flipper part of the APK.

This was a pretty tedious adventure. I may have missed some corner cases
and also restructured a bunch of seemingly unrelated stuff that was
necessary to respect buck module boundaries.

Reviewed By: jknoxville

Differential Revision: D15148004

fbshipit-source-id: bf81f45105f5f16d17daccb3e8050ee00d69fc35
This commit is contained in:
Pascal Hartig
2019-05-01 08:32:57 -07:00
committed by Facebook Github Bot
parent b1c674f6f9
commit 2e65ab7133
13 changed files with 225 additions and 65 deletions

View File

@@ -0,0 +1,38 @@
/*
* 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.flipper.connectivitytest;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.facebook.flipper.android.AndroidFlipperClient;
import com.facebook.flipper.core.FlipperClient;
import com.facebook.flipper.plugins.example.ExampleFlipperPlugin;
/**
* Oh hai! This is probably not the kinda sample you want to copy to your application; we're just
* using this to drive a test run and exit the app afterwards.
*/
public class ConnectionTestActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final FlipperClient client = AndroidFlipperClient.getInstanceIfInitialized();
if (client != null) {
// As we're re-using the identifier, get rid of the default plugin first.
final ExampleFlipperPlugin exampleFlipperPlugin =
client.getPluginByClass(ExampleFlipperPlugin.class);
client.removePlugin(exampleFlipperPlugin);
final ConnectionTestPlugin connectionTestPlugin = new ConnectionTestPlugin(this);
client.addPlugin(connectionTestPlugin);
}
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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.flipper.connectivitytest;
import android.app.Activity;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.facebook.flipper.core.FlipperConnection;
import com.facebook.flipper.core.FlipperObject;
import com.facebook.flipper.core.FlipperPlugin;
import com.facebook.flipper.core.FlipperReceiver;
import com.facebook.flipper.core.FlipperResponder;
import com.facebook.flipper.sample.ExampleActions;
import com.facebook.flipper.sample.network.NetworkClient;
public class ConnectionTestPlugin implements FlipperPlugin {
// We are reusing the existing "Example" logic here. That's generally a pretty bad idea,
// but in war and in testing everything is fair.
private static final String ID = "Example";
private final Activity mActivity;
@Nullable private FlipperConnection mConnection;
public ConnectionTestPlugin(Activity activity) {
mActivity = activity;
}
@Override
public String getId() {
return ID;
}
@Override
public void onConnect(FlipperConnection connection) {
mConnection = connection;
connection.receive(
"displayMessage",
new FlipperReceiver() {
@Override
public void onReceive(final FlipperObject params, FlipperResponder responder) {
if (mActivity != null) {
mActivity.runOnUiThread(
new Runnable() {
@Override
public void run() {
Toast.makeText(mActivity, params.getString("message"), Toast.LENGTH_SHORT)
.show();
}
});
}
responder.success(new FlipperObject.Builder().put("greeting", "Hello").build());
}
});
mActivity.runOnUiThread(
new Runnable() {
@Override
public void run() {
final NetworkClient networkClient = NetworkClient.getInstance();
ExampleActions.sendGetRequest(networkClient.getOkHttpClient());
ExampleActions.sendPostRequest(networkClient.getOkHttpClient());
// We want Flipper to properly disconnect at this point and actually shut down the app.
mActivity.finish();
android.os.Process.sendSignal(android.os.Process.myPid(), 15);
}
});
}
@Override
public void onDisconnect() {
mConnection = null;
}
@Override
public boolean runInBackground() {
return true;
}
}

View File

@@ -0,0 +1,82 @@
/*
* 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.flipper.plugins.example;
import android.app.Activity;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.facebook.flipper.core.FlipperConnection;
import com.facebook.flipper.core.FlipperObject;
import com.facebook.flipper.core.FlipperPlugin;
import com.facebook.flipper.core.FlipperReceiver;
import com.facebook.flipper.core.FlipperResponder;
public class ExampleFlipperPlugin implements FlipperPlugin {
public static final String ID = "Example";
@Nullable private Activity mActivity;
@Nullable private FlipperConnection mConnection;
private int mNotificationsSent = 0;
@Override
public String getId() {
return ID;
}
/*
* Activity to be used to display incoming messages
*/
public void setActivity(Activity activity) {
mActivity = activity;
}
@Override
public void onConnect(FlipperConnection connection) {
mConnection = connection;
connection.receive(
"displayMessage",
new FlipperReceiver() {
@Override
public void onReceive(final FlipperObject params, FlipperResponder responder) {
if (mActivity != null) {
mActivity.runOnUiThread(
new Runnable() {
@Override
public void run() {
Toast.makeText(mActivity, params.getString("message"), Toast.LENGTH_SHORT)
.show();
}
});
}
responder.success(new FlipperObject.Builder().put("greeting", "Hello").build());
}
});
}
public void triggerNotification() {
if (mConnection != null) {
mConnection.send(
"triggerNotification", new FlipperObject.Builder().put("id", mNotificationsSent).build());
mNotificationsSent++;
}
}
@Override
public void onDisconnect() {
mConnection = null;
}
@Override
public boolean runInBackground() {
return true;
}
}

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.flipper.sample;
import android.util.Log;
import com.facebook.flipper.android.AndroidFlipperClient;
import com.facebook.flipper.core.FlipperClient;
import com.facebook.flipper.plugins.example.ExampleFlipperPlugin;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public final class ExampleActions {
public static void sendPostRequest(OkHttpClient client) {
final RequestBody formBody =
new FormBody.Builder().add("app", "Flipper").add("remarks", "Its awesome").build();
final Request request =
new Request.Builder()
.url("https://demo9512366.mockable.io/SonarPost")
.post(formBody)
.build();
client
.newCall(request)
.enqueue(
new Callback() {
@Override
public void onFailure(final Call call, final IOException e) {
e.printStackTrace();
Log.d("Flipper", e.getMessage());
}
@Override
public void onResponse(final Call call, final Response response) throws IOException {
if (response.isSuccessful()) {
Log.d("Flipper", response.body().string());
} else {
Log.d("Flipper", "not successful");
}
}
});
}
public static void sendGetRequest(OkHttpClient client) {
final Request request =
new Request.Builder().url("https://api.github.com/repos/facebook/yoga").get().build();
client
.newCall(request)
.enqueue(
new Callback() {
@Override
public void onFailure(final Call call, final IOException e) {
e.printStackTrace();
Log.d("Flipper", e.getMessage());
}
@Override
public void onResponse(final Call call, final Response response) throws IOException {
if (response.isSuccessful()) {
Log.d("Flipper", response.body().string());
} else {
Log.d("Flipper", "not successful");
}
}
});
}
public static void sendNotification() {
final FlipperClient client = AndroidFlipperClient.getInstanceIfInitialized();
if (client != null) {
final ExampleFlipperPlugin plugin = client.getPluginByClass(ExampleFlipperPlugin.class);
plugin.triggerNotification();
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* 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.flipper.sample;
import android.content.Context;
import com.facebook.flipper.core.FlipperClient;
import com.facebook.flipper.plugins.crashreporter.CrashReporterPlugin;
import com.facebook.flipper.plugins.example.ExampleFlipperPlugin;
import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin;
import com.facebook.flipper.plugins.inspector.DescriptorMapping;
import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin;
import com.facebook.flipper.plugins.leakcanary.LeakCanaryFlipperPlugin;
import com.facebook.flipper.plugins.litho.LithoFlipperDescriptors;
import com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor;
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin;
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin;
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin.SharedPreferencesDescriptor;
import com.facebook.litho.config.ComponentsConfiguration;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
public final class FlipperInitializer {
public interface IntializationResult {
OkHttpClient getOkHttpClient();
}
public static IntializationResult initFlipperPlugins(Context context, FlipperClient client) {
final DescriptorMapping descriptorMapping = DescriptorMapping.withDefaults();
final NetworkFlipperPlugin networkPlugin = new NetworkFlipperPlugin();
final FlipperOkhttpInterceptor interceptor = new FlipperOkhttpInterceptor(networkPlugin);
// Normally, you would want to make this dependent on a BuildConfig flag, but
// for this demo application we can safely assume that you always want to debug.
ComponentsConfiguration.isDebugModeEnabled = true;
LithoFlipperDescriptors.add(descriptorMapping);
client.addPlugin(new InspectorFlipperPlugin(context, descriptorMapping));
client.addPlugin(networkPlugin);
client.addPlugin(
new SharedPreferencesFlipperPlugin(
context,
Arrays.asList(
new SharedPreferencesDescriptor("sample", Context.MODE_PRIVATE),
new SharedPreferencesDescriptor("other_sample", Context.MODE_PRIVATE))));
client.addPlugin(new LeakCanaryFlipperPlugin());
client.addPlugin(new FrescoFlipperPlugin());
client.addPlugin(new ExampleFlipperPlugin());
client.addPlugin(CrashReporterPlugin.getInstance());
client.start();
final OkHttpClient okHttpClient =
new OkHttpClient.Builder()
.addNetworkInterceptor(interceptor)
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.MINUTES)
.build();
return new IntializationResult() {
@Override
public OkHttpClient getOkHttpClient() {
return okHttpClient;
}
};
}
}