From ed5e90694ac2205ed0767a56c44d85683e3b5aa3 Mon Sep 17 00:00:00 2001 From: Ahmed Tarek Date: Sat, 20 May 2017 22:04:25 +0200 Subject: [PATCH] rewrite Instacapture in Kotlin --- README.md | 2 +- .../tarek360/sample/BaseSampleActivity.java | 6 +- .../com/tarek360/sample/utility/Utility.java | 2 +- .../ActivityReferenceManager.java | 47 ---- .../instacapture/ActivityReferenceManager.kt | 44 ++++ .../tarek360/instacapture/Instacapture.java | 90 ------- .../com/tarek360/instacapture/Instacapture.kt | 83 +++++++ .../InstacaptureConfiguration.java | 38 --- .../ActivityNotRunningException.java | 19 -- .../exception/ActivityNotRunningException.kt | 16 ++ .../exception/IllegalScreenSizeException.java | 15 -- .../exception/IllegalScreenSizeException.kt | 10 + .../ScreenCapturingFailedException.java | 15 -- .../ScreenCapturingFailedException.kt | 10 + ...Listener.java => ScreenCaptureListener.kt} | 20 +- ....java => SimpleScreenCapturingListener.kt} | 19 +- .../instacapture/screenshot/FieldHelper.java | 73 ------ .../instacapture/screenshot/FieldHelper.kt | 69 ++++++ .../instacapture/screenshot/RootViewInfo.kt | 2 +- .../screenshot/ScreenshotProvider.java | 20 -- .../screenshot/ScreenshotProvider.kt | 18 ++ .../screenshot/ScreenshotTaker.java | 232 ------------------ .../screenshot/ScreenshotTaker.kt | 223 +++++++++++++++++ .../screenshot/ViewsBitmapObservable.java | 40 --- .../screenshot/ViewsBitmapObservable.kt | 29 +++ .../tarek360/instacapture/utility/Logger.java | 49 ---- .../tarek360/instacapture/utility/Logger.kt | 45 ++++ 27 files changed, 571 insertions(+), 665 deletions(-) delete mode 100644 instacapture/src/main/java/com/tarek360/instacapture/ActivityReferenceManager.java create mode 100644 instacapture/src/main/java/com/tarek360/instacapture/ActivityReferenceManager.kt delete mode 100644 instacapture/src/main/java/com/tarek360/instacapture/Instacapture.java create mode 100644 instacapture/src/main/java/com/tarek360/instacapture/Instacapture.kt delete mode 100644 instacapture/src/main/java/com/tarek360/instacapture/InstacaptureConfiguration.java delete mode 100644 instacapture/src/main/java/com/tarek360/instacapture/exception/ActivityNotRunningException.java create mode 100644 instacapture/src/main/java/com/tarek360/instacapture/exception/ActivityNotRunningException.kt delete mode 100644 instacapture/src/main/java/com/tarek360/instacapture/exception/IllegalScreenSizeException.java create mode 100644 instacapture/src/main/java/com/tarek360/instacapture/exception/IllegalScreenSizeException.kt delete mode 100644 instacapture/src/main/java/com/tarek360/instacapture/exception/ScreenCapturingFailedException.java create mode 100644 instacapture/src/main/java/com/tarek360/instacapture/exception/ScreenCapturingFailedException.kt rename instacapture/src/main/java/com/tarek360/instacapture/listener/{ScreenCaptureListener.java => ScreenCaptureListener.kt} (52%) rename instacapture/src/main/java/com/tarek360/instacapture/listener/{SimpleScreenCapturingListener.java => SimpleScreenCapturingListener.kt} (60%) delete mode 100644 instacapture/src/main/java/com/tarek360/instacapture/screenshot/FieldHelper.java create mode 100644 instacapture/src/main/java/com/tarek360/instacapture/screenshot/FieldHelper.kt delete mode 100644 instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotProvider.java create mode 100644 instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotProvider.kt delete mode 100644 instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotTaker.java create mode 100644 instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotTaker.kt delete mode 100644 instacapture/src/main/java/com/tarek360/instacapture/screenshot/ViewsBitmapObservable.java create mode 100644 instacapture/src/main/java/com/tarek360/instacapture/screenshot/ViewsBitmapObservable.kt delete mode 100644 instacapture/src/main/java/com/tarek360/instacapture/utility/Logger.java create mode 100644 instacapture/src/main/java/com/tarek360/instacapture/utility/Logger.kt diff --git a/README.md b/README.md index 2e816de..03d15f3 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Add this to your module `build.gradle` file: ```gradle dependencies { ... - compile "com.github.tarek360:instacapture:2.0.0-beta" + compile "com.github.tarek360:instacapture:kotlin-2.0.0-beta" } ``` diff --git a/app/src/main/java/com/tarek360/sample/BaseSampleActivity.java b/app/src/main/java/com/tarek360/sample/BaseSampleActivity.java index 108773c..49e80dc 100644 --- a/app/src/main/java/com/tarek360/sample/BaseSampleActivity.java +++ b/app/src/main/java/com/tarek360/sample/BaseSampleActivity.java @@ -24,7 +24,7 @@ public abstract class BaseSampleActivity extends AppCompatActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - Instacapture.enableLogging(true); + Instacapture.INSTANCE.enableLogging(true); } @Override @@ -40,7 +40,7 @@ protected void showAlertDialog() { protected void captureScreenshot(@Nullable View... ignoredViews) { - Instacapture.capture(this, new SimpleScreenCapturingListener() { + Instacapture.INSTANCE.capture(this, new SimpleScreenCapturingListener() { @Override public void onCaptureComplete(Bitmap bitmap) { @@ -58,7 +58,7 @@ public void call(File file) { }, ignoredViews); - Instacapture.captureRx(this, ignoredViews).subscribe(new Action1() { + Instacapture.INSTANCE.captureRx(this, ignoredViews).subscribe(new Action1() { @Override public void call(Bitmap bitmap) { diff --git a/app/src/main/java/com/tarek360/sample/utility/Utility.java b/app/src/main/java/com/tarek360/sample/utility/Utility.java index e5048ce..8d7c992 100644 --- a/app/src/main/java/com/tarek360/sample/utility/Utility.java +++ b/app/src/main/java/com/tarek360/sample/utility/Utility.java @@ -95,7 +95,7 @@ public void call(final Subscriber subscriber) { subscriber.onNext(screenshotFile); subscriber.onCompleted(); - Logger.d("Screenshot saved to " + screenshotFile.getAbsolutePath()); + Logger.INSTANCE.d("Screenshot saved to " + screenshotFile.getAbsolutePath()); } catch (final IOException e) { subscriber.onError(e); } finally { diff --git a/instacapture/src/main/java/com/tarek360/instacapture/ActivityReferenceManager.java b/instacapture/src/main/java/com/tarek360/instacapture/ActivityReferenceManager.java deleted file mode 100644 index f82873d..0000000 --- a/instacapture/src/main/java/com/tarek360/instacapture/ActivityReferenceManager.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.tarek360.instacapture; - -import android.app.Activity; -import android.os.Build; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; - -import java.lang.ref.WeakReference; - -/** - * Created by tarek on 5/17/16. - */ -public final class ActivityReferenceManager { - - @Nullable - private WeakReference mActivity; - - public void setActivity(@NonNull final Activity activity) { - this.mActivity = new WeakReference<>(activity); - } - - @Nullable - public Activity getValidatedActivity() { - if (mActivity == null) { - return null; - } - - final Activity activity = mActivity.get(); - if (!isActivityValid(activity)) { - return null; - } - - return activity; - } - - private boolean isActivityValid(@Nullable final Activity activity) { - if (activity == null) { - return false; - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - return !activity.isFinishing() && !activity.isDestroyed(); - } else { - return !activity.isFinishing(); - } - } -} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/ActivityReferenceManager.kt b/instacapture/src/main/java/com/tarek360/instacapture/ActivityReferenceManager.kt new file mode 100644 index 0000000..7ae803c --- /dev/null +++ b/instacapture/src/main/java/com/tarek360/instacapture/ActivityReferenceManager.kt @@ -0,0 +1,44 @@ +package com.tarek360.instacapture + +import android.app.Activity +import android.os.Build + +import java.lang.ref.WeakReference + +/** + * Created by tarek on 5/17/16. + */ +class ActivityReferenceManager { + + private var mActivity: WeakReference? = null + + fun setActivity(activity: Activity) { + this.mActivity = WeakReference(activity) + } + + val validatedActivity: Activity? + get() { + if (mActivity == null) { + return null + } + + val activity = mActivity!!.get() + if (!isActivityValid(activity)) { + return null + } + + return activity + } + + private fun isActivityValid(activity: Activity?): Boolean { + if (activity == null) { + return false + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + return !activity.isFinishing && !activity.isDestroyed + } else { + return !activity.isFinishing + } + } +} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/Instacapture.java b/instacapture/src/main/java/com/tarek360/instacapture/Instacapture.java deleted file mode 100644 index 2f05d43..0000000 --- a/instacapture/src/main/java/com/tarek360/instacapture/Instacapture.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.tarek360.instacapture; - -import android.app.Activity; -import android.graphics.Bitmap; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.view.View; - -import com.tarek360.instacapture.exception.ActivityNotRunningException; -import com.tarek360.instacapture.listener.ScreenCaptureListener; -import com.tarek360.instacapture.screenshot.ScreenshotProvider; -import com.tarek360.instacapture.utility.Logger; - -import rx.Observable; -import rx.Subscriber; -import rx.android.schedulers.AndroidSchedulers; - -/** - * Created by tarek on 5/20/17. - */ - -public class Instacapture { - - private static final String MESSAGE_IS_ACTIVITY_RUNNING = "Is your activity running?"; - private static final String ERROR_SCREENSHOT_CAPTURE_FAILED = "Screenshot capture failed"; - - public static void capture(@NonNull Activity activity, - @NonNull final ScreenCaptureListener screenCaptureListener, - @Nullable View... ignoredViews) { - - screenCaptureListener.onCaptureStarted(); - - captureRx(activity, ignoredViews).subscribe(new Subscriber() { - @Override - public void onCompleted() { - } - - @Override - public void onError(final Throwable e) { - Logger.e(ERROR_SCREENSHOT_CAPTURE_FAILED); - Logger.printStackTrace(e); - screenCaptureListener.onCaptureFailed(e); - } - - @Override - public void onNext(final Bitmap bitmap) { - screenCaptureListener.onCaptureComplete(bitmap); - } - }); - - } - - public static Observable captureRx(@NonNull Activity activity, - @Nullable View... ignoredViews) { - - ActivityReferenceManager activityReferenceManager = new ActivityReferenceManager(); - activityReferenceManager.setActivity(activity); - - final Activity validatedActivity = activityReferenceManager.getValidatedActivity(); - if (validatedActivity == null) { - return Observable.error(new ActivityNotRunningException(MESSAGE_IS_ACTIVITY_RUNNING)); - } - - ScreenshotProvider screenshotProvider = new ScreenshotProvider(); - - return screenshotProvider.getScreenshotBitmap(activity, ignoredViews) - .observeOn(AndroidSchedulers.mainThread()); - } - - /** - * Get single tone instance. - * - * @param activity . - * @return Instacapture single tone instance. - */ - - /** - * Enable logging or disable it. - * - * @param enable set it true to enable logging. - */ - public static void enableLogging(boolean enable) { - if (enable) { - Logger.enable(); - } else { - Logger.disable(); - } - } - -} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/Instacapture.kt b/instacapture/src/main/java/com/tarek360/instacapture/Instacapture.kt new file mode 100644 index 0000000..126245a --- /dev/null +++ b/instacapture/src/main/java/com/tarek360/instacapture/Instacapture.kt @@ -0,0 +1,83 @@ +package com.tarek360.instacapture + +import android.app.Activity +import android.graphics.Bitmap +import android.view.View + +import com.tarek360.instacapture.exception.ActivityNotRunningException +import com.tarek360.instacapture.listener.ScreenCaptureListener +import com.tarek360.instacapture.screenshot.ScreenshotProvider +import com.tarek360.instacapture.utility.Logger + +import rx.Observable +import rx.Subscriber +import rx.android.schedulers.AndroidSchedulers + +/** + * Created by tarek on 5/20/17. + */ + +object Instacapture { + + private val MESSAGE_IS_ACTIVITY_RUNNING = "Is your activity running?" + private val ERROR_SCREENSHOT_CAPTURE_FAILED = "Screenshot capture failed" + + fun capture(activity: Activity, + screenCaptureListener: ScreenCaptureListener, + vararg ignoredViews: View) { + + screenCaptureListener.onCaptureStarted() + + captureRx(activity, *ignoredViews).subscribe(object : Subscriber() { + override fun onCompleted() {} + + override fun onError(e: Throwable) { + Logger.e(ERROR_SCREENSHOT_CAPTURE_FAILED) + Logger.printStackTrace(e) + screenCaptureListener.onCaptureFailed(e) + } + + override fun onNext(bitmap: Bitmap) { + screenCaptureListener.onCaptureComplete(bitmap) + } + }) + + } + + fun captureRx(activity: Activity, + vararg ignoredViews: View): Observable { + + val activityReferenceManager = ActivityReferenceManager() + activityReferenceManager.setActivity(activity) + + val validatedActivity = activityReferenceManager.validatedActivity ?: + return Observable.error(ActivityNotRunningException(MESSAGE_IS_ACTIVITY_RUNNING)) + + val screenshotProvider = ScreenshotProvider() + + return screenshotProvider.getScreenshotBitmap(validatedActivity, + ignoredViews.asList().toTypedArray()).observeOn(AndroidSchedulers.mainThread()) + } + + /** + * Get single tone instance. + + * @param activity . + * * + * @return Instacapture single tone instance. + */ + + /** + * Enable logging or disable it. + + * @param enable set it true to enable logging. + */ + fun enableLogging(enable: Boolean) { + if (enable) { + Logger.enable() + } else { + Logger.disable() + } + } + +} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/InstacaptureConfiguration.java b/instacapture/src/main/java/com/tarek360/instacapture/InstacaptureConfiguration.java deleted file mode 100644 index 79e40b0..0000000 --- a/instacapture/src/main/java/com/tarek360/instacapture/InstacaptureConfiguration.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.tarek360.instacapture; - -/** - * Created by tarek on 5/17/16. - */ -class InstacaptureConfiguration { - - final boolean logging; - - private InstacaptureConfiguration(final Builder builder) { - - logging = builder.logging; - } - - public static InstacaptureConfiguration createDefault() { - return new Builder().build(); - } - - /** - * Builder for {@link InstacaptureConfiguration} - */ - public static class Builder { - - private boolean logging; - - public Builder logging(boolean logging) { - this.logging = logging; - return this; - } - - /** - * Builds configured {@link InstacaptureConfiguration} object - */ - public InstacaptureConfiguration build() { - return new InstacaptureConfiguration(this); - } - } -} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/exception/ActivityNotRunningException.java b/instacapture/src/main/java/com/tarek360/instacapture/exception/ActivityNotRunningException.java deleted file mode 100644 index d393851..0000000 --- a/instacapture/src/main/java/com/tarek360/instacapture/exception/ActivityNotRunningException.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.tarek360.instacapture.exception; - -/** - * Created by tarek on 5/31/16. - */ - -/** - * This exception is thrown when the Activity is finished or destroyed. - */ -public class ActivityNotRunningException extends RuntimeException { - - public ActivityNotRunningException() { - } - - public ActivityNotRunningException(String name) { - super(name); - } -} - diff --git a/instacapture/src/main/java/com/tarek360/instacapture/exception/ActivityNotRunningException.kt b/instacapture/src/main/java/com/tarek360/instacapture/exception/ActivityNotRunningException.kt new file mode 100644 index 0000000..e4ea640 --- /dev/null +++ b/instacapture/src/main/java/com/tarek360/instacapture/exception/ActivityNotRunningException.kt @@ -0,0 +1,16 @@ +package com.tarek360.instacapture.exception + +/** + * Created by tarek on 5/31/16. + */ + +/** + * This exception is thrown when the Activity is finished or destroyed. + */ +class ActivityNotRunningException : RuntimeException { + + constructor() + + constructor(name: String) : super(name) +} + diff --git a/instacapture/src/main/java/com/tarek360/instacapture/exception/IllegalScreenSizeException.java b/instacapture/src/main/java/com/tarek360/instacapture/exception/IllegalScreenSizeException.java deleted file mode 100644 index b16a4c9..0000000 --- a/instacapture/src/main/java/com/tarek360/instacapture/exception/IllegalScreenSizeException.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.tarek360.instacapture.exception; - -/** - * Created by tarek on 5/17/16. - */ - -/** - * Throw this exception to know thatActivity width or height are <= 0. - */ -public final class IllegalScreenSizeException extends Exception { - - public IllegalScreenSizeException() { - super("Activity width or height are <= 0"); - } -} \ No newline at end of file diff --git a/instacapture/src/main/java/com/tarek360/instacapture/exception/IllegalScreenSizeException.kt b/instacapture/src/main/java/com/tarek360/instacapture/exception/IllegalScreenSizeException.kt new file mode 100644 index 0000000..0bc74c3 --- /dev/null +++ b/instacapture/src/main/java/com/tarek360/instacapture/exception/IllegalScreenSizeException.kt @@ -0,0 +1,10 @@ +package com.tarek360.instacapture.exception + +/** + * Created by tarek on 5/17/16. + */ + +/** + * Throw this exception to know thatActivity width or height are <= 0. + */ +class IllegalScreenSizeException : Exception("Activity width or height are <= 0") \ No newline at end of file diff --git a/instacapture/src/main/java/com/tarek360/instacapture/exception/ScreenCapturingFailedException.java b/instacapture/src/main/java/com/tarek360/instacapture/exception/ScreenCapturingFailedException.java deleted file mode 100644 index a6c5a16..0000000 --- a/instacapture/src/main/java/com/tarek360/instacapture/exception/ScreenCapturingFailedException.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.tarek360.instacapture.exception; - -/** - * Created by tarek on 5/17/16. - */ - -/** - * Throw this exception to know that screen capturing failed. - */ -public final class ScreenCapturingFailedException extends RuntimeException { - - public ScreenCapturingFailedException(Exception e) { - super(e); - } -} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/exception/ScreenCapturingFailedException.kt b/instacapture/src/main/java/com/tarek360/instacapture/exception/ScreenCapturingFailedException.kt new file mode 100644 index 0000000..320ad3c --- /dev/null +++ b/instacapture/src/main/java/com/tarek360/instacapture/exception/ScreenCapturingFailedException.kt @@ -0,0 +1,10 @@ +package com.tarek360.instacapture.exception + +/** + * Created by tarek on 5/17/16. + */ + +/** + * Throw this exception to know that screen capturing failed. + */ +class ScreenCapturingFailedException(e: Exception) : RuntimeException(e) diff --git a/instacapture/src/main/java/com/tarek360/instacapture/listener/ScreenCaptureListener.java b/instacapture/src/main/java/com/tarek360/instacapture/listener/ScreenCaptureListener.kt similarity index 52% rename from instacapture/src/main/java/com/tarek360/instacapture/listener/ScreenCaptureListener.java rename to instacapture/src/main/java/com/tarek360/instacapture/listener/ScreenCaptureListener.kt index 7a65008..50ed2df 100644 --- a/instacapture/src/main/java/com/tarek360/instacapture/listener/ScreenCaptureListener.java +++ b/instacapture/src/main/java/com/tarek360/instacapture/listener/ScreenCaptureListener.kt @@ -1,34 +1,34 @@ -package com.tarek360.instacapture.listener; +package com.tarek360.instacapture.listener -import android.graphics.Bitmap; +import android.graphics.Bitmap /** * Created by tarek on 5/17/16. */ /** - * Listener for image loading process.
- * You can use {@link SimpleScreenCapturingListener} for implementing only needed methods. - * + * Listener for image loading process.

+ * You can use [SimpleScreenCapturingListener] for implementing only needed methods. + * @see SimpleScreenCapturingListener */ -public interface ScreenCaptureListener { +interface ScreenCaptureListener { /** * Is called when screen capturing task was started */ - void onCaptureStarted(); + fun onCaptureStarted() /** * Is called when an error was occurred during screen capturing */ - void onCaptureFailed(Throwable e); + fun onCaptureFailed(e: Throwable) /** * Is called when image is loaded successfully (and displayed in View if one was specified) - * + * @param bitmap Captured screen bitmap */ - void onCaptureComplete(Bitmap bitmap); + fun onCaptureComplete(bitmap: Bitmap) } diff --git a/instacapture/src/main/java/com/tarek360/instacapture/listener/SimpleScreenCapturingListener.java b/instacapture/src/main/java/com/tarek360/instacapture/listener/SimpleScreenCapturingListener.kt similarity index 60% rename from instacapture/src/main/java/com/tarek360/instacapture/listener/SimpleScreenCapturingListener.java rename to instacapture/src/main/java/com/tarek360/instacapture/listener/SimpleScreenCapturingListener.kt index 7934436..bbca785 100644 --- a/instacapture/src/main/java/com/tarek360/instacapture/listener/SimpleScreenCapturingListener.java +++ b/instacapture/src/main/java/com/tarek360/instacapture/listener/SimpleScreenCapturingListener.kt @@ -1,6 +1,6 @@ -package com.tarek360.instacapture.listener; +package com.tarek360.instacapture.listener -import android.graphics.Bitmap; +import android.graphics.Bitmap /** * Created by tarek on 5/17/16. @@ -10,34 +10,31 @@ * A convenient class to extend when you only want to listen for a subset of all the screen * capturing * events. This implements all methods in the - * {@link ScreenCaptureListener} but does nothing. + * [ScreenCaptureListener] but does nothing. */ -public class SimpleScreenCapturingListener implements ScreenCaptureListener { +open class SimpleScreenCapturingListener : ScreenCaptureListener { /** * Is called when screen capturing task was started */ - @Override - public void onCaptureStarted() { + override fun onCaptureStarted() { // Empty implementation } /** * Is called when an error was occurred during screen capturing. */ - @Override - public void onCaptureFailed(Throwable e) { + override fun onCaptureFailed(e: Throwable) { // Empty implementation } /** * Is called when screen is captured successfully. - * + * @param bitmap Captured screen bitmap */ - @Override - public void onCaptureComplete(Bitmap bitmap) { + override fun onCaptureComplete(bitmap: Bitmap) { // Empty implementation } } diff --git a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/FieldHelper.java b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/FieldHelper.java deleted file mode 100644 index 2b90151..0000000 --- a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/FieldHelper.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.tarek360.instacapture.screenshot; - -import android.app.Activity; -import android.os.Build; -import android.view.View; -import android.view.WindowManager; - -import com.tarek360.instacapture.exception.ScreenCapturingFailedException; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by tarek on 5/18/16. - */ -class FieldHelper { - - private final static String FIELD_NAME_WINDOW_MANAGER = "mWindowManager"; - private final static String FIELD_NAME_GLOBAL = "mGlobal"; - private final static String FIELD_NAME_ROOTS = "mRoots"; - private final static String FIELD_NAME_PARAMS = "mParams"; - private final static String FIELD_NAME_VIEW = "mView"; - - private FieldHelper() { - } - - public static List getRootViews(Activity activity) { - List rootViews = new ArrayList<>(); - - Object windowManager; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - windowManager = getFieldValue(FIELD_NAME_GLOBAL, activity.getWindowManager()); - } else { - windowManager = getFieldValue(FIELD_NAME_WINDOW_MANAGER, activity.getWindowManager()); - } - - Object rootObjects = getFieldValue(FIELD_NAME_ROOTS, windowManager); - Object paramsObject = getFieldValue(FIELD_NAME_PARAMS, windowManager); - - Object[] viewRoots; - WindowManager.LayoutParams[] params; - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - viewRoots = ((List) rootObjects).toArray(); - List paramsList = (List) paramsObject; - params = paramsList.toArray(new WindowManager.LayoutParams[paramsList.size()]); - } else { - viewRoots = (Object[]) rootObjects; - params = (WindowManager.LayoutParams[]) paramsObject; - } - - for (int i = 0; i < viewRoots.length; i++) { - View view = (View) getFieldValue(FIELD_NAME_VIEW, viewRoots[i]); - if (view.getVisibility() == View.VISIBLE) { - rootViews.add(new RootViewInfo(view, params[i])); - } - } - - return rootViews; - } - - private static Object getFieldValue(String fieldName, Object target) { - - try { - Field field = target.getClass().getDeclaredField(fieldName); - field.setAccessible(true); - return field.get(target); - } catch (Exception e) { - throw new ScreenCapturingFailedException(e); - } - } -} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/FieldHelper.kt b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/FieldHelper.kt new file mode 100644 index 0000000..625ec36 --- /dev/null +++ b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/FieldHelper.kt @@ -0,0 +1,69 @@ +package com.tarek360.instacapture.screenshot + +import android.app.Activity +import android.os.Build +import android.view.View +import android.view.WindowManager + +import com.tarek360.instacapture.exception.ScreenCapturingFailedException + +import java.util.ArrayList + +/** + * Created by tarek on 5/18/16. + */ +internal object FieldHelper { + + private val FIELD_NAME_WINDOW_MANAGER = "mWindowManager" + private val FIELD_NAME_GLOBAL = "mGlobal" + private val FIELD_NAME_ROOTS = "mRoots" + private val FIELD_NAME_PARAMS = "mParams" + private val FIELD_NAME_VIEW = "mView" + + fun getRootViews(activity: Activity): List { + val rootViews = ArrayList() + + val windowManager: Any + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + windowManager = getFieldValue(FIELD_NAME_GLOBAL, activity.windowManager)!! + } else { + windowManager = getFieldValue(FIELD_NAME_WINDOW_MANAGER, activity.windowManager)!! + } + + val rootObjects = getFieldValue(FIELD_NAME_ROOTS, windowManager) + val paramsObject = getFieldValue(FIELD_NAME_PARAMS, windowManager) + + val viewRoots: Array + val params: Array + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + viewRoots = (rootObjects as List<*>).toTypedArray() + val paramsList = paramsObject as List + params = paramsList.toTypedArray() + } else { + viewRoots = rootObjects as Array + params = paramsObject as Array + } + + for (i in viewRoots.indices) { + val view = getFieldValue(FIELD_NAME_VIEW, viewRoots[i]) as View + if (view.visibility == View.VISIBLE) { + rootViews.add(RootViewInfo(view, params[i])) + } + } + + return rootViews + } + + private fun getFieldValue(fieldName: String, target: Any?): Any? { + + try { + val field = target?.javaClass?.getDeclaredField(fieldName) + field?.isAccessible = true + return field?.get(target) + } catch (e: Exception) { + throw ScreenCapturingFailedException(e) + } + + } +} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/RootViewInfo.kt b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/RootViewInfo.kt index 7606967..c118fc2 100644 --- a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/RootViewInfo.kt +++ b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/RootViewInfo.kt @@ -6,7 +6,7 @@ import android.view.WindowManager /** * Created by tarek on 5/18/16. */ -class RootViewInfo(val view: View, val layoutParams: WindowManager.LayoutParams) { +class RootViewInfo(val view: View, val layoutParams: WindowManager.LayoutParams?) { val top: Int val left: Int diff --git a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotProvider.java b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotProvider.java deleted file mode 100644 index fd35855..0000000 --- a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotProvider.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.tarek360.instacapture.screenshot; - -import android.app.Activity; -import android.graphics.Bitmap; -import android.support.annotation.NonNull; -import android.view.View; - -import rx.Observable; - -/** - * Created by tarek on 5/17/16. - */ -public class ScreenshotProvider { - - public Observable getScreenshotBitmap(@NonNull final Activity activity, - final View[] removedViews) { - - return ViewsBitmapObservable.get(activity, removedViews); - } -} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotProvider.kt b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotProvider.kt new file mode 100644 index 0000000..56509db --- /dev/null +++ b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotProvider.kt @@ -0,0 +1,18 @@ +package com.tarek360.instacapture.screenshot + +import android.app.Activity +import android.graphics.Bitmap +import android.view.View + +import rx.Observable + +/** + * Created by tarek on 5/17/16. + */ +class ScreenshotProvider { + + fun getScreenshotBitmap(activity: Activity, removedViews: Array): Observable { + + return ViewsBitmapObservable[activity, removedViews] + } +} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotTaker.java b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotTaker.java deleted file mode 100644 index 53f95a6..0000000 --- a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotTaker.java +++ /dev/null @@ -1,232 +0,0 @@ -package com.tarek360.instacapture.screenshot; - -import android.app.Activity; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.PorterDuff; -import android.graphics.PorterDuffXfermode; -import android.opengl.GLES20; -import android.opengl.GLSurfaceView; -import android.os.Build; -import android.support.annotation.RequiresApi; -import android.view.TextureView; -import android.view.View; -import android.view.ViewGroup; -import android.view.WindowManager; - -import com.tarek360.instacapture.exception.ScreenCapturingFailedException; -import com.tarek360.instacapture.utility.Logger; - -import java.nio.IntBuffer; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CountDownLatch; - -import javax.microedition.khronos.egl.EGL10; -import javax.microedition.khronos.egl.EGLContext; -import javax.microedition.khronos.opengles.GL10; - -/** - * Created by tarek on 5/17/16. - */ -public final class ScreenshotTaker { - - private ScreenshotTaker() { - } - - /** - * Capture screenshot for the current activity and return bitmap of it. - * - * @param activity current activity. - * @param ignoredViews from the screenshot. - * @return Bitmap of screenshot. - * @throws ScreenCapturingFailedException if unexpected error is occurred during capturing - * screenshot - */ - public static Bitmap getScreenshotBitmap(Activity activity, View[] ignoredViews) { - if (activity == null) { - throw new IllegalArgumentException("Parameter activity cannot be null."); - } - - final List viewRoots = FieldHelper.getRootViews(activity); - Logger.d("viewRoots count: " + viewRoots.size()); - View main = activity.getWindow().getDecorView(); - - final Bitmap bitmap; - try { - bitmap = Bitmap.createBitmap(main.getWidth(), main.getHeight(), Bitmap.Config.ARGB_8888); - } catch (final IllegalArgumentException e) { - return null; - } - - drawRootsToBitmap(viewRoots, bitmap, ignoredViews); - - return bitmap; - } - - //static int count = 0 ; - private static void drawRootsToBitmap(List viewRoots, Bitmap bitmap, - View[] ignoredViews) { - //count = 0; - for (RootViewInfo rootData : viewRoots) { - drawRootToBitmap(rootData, bitmap, ignoredViews); - } - } - - private static void drawRootToBitmap(final RootViewInfo rootViewInfo, Bitmap bitmap, - View[] ignoredViews) { - - // support dim screen - if ((rootViewInfo.getLayoutParams().flags & WindowManager.LayoutParams.FLAG_DIM_BEHIND) - == WindowManager.LayoutParams.FLAG_DIM_BEHIND) { - Canvas dimCanvas = new Canvas(bitmap); - - int alpha = (int) (255 * rootViewInfo.getLayoutParams().dimAmount); - dimCanvas.drawARGB(alpha, 0, 0, 0); - } - - final Canvas canvas = new Canvas(bitmap); - canvas.translate(rootViewInfo.getLeft(), rootViewInfo.getTop()); - - int[] ignoredViewsVisibility = null; - if (ignoredViews != null) { - ignoredViewsVisibility = new int[ignoredViews.length]; - } - - if (ignoredViews != null) { - for (int i = 0; i < ignoredViews.length; i++) { - if (ignoredViews[i] != null) { - ignoredViewsVisibility[i] = ignoredViews[i].getVisibility(); - ignoredViews[i].setVisibility(View.INVISIBLE); - } - } - } - - rootViewInfo.getView().draw(canvas); - //Draw undrawable views - drawUnDrawableViews(rootViewInfo.getView(), canvas); - - if (ignoredViews != null) { - for (int i = 0; i < ignoredViews.length; i++) { - if (ignoredViews[i] != null) { - ignoredViews[i].setVisibility(ignoredViewsVisibility[i]); - } - } - } - } - - private static ArrayList drawUnDrawableViews(View v, Canvas canvas) { - - if (!(v instanceof ViewGroup)) { - ArrayList viewArrayList = new ArrayList<>(); - viewArrayList.add(v); - return viewArrayList; - } - - ArrayList result = new ArrayList<>(); - - ViewGroup viewGroup = (ViewGroup) v; - for (int i = 0; i < viewGroup.getChildCount(); i++) { - - View child = viewGroup.getChildAt(i); - - ArrayList viewArrayList = new ArrayList<>(); - viewArrayList.add(v); - viewArrayList.addAll(drawUnDrawableViews(child, canvas)); - - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH - && child instanceof TextureView) { - drawTextureView((TextureView) child, canvas); - } - - if (child instanceof GLSurfaceView) { - drawGLSurfaceView((GLSurfaceView) child, canvas); - } - - result.addAll(viewArrayList); - } - return result; - } - - private static void drawGLSurfaceView(GLSurfaceView surfaceView, Canvas canvas) { - Logger.d("Drawing GLSurfaceView"); - - if (surfaceView.getWindowToken() != null) { - int[] location = new int[2]; - - surfaceView.getLocationOnScreen(location); - final int width = surfaceView.getWidth(); - final int height = surfaceView.getHeight(); - - final int x = 0; - final int y = 0; - int[] b = new int[width * (y + height)]; - - final IntBuffer ib = IntBuffer.wrap(b); - ib.position(0); - - //To wait for the async call to finish before going forward - final CountDownLatch countDownLatch = new CountDownLatch(1); - surfaceView.queueEvent(new Runnable() { - @Override - public void run() { - EGL10 egl = (EGL10) EGLContext.getEGL(); - egl.eglWaitGL(); - GL10 gl = (GL10) egl.eglGetCurrentContext().getGL(); - - gl.glFinish(); - - try { - Thread.sleep(200); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - gl.glReadPixels(x, 0, width, y + height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib); - countDownLatch.countDown(); - } - }); - - try { - countDownLatch.await(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - int[] bt = new int[width * height]; - int i = 0; - for (int k = 0; i < height; k++) { - for (int j = 0; j < width; j++) { - int pix = b[(i * width + j)]; - int pb = pix >> 16 & 0xFF; - int pr = pix << 16 & 0xFF0000; - int pix1 = pix & 0xFF00FF00 | pr | pb; - bt[((height - k - 1) * width + j)] = pix1; - } - i++; - } - - Bitmap sb = Bitmap.createBitmap(bt, width, height, Bitmap.Config.ARGB_8888); - Paint paint = new Paint(); - paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)); - canvas.drawBitmap(sb, location[0], location[1], paint); - sb.recycle(); - } - } - - @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH) - private static void drawTextureView(TextureView textureView, Canvas canvas) { - Logger.d("Drawing TextureView"); - - int[] textureViewLocation = new int[2]; - textureView.getLocationOnScreen(textureViewLocation); - Bitmap textureViewBitmap = textureView.getBitmap(); - if (textureViewBitmap != null) { - Paint paint = new Paint(); - paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)); - canvas.drawBitmap(textureViewBitmap, textureViewLocation[0], textureViewLocation[1], paint); - textureViewBitmap.recycle(); - } - } -} - diff --git a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotTaker.kt b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotTaker.kt new file mode 100644 index 0000000..9b9d97f --- /dev/null +++ b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ScreenshotTaker.kt @@ -0,0 +1,223 @@ +package com.tarek360.instacapture.screenshot + +import android.app.Activity +import android.graphics.* +import android.opengl.GLES20 +import android.opengl.GLSurfaceView +import android.os.Build +import android.support.annotation.RequiresApi +import android.view.TextureView +import android.view.View +import android.view.ViewGroup +import android.view.WindowManager +import com.tarek360.instacapture.exception.ScreenCapturingFailedException +import com.tarek360.instacapture.utility.Logger +import java.nio.IntBuffer +import java.util.* +import java.util.concurrent.CountDownLatch +import javax.microedition.khronos.egl.EGL10 +import javax.microedition.khronos.egl.EGLContext +import javax.microedition.khronos.opengles.GL10 + +/** + * Created by tarek on 5/17/16. + */ +object ScreenshotTaker { + + /** + * Capture screenshot for the current activity and return bitmap of it. + + * @param activity current activity. + * * + * @param ignoredViews from the screenshot. + * * + * @return Bitmap of screenshot. + * * + * @throws ScreenCapturingFailedException if unexpected error is occurred during capturing + * * screenshot + */ + fun getScreenshotBitmap(activity: Activity?, ignoredViews: Array?): Bitmap? { + if (activity == null) { + throw IllegalArgumentException("Parameter activity cannot be null.") + } + + val paramsObject = Any() + + if (paramsObject is List<*>) { + + } + + val viewRoots = FieldHelper.getRootViews(activity) + Logger.d("viewRoots count: " + viewRoots.size) + val main = activity.window.decorView + + val bitmap: Bitmap + try { + bitmap = Bitmap.createBitmap(main.width, main.height, Bitmap.Config.ARGB_8888) + } catch (e: IllegalArgumentException) { + return null + } + + drawRootsToBitmap(viewRoots, bitmap, ignoredViews) + + return bitmap + } + + //static int count = 0 ; + private fun drawRootsToBitmap(viewRoots: List, bitmap: Bitmap, + ignoredViews: Array?) { + //count = 0; + for (rootData in viewRoots) { + drawRootToBitmap(rootData, bitmap, ignoredViews) + } + } + + private fun drawRootToBitmap(rootViewInfo: RootViewInfo, bitmap: Bitmap, + ignoredViews: Array?) { + + // support dim screen + if (rootViewInfo.layoutParams!!.flags and WindowManager.LayoutParams.FLAG_DIM_BEHIND + == WindowManager.LayoutParams.FLAG_DIM_BEHIND) { + val dimCanvas = Canvas(bitmap) + + val alpha = (255 * rootViewInfo.layoutParams.dimAmount).toInt() + dimCanvas.drawARGB(alpha, 0, 0, 0) + } + + val canvas = Canvas(bitmap) + canvas.translate(rootViewInfo.left.toFloat(), rootViewInfo.top.toFloat()) + + if (ignoredViews != null) { + + val ignoredViewsVisibility = IntArray(ignoredViews.size) + + for (i in ignoredViews.indices) { + ignoredViewsVisibility[i] = ignoredViews[i].visibility + ignoredViews[i].visibility = View.INVISIBLE + } + + rootViewInfo.view.draw(canvas) + //Draw undrawable views + drawUnDrawableViews(rootViewInfo.view, canvas) + + for (i in ignoredViews.indices) { + ignoredViews[i].visibility = ignoredViewsVisibility[i] + + } + + } + } + + private fun drawUnDrawableViews(v: View, canvas: Canvas): ArrayList { + + if (v !is ViewGroup) { + val viewArrayList = ArrayList() + viewArrayList.add(v) + return viewArrayList + } + + val result = ArrayList() + + val viewGroup = v + for (i in 0..viewGroup.childCount - 1) { + + val child = viewGroup.getChildAt(i) + + val viewArrayList = ArrayList() + viewArrayList.add(v) + viewArrayList.addAll(drawUnDrawableViews(child, canvas)) + + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && child is TextureView) { + drawTextureView(child, canvas) + } + + if (child is GLSurfaceView) { + drawGLSurfaceView(child, canvas) + } + + result.addAll(viewArrayList) + } + return result + } + + private fun drawGLSurfaceView(surfaceView: GLSurfaceView, canvas: Canvas) { + Logger.d("Drawing GLSurfaceView") + + if (surfaceView.windowToken != null) { + val location = IntArray(2) + + surfaceView.getLocationOnScreen(location) + val width = surfaceView.width + val height = surfaceView.height + + val x = 0 + val y = 0 + val b = IntArray(width * (y + height)) + + val ib = IntBuffer.wrap(b) + ib.position(0) + + //To wait for the async call to finish before going forward + val countDownLatch = CountDownLatch(1) + surfaceView.queueEvent { + val egl = EGLContext.getEGL() as EGL10 + egl.eglWaitGL() + val gl = egl.eglGetCurrentContext().gl as GL10 + + gl.glFinish() + + try { + Thread.sleep(200) + } catch (e: InterruptedException) { + e.printStackTrace() + } + + gl.glReadPixels(x, 0, width, y + height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib) + countDownLatch.countDown() + } + + try { + countDownLatch.await() + } catch (e: InterruptedException) { + e.printStackTrace() + } + + val bt = IntArray(width * height) + var i = 0 + var k = 0 + while (i < height) { + for (j in 0..width - 1) { + val pix = b[i * width + j] + val pb = pix shr 16 and 0xFF + val pr = pix shl 16 and 0xFF0000 + val pix1 = pix and 0xFF00FF00.toInt() or pr or pb + bt[(height - k - 1) * width + j] = pix1 + } + i++ + k++ + } + + val sb = Bitmap.createBitmap(bt, width, height, Bitmap.Config.ARGB_8888) + val paint = Paint() + paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_ATOP) + canvas.drawBitmap(sb, location[0].toFloat(), location[1].toFloat(), paint) + sb.recycle() + } + } + + @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH) + private fun drawTextureView(textureView: TextureView, canvas: Canvas) { + Logger.d("Drawing TextureView") + + val textureViewLocation = IntArray(2) + textureView.getLocationOnScreen(textureViewLocation) + val textureViewBitmap = textureView.bitmap + if (textureViewBitmap != null) { + val paint = Paint() + paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_ATOP) + canvas.drawBitmap(textureViewBitmap, textureViewLocation[0].toFloat(), textureViewLocation[1].toFloat(), paint) + textureViewBitmap.recycle() + } + } +} + diff --git a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ViewsBitmapObservable.java b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ViewsBitmapObservable.java deleted file mode 100644 index 98a3aee..0000000 --- a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ViewsBitmapObservable.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.tarek360.instacapture.screenshot; - -import android.app.Activity; -import android.graphics.Bitmap; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.view.View; - -import com.tarek360.instacapture.exception.IllegalScreenSizeException; - -import rx.Observable; -import rx.functions.Func0; - -/** - * Created by tarek on 5/17/16. - */ -public final class ViewsBitmapObservable { - - private ViewsBitmapObservable() { - - } - - @NonNull - public static Observable get(@NonNull final Activity activity, - @Nullable final View[] removedViews) { - - return Observable.defer(new Func0>() { - @Override - public Observable call() { - - Bitmap screenBitmap = ScreenshotTaker.getScreenshotBitmap(activity, removedViews); - if (screenBitmap != null) { - return Observable.just(screenBitmap); - } else { - return Observable.error(new IllegalScreenSizeException()); - } - } - }); - } -} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ViewsBitmapObservable.kt b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ViewsBitmapObservable.kt new file mode 100644 index 0000000..c09b95a --- /dev/null +++ b/instacapture/src/main/java/com/tarek360/instacapture/screenshot/ViewsBitmapObservable.kt @@ -0,0 +1,29 @@ +package com.tarek360.instacapture.screenshot + +import android.app.Activity +import android.graphics.Bitmap +import android.view.View + +import com.tarek360.instacapture.exception.IllegalScreenSizeException + +import rx.Observable +import rx.functions.Func0 + +/** + * Created by tarek on 5/17/16. + */ +object ViewsBitmapObservable { + + operator fun get(activity: Activity, + removedViews: Array?): Observable { + + return Observable.defer { + val screenBitmap = ScreenshotTaker.getScreenshotBitmap(activity, removedViews) + if (screenBitmap != null) { + Observable.just(screenBitmap) + } else { + Observable.error(IllegalScreenSizeException()) + } + } + } +} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/utility/Logger.java b/instacapture/src/main/java/com/tarek360/instacapture/utility/Logger.java deleted file mode 100644 index 9b512c1..0000000 --- a/instacapture/src/main/java/com/tarek360/instacapture/utility/Logger.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.tarek360.instacapture.utility; - -import android.support.annotation.NonNull; -import android.util.Log; - -/** - * Created by tarek on 5/17/16. - */ -public final class Logger { - - private static final String TAG = "Instacapture"; - - private static boolean enable; - - private Logger() { - } - - public static void enable() { - enable = true; - } - - public static void disable() { - enable = false; - } - - public static void d(@NonNull final CharSequence message) { - if (enable) { - Log.d(TAG, message.toString()); - } - } - - public static void w(@NonNull final CharSequence message) { - if (enable) { - Log.w(TAG, message.toString()); - } - } - - public static void e(@NonNull final CharSequence message) { - if (enable) { - Log.e(TAG, message.toString()); - } - } - - public static void printStackTrace(@NonNull final Throwable throwable) { - if (enable) { - Log.e(TAG, "Logging caught exception", throwable); - } - } -} diff --git a/instacapture/src/main/java/com/tarek360/instacapture/utility/Logger.kt b/instacapture/src/main/java/com/tarek360/instacapture/utility/Logger.kt new file mode 100644 index 0000000..7e72798 --- /dev/null +++ b/instacapture/src/main/java/com/tarek360/instacapture/utility/Logger.kt @@ -0,0 +1,45 @@ +package com.tarek360.instacapture.utility + +import android.util.Log + +/** + * Created by tarek on 5/17/16. + */ +object Logger { + + private val TAG = "Instacapture" + + private var enable: Boolean = false + + fun enable() { + enable = true + } + + fun disable() { + enable = false + } + + fun d(message: CharSequence) { + if (enable) { + Log.d(TAG, message.toString()) + } + } + + fun w(message: CharSequence) { + if (enable) { + Log.w(TAG, message.toString()) + } + } + + fun e(message: CharSequence) { + if (enable) { + Log.e(TAG, message.toString()) + } + } + + fun printStackTrace(throwable: Throwable) { + if (enable) { + Log.e(TAG, "Logging caught exception", throwable) + } + } +}