2020-01-25 10:25:02 +01:00
|
|
|
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>. */
|
2020-01-29 08:13:14 +01:00
|
|
|
import android.content.ComponentName;
|
2020-01-25 10:25:02 +01:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2020-01-29 08:13:14 +01:00
|
|
|
import android.content.pm.ResolveInfo;
|
2020-01-25 10:25:02 +01:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2020-01-29 08:13:14 +01:00
|
|
|
import android.os.Parcelable;
|
|
|
|
|
2020-01-25 10:25:02 +01:00
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
2020-01-29 08:13:14 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2020-01-25 15:51:45 +01:00
|
|
|
import java.util.Objects;
|
2020-01-25 10:25:02 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2020-01-28 18:54:19 +01:00
|
|
|
import static app.fedilab.nitterizeme.MainActivity.SET_INVIDIOUS_ENABLED;
|
|
|
|
import static app.fedilab.nitterizeme.MainActivity.SET_NITTER_ENABLED;
|
|
|
|
|
2020-01-25 10:25:02 +01:00
|
|
|
|
|
|
|
public class TransformActivity extends AppCompatActivity {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final Pattern youtubePattern = Pattern.compile("(www\\.|m\\.)?(youtube\\.com|youtu\\.be|youtube-nocookie\\.com)/(((?!([\"'<])).)*)");
|
|
|
|
final Pattern nitterPattern = Pattern.compile("(mobile\\.|www\\.)?twitter.com([\\w-/]+)");
|
2020-01-29 12:58:26 +01:00
|
|
|
final Pattern googleMap = Pattern.compile("google\\.com/maps[^@]+@([\\d.,z]{3,}).*");
|
2020-01-25 10:25:02 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
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;
|
|
|
|
//Twitter URLs
|
|
|
|
if( url.contains("twitter")) {
|
2020-01-28 18:54:19 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-01-29 08:13:14 +01:00
|
|
|
Intent delegate = new Intent(Intent.ACTION_VIEW);
|
|
|
|
delegate.setData(Uri.parse(newUrl));
|
2020-01-29 12:58:26 +01:00
|
|
|
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
if (delegate.resolveActivity(getPackageManager()) != null) {
|
|
|
|
startActivity(delegate);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
forwardToBrowser(intent);
|
|
|
|
}
|
|
|
|
}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];
|
|
|
|
}
|
2020-02-02 19:12:04 +01:00
|
|
|
String osmHost = sharedpreferences.getString(MainActivity.SET_OSM_HOST, MainActivity.DEFAULT_OSM_HOST).toLowerCase();
|
|
|
|
newUrl = "https://"+osmHost+"/#map="+zoom+"/"+data[0]+"/"+data[1];
|
2020-01-29 12:58:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Intent delegate = new Intent(Intent.ACTION_VIEW);
|
|
|
|
delegate.setData(Uri.parse(newUrl));
|
2020-01-29 08:13:14 +01:00
|
|
|
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
if (delegate.resolveActivity(getPackageManager()) != null) {
|
|
|
|
startActivity(delegate);
|
|
|
|
}
|
2020-01-28 18:54:19 +01:00
|
|
|
} else {
|
2020-01-29 08:13:14 +01:00
|
|
|
forwardToBrowser(intent);
|
2020-01-25 10:25:02 +01:00
|
|
|
}
|
|
|
|
}else{ //Youtube URL
|
2020-01-28 18:54:19 +01:00
|
|
|
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";
|
|
|
|
}
|
2020-01-25 10:25:02 +01:00
|
|
|
}
|
2020-01-29 08:13:14 +01:00
|
|
|
Intent delegate = new Intent(Intent.ACTION_VIEW);
|
|
|
|
delegate.setData(Uri.parse(newUrl));
|
|
|
|
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
if (delegate.resolveActivity(getPackageManager()) != null) {
|
|
|
|
startActivity(delegate);
|
|
|
|
}
|
2020-01-28 18:54:19 +01:00
|
|
|
}else{
|
2020-01-29 08:13:14 +01:00
|
|
|
forwardToBrowser(intent);
|
2020-01-25 10:25:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 08:13:14 +01:00
|
|
|
}
|
2020-01-25 10:25:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-29 08:13:14 +01:00
|
|
|
|
|
|
|
private void forwardToBrowser(Intent i) {
|
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.setAction(android.content.Intent.ACTION_VIEW);
|
|
|
|
intent.setDataAndType(i.getData(), i.getType());
|
|
|
|
List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0);
|
|
|
|
ArrayList<Intent> targetIntents = new ArrayList<>();
|
|
|
|
String thisPackageName = getApplicationContext().getPackageName();
|
|
|
|
for (ResolveInfo currentInfo : activities) {
|
|
|
|
String packageName = currentInfo.activityInfo.packageName;
|
|
|
|
if (!thisPackageName.equals(packageName)) {
|
|
|
|
Intent targetIntent = new Intent(android.content.Intent.ACTION_VIEW);
|
|
|
|
targetIntent.setDataAndType(intent.getData(), intent.getType());
|
|
|
|
targetIntent.setPackage(intent.getPackage());
|
|
|
|
targetIntent.setComponent(new ComponentName(packageName, currentInfo.activityInfo.name));
|
|
|
|
targetIntents.add(targetIntent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (targetIntents.size() > 0) {
|
|
|
|
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), getString(R.string.open_with));
|
|
|
|
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
|
|
|
|
startActivity(chooserIntent);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 10:25:02 +01:00
|
|
|
}
|