This commit is contained in:
Thomas 2020-02-17 11:57:14 +01:00
parent e91a7bc39b
commit a98f05bd6f
4 changed files with 411 additions and 52 deletions

View File

@ -23,6 +23,26 @@
</activity>
<activity android:name=".TransformActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar">
<!-- The app should handle these domains, more can be added here -->
<!-- t.co -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="t.co"
android:pathPattern=".*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"
android:host="t.co" />
</intent-filter>
<!-- mobile.twitter.com -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -31,6 +51,14 @@
android:host="mobile.twitter.com"
android:pathPattern=".*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"
android:host="mobile.twitter.com" />
</intent-filter>
<!-- www.twitter.com -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -39,6 +67,14 @@
android:host="www.twitter.com"
android:pathPattern=".*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"
android:host="www.twitter.com" />
</intent-filter>
<!-- twitter.com -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -47,6 +83,15 @@
android:host="twitter.com"
android:pathPattern=".*"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data
android:mimeType="text/plain"
android:host="twitter.com" />
</intent-filter>
<!-- www.youtube.com -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -54,6 +99,14 @@
<data android:scheme="https"
android:host="www.youtube.com" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"
android:host="www.youtube.com" />
</intent-filter>
<!-- m.youtube.com -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -61,6 +114,14 @@
<data android:scheme="https"
android:host="m.youtube.com" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"
android:host="m.youtube.com" />
</intent-filter>
<!-- youtube.com -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -68,6 +129,14 @@
<data android:scheme="https"
android:host="youtube.com" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"
android:host="youtube.com" />
</intent-filter>
<!-- youtu.be -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -75,6 +144,14 @@
<data android:scheme="https"
android:host="youtu.be" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"
android:host="youtu.be" />
</intent-filter>
<!-- youtube-nocookie.com -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -82,6 +159,14 @@
<data android:scheme="https"
android:host="youtube-nocookie.com" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"
android:host="youtube-nocookie.com" />
</intent-filter>
<!-- google.com/maps/ -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -90,6 +175,15 @@
android:pathPattern="/maps/.*"
android:host="google.com" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"
android:pathPattern="/maps/.*"
android:host="google.com" />
</intent-filter>
<!-- www.google.com/maps/ -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
@ -98,6 +192,14 @@
android:pathPattern="/maps/.*"
android:host="www.google.com" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"
android:pathPattern="/maps/.*"
android:host="www.google.com" />
</intent-filter>
</activity>
<activity

View File

@ -61,6 +61,7 @@ public class MainActivity extends AppCompatActivity {
//Supported domains
private String[] domains = {
"t.co",
"twitter.com",
"mobile.twitter.com",
"www.twitter.com",

View File

@ -13,18 +13,19 @@ package app.fedilab.nitterizeme;
*
* You should have received a copy of the GNU General Public License along with NitterizeMe; if not,
* see <http://www.gnu.org/licenses>. */
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Patterns;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -35,7 +36,7 @@ import static app.fedilab.nitterizeme.MainActivity.SET_INVIDIOUS_ENABLED;
import static app.fedilab.nitterizeme.MainActivity.SET_NITTER_ENABLED;
public class TransformActivity extends AppCompatActivity {
public class TransformActivity extends Activity {
@ -47,89 +48,89 @@ public class TransformActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
SharedPreferences sharedpreferences = getSharedPreferences(MainActivity.APP_PREFS, Context.MODE_PRIVATE);
Intent intent = getIntent();
if( intent != null && intent.getData() != null){
String url = intent.getData().toString();
String newUrl = null;
if( Objects.requireNonNull(intent.getAction()).equals(Intent.ACTION_VIEW)){
String action = intent.getAction();
String url = Objects.requireNonNull(intent.getData()).toString();
//Twitter URLs
if( url.contains("twitter")) {
if( url.contains("t.co")) {
Thread thread = new Thread() {
@Override
public void run() {
String notShortnedURL = Utils.checkUrl(url);
if( notShortnedURL == null) {
notShortnedURL = url;
}
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;
}
Intent delegate = new Intent(action);
delegate.setData(Uri.parse(newUrlFinal));
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (delegate.resolveActivity(getPackageManager()) != null) {
startActivity(delegate);
}
} else {
forwardToBrowser(intent, action);
}
}
};
thread.start();
} else if( url.contains("twitter")) {
boolean nitter_enabled = sharedpreferences.getBoolean(SET_NITTER_ENABLED, true);
if(nitter_enabled) {
Matcher matcher = nitterPattern.matcher(url);
while (matcher.find()) {
final String nitter_directory = matcher.group(2);
String nitterHost = sharedpreferences.getString(MainActivity.SET_NITTER_HOST, MainActivity.DEFAULT_NITTER_HOST).toLowerCase();
newUrl = "https://" + nitterHost + nitter_directory;
}
Intent delegate = new Intent(Intent.ACTION_VIEW);
delegate.setData(Uri.parse(newUrl));
Intent delegate = new Intent(action);
delegate.setData(Uri.parse(transformUrl(url)));
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (delegate.resolveActivity(getPackageManager()) != null) {
startActivity(delegate);
}
} else {
forwardToBrowser(intent);
forwardToBrowser(intent, action);
}
}else if( url.contains("google")) {
boolean osm_enabled = sharedpreferences.getBoolean(MainActivity.SET_OSM_ENABLED, true);
if(osm_enabled) {
Matcher matcher = googleMap.matcher(url);
while (matcher.find()) {
final String localization = matcher.group(1);
assert localization != null;
String[] data = localization.split(",");
if( data.length > 2 ){
String zoom;
String[] details = data[2].split("\\.");
if( details.length > 0 ) {
zoom = details[0];
}else {
zoom = data[2];
}
String osmHost = sharedpreferences.getString(MainActivity.SET_OSM_HOST, MainActivity.DEFAULT_OSM_HOST).toLowerCase();
newUrl = "https://"+osmHost+"/#map="+zoom+"/"+data[0]+"/"+data[1];
}
}
Intent delegate = new Intent(Intent.ACTION_VIEW);
delegate.setData(Uri.parse(newUrl));
Intent delegate = new Intent(action);
delegate.setData(Uri.parse(transformUrl(url)));
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (delegate.resolveActivity(getPackageManager()) != null) {
startActivity(delegate);
}
} else {
forwardToBrowser(intent);
forwardToBrowser(intent, action);
}
}else{ //Youtube URL
boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true);
if( invidious_enabled) {
Matcher matcher = youtubePattern.matcher(url);
while (matcher.find()) {
final String youtubeId = matcher.group(3);
String invidiousHost = sharedpreferences.getString(MainActivity.SET_INVIDIOUS_HOST, MainActivity.DEFAULT_INVIDIOUS_HOST).toLowerCase();
if (Objects.requireNonNull(matcher.group(2)).compareTo("youtu.be") == 0) {
newUrl = "https://" + invidiousHost + "/watch?v=" + youtubeId + "&local=true";
} else {
newUrl = "https://" + invidiousHost + "/" + youtubeId + "&local=true";
}
}
Intent delegate = new Intent(Intent.ACTION_VIEW);
delegate.setData(Uri.parse(newUrl));
Intent delegate = new Intent(action);
delegate.setData(Uri.parse(transformUrl(url)));
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (delegate.resolveActivity(getPackageManager()) != null) {
startActivity(delegate);
}
}else{
forwardToBrowser(intent);
forwardToBrowser(intent, action);
}
}
}
else if( Objects.requireNonNull(intent.getAction()).equals(Intent.ACTION_SEND)){
share(intent.getStringExtra(Intent.EXTRA_TEXT));
}
}
private void forwardToBrowser(Intent i) {
private void forwardToBrowser(Intent i, String action) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setAction(action);
intent.setDataAndType(i.getData(), i.getType());
List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0);
ArrayList<Intent> targetIntents = new ArrayList<>();
@ -137,7 +138,7 @@ public class TransformActivity extends AppCompatActivity {
for (ResolveInfo currentInfo : activities) {
String packageName = currentInfo.activityInfo.packageName;
if (!thisPackageName.equals(packageName)) {
Intent targetIntent = new Intent(android.content.Intent.ACTION_VIEW);
Intent targetIntent = new Intent(action);
targetIntent.setDataAndType(intent.getData(), intent.getType());
targetIntent.setPackage(intent.getPackage());
targetIntent.setComponent(new ComponentName(packageName, currentInfo.activityInfo.name));
@ -151,4 +152,193 @@ public class TransformActivity extends AppCompatActivity {
finish();
}
}
private String transformUrl(String url) {
SharedPreferences sharedpreferences = getSharedPreferences(MainActivity.APP_PREFS, Context.MODE_PRIVATE);
String newUrl = null;
if( url.contains("twitter")) {
boolean nitter_enabled = sharedpreferences.getBoolean(SET_NITTER_ENABLED, true);
if(nitter_enabled) {
Matcher matcher = nitterPattern.matcher(url);
while (matcher.find()) {
final String nitter_directory = matcher.group(2);
String nitterHost = sharedpreferences.getString(MainActivity.SET_NITTER_HOST, MainActivity.DEFAULT_NITTER_HOST).toLowerCase();
newUrl = "https://" + nitterHost + nitter_directory;
}
if( newUrl != null) {
return newUrl;
}else {
return url;
}
} else {
return url;
}
}else if( url.contains("google")) {
boolean osm_enabled = sharedpreferences.getBoolean(MainActivity.SET_OSM_ENABLED, true);
if(osm_enabled) {
Matcher matcher = googleMap.matcher(url);
while (matcher.find()) {
final String localization = matcher.group(1);
assert localization != null;
String[] data = localization.split(",");
if( data.length > 2 ){
String zoom;
String[] details = data[2].split("\\.");
if( details.length > 0 ) {
zoom = details[0];
}else {
zoom = data[2];
}
String osmHost = sharedpreferences.getString(MainActivity.SET_OSM_HOST, MainActivity.DEFAULT_OSM_HOST).toLowerCase();
newUrl = "https://"+osmHost+"/#map="+zoom+"/"+data[0]+"/"+data[1];
}
}
if( newUrl != null) {
return newUrl;
}else {
return url;
}
} else {
return url;
}
}else{ //Youtube URL
boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true);
if( invidious_enabled) {
Matcher matcher = youtubePattern.matcher(url);
while (matcher.find()) {
final String youtubeId = matcher.group(3);
String invidiousHost = sharedpreferences.getString(MainActivity.SET_INVIDIOUS_HOST, MainActivity.DEFAULT_INVIDIOUS_HOST).toLowerCase();
if (Objects.requireNonNull(matcher.group(2)).compareTo("youtu.be") == 0) {
newUrl = "https://" + invidiousHost + "/watch?v=" + youtubeId + "&local=true";
} else {
newUrl = "https://" + invidiousHost + "/" + youtubeId + "&local=true";
}
}
if( newUrl != null) {
return newUrl;
}else {
return url;
}
}else{
return url;
}
}
}
private void share(String extraText) {
SharedPreferences sharedpreferences = getSharedPreferences(MainActivity.APP_PREFS, Context.MODE_PRIVATE);
String url = null;
if (extraText != null) {
Matcher matcher;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
matcher = Patterns.WEB_URL.matcher(extraText);
else
matcher = Utils.urlPattern.matcher(extraText);
while (matcher.find()) {
int matchStart = matcher.start(1);
int matchEnd = matcher.end();
if (matchStart < matchEnd && extraText.length() >= matchEnd) {
url = extraText.substring(matchStart, matchEnd);
}
}
}
String newUrl = null;
if( url == null){
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, extraText);
sendIntent.setType("text/plain");
startActivity(sendIntent);
return;
}
if( url.contains("twitter")) {
boolean nitter_enabled = sharedpreferences.getBoolean(SET_NITTER_ENABLED, true);
if(nitter_enabled) {
Matcher matcher = nitterPattern.matcher(url);
while (matcher.find()) {
final String nitter_directory = matcher.group(2);
String nitterHost = sharedpreferences.getString(MainActivity.SET_NITTER_HOST, MainActivity.DEFAULT_NITTER_HOST).toLowerCase();
newUrl = "https://" + nitterHost + nitter_directory;
}
}
}else if( url.contains("google")) {
boolean osm_enabled = sharedpreferences.getBoolean(MainActivity.SET_OSM_ENABLED, true);
if(osm_enabled) {
Matcher matcher = googleMap.matcher(url);
while (matcher.find()) {
final String localization = matcher.group(1);
assert localization != null;
String[] data = localization.split(",");
if( data.length > 2 ){
String zoom;
String[] details = data[2].split("\\.");
if( details.length > 0 ) {
zoom = details[0];
}else {
zoom = data[2];
}
String osmHost = sharedpreferences.getString(MainActivity.SET_OSM_HOST, MainActivity.DEFAULT_OSM_HOST).toLowerCase();
newUrl = "https://"+osmHost+"/#map="+zoom+"/"+data[0]+"/"+data[1];
}
}
}
}else{ //Youtube URL
boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true);
if( invidious_enabled) {
Matcher matcher = youtubePattern.matcher(url);
while (matcher.find()) {
final String youtubeId = matcher.group(3);
String invidiousHost = sharedpreferences.getString(MainActivity.SET_INVIDIOUS_HOST, MainActivity.DEFAULT_INVIDIOUS_HOST).toLowerCase();
if (Objects.requireNonNull(matcher.group(2)).compareTo("youtu.be") == 0) {
newUrl = "https://" + invidiousHost + "/watch?v=" + youtubeId + "&local=true";
} else {
newUrl = "https://" + invidiousHost + "/" + youtubeId + "&local=true";
}
}
}
} if( url.contains("t.co")) {
String finalUrl = url;
String finalExtraText = extraText;
Thread thread = new Thread() {
@Override
public void run() {
String notShortnedURL = Utils.checkUrl(finalUrl);
if( notShortnedURL == null) {
notShortnedURL = finalUrl;
}
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;
}
String newExtraText = finalExtraText.replaceAll(Pattern.quote(finalUrl), Matcher.quoteReplacement(newUrlFinal));
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, newExtraText);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
}
};
thread.start();
return;
}
if( newUrl != null) {
extraText = extraText.replaceAll(Pattern.quote(url), Matcher.quoteReplacement(newUrl));
}
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, extraText);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
}

View File

@ -0,0 +1,66 @@
package app.fedilab.nitterizeme;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of NitterizeMe
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* NitterizeMe is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with NitterizeMe; if not,
* see <http://www.gnu.org/licenses>. */
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
class Utils {
static final Pattern urlPattern = Pattern.compile(
"(?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){
URL url;
String redirect = null;
try {
url = new URL(urlConnection);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
httpsURLConnection.setInstanceFollowRedirects(false);
httpsURLConnection.setRequestMethod("HEAD");
if( httpsURLConnection.getResponseCode() == 301) {
Map<String, List<String>> map = httpsURLConnection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
if (entry.toString().toLowerCase().startsWith("location")) {
Matcher matcher = urlPattern.matcher(entry.toString());
if (matcher.find()) {
redirect = matcher.group(1);
}
}
}
}
httpsURLConnection.getInputStream().close();
if (redirect != null && redirect.compareTo(urlConnection)!=0){
URL redirectURL = new URL(redirect);
String host = redirectURL.getHost();
String protocol = redirectURL.getProtocol();
if( protocol == null || host == null){
redirect = null;
}
}
return redirect;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}