Skip to content

Commit

Permalink
Fix API receiver isolation
Browse files Browse the repository at this point in the history
  • Loading branch information
tretdm committed Nov 26, 2023
1 parent 9224b5d commit 829c571
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 76 deletions.
14 changes: 11 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@
tools:targetApi="tiramisu"
android:requestLegacyExternalStorage="true"
android:localeConfig="@xml/locales">
<receiver android:name=".longpoll_api.receivers.LongPollReceiver"
<receiver android:name=".receivers.LongPollReceiver"
android:exported="true"
android:permission="GET_LONGPOLL_NOTIFICATIONS">
<intent-filter>
<action android:name="uk.openvk.android.legacy.LONGPOLL_RECEIVE" />
<action android:name="uk.openvk.android.refresh.LONGPOLL_RECEIVE" />
</intent-filter>
</receiver>
<receiver
android:name=".receivers.OvkAPIReceiver"
android:enabled="true"
android:exported="true"
android:permission="RECEIVE_NETWORK_API_DATA">
<intent-filter>
<action android:name="uk.openvk.android.refresh.API_DATA_RECEIVE" />
</intent-filter>
</receiver>
<activity
Expand Down Expand Up @@ -166,7 +175,6 @@
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />

<service android:enabled="true" android:name=".longpoll_api.LongPollService"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ private void sendMessage(final int message, String response) {
msg.what = message;
final Bundle bundle = new Bundle();
bundle.putString("response", response);
bundle.putString("address", apiListeners.from);
msg.setData(bundle);
handler.post(new Runnable() {
@Override
Expand All @@ -507,6 +508,7 @@ private void sendMessage(final int message, String response, int id) {
final Bundle bundle = new Bundle();
bundle.putString("response", response);
bundle.putInt("id", id);
bundle.putString("address", apiListeners.from);
msg.setData(bundle);
handler.post(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class OvkAPIWrapper {

private OkHttpClient httpClient = null;
private boolean logging_enabled = true; // default for beta releases
private String client_name = "openvk_legacy_android";
private String client_name = "openvk_refresh_android";
public Handler handler;
OvkAPIListeners apiListeners;

Expand Down Expand Up @@ -786,7 +786,7 @@ public void run() {
error.description = e.getMessage();
sendMessage(HandlerMessages.NO_INTERNET_CONNECTION, method, error.description);
} catch (SocketException e) {
if(e.getMessage().contains("ETIMEDOUT")) {
if(Objects.requireNonNull(e.getMessage()).contains("ETIMEDOUT")) {
if(logging_enabled) Log.e(OvkApplication.API_TAG,
String.format("Connection error: %s", e.getMessage()));
error.description = e.getMessage();
Expand Down Expand Up @@ -863,8 +863,8 @@ private void sendMessage(final int message, String response) {
msg.what = message;
final Bundle bundle = new Bundle();
bundle.putString("response", response);
bundle.putString("address", apiListeners.from);
msg.setData(bundle);

handler.post(new Runnable() {
@Override
public void run() {
Expand All @@ -887,6 +887,7 @@ private void sendMessage(final int message, String method, String response) {
final Bundle bundle = new Bundle();
bundle.putString("response", response);
bundle.putString("method", method);
bundle.putString("address", apiListeners.from);
msg.setData(bundle);
handler.post(new Runnable() {
@Override
Expand Down Expand Up @@ -916,6 +917,7 @@ private void sendMessage(final int message, String method, String args, String r
bundle.putString("response", response);
bundle.putString("method", method);
bundle.putString("args", args);
bundle.putString("address", apiListeners.from);
msg.setData(bundle);
handler.post(new Runnable() {
@Override
Expand All @@ -941,6 +943,7 @@ private void sendMessage(final int message, String method, String args, String w
bundle.putString("method", method);
bundle.putString("args", args);
bundle.putString("where", where);
bundle.putString("address", apiListeners.from);
msg.setData(bundle);
handler.post(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ private void sendMessage(final int message, String filename, String response) {
final Bundle bundle = new Bundle();
bundle.putString("filename", filename);
bundle.putString("response", response);
bundle.putString("address", apiListeners.from);
msg.setData(bundle);
handler.post(new Runnable() {
@Override
Expand All @@ -300,6 +301,7 @@ public void updateLoadProgress(String filename, String url, final long position,
bundle.putString("url", url);
bundle.putLong("position", position);
bundle.putLong("length", length);
bundle.putString("address", apiListeners.from);
msg.setData(bundle);
handler.post(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ public void onReceive(Context context, Intent intent) {
if(activity instanceof final NetworkActivity netActivity) {
OpenVKAPI ovk_api = netActivity.ovk_api;
final Bundle data = intent.getExtras();
final Message msg = parseJSONData(ovk_api.wrapper, netActivity.handler, data);
ovk_api.wrapper.handler.post(new Runnable() {
@Override
public void run() {
if(data.getString("address").equals(activity.getLocalClassName())) {
final Message msg = parseJSONData(ovk_api.wrapper, netActivity.handler, data);
ovk_api.wrapper.handler.post(() -> {
Log.d(OvkApplication.API_TAG,
String.format("Handling message %s in %s", msg.what, activity.getLocalClassName())
String.format("Handling message %s in %s (%s)", msg.what, activity.getLocalClassName(),
data.getString("address"))
);
netActivity.receiveState(msg.what, data);
}
});
});
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,65 +47,53 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
public void registerAPIDataReceiver() {
receiver = new OvkAPIReceiver(this);
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter(
"uk.openvk.android.legacy.API_DATA_RECEIVE"));
"uk.openvk.android.refresh.API_DATA_RECEIVE"));
}

private void setAPIListeners(final OvkAPIListeners listeners) {
listeners.from = getLocalClassName();
listeners.successListener = new OvkAPIListeners.OnAPISuccessListener() {
@Override
public void onAPISuccess(final Context ctx, int msg_code, final Bundle data) {
if(!BuildConfig.BUILD_TYPE.equals("release"))
Log.d(OvkApplication.APP_TAG,
String.format(
"Handling API message %s in %s",
msg_code,
getLocalClassName()
)
);
if(msg_code == HandlerMessages.PARSE_JSON) {
new Thread(new Runnable() {
@Override
public void run() {
Intent intent = new Intent();
intent.setAction("uk.openvk.android.legacy.API_DATA_RECEIVE");
data.putString("address", listeners.from);
intent.putExtras(data);
LocalBroadcastManager.getInstance(ctx).sendBroadcast(intent);
}
}).start();
} else {
receiveState(msg_code, data);
}
}
};
listeners.failListener = new OvkAPIListeners.OnAPIFailListener() {
@Override
public void onAPIFailed(Context ctx, int msg_code, final Bundle data) {
if(!BuildConfig.BUILD_TYPE.equals("release"))
Log.d(OvkApplication.APP_TAG,
String.format(
"Handling API message %s in %s",
msg_code,
getLocalClassName()
)
);
listeners.successListener = (ctx, msg_code, data) -> {
if(!BuildConfig.BUILD_TYPE.equals("release"))
Log.d(OvkApplication.APP_TAG,
String.format(
"Handling API message %s in %s (%s)",
msg_code,
getLocalClassName(),
data.getString("address")
)
);
if(msg_code == HandlerMessages.PARSE_JSON) {
new Thread(() -> {
Intent intent = new Intent();
intent.setAction("uk.openvk.android.refresh.API_DATA_RECEIVE");
intent.putExtras(data);
LocalBroadcastManager.getInstance(ctx).sendBroadcast(intent);
}).start();
} else {
receiveState(msg_code, data);
}
};
listeners.processListener = new OvkAPIListeners.OnAPIProcessListener() {
@Override
public void onAPIProcess(Context ctx, Bundle data, long value, long length) {
if(!BuildConfig.BUILD_TYPE.equals("release"))
Log.d(OvkApplication.APP_TAG,
String.format(
"Handling API message %s in %s",
HandlerMessages.UPLOAD_PROGRESS,
getLocalClassName()
)
);
receiveState(HandlerMessages.UPLOAD_PROGRESS, data);
}
listeners.failListener = (ctx, msg_code, data) -> {
if(!BuildConfig.BUILD_TYPE.equals("release"))
Log.d(OvkApplication.APP_TAG,
String.format(
"Handling API message %s in %s",
msg_code,
getLocalClassName()
)
);
receiveState(msg_code, data);
};
listeners.processListener = (ctx, data, value, length) -> {
if(!BuildConfig.BUILD_TYPE.equals("release"))
Log.d(OvkApplication.APP_TAG,
String.format(
"Handling API message %s in %s",
HandlerMessages.UPLOAD_PROGRESS,
getLocalClassName()
)
);
receiveState(HandlerMessages.UPLOAD_PROGRESS, data);
};
ovk_api.wrapper.setAPIListeners(listeners);
ovk_api.dlman.setAPIListeners(listeners);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -24,7 +23,6 @@
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView;
import uk.openvk.android.refresh.Global;
import uk.openvk.android.refresh.OvkApplication;
import uk.openvk.android.refresh.R;
import uk.openvk.android.refresh.api.entities.Message;
import uk.openvk.android.refresh.ui.util.glide.GlideApp;
Expand Down Expand Up @@ -68,7 +66,6 @@ public class Holder extends RecyclerView.ViewHolder {
private final TextView msg_text;
private final TextView msg_text_2;
private final TextView msg_timestamp;
private final TextView msg_timestamp_2;
private final LinearLayout horizontal_layout;
private final LinearLayout vertical_layout;

Expand All @@ -80,7 +77,6 @@ public Holder(View view) {
this.msg_text = (TextView) view.findViewById(R.id.msg_text);
this.msg_timestamp = (TextView) view.findViewById(R.id.timestamp);
this.msg_text_2 = (TextView) view.findViewById(R.id.msg_text_2);
this.msg_timestamp_2 = (TextView) view.findViewById(R.id.timestamp_vertical);
}

@SuppressLint({"SimpleDateFormat", "UseCompatLoadingForDrawables"})
Expand Down Expand Up @@ -177,13 +173,6 @@ private Message getItem(int position) {
if (position >= 0) {
return items.get(position);
} else {
Log.e(OvkApplication.APP_TAG,
String.format(
"[NewsfeedAdapter] Invalid position %s of %s",
position,
items.size()
)
);
return null;
}
} catch (Exception ex) {
Expand Down

0 comments on commit 829c571

Please sign in to comment.