diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 5ce399d91..9c5ea2381 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -76,6 +76,11 @@
+
+
+
+
+
@@ -38,6 +41,13 @@ import org.schabi.newpipe.extractor.StreamingService;
public class VideoItemDetailActivity extends AppCompatActivity {
+ /**
+ * Removes invisible separators (\p{Z}) and punctuation characters including
+ * brackets (\p{P}). See http://www.regular-expressions.info/unicode.html for
+ * more details.
+ */
+ private final static String REGEX_REMOVE_FROM_URL = "[\\p{Z}\\p{P}]";
+
private static final String TAG = VideoItemDetailActivity.class.toString();
private VideoItemDetailFragment fragment;
@@ -86,39 +96,31 @@ public class VideoItemDetailActivity extends AppCompatActivity {
private void handleIntent(Intent intent) {
Bundle arguments = new Bundle();
- // this means the video was called though another app
+ boolean autoplay = false;
if (intent.getData() != null) {
+ // this means the video was called though another app
videoUrl = intent.getData().toString();
- StreamingService[] serviceList = NewPipe.getServices();
- //StreamExtractor videoExtractor = null;
- for (int i = 0; i < serviceList.length; i++) {
- if (serviceList[i].getUrlIdHandlerInstance().acceptUrl(videoUrl)) {
- arguments.putInt(VideoItemDetailFragment.STREAMING_SERVICE, i);
- currentStreamingService = i;
- //videoExtractor = ServiceList.getService(i).getExtractorInstance();
- break;
- }
- }
+ currentStreamingService = getServiceIdByUrl(videoUrl);
if(currentStreamingService == -1) {
Toast.makeText(this, R.string.url_not_supported_toast, Toast.LENGTH_LONG)
.show();
}
- //arguments.putString(VideoItemDetailFragment.VIDEO_URL,
- // videoExtractor.getUrl(videoExtractor.getId(videoUrl)));//cleans URL
- arguments.putString(VideoItemDetailFragment.VIDEO_URL, videoUrl);
-
- arguments.putBoolean(VideoItemDetailFragment.AUTO_PLAY,
- PreferenceManager.getDefaultSharedPreferences(this)
- .getBoolean(getString(R.string.autoplay_through_intent_key), false));
+ autoplay = PreferenceManager.getDefaultSharedPreferences(this)
+ .getBoolean(getString(R.string.autoplay_through_intent_key), false);
+ } else if(intent.getStringExtra(Intent.EXTRA_TEXT) != null) {
+ //this means that vidoe was called through share menu
+ String extraText = intent.getStringExtra(Intent.EXTRA_TEXT);
+ videoUrl = getUris(extraText)[0];
+ currentStreamingService = getServiceIdByUrl(videoUrl);
} else {
+ //this is if the video was called through another NewPipe activity
videoUrl = intent.getStringExtra(VideoItemDetailFragment.VIDEO_URL);
currentStreamingService = intent.getIntExtra(VideoItemDetailFragment.STREAMING_SERVICE, -1);
- arguments.putString(VideoItemDetailFragment.VIDEO_URL, videoUrl);
- arguments.putInt(VideoItemDetailFragment.STREAMING_SERVICE, currentStreamingService);
- arguments.putBoolean(VideoItemDetailFragment.AUTO_PLAY, false);
}
+ arguments.putBoolean(VideoItemDetailFragment.AUTO_PLAY, autoplay);
+ arguments.putString(VideoItemDetailFragment.VIDEO_URL, videoUrl);
+ arguments.putInt(VideoItemDetailFragment.STREAMING_SERVICE, currentStreamingService);
addFragment(arguments);
-
}
private void addFragment(final Bundle arguments) {
@@ -171,4 +173,70 @@ public class VideoItemDetailActivity extends AppCompatActivity {
fragment.onCreateOptionsMenu(menu, getMenuInflater());
return true;
}
+
+
+ /**
+ * 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
+ */
+ private String[] getUris(final String sharedText) {
+ final Collection 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()]);
+ }
+
+ private static String removeHeadingGibberish(final String input) {
+ 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());
+ }
+
+ private static String trim(final String input) {
+ 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;
+ }
+ }
+
+ private int getServiceIdByUrl(String url) {
+ StreamingService[] serviceList = NewPipe.getServices();
+ int service = -1;
+ for (int i = 0; i < serviceList.length; i++) {
+ if (serviceList[i].getUrlIdHandlerInstance().acceptUrl(videoUrl)) {
+ service = i;
+ //videoExtractor = ServiceList.getService(i).getExtractorInstance();
+ break;
+ }
+ }
+ return service;
+ }
}
diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java
index 5669a3308..2d2159768 100644
--- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java
+++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java
@@ -32,6 +32,7 @@ import java.util.ArrayList;
import java.util.List;
import us.shandian.giga.get.DownloadManager;
+import us.shandian.giga.get.DownloadMission;
import us.shandian.giga.service.DownloadManagerService;
@@ -227,7 +228,11 @@ public class DownloadDialog extends DialogFragment {
fName + arguments.getString(FILE_SUFFIX_AUDIO),
audioButton.isChecked(),
threads.getProgress() + 1);
- mBinder.onMissionAdded(mManager.getMission(res));
+ DownloadMission mission = mManager.getMission(res);
+ mBinder.onMissionAdded(mission);
+ // add download listener to allow media scan notification
+ DownloadListener listener = new DownloadListener(getContext(), mission);
+ mission.addListener(listener);
}
if(videoButton.isChecked()){
@@ -236,7 +241,11 @@ public class DownloadDialog extends DialogFragment {
fName + arguments.getString(FILE_SUFFIX_VIDEO),
audioButton.isChecked(),
threads.getProgress() + 1);
- mBinder.onMissionAdded(mManager.getMission(res));
+ DownloadMission mission = mManager.getMission(res);
+ mBinder.onMissionAdded(mission);
+ // add download listener to allow media scan notification
+ DownloadListener listener = new DownloadListener(getContext(), mission);
+ mission.addListener(listener);
}
getDialog().dismiss();
diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadListener.java b/app/src/main/java/org/schabi/newpipe/download/DownloadListener.java
new file mode 100644
index 000000000..eab30fddd
--- /dev/null
+++ b/app/src/main/java/org/schabi/newpipe/download/DownloadListener.java
@@ -0,0 +1,62 @@
+package org.schabi.newpipe.download;
+
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+
+import us.shandian.giga.get.DownloadMission;
+import us.shandian.giga.get.DownloadMission.MissionListener;
+
+/**
+ * Created by erwin on 06.11.16.
+ *
+ * Copyright (C) Christian Schabesberger 2016
+ * DownloadListener.java is part of NewPipe.
+ *
+ * NewPipe 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.
+ *
+ * NewPipe 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 NewPipe. If not, see .
+ */
+
+class DownloadListener implements MissionListener
+{
+ DownloadMission mMission;
+ Context mContext;
+
+ public DownloadListener(Context context, DownloadMission mission)
+ {
+ super();
+ mMission = mission;
+ mContext = context;
+ }
+
+ @Override
+ public void onProgressUpdate(long done, long total)
+ {
+ // do nothing special ...
+ }
+
+ @Override
+ public void onFinish()
+ {
+ // notify media scanner on downloaded media file ...
+ mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
+ Uri.parse( "file://" + mMission.location
+ + "/" + mMission.name)));
+ }
+
+ @Override
+ public void onError(int errCode)
+ {
+ // do nothing special ...
+ }
+}
diff --git a/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java b/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java
index 85151efb3..8ebb9b155 100644
--- a/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java
+++ b/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java
@@ -474,7 +474,7 @@ public class ErrorActivity extends AppCompatActivity {
String ipRange = "none";
try {
Downloader dl = Downloader.getInstance();
- String ip = dl.download("https://ifcfg.me/ip");
+ String ip = dl.download("https://ipv4.icanhazip.com");
ipRange = Parser.matchGroup1("([0-9]*\\.[0-9]*\\.)[0-9]*\\.[0-9]*", ip)
+ "0.0";