Skip to content

Commit

Permalink
Actually parse input text according to url options
Browse files Browse the repository at this point in the history
Resolves GH-4

Signed-off-by: Eddie Ringle <eddie@ringle.io>
  • Loading branch information
EddieRingle committed Jul 24, 2016
1 parent 5116355 commit 8867ba3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
Expand Down Expand Up @@ -67,6 +68,74 @@ public class GenerateFragment extends Fragment {
@InjectView(R.id.copy_password)
ImageButton mCopyPassword;

@InjectView(R.id.using_text)
TextView mUsingText;

private static String processInputText(PMProfile profile, String input) {
if (input.isEmpty()) {
return "";
}
Uri uri;
boolean emptyProtocol;
if (input.contains("://")) {
emptyProtocol = false;
uri = Uri.parse(input);
} else {
emptyProtocol = true;
uri = Uri.parse("://" + input);
}
StringBuilder result = new StringBuilder(input.length());
if (profile.getUseUrlProtocol() && uri.getScheme() != null && !emptyProtocol) {
result.append(uri.getScheme());
result.append(':');
if (input.contains("://")) {
result.append("//");
}
}
if (uri.getUserInfo() != null) {
result.append(uri.getUserInfo()).append('@');
}
if (uri.getHost() != null) {
String[] host = uri.getHost().split("\\.");
if (profile.getUseUrlSubdomain() && host.length > 2) {
for (int i = 0; i < host.length - 2; i++) {
if (i != 0) {
result.append('.');
}
result.append(host[i]);
}
if (profile.getUseUrlDomain()) {
result.append('.');
}
}
if (profile.getUseUrlDomain()) {
if (host.length > 1) {
result.append(host[host.length - 2]);
}
if (host.length > 0) {
result.append('.').append(host[host.length - 1]);
}
}
}
if (profile.getUseUrlOther()) {
if (uri.getPort() >= 0) {
result.append(':').append(uri.getPort());
}
boolean hasPath = false;
if (uri.getPath() != null) {
hasPath = true;
result.append(uri.getPath());
}
if (uri.getQuery() != null) {
if (!hasPath) {
result.append('/');
}
result.append('?').append(uri.getQuery());
}
}
return result.toString();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_generate, container, false);
Expand All @@ -89,6 +158,8 @@ public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if ("file:///android_asset/passwordmaker.html".equals(url)) {
String useText = processInputText(mSelectedProfile, mInputText.getText().toString());
mUsingText.setText(getString(R.string.using_text, useText));
StringBuilder sb = new StringBuilder();
sb.append("javascript:pmxApp.submitPassword(pmxGenerate(");
sb.append("'" + StringEscapeUtils.escapeEcmaScript(mSelectedProfile.getCharacterSet()) + "',");
Expand All @@ -97,7 +168,7 @@ public void onPageFinished(WebView view, String url) {
sb.append(Integer.toString(mSelectedProfile.getL33tLevel()) + ",");
sb.append(Integer.toString(mSelectedProfile.getPasswordLength()) + ",");
sb.append("'" + mMasterPassword.getText().toString() + "',");
sb.append("'" + mInputText.getText().toString() + "',");
sb.append("'" + useText + "',");
sb.append("'" + StringEscapeUtils.escapeEcmaScript(mSelectedProfile.getUsername()) + "',");
sb.append("'" + StringEscapeUtils.escapeEcmaScript(mSelectedProfile.getModifier()) + "',");
sb.append("'" + StringEscapeUtils.escapeEcmaScript(mSelectedProfile.getPasswordPrefix()) + "',");
Expand Down
10 changes: 9 additions & 1 deletion passwordmaker-x/src/main/res/layout/fragment_generate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,13 @@
android:contentDescription="Copy to clipboard"
android:src="@drawable/ic_menu_copy" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="4dp" />
<TextView
android:id="@+id/using_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
</ScrollView>
</ScrollView>
1 change: 1 addition & 0 deletions passwordmaker-x/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<string name="l33t_level_7">Level 7</string>
<string name="l33t_level_8">Level 8</string>
<string name="l33t_level_9">Level 9</string>
<string name="using_text">Using text: %1$s</string>

<string-array name="l33t_levels">
<item>@string/l33t_level_1</item>
Expand Down

0 comments on commit 8867ba3

Please sign in to comment.