diff --git a/app/build.gradle b/app/build.gradle index ca28c3d..5e78162 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,8 +47,8 @@ dependencies { compile libraries.supportDesign compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4' - debugCompile 'com.github.tatocaster.BuildNumberOverlay:buildnumberoverlaylibrary:1.0.1' - releaseCompile 'com.github.tatocaster.BuildNumberOverlay:buildnumberoverlaylibrary-no-op:1.0.1' + debugCompile project(':buildnumberoverlaylibrary') + releaseCompile project(':buildnumberoverlaylibrary-no-op') debugCompile libraries.leakCanary releaseCompile libraries.leakCanaryReleaseAndTest diff --git a/app/src/main/java/me/tatocaster/buildversionoverlay/MyApp.java b/app/src/main/java/me/tatocaster/buildversionoverlay/MyApp.java index 10aee0c..78fc52a 100644 --- a/app/src/main/java/me/tatocaster/buildversionoverlay/MyApp.java +++ b/app/src/main/java/me/tatocaster/buildversionoverlay/MyApp.java @@ -1,6 +1,8 @@ package me.tatocaster.buildversionoverlay; import android.app.Application; +import android.graphics.Color; +import android.view.Gravity; import me.tatocaster.buildnumberoverlaylibrary.NumberOverlay; @@ -9,11 +11,20 @@ */ public class MyApp extends Application { - @Override - public void onCreate() { - super.onCreate(); - - // initialize build number overlay - NumberOverlay.initialize(this); - } + @Override + public void onCreate() { + super.onCreate(); + + // initialize build number overlay + new NumberOverlay.Builder(this) + .setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL) + .setText(String.format("Name: %s \n Code: %s", + BuildConfig.VERSION_NAME, + BuildConfig.VERSION_CODE)) + .setTextColor(Color.YELLOW) + .setBackgroundColor(Color.BLUE) + .setWidth(300) + .setHeight(200) + .build(); + } } diff --git a/build.gradle b/build.gradle index 01d2517..a225e42 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0-beta2' + classpath 'com.android.tools.build:gradle:2.3.2' // jitpack classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' diff --git a/buildnumberoverlaylibrary-no-op/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlay.java b/buildnumberoverlaylibrary-no-op/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlay.java index 1c2fca4..b980efe 100644 --- a/buildnumberoverlaylibrary-no-op/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlay.java +++ b/buildnumberoverlaylibrary-no-op/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlay.java @@ -42,5 +42,98 @@ public static synchronized boolean isInitialized() { public static Context getApplicationContext() { return null; } + + public static class Builder{ + + /** + * Builder for {@link NumberOverlay}. If you want to initialize call method {@link Builder#build()} + * Example of use: + * + *

new NumberOverlay.Builder(this)

+ *

.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL)

+ *

.setText(String.format("DEBUG VERSION"))

+ *

.setTextColor(Color.YELLOW)

+ *

.setBackgroundColor(Color.BLUE)

+ *

.build();

+ *
+ * @param context application + */ + public Builder(Context context) { + + + } + + + /** + * Set background color of canvas, otherwise it is used default background Color.BLACK + * @param backgroundColor color + * @return builder + */ + public Builder setBackgroundColor(int backgroundColor) { + + return this; + } + + + /** + * Set text color, otherwise it is used default Color.RED + * @param textColor text color + * @return builder + */ + public Builder setTextColor(int textColor) { + return this; + } + + + /** + * Set formatted text, otherwise it is used default text with build number and build code. + * + *

Name: version name

+ *

Code: version code

+ *
+ * @param text text to set + * @return builder + */ + public Builder setText(String text) { + + return this; + } + + + /** + * Set gravity of canvas, otherwise it is used default value Gravity.BOTTOM | Gravity.END + * @param gravity gravity of canvas + * @return builder + */ + public Builder setGravity(int gravity) { + return this; + } + + + /** + * Set height of canvas, otherwise it is used default value 300 pixels + * @param height canvas height in pixels + * @return builder + */ + public Builder setHeight(int height) { + return this; + } + + /** + * Set width of canvas, otherwise it is used default value 150 pixels + * @param width canvas width in pixels + * @return builder + */ + public Builder setWidth(int width) { + return this; + } + + /** + * Build set properties and initialize number overlay + */ + public void build(){ + + } + } } diff --git a/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/AccessPermissionActivity.java b/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/AccessPermissionActivity.java index 7d91716..cb6744a 100644 --- a/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/AccessPermissionActivity.java +++ b/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/AccessPermissionActivity.java @@ -12,6 +12,7 @@ import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; +import me.tatocaster.buildnumberoverlaylibrary.NumberOverlayView.OverlayView; import me.tatocaster.buildnumberoverlaylibrary.utils.Utils; /** @@ -21,23 +22,40 @@ public class AccessPermissionActivity extends AppCompatActivity { private static final int PERMISSION_REQUEST_CODE = 9991; - + private static final String PROPERTIES = "Properties"; + + private OverlayView.Properties properties; + + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - + + getPropertiesFromExtra(); + if (isSystemAlertPermissionGranted(this)) - startOverlayService(); + startOverlayService(properties); else requestSystemAlertPermission(this, null, PERMISSION_REQUEST_CODE); } - - /** + + private void getPropertiesFromExtra() { + if (getIntent() == null){ + return; + } + + properties = (OverlayView.Properties) getIntent().getSerializableExtra(PROPERTIES); + } + + /** * start the service - */ - private void startOverlayService() { - if (!Utils.isOverlayingServiceIsRunning(this, OverlayService.class)) - startService(new Intent(AccessPermissionActivity.this, OverlayService.class)); + * @param properties + */ + private void startOverlayService(OverlayView.Properties properties) { + if (!Utils.isOverlayingServiceIsRunning(this, OverlayService.class)) { + Intent intent = OverlayService.getCallingIntent(this, properties); + startService(intent); + } finish(); } @@ -68,8 +86,15 @@ public static boolean isSystemAlertPermissionGranted(Context context) { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PERMISSION_REQUEST_CODE && isSystemAlertPermissionGranted(NumberOverlay.getApplicationContext())) - startOverlayService(); + startOverlayService(properties); super.onActivityResult(requestCode, resultCode, data); } + + public static Intent getCallingIntent(Context context, OverlayView.Properties properties){ + Intent intent = new Intent(context, AccessPermissionActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.putExtra(PROPERTIES, properties); + return intent; + } } diff --git a/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlay.java b/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlay.java index 33ca1a0..e4f0cf3 100644 --- a/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlay.java +++ b/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlay.java @@ -2,7 +2,10 @@ import android.content.Context; import android.content.Intent; +import android.graphics.Color; +import android.view.Gravity; +import me.tatocaster.buildnumberoverlaylibrary.NumberOverlayView.OverlayView; import me.tatocaster.buildnumberoverlaylibrary.exceptions.NumberOverlayException; import me.tatocaster.buildnumberoverlaylibrary.utils.Validation; @@ -10,12 +13,25 @@ * Created by tatocaster on 1/28/17. */ +/** + * + * Example of use: + * + *

new NumberOverlay.Builder(this)

+ *

.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL)

+ *

.setText(String.format("DEBUG VERSION"))

+ *

.setTextColor(Color.YELLOW)

+ *

.setBackgroundColor(Color.BLUE)

+ *

.build();

+ *
+ */ public class NumberOverlay { private static final String TAG = NumberOverlay.class.getCanonicalName(); private static Context applicationContext; private static final String MULTIPLE_INSTANCE_ERROR_STRING = "Can not initialize multiple times!"; + /** * instance @@ -31,8 +47,9 @@ private NumberOverlay() { * initialize this in Application. * * @param applicationContext The application context + * @param builder */ - public static synchronized void initialize(Context applicationContext) { + private static synchronized void initialize(Context applicationContext, Builder builder) { if (instance != null || initialized) { throw new NumberOverlayException(MULTIPLE_INSTANCE_ERROR_STRING); } @@ -40,8 +57,7 @@ public static synchronized void initialize(Context applicationContext) { instance = new NumberOverlay(); NumberOverlay.applicationContext = applicationContext; - Intent intent = new Intent(getApplicationContext(), AccessPermissionActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + Intent intent = AccessPermissionActivity.getCallingIntent(getApplicationContext(), builder.getProperties()); getApplicationContext().startActivity(intent); } @@ -64,5 +80,150 @@ public static Context getApplicationContext() { Validation.sdkInitialized(); return applicationContext; } + + + /** + * + */ + public static class Builder{ + + + private static final int CANVAS_WIDTH = 300; + private static final int CANVAS_HEIGHT = 150; + private static final int GRAVITY = Gravity.BOTTOM | Gravity.END; + private static final String TEXT = String.format("Name: %s \n Code: %s", + BuildConfig.VERSION_NAME, + BuildConfig.VERSION_CODE); + private static final int TEXT_COLOR = Color.RED; + private static final int BACKGROUND_COLOR = Color.BLACK; + + private OverlayView.Properties properties; + private Context context; + + + /** + * Builder for {@link NumberOverlay}. If you want to initialize call method {@link Builder#build()} + * Example of use: + * + *

new NumberOverlay.Builder(this)

+ *

.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL)

+ *

.setText(String.format("DEBUG VERSION"))

+ *

.setTextColor(Color.YELLOW)

+ *

.setBackgroundColor(Color.BLUE)

+ *

.build();

+ *
+ * @param context application + */ + public Builder(Context context) { + this.context = context; + properties = new OverlayView.Properties(); + } + + + /** + * Set background color of canvas, otherwise it is used default background Color.BLACK + * @param backgroundColor color + * @return builder + */ + public Builder setBackgroundColor(int backgroundColor) { + this.properties.setBackgroundColor(backgroundColor); + return this; + } + + + /** + * Set text color, otherwise it is used default Color.RED + * @param textColor text color + * @return builder + */ + public Builder setTextColor(int textColor) { + this.properties.setTextColor(textColor); + return this; + } + + + /** + * Set formatted text, otherwise it is used default text with build number and build code. + * + *

Name: version name

+ *

Code: version code

+ *
+ * @param text text to set + * @return builder + */ + public Builder setText(String text) { + this.properties.setText(text); + return this; + } + + + /** + * Set gravity of canvas, otherwise it is used default value Gravity.BOTTOM | Gravity.END + * @param gravity gravity of canvas + * @return builder + */ + public Builder setGravity(int gravity) { + this.properties.setGravity(gravity); + return this; + } + + + /** + * Set height of canvas, otherwise it is used default value {@value CANVAS_HEIGHT} pixels + * @param height canvas height in pixels + * @return builder + */ + public Builder setHeight(int height) { + this.properties.setCanvasHeight(height); + return this; + } + + /** + * Set width of canvas, otherwise it is used default value {@value CANVAS_WIDTH} pixels + * @param width canvas width in pixels + * @return builder + */ + public Builder setWidth(int width) { + this.properties.setCanvasWidth(width); + return this; + } + + + OverlayView.Properties getProperties() { + return properties; + } + + /** + * Build set properties and initialize number overlay + */ + public void build(){ + if (properties.getCanvasWidth() == null) { + properties.setCanvasWidth(CANVAS_WIDTH); + } + + if (properties.getCanvasHeight() == null) { + properties.setCanvasHeight(CANVAS_HEIGHT); + } + + if (properties.getBackgroundColor() == null) { + properties.setBackgroundColor(BACKGROUND_COLOR); + } + + if (properties.getTextColor() == null) { + properties.setTextColor(TEXT_COLOR); + } + + if (properties.getText() == null) { + properties.setText(TEXT); + } + + if (properties.getGravity() == null) { + properties.setGravity(GRAVITY); + } + + NumberOverlay.initialize(context, this); + } + } + } diff --git a/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlayView/OverlayView.java b/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlayView/OverlayView.java index 5acf17a..b44d810 100644 --- a/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlayView/OverlayView.java +++ b/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/NumberOverlayView/OverlayView.java @@ -2,14 +2,15 @@ import android.content.Context; import android.content.pm.PackageInfo; -import android.graphics.Color; import android.graphics.PixelFormat; import android.util.AttributeSet; -import android.view.Gravity; import android.view.View; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.TextView; + +import java.io.Serializable; + import me.tatocaster.buildnumberoverlaylibrary.utils.Utils; /** @@ -28,8 +29,6 @@ public class OverlayView extends View { private WindowManager mWindowManager; private LinearLayout mLinearLayout; - private static final int CANVAS_WIDTH = 300; - private static final int CANVAS_HEIGHT = 150; /** * main package info */ @@ -51,27 +50,23 @@ private void init(Context context) { mPackageInfo = Utils.getVersionInfo(mContext); } - public void addToWindowManager() { + public void addToWindowManager(Properties properties) { WindowManager.LayoutParams windowLayoutParams = new WindowManager.LayoutParams( - CANVAS_WIDTH, - CANVAS_HEIGHT, + properties.getCanvasWidth(), + properties.getCanvasHeight(), WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); - windowLayoutParams.gravity = Gravity.BOTTOM | Gravity.END; + windowLayoutParams.gravity = properties.getGravity(); mLinearLayout = new LinearLayout(mContext); - mLinearLayout.setBackgroundColor(Color.BLACK); + mLinearLayout.setBackgroundColor(properties.getBackgroundColor()); mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); mWindowManager.addView(mLinearLayout, windowLayoutParams); TextView textView = new TextView(mContext); - textView.setTextColor(Color.RED); - textView.setText( - String.format("Name: %s \n Code: %s", - mPackageInfo.versionName, - mPackageInfo.versionCode) - ); + textView.setTextColor(properties.getTextColor()); + textView.setText(properties.getText()); mLinearLayout.addView(textView); } @@ -81,4 +76,61 @@ public void addToWindowManager() { public void destroy() { mWindowManager.removeView(mLinearLayout); } + + public static class Properties implements Serializable{ + private Integer backgroundColor = null; + private Integer textColor = null; + private String text = null; + private Integer gravity = null; + private Integer canvasHeight = null; + private Integer canvasWidth = null; + + public Integer getBackgroundColor() { + return backgroundColor; + } + + public Integer getTextColor() { + return textColor; + } + + public String getText() { + return text; + } + + public Integer getGravity() { + return gravity; + } + + public Integer getCanvasHeight() { + return canvasHeight; + } + + public Integer getCanvasWidth() { + return canvasWidth; + } + + public void setBackgroundColor(int color) { + this.backgroundColor = color; + } + + public void setTextColor(int textColor) { + this.textColor = textColor; + } + + public void setText(String text) { + this.text = text; + } + + public void setGravity(int gravity) { + this.gravity = gravity; + } + + public void setCanvasHeight(int canvasHeight) { + this.canvasHeight = canvasHeight; + } + + public void setCanvasWidth(int canvasWidth) { + this.canvasWidth = canvasWidth; + } + } } diff --git a/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/OverlayService.java b/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/OverlayService.java index 2543680..b6cb0ed 100644 --- a/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/OverlayService.java +++ b/buildnumberoverlaylibrary/src/main/java/me/tatocaster/buildnumberoverlaylibrary/OverlayService.java @@ -2,6 +2,7 @@ import android.app.Notification; import android.app.Service; +import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.support.annotation.Nullable; @@ -16,9 +17,11 @@ public class OverlayService extends Service { private static final String TAG = "OverlayService"; private static final int FOREGROUND_ID = 9998; - private OverlayView mOverlayView; - - @Nullable + private static final String PROPERTIES = "Properties"; + private OverlayView mOverlayView; + private OverlayView.Properties properties; + + @Nullable @Override public IBinder onBind(Intent intent) { return null; @@ -26,15 +29,26 @@ public IBinder onBind(Intent intent) { @Override public int onStartCommand(Intent intent, int flags, int startId) { - + getPropertiesFromExtra(intent); + mOverlayView = new OverlayView(NumberOverlay.getApplicationContext()); - mOverlayView.addToWindowManager(); + mOverlayView.addToWindowManager(properties); + + // this needs to be here, because without the startForeground(), our view will not retain always /* startForeground(FOREGROUND_ID, createNotification()); return START_STICKY;*/ return START_NOT_STICKY; } + + private void getPropertiesFromExtra(Intent intent) { + if (intent == null){ + return; + } + + properties = (OverlayView.Properties) intent.getSerializableExtra(PROPERTIES); + } @Override public void onDestroy() { @@ -52,4 +66,10 @@ private Notification createNotification() { .setContentTitle("Build Number Overlay") .build(); } + + public static Intent getCallingIntent(Context context, OverlayView.Properties properties) { + Intent intent = new Intent(context, OverlayService.class); + intent.putExtra(PROPERTIES, properties); + return intent; + } }