diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..67c9793
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+ShareWithTitle/build/apk/
+ShareWithTitle/build/classes/
+ShareWithTitle/build
diff --git a/ShareWithTitle/build/res/all/debug/layout/activity_main.xml b/ShareWithTitle/build/res/all/debug/layout/activity_main.xml
index a0860a2..a8b7cd2 100644
--- a/ShareWithTitle/build/res/all/debug/layout/activity_main.xml
+++ b/ShareWithTitle/build/res/all/debug/layout/activity_main.xml
@@ -12,6 +12,18 @@
android:id="@+id/greet_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:text="@string/hello_world" />
+ android:text="@string/hello_world"
+ android:layout_marginTop="69dp"
+ android:layout_alignParentTop="true"
+ android:layout_alignParentLeft="true"/>
+
+
diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java
index a6509d7..2b2e436 100644
--- a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java
+++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/MainActivity.java
@@ -6,6 +6,8 @@ import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
+import android.view.View;
+import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
@@ -19,24 +21,37 @@ public class MainActivity extends Activity {
Intent intent = getIntent();
String action = intent.getAction();
- TextView textView = (TextView) findViewById(R.id.greet_text);
+ final TextView textView = (TextView) findViewById(R.id.greet_text);
textView.setText("");
+ final View progressBar = (ProgressBar)findViewById(R.id.progressBar);
+
if(action.equals(Intent.ACTION_SEND)) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
Log.d("Share With Title", "Received shared text: " + sharedText);
+ new TitleRetriever(sharedText).retrieve(new TitleViewUpdater() {
+ @Override
+ public void update(String title) {
+ textView.setText(title);
+ progressBar.setVisibility(View.VISIBLE);
+ }
- ShareableOutput shareableOutput = new ShareableOutput(sharedText);
- textView.setText(shareableOutput.output());
- if (shareableOutput.isValidUrl()) {
- return;
- }
- showToast("Could not retrieve title. Not a valid url.");
- } else if (action.equals(Intent.ACTION_MAIN)) {
- Log.d("Share With Title", "Started from main");
+ @Override
+ public void showProgress() {
+ progressBar.animate();
+ }
- textView.setText("Nothing to see here!");
+ @Override
+ public void finish() {
+ progressBar.setVisibility(View.GONE);
+ }
+
+ @Override
+ public void showError(String errorMessage) {
+ showToast(errorMessage);
+ }
+ });
}
}
diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/PageDownloader.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/PageDownloader.java
new file mode 100644
index 0000000..09abaca
--- /dev/null
+++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/PageDownloader.java
@@ -0,0 +1,57 @@
+package in.sdqali.sharewithtitle;
+
+import android.util.Log;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+public class PageDownloader {
+ public PageDownloader() {
+ }
+
+ String downloadUrl(String urlText) {
+ try {
+ URL url = null;
+ url = new URL(urlText);
+ HttpURLConnection con = null;
+ con = (HttpURLConnection) url
+ .openConnection();
+ String content = readStream(con.getInputStream());
+ Log.d("Share With Title", "content: " + content);
+ return content;
+
+ } catch (Exception e) {
+ return urlText;
+ }
+ }
+
+
+ private String readStream(InputStream in) {
+ String output = "";
+ BufferedReader reader = null;
+ try {
+ reader = new BufferedReader(new InputStreamReader(in));
+ String line = "";
+ while ((line = reader.readLine()) != null) {
+ output += line;
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ return output;
+ }
+
+
+}
\ No newline at end of file
diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleRetriever.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleRetriever.java
new file mode 100644
index 0000000..282d237
--- /dev/null
+++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleRetriever.java
@@ -0,0 +1,64 @@
+package in.sdqali.sharewithtitle;
+
+import android.content.Context;
+import android.os.AsyncTask;
+import android.util.Log;
+import android.webkit.URLUtil;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Created by sdqali on 7/20/13.
+ */
+public class TitleRetriever {
+ private String urlText;
+
+ public TitleRetriever(String url) {
+ this.urlText = url;
+ }
+
+ public void retrieve(TitleViewUpdater viewUpdater) {
+ if(isValidUrl(urlText)) {
+ new DownloadTaskNew(viewUpdater).execute(urlText);
+ } else {
+ viewUpdater.showError("Not a valid url. Could not load title!");
+ }
+ }
+
+ private boolean isValidUrl(String urlText) {
+ return (URLUtil.isHttpsUrl(urlText) || URLUtil.isHttpUrl(urlText));
+ }
+
+ private class DownloadTaskNew extends AsyncTask {
+ private TitleViewUpdater viewUpdater;
+ private final PageDownloader pageDownloader = new PageDownloader();
+
+ public DownloadTaskNew(TitleViewUpdater viewUpdater) {
+ this.viewUpdater = viewUpdater;
+ }
+
+ @Override
+ protected String doInBackground(String... urls) {
+ viewUpdater.showProgress();
+ return pageDownloader.downloadUrl(urls[0]);
+ }
+
+ @Override
+ protected void onPostExecute(String rawHtml) {
+ Log.d("Share With Title", "On post: " + rawHtml);
+ Pattern p = Pattern.compile(".*?(.*?).*?", Pattern.DOTALL);
+ Matcher m = p.matcher(rawHtml);
+ String title;
+ viewUpdater.update(urlText);
+ while (m.find()) {
+ title = m.group(1);
+ Log.d("Share With Title", "Title: " + title);
+ viewUpdater.update(title);
+ }
+ viewUpdater.finish();
+ }
+ }
+}
diff --git a/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleViewUpdater.java b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleViewUpdater.java
new file mode 100644
index 0000000..bb94dc3
--- /dev/null
+++ b/ShareWithTitle/src/main/java/in/sdqali/sharewithtitle/TitleViewUpdater.java
@@ -0,0 +1,11 @@
+package in.sdqali.sharewithtitle;
+
+/**
+ * Created by sdqali on 7/20/13.
+ */
+public interface TitleViewUpdater {
+ public void update(String title);
+ public void showProgress();
+ public void finish();
+ public void showError(String errorMessage);
+}
diff --git a/ShareWithTitle/src/main/res/layout/activity_main.xml b/ShareWithTitle/src/main/res/layout/activity_main.xml
index a0860a2..a8b7cd2 100644
--- a/ShareWithTitle/src/main/res/layout/activity_main.xml
+++ b/ShareWithTitle/src/main/res/layout/activity_main.xml
@@ -12,6 +12,18 @@
android:id="@+id/greet_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:text="@string/hello_world" />
+ android:text="@string/hello_world"
+ android:layout_marginTop="69dp"
+ android:layout_alignParentTop="true"
+ android:layout_alignParentLeft="true"/>
+
+