62 lines
1.8 KiB
Java
62 lines
1.8 KiB
Java
package net.crystalyx.sharewithtitle;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.widget.Toast;
|
|
|
|
import java.net.MalformedURLException;
|
|
import java.net.URI;
|
|
import java.net.URL;
|
|
|
|
public class MainActivity extends Activity {
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
Intent intent = getIntent();
|
|
String type = intent.getType();
|
|
|
|
if (Intent.ACTION_SEND.equals(intent.getAction()) && "text/plain".equals(type)) {
|
|
handleUrl(intent.getStringExtra(Intent.EXTRA_TEXT));
|
|
}
|
|
|
|
finish();
|
|
}
|
|
|
|
private void handleUrl(String textUrl) {
|
|
URL originalUrl;
|
|
|
|
try {
|
|
originalUrl = new URI(textUrl).toURL();
|
|
} catch (Exception e) {
|
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
return;
|
|
}
|
|
|
|
HttpThreadHandler handler = new HttpThreadHandler(originalUrl);
|
|
Thread job = new Thread(handler);
|
|
|
|
job.start();
|
|
try {
|
|
job.join();
|
|
} catch (InterruptedException e) {
|
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
}
|
|
|
|
String title = handler.getTitle();
|
|
String errorMessage = handler.getErrorMessage();
|
|
|
|
if (title != null) {
|
|
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
|
|
sharingIntent.setType("text/plain");
|
|
sharingIntent.putExtra(Intent.EXTRA_TEXT, title + "\n\n" + originalUrl);
|
|
startActivity(Intent.createChooser(sharingIntent, "Share via"));
|
|
} else if (errorMessage != null) {
|
|
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
|
|
}
|