unshortify/app/src/main/java/com/blankie/unshortify/ShareActivity.java

99 lines
3.4 KiB
Java
Raw Normal View History

2022-07-11 03:41:46 +00:00
package com.blankie.unshortify;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Toast;
import android.content.Intent;
import android.content.ActivityNotFoundException;
import android.net.Uri;
2022-07-12 06:05:35 +00:00
import android.util.Log;
2022-07-11 03:41:46 +00:00
import com.blankie.unshortify.R;
public class ShareActivity extends Activity {
public static final String TAG = "unshortify";
2022-07-12 06:05:35 +00:00
private void logVariable(final String name, final String value) {
String msg = name + " = ";
if (value != null) {
msg += "[" + value + "]";
} else {
msg += "null";
}
Log.d(TAG, msg);
}
2022-07-11 03:41:46 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
final Intent intent = getIntent();
final String urlString = intent.getStringExtra(Intent.EXTRA_TEXT);
2022-07-12 06:05:35 +00:00
logVariable("urlString", urlString);
2022-07-11 03:41:46 +00:00
if (urlString == null || urlString == "") {
Toast.makeText(getApplicationContext(), R.string.empty_url,
Toast.LENGTH_SHORT).show();
return;
}
final Uri urlParsed = Uri.parse(urlString);
2022-07-12 07:28:40 +00:00
final String scheme = urlParsed.getScheme();
logVariable("scheme", scheme);
if (scheme != null && !scheme.equals("") && !scheme.equals("http") &&
!scheme.equals("https")) {
2022-07-11 03:41:46 +00:00
Toast.makeText(getApplicationContext(), R.string.unknown_scheme,
Toast.LENGTH_SHORT).show();
return;
}
2022-07-12 07:28:40 +00:00
final String host = urlParsed.getHost();
logVariable("host", host);
if (host == null || (!host.equals("youtube.com") &&
!host.endsWith(".youtube.com"))) {
2022-07-11 03:41:46 +00:00
Toast.makeText(getApplicationContext(), R.string.non_youtube_url,
Toast.LENGTH_SHORT).show();
return;
}
2022-07-12 07:28:40 +00:00
final String path = urlParsed.getPath();
logVariable("path", path);
if (path == null) {
2022-07-11 03:41:46 +00:00
Toast.makeText(getApplicationContext(), R.string.null_path,
Toast.LENGTH_SHORT).show();
return;
}
2022-07-12 07:28:40 +00:00
if (!path.startsWith("/shorts/")) {
2022-07-11 03:41:46 +00:00
Toast.makeText(getApplicationContext(), R.string.non_shorts_url,
Toast.LENGTH_SHORT).show();
return;
}
final String[] pathSegments = urlParsed.getPath().split("/");
if (pathSegments.length != 3) {
Toast.makeText(getApplicationContext(), R.string.non_shorts_url,
Toast.LENGTH_SHORT).show();
return;
}
Uri.Builder builder = new Uri.Builder();
final Uri unshortifiedUrl = builder.scheme("https")
.authority("www.youtube.com")
.path("/watch")
.appendQueryParameter("v", pathSegments[2])
.build();
2022-07-11 05:01:38 +00:00
final Intent unshortifiedIntent = new Intent(Intent.ACTION_VIEW, unshortifiedUrl);
2022-07-11 03:41:46 +00:00
try {
2022-07-12 07:38:50 +00:00
startActivity(unshortifiedIntent);
2022-07-11 03:41:46 +00:00
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.no_activity_found,
Toast.LENGTH_SHORT).show();
}
} finally {
finish();
}
}
}