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

99 lines
3.4 KiB
Java

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;
import android.util.Log;
import com.blankie.unshortify.R;
public class ShareActivity extends Activity {
public static final String TAG = "unshortify";
private void logVariable(final String name, final String value) {
String msg = name + " = ";
if (value != null) {
msg += "[" + value + "]";
} else {
msg += "null";
}
Log.d(TAG, msg);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
final Intent intent = getIntent();
final String urlString = intent.getStringExtra(Intent.EXTRA_TEXT);
logVariable("urlString", urlString);
if (urlString == null || urlString == "") {
Toast.makeText(getApplicationContext(), R.string.empty_url,
Toast.LENGTH_SHORT).show();
return;
}
final Uri urlParsed = Uri.parse(urlString);
final String scheme = urlParsed.getScheme();
logVariable("scheme", scheme);
if (scheme != null && !scheme.equals("") && !scheme.equals("http") &&
!scheme.equals("https")) {
Toast.makeText(getApplicationContext(), R.string.unknown_scheme,
Toast.LENGTH_SHORT).show();
return;
}
final String host = urlParsed.getHost();
logVariable("host", host);
if (host == null || (!host.equals("youtube.com") &&
!host.endsWith(".youtube.com"))) {
Toast.makeText(getApplicationContext(), R.string.non_youtube_url,
Toast.LENGTH_SHORT).show();
return;
}
final String path = urlParsed.getPath();
logVariable("path", path);
if (path == null) {
Toast.makeText(getApplicationContext(), R.string.null_path,
Toast.LENGTH_SHORT).show();
return;
}
if (!path.startsWith("/shorts/")) {
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();
final Intent unshortifiedIntent = new Intent(Intent.ACTION_VIEW, unshortifiedUrl);
try {
startActivity(unshortifiedIntent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.no_activity_found,
Toast.LENGTH_SHORT).show();
}
} finally {
finish();
}
}
}