Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added link sharing functionality (#103) #127

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions res/menu/article.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@
android:showAsAction="never"
/>

<item android:id="@+id/action_share_link"
android:title="@string/action_share_link"
android:enabled="false"
android:showAsAction="never"
/>

</menu>
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<string name="action_select_style">Style...</string>
<string name="select_style">Select style</string>
<string name="default_style_title">Default</string>
<string name="action_share_link">Share link</string>
<string name="subtitle_settings">Settings</string>
<string name="setting_remote_content_always">Always</string>
<string name="setting_remote_content_wifi">When on Wi-Fi</string>
Expand Down
85 changes: 84 additions & 1 deletion src/itkach/aard2/ArticleFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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("<a\\s+(?:[^>]*?\\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) {
Expand All @@ -226,7 +309,7 @@ public void onDestroy() {
}
miFullscreen = null;
miBookmark = null;
miShare = null;
super.onDestroy();
}

}
4 changes: 2 additions & 2 deletions src/itkach/aard2/ArticleWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/itkach/aard2/FragmentRunnable.java
Original file line number Diff line number Diff line change
@@ -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;
}
}