Sane state.

This commit is contained in:
Sadique Ali 2013-07-20 14:56:21 -07:00
parent 3a63e68d28
commit 31082ecf08
7 changed files with 186 additions and 12 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
ShareWithTitle/build/apk/
ShareWithTitle/build/classes/
ShareWithTitle/build

View File

@ -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"/>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:id="@+id/progressBar"
android:layout_below="@+id/progressBar"
android:layout_toRightOf="@+id/progressBar"/>
</RelativeLayout>

View File

@ -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);
}
});
}
}

View File

@ -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;
}
}

View File

@ -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<String, Void, String> {
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("<head>.*?<title>(.*?)</title>.*?</head>", 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();
}
}
}

View File

@ -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);
}

View File

@ -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"/>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:id="@+id/progressBar"
android:layout_below="@+id/progressBar"
android:layout_toRightOf="@+id/progressBar"/>
</RelativeLayout>