33
  1. How can I enable auto-start permission programmatically?
  2. How to find which phone need to do auto-start code?
  3. How to check if the auto start permission is enable or disable?

I am able to find only about Display popup permission with canDrawOverlay() permission`.

I want to enable auto-start for the device if it is not enabled.


I have found a solution for Xiaomi, honor and let.

if(Build.BRAND.equalsIgnoreCase("xiaomi") ){

                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
                startActivity(intent);


            }else if(Build.BRAND.equalsIgnoreCase("Letv")){

                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"));
                startActivity(intent);

            }
            else if(Build.BRAND.equalsIgnoreCase("Honor")){

                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
                startActivity(intent);

            }
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Sagar
  • 5,273
  • 4
  • 37
  • 50
  • Propably it is impossible but there must be intent which sends user to proper page in settings where user can turn it on manually like in huawei or xiaomi devices. – RadekJ Jun 06 '17 at 10:09
  • I know but for xiaomi, huawei, letv there is package name available with the help of that we can achive that – Sagar Jun 06 '17 at 10:14
  • what about the oppo devices? do you have any solution for oppo devices?? – Shaifali Rajput Jul 27 '17 at 04:57
  • @Shaifali Rajput try this it may help you I haven't tested this one check and notify `Intent intent = new Intent(); intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity" )); startActivity(intent);` – Sagar Sep 22 '17 at 13:14
  • @Sagar2869767 thankyou i found that solution and i have posted here too https://stackoverflow.com/questions/41804070/how-to-protect-background-service-alarms-to-be-kill-in-newly-launched-devices-in/45188694#45188694 – Shaifali Rajput Sep 22 '17 at 13:52
  • @Shaifali Rajput can you help me to get on/off floating button permission programmatically in OPPO device – Sagar Sep 23 '17 at 06:03
  • @Sagar2869767 take reference from https://stackoverflow.com/questions/45138266/how-to-open-miui-system-activity-programmatically-in-android – Shaifali Rajput Sep 23 '17 at 07:39

6 Answers6

17

Please check the following solution to enable the floating window and autostart permission for OPPO and VIVO devices.

There's no way to find out whether the Auto-start option is enabled or not. You can manually check under Security permissions => Autostart => Enable Autostart.

On Oppo devices:

  private void initOPPO() {
    try {

        Intent i = new Intent(Intent.ACTION_MAIN);
        i.setComponent(new ComponentName("com.oppo.safe", "com.oppo.safe.permission.floatwindow.FloatWindowListActivity"));
        startActivity(i);
    } catch (Exception e) {
        e.printStackTrace();
        try {

            Intent intent = new Intent("action.coloros.safecenter.FloatWindowListActivity");
            intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.floatwindow.FloatWindowListActivity"));
            startActivity(intent);
        } catch (Exception ee) {

            ee.printStackTrace();
            try{

                Intent i = new Intent("com.coloros.safecenter");
                i.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.sysfloatwindow.FloatWindowListActivity"));
                startActivity(i);
            }catch (Exception e1){

                e1.printStackTrace();
            }
        }

    }
}

Auto Start permission for VIVO

 private static void autoLaunchVivo(Context context) {
    try {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.iqoo.secure",
                "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"));
        context.startActivity(intent);
    } catch (Exception e) {
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.vivo.permissionmanager",
                    "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
            context.startActivity(intent);
        } catch (Exception ex) {
            try {
                Intent intent = new Intent();
                intent.setClassName("com.iqoo.secure",
                        "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager");
                context.startActivity(intent);
            } catch (Exception exx) {
                ex.printStackTrace();
            }
        }
    }
}

Auto Start for OPPO

 if (Build.MANUFACTURER.equalsIgnoreCase("oppo")) {
        try {
            Intent intent = new Intent();
            intent.setClassName("com.coloros.safecenter",
                    "com.coloros.safecenter.permission.startup.StartupAppListActivity");
            startActivity(intent);
        } catch (Exception e) {
            try {
                Intent intent = new Intent();
                intent.setClassName("com.oppo.safe",
                        "com.oppo.safe.permission.startup.StartupAppListActivity");
                startActivity(intent);

            } catch (Exception ex) {
                try {
                    Intent intent = new Intent();
                    intent.setClassName("com.coloros.safecenter",
                            "com.coloros.safecenter.startupapp.StartupAppListActivity");
                    startActivity(intent);
                } catch (Exception exx) {

                }
            }
        }
}
Sagar
  • 5,273
  • 4
  • 37
  • 50
14

Use this helper class

 public class AutoStartHelper {

/***
 * Xiaomi
 */
private final String BRAND_XIAOMI = "xiaomi";
private String PACKAGE_XIAOMI_MAIN = "com.miui.securitycenter";
private String PACKAGE_XIAOMI_COMPONENT = "com.miui.permcenter.autostart.AutoStartManagementActivity";

/***
 * Letv
 */
private final String BRAND_LETV = "letv";
private String PACKAGE_LETV_MAIN = "com.letv.android.letvsafe";
private String PACKAGE_LETV_COMPONENT = "com.letv.android.letvsafe.AutobootManageActivity";

/***
 * ASUS ROG
 */
private final String BRAND_ASUS = "asus";
private String PACKAGE_ASUS_MAIN = "com.asus.mobilemanager";
private String PACKAGE_ASUS_COMPONENT = "com.asus.mobilemanager.powersaver.PowerSaverSettings";

/***
 * Honor
 */
private final String BRAND_HONOR = "honor";
private String PACKAGE_HONOR_MAIN = "com.huawei.systemmanager";
private String PACKAGE_HONOR_COMPONENT = "com.huawei.systemmanager.optimize.process.ProtectActivity";

/**
 * Oppo
 */
private final String BRAND_OPPO = "oppo";
private String PACKAGE_OPPO_MAIN = "com.coloros.safecenter";
private String PACKAGE_OPPO_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";

/**
 * Vivo
 */

private final String BRAND_VIVO = "vivo";
private String PACKAGE_VIVO_MAIN = "com.iqoo.secure";
private String PACKAGE_VIVO_FALLBACK = "com.vivo.perm;issionmanager";
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";

/**
 * Nokia
 */

private final String BRAND_NOKIA = "nokia";
private String PACKAGE_NOKIA_MAIN = "com.evenwell.powersaving.g3";
private String PACKAGE_NOKIA_COMPONENT = "com.evenwell.powersaving.g3.exception.PowerSaverExceptionActivity";


private AutoStartHelper() {
}

public static AutoStartHelper getInstance() {
    return new AutoStartHelper();
}


public void getAutoStartPermission(Context context) {

    String build_info = Build.BRAND.toLowerCase();
    switch (build_info) {
        case BRAND_ASUS:
            autoStartAsus(context);
            break;
        case BRAND_XIAOMI:
            autoStartXiaomi(context);
            break;
        case BRAND_LETV:
            autoStartLetv(context);
            break;
        case BRAND_HONOR:
            autoStartHonor(context);
            break;
        case BRAND_OPPO:
            autoStartOppo(context);
            break;
        case BRAND_VIVO:
            autoStartVivo(context);
            break;
        case BRAND_NOKIA:
            autoStartNokia(context);
            break;

    }

}

private void autoStartAsus(final Context context) {
    if (isPackageExists(context, PACKAGE_ASUS_MAIN)) {

        showAlert(context, (dialog, which) -> {
            try {
            PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                startIntent(context, PACKAGE_ASUS_MAIN, PACKAGE_ASUS_COMPONENT);
            } catch (Exception e) {
                e.printStackTrace();
            }
            dialog.dismiss();
        });

    }


}

private void showAlert(Context context, DialogInterface.OnClickListener onClickListener) {

    new AlertDialog.Builder(context).setTitle("Allow AutoStart")
            .setMessage("Please enable auto start in settings.")
            .setPositiveButton("Allow", onClickListener).show().setCancelable(false);
}

private void autoStartXiaomi(final Context context) {
    if (isPackageExists(context, PACKAGE_XIAOMI_MAIN)) {
        showAlert(context, (dialog, which) -> {
            try {
                PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                startIntent(context, PACKAGE_XIAOMI_MAIN, PACKAGE_XIAOMI_COMPONENT);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });


    }
}

private void autoStartLetv(final Context context) {
    if (isPackageExists(context, PACKAGE_LETV_MAIN)) {
        showAlert(context, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                try {
                    PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                    startIntent(context, PACKAGE_LETV_MAIN, PACKAGE_LETV_COMPONENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


    }
}


private void autoStartHonor(final Context context) {
    if (isPackageExists(context, PACKAGE_HONOR_MAIN)) {
        showAlert(context, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                try {
                    PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                    startIntent(context, PACKAGE_HONOR_MAIN, PACKAGE_HONOR_COMPONENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


    }
}

private void autoStartOppo(final Context context) {
    if (isPackageExists(context, PACKAGE_OPPO_MAIN) || isPackageExists(context, PACKAGE_OPPO_FALLBACK)) {
        showAlert(context, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                try {
                    PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                    startIntent(context, PACKAGE_OPPO_MAIN, PACKAGE_OPPO_COMPONENT);
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                        startIntent(context, PACKAGE_OPPO_FALLBACK, PACKAGE_OPPO_COMPONENT_FALLBACK);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        try {
                            PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                            startIntent(context, PACKAGE_OPPO_MAIN, PACKAGE_OPPO_COMPONENT_FALLBACK_A);
                        } catch (Exception exx) {
                            exx.printStackTrace();
                        }

                    }

                }
            }
        });


    }
}

private void autoStartVivo(final Context context) {
    if (isPackageExists(context, PACKAGE_VIVO_MAIN) || isPackageExists(context, PACKAGE_VIVO_FALLBACK)) {
        showAlert(context, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                try {
                    PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                    startIntent(context, PACKAGE_VIVO_MAIN, PACKAGE_VIVO_COMPONENT);
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                        startIntent(context, PACKAGE_VIVO_FALLBACK, PACKAGE_VIVO_COMPONENT_FALLBACK);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        try {
                            PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                            startIntent(context, PACKAGE_VIVO_MAIN, PACKAGE_VIVO_COMPONENT_FALLBACK_A);
                        } catch (Exception exx) {
                            exx.printStackTrace();
                        }

                    }

                }

            }
        });
    }
}

private void autoStartNokia(final Context context) {
    if (isPackageExists(context, PACKAGE_NOKIA_MAIN)) {
        showAlert(context, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                try {
                    PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                    startIntent(context, PACKAGE_NOKIA_MAIN, PACKAGE_NOKIA_COMPONENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}


private void startIntent(Context context, String packageName, String componentName) throws Exception {
    try {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName, componentName));
        context.startActivity(intent);
    } catch (Exception var5) {
        var5.printStackTrace();
        throw var5;
    }
}

private Boolean isPackageExists(Context context, String targetPackage) {
    List<ApplicationInfo> packages;
    PackageManager pm = context.getPackageManager();
    packages = pm.getInstalledApplications(0);
    for (ApplicationInfo packageInfo :
            packages) {
        if (packageInfo.packageName.equals(targetPackage)) {
            return true;
        }
    }

    return false;
 }
}

in your activity

        AutoStartHelper.getInstance().getAutoStartPermission(this);

there is no way to track that we have enabled autostart or not.

Adarsh Binjola
  • 216
  • 2
  • 10
  • From where can I find `PrefUtil`? – Lasithe Nov 02 '19 at 03:48
  • https://github.com/esripdx/Android-Static-Utils/blob/master/src/com/esri/android/util/SharedPrefsUtils.java – Adarsh Binjola Nov 05 '19 at 05:03
  • HI if user doesnt change auto permission from settings but you save bolean is true in auto refresh in shared preference.. then? – Debasish Ghosh Dec 02 '19 at 13:06
  • 1
    There is no way to track that user have successfully enabled the auto start for your application .saving true to prefs is just a way for not asking user to autostart it so add important notice in your dialog prompt and tell the user why this permission is so much important for your application plus what user will loose if this permission is not given for your app. – Adarsh Binjola Dec 05 '19 at 11:36
  • @Lasitha PrefUtil is nothing but your SharedPreferences – Aditya Patil Jun 01 '20 at 10:19
  • private void autoStartOppo(final Context context) { if (isPackageExists(context, PACKAGE_OPPO_MAIN) || isPackageExists(context, PACKAGE_OPPO_FALLBACK)) -> return false – J3n Aug 17 '22 at 13:50
12

The autostart feature will get enabled automatically when you will download the app from playstore if xiaomi OS wants it as apps like amazon ,google IO etc are also not allowed to autostart ,In this case you have to go to Security permissions -> autostart -> then enable autostart from there.You cannot make the app autostart by code all you can do is you can show a dialog to enable auto start and take the user to the autostart activity but this is not a good option as you cannot check whether autostart is enabled or not.

This is done by Mi in MIUI8 for saving battery.

Reference

You can refer to the article MIUI8

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Rohit Sharma
  • 1,384
  • 12
  • 19
3

As others said there's no way to find out whether the Auto-start option is enabled or not, but we can use an intent to redirect the user to the auto-start settings. Then it is up to the user to allow it or not to allow it.

We can use the ACTION_APPLICATION_DETAILS_SETTINGS or ACTION_MANAGE_APPLICATIONS_SETTINGS flags to redirect directly to the auto-start settings screen.

I have tested this on Xiaomi & OPPO phones and I believe that this code will also work for other custom UI devices like Vivo etc.

On click of a popup dialogue which says please check and enable Autostart option from the app setting screen. Put the below code on the click of the OK button.

  try
    {
        //Open the specific App Info page:
        Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + context.getPackageName()));
        context.startActivity(intent);
    }
    catch ( ActivityNotFoundException e )
    {
        //Open the generic Apps page:
        Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
        context.startActivity(intent);
    }
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Divyanshu Kumar
  • 1,272
  • 15
  • 15
  • this works for oppo realme c2, htc desire and samsung s5 (since there is not auto-start enable option in htc and samsung but it still opens app settings screen)... i hope it works for other custom devices as well – syed_noorullah Nov 27 '19 at 13:45
  • have you tested it for devices other than the ones you stated above ? – syed_noorullah Nov 27 '19 at 14:14
  • 1
    @syed_noorullah i have tested it on stock android (PIE), Honor EMUI, oppo( color os), Xiaomi (MIUI), also i found that for Huawei & Honor (both are same company and have same custom OS). the above code is taking to app info screen but there is no auto run option available. I wonder they are optimizing the phone's background services by killing all except the services of white-listed apps so its kind of difficult to sort things in a Huawei or honor device and if you found something please share it. – Divyanshu Kumar Nov 28 '19 at 03:54
  • check out Adarsh binjola's answer in this same thread. I think he has provided the auto start activity for huawei honor:- private final String BRAND_HONOR = "honor"; private String PACKAGE_HONOR_MAIN = "com.huawei.systemmanager"; private String PACKAGE_HONOR_COMPONENT = "com.huawei.systemmanager.optimize.process.ProtectActivity"; – syed_noorullah Nov 28 '19 at 06:23
0

https://github.com/XomaDev/MIUI-autostart

Hey, try out this library :D

Autostart autostart = new Autostart(applicationContext);

if (autostart.getAutoStartState() == State.ENABLED) {
    // autostart is enabled for sure
}
0

An updated version of This in kotlin

/**
 * To request AutoStart permission based on [Brand].
 */
@Singleton
class AutoStartPermissionManager @Inject constructor(
    @ApplicationContext private val _context: Context
) {

    /**
     * Request AutoStart permission based on [Brand] type.
     * Note-> No permission required for [Brand.OTHER].
     */
    fun requestAutoStartPermission() {
        when (Build.BRAND.uppercase().toEnum(Brand::name, Brand.OTHER)) {
            Brand.XIAOMI, Brand.REDMI -> xiaomiAutoStart()
            Brand.NOKIA -> nokiaAutoStart()
            Brand.LETV -> letvAutoStart()
            Brand.ASUS -> asusAutoStart()
            Brand.HONOR -> honorAutoStart()
            Brand.OPPO -> oppoAutoStart()
            Brand.VIVO -> vivoAutoStart()
            Brand.OTHER -> {}
        }
    }

    /**
     * Request AutoStart permission for [Brand.XIAOMI] and [Brand.REDMI].
     */
    private fun xiaomiAutoStart() {
        if (isPackageExists(XIAOMI_MAIN)) {
            try {
                startAutoStartActivity(_context, XIAOMI_MAIN, XIAOMI_COMPONENT)

            } catch (e: BAExceptions.ActivityNotFound) {
                Logger.error(e, message = e.msg)
            }
        }
    }

    @Throws(BAExceptions::class)
    private fun startAutoStartActivity(context: Context, packageName: String, componentName: String) {
        val intentAutoStartPage = Intent().apply {
            component = ComponentName(packageName, componentName)
            flags = Intent.FLAG_ACTIVITY_NEW_TASK
        }
        try {
            context.startActivity(intentAutoStartPage)
        } catch (e: Exception) {
            throw BAExceptions.ActivityNotFound
        }
    }

    /**
     * Request AutoStart permission for [Brand.NOKIA].
     */
    private fun nokiaAutoStart() {
        if (isPackageExists(NOKIA_MAIN)) {
            try {
                startAutoStartActivity(_context, NOKIA_MAIN, NOKIA_COMPONENT)

            } catch (e: BAExceptions.ActivityNotFound) {
                Logger.error(e, message = e.msg)
            }
        }
    }

    /**
     * Request AutoStart permission for [Brand.LETV].
     */
    private fun letvAutoStart() {
        if (isPackageExists(LETV_MAIN)) {
            try {
                startAutoStartActivity(_context, LETV_MAIN, LETV_COMPONENT)

            } catch (e: BAExceptions.ActivityNotFound) {
                Logger.error(e, message = e.msg)
            }
        }
    }

    /**
     * Request AutoStart permission for [Brand.ASUS].
     */
    private fun asusAutoStart() {
        if (isPackageExists(ASUS_MAIN)) {
            try {
                startAutoStartActivity(_context, ASUS_MAIN, ASUS_COMPONENT)

            } catch (e: BAExceptions.ActivityNotFound) {
                Logger.error(e, message = e.msg)
            }
        }
    }

    /**
     * Request AutoStart permission for [Brand.HONOR].
     */
    private fun honorAutoStart() {
        if (isPackageExists(HONOR_MAIN)) {
            try {
                startAutoStartActivity(_context, HONOR_MAIN, HONOR_COMPONENT)

            } catch (e: BAExceptions.ActivityNotFound) {
                Logger.error(e, message = e.msg)
            }
        }
    }

    /**
     * Request AutoStart permission for [Brand.OPPO].
     */
    private fun oppoAutoStart() {
        if (isPackageExists(OPPO_MAIN) || isPackageExists(OPPO_FALLBACK)) {
            try {
                startAutoStartActivity(_context, OPPO_MAIN, OPPO_COMPONENT)

            } catch (e: BAExceptions.ActivityNotFound) {
                Logger.error(e, message = e.msg)

                try {
                    startAutoStartActivity(_context, OPPO_FALLBACK, OPPO_COMPONENT_FALLBACK)

                } catch (ex: BAExceptions.ActivityNotFound) {
                    Logger.error(ex, message = ex.msg)

                    try {
                        startAutoStartActivity(_context, OPPO_MAIN, OPPO_COMPONENT_FALLBACK_A)

                    } catch (exx: BAExceptions.ActivityNotFound) {
                        Logger.error(exx, message = exx.msg)
                    }
                }
            }
        }
    }

    /**
     * Request AutoStart permission for [Brand.VIVO].
     */
    private fun vivoAutoStart() {
        if (isPackageExists(VIVO_MAIN) || isPackageExists(VIVO_FALLBACK)) {
            try {
                startAutoStartActivity(_context, VIVO_MAIN, VIVO_COMPONENT)

            } catch (e: BAExceptions.ActivityNotFound) {
                Logger.error(e, message = e.msg)

                try {
                    startAutoStartActivity(_context, VIVO_FALLBACK, VIVO_COMPONENT_FALLBACK)

                } catch (ex: BAExceptions.ActivityNotFound) {
                    Logger.error(ex, message = ex.msg)

                    try {
                        startAutoStartActivity(_context, VIVO_MAIN, VIVO_COMPONENT_FALLBACK_A)

                    } catch (exx: BAExceptions.ActivityNotFound) {
                        Logger.error(exx, message = exx.msg)
                    }
                }
            }
        }
    }

    /**
     * Return true if requested package exist false otherwise.
     */
    private fun isPackageExists(targetPackage: String): Boolean {
        val packages = _context.packageManager.getInstalledApplications(0)
        for (packageInfo in packages) {
            if (packageInfo.packageName.equals(targetPackage)) return true
        }

        return false
    }

    /**
     * Type of brands for which we can request AutoStart permission.
     * Note-> No permission required for [Brand.OTHER].
     */
    private enum class Brand {
        REDMI,
        XIAOMI,
        NOKIA,
        LETV,
        ASUS,
        HONOR,
        OPPO,
        VIVO,
        OTHER
    }

    /**
     * All [Brand] packages using this we can request AutoStart permission.
     */
    private companion object BrandPackage {

        // Xiaomi
        private val XIAOMI_MAIN = "com.miui.securitycenter"
        private val XIAOMI_COMPONENT = "com.miui.permcenter.autostart.AutoStartManagementActivity"

        // Nokia
        private val NOKIA_MAIN = "com.evenwell.powersaving.g3"
        private val NOKIA_COMPONENT = "com.evenwell.powersaving.g3.exception.PowerSaverExceptionActivity"

        // Letv
        private val LETV_MAIN = "com.letv.android.letvsafe"
        private val LETV_COMPONENT = "com.letv.android.letvsafe.AutobootManageActivity"

        // ASUS ROG
        private val ASUS_MAIN = "com.asus.mobilemanager"
        private val ASUS_COMPONENT = "com.asus.mobilemanager.powersaver.PowerSaverSettings"

        // Honor
        private val HONOR_MAIN = "com.huawei.systemmanager"
        private val HONOR_COMPONENT = "com.huawei.systemmanager.optimize.process.ProtectActivity"

        // Oppo
        private val OPPO_MAIN = "com.coloros.safecenter"
        private val OPPO_FALLBACK = "com.oppo.safe"
        private val OPPO_COMPONENT = "com.coloros.safecenter.permission.startup.StartupAppListActivity"
        private val OPPO_COMPONENT_FALLBACK = "com.oppo.safe.permission.startup.StartupAppListActivity"
        private val OPPO_COMPONENT_FALLBACK_A = "com.coloros.safecenter.startupapp.StartupAppListActivity"

        // Vivo
        private val VIVO_MAIN = "com.iqoo.secure"
        private val VIVO_FALLBACK = "com.vivo.permissionmanager"
        private val VIVO_COMPONENT = "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"
        private val VIVO_COMPONENT_FALLBACK = "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"
        private val VIVO_COMPONENT_FALLBACK_A = "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager"
    }
}

Helper Extension function

inline fun <reified T : Enum<T>> String.toEnum(value: (T) -> String, def: T): T {
    return enumValues<T>().firstOrNull { value(it) == this } ?: def
}
Lokik Soni
  • 602
  • 1
  • 5
  • 25