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:cn.wyl.superwechat.ui.MainActivity.java

private void checkVersion() {
    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);/*w w w. j a  va 2 s .  co m*/
        }
    }
}

From source file:com.fanxin.app.main.fragment.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);/*w  w w  .j  av  a  2 s. c o  m*/
        }
    }

    //make sure activity will not in background if user is logged into another device or removed
    if (savedInstanceState != null && savedInstanceState.getBoolean(Constant.ACCOUNT_REMOVED, false)) {
        DemoHelper.getInstance().logout(false, null);
        finish();
        startActivity(new Intent(this, LoginActivity.class));
        return;
    } else if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) {
        finish();
        startActivity(new Intent(this, LoginActivity.class));
        return;
    }
    setContentView(R.layout.fx_activity_main);
    // runtime permission for android 6.0, just require all permissions here for simple
    requestPermissions();

    initView();

    //umeng api
    MobclickAgent.updateOnlineConfig(this);
    UmengUpdateAgent.setUpdateOnlyWifi(false);
    UmengUpdateAgent.update(this);

    if (getIntent().getBooleanExtra(Constant.ACCOUNT_CONFLICT, false) && !isConflictDialogShow) {
        showConflictDialog();
    } else if (getIntent().getBooleanExtra(Constant.ACCOUNT_REMOVED, false) && !isAccountRemovedDialogShow) {
        showAccountRemovedDialog();
    }

    inviteMessgeDao = new InviteMessgeDao(this);
    userDao = new UserDao(this);
    //        conversationListFragment=(ConversationListFragment)getSupportFragmentManager().findFragmentByTag(TAG_COVERSATION);
    //        contactListFragment=(ContactListFragment)getSupportFragmentManager().findFragmentByTag(TAG_CONTACTS);
    //        fragmentFind=(FragmentFind)getSupportFragmentManager().findFragmentByTag(TAG_FIND);
    //        fragmentProfile=(FragmentProfile)getSupportFragmentManager().findFragmentByTag(TAG_PROFILE);
    //        FragmentTransaction manager = getSupportFragmentManager().beginTransaction();
    //        if(conversationListFragment==null){
    //            conversationListFragment = new ConversationListFragment();
    //            manager.add(R.id.fragment_container, conversationListFragment, TAG_COVERSATION);
    //        }
    //        if(contactListFragment==null){
    //            contactListFragment = new ContactListFragment();
    //            manager.add(R.id.fragment_container, contactListFragment, TAG_CONTACTS);
    //        }
    //        if(fragmentFind==null){
    //            fragmentFind = new FragmentFind();
    //            manager.add(R.id.fragment_container, fragmentFind, TAG_FIND).hide(fragmentFind);
    //        }
    //        if(fragmentProfile==null){
    //            fragmentProfile = new FragmentProfile();
    //            manager.add(R.id.fragment_container, fragmentProfile, TAG_PROFILE).hide(fragmentProfile);
    //        }
    //        manager.show(conversationListFragment).hide(contactListFragment).hide(fragmentFind).hide(fragmentProfile).commit();
    conversationListFragment = new ConversationListFragment();
    contactListFragment = new ContactListFragment();
    fragmentFind = new FragmentFind();
    fragmentProfile = new FragmentProfile();
    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, conversationListFragment)
            .add(R.id.fragment_container, contactListFragment).hide(contactListFragment)
            .show(conversationListFragment).commit();
    fragments = new Fragment[] { conversationListFragment, contactListFragment, fragmentFind, fragmentProfile };
    //register broadcast receiver to receive the change of group from DemoHelper
    registerBroadcastReceiver();

    EMClient.getInstance().contactManager().setContactListener(new MyContactListener());
    //debug purpose only
    registerInternalDebugReceiver();
}

From source file:com.lchtime.safetyexpress.ui.chat.hx.activity.HXMainActivity.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 ww .  j  a v  a  2 s . c  om
        }
    }

    //make sure activity will not in background if user is logged into another device or removed
    if (savedInstanceState != null && savedInstanceState.getBoolean(Constant.ACCOUNT_REMOVED, false)) {
        DemoHelper.getInstance().logout(false, null);
        finish();
        startActivity(new Intent(this, LoginUI.class));
        return;
    } else if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) {
        finish();
        startActivity(new Intent(this, LoginUI.class));
        return;
    }
    setContentView(R.layout.em_activity_main);
    // runtime permission for android 6.0, just require all permissions here for simple

    ub_id = SpTools.getUserId(this);

    requestPermissions();

    initView();

    showExceptionDialogFromIntent(getIntent());

    inviteMessgeDao = new InviteMessgeDao(this);
    UserDao userDao = new UserDao(this);
    conversationListFragment = new ConversationListFragment();
    contactListFragment = new ContactListFragment();
    //      SettingsFragment settingFragment = new SettingsFragment();
    fragments = new Fragment[] { conversationListFragment, contactListFragment };

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

    //register broadcast receiver to receive the change of group from DemoHelper
    registerBroadcastReceiver();

    EMClient.getInstance().contactManager().setContactListener(new MyContactListener());
    //debug purpose only
    registerInternalDebugReceiver();
}

From source file:com.yisheng.ysim.main.fragment.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);//  w  ww  . ja  v a 2 s .c  om
        }
    }

    //make sure activity will not in background if user is logged into another device or removed
    if (savedInstanceState != null && savedInstanceState.getBoolean(Constant.ACCOUNT_REMOVED, false)) {
        DemoHelper.getInstance().logout(false, null);
        finish();
        startActivity(new Intent(this, LoginActivity.class));
        return;
    } else if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) {
        finish();
        startActivity(new Intent(this, LoginActivity.class));
        return;
    }
    setContentView(R.layout.fx_activity_main);
    // runtime permission for android 6.0, just require all permissions here for simple
    requestPermissions();
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
            | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    initView();

    //umeng api
    MobclickAgent.updateOnlineConfig(this);
    UmengUpdateAgent.setUpdateOnlyWifi(false);
    UmengUpdateAgent.update(this);

    if (getIntent().getBooleanExtra(Constant.ACCOUNT_CONFLICT, false) && !isConflictDialogShow) {
        showConflictDialog();
    } else if (getIntent().getBooleanExtra(Constant.ACCOUNT_REMOVED, false) && !isAccountRemovedDialogShow) {
        showAccountRemovedDialog();
    }

    inviteMessgeDao = new InviteMessgeDao(this);
    userDao = new UserDao(this);
    //        conversationListFragment=(ConversationListFragment)getSupportFragmentManager().findFragmentByTag(TAG_COVERSATION);
    //        contactListFragment=(ContactListFragment)getSupportFragmentManager().findFragmentByTag(TAG_CONTACTS);
    //        fragmentFind=(FragmentFind)getSupportFragmentManager().findFragmentByTag(TAG_FIND);
    //        fragmentProfile=(FragmentProfile)getSupportFragmentManager().findFragmentByTag(TAG_PROFILE);
    //        FragmentTransaction manager = getSupportFragmentManager().beginTransaction();
    //        if(conversationListFragment==null){
    //            conversationListFragment = new ConversationListFragment();
    //            manager.add(R.id.fragment_container, conversationListFragment, TAG_COVERSATION);
    //        }
    //        if(contactListFragment==null){
    //            contactListFragment = new ContactListFragment();
    //            manager.add(R.id.fragment_container, contactListFragment, TAG_CONTACTS);
    //        }
    //        if(fragmentFind==null){
    //            fragmentFind = new FragmentFind();
    //            manager.add(R.id.fragment_container, fragmentFind, TAG_FIND).hide(fragmentFind);
    //        }
    //        if(fragmentProfile==null){
    //            fragmentProfile = new FragmentProfile();
    //            manager.add(R.id.fragment_container, fragmentProfile, TAG_PROFILE).hide(fragmentProfile);
    //        }
    //        manager.show(conversationListFragment).hide(contactListFragment).hide(fragmentFind).hide(fragmentProfile).commit();
    conversationListFragment = new ConversationListFragment();
    csFragment = new ChatFragment();
    Bundle csBundle = new Bundle();
    csBundle.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
    csBundle.putString(EaseConstant.EXTRA_USER_ID, "22222");
    csFragment.setArguments(csBundle);
    contactListFragment = new ContactListFragment();
    fragmentFind = new FragmentFind();
    fragmentProfile = new FragmentProfile();
    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, conversationListFragment)
            .add(R.id.fragment_container, contactListFragment).hide(contactListFragment)
            .show(conversationListFragment).commit();
    fragments = new Fragment[] { conversationListFragment, csFragment, contactListFragment, fragmentFind,
            fragmentProfile };
    //register broadcast receiver to receive the change of group from DemoHelper
    registerBroadcastReceiver();

    EMClient.getInstance().contactManager().setContactListener(new MyContactListener());
    //debug purpose only
    registerInternalDebugReceiver();
    Log.e("id", DemoHelper.getInstance().getCurrentUsernName());
    startService(new Intent(this, ContactsService.class));

    //        ----------
    //        if(!DemoHelper.getInstance().getCurrentUsernName().equals("22222")){
    //            Intent intent=new Intent(this, CsActivity.class);
    //            intent.putExtra("userId","22222");
    //            intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, Constant.CHATTYPE_SINGLE);
    //            startActivity(intent);
    //            this.finish();
    //        }

}

From source file:com.xbh.tmi.ui.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DemoApplication.getInstance().addActivity(this);
    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  ww  w.  j  a v a 2 s . c  o m
        }
    }

    //make sure activity will not in background if user is logged into another device or removed
    if (savedInstanceState != null && savedInstanceState.getBoolean(Constant.ACCOUNT_REMOVED, false)) {
        DemoHelper.getInstance().logout(false, null);
        finish();
        startActivity(new Intent(this, LoginActivity.class));
        return;
    } else if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) {
        finish();
        startActivity(new Intent(this, LoginActivity.class));
        return;
    }
    setContentView(R.layout.em_activity_main);
    // runtime permission for android 6.0, just require all permissions here for simple

    SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
    int age = sp.getInt("head", 0);
    final String headPath = "/mnt/sdcard/" + "head" + ".jpg";
    switch (age) {
    case 2://?
        new Thread() {
            @Override
            public void run() {
                SharedPreferences sharedPre = MainActivity.this.getSharedPreferences("config",
                        MainActivity.this.MODE_PRIVATE);
                String userId = sharedPre.getString("username", "");
                PersonManager.SubmitPost(new File(headPath), userId, 1, mHandler);

                SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putInt("head", 20);
                editor.commit();
            }
        }.start();
        break;
    case 3://
        new Thread() {
            @Override
            public void run() {
                SharedPreferences sharedPre = MainActivity.this.getSharedPreferences("config",
                        MainActivity.this.MODE_PRIVATE);
                String userId = sharedPre.getString("username", "");
                PersonManager.SubmitPost(new File(headPath), userId, 1, mHandler);

                SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putInt("head", 30);
                editor.commit();
            }
        }.start();
        break;
    case 4://QQ
        new Thread() {
            @Override
            public void run() {
                SharedPreferences sharedPre = MainActivity.this.getSharedPreferences("config",
                        MainActivity.this.MODE_PRIVATE);
                String userId = sharedPre.getString("username", "");
                PersonManager.SubmitPost(new File(headPath), userId, 1, mHandler);

                SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putInt("head", 40);
                editor.commit();
            }
        }.start();
        break;
    default:
        Log.e("Lking", "???");
        break;
    }

    requestPermissions();

    initView();

    //umeng api
    MobclickAgent.updateOnlineConfig(this);
    UmengUpdateAgent.setUpdateOnlyWifi(false);
    UmengUpdateAgent.update(this);

    if (getIntent().getBooleanExtra(Constant.ACCOUNT_CONFLICT, false) && !isConflictDialogShow) {
        showConflictDialog();
    } else if (getIntent().getBooleanExtra(Constant.ACCOUNT_REMOVED, false) && !isAccountRemovedDialogShow) {
        showAccountRemovedDialog();
    }

    inviteMessgeDao = new InviteMessgeDao(this);
    UserDao userDao = new UserDao(this);
    conversationListFragment = new ConversationListFragment();
    tmFragment = new TmFragment();
    myFragment = new MyFragment();
    huihuaFragment = new HuihuaFragment();
    fragments = new Fragment[] { huihuaFragment, conversationListFragment, tmFragment, myFragment };

    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, huihuaFragment).commit();

    //register broadcast receiver to receive the change of group from DemoHelper
    registerBroadcastReceiver();

    EMClient.getInstance().contactManager().setContactListener(new MyContactListener());
    //debug purpose only
    registerInternalDebugReceiver();
}

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

private void checkDoze() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        final Intent doze = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (!pm.isIgnoringBatteryOptimizations(getPackageName())
                && getPackageManager().resolveActivity(doze, 0) != null) {
            final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            if (!prefs.getBoolean("nodoze", false)) {
                LayoutInflater inflater = LayoutInflater.from(this);
                View view = inflater.inflate(R.layout.doze, null, false);
                final CheckBox cbDontAsk = (CheckBox) view.findViewById(R.id.cbDontAsk);
                dialogDoze = new AlertDialog.Builder(this).setView(view).setCancelable(true)
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                prefs.edit().putBoolean("nodoze", cbDontAsk.isChecked()).apply();
                                startActivity(doze);
                            }/*from  w  ww  .  jav  a 2  s  .  com*/
                        }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                prefs.edit().putBoolean("nodoze", cbDontAsk.isChecked()).apply();
                            }
                        }).setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialogInterface) {
                                dialogDoze = null;
                            }
                        }).create();
                dialogDoze.show();
            }
        }
    }
}

From source file:github.popeen.dsub.service.DownloadService.java

public static void startService(Context context, Intent intent) {
    PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
    if (Build.VERSION.SDK_INT < 26
            || (powerManager != null && powerManager.isIgnoringBatteryOptimizations(intent.getPackage()))) {
        context.startService(intent);//from ww w  .j av  a 2 s.co  m
    } else {
        context.startForegroundService(intent);
    }
}

From source file:com.eveningoutpost.dexdrip.Home.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    mActivity = this;

    if (!xdrip.checkAppContext(getApplicationContext())) {
        toast("Unusual internal context problem - please report");
        Log.wtf(TAG, "xdrip.checkAppContext FAILED!");
        try {//from  w  w w.j a v  a  2s  . c o  m
            xdrip.initCrashlytics(getApplicationContext());
            Crashlytics.log("xdrip.checkAppContext FAILED!");
        } catch (Exception e) {
            // nothing we can do really
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // not much to do
        }
        if (!xdrip.checkAppContext(getApplicationContext())) {
            toast("Cannot start - please report context problem");
            finish();
        }
    }
    xdrip.checkForcedEnglish(Home.this);
    menu_name = getString(R.string.home_screen);

    super.onCreate(savedInstanceState);
    setTheme(R.style.AppThemeToolBarLite); // for toolbar mode

    set_is_follower();

    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    final boolean checkedeula = checkEula();

    //if (Build.VERSION.SDK_INT >= 21) {
    //    getWindow().setNavigationBarColor(Color.BLUE);
    //    getWindow().setStatusBarColor(getCol(X.color_home_chart_background));
    //}
    setContentView(R.layout.activity_home);

    Toolbar mToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(mToolbar);

    //findViewById(R.id.home_layout_holder).setBackgroundColor(getCol(X.color_home_chart_background));
    this.dexbridgeBattery = (TextView) findViewById(R.id.textBridgeBattery);
    this.parakeetBattery = (TextView) findViewById(R.id.parakeetbattery);
    this.sensorAge = (TextView) findViewById(R.id.libstatus);
    this.extraStatusLineText = (TextView) findViewById(R.id.extraStatusLine);
    this.currentBgValueText = (TextView) findViewById(R.id.currentBgValueRealTime);

    extraStatusLineText.setText("");
    dexbridgeBattery.setText("");
    parakeetBattery.setText("");
    sensorAge.setText("");

    if (BgGraphBuilder.isXLargeTablet(getApplicationContext())) {
        this.currentBgValueText.setTextSize(100);
    }
    this.notificationText = (TextView) findViewById(R.id.notices);
    if (BgGraphBuilder.isXLargeTablet(getApplicationContext())) {
        this.notificationText.setTextSize(40);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Intent intent = new Intent();
        String packageName = getPackageName();
        Log.d(TAG, "Maybe ignoring battery optimization");
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (!pm.isIgnoringBatteryOptimizations(packageName)
                && !prefs.getBoolean("requested_ignore_battery_optimizations_new", false)) {
            Log.d(TAG, "Requesting ignore battery optimization");
            // prefs.edit().putBoolean("requested_ignore_battery_optimizations", true).apply();
            intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
            startActivity(intent);
            JoH.static_toast_long("Select YES for best performance!");
        }
    }

    // jamorham voice input et al
    this.voiceRecognitionText = (TextView) findViewById(R.id.treatmentTextView);
    this.textBloodGlucose = (TextView) findViewById(R.id.textBloodGlucose);
    this.textCarbohydrates = (TextView) findViewById(R.id.textCarbohydrate);
    this.textInsulinDose = (TextView) findViewById(R.id.textInsulinUnits);
    this.textTime = (TextView) findViewById(R.id.textTimeButton);
    this.btnBloodGlucose = (ImageButton) findViewById(R.id.bloodTestButton);
    this.btnCarbohydrates = (ImageButton) findViewById(R.id.buttonCarbs);
    this.btnInsulinDose = (ImageButton) findViewById(R.id.buttonInsulin);
    this.btnCancel = (ImageButton) findViewById(R.id.cancelTreatment);
    this.btnApprove = (ImageButton) findViewById(R.id.approveTreatment);
    this.btnTime = (ImageButton) findViewById(R.id.timeButton);
    this.btnUndo = (ImageButton) findViewById(R.id.btnUndo);
    this.btnRedo = (ImageButton) findViewById(R.id.btnRedo);
    this.btnVehicleMode = (ImageButton) findViewById(R.id.vehicleModeButton);

    hideAllTreatmentButtons();

    if (searchWords == null) {
        initializeSearchWords("");
    }

    this.btnSpeak = (ImageButton) findViewById(R.id.btnTreatment);
    btnSpeak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            promptTextInput();

        }
    });
    btnSpeak.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            promptSpeechInput();
            return true;
        }
    });

    this.btnNote = (ImageButton) findViewById(R.id.btnNote);
    btnNote.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (Home.getPreferencesBooleanDefaultFalse("default_to_voice_notes")) {
                showNoteTextInputDialog(v, 0);
            } else {
                promptSpeechNoteInput(v);
            }
            return false;
        }
    });
    btnNote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!Home.getPreferencesBooleanDefaultFalse("default_to_voice_notes")) {
                showNoteTextInputDialog(v, 0);
            } else {
                promptSpeechNoteInput(v);
            }
        }
    });

    btnCancel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            cancelTreatment();
        }
    });

    btnApprove.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            processAndApproveTreatment();
        }
    });

    btnInsulinDose.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // proccess and approve treatment
            textInsulinDose.setVisibility(View.INVISIBLE);
            btnInsulinDose.setVisibility(View.INVISIBLE);
            Treatments.create(0, thisinsulinnumber, Treatments.getTimeStampWithOffset(thistimeoffset));
            reset_viewport = true;
            if (hideTreatmentButtonsIfAllDone()) {
                updateCurrentBgInfo("insulin button");
            }
        }
    });
    btnCarbohydrates.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // proccess and approve treatment
            textCarbohydrates.setVisibility(View.INVISIBLE);
            btnCarbohydrates.setVisibility(View.INVISIBLE);
            reset_viewport = true;
            Treatments.create(thiscarbsnumber, 0, Treatments.getTimeStampWithOffset(thistimeoffset));
            if (hideTreatmentButtonsIfAllDone()) {
                updateCurrentBgInfo("carbs button");
            }
        }
    });

    btnBloodGlucose.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            reset_viewport = true;
            processCalibration();
        }
    });

    btnTime.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // clears time if clicked
            textTime.setVisibility(View.INVISIBLE);
            btnTime.setVisibility(View.INVISIBLE);
            if (hideTreatmentButtonsIfAllDone()) {
                updateCurrentBgInfo("time button");
            }
        }
    });

    final DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screen_width = dm.widthPixels;
    int screen_height = dm.heightPixels;

    if (screen_width <= 320) {
        small_width = true;
        small_screen = true;
    }
    if (screen_height <= 320) {
        small_height = true;
        small_screen = true;
    }
    final int refdpi = 320;
    Log.d(TAG, "Width height: " + screen_width + " " + screen_height + " DPI:" + dm.densityDpi);

    JoH.fixActionBar(this);
    try {
        getSupportActionBar().setTitle(R.string.app_name);
    } catch (NullPointerException e) {
        Log.e(TAG, "Couldn't set title due to null pointer");
    }
    activityVisible = true;

    // handle incoming extras
    Bundle bundle = getIntent().getExtras();
    processIncomingBundle(bundle);

    checkBadSettings();

    // lower priority
    PlusSyncService.startSyncService(getApplicationContext(), "HomeOnCreate");
    ParakeetHelper.notifyOnNextCheckin(false);

    if ((checkedeula) && (!getString(R.string.app_name).equals("xDrip+"))) {
        showcasemenu(SHOWCASE_VARIANT);
    }

}

From source file:com.sentaroh.android.SMBSync2.ActivityMain.java

@SuppressLint("NewApi")
private void checkBatteryOptimization() {
    if (Build.VERSION.SDK_INT >= 23) {
        Intent intent = new Intent();
        String packageName = mContext.getPackageName();
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        if (pm.isIgnoringBatteryOptimizations(packageName))
            intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
        else {//from   w w w  .j  a  va2  s. c  om
            intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
        }
        startActivity(intent);
    }
}