Skip to content

Commit

Permalink
showing ads now on right place, preparation for release 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
rladstaetter committed Jul 20, 2016
1 parent 07e6df5 commit 191524f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 62 deletions.
1 change: 0 additions & 1 deletion sudoku-android/proguard.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class org.opencv.android.JavaCameraView { *; }
-keep public class com.google.android.gms.ads.Adview { *; }
-keep class scala.collection.SeqLike { public protected *; }
-keep public class net.ladstatt.**
14 changes: 10 additions & 4 deletions sudoku-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.android.com/apk/res/android">

<!-- to access camera and some memory -->
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<!-- for google ads -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Expand All @@ -19,9 +22,6 @@

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

<activity
android:name=".SudokuCapturer"
Expand All @@ -33,7 +33,13 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

</activity> s

<activity
android:name="com.google.android.gms.ads.AdActivity"
android:theme="@android:style/Theme.Translucent"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
android:layout_height="match_parent"
android:id="@+id/mainLayout">

<!--
<!--
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-1727389366588084/4496274256"/>
-->


-->
<org.opencv.android.JavaCameraView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sudoku"/>

<Button
Expand All @@ -27,5 +25,15 @@
android:layout_gravity="center_horizontal|bottom"
android:paddingBottom="10dp"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/use_in_landscape_mode"
android:id="@+id/use_in_landscape"
android:layout_gravity="center_horizontal|top"
android:visibility="visible"
android:background="#000000"/>


</FrameLayout>
1 change: 1 addition & 0 deletions sudoku-android/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

<string name="app_name">Sudoku Capturer</string>
<string name="button_rescan">neues Sudoku lösen</string>
<string name="use_in_landscape_mode">Handy quer halten bitte …</string>

</resources>
3 changes: 2 additions & 1 deletion sudoku-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<resources>
<string name="app_name">Sudoku Capturer</string>
<string name="button_rescan">rescan</string>

<string name="use_in_landscape_mode">Use in Landscape mode</string>
<string name="adunit_id">ca-app-pub-1727389366588084/4496274256</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import _root_.android.app.Activity
import _root_.android.os.{Bundle, Handler, Process}
import _root_.android.util.Log
import _root_.android.view.View.OnClickListener
import _root_.android.view.{Gravity, View, WindowManager}
import _root_.android.widget.{Button, FrameLayout}
import _root_.android.view.{Gravity, View, ViewGroup}
import _root_.android.widget.{Button, FrameLayout, TextView}
import com.google.android.gms.ads.{AdRequest, AdSize, AdView, MobileAds}
import net.ladstatt.sudoku._
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2
Expand Down Expand Up @@ -56,9 +56,13 @@ class SudokuCapturer extends Activity with CvCameraViewListener2 with CanLog {
var frameNr: Int = 0
var solution: Mat = _
var calculationInProgress = false

val defaultLibrary: DigitLibrary = Map().withDefaultValue((Double.MaxValue, None))
val defaultHitCounts: HitCounters = Map().withDefaultValue(Map[Int, Int]().withDefaultValue(0))
val AdUnitId: String = "ca-app-pub-1727389366588084/4496274256"
var adView: AdView = _
var showHint: Boolean = true
var hint: TextView = _

var currState: SudokuState = AndroidOpenCV.DefaultAndroidState

Expand All @@ -68,60 +72,73 @@ class SudokuCapturer extends Activity with CvCameraViewListener2 with CanLog {
TemplateLibrary.templateResource = "templates.csv"
}

def execOnUIThread(f: => Unit): Unit = {
handler.post(new Runnable {
override def run(): Unit = f
})
()
}

/** Called when the activity is first created. */
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)

initAssets()

Log.i(TAG, "called onCreate")
getWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
setContentView(R.layout.sudoku)
//getWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

setContentView(R.layout.sudoku_frame_layout)

MobileAds.initialize(this, AdUnitId)
// val adView = findViewById(R.id.ad_view).asInstanceOf[AdView]



// adView = findViewById(R.id.ad_view).asInstanceOf[AdView]

cameraView = findViewById(R.id.sudoku).asInstanceOf[CameraBridgeViewBase]
cameraView.setCvCameraViewListener(this)
rescanButton = findViewById(R.id.button_rescan).asInstanceOf[Button]
hint = findViewById(R.id.use_in_landscape).asInstanceOf[TextView]

adView = new AdView(this)

rescanButton.setOnClickListener(new OnClickListener {
override def onClick(v: View): Unit = {
// rescanButton.setVisibility(View.GONE)
currState = AndroidOpenCV.DefaultAndroidState
rescanButton.setVisibility(View.GONE)
// adView.setVisibility(View.GONE)
adView.setVisibility(View.GONE)
solution = null

}
})

val adView: AdView = new AdView(this)

adView.setAdSize(AdSize.SMART_BANNER)
adView.setAdUnitId(AdUnitId)
adView.setVisibility(View.VISIBLE)

adView.setForegroundGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP)
// adView = new AdView(this, AdSize.BANNER, AdUnitId)
// adView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP)
// adView.setVisibility(View.VISIBLE)
import FrameLayout.LayoutParams

findViewById(R.id.mainLayout).asInstanceOf[FrameLayout].addView(adView)
val p = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.TOP)

val adRequestBuilder = new AdRequest.Builder()
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
//asdRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// val adUnitTestId: String = "E7CB8BBA8EEEF5723B49B413253C551B"
//adRequestBuilder.addTestDevice(adUnitTestId)
val r = adRequestBuilder.build()

adView.loadAd(r)

handler = new Handler()
rescanButton.setVisibility(View.GONE)
cameraView.enableView()

findViewById(R.id.mainLayout).asInstanceOf[FrameLayout].addView(adView, 2, p)

}

override def onBackPressed(): Unit = {
super.onBackPressed()
//logInfo("backButton pressed")
}

override def onStop(): Unit = {
Expand All @@ -131,54 +148,29 @@ class SudokuCapturer extends Activity with CvCameraViewListener2 with CanLog {

override def onPause(): Unit = {
super.onPause()
//logInfo("onPause called")
if (cameraView != null) cameraView.disableView()
if (adView != null) adView.pause()

}

override def onResume(): Unit = {
super.onResume()
// logInfo("onResume called")
AndroidOpenCV.init()
if (adView != null) adView.resume()
//OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback)
()
}

override def onDestroy(): Unit = {
super.onDestroy()
//logInfo("onDestroy called")
if (cameraView != null) cameraView.disableView()
if (adView != null) adView.destroy()
}


def detectSudoku(inputFrame: CameraBridgeViewBase.CvCameraViewFrame): SudokuResult = {
val frame = inputFrame.rgba()
frameNr = frameNr + 1

val pipeline: FramePipeline = FramePipeline(frame, SParams())

pipeline.detectRectangle match {
case None => SFailure("No rectangle detected", SCandidate(frameNr, pipeline, SRectangle(frame, pipeline.corners, pipeline.corners), currState))
case Some(r) =>
val rectangle: SRectangle = SRectangle(pipeline)
val (sudokuResult, nextState) = Await.result(SCandidate(frameNr, pipeline, rectangle, currState).calc, Duration.Inf)
currState = nextState
sudokuResult
}
}

def execOnUIThread(f: => Unit): Unit = {
handler.post(new Runnable {
override def run(): Unit = f
})
()
}

def onCameraViewStarted(width: Int, height: Int) {
//logInfo(s"onCameraViewStarted (width: $width, height: $height)")
def onCameraViewStarted(width: Int, height: Int): Unit = {
}

def onCameraViewStopped() {
// logInfo("onCameraViewStopped called")
}

def onCameraFrame(inputFrame: CameraBridgeViewBase.CvCameraViewFrame): Mat = {
Expand All @@ -187,21 +179,27 @@ class SudokuCapturer extends Activity with CvCameraViewListener2 with CanLog {
} else {
if (!calculationInProgress) {
calculationInProgress = true
execOnUIThread {
if (showHint) {
hint.setVisibility(View.GONE)
adView.setVisibility(View.VISIBLE)
adView.bringToFront()
}
}
logInfo("starting to find sudoku ...")
val result: Mat =
detectSudoku(inputFrame) match {
case s: SSuccess if s.someSolution.isDefined => {
execOnUIThread({
rescanButton.setVisibility(View.VISIBLE)
// adView.setVisibility(View.VISIBLE)
})
solution = s.someSolution.get.solutionMat
solution
}
case s: SSuccess if s.someSolution.isEmpty => {
s.inputFrame.pipeline.inverted
s.inputFrame.pipeline.frame
}
case e: SFailure => e.inputFrame.pipeline.blurred
case e: SFailure => e.inputFrame.pipeline.grayed
}
calculationInProgress = false
result
Expand All @@ -212,4 +210,20 @@ class SudokuCapturer extends Activity with CvCameraViewListener2 with CanLog {
}
}

def detectSudoku(inputFrame: CameraBridgeViewBase.CvCameraViewFrame): SudokuResult = {
val frame = inputFrame.rgba()
frameNr = frameNr + 1

val pipeline: FramePipeline = FramePipeline(frame, SParams())

pipeline.detectRectangle match {
case None => SFailure("No rectangle detected", SCandidate(frameNr, pipeline, SRectangle(frame, pipeline.corners, pipeline.corners), currState))
case Some(r) =>
val rectangle: SRectangle = SRectangle(pipeline)
val (sudokuResult, nextState) = Await.result(SCandidate(frameNr, pipeline, rectangle, currState).calc, Duration.Inf)
currState = nextState
sudokuResult
}
}

}

0 comments on commit 191524f

Please sign in to comment.