Permalink
Browse files

WIP ImageViewer

  • Loading branch information...
1 parent f5fc2dc commit 6afa0d66be9a61e3f586abd78abca0f067be6c6e @skarcha committed Feb 26, 2011
View
@@ -49,6 +49,16 @@
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
+ <activity android:name="NBImageView" android:theme="@android:style/Theme.Black">
+ <intent-filter>
+ <action android:name="android.intent.action.VIEW"></action>
+ <category android:name="android.intent.category.DEFAULT"></category>
+ <category android:name="android.intent.category.BROWSABLE"></category>
+ <data android:host="yfrog.com" android:scheme="http"></data>
+ <data android:host="twitpic.com" android:scheme="http"></data>
+ <data android:host="picplz.com" android:scheme="http"></data>
+ </intent-filter>
+ </activity>
</application>
</manifest>
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent" android:id="@+id/LinearLayout01">
+ <ImageView android:layout_width="wrap_content" android:id="@+id/imageView1" android:layout_height="wrap_content"></ImageView>
+</LinearLayout>
@@ -4,21 +4,14 @@
import java.net.URI;
import java.net.URISyntaxException;
-import javax.net.ssl.SSLException;
-
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
-import org.apache.http.conn.ssl.AbstractVerifier;
-import org.apache.http.conn.ssl.SSLSocketFactory;
-import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.BasicResponseHandler;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
@@ -31,15 +24,14 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
-import android.content.pm.PackageInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;
public class Main extends Activity {
- private String UserAgent = null;
+ private final String UserAgent = null;
private boolean prefShowRedirect;
@Override
@@ -50,8 +42,6 @@ public void onCreate(Bundle savedInstanceState) {
Intent i = getIntent();
String dataString = i.getDataString();
- UserAgent = "Mozilla/5.0 (compatible; NoBrowser for Android/" + getVersionName() + ")";
-
if (isShortener(dataString)) {
new getFinalUrlTask().execute(dataString);
} else {
@@ -137,9 +127,8 @@ public void onClick(DialogInterface dialog, int id) {
private String getTwitlongerContent (String url) throws IOException, IOException {
String responseBody = null;
- HttpClient httpclient = new DefaultHttpClient();
+ HttpClient httpclient = new Network().getTolerantClient();
try {
- httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, UserAgent);
HttpGet httpget = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
@@ -265,9 +254,8 @@ private String getFinalURL (String url) throws IOException, IOException {
}
//HttpClient httpclient = new DefaultHttpClient();
- HttpClient httpclient = getTolerantClient();
+ HttpClient httpclient = new Network().getTolerantClient();
try {
- httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, UserAgent);
HttpHead httphead = new HttpHead(url);
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpclient.execute(httphead, localContext);
@@ -290,77 +278,7 @@ private void getPrefs() {
prefShowRedirect = prefs.getBoolean("show_redirect", true);
}
- private String getVersionName() {
- PackageInfo pinfo;
-
- try {
- pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
- } catch (android.content.pm.PackageManager.NameNotFoundException e) {
- return null;
- }
-
- return pinfo.versionName;
- }
-
private void toast(String texto) {
Toast.makeText(this, texto, Toast.LENGTH_SHORT).show();
}
-
-/*
- * Black box for me at the moment.
- * Extracted from: http://stackoverflow.com/questions/3135679/android-httpclient-hostname-in-certificate-didnt-match-example-com-exa/3136980#3136980
- *
- * This is the ugly hack to this message:
- * javax.net.ssl.SSLException: hostname in certificate didn't match <market.android.com> != <*.google.com>
- *
- * Ugly hack start here...
- */
-
- class MyVerifier extends AbstractVerifier {
-
- private final X509HostnameVerifier delegate;
-
- public MyVerifier(final X509HostnameVerifier delegate) {
- this.delegate = delegate;
- }
-
- @Override
- public void verify(String host, String[] cns, String[] subjectAlts)
- throws SSLException {
-/*
- boolean ok = false;
- try {
- delegate.verify(host, cns, subjectAlts);
- } catch (SSLException e) {
- for (String cn : cns) {
- if (cn.startsWith("*.")) {
- try {
- delegate.verify(host, new String[] {
- cn.substring(2) }, subjectAlts);
- ok = true;
- } catch (Exception e1) { }
- }
- }
- if(!ok) throw e;
- }
-*/
- }
- }
-
- public DefaultHttpClient getTolerantClient() {
- DefaultHttpClient client = new DefaultHttpClient();
- SSLSocketFactory sslSocketFactory = (SSLSocketFactory) client
- .getConnectionManager().getSchemeRegistry().getScheme("https")
- .getSocketFactory();
- final X509HostnameVerifier delegate = sslSocketFactory
- .getHostnameVerifier();
- if (!(delegate instanceof MyVerifier)) {
- sslSocketFactory.setHostnameVerifier(new MyVerifier(delegate));
- }
- return client;
- }
-
-/*
- * Ugly hack ends here...
- */
}
@@ -0,0 +1,134 @@
+package com.skarcha.nobrowser;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URL;
+
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.BasicResponseHandler;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.app.Activity;
+import android.app.ProgressDialog;
+import android.content.Intent;
+import android.graphics.drawable.Drawable;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.widget.ImageView;
+
+public class NBImageView extends Activity {
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.nbimageview);
+
+ Intent i = getIntent();
+ String dataString = i.getDataString();
+
+ if (dataString.startsWith("http://yfrog.com/")) {
+ dataString += ":android";
+
+ } else if (dataString.startsWith("http://twitpic.com/")) {
+ dataString = "http://twitpic.com/show/full/" + URI.create(dataString).getPath();
+
+ } else if (dataString.startsWith("http://picplz.com/")) {
+ dataString = get_picplz_image_url(URI.create(dataString).getPath().substring(1));
+
+ } else {
+ finish();
+ }
+
+ new getImageTask().execute(dataString);
+ }
+
+ private void showImage (Drawable drawable) {
+ ImageView imgView =(ImageView)findViewById(R.id.imageView1);
+ imgView.setImageDrawable(drawable);
+ }
+
+ private String get_picplz_image_url(String id) {
+ String url = "http://api.picplz.com/api/v2/pic.json?shorturl_id=" + id + "&pic_formats=640r";
+ String responseBody = null;
+
+ HttpClient httpclient = new Network().getTolerantClient();
+
+ try {
+ HttpGet httpget = new HttpGet(url);
+
+ ResponseHandler<String> responseHandler = new BasicResponseHandler();
+ responseBody = httpclient.execute(httpget, responseHandler);
+ } catch (ClientProtocolException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } finally {
+ httpclient.getConnectionManager().shutdown();
+ }
+
+ if (responseBody == null) {
+ return null;
+ }
+
+ String image_url = null;
+ try {
+ JSONObject jsRespuesta = new JSONObject(responseBody);
+ String result = jsRespuesta.getString("result");
+ if (result.equals("ok")) {
+ image_url = jsRespuesta.getJSONObject("value").getJSONArray("pics").getJSONObject(0).getJSONObject("pic_files").getJSONObject("640r").getString("img_url");
+ }
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return image_url;
+ }
+
+ private class getImageTask extends AsyncTask<String, Void, Drawable> {
+ private final ProgressDialog dialog = new ProgressDialog(NBImageView.this);
+
+ @Override
+ protected void onPreExecute() {
+ this.dialog.setMessage(getString(R.string.please_wait));
+ this.dialog.show();
+ }
+
+ @Override
+ protected Drawable doInBackground(final String... args) {
+ Drawable drawable = null;
+ drawable = NBImageView.this.LoadImageFromWebOperations(args[0]);
+ return drawable;
+ }
+
+ @Override
+ protected void onPostExecute(final Drawable drawable) {
+ if (this.dialog.isShowing()) {
+ this.dialog.dismiss();
+ }
+
+ NBImageView.this.showImage(drawable);
+ }
+ }
+
+ private Drawable LoadImageFromWebOperations(String url) {
+ try {
+ InputStream is = (InputStream) new URL(url).getContent();
+ Drawable d = Drawable.createFromStream(is, "src name");
+ return d;
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+
+}
@@ -0,0 +1,89 @@
+package com.skarcha.nobrowser;
+
+import javax.net.ssl.SSLException;
+
+import org.apache.http.conn.ssl.AbstractVerifier;
+import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.conn.ssl.X509HostnameVerifier;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.params.CoreProtocolPNames;
+
+public class Network {
+ /*
+ * Black box for me at the moment.
+ * Extracted from: http://stackoverflow.com/questions/3135679/android-httpclient-hostname-in-certificate-didnt-match-example-com-exa/3136980#3136980
+ *
+ * This is the ugly hack to this message:
+ * javax.net.ssl.SSLException: hostname in certificate didn't match <market.android.com> != <*.google.com>
+ *
+ * Ugly hack start here...
+ */
+
+ class MyVerifier extends AbstractVerifier {
+
+ private final X509HostnameVerifier delegate;
+
+ public MyVerifier(final X509HostnameVerifier delegate) {
+ this.delegate = delegate;
+ }
+
+ @Override
+ public void verify(String host, String[] cns, String[] subjectAlts)
+ throws SSLException {
+/*
+ boolean ok = false;
+ try {
+ delegate.verify(host, cns, subjectAlts);
+ } catch (SSLException e) {
+ for (String cn : cns) {
+ if (cn.startsWith("*.")) {
+ try {
+ delegate.verify(host, new String[] {
+ cn.substring(2) }, subjectAlts);
+ ok = true;
+ } catch (Exception e1) { }
+ }
+ }
+ if(!ok) throw e;
+ }
+*/
+ }
+ }
+
+ public DefaultHttpClient getTolerantClient() {
+ //String UserAgent = "Mozilla/5.0 (compatible; NoBrowser for Android/" + getVersionName() + ")";
+ String UserAgent = "Mozilla/5.0 (compatible; NoBrowser for Android/1.2.0)";
+
+ DefaultHttpClient client = new DefaultHttpClient();
+ client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, UserAgent);
+
+ SSLSocketFactory sslSocketFactory = (SSLSocketFactory) client
+ .getConnectionManager().getSchemeRegistry().getScheme("https")
+ .getSocketFactory();
+ final X509HostnameVerifier delegate = sslSocketFactory
+ .getHostnameVerifier();
+ if (!(delegate instanceof MyVerifier)) {
+ sslSocketFactory.setHostnameVerifier(new MyVerifier(delegate));
+ }
+
+ return client;
+ }
+
+ /*
+ * Ugly hack ends here...
+ */
+
+/*
+ private String getVersionName() {
+ PackageInfo pinfo;
+
+ try {
+ pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
+ } catch (android.content.pm.PackageManager.NameNotFoundException e) {
+ return null;
+ }
+
+ return pinfo.versionName;
+ }
+*/
+}

0 comments on commit 6afa0d6

Please sign in to comment.