Share intent adds site directly without opening browser, supports HTTP Auth too

This commit is contained in:
Travis Burtrum 2015-01-12 01:11:09 -05:00
parent 6f12cc94bc
commit 2828d610bf
8 changed files with 92 additions and 14 deletions

View File

@ -13,7 +13,7 @@ android {
}
buildTypes {
release {
runProguard false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
@ -21,4 +21,5 @@ android {
dependencies {
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'org.jsoup:jsoup:1.8.1'
}

View File

@ -20,17 +20,18 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Browser;
import android.util.Base64;
import android.util.Patterns;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.util.Log;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@ -47,7 +48,6 @@ import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
@ -82,8 +82,11 @@ public class Poche extends Activity {
static String apiUsername;
static String apiToken;
static String pocheUrl;
static String base64login;
String action;
private static final String TAG = "fr.gaulupeau.apps.InThePoche";
private static final String USER_AGENT = "Mozilla/5.0 Wallabag/1.7";
/**
* Called when the activity is first created.
@ -124,10 +127,6 @@ public class Poche extends Activity {
final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
// Start to build the poche URL
Uri.Builder pocheSaveUrl = Uri.parse(pocheUrl).buildUpon();
// Add the parameters from the call
pocheSaveUrl.appendQueryParameter("action", "add");
byte[] data = null;
try {
data = pageUrl.getBytes("UTF-8");
@ -135,10 +134,17 @@ public class Poche extends Activity {
// TODO Auto-generated catch block
e.printStackTrace();
}
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
final String base64 = Base64.encodeToString(data, Base64.DEFAULT);
Log.d(TAG, "mylogpoche: base64 : " + base64);
Log.d(TAG, "mylogpoche: pageurl : " + pageUrl);
/*
// Start to build the poche URL
final Uri.Builder pocheSaveUrl = Uri.parse(pocheUrl).buildUpon();
// Add the parameters from the call
pocheSaveUrl.appendQueryParameter("action", "add");
pocheSaveUrl.appendQueryParameter("url", base64);
System.out.println("base64 : " + base64);
System.out.println("pageurl : " + pageUrl);
// Load the constructed URL in the browser
Intent i = new Intent(Intent.ACTION_VIEW);
@ -148,6 +154,35 @@ public class Poche extends Activity {
// select which one they want to use
startActivity(i);
*/
// Create Request to server and get response
new Thread(new Runnable() {
// After call for background.start this run method call
public void run() {
try {
Connection.Response res = getJsoup()
.followRedirects(false)
.execute();
res = getJsoup()
.cookies(res.cookies())
.data("action", "add", "url", base64)
.execute();
showToast(getString(R.string.success));
} catch (org.jsoup.HttpStatusException e) {
showToast(getString(R.string.error) + " " + e.getStatusCode());
Log.d(TAG, "Error", e);
} catch (Throwable e) {
showToast(getString(R.string.error));
Log.d(TAG, "Error", e);
}
}
}).start();
// That is all this app needs to do, so call finish()
this.finish();
} else {
@ -211,6 +246,13 @@ public class Poche extends Activity {
}
}
private static Connection getJsoup() {
final Connection conn = Jsoup.connect(pocheUrl)
.userAgent(USER_AGENT)
.method(Connection.Method.GET).timeout(10 * 1000);
return base64login == null ? conn : conn.header("Authorization", "Basic " + base64login);
}
private void checkAndHandleAfterUpdate() {
SharedPreferences pref = getSharedPreferences(PREFS_NAME, 0);
@ -237,6 +279,15 @@ public class Poche extends Activity {
pocheUrl = settings.getString("pocheUrl", "http://");
apiUsername = settings.getString("APIUsername", "");
apiToken = settings.getString("APIToken", "");
base64login = null;
final String password = settings.getString("APIPassword", "");
if (!password.isEmpty() && !apiUsername.isEmpty()) {
final String login = apiUsername + ":" + password;
base64login = Base64.encodeToString(login.getBytes(), Base64.DEFAULT);
}
//Log.d(TAG, String.format("username: '%s', password: '%s', base64login: '%s'%n", apiUsername, password, base64login));
}
@Override
@ -336,7 +387,7 @@ public class Poche extends Activity {
// } while (ac.moveToNext());
// }
// ac.close();
//
//
// }

View File

@ -16,6 +16,7 @@ public class Settings extends BaseActionBarActivity {
Button btnDone;
EditText editPocheUrl;
EditText editAPIUsername;
EditText editAPIPassword;
EditText editAPIToken;
TextView textViewVersion;
@ -28,10 +29,13 @@ public class Settings extends BaseActionBarActivity {
String pocheUrl = settings.getString("pocheUrl", "http://");
String apiUsername = settings.getString("APIUsername", "");
String apiToken = settings.getString("APIToken", "");
String password = settings.getString("APIPassword", "");
editPocheUrl = (EditText) findViewById(R.id.pocheUrl);
editPocheUrl.setText(pocheUrl);
editAPIUsername = (EditText) findViewById(R.id.APIUsername);
editAPIUsername.setText(apiUsername);
editAPIPassword = (EditText) findViewById(R.id.APIPassword);
editAPIPassword.setText(password);
editAPIToken = (EditText) findViewById(R.id.APIToken);
editAPIToken.setText(apiToken);
btnDone = (Button) findViewById(R.id.btnDone);
@ -41,6 +45,7 @@ public class Settings extends BaseActionBarActivity {
SharedPreferences.Editor editor = settings.edit();
editor.putString("pocheUrl", editPocheUrl.getText().toString());
editor.putString("APIUsername", editAPIUsername.getText().toString());
editor.putString("APIPassword", editAPIPassword.getText().toString());
editor.putString("APIToken", editAPIToken.getText().toString());
editor.commit();
finish();

View File

@ -61,6 +61,23 @@
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/txtAPIPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:text="@string/txtAPIPassword"
android:textSize="18sp" />
<EditText
android:id="@+id/APIPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="@+id/txtAPIToken"
android:layout_width="wrap_content"

View File

@ -18,7 +18,9 @@
<string name="txtNetOffline">Internetverbindung prüfen!</string>
<string name="txtConfigNotSet">Bitte konfiguriere die App zunächst!</string>
<string name="txtAPIUsername"><b><u>Deine User ID:</u></b></string>
<string name="txtAPIPassword"><b><u>Your Http Auth password, leave blank for none:</u></b></string>
<string name="txtAPIToken"><b><u>Dein Token:</u></b></string>
<string name="error">Fehler</string>
<string name="success">Erfolg</string>
<string name="error_feed">Fehler beim laden des Feeds</string>
</resources>

View File

@ -18,7 +18,9 @@
<string name="txtNetOffline">Check Internet Connectivity !</string>
<string name="txtConfigNotSet">Please configure the app before Syncing !</string>
<string name="txtAPIUsername"><b><u>Your User ID:</u></b></string>
<string name="txtAPIPassword"><b><u>Your Http Auth password, leave blank for none:</u></b></string>
<string name="txtAPIToken"><b><u>Your Token:</u></b></string>
<string name="error">Error</string>
<string name="success">Success</string>
<string name="error_feed">Error loading feed</string>
</resources>

View File

@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
classpath 'com.android.tools.build:gradle:1.0.0'
}
}

View File

@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip