Giter VIP home page Giter VIP logo

Comments (3)

svallejos avatar svallejos commented on May 22, 2024 1

Thank you! Your code was very useful. I made an adaptation of your helper in my Android project to avoid problems with brand names: If a known main package exists, i just try to start the activity with every possible known component name related to that package. It worked for me. I leave the code in case it is useful for you. It is in Java, but you can program it in kotlin easily.

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AutoStartPermissionHelper {

    /***
     * Xiaomi
     */
    private String PACKAGE_XIAOMI_MAIN = "com.miui.securitycenter";
    private String PACKAGE_XIAOMI_COMPONENT = "com.miui.permcenter.autostart.AutoStartManagementActivity";
    private List<String> XIAOMI_PACKAGES = Collections.singletonList(PACKAGE_XIAOMI_COMPONENT);

    /***
     * Letv
     */
    private String PACKAGE_LETV_MAIN = "com.letv.android.letvsafe";
    private String PACKAGE_LETV_COMPONENT = "com.letv.android.letvsafe.AutobootManageActivity";
    private List<String> LETV_PACKAGES = Collections.singletonList(PACKAGE_LETV_COMPONENT);

    /***
     * ASUS ROG
     */
    private String PACKAGE_ASUS_MAIN = "com.asus.mobilemanager";
    private String PACKAGE_ASUS_COMPONENT = "com.asus.mobilemanager.powersaver.PowerSaverSettings";
    private List<String> ASUS_PACKAGES = Collections.singletonList(PACKAGE_ASUS_COMPONENT);

    /***
     * Honor
     */
    private String PACKAGE_HONOR_MAIN = "com.huawei.systemmanager";
    private String PACKAGE_HONOR_COMPONENT = "com.huawei.systemmanager.optimize.process.ProtectActivity";
    private List<String> HONOR_PACKAGES = Collections.singletonList(PACKAGE_HONOR_COMPONENT);

    /***
     * Huawei
     */
    private String PACKAGE_HUAWEI_MAIN = "com.huawei.systemmanager";
    private String PACKAGE_HUAWEI_COMPONENT = "com.huawei.systemmanager.optimize.process.ProtectActivity";
    private List<String> HUAWEI_PACKAGES = Collections.singletonList(PACKAGE_HUAWEI_COMPONENT);

    /**
     * Oppo
     */
    private String PACKAGE_OPPO_MAIN = "com.coloros.safecenter";
    private String PACKAGE_OPPO_MAIN_FALLBACK = "com.oppo.safe";
    private String PACKAGE_OPPO_COMPONENT = "com.coloros.safecenter.permission.startup.StartupAppListActivity";
    private String PACKAGE_OPPO_COMPONENT_FALLBACK = "com.oppo.safe.permission.startup.StartupAppListActivity";
    private String PACKAGE_OPPO_COMPONENT_FALLBACK_A = "com.coloros.safecenter.startupapp.StartupAppListActivity";
    private List<String> OPPO_PACKAGES = Arrays.asList(PACKAGE_OPPO_COMPONENT,PACKAGE_OPPO_COMPONENT_FALLBACK,PACKAGE_OPPO_COMPONENT_FALLBACK_A);

    /**
     * Vivo
     */

    private String PACKAGE_VIVO_MAIN = "com.iqoo.secure";
    private String PACKAGE_VIVO_MAIN_FALLBACK = "com.vivo.permissionmanager";
    private String PACKAGE_VIVO_COMPONENT = "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity";
    private String PACKAGE_VIVO_COMPONENT_FALLBACK = "com.vivo.permissionmanager.activity.BgStartUpManagerActivity";
    private String PACKAGE_VIVO_COMPONENT_FALLBACK_A = "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager";
    private List<String> VIVO_PACKAGES = Arrays.asList(PACKAGE_VIVO_COMPONENT,PACKAGE_VIVO_COMPONENT_FALLBACK,PACKAGE_VIVO_COMPONENT_FALLBACK_A);

    /**
     * Nokia
     */
    private String PACKAGE_NOKIA_MAIN = "com.evenwell.powersaving.g3";
    private String PACKAGE_NOKIA_COMPONENT = "com.evenwell.powersaving.g3.exception.PowerSaverExceptionActivity";
    private List<String> NOKIA_PACKAGES = Collections.singletonList(PACKAGE_NOKIA_COMPONENT);

    /**
     * Packages and components mapper
     */
    Map<String, List<String>> PACKAGES  = new HashMap<String, List<String>>() {{
        put(PACKAGE_XIAOMI_MAIN, XIAOMI_PACKAGES);
        put(PACKAGE_LETV_MAIN, LETV_PACKAGES);
        put(PACKAGE_ASUS_MAIN, ASUS_PACKAGES);
        put(PACKAGE_HONOR_MAIN, HONOR_PACKAGES);
        put(PACKAGE_HUAWEI_MAIN, HUAWEI_PACKAGES);
        put(PACKAGE_OPPO_MAIN, OPPO_PACKAGES);
        put(PACKAGE_OPPO_MAIN_FALLBACK, OPPO_PACKAGES);
        put(PACKAGE_VIVO_MAIN, VIVO_PACKAGES);
        put(PACKAGE_VIVO_MAIN_FALLBACK, VIVO_PACKAGES);
        put(PACKAGE_NOKIA_MAIN, NOKIA_PACKAGES);
    }};

    private static AutoStartPermissionHelper INSTANCE = null;

    private AutoStartPermissionHelper() {

    }

    public static AutoStartPermissionHelper getInstance() {
        if (INSTANCE == null)
            INSTANCE = new AutoStartPermissionHelper();
        return INSTANCE;
    }

    public boolean isAutoStartPermissionAvailable(Context context) {
        List<ApplicationInfo> packages = context.getPackageManager().getInstalledApplications(0);
        for (ApplicationInfo appInfo: packages) {
            if (PACKAGES.keySet().contains(appInfo.packageName))
                return true;
        }
        return false;
    }


    public void getAutoStartPermission(Context context) {
        List<ApplicationInfo> packages = context.getPackageManager().getInstalledApplications(0);
        for (ApplicationInfo appInfo: packages) {
            String mainPackage = appInfo.packageName;
            if (PACKAGES.keySet().contains(mainPackage)) {
                List<String> components = PACKAGES.get(mainPackage);
                if (components != null && startIntent(context, mainPackage,components))
                    return;
            }
        }
    }

    private boolean startIntent(Context context, String mainPackage, List<String> components) {
        Intent intent = new Intent();

        for (String component: components) {
            try {
                intent.setComponent(new ComponentName(mainPackage, component));
                context.startActivity(intent);
                return true;
            } catch (Exception ignored) {

            }
        }

        return false;
    }

}

from autostarter.

judemanutd avatar judemanutd commented on May 22, 2024

Thanks @svallejos , I have access to a redmi note 7 which should also fall under the redmi brand so I'll test it out and have that update pushed up. Good catch!! Thanks again!

from autostarter.

judemanutd avatar judemanutd commented on May 22, 2024

@svallejos thanks for the snippet, I've held off on implementing brute force as of now because I really don't know what kind of devices the library is being used in and the load that would be there at the time the library is called so its safer to use brands as of now.

from autostarter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.