Skip to content

Commit

Permalink
1. Fixing issue in multi-process 2. Adding filter for network emit so…
Browse files Browse the repository at this point in the history
…urce.
  • Loading branch information
whataa committed Jul 24, 2019
1 parent 94803bc commit e648c74
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
android:theme="@style/AppTheme.Translucent" />
<activity android:name=".ui.PicturesActivity" />
<activity android:name=".ui.StackViewActivity" />

<service android:name=".RemoteService"
android:process=":second"/>
</application>

</manifest>
72 changes: 72 additions & 0 deletions app/src/main/java/tech/linjiang/android/pandora/RemoteService.java
Original file line number Diff line number Diff line change
@@ -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<Void> cb = new Callback<Void>() {
@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);
}
}
12 changes: 11 additions & 1 deletion pandora-core/src/main/java/tech/linjiang/pandora/Pandora.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -98,6 +105,9 @@ public void addFunction(IFunc func) {
* Open the panel.
*/
public void open() {
if (notHostProcess) {
return;
}
funcController.open();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down
15 changes: 15 additions & 0 deletions pandora-core/src/main/java/tech/linjiang/pandora/util/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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,
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pandora-core/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@
<string name="pd_name_reset">重置</string>
<string name="pd_name_turn_on">打开</string>
<string name="pd_name_threshold">摇晃系数</string>
<string name="pd_name_previous">上一个</string>
<string name="pd_name_next">下一个</string>
<string name="pd_relaunch_request">设置成功,重启应用后生效</string>
</resources>
3 changes: 3 additions & 0 deletions pandora-core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@
<string name="pd_name_reset">reset</string>
<string name="pd_name_turn_on">turn on</string>
<string name="pd_name_threshold">threshold</string>
<string name="pd_name_previous">previous</string>
<string name="pd_name_next">next</string>
<string name="pd_relaunch_request">success, please restart app</string>
</resources>

0 comments on commit e648c74

Please sign in to comment.