Example usage for android.os PowerManager isIgnoringBatteryOptimizations

List of usage examples for android.os PowerManager isIgnoringBatteryOptimizations

Introduction

In this page you can find the example usage for android.os PowerManager isIgnoringBatteryOptimizations.

Prototype

public boolean isIgnoringBatteryOptimizations(String packageName) 

Source Link

Document

Return whether the given application package name is on the device's power whitelist.

Usage

From source file:com.mendhak.gpslogger.common.Systems.java

/**
 * Returns true if the device is in Doze/Idle mode. Should be called before checking the network connection because
 * the ConnectionManager may report the device is connected when it isn't during Idle mode.
 * https://github.com/yigit/android-priority-jobqueue/blob/master/jobqueue/src/main/java/com/path/android/jobqueue/network/NetworkUtilImpl.java#L60
 *//*from w  w w .j  ava 2  s.c  om*/
@TargetApi(23)
public static boolean isDozing(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return powerManager.isDeviceIdleMode()
                && !powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
    } else {
        return false;
    }
}

From source file:at.bitfire.davdroid.ui.StartupDialogFragment.java

public static StartupDialogFragment[] getStartupDialogs(Context context) {
    List<StartupDialogFragment> dialogs = new LinkedList<>();

    @Cleanup/*from   w  w w. j a va2 s.  c  om*/
    ServiceDB.OpenHelper dbHelper = new ServiceDB.OpenHelper(context);
    Settings settings = new Settings(dbHelper.getReadableDatabase());

    if (BuildConfig.VERSION_NAME.contains("-alpha") || BuildConfig.VERSION_NAME.contains("-beta")
            || BuildConfig.VERSION_NAME.contains("-rc"))
        dialogs.add(StartupDialogFragment.instantiate(Mode.DEVELOPMENT_VERSION));
    else
        dialogs.add(StartupDialogFragment.instantiate(Mode.FDROID_DONATE));

    // battery optimization whitelisting
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
            && settings.getBoolean(HINT_BATTERY_OPTIMIZATIONS, true)) {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (!powerManager.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID))
            dialogs.add(StartupDialogFragment.instantiate(Mode.BATTERY_OPTIMIZATIONS));
    }

    // OpenTasks information
    if (!LocalTaskList.tasksProviderAvailable(context)
            && settings.getBoolean(HINT_OPENTASKS_NOT_INSTALLED, true))
        dialogs.add(StartupDialogFragment.instantiate(Mode.OPENTASKS_NOT_INSTALLED));

    Collections.reverse(dialogs);
    return dialogs.toArray(new StartupDialogFragment[dialogs.size()]);
}

From source file:org.deviceconnect.android.manager.core.util.DConnectUtil.java

/**
 * ??Context??Doze?????????./*from w  w  w.j a va2s .  c  o  m*/
 * <p>
 * Android M ?? OS ????true????
 * </p>
 *
 * @param context 
 * @return Doze????true????false
 */
public static boolean isDozeMode(final Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    } else {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
    }
}

From source file:android_network.hetnet.vpn_service.Util.java

@TargetApi(Build.VERSION_CODES.M)
public static boolean batteryOptimizing(Context context) {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    return !pm.isIgnoringBatteryOptimizations(context.getPackageName());
}

From source file:com.commonsware.android.antidoze.EventDemoActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
        String pkg = getPackageName();
        PowerManager pm = getSystemService(PowerManager.class);

        if (!pm.isIgnoringBatteryOptimizations(pkg)) {
            Intent i = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
                    .setData(Uri.parse("package:" + pkg));

            startActivity(i);/* w  w  w . jav  a 2 s.  c om*/
        }
    }

    if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
        getSupportFragmentManager().beginTransaction().add(android.R.id.content, new EventLogFragment())
                .commit();
        startService(new Intent(this, ScheduledService.class));
    }
}

From source file:org.microg.tools.selfcheck.SystemChecks.java

private void isBatterySavingDisabled(final Context context, ResultCollector collector) {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    collector.addResult(context.getString(R.string.self_check_name_battery_optimizations),
            pm.isIgnoringBatteryOptimizations(context.getPackageName()) ? Positive : Negative,
            context.getString(R.string.self_check_resolution_battery_optimizations), this);
}

From source file:com.vurtex.wakeup.activity.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        String packageName = getPackageName();
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (!pm.isIgnoringBatteryOptimizations(packageName)) {
            Intent intent = new Intent();
            intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
            startActivity(intent);//from w w w .j  a v a  2  s.c om
        }
    }

    //make sure activity will not in background if user is logged into another device or removed

    setContentView(R.layout.activity_main);

    initView();

    conversationListFragment = new Item1Fragment();
    contactListFragment = new Item1Fragment();
    Fragment settingFragment = new Item1Fragment();
    fragments = new Fragment[] { conversationListFragment, contactListFragment, settingFragment };

    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, conversationListFragment)
            .add(R.id.fragment_container, contactListFragment).hide(contactListFragment)
            .show(conversationListFragment).commit();

    //register broadcast receiver to receive the change

}

From source file:be.blinkt.openvpn.activities.MainActivity.java

@TargetApi(Build.VERSION_CODES.M)
private void requestDozeDisable() {
    Intent intent = new Intent();
    String packageName = getPackageName();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName))
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    else {//from w  ww.j av a2s.  c  om
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
    }
    startActivity(intent);
}

From source file:com.master.metehan.filtereagle.Util.java

public static String getGeneralInfo(Context context) {
    StringBuilder sb = new StringBuilder();
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    sb.append(String.format("Interactive %B\r\n", isInteractive(context)));
    sb.append(String.format("Connected %B\r\n", isConnected(context)));
    sb.append(String.format("WiFi %B\r\n", isWifiActive(context)));
    sb.append(String.format("Metered %B\r\n", isMeteredNetwork(context)));
    sb.append(String.format("Roaming %B\r\n", isRoaming(context)));

    sb.append(String.format("Type %s\r\n", getPhoneTypeName(tm.getPhoneType())));

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1 || !hasPhoneStatePermission(context)) {
        if (tm.getSimState() == TelephonyManager.SIM_STATE_READY)
            sb.append(String.format("SIM %s/%s/%s\r\n", tm.getSimCountryIso(), tm.getSimOperatorName(),
                    tm.getSimOperator()));
        if (tm.getNetworkType() != TelephonyManager.NETWORK_TYPE_UNKNOWN)
            sb.append(String.format("Network %s/%s/%s\r\n", tm.getNetworkCountryIso(),
                    tm.getNetworkOperatorName(), tm.getNetworkOperator()));
    }/*from www  .j  a  v  a 2 s  . com*/

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        sb.append(String.format("Power saving %B\r\n", pm.isPowerSaveMode()));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        sb.append(String.format("Battery optimizing %B\r\n",
                !pm.isIgnoringBatteryOptimizations(context.getPackageName())));

    if (sb.length() > 2)
        sb.setLength(sb.length() - 2);

    return sb.toString();
}

From source file:com.easemob.helpdeskdemo.ui.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        String packageName = getPackageName();
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (!pm.isIgnoringBatteryOptimizations(packageName)) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
            startActivity(intent);// www  .j  a  va  2  s .  co m
        }
    }

    setContentView(R.layout.em_activity_main);
    if (savedInstanceState != null) {
        currentTabIndex = savedInstanceState.getInt("selectedIndex", 0);
    }
    if (shopFragment != null) {
        shopFragment = getSupportFragmentManager().findFragmentByTag(shopFragment.getClass().getName());
        settingFragment = getSupportFragmentManager().findFragmentByTag(settingFragment.getClass().getName());
        ticketListFragment = getSupportFragmentManager()
                .findFragmentByTag(ticketListFragment.getClass().getName());
        fragments = new Fragment[] { shopFragment, ticketListFragment, settingFragment };
        getSupportFragmentManager().beginTransaction().hide(shopFragment).hide(settingFragment)
                .hide(ticketListFragment).show(fragments[currentTabIndex]).commit();

    } else {
        shopFragment = new ShopFragment();
        settingFragment = new SettingFragment();
        ticketListFragment = new TicketListFragment();
        fragments = new Fragment[] { shopFragment, ticketListFragment, settingFragment };
        // shopFragment?
        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
        trx.add(R.id.fragment_container, shopFragment, shopFragment.getClass().getName())
                .add(R.id.fragment_container, ticketListFragment, ticketListFragment.getClass().getName())
                .add(R.id.fragment_container, settingFragment, settingFragment.getClass().getName())
                .hide(settingFragment).hide(ticketListFragment).hide(shopFragment)
                .show(fragments[currentTabIndex]);
        trx.commit();
    }
    mBottomNav = $(R.id.bottom_navigation);
    mBottomNav.setBottomNavigationSelectedListener(this);
    //??listener
    connectionListener = new MyConnectionListener();
    ChatClient.getInstance().addConnectionListener(connectionListener);
    //6.0????target api?23demo??????
    requestPermissions();
}