Skip to content

Commit

Permalink
Add reflection module
Browse files Browse the repository at this point in the history
  • Loading branch information
bartwell committed Jan 20, 2017
1 parent f9f312f commit d591546
Show file tree
Hide file tree
Showing 20 changed files with 514 additions and 30 deletions.
13 changes: 9 additions & 4 deletions README.md
Expand Up @@ -17,6 +17,7 @@ _You can use `compile` instead of `debugCompile` if you need._

__2.__ Add libraries of modules that you need:
```groovy
debugCompile 'ru.bartwell:ultradebugger.module.reflection:1.0'
debugCompile 'ru.bartwell:ultradebugger.module.sqlite:1.0'
debugCompile 'ru.bartwell:ultradebugger.module.sharedpreferences:1.0'
```
Expand All @@ -41,6 +42,7 @@ Ultra Debugger is module-based tools for easy Android applications debugging. It

Currently available modules:

* Reflection - allow call methods from current activity, see list of fields and methods in this activity
* SQLite - allow see, add, edit and delete items from databases
* Shared Preferences - allow see, add, edit and delete items from shared preferences

Expand All @@ -58,13 +60,16 @@ Your help is really appreciated.

## Modules creation

1. Create Android library project.
2. Add base library as dependency:
__1.__ Create Android library project.

__2.__ Add base library as dependency:
```groovy
compile 'ru.bartwell:ultradebugger.base:1.0'
```
3. Create class `Module extends BaseModule` in package `ru.bartwell.ultradebugger.module.xxx`, where `xxx` - your module name.
4. Implement methods in your class:

__3.__ Create class `Module extends BaseModule` in package `ru.bartwell.ultradebugger.module.xxx`, where `xxx` - your module name.

__4.__ Implement methods in your class:
* `String getName()` - return human readable your module name from this method
* `String getDescription()` - return description of your module
* `HttpResponse handle(HttpRequest)` - handle HTTP requests in this method and return result which will sent into browser.
Expand Down
Expand Up @@ -2,25 +2,33 @@

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import ru.bartwell.ultradebugger.UltraDebugger;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((TextView)findViewById(R.id.url)).setText(getString(R.string.ip_format, UltraDebugger.getPort()));
((TextView) findViewById(R.id.url)).setText(getString(R.string.ip_format, UltraDebugger.getPort()));
new DbHelper(this).getReadableDatabase();
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putString("string_value", "String value");
editor.putInt("int_value", 123);
editor.putBoolean("boolean_value", true);
editor.commit();
}

public void showToast(String text) {
Log.d(TAG, "showToast(): " + text);
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
}
44 changes: 37 additions & 7 deletions base/src/main/java/ru/bartwell/ultradebugger/base/BaseModule.java
@@ -1,9 +1,12 @@
package ru.bartwell.ultradebugger.base;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -16,12 +19,14 @@
*/

public abstract class BaseModule {
@NonNull
private Context mContext;

public BaseModule(Context context) {
public BaseModule(@NonNull Context context) {
mContext = context;
}

@NonNull
public Context getContext() {
return mContext;
}
Expand All @@ -31,29 +36,54 @@ protected String getString(@StringRes int stringResId) {
return mContext.getString(stringResId);
}

public Map<String, String> getMapFromParameters(Map<String, List<String>> parameters, String name) {
@NonNull
protected Map<String, String> getMapFromParameters(@Nullable Map<String, List<String>> parameters, @Nullable String name) {
Map<String, String> result = new HashMap<>();
for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
if (entry.getKey().startsWith(name + "[")) {
result.put(entry.getKey().substring(name.length() + 1, entry.getKey().length() - 1), entry.getValue().get(0));
if (parameters != null && name != null) {
for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
if (entry.getKey().startsWith(name + "[")) {
result.put(entry.getKey().substring(name.length() + 1, entry.getKey().length() - 1), entry.getValue().get(0));
}
}
}
return result;
}

@NonNull
protected List<String> getListFromParameters(@Nullable Map<String, List<String>> parameters, @Nullable String name) {
if (parameters != null && name != null) {
List<String> values = parameters.get(name + "[]");
if (values != null) {
return values;
}
}
return new ArrayList<>();
}

@Nullable
public String getParameterValue(@Nullable Map<String, List<String>> parameters, @Nullable String key) {
protected String getParameterValue(@Nullable Map<String, List<String>> parameters, @Nullable String key) {
if (parameters != null && key != null && parameters.get(key) != null) {
return parameters.get(key).get(0);
}
return null;
}

@NonNull
protected String getQueryStringFromArray(@NonNull String paramName, @Nullable String[] array, boolean startWithAmp) {
if (array != null && array.length > 0) {
return (startWithAmp ? "&" : "?") +
paramName + "[]=" +
TextUtils.join("&" + paramName + "[]=", array);
}
return "";
}

@Nullable
public abstract String getName();

@Nullable
public abstract String getDescription();

public abstract HttpResponse handle(HttpRequest request);
@NonNull
public abstract HttpResponse handle(@NonNull HttpRequest request);
}
Expand Up @@ -5,6 +5,11 @@
*/

public class ErrorPage extends Page {

public ErrorPage(String errorText) {
this(errorText, true);
}

public ErrorPage(String errorText, boolean showHomeButton) {
Content content = new Content();
content.add(new RawContentPart("<p align=\"center\">" + errorText + "</p>"));
Expand Down
24 changes: 11 additions & 13 deletions base/src/main/java/ru/bartwell/ultradebugger/base/html/Form.java
Expand Up @@ -2,9 +2,9 @@

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -15,7 +15,7 @@ public class Form extends ContentPart {

private final Table mTable;
private int mRow;
private Map<String, String> mHiddens = new HashMap<>();
private StringBuilder mHiddens = new StringBuilder();

public void setPostMethod(boolean isPostMethod) {
mIsPostMethod = isPostMethod;
Expand All @@ -37,19 +37,23 @@ public void addInputText(@NonNull String label, @NonNull String name, @Nullable
value = "";
}
mTable.add(0, mRow, new RawContentPart(label));
mTable.add(1, mRow, new RawContentPart("<input type=\"text\" name=\"" + name + "\" value=\"" + value + "\">"));
mTable.add(1, mRow, new RawContentPart("<input type=\"text\" name=\"" + TextUtils.htmlEncode(name) + "\" value=\"" + TextUtils.htmlEncode(value) + "\">"));
mRow++;
}

public void addHidden(String name, String value) {
mHiddens.put(name, value);
mHiddens.append("<input type=\"hidden\" name=\"")
.append(TextUtils.htmlEncode(name))
.append("\" value=\"")
.append(TextUtils.htmlEncode(value))
.append("\">");
}

public void addSelect(String label, String name, ArrayList<String> values) {
mTable.add(0, mRow, new RawContentPart(label));
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<select name=\"")
.append(name)
.append(TextUtils.htmlEncode(name))
.append("\">");
for (String value : values) {
stringBuilder.append("<option>")
Expand All @@ -62,7 +66,7 @@ public void addSelect(String label, String name, ArrayList<String> values) {
}

public void addSubmit(String label) {
mTable.add(1, mRow, new RawContentPart("<input type=\"submit\" value=\"" + label + "\">"));
mTable.add(1, mRow, new RawContentPart("<input type=\"submit\" value=\"" + TextUtils.htmlEncode(label) + "\">"));
mRow++;
}

Expand All @@ -74,13 +78,7 @@ public String toHtml() {
.append("\" method=\"")
.append(mIsPostMethod ? "post" : "get")
.append("\">");
for (Map.Entry<String, String> entry : mHiddens.entrySet()) {
stringBuilder.append("<input type=\"hidden\" name=\"")
.append(entry.getKey())
.append("\" value=\"")
.append(entry.getValue())
.append("\">");
}
stringBuilder.append(mHiddens.toString());
stringBuilder.append(mTable.toHtml())
.append("</form>");
return stringBuilder.toString();
Expand Down
@@ -0,0 +1,28 @@
package ru.bartwell.ultradebugger.base.html;

import android.support.annotation.IntRange;
import android.support.annotation.NonNull;

/**
* Created by BArtWell on 20.01.2017.
*/

public class HeadingContentPart extends ContentPart {
@IntRange(from = 1, to = 6)
private int mImportance;
@NonNull
private final String mTitle;

public HeadingContentPart(@IntRange(from = 1, to = 6) int importance, @NonNull String title) {
super();
mImportance = importance;
mTitle = title;
}

@NonNull
@Override
public String toHtml() {
return "<h" + mImportance + ">" + mTitle + "</h" + mImportance + ">";

}
}
Expand Up @@ -14,6 +14,10 @@ public void setTitle(String title) {
mTitle = title;
}

public Content getContent() {
return mContent;
}

public void setContent(Content content) {
mContent = content;
}
Expand Down
1 change: 1 addition & 0 deletions reflection/.gitignore
@@ -0,0 +1 @@
/build
34 changes: 34 additions & 0 deletions reflection/build.gradle
@@ -0,0 +1,34 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 24
buildToolsVersion "24.0.3"

defaultConfig {
minSdkVersion 9
targetSdkVersion 24
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile project(':base')
compile 'com.google.code.gson:gson:2.8.0'
}

apply from: '../maven_push.gradle'
3 changes: 3 additions & 0 deletions reflection/gradle.properties
@@ -0,0 +1,3 @@
POM_NAME=Ultra Debugger Reflection Module
POM_ARTIFACT_ID=ultradebugger.module.reflection
POM_PACKAGING=aar
17 changes: 17 additions & 0 deletions reflection/proguard-rules.pro
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\BArtWell\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
@@ -0,0 +1,26 @@
package ru.bartwell.ultradebugger.module.reflection;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("ru.bartwell.ultradebugger.module.reflection.test", appContext.getPackageName());
}
}
11 changes: 11 additions & 0 deletions reflection/src/main/AndroidManifest.xml
@@ -0,0 +1,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.bartwell.ultradebugger.module.reflection">

<application android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
>

</application>

</manifest>

0 comments on commit d591546

Please sign in to comment.