From e648c74275b260853212dbbd082fa2c0c6c55926 Mon Sep 17 00:00:00 2001 From: linjiang Date: Fri, 14 Jun 2019 15:14:29 +0800 Subject: [PATCH] 1. Fixing issue in multi-process 2. Adding filter for network emit source. --- app/src/main/AndroidManifest.xml | 3 + .../android/pandora/RemoteService.java | 72 +++++++++++++++++++ .../java/tech/linjiang/pandora/Pandora.java | 12 +++- .../pandora/network/OkHttpInterceptor.java | 4 +- .../pandora/ui/fragment/ConfigFragment.java | 5 ++ .../tech/linjiang/pandora/util/Config.java | 15 ++++ .../linjiang/pandora/util/SensorDetector.java | 6 +- .../src/main/res/values-zh/strings.xml | 3 + pandora-core/src/main/res/values/strings.xml | 3 + 9 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/tech/linjiang/android/pandora/RemoteService.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 47d8368..6be55ee 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -28,6 +28,9 @@ android:theme="@style/AppTheme.Translucent" /> + + \ No newline at end of file diff --git a/app/src/main/java/tech/linjiang/android/pandora/RemoteService.java b/app/src/main/java/tech/linjiang/android/pandora/RemoteService.java new file mode 100644 index 0000000..462d289 --- /dev/null +++ b/app/src/main/java/tech/linjiang/android/pandora/RemoteService.java @@ -0,0 +1,72 @@ +package tech.linjiang.android.pandora; + +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.os.IBinder; +import android.util.Log; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import tech.linjiang.android.pandora.net.ApiService; +import tech.linjiang.android.pandora.utils.ThreadPool; +import tech.linjiang.pandora.Pandora; + +/** + * This service is used to test the behaviors of Pandora in multiple processes + */ +public class RemoteService extends Service { + + public static void start(Context context) { + Intent intent = new Intent(context, RemoteService.class); + context.startService(intent); + } + private static final String TAG = "ProcessService"; + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public void onCreate() { + super.onCreate(); + Log.d(TAG, "onCreate: "); + Pandora.get().open(); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + Log.d(TAG, "onStartCommand: "); + ThreadPool.post(() -> { + try { + URL url = new URL("https://www.v2ex.com/api/topics/latest.json"); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.getInputStream(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + }); + ApiService.HttpbinApi api = ApiService.getInstance(); + Callback cb = new Callback() { + @Override + public void onResponse(Call call, Response response) { + } + + @Override + public void onFailure(Call call, Throwable t) { + t.printStackTrace(); + } + }; + api.get().enqueue(cb); + return super.onStartCommand(intent, flags, startId); + } +} diff --git a/pandora-core/src/main/java/tech/linjiang/pandora/Pandora.java b/pandora-core/src/main/java/tech/linjiang/pandora/Pandora.java index 22eaf92..88b9752 100644 --- a/pandora-core/src/main/java/tech/linjiang/pandora/Pandora.java +++ b/pandora-core/src/main/java/tech/linjiang/pandora/Pandora.java @@ -39,7 +39,7 @@ public boolean onCreate() { private void init(Application app) { Utils.init(app); funcController = new FuncController(app); - sensorDetector = new SensorDetector(this); + sensorDetector = new SensorDetector(notHostProcess ? null : this); interceptor = new OkHttpInterceptor(); databases = new Databases(); sharedPref = new SharedPref(); @@ -49,9 +49,16 @@ private void init(Application app) { } public static Pandora get() { + if (INSTANCE == null) { + // Not the host process + Pandora pandora = new Pandora(); + pandora.notHostProcess = true; + pandora.onCreate(); + } return INSTANCE; } + private boolean notHostProcess; private OkHttpInterceptor interceptor; private Databases databases; private SharedPref sharedPref; @@ -98,6 +105,9 @@ public void addFunction(IFunc func) { * Open the panel. */ public void open() { + if (notHostProcess) { + return; + } funcController.open(); } diff --git a/pandora-core/src/main/java/tech/linjiang/pandora/network/OkHttpInterceptor.java b/pandora-core/src/main/java/tech/linjiang/pandora/network/OkHttpInterceptor.java index 7308a06..f9b5dbc 100644 --- a/pandora-core/src/main/java/tech/linjiang/pandora/network/OkHttpInterceptor.java +++ b/pandora-core/src/main/java/tech/linjiang/pandora/network/OkHttpInterceptor.java @@ -36,7 +36,9 @@ public class OkHttpInterceptor implements Interceptor { public static final long MAX_SIZE_BODY = 1024 * 1024 * 2; public OkHttpInterceptor() { - OkUrlFactory.init(); + if (Config.getNETWORK_URL_CONNECTION()) { + OkUrlFactory.init(); + } } private NetStateListener listener; diff --git a/pandora-core/src/main/java/tech/linjiang/pandora/ui/fragment/ConfigFragment.java b/pandora-core/src/main/java/tech/linjiang/pandora/ui/fragment/ConfigFragment.java index ee88dc7..7fd2f71 100644 --- a/pandora-core/src/main/java/tech/linjiang/pandora/ui/fragment/ConfigFragment.java +++ b/pandora-core/src/main/java/tech/linjiang/pandora/ui/fragment/ConfigFragment.java @@ -74,6 +74,10 @@ public void onItemClick(int position, BaseItem item) { } launch(EditFragment.class, bundle, CODE1); break; + case Config.Type.NETWORK_URLCONNECTION: + Config.setNETWORK_URL_CONNECTION(!Config.getNETWORK_URL_CONNECTION()); + Utils.toast(R.string.pd_relaunch_request); + break; case Config.Type.SANDBOX_DPM: Config.setSANDBOX_DPM(!Config.getSANDBOX_DPM()); break; @@ -97,6 +101,7 @@ private void refreshData() { data.add(new NameArrowItem("delay for each request(ms)", "" + Config.getNETWORK_DELAY_REQ()).setTag(Config.Type.NETWORK_DELAY_REQ)); data.add(new NameArrowItem("delay for each response(ms)", "" + Config.getNETWORK_DELAY_RES()).setTag(Config.Type.NETWORK_DELAY_RES)); data.add(new NameArrowItem("the maximum number of first loads", "" + Config.getNETWORK_PAGE_SIZE()).setTag(Config.Type.NETWORK_PAGE_SIZE)); + data.add(new CheckBoxItem("Show urlConnection request", Config.getNETWORK_URL_CONNECTION()).setTag(Config.Type.NETWORK_URLCONNECTION)); data.add(new TitleItem(ViewKnife.getString(R.string.pd_name_sandbox))); data.add(new CheckBoxItem("show device-protect-mode file\n(only for api>=24)", Config.getSANDBOX_DPM()).setTag(Config.Type.SANDBOX_DPM)); diff --git a/pandora-core/src/main/java/tech/linjiang/pandora/util/Config.java b/pandora-core/src/main/java/tech/linjiang/pandora/util/Config.java index 7ff9c75..0b35dff 100644 --- a/pandora-core/src/main/java/tech/linjiang/pandora/util/Config.java +++ b/pandora-core/src/main/java/tech/linjiang/pandora/util/Config.java @@ -23,6 +23,7 @@ public class Config { private static final String KEY_NETWORK_DELAY_RES = "key_network_delay_res"; private static final String KEY_SANDBOX_DPM = "key_sandbox_dpm"; private static final String KEY_NETWORK_PAGE_SIZE = "key_network_page_size"; + private static final String KEY_NETWORK_URLCONNECTION = "key_network_urlconnection"; private static final String KEY_UI_ACTIVITY_GRAVITY = "key_ui_activity_gravity"; private static final String KEY_UI_GRID_INTERVAL = "key_ui_grid_interval"; private static final String KEY_UI_IGNORE_SYS_LAYER = "key_ui_ignore_sys_layer"; @@ -35,6 +36,7 @@ public class Config { private static final long DEF_KEY_NETWORK_DELAY_RES = 0; private static final boolean DEF_KEY_SANDBOX_DPM = false; private static final int DEF_KEY_NETWORK_PAGE_SIZE = 512; + private static final boolean DEF_KEY_NETWORK_URLCONNECTION = true; private static final int DEF_UI_ACTIVITY_GRAVITY = Gravity.START | Gravity.BOTTOM; private static final int DEF_UI_GRID_INTERVAL = 5; private static final boolean DEF_UI_IGNORE_SYS_LAYER = false; @@ -170,6 +172,17 @@ public static void setNETWORK_PAGE_SIZE(int value) { // + public static boolean getNETWORK_URL_CONNECTION() { + return getSp().getBoolean(KEY_NETWORK_URLCONNECTION, DEF_KEY_NETWORK_URLCONNECTION); + } + public static void setNETWORK_URL_CONNECTION(boolean value) { + getSp().edit() + .putBoolean(KEY_NETWORK_URLCONNECTION, value) + .apply(); + } + + // + public static boolean getUI_IGNORE_SYS_LAYER() { return getSp().getBoolean(KEY_UI_IGNORE_SYS_LAYER, DEF_UI_IGNORE_SYS_LAYER); } @@ -190,6 +203,7 @@ public static void setUI_IGNORE_SYS_LAYER(Boolean value) { Type.NETWORK_DELAY_REQ, Type.NETWORK_DELAY_RES, Type.NETWORK_PAGE_SIZE, + Type.NETWORK_URLCONNECTION, Type.SANDBOX_DPM, Type.UI_ACTIVITY_GRAVITY, Type.UI_GRID_INTERVAL, @@ -207,6 +221,7 @@ public static void setUI_IGNORE_SYS_LAYER(Boolean value) { int NETWORK_DELAY_REQ = 0x20; int NETWORK_DELAY_RES = 0x21; int NETWORK_PAGE_SIZE = 0x22; + int NETWORK_URLCONNECTION = 0x23; int SANDBOX_DPM = 0x30; diff --git a/pandora-core/src/main/java/tech/linjiang/pandora/util/SensorDetector.java b/pandora-core/src/main/java/tech/linjiang/pandora/util/SensorDetector.java index 79af04a..4ede0f2 100644 --- a/pandora-core/src/main/java/tech/linjiang/pandora/util/SensorDetector.java +++ b/pandora-core/src/main/java/tech/linjiang/pandora/util/SensorDetector.java @@ -17,8 +17,10 @@ public class SensorDetector implements SensorEventListener { private Callback callback; public SensorDetector(Callback callback) { - register(); - this.callback = callback; + if (callback != null) { + register(); + this.callback = callback; + } } @Override diff --git a/pandora-core/src/main/res/values-zh/strings.xml b/pandora-core/src/main/res/values-zh/strings.xml index 4792749..bffeea5 100644 --- a/pandora-core/src/main/res/values-zh/strings.xml +++ b/pandora-core/src/main/res/values-zh/strings.xml @@ -57,4 +57,7 @@ 重置 打开 摇晃系数 + 上一个 + 下一个 + 设置成功,重启应用后生效 \ No newline at end of file diff --git a/pandora-core/src/main/res/values/strings.xml b/pandora-core/src/main/res/values/strings.xml index 83a93aa..db29f1b 100644 --- a/pandora-core/src/main/res/values/strings.xml +++ b/pandora-core/src/main/res/values/strings.xml @@ -56,4 +56,7 @@ reset turn on threshold + previous + next + success, please restart app