-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLRedirectionManager.java
189 lines (157 loc) · 5.09 KB
/
URLRedirectionManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package org.comic.verse;
import android.content.Context;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import java.util.ArrayList;
public class URLRedirectionManager {
private ArrayList<VideoURL> videoURLList = new ArrayList<>();
private int processedURLCount;
private Context appContext;
private OnURLProcessedListener urlProcessedListener;
private OnStringResponseListener stringResponseListener;
public boolean succesResponse;
public interface OnURLProcessedListener {
void onSuccess(ArrayList<VideoURL> videoURLList);
void onError(String errorMessage);
}
public interface OnStringResponseListener {
void onSuccess(String url);
void onError(String errorMessage);
}
public void setListener(OnURLProcessedListener listener) {
this.urlProcessedListener = listener;
}
public URLRedirectionManager(Context context) {
this.appContext = context;
}
public void redirectURL(String url, OnStringResponseListener responseListener) {
this.stringResponseListener = responseListener;
WebView webView = new WebView(appContext);
webView.getSettings().setLoadsImagesAutomatically(false);
webView.getSettings().setJavaScriptEnabled(false);
webView.setWebViewClient(new CustomStringWebViewClient());
webView.loadUrl(url);
}
public void addURL(VideoURL videoURL) {
videoURLList.add(videoURL);
}
public void attachList() {
this.processedURLCount = 0;
this.succesResponse = false;
for (int i = 0; i < videoURLList.size(); i++) {
WebView webView = new WebView(appContext);
webView.getSettings().setLoadsImagesAutomatically(false);
webView.getSettings().setJavaScriptEnabled(false);
webView.setWebViewClient(new CustomWebViewClient(i));
webView.loadUrl(videoURLList.get(i).getUrl());
}
}
public ArrayList<VideoURL> getList() {
return videoURLList;
}
private class CustomWebViewClient extends WebViewClient {
private int urlPosition;
public CustomWebViewClient(int position) {
this.urlPosition = position;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String actualUrl = url;
videoURLList.get(urlPosition).setFixUrl(actualUrl);
processedURLCount++;
if (processedURLCount == videoURLList.size()) {
if (urlProcessedListener != null && !succesResponse) {
urlProcessedListener.onSuccess(videoURLList);
succesResponse = true;
}
}
view.destroy();
return true; //must be true
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (urlProcessedListener != null && !succesResponse) {
urlProcessedListener.onError(error.getDescription().toString());
succesResponse = true;
}
}
}
private class CustomStringWebViewClient extends WebViewClient {
public CustomStringWebViewClient() {}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String actualUrl = url;
if (stringResponseListener != null) {
stringResponseListener.onSuccess(actualUrl);
}
view.destroy();
return true; //must be true
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (stringResponseListener != null) {
stringResponseListener.onError(error.getDescription().toString());
}
}
}
@Override
public String toString() {
StringBuilder urlListStringBuilder = new StringBuilder();
for (VideoURL videoURL : videoURLList) {
urlListStringBuilder.append(videoURL.toString()).append("\n");
}
return urlListStringBuilder.toString();
}
public static class Builder {
private Context builderContext;
private URLRedirectionManager redirectionManager;
public Builder(Context context) {
this.builderContext = context;
this.redirectionManager = new URLRedirectionManager(context);
}
public Builder addURL(VideoURL videoURL) {
this.redirectionManager.addURL(videoURL);
return this;
}
public Builder setListener(OnURLProcessedListener listener) {
this.redirectionManager.setListener(listener);
return this;
}
public URLRedirectionManager build() {
return this.redirectionManager;
}
}
public static class VideoURL {
private String title;
private String url;
private String fixUrl;
public VideoURL(String title, String url) {
this.url = url;
this.title = title;
}
public String getUrl() {
return url;
}
public void setFixUrl(String fixUrl) {
this.fixUrl = fixUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "VideoURL{" +
"url='" + url + '\'' +
", fixUrl='" + fixUrl + '\'' +
", title='" + title + '\'' +
'}';
}
}
}