Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions app/src/main/java/com/phantomvk/identifier/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
import android.util.Log
import android.widget.Button
import android.widget.TextView
Expand Down Expand Up @@ -39,10 +40,10 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.system_textview)
findViewById<Button>(R.id.button).setOnClickListener { getId() }
findViewById<Button>(R.id.refresh).setOnClickListener { getId() }
findViewById<Button>(R.id.clear_cache).setOnClickListener { IdentifierManager.clearMemoryCache() }
findViewById<Button>(R.id.uninstall).setOnClickListener { uninstall() }
findViewById<Button>(R.id.button_settings).setOnClickListener { startActivity(Intent(this, SettingsActivity::class.java)) }
findViewById<Button>(R.id.appSettings).setOnClickListener { openAppDetailsSettings() }
findViewById<Button>(R.id.configs).setOnClickListener { startActivity(Intent(this, SettingsActivity::class.java)) }
getId()
}

Expand Down Expand Up @@ -191,8 +192,9 @@ class MainActivity : AppCompatActivity() {
}
}

private fun uninstall() {
val i = Intent(Intent.ACTION_DELETE, Uri.parse("package:$packageName"))
private fun openAppDetailsSettings() {
val i = Intent(ACTION_APPLICATION_DETAILS_SETTINGS)
i.setData(Uri.parse("package:$packageName"))
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(i)
}
Expand Down
22 changes: 16 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical"
tools:context=".MainActivity">

Expand All @@ -15,9 +14,11 @@
android:orientation="horizontal">

<Button
android:id="@+id/button"
android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:minHeight="0dp"
android:text="Refresh"
android:textAllCaps="false"
android:textSize="12sp" />
Expand All @@ -26,23 +27,29 @@
android:id="@+id/clear_cache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:minHeight="0dp"
android:text="Clear cache"
android:textAllCaps="false"
android:textSize="12sp" />

<Button
android:id="@+id/uninstall"
android:id="@+id/appSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Uninstall"
android:minWidth="0dp"
android:minHeight="0dp"
android:text="App Settings"
android:textAllCaps="false"
android:textSize="12sp" />

<Button
android:id="@+id/button_settings"
android:id="@+id/configs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:minWidth="0dp"
android:minHeight="0dp"
android:text="Configs"
android:textAllCaps="false"
android:textSize="12sp" />
</LinearLayout>
Expand All @@ -52,6 +59,9 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/button_list"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textColor="#FF000000"
android:textSize="14sp"
android:textStyle="bold" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ internal abstract class AbstractProvider(protected val config: ProviderConfig) {
aaidCode: Int
): BinderResult {
return when (val o = getId(binder, oaidCode)) {
is BinderResult.Failed -> o
is BinderResult.Success -> {
val vaid = invokeById(IdEnum.VAID) { getId(binder, vaidCode) }
val aaid = invokeById(IdEnum.AAID) { getId(binder, aaidCode) }
BinderResult.Success(o.id, vaid, aaid)
is Failed -> o
is Success -> {
val vaid = if (config.idConfig.isVaidEnabled) (getId(binder, vaidCode) as? Success)?.id else null
val aaid = if (config.idConfig.isAaidEnabled) (getId(binder, aaidCode) as? Success)?.id else null
Success(o.id, vaid, aaid)
}
}
}
Expand All @@ -89,34 +89,24 @@ internal abstract class AbstractProvider(protected val config: ProviderConfig) {
reply.readException()
return checkId(reply.readString())
} catch (t: Throwable) {
return BinderResult.Failed(EXCEPTION_THROWN, t)
return Failed(EXCEPTION_THROWN, t)
} finally {
reply.recycle()
data.recycle()
}
}

protected fun invokeById(@IdEnum type: Int, callback: () -> BinderResult): String? {
val isEnabled = when (type) {
IdEnum.AAID -> config.idConfig.isAaidEnabled
IdEnum.VAID -> config.idConfig.isVaidEnabled
else -> false
}

return if (isEnabled) (callback.invoke() as? BinderResult.Success)?.id else null
}

protected fun checkId(id: String?, callback: Consumer? = null): BinderResult {
val result = when {
id.isNullOrBlank() -> BinderResult.Failed(ID_IS_NULL_OR_BLANK)
id.any { it != '0' && it != '-' } -> BinderResult.Success(id, null, null)
else -> BinderResult.Failed(ID_IS_INVALID)
id.isNullOrBlank() -> Failed(ID_IS_NULL_OR_BLANK)
id.any { it != '0' && it != '-' } -> Success(id, null, null)
else -> Failed(ID_IS_INVALID)
}

if (callback != null) {
when (result) {
is BinderResult.Failed -> callback.onError(result.msg)
is BinderResult.Success -> callback.onSuccess(IdentifierResult(result.id))
is Failed -> callback.onError(result.msg)
is Success -> callback.onSuccess(IdentifierResult(result.id))
}
}

Expand All @@ -143,8 +133,8 @@ internal abstract class AbstractProvider(protected val config: ProviderConfig) {

protected fun verifyResult(r: BinderResult) {
when (r) {
is BinderResult.Success -> getConsumer().onSuccess(IdentifierResult(r.id, r.aaid, r.vaid))
is BinderResult.Failed -> getConsumer().onError(r.msg, r.throwable)
is Success -> getConsumer().onSuccess(IdentifierResult(r.id, r.aaid, r.vaid))
is Failed -> getConsumer().onError(r.msg, r.throwable)
}
}

Expand Down Expand Up @@ -192,10 +182,9 @@ internal abstract class AbstractProvider(protected val config: ProviderConfig) {
fun call(binder: IBinder): BinderResult
}

protected sealed interface BinderResult {
class Success(val id: String, val vaid: String?, val aaid: String?) : BinderResult
class Failed(val msg: String, val throwable: Throwable? = null) : BinderResult
}
protected sealed interface BinderResult
class Success(val id: String, val vaid: String?, val aaid: String?) : BinderResult
class Failed(val msg: String, val throwable: Throwable? = null) : BinderResult

@IntDef(IdEnum.AAID, IdEnum.VAID)
@Retention(AnnotationRetention.SOURCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class AsusProvider(config: ProviderConfig) : AbstractProvider(config) {
override fun call(binder: IBinder): BinderResult {
if (config.isVerifyLimitAdTracking) {
if (!readBoolean(binder, 1, true, null)) {
return BinderResult.Failed(LIMIT_AD_TRACKING_IS_ENABLED)
return Failed(LIMIT_AD_TRACKING_IS_ENABLED)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ internal class FreemeProvider(config: ProviderConfig) : AbstractProvider(config)
override fun call(binder: IBinder): BinderResult {
val asInterface = IdsSupplier.Stub.asInterface(binder)
if (asInterface == null) {
return BinderResult.Failed(AIDL_INTERFACE_IS_NULL)
return Failed(AIDL_INTERFACE_IS_NULL)
}

if (config.isVerifyLimitAdTracking) {
if (!asInterface.isSupported) {
return BinderResult.Failed(LIMIT_AD_TRACKING_IS_ENABLED)
return Failed(LIMIT_AD_TRACKING_IS_ENABLED)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class GoogleAdsIdProvider(config: ProviderConfig) : AbstractProvider(co
override fun call(binder: IBinder): BinderResult {
if (config.isVerifyLimitAdTracking) {
if (readBoolean(binder, 2, false) { it.writeInt(1) }) {
return BinderResult.Failed(LIMIT_AD_TRACKING_IS_ENABLED)
return Failed(LIMIT_AD_TRACKING_IS_ENABLED)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,37 @@ internal class HonorServiceProvider(config: ProviderConfig) : AbstractProvider(c

override fun run() {
val intent = Intent("com.hihonor.id.HnOaIdService").setPackage("com.hihonor.id")
bindService(intent) { binder ->
if (config.isVerifyLimitAdTracking) {
isLimited(binder) { result ->
if (result is BinderResult.Success) {
getId(binder) { r -> verifyResult(r) }
} else {
verifyResult(result)
}
}
} else {
getId(binder) { r -> verifyResult(r) }
}
}
bindService(intent) { b -> if (config.isVerifyLimitAdTracking) callBinder(3, b) else callBinder(2, b) }
}

private fun getId(remote: IBinder, callback: OnResultCallback) {
/**
* @param code 2:getId(), 3:isLimited()
* @param remote binder instance
*/
private fun callBinder(code: Int, remote: IBinder) {
val callback = object : IOAIDCallBack.Stub() {
override fun a(i: Int, j: Long, z: Boolean, f: Float, d: Double, str: String?) {}
override fun onResult(i: Int, bundle: Bundle?) {
if (i != 0 || bundle == null) {
callback.call(BinderResult.Failed(BUNDLE_IS_NULL))
} else {
callback.call(checkId(bundle.getString("oa_id_flag")))
}
}
}

callBinder(remote, callback, 2)
}
when (code) {
2 -> { // getId()
if (i == 0 && bundle != null) {
verifyResult(checkId(bundle.getString("oa_id_flag")))
} else {
getConsumer().onError(BUNDLE_IS_NULL)
}
}

private fun isLimited(remote: IBinder, callback: OnResultCallback) {
val callback = object : IOAIDCallBack.Stub() {
override fun a(i: Int, j: Long, z: Boolean, f: Float, d: Double, str: String?) {}
override fun onResult(i: Int, bundle: Bundle?) {
if (i == 0 && bundle?.getBoolean("oa_id_limit_state") == true) {
callback.call(BinderResult.Failed(LIMIT_AD_TRACKING_IS_ENABLED))
} else {
callback.call(BinderResult.Success("", "", ""))
3 -> { // isLimited()
if (i == 0 && bundle?.getBoolean("oa_id_limit_state") == true) {
getConsumer().onError(LIMIT_AD_TRACKING_IS_ENABLED)
} else {
callBinder(2, remote)
}
}
}
}
}

callBinder(remote, callback, 3)
}

private fun callBinder(remote: IBinder, callback: IOAIDCallBack, code: Int) {
val data = Parcel.obtain()
val reply = Parcel.obtain()
try {
Expand All @@ -69,13 +54,10 @@ internal class HonorServiceProvider(config: ProviderConfig) : AbstractProvider(c
remote.transact(code, data, reply, 0)
reply.readException()
} catch (t: Throwable) {
getConsumer().onError(EXCEPTION_THROWN, t)
} finally {
reply.recycle()
data.recycle()
}
}

private fun interface OnResultCallback {
fun call(result: BinderResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal abstract class HuaweiBaseProvider(config: ProviderConfig) : AbstractPro
}

val latch = CountDownLatch(1)
var result: BinderResult = BinderResult.Failed(EXCEPTION_THROWN)
var result: BinderResult = Failed(EXCEPTION_THROWN)

try {
com.huawei.hms.aaid.HmsInstanceId.getInstance(config.context).aaid
Expand All @@ -20,16 +20,16 @@ internal abstract class HuaweiBaseProvider(config: ProviderConfig) : AbstractPro
latch.countDown()
}
.addOnFailureListener { t: Throwable ->
result = BinderResult.Failed(EXCEPTION_THROWN, t)
result = Failed(EXCEPTION_THROWN, t)
latch.countDown()
}
} catch (t: Throwable) {
result = BinderResult.Failed(EXCEPTION_THROWN, t)
result = Failed(EXCEPTION_THROWN, t)
latch.countDown()
}

latch.await()
return (result as? BinderResult.Success)?.id
return (result as? Success)?.id
}

protected fun getVAID(): String? {
Expand All @@ -38,7 +38,7 @@ internal abstract class HuaweiBaseProvider(config: ProviderConfig) : AbstractPro
}

val latch = CountDownLatch(1)
var result: BinderResult = BinderResult.Failed(EXCEPTION_THROWN)
var result: BinderResult = Failed(EXCEPTION_THROWN)

try {
com.huawei.hms.opendevice.OpenDevice.getOpenDeviceClient(config.context).odid
Expand All @@ -47,15 +47,15 @@ internal abstract class HuaweiBaseProvider(config: ProviderConfig) : AbstractPro
latch.countDown()
}
.addOnFailureListener { t: Throwable ->
result = BinderResult.Failed(EXCEPTION_THROWN, t)
result = Failed(EXCEPTION_THROWN, t)
latch.countDown()
}
} catch (t: Throwable) {
result = BinderResult.Failed(EXCEPTION_THROWN, t)
result = Failed(EXCEPTION_THROWN, t)
latch.countDown()
}

latch.await()
return (result as? BinderResult.Success)?.id
return (result as? Success)?.id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ internal class HuaweiContentProvider(config: ProviderConfig) : HuaweiBaseProvide
}

when (val r = checkId(c.getString(code))) {
is BinderResult.Failed -> getConsumer().onError(r.msg, r.throwable)
is BinderResult.Success -> getConsumer().onSuccess(IdentifierResult(r.id, getAAID(), getVAID()))
is Failed -> getConsumer().onError(r.msg, r.throwable)
is Success -> getConsumer().onSuccess(IdentifierResult(r.id, getAAID(), getVAID()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ internal class HuaweiSdkProvider(config: ProviderConfig) : HuaweiBaseProvider(co
}

when (val r = checkId(info.id)) {
is BinderResult.Failed -> getConsumer().onError(r.msg, r.throwable)
is BinderResult.Success -> getConsumer().onSuccess(IdentifierResult(r.id, getAAID(), getVAID()))
is Failed -> getConsumer().onError(r.msg, r.throwable)
is Success -> getConsumer().onSuccess(IdentifierResult(r.id, getAAID(), getVAID()))
}
}
}
Loading