judemanutd / AutoStarter

This library helps bring up the autostart permission manager of a phone to the user so they can add an app to autostart.
MIT License
626 stars 115 forks source link

Problems with Xiaomi (redmi brand) #20

Closed ghost closed 5 years ago

ghost commented 5 years ago

Some smartphones return redmi instead of xiaomi when invoking Build.BRAND.toLowerCase(). The packages still have the same names (com.miui.securitycenter and com.miui.permcenter.autostart.AutoStartManagementActivity). The solution may be just adding a second constant _BRAND_XIAOMI2 = redmi.

You can recreate this situation with a Xiaomi Note Pro 8.

PD: sorry for my bad english

judemanutd commented 5 years ago

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!

ghost commented 5 years ago

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;
    }

}
judemanutd commented 5 years ago

@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.