Prompt dialog

This commit is contained in:
Thomas 2020-02-18 11:26:29 +01:00
parent 825407307e
commit f5728f9325
6 changed files with 173 additions and 23 deletions

View File

@ -25,7 +25,7 @@
<activity
android:name=".TransformActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:theme="@style/Theme.AppCompat.Translucent"
android:noHistory="true">
<!-- The app should handle these domains, more can be added here -->

View File

@ -22,10 +22,19 @@ import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcelable;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
@ -45,7 +54,8 @@ import static app.fedilab.nitterizeme.MainActivity.youtube_domains;
public class TransformActivity extends Activity {
private Thread thread;
private String notShortnedURLDialog;
final Pattern youtubePattern = Pattern.compile("(www\\.|m\\.)?(youtube\\.com|youtu\\.be|youtube-nocookie\\.com)/(((?!([\"'<])).)*)");
final Pattern nitterPattern = Pattern.compile("(mobile\\.|www\\.)?twitter.com([\\w-/]+)");
final Pattern maps = Pattern.compile("/maps/place/[^@]+@([\\d.,z]{3,}).*");
@ -72,35 +82,118 @@ public class TransformActivity extends Activity {
} catch (MalformedURLException e) {
e.printStackTrace();
}
//Shortened URLs
if( Arrays.asList(shortener_domains).contains(host)) {
Thread thread = new Thread() {
@Override
public void run() {
String notShortnedURL = Utils.checkUrl(url);
if( notShortnedURL == null) {
notShortnedURL = url;
AlertDialog.Builder unshortenAlertBuilder = new AlertDialog.Builder(TransformActivity.this);
unshortenAlertBuilder.setTitle(R.string.shortened_detected);
View view = getLayoutInflater().inflate(R.layout.popup_unshorten, new LinearLayout(getApplicationContext()), false);
unshortenAlertBuilder.setView(view);
unshortenAlertBuilder.setIcon(R.mipmap.ic_launcher);
unshortenAlertBuilder.setPositiveButton(R.string.open, (dialog, id) -> {
if( notShortnedURLDialog != null){
URL url_1;
String realHost = null;
try {
url_1 = new URL(notShortnedURLDialog);
realHost = url_1.getHost();
} catch (MalformedURLException e) {
e.printStackTrace();
}
boolean nitter_enabled = sharedpreferences.getBoolean(SET_NITTER_ENABLED, true);
if(nitter_enabled) {
String newUrlFinal = notShortnedURL;
Matcher matcher = nitterPattern.matcher(notShortnedURL);
while (matcher.find()) {
final String nitter_directory = matcher.group(2);
String nitterHost = sharedpreferences.getString(MainActivity.SET_NITTER_HOST, MainActivity.DEFAULT_NITTER_HOST).toLowerCase();
newUrlFinal = "https://" + nitterHost + nitter_directory;
if( Arrays.asList(twitter_domains).contains(realHost)) {
boolean nitter_enabled = sharedpreferences.getBoolean(SET_NITTER_ENABLED, true);
if(nitter_enabled) {
Intent delegate = new Intent(Intent.ACTION_VIEW);
String transformedURL = transformUrl(url);
if( transformedURL != null) {
delegate.setData(Uri.parse(transformUrl(url)));
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (delegate.resolveActivity(getPackageManager()) != null) {
startActivity(delegate);
}
}else{
forwardToBrowser(intent);
}
} else {
forwardToBrowser(intent);
}
}
//Maps URLs (containing /maps/place like Google Maps links)
else if( url.contains("/maps/place")) {
boolean osm_enabled = sharedpreferences.getBoolean(MainActivity.SET_OSM_ENABLED, true);
if(osm_enabled) {
Intent delegate = new Intent(Intent.ACTION_VIEW);
String transformedURL = transformUrl(url);
if( transformedURL != null) {
delegate.setData(Uri.parse(transformUrl(url)));
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (delegate.resolveActivity(getPackageManager()) != null) {
startActivity(delegate);
}
}else {
forwardToBrowser(intent);
}
} else {
forwardToBrowser(intent);
}
}
//YouTube URLs
else if(Arrays.asList(youtube_domains).contains(realHost)){ //Youtube URL
boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true);
if( invidious_enabled) {
Intent delegate = new Intent(Intent.ACTION_VIEW);
String transformedURL = transformUrl(url);
if( transformedURL != null) {
delegate.setData(Uri.parse(transformUrl(url)));
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (delegate.resolveActivity(getPackageManager()) != null) {
startActivity(delegate);
}
}else {
forwardToBrowser(intent);
}
}else{
forwardToBrowser(intent);
}
}else {
Intent delegate = new Intent(Intent.ACTION_VIEW);
delegate.setData(Uri.parse(newUrlFinal));
delegate.setData(Uri.parse(notShortnedURLDialog));
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (delegate.resolveActivity(getPackageManager()) != null) {
startActivity(delegate);
}
} else {
forwardToBrowser(intent);
}
}
dialog.dismiss();
finish();
});
unshortenAlertBuilder.setNegativeButton(R.string.dismiss, (dialog, id) -> {
dialog.dismiss();
finish();
});
AlertDialog alertDialog = unshortenAlertBuilder.create();
alertDialog.show();
Button positiveButton = (alertDialog).getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setEnabled(false);
thread = new Thread() {
@Override
public void run() {
notShortnedURLDialog = Utils.checkUrl(url);
if( notShortnedURLDialog == null) {
notShortnedURLDialog = url;
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
positiveButton.setEnabled(true);
String message = getString(R.string.try_to_redirect, url, notShortnedURLDialog);
TextView indications = view.findViewById(R.id.indications);
RelativeLayout progress = view.findViewById(R.id.progress);
indications.setText(message);
indications.setVisibility(View.VISIBLE);
progress.setVisibility(View.GONE);
};
mainHandler.post(myRunnable);
}
};
thread.start();
}
@ -169,6 +262,13 @@ public class TransformActivity extends Activity {
}
}
@Override
protected void onDestroy(){
if( thread != null && thread.isAlive()){
thread.interrupt();
}
super.onDestroy();
}
/**
* Forward the intent to a browser

View File

@ -28,11 +28,17 @@ class Utils {
"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,10}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
static String checkUrl(String urlConnection){
/**
* Returns the unshortened URL
* @param urlStr String URL to check
* @return String the Unshortened URL
*/
static String checkUrl(String urlStr){
URL url;
String redirect = null;
try {
url = new URL(urlConnection);
url = new URL(urlStr);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
httpsURLConnection.setInstanceFollowRedirects(false);
@ -49,7 +55,7 @@ class Utils {
}
}
httpsURLConnection.getInputStream().close();
if (redirect != null && redirect.compareTo(urlConnection)!=0){
if (redirect != null && redirect.compareTo(urlStr)!=0){
URL redirectURL = new URL(redirect);
String host = redirectURL.getHost();
String protocol = redirectURL.getProtocol();

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="@dimen/fab_margin"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:layout_width="wrap_content"
android:id="@+id/loading"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<TextView
android:layout_marginTop="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/loading"
android:text="@string/resolving_shortened"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/indications"
android:visibility="gone"/>
</RelativeLayout>

View File

@ -31,4 +31,9 @@
<string name="hide_supported_links">Hide supported links</string>
<string name="use_geo_uri">Use geo URI</string>
<string name="how_to">How-to</string>
<string name="shortened_detected">Shortened URL</string>
<string name="open">Open</string>
<string name="dismiss">Dismiss</string>
<string name="resolving_shortened">Resolving shortened URL, please wait...</string>
<string name="try_to_redirect">%1$s \n\nwill redirect you to\n\n<b>%2$s</b></string>
</resources>

View File

@ -17,4 +17,13 @@
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="Theme.AppCompat.Translucent">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
</resources>