Skip to content

Commit

Permalink
Upgrade compileSDK to 30 and clean warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
yongce committed Jul 18, 2020
1 parent 5c81799 commit 26fd34b
Show file tree
Hide file tree
Showing 21 changed files with 160 additions and 248 deletions.
2 changes: 1 addition & 1 deletion android_project_common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ext {

versions = [
// compile
'compileSdk' : 29,
'compileSdk' : 30,

// Android official support
'kotlin' : '1.3.72',
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.android.tools.lint.detector.api.JavaContext
import com.intellij.psi.PsiMethod
import org.jetbrains.uast.UCallExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.getContainingClass
import org.jetbrains.uast.getContainingUClass

abstract class WrapperDetectorBase : Detector(), Detector.UastScanner {
protected abstract val applicableMethods: List<String>
Expand All @@ -17,7 +17,7 @@ abstract class WrapperDetectorBase : Detector(), Detector.UastScanner {

override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
val evaluator = context.evaluator
val surroundingClass = node.getContainingClass()
val surroundingClass = node.getContainingUClass()?.javaPsi
if (surroundingClass == null) {
println(
"Fatal error in WrapperDetectorBase! Failed to get surrounding" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LintGoodActivity : AppCompatBaseActivity() { // lint good
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id = item.itemId
item.itemId

return super.onOptionsItemSelected(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LintViolationActivity : AppCompatActivity() { // lint violation
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id = item.itemId
item.itemId

sendBroadcast(Intent(TEST_ACTION)) // lint violation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object IntentHelperLintCase {
return intent.getBooleanExtra(key, defValue) // lint violation
}

fun getBundleExtraBad(intent: Intent, key: String): Bundle {
fun getBundleExtraBad(intent: Intent, key: String): Bundle? {
return intent.getBundleExtra(key) // lint violation
}
}
4 changes: 1 addition & 3 deletions baseLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ dependencies {
testImplementation deps.test.runner
testImplementation deps.test.rules
testImplementation deps.test.truth
testImplementation deps.test.mockitoCore
testImplementation deps.test.powermockMockito
testImplementation deps.test.powermockJunit
testImplementation deps.test.mockk
testImplementation deps.test.robolectric

// Android Testing Support Library's runner and rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class AppInfo(val pkgName: String) {
var appName: String? = null
var appIcon: Drawable? = null
var versionName: String? = null
var versionCode: Int = 0
var versionCode: Long = 0
var apkPath: String? = null
var installTime: Long = 0
var updateTime: Long = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import me.ycdev.android.lib.common.utils.MiscUtils
import me.ycdev.android.lib.common.utils.PackageUtils
import me.ycdev.android.lib.common.utils.StringUtils

@Suppress("unused")
class AppsLoader private constructor(cxt: Context) {
private val appContext: Context = cxt.applicationContext
private val pm: PackageManager = cxt.packageManager
Expand Down Expand Up @@ -89,7 +90,12 @@ class AppsLoader private constructor(cxt: Context) {
item.isUpdatedSysApp = aiFlag and ApplicationInfo.FLAG_UPDATED_SYSTEM_APP != 0

item.versionName = pkgInfo.versionName
item.versionCode = pkgInfo.versionCode
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
item.versionCode = pkgInfo.longVersionCode
} else {
@Suppress("DEPRECATION")
item.versionCode = pkgInfo.versionCode.toLong()
}

item.apkPath = pkgInfo.applicationInfo.sourceDir
item.isDisabled = !PackageUtils.isPkgEnabled(appContext, pkgInfo.packageName)
Expand Down
128 changes: 76 additions & 52 deletions baseLib/src/main/java/me/ycdev/android/lib/common/net/NetworkUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package me.ycdev.android.lib.common.net

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.Network
import android.net.NetworkCapabilities
import android.os.Build
import android.telephony.TelephonyManager
import android.text.TextUtils
import androidx.annotation.IntDef
Expand All @@ -26,6 +28,7 @@ object NetworkUtils {
const val NETWORK_TYPE_2G = 10
const val NETWORK_TYPE_3G = 11
const val NETWORK_TYPE_4G = 12
const val NETWORK_TYPE_5G = 13
const val NETWORK_TYPE_COMPANION_PROXY = 20

@Retention(AnnotationRetention.SOURCE)
Expand All @@ -36,40 +39,48 @@ object NetworkUtils {
NETWORK_TYPE_2G,
NETWORK_TYPE_3G,
NETWORK_TYPE_4G,
NETWORK_TYPE_5G,
NETWORK_TYPE_COMPANION_PROXY
)
annotation class NetworkType

@Suppress("DEPRECATION")
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
fun dumpActiveNetworkInfo(cxt: Context): String {
val info = getActiveNetworkInfo(cxt) ?: return "No active network"

val sb = StringBuilder()
sb.append("type=").append(info.type)
.append(", subType=").append(info.subtype)
.append(", infoDump=").append(info)
return sb.toString()
val capabilities = getActiveNetworkCapabilities(cxt) ?: return "No active network"
return capabilities.toString()
}

@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
fun getActiveNetworkInfo(cxt: Context): NetworkInfo? {
val cm = cxt.getSystemService(
Context.CONNECTIVITY_SERVICE
) as ConnectivityManager?
fun getActiveNetwork(cxt: Context): Network? {
val cm = cxt.getSystemService(ConnectivityManager::class.java)
if (cm == null) {
LibLogger.w(TAG, "failed to get connectivity service")
return null
}

var netInfo: NetworkInfo? = null
try {
netInfo = cm.activeNetworkInfo
return cm.activeNetwork
} catch (e: Exception) {
LibLogger.w(TAG, "failed to get active network info", e)
LibLogger.w(TAG, "failed to get active network", e)
}
return null
}

@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
fun getActiveNetworkCapabilities(cxt: Context): NetworkCapabilities? {
val cm = cxt.getSystemService(ConnectivityManager::class.java)
if (cm == null) {
LibLogger.w(TAG, "failed to get connectivity service")
return null
}

return netInfo
try {
val network = cm.activeNetwork ?: return null
return cm.getNetworkCapabilities(network)
} catch (e: Exception) {
LibLogger.w(TAG, "failed to get active network", e)
}
return null
}

/**
Expand All @@ -79,24 +90,20 @@ object NetworkUtils {
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@NetworkType
fun getNetworkType(cxt: Context): Int {
val netInfo = getActiveNetworkInfo(cxt) ?: return NETWORK_TYPE_NONE

@Suppress("DEPRECATION")
return getNetworkType(netInfo.type, netInfo.subtype)
val capabilities = getActiveNetworkCapabilities(cxt) ?: return NETWORK_TYPE_NONE
return getNetworkType(capabilities)
}

@Suppress("DEPRECATION", "UNUSED_PARAMETER")
@NetworkType
@VisibleForTesting
internal fun getNetworkType(type: Int, subType: Int): Int {
if (type == ConnectivityManager.TYPE_WIFI ||
type == ConnectivityManager.TYPE_WIMAX ||
type == ConnectivityManager.TYPE_ETHERNET
internal fun getNetworkType(capabilities: NetworkCapabilities): Int {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)
) {
return NETWORK_TYPE_WIFI
} else if (type == ConnectivityManager.TYPE_MOBILE || type == ConnectivityManager.TYPE_MOBILE_MMS) {
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return NETWORK_TYPE_MOBILE
} else if (type == WEAR_OS_COMPANION_PROXY) {
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
// Wear OS
return NETWORK_TYPE_COMPANION_PROXY
}
Expand All @@ -105,49 +112,66 @@ object NetworkUtils {

/**
* @return One of values [NETWORK_TYPE_2G], [NETWORK_TYPE_3G],
* [NETWORK_TYPE_4G] or [NETWORK_TYPE_NONE]
* [NETWORK_TYPE_4G], [NETWORK_TYPE_5G] or [NETWORK_TYPE_NONE]
*/
@NetworkType
@RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
fun getMobileNetworkType(cxt: Context): Int {
// Code from android-5.1.1_r4:
// frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java
// in NetworkControllerImpl#mapIconSets()
val tm = cxt.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager?
// #1 Code from android-5.1.1_r4:
// frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java
// in NetworkControllerImpl#mapIconSets()
// #2 Code from master (Android R):
// frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java
// in MobileSignalController#mapIconSets()
val tm = cxt.getSystemService(TelephonyManager::class.java)
if (tm == null) {
LibLogger.w(TAG, "failed to get telephony service")
return NETWORK_TYPE_NONE
}

val tmType: Int
try {
tmType = tm.networkType
@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
tmType = tm.dataNetworkType
} else {
tmType = tm.networkType
}
} catch (e: Exception) {
LibLogger.w(TAG, "failed to get telephony network type", e)
return NETWORK_TYPE_NONE
}

when (tmType) {
TelephonyManager.NETWORK_TYPE_UNKNOWN -> return NETWORK_TYPE_NONE

TelephonyManager.NETWORK_TYPE_LTE -> return NETWORK_TYPE_4G

TelephonyManager.NETWORK_TYPE_EVDO_0, TelephonyManager.NETWORK_TYPE_EVDO_A, TelephonyManager.NETWORK_TYPE_EVDO_B, TelephonyManager.NETWORK_TYPE_EHRPD, TelephonyManager.NETWORK_TYPE_UMTS ->
// case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
return NETWORK_TYPE_3G

TelephonyManager.NETWORK_TYPE_HSDPA, TelephonyManager.NETWORK_TYPE_HSUPA, TelephonyManager.NETWORK_TYPE_HSPA, TelephonyManager.NETWORK_TYPE_HSPAP -> return NETWORK_TYPE_3G // H

TelephonyManager.NETWORK_TYPE_GPRS, TelephonyManager.NETWORK_TYPE_EDGE, TelephonyManager.NETWORK_TYPE_CDMA, TelephonyManager.NETWORK_TYPE_1xRTT ->
// case TelephonyManager.NETWORK_TYPE_GSM:
return NETWORK_TYPE_2G
return when (tmType) {
TelephonyManager.NETWORK_TYPE_UNKNOWN -> NETWORK_TYPE_NONE
TelephonyManager.NETWORK_TYPE_LTE -> NETWORK_TYPE_4G
TelephonyManager.NETWORK_TYPE_NR -> NETWORK_TYPE_5G

TelephonyManager.NETWORK_TYPE_EVDO_0,
TelephonyManager.NETWORK_TYPE_EVDO_A,
TelephonyManager.NETWORK_TYPE_EVDO_B,
TelephonyManager.NETWORK_TYPE_EHRPD,
TelephonyManager.NETWORK_TYPE_TD_SCDMA,
TelephonyManager.NETWORK_TYPE_UMTS -> NETWORK_TYPE_3G

TelephonyManager.NETWORK_TYPE_HSDPA,
TelephonyManager.NETWORK_TYPE_HSUPA,
TelephonyManager.NETWORK_TYPE_HSPA,
TelephonyManager.NETWORK_TYPE_HSPAP -> NETWORK_TYPE_3G // H

TelephonyManager.NETWORK_TYPE_GPRS,
TelephonyManager.NETWORK_TYPE_EDGE,
TelephonyManager.NETWORK_TYPE_CDMA,
TelephonyManager.NETWORK_TYPE_GSM,
TelephonyManager.NETWORK_TYPE_1xRTT -> NETWORK_TYPE_2G

else -> NETWORK_TYPE_2G
}
return NETWORK_TYPE_2G
}

/**
* @return One of values [NETWORK_TYPE_WIFI], [NETWORK_TYPE_2G],
* [NETWORK_TYPE_3G], [NETWORK_TYPE_4G] or [NETWORK_TYPE_NONE]
* [NETWORK_TYPE_3G], [NETWORK_TYPE_4G], [NETWORK_TYPE_5G] or [NETWORK_TYPE_NONE]
*/
@RequiresPermission(
allOf = [
Expand All @@ -169,8 +193,8 @@ object NetworkUtils {
*/
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
fun isNetworkAvailable(cxt: Context): Boolean {
val network = getActiveNetworkInfo(cxt)
return network != null && network.isConnected
val capabilities = getActiveNetworkCapabilities(cxt) ?: return false
return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ object LibLogger {
msgFull = String.format(Locale.US, msgFull, *args)
}
if (tr == null) {
Log.println(level, tag, msgFull)
Log.println(level, tag, msgFull!!)
} else {
Log.println(level, tag, msgFull + "\n" + Log.getStackTraceString(tr))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package me.ycdev.android.lib.common.utils

import android.content.Context
import android.os.Build
import android.os.Environment
import android.os.storage.StorageManager
import androidx.annotation.WorkerThread
import java.io.File

@Suppress("unused")
Expand All @@ -25,15 +28,23 @@ object StorageUtils {
fun isExternalStorageAvailable(): Boolean =
Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED

@Suppress("DEPRECATION")
fun getExternalStoragePath(): String = Environment.getExternalStorageDirectory().absolutePath

/**
* Returns the number of usable free bytes on the partition containing this path.
* Returns 0 if this path does not exist.
* @see File.getUsableSpace
*/
fun getUsableSpace(path: File): Long {
return path.usableSpace
@WorkerThread
fun getUsableSpace(path: File, context: Context): Long {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val storageMgr = context.getSystemService(StorageManager::class.java) ?: return 0
val uuid = storageMgr.getUuidForPath(path)
return storageMgr.getAllocatableBytes(uuid)
} else {
return path.usableSpace
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package me.ycdev.android.lib.common.utils

import android.os.Handler
import android.os.Looper
import android.os.Message
import java.lang.ref.WeakReference

@Suppress("unused")
class WeakHandler(msgHandler: Callback) : Handler() {
class WeakHandler(looper: Looper, msgHandler: Callback) : Handler(looper) {
private val targetHandler: WeakReference<Callback> = WeakReference(msgHandler)

constructor(msgHandler: Callback) : this(Looper.myLooper()!!, msgHandler)

override fun handleMessage(msg: Message) {
val realHandler = targetHandler.get()
realHandler?.handleMessage(msg)
Expand Down
Loading

0 comments on commit 26fd34b

Please sign in to comment.