Example usage for android.provider Settings ACTION_MANAGE_OVERLAY_PERMISSION

List of usage examples for android.provider Settings ACTION_MANAGE_OVERLAY_PERMISSION

Introduction

In this page you can find the example usage for android.provider Settings ACTION_MANAGE_OVERLAY_PERMISSION.

Prototype

String ACTION_MANAGE_OVERLAY_PERMISSION

To view the source code for android.provider Settings ACTION_MANAGE_OVERLAY_PERMISSION.

Click Source Link

Document

Activity Action: Show screen for controlling which apps can draw on top of other apps.

Usage

From source file:com.tencent.wstt.gt.activity.GTMainActivity.java

private void requestAlertWindowPermission() {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
    intent.setData(Uri.parse("package:" + getPackageName()));
    try {//from   w w  w  .j av a2  s  .  com
        startActivityForResult(intent, REQUEST_FLOAT_VIEW);
    } catch (Exception e) {
        // ????
    }

}

From source file:com.example.captain_miao.grantap.ShadowPermissionActivity.java

@TargetApi(value = Build.VERSION_CODES.M)
public void requestPermissions(List<String> needPermissions) {
    //first SYSTEM_ALERT_WINDOW
    if (!hasRequestedSystemAlertWindow && !TextUtils.isEmpty(permissionSystemAlertWindow)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + packageName));
        startActivityForResult(intent, REQ_CODE_REQUEST_SYSTEM_ALERT_WINDOW);
    } else if (!hasRequestedWriteSettings && !TextUtils.isEmpty(permissionWriteSettings)) {
        //second WRITE_SETTINGS
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + packageName));
        startActivityForResult(intent, REQ_CODE_REQUEST_WRITE_SETTING);
    } else {/*w ww. j av  a2 s . c om*/
        //other permission
        ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]),
                REQ_CODE_PERMISSION_REQUEST);
    }
}

From source file:com.github.yuqilin.qmediaplayerapp.util.Permissions.java

private static Dialog createSettingsDialogCompat(final Activity activity, int mode) {
    int titleId = 0, textId = 0;
    String action = Settings.ACTION_MANAGE_WRITE_SETTINGS;
    switch (mode) {
    case PERMISSION_SYSTEM_RINGTONE:
        titleId = R.string.allow_settings_access_ringtone_title;
        textId = R.string.allow_settings_access_ringtone_description;
        break;/*from w  ww .j av  a2s .c o m*/
    case PERMISSION_SYSTEM_BRIGHTNESS:
        titleId = R.string.allow_settings_access_brightness_title;
        textId = R.string.allow_settings_access_brightness_description;
        break;
    case PERMISSION_SYSTEM_DRAW_OVRLAYS:
        titleId = R.string.allow_draw_overlays_title;
        textId = R.string.allow_sdraw_overlays_description;
        action = Settings.ACTION_MANAGE_OVERLAY_PERMISSION;
        break;
    }
    final String finalAction = action;
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity).setTitle(activity.getString(titleId))
            .setMessage(activity.getString(textId)).setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton(activity.getString(R.string.permission_ask_again),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int whichButton) {
                            SharedPreferences settings = PreferenceManager
                                    .getDefaultSharedPreferences(activity);
                            Intent i = new Intent(finalAction);
                            i.setData(Uri.parse("package:" + activity.getPackageName()));
                            try {
                                activity.startActivity(i);
                            } catch (Exception ex) {
                            }
                            SharedPreferences.Editor editor = settings.edit();
                            editor.putBoolean("user_declined_settings_access", true);
                            Util.commitPreferences(editor);
                        }
                    });
    return dialogBuilder.show();
}

From source file:com.wanderfar.expander.Settings.SettingsActivity.java

private void requestSystemAlertPermission(Activity context, Fragment fragment, int requestCode) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        setFloatingUIEnabledorDisabled();
    } else {//from w  ww. ja  v a  2 s.c om
        final String packageName = context == null ? fragment.getActivity().getPackageName()
                : context.getPackageName();
        final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + packageName));
        if (fragment != null)
            fragment.startActivityForResult(intent, requestCode);
        else
            context.startActivityForResult(intent, requestCode);
    }

}

From source file:org.videolan.vlc.util.Permissions.java

private static Dialog createSettingsDialogCompat(final Activity activity, int mode) {
    int titleId = 0, textId = 0;
    String action = Settings.ACTION_MANAGE_WRITE_SETTINGS;
    switch (mode) {
    case PERMISSION_SYSTEM_RINGTONE:
        titleId = R.string.allow_settings_access_ringtone_title;
        textId = R.string.allow_settings_access_ringtone_description;
        break;/*from  www .  ja v  a  2 s  .  c  om*/
    case PERMISSION_SYSTEM_BRIGHTNESS:
        titleId = R.string.allow_settings_access_brightness_title;
        textId = R.string.allow_settings_access_brightness_description;
        break;
    case PERMISSION_SYSTEM_DRAW_OVRLAYS:
        titleId = R.string.allow_draw_overlays_title;
        textId = R.string.allow_sdraw_overlays_description;
        action = Settings.ACTION_MANAGE_OVERLAY_PERMISSION;
        break;
    }
    final String finalAction = action;
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity).setTitle(activity.getString(titleId))
            .setMessage(activity.getString(textId)).setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton(activity.getString(R.string.permission_ask_again),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int whichButton) {
                            SharedPreferences settings = PreferenceManager
                                    .getDefaultSharedPreferences(activity);
                            Intent i = new Intent(finalAction);
                            i.setData(Uri.parse("package:" + activity.getPackageName()));
                            try {
                                activity.startActivity(i);
                            } catch (Exception ex) {
                            }
                            SharedPreferences.Editor editor = settings.edit();
                            editor.putBoolean("user_declined_settings_access", true);
                            editor.apply();
                        }
                    });
    return dialogBuilder.show();
}

From source file:com.pluscubed.velociraptor.SettingsActivity.java

@SuppressWarnings("ConstantConditions")
@Override//from   w w  w . j a v a2 s.c o  m
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    ButterKnife.bind(this);

    setSupportActionBar(toolbar);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        View marshmallowPermissionsCard = findViewById(R.id.card_m_permissions);
        marshmallowPermissionsCard.setVisibility(View.GONE);
    }

    openStreetMapView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ShareCompat.IntentBuilder.from(SettingsActivity.this).setText("https://www.openstreetmap.org")
                    .setType("text/plain").startChooser();
        }
    });

    checkCoverageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mGoogleApiClient = new GoogleApiClient.Builder(SettingsActivity.this)
                    .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                        @Override
                        @SuppressWarnings("MissingPermission")
                        public void onConnected(@Nullable Bundle bundle) {
                            String uriString = "http://product.itoworld.com/map/124";
                            if (isLocationPermissionGranted()) {
                                Location lastLocation = LocationServices.FusedLocationApi
                                        .getLastLocation(mGoogleApiClient);
                                if (lastLocation != null) {
                                    uriString += "?lon=" + lastLocation.getLongitude() + "&lat="
                                            + lastLocation.getLatitude() + "&zoom=12";
                                }
                            }
                            Intent intent = new Intent();
                            intent.setData(Uri.parse(uriString));
                            intent.setAction(Intent.ACTION_VIEW);
                            try {
                                startActivity(intent);
                            } catch (ActivityNotFoundException e) {
                                Snackbar.make(enableFloatingButton, R.string.open_coverage_map_failed,
                                        Snackbar.LENGTH_LONG).show();
                            }

                            mGoogleApiClient.disconnect();
                        }

                        @Override
                        public void onConnectionSuspended(int i) {
                        }
                    }).addApi(LocationServices.API).build();

            mGoogleApiClient.connect();
        }
    });

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    View notifControls = findViewById(R.id.switch_notif_controls);
    notifControls.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SettingsActivity.this, FloatingService.class);
            intent.putExtra(FloatingService.EXTRA_NOTIF_START, true);
            PendingIntent pending = PendingIntent.getService(SettingsActivity.this, PENDING_SERVICE, intent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Intent intentClose = new Intent(SettingsActivity.this, FloatingService.class);
            intentClose.putExtra(FloatingService.EXTRA_NOTIF_CLOSE, true);
            PendingIntent pendingClose = PendingIntent.getService(SettingsActivity.this, PENDING_SERVICE_CLOSE,
                    intentClose, PendingIntent.FLAG_CANCEL_CURRENT);

            Intent settings = new Intent(SettingsActivity.this, SettingsActivity.class);
            PendingIntent settingsIntent = PendingIntent.getActivity(SettingsActivity.this, PENDING_SETTINGS,
                    settings, PendingIntent.FLAG_CANCEL_CURRENT);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(SettingsActivity.this)
                    .setSmallIcon(R.drawable.ic_speedometer)
                    .setContentTitle(getString(R.string.controls_notif_title))
                    .setContentText(getString(R.string.controls_notif_desc))
                    .addAction(0, getString(R.string.show), pending)
                    .addAction(0, getString(R.string.hide), pendingClose).setDeleteIntent(pendingClose)
                    .setContentIntent(settingsIntent);
            Notification notification = builder.build();
            notificationManager.notify(NOTIFICATION_CONTROLS, notification);
        }
    });

    Button openAppSelection = (Button) findViewById(R.id.button_app_selection);
    openAppSelection.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(SettingsActivity.this, AppSelectionActivity.class));
        }
    });

    autoDisplaySwitch.setChecked(PrefUtils.isAutoDisplayEnabled(this));
    autoDisplaySwitch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean autoDisplayEnabled = autoDisplaySwitch.isChecked();
            PrefUtils.setAutoDisplay(SettingsActivity.this, autoDisplayEnabled);
            updateAppDetectionEnabled(autoDisplayEnabled);
        }
    });

    enableServiceButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
            } catch (ActivityNotFoundException e) {
                Snackbar.make(enableServiceButton, R.string.open_settings_failed_accessibility,
                        Snackbar.LENGTH_LONG).show();
            }
        }
    });

    enableFloatingButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                //Open the current default browswer App Info page
                openSettings(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, BuildConfig.APPLICATION_ID);
            } catch (ActivityNotFoundException ignored) {
                Snackbar.make(enableFloatingButton, R.string.open_settings_failed_overlay, Snackbar.LENGTH_LONG)
                        .show();
            }
        }
    });

    enableLocationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityCompat.requestPermissions(SettingsActivity.this,
                    new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQUEST_LOCATION);
        }
    });

    ArrayAdapter<String> unitAdapter = new ArrayAdapter<>(this, R.layout.spinner_item_text,
            new String[] { "mph", "km/h" });
    unitAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
    unitSpinner.setAdapter(unitAdapter);
    unitSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (PrefUtils.getUseMetric(SettingsActivity.this) != (position == 1)) {
                PrefUtils.setUseMetric(SettingsActivity.this, position == 1);
                unitSpinner.setDropDownVerticalOffset(Utils.convertDpToPx(SettingsActivity.this,
                        unitSpinner.getSelectedItemPosition() * -48));

                updateFloatingServicePrefs();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    unitSpinner.setSelection(PrefUtils.getUseMetric(this) ? 1 : 0);
    unitSpinner
            .setDropDownVerticalOffset(Utils.convertDpToPx(this, unitSpinner.getSelectedItemPosition() * -48));

    ArrayAdapter<String> styleAdapter = new ArrayAdapter<>(this, R.layout.spinner_item_text,
            new String[] { getString(R.string.united_states), getString(R.string.international) });
    styleAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
    styleSpinner.setAdapter(styleAdapter);
    styleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (position != PrefUtils.getSignStyle(SettingsActivity.this)) {
                PrefUtils.setSignStyle(SettingsActivity.this, position);
                styleSpinner.setDropDownVerticalOffset(Utils.convertDpToPx(SettingsActivity.this,
                        styleSpinner.getSelectedItemPosition() * -48));

                updateFloatingServicePrefs();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    styleSpinner.setSelection(PrefUtils.getSignStyle(this));
    styleSpinner
            .setDropDownVerticalOffset(Utils.convertDpToPx(this, styleSpinner.getSelectedItemPosition() * -48));

    toleranceView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new ToleranceDialogFragment().show(getFragmentManager(), "dialog_tolerance");
        }
    });

    showSpeedometerSwitch.setChecked(PrefUtils.getShowSpeedometer(this));
    ((View) showSpeedometerSwitch.getParent()).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSpeedometerSwitch.setChecked(!showSpeedometerSwitch.isChecked());

            PrefUtils.setShowSpeedometer(SettingsActivity.this, showSpeedometerSwitch.isChecked());

            updateFloatingServicePrefs();
        }
    });

    debuggingSwitch.setChecked(PrefUtils.isDebuggingEnabled(this));
    ((View) debuggingSwitch.getParent()).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            debuggingSwitch.setChecked(!debuggingSwitch.isChecked());

            PrefUtils.setDebugging(SettingsActivity.this, debuggingSwitch.isChecked());

            updateFloatingServicePrefs();
        }
    });

    beepSwitch.setChecked(PrefUtils.isBeepAlertEnabled(this));
    beepSwitch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PrefUtils.setBeepAlertEnabled(SettingsActivity.this, beepSwitch.isChecked());
        }
    });
    testBeepButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Utils.playBeep();
        }
    });

    androidAutoSwitch.setChecked(PrefUtils.isAutoDisplayEnabled(this));
    androidAutoSwitch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean checked = androidAutoSwitch.isChecked();
            if (checked) {
                new MaterialDialog.Builder(SettingsActivity.this)
                        .content(R.string.android_auto_instruction_dialog).positiveText(android.R.string.ok)
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                PrefUtils.setAutoIntegrationEnabled(SettingsActivity.this, true);
                            }
                        }).show();
            } else {
                PrefUtils.setAutoIntegrationEnabled(SettingsActivity.this, checked);
            }
        }
    });

    invalidateStates();

    if (BuildConfig.VERSION_CODE > PrefUtils.getVersionCode(this) && !PrefUtils.isFirstRun(this)) {
        showChangelog();
    }

    billingProcessor = new BillingProcessor(this, getString(R.string.play_license_key),
            new BillingProcessor.IBillingHandler() {
                @Override
                public void onProductPurchased(String productId, TransactionDetails details) {
                    PrefUtils.setSupported(SettingsActivity.this, true);
                    if (Arrays.asList(PURCHASES).contains(productId))
                        billingProcessor.consumePurchase(productId);
                }

                @Override
                public void onPurchaseHistoryRestored() {

                }

                @Override
                public void onBillingError(int errorCode, Throwable error) {
                    if (errorCode != 110) {
                        Snackbar.make(findViewById(android.R.id.content),
                                "Billing error: code = " + errorCode + ", error: "
                                        + (error != null ? error.getMessage() : "?"),
                                Snackbar.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onBillingInitialized() {
                    billingProcessor.loadOwnedPurchasesFromGoogle();
                }
            });

    PrefUtils.setFirstRun(this, false);
    PrefUtils.setVersionCode(this, BuildConfig.VERSION_CODE);
}

From source file:com.pluscubed.velociraptor.settings.SettingsActivity.java

@SuppressWarnings("ConstantConditions")
@Override//from   w w w.j  av  a2 s.c  o  m
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    ButterKnife.bind(this);

    setSupportActionBar(toolbar);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        View marshmallowPermissionsCard = findViewById(R.id.card_m_permissions);
        marshmallowPermissionsCard.setVisibility(View.GONE);
    }

    openStreetMapView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ShareCompat.IntentBuilder.from(SettingsActivity.this).setText("https://www.openstreetmap.org")
                    .setType("text/plain").startChooser();
        }
    });

    checkCoverageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            googleApiClient = new GoogleApiClient.Builder(SettingsActivity.this)
                    .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                        @Override
                        @SuppressWarnings("MissingPermission")
                        public void onConnected(@Nullable Bundle bundle) {
                            String uriString = "http://product.itoworld.com/map/124";
                            if (Utils.isLocationPermissionGranted(SettingsActivity.this)) {
                                Location lastLocation = LocationServices.FusedLocationApi
                                        .getLastLocation(googleApiClient);
                                if (lastLocation != null) {
                                    uriString += "?lon=" + lastLocation.getLongitude() + "&lat="
                                            + lastLocation.getLatitude() + "&zoom=12";
                                }
                            }
                            Intent intent = new Intent();
                            intent.setData(Uri.parse(uriString));
                            intent.setAction(Intent.ACTION_VIEW);
                            try {
                                startActivity(intent);
                            } catch (ActivityNotFoundException e) {
                                Snackbar.make(enableFloatingButton, R.string.open_coverage_map_failed,
                                        Snackbar.LENGTH_LONG).show();
                            }

                            googleApiClient.disconnect();
                        }

                        @Override
                        public void onConnectionSuspended(int i) {
                        }
                    }).addApi(LocationServices.API).build();

            googleApiClient.connect();
        }
    });

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notifControlsContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SettingsActivity.this, SpeedLimitService.class);
            intent.putExtra(SpeedLimitService.EXTRA_NOTIF_START, true);
            PendingIntent pending = PendingIntent.getService(SettingsActivity.this, PENDING_SERVICE, intent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Intent intentClose = new Intent(SettingsActivity.this, SpeedLimitService.class);
            intentClose.putExtra(SpeedLimitService.EXTRA_NOTIF_CLOSE, true);
            PendingIntent pendingClose = PendingIntent.getService(SettingsActivity.this, PENDING_SERVICE_CLOSE,
                    intentClose, PendingIntent.FLAG_CANCEL_CURRENT);

            Intent settings = new Intent(SettingsActivity.this, SettingsActivity.class);
            PendingIntent settingsIntent = PendingIntent.getActivity(SettingsActivity.this, PENDING_SETTINGS,
                    settings, PendingIntent.FLAG_CANCEL_CURRENT);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(SettingsActivity.this)
                    .setSmallIcon(R.drawable.ic_speedometer)
                    .setContentTitle(getString(R.string.controls_notif_title))
                    .setContentText(getString(R.string.controls_notif_desc))
                    .addAction(0, getString(R.string.show), pending)
                    .addAction(0, getString(R.string.hide), pendingClose).setDeleteIntent(pendingClose)
                    .setContentIntent(settingsIntent);
            Notification notification = builder.build();
            notificationManager.notify(NOTIFICATION_CONTROLS, notification);
        }
    });

    appSelectionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(SettingsActivity.this, AppSelectionActivity.class));
        }
    });

    appDetectionSwitch.setChecked(PrefUtils.isAutoDisplayEnabled(this));
    appDetectionContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            appDetectionSwitch.toggle();
            boolean autoDisplayEnabled = appDetectionSwitch.isChecked();
            PrefUtils.setAutoDisplay(SettingsActivity.this, autoDisplayEnabled);
            updateAppDetectionOptionStates();
        }
    });

    enableServiceButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
            } catch (ActivityNotFoundException e) {
                Snackbar.make(enableServiceButton, R.string.open_settings_failed_accessibility,
                        Snackbar.LENGTH_LONG).show();
            }
        }
    });

    enableFloatingButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                //Open the current default browswer App Info page
                openSettings(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, BuildConfig.APPLICATION_ID);
            } catch (ActivityNotFoundException ignored) {
                Snackbar.make(enableFloatingButton, R.string.open_settings_failed_overlay, Snackbar.LENGTH_LONG)
                        .show();
            }
        }
    });

    enableLocationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ActivityCompat.requestPermissions(SettingsActivity.this,
                    new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQUEST_LOCATION);
        }
    });

    ArrayAdapter<String> unitAdapter = new ArrayAdapter<>(this, R.layout.spinner_item_text,
            new String[] { "mph", "km/h" });
    unitAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
    unitSpinner.setAdapter(unitAdapter);
    unitSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (PrefUtils.getUseMetric(SettingsActivity.this) != (position == 1)) {
                PrefUtils.setUseMetric(SettingsActivity.this, position == 1);
                unitSpinner.setDropDownVerticalOffset(Utils.convertDpToPx(SettingsActivity.this,
                        unitSpinner.getSelectedItemPosition() * -48));

                Utils.updateFloatingServicePrefs(SettingsActivity.this);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    unitSpinner.setSelection(PrefUtils.getUseMetric(this) ? 1 : 0);
    unitSpinner
            .setDropDownVerticalOffset(Utils.convertDpToPx(this, unitSpinner.getSelectedItemPosition() * -48));

    ArrayAdapter<String> styleAdapter = new ArrayAdapter<>(this, R.layout.spinner_item_text,
            new String[] { getString(R.string.united_states), getString(R.string.international) });
    styleAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
    styleSpinner.setAdapter(styleAdapter);
    styleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (position != PrefUtils.getSignStyle(SettingsActivity.this)) {
                PrefUtils.setSignStyle(SettingsActivity.this, position);
                styleSpinner.setDropDownVerticalOffset(Utils.convertDpToPx(SettingsActivity.this,
                        styleSpinner.getSelectedItemPosition() * -48));

                Utils.updateFloatingServicePrefs(SettingsActivity.this);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    styleSpinner.setSelection(PrefUtils.getSignStyle(this));
    styleSpinner
            .setDropDownVerticalOffset(Utils.convertDpToPx(this, styleSpinner.getSelectedItemPosition() * -48));

    toleranceView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new ToleranceDialogFragment().show(getFragmentManager(), "dialog_tolerance");
        }
    });

    sizeView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new SizeDialogFragment().show(getFragmentManager(), "dialog_size");
        }
    });

    opacityView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new OpacityDialogFragment().show(getFragmentManager(), "dialog_opacity");
        }
    });

    showSpeedometerSwitch.setChecked(PrefUtils.getShowSpeedometer(this));
    ((View) showSpeedometerSwitch.getParent()).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSpeedometerSwitch.setChecked(!showSpeedometerSwitch.isChecked());

            PrefUtils.setShowSpeedometer(SettingsActivity.this, showSpeedometerSwitch.isChecked());

            Utils.updateFloatingServicePrefs(SettingsActivity.this);
        }
    });

    debuggingSwitch.setChecked(PrefUtils.isDebuggingEnabled(this));
    ((View) debuggingSwitch.getParent()).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            debuggingSwitch.setChecked(!debuggingSwitch.isChecked());

            PrefUtils.setDebugging(SettingsActivity.this, debuggingSwitch.isChecked());

            Utils.updateFloatingServicePrefs(SettingsActivity.this);
        }
    });

    beepSwitch.setChecked(PrefUtils.isBeepAlertEnabled(this));
    beepSwitch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PrefUtils.setBeepAlertEnabled(SettingsActivity.this, beepSwitch.isChecked());
        }
    });
    testBeepButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Utils.playBeep();
        }
    });

    androidAutoSwitch.setChecked(PrefUtils.isAutoIntegrationEnabled(this));
    androidAutoContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (BuildConfig.FLAVOR.equals("play")) {
                Snackbar.make(findViewById(android.R.id.content), R.string.auto_not_available,
                        Snackbar.LENGTH_LONG).show();
                return;
            }
            if (!androidAutoSwitch.isEnabled()) {
                return;
            }
            androidAutoSwitch.toggle();
            boolean checked = androidAutoSwitch.isChecked();
            if (checked) {
                new MaterialDialog.Builder(SettingsActivity.this)
                        .content(R.string.android_auto_instruction_dialog).positiveText(android.R.string.ok)
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                PrefUtils.setAutoIntegrationEnabled(SettingsActivity.this, true);
                            }
                        }).show();
            } else {
                PrefUtils.setAutoIntegrationEnabled(SettingsActivity.this, checked);
            }
        }
    });

    invalidateStates();

    if (BuildConfig.VERSION_CODE > PrefUtils.getVersionCode(this) && !PrefUtils.isFirstRun(this)) {
        showChangelog();
    }

    billingProcessor = new BillingProcessor(this, getString(R.string.play_license_key),
            new BillingProcessor.IBillingHandler() {
                @Override
                public void onProductPurchased(String productId, TransactionDetails details) {
                    PrefUtils.setSupported(SettingsActivity.this, true);
                    if (Arrays.asList(PURCHASES).contains(productId))
                        billingProcessor.consumePurchase(productId);
                }

                @Override
                public void onPurchaseHistoryRestored() {

                }

                @Override
                public void onBillingError(int errorCode, Throwable error) {
                    if (errorCode != 110) {
                        Snackbar.make(findViewById(android.R.id.content),
                                "Billing error: code = " + errorCode + ", error: "
                                        + (error != null ? error.getMessage() : "?"),
                                Snackbar.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onBillingInitialized() {
                    billingProcessor.loadOwnedPurchasesFromGoogle();
                }
            });

    PrefUtils.setFirstRun(this, false);
    PrefUtils.setVersionCode(this, BuildConfig.VERSION_CODE);
}

From source file:org.deviceconnect.android.deviceplugin.host.camera.CameraOverlay.java

/**
 * ??????.//  w  w  w.  j  av  a2  s.  c o  m
 * @param resultReceiver ????
 */
@TargetApi(23)
private void checkOverlayDrawingCapability(@NonNull final ResultReceiver resultReceiver) {
    if (Settings.canDrawOverlays(mContext)) {
        resultReceiver.send(Activity.RESULT_OK, null);
    } else {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + mContext.getPackageName()));
        IntentHandlerActivity.startActivityForResult(mContext, intent, new ResultReceiver(mHandler) {
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                if (Settings.canDrawOverlays(mContext)) {
                    resultReceiver.send(Activity.RESULT_OK, null);
                } else {
                    resultReceiver.send(Activity.RESULT_CANCELED, null);
                }
            }
        });
    }
}

From source file:apps.junkuvo.alertapptowalksafely.MainActivity.java

private void start(View v) {
    if (checkOverlayPermission()) {
        mAlertService.startOverlay();//from w  w  w  . j a  v a2  s .  com
        startProcesses(v);
    } else {
        /** if not construct intent to request permission */
        Intent i = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        //            i.setFlags(FLAG_ACTIVITY_NEW_TASK);
        /** request permission via start activity for result */
        startActivityForResult(i, CHECK_OVERLAY_PERMISSION_REQUEST_CODE);
    }
}

From source file:co.taqat.call.LinphoneActivity.java

public boolean checkAndRequestOverlayPermission() {
    Log.i("[Permission] Draw overlays permission is "
            + (Compatibility.canDrawOverlays(this) ? "granted" : "denied"));
    if (!Compatibility.canDrawOverlays(this)) {
        Log.i("[Permission] Asking for overlay");
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, PERMISSIONS_REQUEST_OVERLAY);
        return false;
    }// www . j a  v a 2 s  . c o  m
    return true;
}