Skip to content

Commit

Permalink
rewrite Instacapture in Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Tarek committed May 20, 2017
1 parent 0411134 commit ed5e906
Show file tree
Hide file tree
Showing 27 changed files with 571 additions and 665 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/tarek360/sample/BaseSampleActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {

Expand All @@ -58,7 +58,7 @@ public void call(File file) {
}, ignoredViews);


Instacapture.captureRx(this, ignoredViews).subscribe(new Action1<Bitmap>() {
Instacapture.INSTANCE.captureRx(this, ignoredViews).subscribe(new Action1<Bitmap>() {
@Override
public void call(Bitmap bitmap) {

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/tarek360/sample/utility/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void call(final Subscriber<? super File> 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 {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<Activity>? = 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
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<Bitmap>() {
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<Bitmap> {

val activityReferenceManager = ActivityReferenceManager()
activityReferenceManager.setActivity(activity)

val validatedActivity = activityReferenceManager.validatedActivity ?:
return Observable.error<Bitmap>(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()
}
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit ed5e906

Please sign in to comment.