NewPipe-app-android/app/src/main/java/org/schabi/newpipe/RouterActivity.java

193 lines
6.4 KiB
Java
Raw Normal View History

2017-02-27 21:14:03 +01:00
package org.schabi.newpipe;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
2018-01-23 01:40:00 +01:00
import android.text.TextUtils;
2017-02-27 21:14:03 +01:00
import android.widget.Toast;
2018-01-23 01:40:00 +01:00
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.report.UserAction;
import org.schabi.newpipe.util.ExtractorHelper;
import org.schabi.newpipe.util.NavigationHelper;
2017-02-27 21:14:03 +01:00
import java.util.Collection;
import java.util.HashSet;
2018-01-23 01:40:00 +01:00
import icepick.Icepick;
import icepick.State;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
/*
2017-02-27 21:14:03 +01:00
* Copyright (C) Christian Schabesberger 2017 <chris.schabesberger@mailbox.org>
2017-09-26 17:29:38 +02:00
* RouterActivity.java is part of NewPipe.
2017-02-27 21:14:03 +01:00
*
2017-09-26 17:29:38 +02:00
* NewPipe is free software: you can redistribute it and/or modify
2017-02-27 21:14:03 +01:00
* 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.
*
2017-09-26 17:29:38 +02:00
* NewPipe is distributed in the hope that it will be useful,
2017-02-27 21:14:03 +01:00
* 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
2017-09-26 17:29:38 +02:00
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2017-02-27 21:14:03 +01:00
*/
/**
* This Acitivty is designed to route share/open intents to the specified service, and
* to the part of the service which can handle the url.
*/
public class RouterActivity extends AppCompatActivity {
2017-02-27 21:14:03 +01:00
2018-01-23 01:40:00 +01:00
@State
protected String currentUrl;
protected CompositeDisposable disposables = new CompositeDisposable();
2017-02-27 21:14:03 +01:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2018-01-23 01:40:00 +01:00
Icepick.restoreInstanceState(this, savedInstanceState);
2017-06-05 21:33:01 +02:00
2018-01-23 01:40:00 +01:00
if (TextUtils.isEmpty(currentUrl)) {
currentUrl = getUrl(getIntent());
if (TextUtils.isEmpty(currentUrl)) {
Toast.makeText(this, R.string.invalid_url_toast, Toast.LENGTH_LONG).show();
finish();
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Icepick.saveInstanceState(this, outState);
}
@Override
protected void onStart() {
super.onStart();
handleUrl(currentUrl);
2017-02-27 21:14:03 +01:00
}
2017-06-05 21:33:01 +02:00
protected void handleUrl(String url) {
2018-01-23 01:40:00 +01:00
disposables.add(Observable
.fromCallable(() -> NavigationHelper.getIntentByLink(this, url))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(intent -> {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}, this::handleError)
);
}
protected void handleError(Throwable error) {
error.printStackTrace();
if (error instanceof ExtractionException) {
2017-06-05 21:33:01 +02:00
Toast.makeText(this, R.string.url_not_supported_toast, Toast.LENGTH_LONG).show();
2018-01-23 01:40:00 +01:00
} else {
ExtractorHelper.handleGeneralException(this, -1, null, error, UserAction.SOMETHING_ELSE, null);
2017-06-05 21:33:01 +02:00
}
finish();
2017-06-05 21:33:01 +02:00
}
2018-01-23 01:40:00 +01:00
@Override
protected void onDestroy() {
super.onDestroy();
disposables.clear();
}
2017-06-05 21:33:01 +02:00
/*//////////////////////////////////////////////////////////////////////////
// Utils
//////////////////////////////////////////////////////////////////////////*/
/**
* Removes invisible separators (\p{Z}) and punctuation characters including
* brackets (\p{P}). See http://www.regular-expressions.info/unicode.html for
* more details.
*/
protected final static String REGEX_REMOVE_FROM_URL = "[\\p{Z}\\p{P}]";
2017-06-05 21:33:01 +02:00
protected String getUrl(Intent intent) {
// first gather data and find service
2017-06-05 21:33:01 +02:00
String videoUrl = null;
if (intent.getData() != null) {
// this means the video was called though another app
videoUrl = intent.getData().toString();
} else if (intent.getStringExtra(Intent.EXTRA_TEXT) != null) {
//this means that vidoe was called through share menu
String extraText = intent.getStringExtra(Intent.EXTRA_TEXT);
2018-01-23 01:40:00 +01:00
final String[] uris = getUris(extraText);
videoUrl = uris.length > 0 ? uris[0] : null;
}
2017-06-05 21:33:01 +02:00
return videoUrl;
}
2017-02-27 21:14:03 +01:00
2017-06-05 21:33:01 +02:00
protected String removeHeadingGibberish(final String input) {
2017-02-27 21:14:03 +01:00
int start = 0;
for (int i = input.indexOf("://") - 1; i >= 0; i--) {
if (!input.substring(i, i + 1).matches("\\p{L}")) {
start = i + 1;
break;
}
}
return input.substring(start, input.length());
}
2017-06-05 21:33:01 +02:00
protected String trim(final String input) {
2017-02-27 21:14:03 +01:00
if (input == null || input.length() < 1) {
return input;
} else {
String output = input;
while (output.length() > 0 && output.substring(0, 1).matches(REGEX_REMOVE_FROM_URL)) {
output = output.substring(1);
}
while (output.length() > 0
&& output.substring(output.length() - 1, output.length()).matches(REGEX_REMOVE_FROM_URL)) {
output = output.substring(0, output.length() - 1);
}
return output;
}
}
/**
* Retrieves all Strings which look remotely like URLs from a text.
* Used if NewPipe was called through share menu.
*
* @param sharedText text to scan for URLs.
* @return potential URLs
*/
2017-06-05 21:33:01 +02:00
protected String[] getUris(final String sharedText) {
2017-02-27 21:14:03 +01:00
final Collection<String> result = new HashSet<>();
if (sharedText != null) {
final String[] array = sharedText.split("\\p{Space}");
for (String s : array) {
s = trim(s);
if (s.length() != 0) {
if (s.matches(".+://.+")) {
result.add(removeHeadingGibberish(s));
} else if (s.matches(".+\\..+")) {
result.add("http://" + s);
}
}
}
}
return result.toArray(new String[result.size()]);
}
}