Check redirect
This commit is contained in:
parent
fd0832c88d
commit
f98ae2002e
|
@ -27,6 +27,7 @@ import android.graphics.drawable.Drawable;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
|
@ -64,6 +65,8 @@ import com.github.penfeizhou.animation.apng.decode.APNGParser;
|
||||||
import com.github.penfeizhou.animation.gif.GifDrawable;
|
import com.github.penfeizhou.animation.gif.GifDrawable;
|
||||||
import com.github.penfeizhou.animation.gif.decode.GifParser;
|
import com.github.penfeizhou.animation.gif.decode.GifParser;
|
||||||
|
|
||||||
|
import net.gotev.uploadservice.http.HttpConnection;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
@ -86,6 +89,7 @@ import app.fedilab.android.activities.PeertubeActivity;
|
||||||
import app.fedilab.android.activities.ShowAccountActivity;
|
import app.fedilab.android.activities.ShowAccountActivity;
|
||||||
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
|
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
|
||||||
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
|
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
|
||||||
|
import app.fedilab.android.client.HttpsConnection;
|
||||||
import app.fedilab.android.fragments.TabLayoutNotificationsFragment;
|
import app.fedilab.android.fragments.TabLayoutNotificationsFragment;
|
||||||
import app.fedilab.android.helper.CrossActions;
|
import app.fedilab.android.helper.CrossActions;
|
||||||
import app.fedilab.android.helper.CustomQuoteSpan;
|
import app.fedilab.android.helper.CustomQuoteSpan;
|
||||||
|
@ -667,11 +671,6 @@ public class Status implements Parcelable {
|
||||||
PopupMenu popup = new PopupMenu(context, textView);
|
PopupMenu popup = new PopupMenu(context, textView);
|
||||||
popup.getMenuInflater()
|
popup.getMenuInflater()
|
||||||
.inflate(R.menu.links_popup, popup.getMenu());
|
.inflate(R.menu.links_popup, popup.getMenu());
|
||||||
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
|
|
||||||
public boolean onMenuItemClick(MenuItem item) {
|
|
||||||
switch (item.getItemId()) {
|
|
||||||
case R.id.action_show_link:
|
|
||||||
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
|
|
||||||
int style;
|
int style;
|
||||||
if (theme == Helper.THEME_DARK) {
|
if (theme == Helper.THEME_DARK) {
|
||||||
style = R.style.DialogDark;
|
style = R.style.DialogDark;
|
||||||
|
@ -680,6 +679,12 @@ public class Status implements Parcelable {
|
||||||
} else {
|
} else {
|
||||||
style = R.style.Dialog;
|
style = R.style.Dialog;
|
||||||
}
|
}
|
||||||
|
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
|
||||||
|
public boolean onMenuItemClick(MenuItem item) {
|
||||||
|
switch (item.getItemId()) {
|
||||||
|
case R.id.action_show_link:
|
||||||
|
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
|
||||||
|
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(context, style);
|
AlertDialog.Builder builder = new AlertDialog.Builder(context, style);
|
||||||
builder.setMessage(url);
|
builder.setMessage(url);
|
||||||
builder.setTitle(context.getString(R.string.display_full_link));
|
builder.setTitle(context.getString(R.string.display_full_link));
|
||||||
|
@ -716,6 +721,38 @@ public class Status implements Parcelable {
|
||||||
Toasty.info(context, context.getString(R.string.clipboard_url), Toast.LENGTH_LONG).show();
|
Toasty.info(context, context.getString(R.string.clipboard_url), Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case R.id.action_unshorten:
|
||||||
|
Thread thread = new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
String response = new HttpsConnection(context, null).checkUrl(url);
|
||||||
|
|
||||||
|
Handler mainHandler = new Handler(context.getMainLooper());
|
||||||
|
|
||||||
|
Runnable myRunnable = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
AlertDialog.Builder builder = new AlertDialog.Builder(context, style);
|
||||||
|
if( response != null ) {
|
||||||
|
builder.setMessage(response);
|
||||||
|
}else{
|
||||||
|
builder.setMessage(R.string.no_redirect);
|
||||||
|
}
|
||||||
|
builder.setTitle(context.getString(R.string.check_redirect));
|
||||||
|
builder.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
}).show();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
mainHandler.post(myRunnable);
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
thread.start();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,7 @@ import java.net.HttpURLConnection;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.PasswordAuthentication;
|
import java.net.PasswordAuthentication;
|
||||||
|
import java.net.ProtocolException;
|
||||||
import java.net.Proxy;
|
import java.net.Proxy;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
@ -76,6 +77,8 @@ import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
import static app.fedilab.android.helper.Helper.urlPattern;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Thomas on 17/11/2017.
|
* Created by Thomas on 17/11/2017.
|
||||||
|
@ -253,6 +256,41 @@ public class HttpsConnection {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public String checkUrl(String urlConnection){
|
||||||
|
URL url;
|
||||||
|
String redirect = null;
|
||||||
|
try {
|
||||||
|
url = new URL(urlConnection);
|
||||||
|
if (proxy != null)
|
||||||
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
||||||
|
else
|
||||||
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
||||||
|
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
|
||||||
|
httpsURLConnection.setInstanceFollowRedirects(false);
|
||||||
|
httpsURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
|
||||||
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
|
||||||
|
httpsURLConnection.setRequestMethod("GET");
|
||||||
|
if( httpsURLConnection.getResponseCode() == 301) {
|
||||||
|
Map<String, List<String>> map = httpsURLConnection.getHeaderFields();
|
||||||
|
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
|
||||||
|
if (entry.toString().startsWith("Location") || entry.toString().startsWith("Location")) {
|
||||||
|
Matcher matcher = urlPattern.matcher(entry.toString());
|
||||||
|
if (matcher.find()) {
|
||||||
|
redirect = matcher.group(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
httpsURLConnection.getInputStream().close();
|
||||||
|
return redirect != null && redirect.compareTo(urlConnection)!=0?redirect:null;
|
||||||
|
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public String get(String urlConnection) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
public String get(String urlConnection) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= 21) {
|
if (Build.VERSION.SDK_INT >= 21) {
|
||||||
|
|
|
@ -17,4 +17,8 @@
|
||||||
android:id="@+id/action_copy_link"
|
android:id="@+id/action_copy_link"
|
||||||
android:title="@string/copy_link"
|
android:title="@string/copy_link"
|
||||||
/>
|
/>
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_unshorten"
|
||||||
|
android:title="@string/check_redirect"
|
||||||
|
/>
|
||||||
</menu>
|
</menu>
|
||||||
|
|
|
@ -1194,4 +1194,6 @@
|
||||||
<string name="share_link">Share link</string>
|
<string name="share_link">Share link</string>
|
||||||
<string name="link_copy">The URL has been copied to the clipboard</string>
|
<string name="link_copy">The URL has been copied to the clipboard</string>
|
||||||
<string name="open_other_app">Open with another app</string>
|
<string name="open_other_app">Open with another app</string>
|
||||||
|
<string name="check_redirect">Check redirect</string>
|
||||||
|
<string name="no_redirect">This URL does not redirect</string>
|
||||||
</resources>
|
</resources>
|
Loading…
Reference in New Issue