Skip to content

Commit

Permalink
Merge pull request #7 from orhanobut/oo/bee-settings
Browse files Browse the repository at this point in the history
v1.4 changes
  • Loading branch information
orhanobut committed Jun 11, 2015
2 parents 99e5345 + 36cc0b1 commit 462d23c
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 60 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Bee is a QA/Debug tool that works like a widget in any application. The idea is
#### Gradle

```groovy
compile 'com.orhanobut:bee:1.3@aar'
compile 'com.orhanobut:bee:1.4@aar'
```

#### Usage
Expand Down Expand Up @@ -75,14 +75,6 @@ public class SampleBeeConfig extends BeeConfig {
Log.d(TAG, "onEndPointSelected");
}

/**
* Change the bee button position
*/
@Override
public int getBeePosition() {
return Gravity.LEFT | Gravity.CENTER_VERTICAL;
}

}
```

Expand All @@ -94,7 +86,11 @@ In order to activate Bee, you need to pass activity as context. You can either i
protected void onCreate(Bundle savedInstanceState) {
...

Bee.inject(this, SampleBeeConfig.class);
Bee.init(this)
.setBeeSize(100) //optional bee button size
.setBeePosition(Gravity.CENTER) //optional bee button position
.setBeeMargin(0, 0, 0, 400) //optional margin for the bee button
.inject(SampleBeeConfig.class); //required
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.orhanobut.android.beesample;

import android.util.Log;
import android.view.Gravity;

import com.orhanobut.bee.BeeConfig;
import com.orhanobut.bee.widgets.Button;
Expand All @@ -19,11 +18,6 @@ public class AppBeeConfig extends BeeConfig {
private static final String TAG = AppBeeConfig.class.getSimpleName();


@Override
public int getBeePosition() {
return Gravity.LEFT | Gravity.CENTER_VERTICAL;
}

/**
* Add extra information by using content object.
*/
Expand All @@ -32,14 +26,6 @@ public void onInfoContentCreated(Map<String, String> content) {
content.put("Current End Point", "http://www.google.com");
}

/**
* It is called when the save button is pressed
*/
@Override
public void onSave() {
super.onSave();
}

/**
* It is called when the close button is pressed
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;

import com.orhanobut.bee.Bee;
import com.orhanobut.bee.BeeLog;
Expand All @@ -14,7 +15,12 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Bee.inject(this, AppBeeConfig.class);

Bee.init(this)
.setBeeSize(100)
.setBeePosition(Gravity.CENTER)
.setBeeMargin(0, 0, 0, 400)
.inject(AppBeeConfig.class);

BeeLog.d("MainActivity", "onCreate");
BeeLog.d("MainActivity", "user logged in");
Expand Down
24 changes: 17 additions & 7 deletions bee/src/main/java/com/orhanobut/bee/Bee.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,26 @@ public class Bee {

private static final String TAG = Bee.class.getSimpleName();

public static void inject(Context context, Class<?> clazz) {
if (context == null) {
private final Settings settings;

public Bee(Settings settings) {
this.settings = settings;
}

public static Settings init(Context context) {
return new Settings(context);
}

public void inject(Class<?> config) {
if (settings.getContext() == null) {
throw new NullPointerException("Context may not be null");
}
if (clazz == null) {
if (config == null) {
throw new NullPointerException("Class may not be null");
}

try {
new BeeHandler(context, clazz);
new BeeHandler(settings, config);
} catch (IllegalAccessException e) {
Log.d(TAG, e.getMessage());
} catch (InstantiationException e) {
Expand All @@ -39,13 +49,13 @@ static class BeeHandler {
private final List<MethodInfo> methodInfoList = new ArrayList<>();
private final UiHandler helper;

BeeHandler(Context context, Class<?> clazz) throws IllegalAccessException, InstantiationException {
BeeHandler(Settings settings, Class<?> clazz) throws IllegalAccessException, InstantiationException {
instance = (ConfigListener) clazz.newInstance();
instance.setContext(context);
instance.setContext(settings.getContext());

fillMethods(clazz.getDeclaredMethods());

helper = new UiHandler(context, methodInfoList, instance);
helper = new UiHandler(settings, methodInfoList, instance);
helper.inject();
}

Expand Down
13 changes: 1 addition & 12 deletions bee/src/main/java/com/orhanobut/bee/BeeConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.orhanobut.bee;

import android.content.Context;
import android.view.Gravity;

import java.util.List;
import java.util.Map;
Expand All @@ -15,11 +14,6 @@ public abstract class BeeConfig implements ConfigListener {

private Context context;

@Override
public int getBeePosition() {
return Gravity.CENTER_VERTICAL | Gravity.END;
}

@Override
public void setContext(Context context) {
this.context = context;
Expand All @@ -34,12 +28,7 @@ public Context getContext() {
public void onClose() {

}

@Override
public void onSave() {

}


@Override
public void onInfoContentCreated(Map<String, String> content) {
}
Expand Down
5 changes: 0 additions & 5 deletions bee/src/main/java/com/orhanobut/bee/ConfigListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
interface ConfigListener {

int getBeePosition();

@SuppressWarnings("unused")
void onInfoContentCreated(Map<String, String> content);

Expand All @@ -23,9 +21,6 @@ interface ConfigListener {
@SuppressWarnings("unused")
void onClose();

@SuppressWarnings("unused")
void onSave();

@SuppressWarnings("unused")
Context getContext();

Expand Down
56 changes: 56 additions & 0 deletions bee/src/main/java/com/orhanobut/bee/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.orhanobut.bee;

import android.content.Context;
import android.view.Gravity;

/**
* @author Orhan Obut
*/
public class Settings {

private static final int DEFAULT_BEE_SIZE = 80;

private Context context;
private int gravity = Gravity.CENTER_VERTICAL | Gravity.END;
private int beeSize = DEFAULT_BEE_SIZE;
private int[] beeMargin;

public Settings(Context context) {
this.context = context;
}

public Settings setBeePosition(int gravity) {
this.gravity = gravity;
return this;
}

public Settings setBeeMargin(int left, int top, int right, int bottom) {
this.beeMargin = new int[]{left, top, right, bottom};
return this;
}

public Settings setBeeSize(int size) {
this.beeSize = size;
return this;
}

public void inject(Class<?> config) {
new Bee(this).inject(config);
}

public Context getContext() {
return context;
}

public int getGravity() {
return gravity;
}

public int getBeeSize() {
return beeSize;
}

public int[] getBeeMargin() {
return beeMargin;
}
}
31 changes: 22 additions & 9 deletions bee/src/main/java/com/orhanobut/bee/UiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.graphics.Point;
import android.graphics.PointF;
import android.os.Build;
import android.os.SystemClock;
import android.text.ClipboardManager;
import android.util.Log;
import android.view.Display;
Expand Down Expand Up @@ -36,7 +37,6 @@
final class UiHandler implements View.OnClickListener {

private static final String TAG = UiHandler.class.getSimpleName();
private static final int BEE_SIZE = 80;

/**
* Stores all settings views
Expand Down Expand Up @@ -67,8 +67,11 @@ final class UiHandler implements View.OnClickListener {

private final Point displaySize;

public UiHandler(Context context, List<MethodInfo> list, ConfigListener listener) {
this.context = context;
private final Settings settings;

public UiHandler(Settings settings, List<MethodInfo> list, ConfigListener listener) {
this.settings = settings;
this.context = settings.getContext();
this.methodInfoList = list;
this.configListener = listener;

Expand Down Expand Up @@ -203,8 +206,13 @@ public void onClick(View v) {
private void setBeeButton(ViewGroup rootView) {
beeImageView.setImageResource(R.drawable.bee);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
BEE_SIZE, BEE_SIZE, configListener.getBeePosition()
settings.getBeeSize(), settings.getBeeSize(), settings.getGravity()
);
int[] margin = settings.getBeeMargin();
if (margin != null) {
params.setMargins(margin[0], margin[1], margin[2], margin[3]);
}

beeImageView.setLayoutParams(params);
beeImageView.setOnClickListener(null);

Expand All @@ -220,6 +228,7 @@ private void setBeeButton(ViewGroup rootView) {
final GestureDetector gestureDetector = new GestureDetector(context, gestureListener);

PointF touchPos = new PointF();
long touchTime;

@Override
public boolean onTouch(View v, MotionEvent event) {
Expand All @@ -228,35 +237,39 @@ public boolean onTouch(View v, MotionEvent event) {
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchTime = SystemClock.uptimeMillis();
touchPos.set(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
int x = (int) event.getRawX();
int y = (int) event.getRawY();

if (!isMoveable(x, y)) {
return true;
break;
}
if (!isInBoundaries(x, y)) {
return true;
break;
}

FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) v.getLayoutParams();
params.topMargin = y - v.getHeight() / 2;
params.leftMargin = x - v.getWidth() / 2;
params.gravity = Gravity.NO_GRAVITY;
v.setLayoutParams(params);
return true;
break;
}
return false;
return SystemClock.uptimeMillis() - touchTime > 200;
}

private boolean isMoveable(int x, int y) {
if (SystemClock.uptimeMillis() - touchTime < 200) {
return false;
}
return (Math.abs(x - touchPos.x) > MIN_MOVEMENT || Math.abs(y - touchPos.y) > MIN_MOVEMENT);
}

private boolean isInBoundaries(int x, int y) {
int half = BEE_SIZE / 2;
int half = settings.getBeeSize() / 2;
return !(x + half > displaySize.x || x < half || y + half > displaySize.y || y < half + 50);
}

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#
#VERSION_NAME=1.1-SNAPSHOT
#VERSION_CODE=2
VERSION_NAME=1.3
VERSION_CODE=4
VERSION_NAME=1.4
VERSION_CODE=5
GROUP=com.orhanobut

POM_DESCRIPTION=Debug and QA tool to configure application on the fly
Expand Down

0 comments on commit 462d23c

Please sign in to comment.