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

81 lines
2.9 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 com.blankie.unshortify.R;
public class ShareActivity extends Activity {
public static final String TAG = "unshortify";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
final Intent intent = getIntent();
final String urlString = intent.getStringExtra(Intent.EXTRA_TEXT);
if (urlString == null || urlString == "") {
Toast.makeText(getApplicationContext(), R.string.empty_url,
Toast.LENGTH_SHORT).show();
return;
}
final Uri urlParsed = Uri.parse(urlString);
if (urlParsed.getScheme() != "http" && urlParsed.getScheme() != "https" &&
urlParsed.getScheme() != "") {
Toast.makeText(getApplicationContext(), R.string.unknown_scheme,
Toast.LENGTH_SHORT).show();
return;
}
if (urlParsed.getHost() == null || (urlParsed.getHost() != "youtube.com" &&
urlParsed.getHost().endsWith(".youtube.com")) {
Toast.makeText(getApplicationContext(), R.string.non_youtube_url,
Toast.LENGTH_SHORT).show();
return;
}
if (urlParsed.getPath() == null) {
Toast.makeText(getApplicationContext(), R.string.null_path,
Toast.LENGTH_SHORT).show();
return;
}
if (!urlParsed.getPath().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 intent = new Intent(Intent.ACTION_VIEW, unshortifiedUrl);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.no_activity_found,
Toast.LENGTH_SHORT).show();
}
} finally {
finish();
}
}
}