Example usage for android.provider Settings canDrawOverlays

List of usage examples for android.provider Settings canDrawOverlays

Introduction

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

Prototype

public static boolean canDrawOverlays(Context context) 

Source Link

Document

Checks if the specified context can draw on top of other apps.

Usage

From source file:com.rks.musicx.misc.utils.permissionManager.java

public static void widgetPermission(@NonNull Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(activity)) {
        MaterialDialog.Builder dialog = new MaterialDialog.Builder(activity).title(R.string.permissions_title)
                .content(R.string.draw_over_permissions_message).positiveText(R.string.btn_continue)
                .negativeText(R.string.btn_cancel).autoDismiss(true)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override/*from   w  ww .j av  a  2 s  .co  m*/
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                    Uri.fromParts("package", activity.getPackageName(), null));
                            if (Helper.isActivityPresent(activity, intent)) {
                                activity.startActivityForResult(intent, OVERLAY_REQ);
                            } else {
                                Toast.makeText(activity,
                                        "No app found to handle floating widget enable permission",
                                        Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }).onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        Toast.makeText(activity, R.string.toast_permissions_not_granted, Toast.LENGTH_SHORT)
                                .show();
                        dialog.dismiss();
                    }
                }).neutralText("Never show again").onNeutral(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        Extras.getInstance().setWidgetTrack(true);
                        dialog.dismiss();
                    }
                });
        if (activity.hasWindowFocus() || !activity.isFinishing()) {
            dialog.show();
        }
    }
}

From source file:com.example.captain_miao.grantap.utils.PermissionUtils.java

@TargetApi(value = Build.VERSION_CODES.M)
public static List<String> findDeniedPermissions(Activity activity, String... permission) {
    List<String> denyPermissions = new ArrayList<>();
    for (String value : permission) {
        // CommonsWare's blog post:https://commonsware.com/blog/2015/08/17/random-musings-android-6p0-sdk.html
        //support SYSTEM_ALERT_WINDOW,WRITE_SETTINGS
        if (isOverMarshmallow() && value.equals(Manifest.permission.SYSTEM_ALERT_WINDOW)) {
            if (!Settings.canDrawOverlays(activity)) {
                denyPermissions.add(value);
            }//  ww  w  .j  a v  a  2 s .  co  m
        } else if (isOverMarshmallow() && value.equals(Manifest.permission.WRITE_SETTINGS)) {
            if (!Settings.System.canWrite(activity)) {
                denyPermissions.add(value);
            }
        }
        //else if (activity.checkSelfPermission(value) != PackageManager.PERMISSION_GRANTED) {
        //    denyPermissions.add(value);
        //}
        else if (PermissionChecker.checkSelfPermission(activity, value) != PackageManager.PERMISSION_GRANTED) {
            denyPermissions.add(value);
        }
    }
    return denyPermissions;
}

From source file:trumeet.keyguard.utils.MaskWindowUtils.java

@Nullable
private View showKeyguardView(boolean showSettings) {
    if (Build.VERSION.SDK_INT >= 23) {
        if (!Settings.canDrawOverlays(mContext)) {
            if (!showSettings)
                return null;
            Intent i = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            mContext.startActivity(i);/*from   w w w .  ja  va 2  s .co  m*/
            return null;
        }
    }
    View keyguardView = LayoutInflater.from(mContext).inflate(R.layout.layout_keyguard, null);
    WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
    wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    wmParams.format = PixelFormat.TRANSPARENT;
    wmParams.flags = WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
            | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
    wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    wmParams.height = WindowManager.LayoutParams.MATCH_PARENT;
    mManager.addView(keyguardView, wmParams);
    mContext.startActivity(new Intent(mContext, KeyguardLiveActivity.class));
    return keyguardView;
}

From source file:com.farmerbb.taskbar.service.NotificationService.java

@TargetApi(Build.VERSION_CODES.M)
@Override// w  w w. j a  va  2  s  .c o m
public void onCreate() {
    super.onCreate();

    SharedPreferences pref = U.getSharedPreferences(this);
    if (pref.getBoolean("taskbar_active", false)) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(this)) {
            isHidden = U.getSharedPreferences(this).getBoolean("is_hidden", false);
            String label = getString(isHidden ? R.string.action_show : R.string.action_hide);

            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_CANCEL_CURRENT);
            PendingIntent receiverIntent = PendingIntent.getBroadcast(this, 0,
                    new Intent("com.farmerbb.taskbar.SHOW_HIDE_TASKBAR"), PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent receiverIntent2 = PendingIntent.getBroadcast(this, 0,
                    new Intent("com.farmerbb.taskbar.QUIT"), PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(pref.getBoolean("app_drawer_icon", false) ? R.drawable.ic_system
                            : R.drawable.ic_allapps)
                    .setContentIntent(contentIntent).setContentTitle(getString(R.string.taskbar_is_active))
                    .setContentText(getString(R.string.click_to_open_settings))
                    .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
                    .addAction(0, label, receiverIntent)
                    .addAction(0, getString(R.string.action_quit), receiverIntent2)
                    .setPriority(Notification.PRIORITY_MIN).setShowWhen(false).setOngoing(true);

            startForeground(8675309, mBuilder.build());

            LocalBroadcastManager.getInstance(this)
                    .sendBroadcast(new Intent("com.farmerbb.taskbar.UPDATE_SWITCH"));

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
                TileService.requestListeningState(this, new ComponentName(BuildConfig.APPLICATION_ID,
                        QuickSettingsTileService.class.getName()));

            if (!isHidden) {
                registerReceiver(userForegroundReceiver, new IntentFilter(Intent.ACTION_USER_FOREGROUND));
                registerReceiver(userBackgroundReceiver, new IntentFilter(Intent.ACTION_USER_BACKGROUND));
            }
        } else {
            pref.edit().putBoolean("taskbar_active", false).apply();

            stopSelf();
        }
    } else
        stopSelf();
}

From source file:com.canking.sdcardhelper.PermissionUtils.java

/**
 * ??//from   w  ww.  ja  v a2 s.  c om
 *
 * @param cxt
 * @param req
 * @return
 */
@TargetApi(23)
public static <C extends Context> boolean checkSettingAlertPermission(Activity cxt, int req, C t) {
    Activity activity = (Activity) cxt;
    if (!Settings.canDrawOverlays(activity.getBaseContext())) {
        Log.i(TAG, "Setting not permission");

        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + activity.getPackageName()));
        activity.startActivityForResult(intent, req);
        return false;
    }

    return true;
}

From source file:com.androidzeitgeist.webcards.overlay.OverlayService.java

private boolean canDrawOverlays() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return Settings.canDrawOverlays(this);
    }/*from  w  ww . j  a va  2  s . c o m*/

    return true;
}

From source file:com.youshe.yangyi.common_app.util.PermissionUtils.java

/**
 * ??/*from   ww w  .j a  va 2s . c om*/
 *
 * @param cxt
 * @param req
 * @return
 */
@TargetApi(23)
public static boolean checkSettingAlertPermission(Object cxt, int req) {
    if (cxt instanceof Activity) {
        Activity activity = (Activity) cxt;
        if (!Settings.canDrawOverlays(activity.getBaseContext())) {
            Log.i(TAG, "Setting not permission");

            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + activity.getPackageName()));
            activity.startActivityForResult(intent, req);
            return false;
        }
    } else if (cxt instanceof Fragment) {
        Fragment fragment = (Fragment) cxt;
        if (!Settings.canDrawOverlays(fragment.getActivity())) {
            Log.i(TAG, "Setting not permission");

            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + fragment.getActivity().getPackageName()));
            fragment.startActivityForResult(intent, req);
            return false;
        }
    } else {
        throw new RuntimeException("cxt is net a activity or fragment");
    }

    return true;
}

From source file:com.hengtiansoft.ecommerce.library.base.util.PermissionUtils.java

/**
 * ??//from ww  w  .  j  ava 2s . c  o  m
 *
 * @param cxt
 * @param req
 * @return
 */
@TargetApi(23)
public static boolean checkSettingAlertPermission(Object cxt, int req) {
    if (cxt instanceof Activity) {
        Activity activity = (Activity) cxt;
        if (!Settings.canDrawOverlays(activity.getBaseContext())) {
            LogUtil.i("Setting not permission");

            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + activity.getPackageName()));
            activity.startActivityForResult(intent, req);
            return false;
        }
    } else if (cxt instanceof Fragment) {
        Fragment fragment = (Fragment) cxt;
        if (!Settings.canDrawOverlays(fragment.getActivity())) {
            LogUtil.i("Setting not permission");

            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + fragment.getActivity().getPackageName()));
            fragment.startActivityForResult(intent, req);
            return false;
        }
    } else {
        throw new RuntimeException("cxt is net a activity or fragment");
    }

    return true;
}

From source file:com.crea_si.eviacam.service.SplashActivity.java

/**
 * Check permissions and ask user when necessary
 *
 * @return true if the user granted all permissions, false when some permissions are
 *         missing and therefore need to wait for the user
 *///from w  ww .ja  v a  2s .  c om
private boolean checkPermissions() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        // Nothing to do for versions below API 23
        return true;
    }

    // Camera permission
    final boolean hasCameraPerm = ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;

    if (!hasCameraPerm) {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA },
                CAMERA_PERMISSION_REQUEST);
        return false;
    }

    // Manage overlay permission
    final boolean hasManageOverlayPerm = Settings.canDrawOverlays(this);

    if (!hasManageOverlayPerm) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, MANAGE_OVERLAY_PERMISSION_REQUEST);
        return false;
    }

    return true;
}

From source file:com.lvbo.template.common.Utils.PermissionUtils.java

/**
 * ??/*from w  w  w .ja v  a2 s  .c  o  m*/
 *
 * @param cxt
 * @param req
 * @return
 */
@TargetApi(23)
public static boolean checkSettingAlertPermission(Object cxt, int req) {
    if (cxt instanceof Activity) {
        Activity activity = (Activity) cxt;
        if (!Settings.canDrawOverlays(activity.getBaseContext())) {
            Log.i(TAG, "Setting not permission");

            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + activity.getPackageName()));
            activity.startActivityForResult(intent, req);
            return false;
        }
    } else if (cxt instanceof Fragment) {
        Fragment fragment = (Fragment) cxt;
        if (!Settings.canDrawOverlays(fragment.getActivity())) {
            Log.i(TAG, "Setting not permission");

            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + fragment.getActivity().getPackageName()));
            fragment.startActivityForResult(intent, req);
            Context c;
            return false;
        }
    } else {
        throw new RuntimeException("cxt is net a activity or fragment");
    }

    return true;
}