All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.wisetrack.sdk.PageDetector.kt Maven / Gradle / Ivy

There is a newer version: 1.5.3
Show newest version
package com.wisetrack.sdk

import android.app.Activity
import android.content.Context
import androidx.fragment.app.Fragment
import dalvik.system.DexFile
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserFactory

/**
@author hamed (@hamed-hsb)
 * @since 27 Jul 2024
 */

class PageDetector (val context: Context, val logger: ILogger) {

    val classes = mutableListOf>()

    init {
        findClasses(context, Activity::class.java)
        findClasses(context, Fragment::class.java)
       val c =  detectDefaultLaunchActivity()
        System.out.println("DEF : ${c}")
    }

     fun detect(): MutableList>? {
        if (classes.isNullOrEmpty())
            return null

        return classes
    }


    private fun findClasses(
        context: Context,
        superclass: Class<*>
    ) {

        try {
            val packageName = context.packageName
            val dexFile = DexFile(context.packageCodePath)
            val iter = dexFile.entries()
            while (iter.hasMoreElements()) {
                val className = iter.nextElement()
                if (className.startsWith(packageName)) {
                    val cls = Class.forName(className)
                    if (superclass.isAssignableFrom(cls) && cls != superclass) {
                        if (isFragment(cls))
                            classes.add(mapOf("name" to className, "type" to "fragment"))

                        if (isActivity(cls))
                            classes.add(mapOf("name" to className, "type" to "activity"))
                    }
                }
            }
        } catch (e: Exception) {
        }
    }

    fun detectDefaultLaunchActivity(): Class<*>? {

        try {
            val manifest = XmlPullParserFactory.newInstance().newPullParser()
            manifest.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true)
            manifest.setInput(context.assets.open("AndroidManifest.xml"), null)

            while (manifest.next() != XmlPullParser.END_DOCUMENT) {
                if (manifest.eventType == XmlPullParser.START_TAG) {
                    if (manifest.name.equals("activity")) {
                        var intentFilterFound = false
                        while (manifest.next() != XmlPullParser.END_TAG) {
                            if (manifest.eventType == XmlPullParser.START_TAG && manifest.name.equals("intent-filter")) {
                                while (manifest.next() != XmlPullParser.END_TAG) {
                                    if (manifest.eventType == XmlPullParser.START_TAG && manifest.name.equals("action")) {
                                        if (manifest.getAttributeValue(null, "android:name") == "android.intent.action.MAIN") {
                                            intentFilterFound = true
                                            break
                                        }
                                    } else if (manifest.eventType == XmlPullParser.START_TAG && manifest.name.equals("category")) {
                                        if (manifest.getAttributeValue(null, "android:name") == "android.intent.category.LAUNCHER") {
                                            intentFilterFound = true
                                            break
                                        }
                                    }
                                }
                                if (intentFilterFound) {
                                    break
                                }
                            }
                        }
                        if (intentFilterFound) {
                            return Class.forName(manifest.getAttributeValue(null, "android:name"))
                        }
                    }
                }
            }
        } catch (e: Exception) {
            logger.error("Error parsing AndroidManifest.xml", e)
        }
        return null
    }
    fun isActivity(clazz: Class<*>): Boolean =
        Activity::class.java.isAssignableFrom(clazz)

    fun isFragment(clazz: Class<*>): Boolean =
        Fragment::class.java.isAssignableFrom(clazz)
}







© 2015 - 2024 Weber Informatics LLC | Privacy Policy