diff --git a/res/menu/article.xml b/res/menu/article.xml
index b75d7e0..15e9801 100644
--- a/res/menu/article.xml
+++ b/res/menu/article.xml
@@ -40,4 +40,10 @@
android:showAsAction="never"
/>
+
+
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 83b24b1..85ebc7c 100755
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -35,6 +35,7 @@
Style...
Select style
Default
+ Share link
Settings
Always
When on Wi-Fi
diff --git a/src/itkach/aard2/ArticleFragment.java b/src/itkach/aard2/ArticleFragment.java
index 751e053..d26b80f 100644
--- a/src/itkach/aard2/ArticleFragment.java
+++ b/src/itkach/aard2/ArticleFragment.java
@@ -4,7 +4,9 @@
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
+import android.content.Intent;
import android.graphics.drawable.Drawable;
+import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
@@ -20,6 +22,16 @@
import android.widget.ProgressBar;
import android.widget.TextView;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
public class ArticleFragment extends Fragment {
@@ -28,10 +40,12 @@ public class ArticleFragment extends Fragment {
private ArticleWebView view;
private MenuItem miBookmark;
private MenuItem miFullscreen;
+ private MenuItem miShare;
private Drawable icBookmark;
private Drawable icBookmarkO;
private Drawable icFullscreen;
private String url;
+ private String originalUrl = null;
@Override
@@ -54,10 +68,12 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.article, menu);
miBookmark = menu.findItem(R.id.action_bookmark_article);
miFullscreen = menu.findItem(R.id.action_fullscreen);
+ miShare = menu.findItem(R.id.action_share_link);
if (Build.VERSION.SDK_INT < 19) {
miFullscreen.setVisible(false);
miFullscreen.setEnabled(false);
}
+ this.retrieveOriginalUrl();
}
private void displayBookmarked(boolean value) {
@@ -129,6 +145,17 @@ public void onClick(DialogInterface dialog, int which) {
dialog.show();
return true;
}
+ if (itemId == R.id.action_share_link) {
+ if (this.originalUrl == null) {
+ return false;
+ }
+ Intent share = new Intent(Intent.ACTION_SEND);
+ share.setType("text/plain");
+ share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
+ share.putExtra(Intent.EXTRA_TEXT, this.originalUrl);
+ getContext().startActivity(Intent.createChooser(share, getString(R.string.action_share_link)));
+ return true;
+ }
return super.onOptionsItemSelected(item);
}
@@ -218,6 +245,62 @@ public ArticleWebView getWebView() {
return view;
}
+ void enableShareButton() {
+ if (this.miShare != null) {
+ this.miShare.setEnabled(true);
+ }
+ }
+
+ void activateShareButton(final String url) {
+ this.originalUrl = url;
+ this.getActivity().runOnUiThread(new FragmentRunnable(this) {
+ @Override
+ public void run() {
+ if (url != null) {
+ ((ArticleFragment)this.fragment).enableShareButton();
+ }
+ }
+ });
+ }
+
+ private void retrieveOriginalUrl() {
+ final String url = this.url;
+ AsyncTask.execute(new FragmentRunnable(this) {
+ @Override
+ public void run() {
+ try {
+ URL endpoint = new URL(url);
+ HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection();
+ if (connection.getResponseCode() != 200) {
+ return;
+ }
+ InputStream responseBody = connection.getInputStream();
+ StringBuilder htmlBuilder = new StringBuilder();
+ Reader reader = new BufferedReader(new InputStreamReader(responseBody, "UTF-8"));
+ int character = 0;
+ while ((character = reader.read()) != -1) {
+ htmlBuilder.append((char) character);
+ }
+ String html = htmlBuilder.toString();
+ String articleUrl = null;
+ Pattern pattern = Pattern.compile("]*?\\s+)?href=\"([^\"]*)\"(>|([\\s]+([^>]+)))");
+ Matcher matcher = pattern.matcher(html);
+ while (matcher.find()) {
+ if (matcher.group().contains("id=\"view-online-link\"")) {
+ articleUrl = matcher.group(1);
+ break;
+ }
+ }
+ ((ArticleFragment)this.fragment).activateShareButton(articleUrl);
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ }
+
@Override
public void onDestroy() {
if (view != null) {
@@ -226,7 +309,7 @@ public void onDestroy() {
}
miFullscreen = null;
miBookmark = null;
+ miShare = null;
super.onDestroy();
}
-
}
\ No newline at end of file
diff --git a/src/itkach/aard2/ArticleWebView.java b/src/itkach/aard2/ArticleWebView.java
index c47ad69..ee20fa0 100644
--- a/src/itkach/aard2/ArticleWebView.java
+++ b/src/itkach/aard2/ArticleWebView.java
@@ -104,7 +104,7 @@ public ArticleWebView(Context context) {
this(context, null);
}
- public ArticleWebView(Context context, AttributeSet attrs) {
+ public ArticleWebView(final Context context, AttributeSet attrs) {
super(context, attrs);
connectivityManager = (ConnectivityManager) context
@@ -267,7 +267,7 @@ public boolean onLongClick(View view) {
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
share.putExtra(Intent.EXTRA_TEXT, url);
- getContext().startActivity(Intent.createChooser(share, "Share Link"));
+ getContext().startActivity(Intent.createChooser(share, context.getString(R.string.action_share_link)));
return true;
}
}
diff --git a/src/itkach/aard2/FragmentRunnable.java b/src/itkach/aard2/FragmentRunnable.java
new file mode 100644
index 0000000..fb35a59
--- /dev/null
+++ b/src/itkach/aard2/FragmentRunnable.java
@@ -0,0 +1,11 @@
+package itkach.aard2;
+
+import android.support.v4.app.Fragment;
+
+public abstract class FragmentRunnable implements Runnable {
+ protected Fragment fragment;
+
+ FragmentRunnable(Fragment fragment) {
+ this.fragment = fragment;
+ }
+}