Add metadata when sharing video

This commit is contained in:
Thomas 2020-04-05 12:40:43 +02:00
parent 71a026425c
commit cce5ddaa8d
11 changed files with 172 additions and 39 deletions

View File

@ -4,6 +4,9 @@
package="app.fedilab.nitterizeme">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
@ -185,6 +188,7 @@
<activity android:name="app.fedilab.nitterizeme.WebviewPlayerActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@style/AppTheme"/>
</application>

View File

@ -554,19 +554,13 @@ public class MainActivity extends AppCompatActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_about) {
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);

View File

@ -22,6 +22,7 @@ import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.FrameLayout;
import androidx.appcompat.app.AppCompatActivity;
/**

View File

@ -427,21 +427,21 @@ public class TransformActivity extends Activity {
boolean embedded_player = sharedpreferences.getBoolean(SET_EMBEDDED_PLAYER, false);
if (Arrays.asList(invidious_instances).contains(Objects.requireNonNull(i.getData()).getHost()) && embedded_player) {
if( !i.getData().toString().contains("videoplayback")){
if (!i.getData().toString().contains("videoplayback")) {
Intent intentPlayer = new Intent(TransformActivity.this, WebviewPlayerActivity.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
intentPlayer.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intentPlayer.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
}
intentPlayer.putExtra("url", i.getData().toString());
startActivity(intentPlayer);
}else{
} else {
Intent intentStreamingUrl = new Intent(Utils.RECEIVE_STREAMING_URL);
Bundle b = new Bundle();
b.putString("streaming_url", i.getData().toString());
intentStreamingUrl.putExtras(b);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentStreamingUrl);
}
}else if (targetIntents.size() > 0) {
} else 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);

View File

@ -15,23 +15,32 @@ package app.fedilab.nitterizeme;
* see <http://www.gnu.org/licenses>. */
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
import static android.content.Context.DOWNLOAD_SERVICE;
import static app.fedilab.nitterizeme.MainActivity.shortener_domains;
class Utils {
public static final String RECEIVE_STREAMING_URL = "receive_streaming_url";
static final String RECEIVE_STREAMING_URL = "receive_streaming_url";
private static final String[] UTM_PARAMS = {
"utm_\\w+",
@ -154,4 +163,34 @@ class Utils {
}
return url;
}
/**
* Manage downloads with URLs
*
* @param context Context
* @param url String download url
*/
static void manageDownloadsNoPopup(final Context context, final String url) {
final DownloadManager.Request request;
try {
request = new DownloadManager.Request(Uri.parse(url.trim()));
} catch (Exception e) {
return;
}
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.ENGLISH);
Date now = new Date();
final String fileName = "NitterizeMe_" + formatter.format(now) + ".mp4";
request.allowScanningByMediaScanner();
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager dm = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
assert dm != null;
dm.enqueue(request);
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}

View File

@ -13,13 +13,20 @@ 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.Manifest;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -29,32 +36,40 @@ import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
public class WebviewPlayerActivity extends AppCompatActivity {
private static final int EXTERNAL_STORAGE_REQUEST_CODE = 84;
private String videoUrl;
private WebView webView;
private RelativeLayout loader;
private BroadcastReceiver receive_data;
private FrameLayout webview_container;
private String streaming_url;
private String initialUrl;
@SuppressLint("SetJavaScriptEnabled")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = null;
initialUrl = null;
Bundle b = getIntent().getExtras();
if (b != null) {
url = b.getString("url", null);
initialUrl = b.getString("url", null);
}
if( url == null){
if (initialUrl == null) {
finish();
}
setContentView(R.layout.activity_webview_player);
@ -65,6 +80,7 @@ public class WebviewPlayerActivity extends AppCompatActivity {
final ViewGroup videoLayout = findViewById(R.id.videoLayout);
webView = findViewById(R.id.webview);
webView.setBackgroundColor(Color.TRANSPARENT);
loader = findViewById(R.id.loader);
webview_container = findViewById(R.id.webview_container);
webView.getSettings().setJavaScriptEnabled(true);
@ -93,8 +109,8 @@ public class WebviewPlayerActivity extends AppCompatActivity {
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
assert b != null;
String streaming_url = b.getString("streaming_url", null);
if (streaming_url != null ) {
streaming_url = b.getString("streaming_url", null);
if (streaming_url != null) {
webView.stopLoading();
webView.loadUrl(streaming_url);
loader.setVisibility(View.GONE);
@ -105,16 +121,15 @@ public class WebviewPlayerActivity extends AppCompatActivity {
webView.setWebChromeClient(playerChromeClient);
LocalBroadcastManager.getInstance(WebviewPlayerActivity.this).registerReceiver(receive_data, new IntentFilter(Utils.RECEIVE_STREAMING_URL));
String finalUrl = url;
AsyncTask.execute(() -> {
try {
Document document = Jsoup
.connect(finalUrl).ignoreContentType(true).get();
.connect(initialUrl).ignoreContentType(true).get();
Element video = document.select("video").first();
if( video != null ){
if (video != null) {
Element source = video.select("source").first();
if( source != null ) {
if (source != null) {
videoUrl = source.absUrl("src");
runOnUiThread(() -> webView.loadUrl(videoUrl));
}
@ -127,37 +142,88 @@ public class WebviewPlayerActivity extends AppCompatActivity {
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_player, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
int id = item.getItemId();
if (id == R.id.action_share && initialUrl != null) {
AsyncTask.execute(() -> {
try {
Document document = Jsoup
.connect(initialUrl).ignoreContentType(true).get();
Element metaTitle = document.select("meta[property=\"og:title\"]").first();
String title = metaTitle.attr("content");
Element metaMedia = document.select("meta[property=\"og:video:url\"]").first();
String media = metaMedia.attr("content");
Element metaDescription = document.select("meta[property=\"og:description\"]").first();
String description = metaDescription.attr("content");
runOnUiThread(() -> {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, title);
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(media));
sendIntent.putExtra(Intent.EXTRA_TEXT, description + "\n\n" + initialUrl);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getString(R.string.share_with)));
});
} catch (IOException e) {
e.printStackTrace();
}
});
return true;
} else if (id == R.id.action_download && streaming_url != null) {
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(WebviewPlayerActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(WebviewPlayerActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(WebviewPlayerActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_REQUEST_CODE);
} else {
Utils.manageDownloadsNoPopup(WebviewPlayerActivity.this, streaming_url);
}
} else {
Utils.manageDownloadsNoPopup(WebviewPlayerActivity.this, streaming_url);
}
return true;
} else if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPause(){
public void onPause() {
super.onPause();
if( webView != null ){
if (webView != null) {
webView.onPause();
}
}
@Override
public void onResume(){
public void onResume() {
super.onResume();
if( webView != null ){
if (webView != null) {
webView.onResume();
}
}
@Override
public void onDestroy(){
public void onDestroy() {
super.onDestroy();
if (receive_data != null)
if (receive_data != null) {
LocalBroadcastManager.getInstance(WebviewPlayerActivity.this).unregisterReceiver(receive_data);
}
if (webView != null) {
webView.stopLoading();
webView.destroy();
}
}
}

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M19,9h-4V3H9v6H5l7,7 7,-7zM5,18v2h14v-2H5z"/>
</vector>

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -2.92,-2.92z"/>
</vector>

View File

@ -14,26 +14,23 @@
* You should have received a copy of the GNU General Public License along with NitterizeMe; if not,
* see <http://www.gnu.org/licenses>. */
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
tools:context=".WebviewPlayerActivity">
<FrameLayout
android:id="@+id/webview_container"
android:background="@android:color/black"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
android:visibility="gone">
<WebView
android:background="@android:color/black"
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
android:layout_height="match_parent" />
</FrameLayout>
<!-- View where the video will be shown when video goes fullscreen -->
<RelativeLayout
@ -41,13 +38,15 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/loader"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>

View File

@ -0,0 +1,17 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="app.fedilab.nitterizeme.WebviewPlayerActivity">
<item
android:id="@+id/action_share"
android:icon="@drawable/ic_share"
android:orderInCategory="1"
android:title="@string/share"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_download"
android:icon="@drawable/ic_download"
android:orderInCategory="2"
android:title="@string/download"
app:showAsAction="ifRoom" />
</menu>

View File

@ -53,4 +53,7 @@
<string name="about_instances">The list of instances comes from\n\n <a href="%1$s">%2$s</a>\n\nYou can contribute to this list by adding or removing instances.</string>
<string name="about_instances_title">About instances</string>
<string name="use_built_in_player">Use the built-in player</string>
<string name="share">Share</string>
<string name="download">Download</string>
<string name="share_with">Share with</string>
</resources>