From 1436feba85a615b0632e7ed62abaa4a99d309ecf Mon Sep 17 00:00:00 2001 From: "mazen.rashed@mawdoo3.com" Date: Sun, 13 Sep 2020 15:01:13 +0300 Subject: [PATCH] Migrate to AndroidX Fix Android 28 issue by ask for fine location permission before scanning https://github.com/mazenrashed/Printooth/issues/27 --- .idea/modules.xml | 5 ++- app/build.gradle | 11 +++-- .../example/ExampleInstrumentedTest.kt | 16 +------- .../com/mazenrashed/example/MainActivity.kt | 2 +- .../com/mazenrashed/example/WoosimActivity.kt | 2 +- app/src/main/res/layout/activity_main.xml | 12 +++--- app/src/main/res/layout/activity_woosim.xml | 10 ++--- build.gradle | 4 +- gradle.properties | 2 + gradle/wrapper/gradle-wrapper.properties | 2 +- printooth/build.gradle | 35 ++++++++++++---- .../ExampleInstrumentedTest.kt | 4 +- printooth/src/main/AndroidManifest.xml | 4 +- .../printooth/ui/ScanningActivity.kt | 40 ++++++++++-------- .../src/main/res/layout/activity_scanning.xml | 41 +++---------------- 15 files changed, 87 insertions(+), 103 deletions(-) diff --git a/.idea/modules.xml b/.idea/modules.xml index 63d7d6f..817c7c6 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,10 +3,11 @@ + + + - - diff --git a/app/build.gradle b/app/build.gradle index 21bc93a..15132e3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -14,7 +14,7 @@ android { targetSdkVersion 28 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } buildTypes { release { @@ -27,16 +27,15 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } - } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'com.android.support:appcompat-v7:28.0.0' - implementation 'com.android.support.constraint:constraint-layout:1.1.3' + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.1' testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' implementation project(':printooth') } diff --git a/app/src/androidTest/java/com/mazenrashed/example/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/mazenrashed/example/ExampleInstrumentedTest.kt index 17e164e..bf4653c 100644 --- a/app/src/androidTest/java/com/mazenrashed/example/ExampleInstrumentedTest.kt +++ b/app/src/androidTest/java/com/mazenrashed/example/ExampleInstrumentedTest.kt @@ -1,24 +1,12 @@ package com.mazenrashed.example -import android.support.test.InstrumentationRegistry -import android.support.test.runner.AndroidJUnit4 - -import org.junit.Test +import androidx.test.ext.junit.runners.AndroidJUnit4 import org.junit.runner.RunWith -import org.junit.Assert.* - /** * Instrumented test, which will execute on an Android device. * * See [testing documentation](http://d.android.com/tools/testing). */ @RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getTargetContext() - assertEquals("com.mazenrashed.universalbluetoothprinter", appContext.packageName) - } -} +class ExampleInstrumentedTest diff --git a/app/src/main/java/com/mazenrashed/example/MainActivity.kt b/app/src/main/java/com/mazenrashed/example/MainActivity.kt index 30bb491..604cd91 100644 --- a/app/src/main/java/com/mazenrashed/example/MainActivity.kt +++ b/app/src/main/java/com/mazenrashed/example/MainActivity.kt @@ -3,8 +3,8 @@ package com.mazenrashed.example import android.app.Activity import android.content.Intent import android.os.Bundle -import android.support.v7.app.AppCompatActivity import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity import com.mazenrashed.printooth.Printooth import com.mazenrashed.printooth.data.converter.ArabicConverter import com.mazenrashed.printooth.data.printable.ImagePrintable diff --git a/app/src/main/java/com/mazenrashed/example/WoosimActivity.kt b/app/src/main/java/com/mazenrashed/example/WoosimActivity.kt index 6d66ff0..f6d5b5b 100644 --- a/app/src/main/java/com/mazenrashed/example/WoosimActivity.kt +++ b/app/src/main/java/com/mazenrashed/example/WoosimActivity.kt @@ -3,8 +3,8 @@ package com.mazenrashed.example import android.app.Activity import android.content.Intent import android.os.Bundle -import android.support.v7.app.AppCompatActivity import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity import com.mazenrashed.printooth.Printooth import com.mazenrashed.printooth.data.printable.ImagePrintable import com.mazenrashed.printooth.data.printable.Printable diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 378c509..415251b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,12 +1,12 @@ - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_woosim.xml b/app/src/main/res/layout/activity_woosim.xml index 3668bfe..b3671da 100644 --- a/app/src/main/res/layout/activity_woosim.xml +++ b/app/src/main/res/layout/activity_woosim.xml @@ -1,12 +1,12 @@ - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 92c676c..54198e6 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,14 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.3.31' + ext.kotlin_version = '1.4.0' repositories { google() mavenCentral() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.4.1' + classpath 'com.android.tools.build:gradle:4.0.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle.properties b/gradle.properties index 1487463..dbf9c12 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,6 +6,8 @@ # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. +android.enableJetifier=true +android.useAndroidX=true org.gradle.jvmargs=-Xmx1536m # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 085b201..ced21bf 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip diff --git a/printooth/build.gradle b/printooth/build.gradle index ceebfbb..48cf288 100644 --- a/printooth/build.gradle +++ b/printooth/build.gradle @@ -1,19 +1,22 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-kapt' android { - compileSdkVersion 28 + compileSdkVersion 29 defaultConfig { minSdkVersion 19 - targetSdkVersion 28 + targetSdkVersion 29 versionCode 1 versionName "1.0" + multiDexEnabled true + vectorDrawables.useSupportLibrary = true - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } @@ -23,20 +26,36 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + + + lintOptions { + checkReleaseBuilds false + // Or, if you prefer, you can continue to check for errors in release builds, + // but continue the build even when errors are found: + abortOnError false + } + compileOptions { - sourceCompatibility = '1.8' - targetCompatibility = '1.8' + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + + kotlinOptions { + jvmTarget = JavaVersion.VERSION_1_8.toString() } } dependencies { - implementation 'com.android.support:appcompat-v7:28.0.0' + implementation 'androidx.appcompat:appcompat:1.2.0' testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'io.paperdb:paperdb:2.6' + //Runtime permissions + implementation 'com.afollestad.assent:core:3.0.0-RC4' + implementation 'com.android.support:design:29.0.0' } repositories { mavenCentral() diff --git a/printooth/src/androidTest/java/me/splashco/universalbluethootprinting/ExampleInstrumentedTest.kt b/printooth/src/androidTest/java/me/splashco/universalbluethootprinting/ExampleInstrumentedTest.kt index 6d4aa72..d1a2381 100644 --- a/printooth/src/androidTest/java/me/splashco/universalbluethootprinting/ExampleInstrumentedTest.kt +++ b/printooth/src/androidTest/java/me/splashco/universalbluethootprinting/ExampleInstrumentedTest.kt @@ -1,8 +1,8 @@ package me.splashco.universalbluethootprinting -import android.support.test.InstrumentationRegistry -import android.support.test.runner.AndroidJUnit4 import android.util.Log +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith diff --git a/printooth/src/main/AndroidManifest.xml b/printooth/src/main/AndroidManifest.xml index 589b645..c85b661 100644 --- a/printooth/src/main/AndroidManifest.xml +++ b/printooth/src/main/AndroidManifest.xml @@ -3,9 +3,9 @@ + - + diff --git a/printooth/src/main/java/com/mazenrashed/printooth/ui/ScanningActivity.kt b/printooth/src/main/java/com/mazenrashed/printooth/ui/ScanningActivity.kt index efbfdbb..0926900 100644 --- a/printooth/src/main/java/com/mazenrashed/printooth/ui/ScanningActivity.kt +++ b/printooth/src/main/java/com/mazenrashed/printooth/ui/ScanningActivity.kt @@ -5,8 +5,6 @@ import android.bluetooth.BluetoothDevice import android.content.Context import android.os.Bundle import android.os.Handler -import android.support.v7.app.AppCompatActivity -import android.support.v7.widget.Toolbar import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -14,11 +12,14 @@ import android.widget.ArrayAdapter import android.widget.ImageView import android.widget.TextView import android.widget.Toast -import kotlinx.android.synthetic.main.activity_scanning.* -import com.mazenrashed.printooth.data.DiscoveryCallback -import com.mazenrashed.printooth.utilities.Bluetooth +import androidx.appcompat.app.AppCompatActivity +import com.afollestad.assent.Permission +import com.afollestad.assent.runWithPermissions import com.mazenrashed.printooth.Printooth import com.mazenrashed.printooth.R +import com.mazenrashed.printooth.data.DiscoveryCallback +import com.mazenrashed.printooth.utilities.Bluetooth +import kotlinx.android.synthetic.main.activity_scanning.* class ScanningActivity : AppCompatActivity() { private lateinit var bluetooth: Bluetooth @@ -43,14 +44,14 @@ class ScanningActivity : AppCompatActivity() { bluetooth.setDiscoveryCallback(object : DiscoveryCallback { override fun onDiscoveryStarted() { refreshLayout.isRefreshing = true - (toolbar as Toolbar).title = "Scanning.." + toolbar.title = "Scanning.." devices.clear() devices.addAll(bluetooth.pairedDevices) adapter.notifyDataSetChanged() } override fun onDiscoveryFinished() { - (toolbar as Toolbar).title = if (devices.isNotEmpty()) "Select Printing" else "No devices" + toolbar.title = if (devices.isNotEmpty()) "Select a Printer" else "No devices" refreshLayout.isRefreshing = false } @@ -71,7 +72,7 @@ class ScanningActivity : AppCompatActivity() { override fun onDeviceUnpaired(device: BluetoothDevice) { Toast.makeText(this@ScanningActivity, "Device unpaired", Toast.LENGTH_SHORT).show() - var pairedPrinter = Printooth.getPairedPrinter() + val pairedPrinter = Printooth.getPairedPrinter() if (pairedPrinter != null && pairedPrinter.address == device.address) Printooth.removeCurrentPrinter() devices.remove(device) @@ -89,13 +90,12 @@ class ScanningActivity : AppCompatActivity() { private fun initListeners() { refreshLayout.setOnRefreshListener { bluetooth.startScanning() } printers.setOnItemClickListener { _, _, i, _ -> - var device = devices[i] + val device = devices[i] if (device.bondState == BluetoothDevice.BOND_BONDED) { Printooth.setPrinter(device.name, device.address) setResult(Activity.RESULT_OK) this@ScanningActivity.finish() - } - else if (device.bondState == BluetoothDevice.BOND_NONE) + } else if (device.bondState == BluetoothDevice.BOND_NONE) bluetooth.pair(devices[i]) adapter.notifyDataSetChanged() } @@ -107,12 +107,16 @@ class ScanningActivity : AppCompatActivity() { override fun onStart() { super.onStart() - bluetooth.onStart() - if (!bluetooth.isEnabled) - bluetooth.enable() - Handler().postDelayed({ - bluetooth.startScanning() - }, 1000) + + runWithPermissions(Permission.ACCESS_FINE_LOCATION) { + bluetooth.onStart() + if (!bluetooth.isEnabled) + bluetooth.enable() + Handler().postDelayed({ + bluetooth.startScanning() + }, 1000) + } + } override fun onStop() { @@ -129,7 +133,7 @@ class ScanningActivity : AppCompatActivity() { return devices.size } - override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { return LayoutInflater.from(context) .inflate(R.layout.bluetooth_device_row, parent, false).apply { findViewById(R.id.name).text = if (devices[position].name.isNullOrEmpty()) devices[position].address else devices[position].name diff --git a/printooth/src/main/res/layout/activity_scanning.xml b/printooth/src/main/res/layout/activity_scanning.xml index 74b6688..04004f5 100644 --- a/printooth/src/main/res/layout/activity_scanning.xml +++ b/printooth/src/main/res/layout/activity_scanning.xml @@ -4,16 +4,15 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + android:layout_height="match_parent" /> +